aboutsummaryrefslogtreecommitdiff
path: root/docs/api/file.md
blob: 16e5359017d2b1101e7e4997c659dd37d5396e16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Bun.js has fast paths for common use cases that make Web APIs live up to the performance demands of servers and CLIs.

`Bun.file(path)` returns a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) that represents a lazily-loaded file.

When you pass a file blob to `Bun.write`, Bun automatically uses a faster system call:

```js
const blob = Bun.file("input.txt");
await Bun.write("output.txt", blob);
```

On Linux, this uses the [`copy_file_range`](https://man7.org/linux/man-pages/man2/copy_file_range.2.html) syscall and on macOS, this becomes `clonefile` (or [`fcopyfile`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/copyfile.3.html)).

`Bun.write` also supports [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects. It automatically converts to a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).

```js
// Eventually, this will stream the response to disk but today it buffers
await Bun.write("index.html", await fetch("https://example.com"));
```
' width='13' height='13' alt='Gravatar' /> Jarred Sumner 7-278/+363 2022-03-24Update response.zigGravatar Jarred Sumner 1-6/+6 2022-03-24[bun.js] Enable SharedArrayBuffer and expose internal LoaderGravatar Jarred Sumner 1-7/+16 2022-03-23handle bodies of 0 length betterGravatar Jarred Sumner 6-42/+86 2022-03-23Update server.zigGravatar Jarred Sumner 1-1/+2 2022-03-23Implement Request body support!Gravatar Jarred Sumner 3-30/+132 2022-03-23Support `Request.headers` and `Request.url` in http serverGravatar Jarred Sumner 5-30/+134 2022-03-23Update MakefileGravatar Jarred Sumner 1-1/+1 2022-03-23:scissors: testGravatar Jarred Sumner 1-14/+2 2022-03-23[bun.js] Bun.write for macOSGravatar Jarred Sumner 14-76/+232 2022-03-23[bun.js] Implement Bun.write()Gravatar Jarred SUmner 11-63/+959 2022-03-22Handle integer sizes greater than i32Gravatar Jarred SUmner 14-74/+142 2022-03-22Linux-specific tweaksGravatar Jarred SUmner 6-43/+52 2022-03-22`Response.file` -> `Bun.file`Gravatar Jarred Sumner 3-72/+65 2022-03-21sendfile worksGravatar Jarred Sumner 7-62/+174 2022-03-21[bun.js] 2/? Implement `Response.file`, sendfile editionGravatar Jarred Sumner 8-161/+506 2022-03-21[bun.js] 1/? Implement `Response.file`Gravatar Jarred Sumner 13-103/+1103