fs-write-stream-atomic-modern is a compatibility-focused maintained fork of
fs-write-stream-atomic.
It provides the same CommonJS Writable-stream API for supported modern Node.js versions, while replacing safe-to-remove legacy dependencies and modernizing the tests, type declarations, and cross-platform CI.
npm install fs-write-stream-atomic-modernNode.js 22 or newer is required.
const fs = require('node:fs')
const fsWriteStreamAtomic = require('fs-write-stream-atomic-modern')
const destination = fsWriteStreamAtomic('output.txt')
fs.createReadStream('input.txt').pipe(destination)
destination.on('finish', () => {
// The temporary file has closed and the rename has completed. At this point
// output.txt contains all bytes accepted by this stream.
})The export works both as a function and as a constructor:
const first = fsWriteStreamAtomic('first.txt')
const second = new fsWriteStreamAtomic('second.txt')filenameaccepts the path values supported byfs.WriteStream.options.chownmay contain numericuidandgidvalues. Ownership is applied to the temporary file before rename.options.encodingdefaults toutf8.options.modedefaults to0o666.options.flagsdefaults tow.- Other Writable and filesystem WriteStream options are forwarded for compatibility.
The returned object is a Writable stream. It supports write(), end(),
pipe() as a destination, destroy(), and the normal EventEmitter methods.
This package intentionally delays its outer finish event. The sequence on a
successful write is:
write temporary file
→ close temporary file
→ optional chown
→ rename temporary file over destination
→ finish
→ asynchronous boundary
→ close
Therefore the destination is ready when finish fires. On an operation error,
the stream attempts to remove its temporary file and emits error before
close; it does not emit finish.
The package writes beside the destination and then asks the filesystem to
replace the destination with rename(). On filesystems where same-filesystem
rename replacement is atomic, readers do not observe a partially streamed
destination.
This is not a power-loss durability guarantee. The package does not call
fsync(), does not provide a cross-filesystem copy fallback, and cannot extend
local rename guarantees to every network filesystem.
After this package is published, npm aliases can let an application retain its
old require('fs-write-stream-atomic') calls:
{
"dependencies": {
"fs-write-stream-atomic": "npm:fs-write-stream-atomic-modern@^1.0.10"
}
}See COMPATIBILITY.md for observable behavior and known differences, and MODERNIZATION.md for the dependency and tooling decisions.
The original implementation is by Isaac Z. Schlueter and contributors. This fork retains the ISC license and original copyright notice.