diff options
author | 2023-07-26 14:59:39 -0700 | |
---|---|---|
committer | 2023-07-26 14:59:39 -0700 | |
commit | 4c89c60867591b50e0b31bf5009fd5ad6a3cebe1 (patch) | |
tree | fc1d2f47309c0345a850933496baa40d94bfdcbb /docs/guides/write-file/filesink.md | |
parent | 6bfee02301a2e2a0b79339974af0445eb5a2688f (diff) | |
download | bun-4c89c60867591b50e0b31bf5009fd5ad6a3cebe1.tar.gz bun-4c89c60867591b50e0b31bf5009fd5ad6a3cebe1.tar.zst bun-4c89c60867591b50e0b31bf5009fd5ad6a3cebe1.zip |
Add files (#3826)
Diffstat (limited to 'docs/guides/write-file/filesink.md')
-rw-r--r-- | docs/guides/write-file/filesink.md | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/docs/guides/write-file/filesink.md b/docs/guides/write-file/filesink.md new file mode 100644 index 000000000..10ea792ee --- /dev/null +++ b/docs/guides/write-file/filesink.md @@ -0,0 +1,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). |