Mwriter
Bun

method

fs.promises.FileHandle.writer

options?: WriterOptions
): Writer;

Return a node:stream/iter writer backed by this file handle.

The writer supports both Symbol.asyncDispose and Symbol.dispose:

  • await using w = fh.writer() — if the writer is still open (no end() called), asyncDispose calls fail(). If end() is pending, it waits for it to complete.
  • using w = fh.writer() — calls fail() unconditionally.

The writeSync() and writevSync() methods enable the try-sync fast path used by stream/iter pipeTo(). When the reader's chunk size matches the writer's chunkSize, all writes in a pipeTo() pipeline complete synchronously with zero promise overhead.

This function is only available when the --experimental-stream-iter flag is enabled.

import { open } from 'node:fs/promises';
import { from, pipeTo } from 'node:stream/iter';
import { compressGzip } from 'node:zlib/iter';

// Async pipeline
const fh = await open('output.gz', 'w');
await pipeTo(from('Hello!'), compressGzip(), fh.writer({ autoClose: true }));

// Sync pipeline with limit
const src = await open('input.txt', 'r');
const dst = await open('output.txt', 'w');
const w = dst.writer({ limit: 1024 * 1024 }); // Max 1 MB
await pipeTo(src.pull({ autoClose: true }), w);
await w.end();
await dst.close();

Referenced types

interface WriterOptions

  • autoClose?: boolean

    Close the file handle when the writer ends or fails.

  • chunkSize?: number

    Maximum chunk size in bytes for synchronous write operations. Writes larger than this threshold fall back to async I/O. Set this to match the reader's chunkSize for optimal pipeTo() performance.

  • limit?: number

    Maximum number of bytes the writer will accept. Async writes (write(), writev()) that would exceed the limit reject with ERR_OUT_OF_RANGE. Sync writes (writeSync(), writevSync()) return false.

  • start?: number

    Byte offset to start writing at. When specified, writes use explicit positioning.

interface Writer