blob: 10ea792ee69daf7bdc5fc6b4b654476f92a89ade (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
---
name: Write a file incrementally
---
Bun provides an API for incrementally writing to a file. This is useful for writing large files, or for writing to a file over a long period of time.
Call `.writer()` on a `BunFile` to retrieve a `FileSink` instance. This instance can be used to efficiently buffer data and periodically "flush" it to disk. You can write & flush many times.
```ts
const file = Bun.file("/path/to/file.txt");
const writer = file.writer();
writer.write("lorem");
writer.write("ipsum");
writer.write("dolor");
writer.flush();
// continue writing & flushing
```
---
The `.write()` method can accept strings or binary data.
```ts
w.write("hello");
w.write(Buffer.from("there"));
w.write(new Uint8Array([0, 255, 128]));
writer.flush();
```
---
The `FileSink` will also auto-flush when its internal buffer is full. You can configure the buffer size with the `highWaterMark` option.
```ts
const file = Bun.file("/path/to/file.txt");
const writer = file.writer({ highWaterMark: 1024 * 1024 }); // 1MB
```
---
When you're done writing to the file, call `.end()` to auto-flush the buffer and close the file.
```ts
writer.end();
```
---
Full documentation: [FileSink](/docs/api/file-io#incremental-writing-with-filesink).
|