1 var fs = require('graceful-fs')
2 var Writable = require('readable-stream').Writable
3 var util = require('util')
4 var MurmurHash3 = require('imurmurhash')
5 var iferr = require('iferr')
7 function murmurhex () {
8 var hash = MurmurHash3('')
9 for (var ii = 0; ii < arguments.length; ++ii) {
10 hash.hash(hash + arguments[ii])
16 function getTmpname (filename) {
17 return filename + '.' + murmurhex(__filename, process.pid, ++invocations)
20 var setImmediate = global.setImmediate || setTimeout
22 module.exports = WriteStreamAtomic
25 // 1. Write everything written to the stream to a temp file.
26 // 2. If there are no errors:
27 // a. moves the temp file into its final destination
28 // b. emits `finish` & `closed` ONLY after the file is
29 // fully flushed and renamed.
30 // 3. If there's an error, removes the temp file.
32 util.inherits(WriteStreamAtomic, Writable)
33 function WriteStreamAtomic (path, options) {
34 if (!(this instanceof WriteStreamAtomic)) {
35 return new WriteStreamAtomic(path, options)
37 Writable.call(this, options)
39 this.__atomicTarget = path
40 this.__atomicTmp = getTmpname(path)
42 this.__atomicChown = options && options.chown
44 this.__atomicClosed = false
46 this.__atomicStream = fs.WriteStream(this.__atomicTmp, options)
48 this.__atomicStream.once('open', handleOpen(this))
49 this.__atomicStream.once('close', handleClose(this))
50 this.__atomicStream.once('error', handleError(this))
53 // We have to suppress default finish emitting, because ordinarily it
54 // would happen as soon as `end` is called on us and all of the
55 // data has been written to our target stream. So we suppress
56 // finish from being emitted here, and only emit it after our
57 // target stream is closed and we've moved everything around.
58 WriteStreamAtomic.prototype.emit = function (event) {
59 if (event === 'finish') return this.__atomicStream.end()
60 return Writable.prototype.emit.apply(this, arguments)
63 WriteStreamAtomic.prototype._write = function (buffer, encoding, cb) {
64 var flushed = this.__atomicStream.write(buffer, encoding)
65 if (flushed) return cb()
66 this.__atomicStream.once('drain', cb)
69 function handleOpen (writeStream) {
70 return function (fd) {
71 writeStream.emit('open', fd)
75 function handleClose (writeStream) {
77 if (writeStream.__atomicClosed) return
78 writeStream.__atomicClosed = true
79 if (writeStream.__atomicChown) {
80 var uid = writeStream.__atomicChown.uid
81 var gid = writeStream.__atomicChown.gid
82 return fs.chown(writeStream.__atomicTmp, uid, gid, iferr(cleanup, moveIntoPlace))
87 function cleanup (err) {
88 fs.unlink(writeStream.__atomicTmp, function () {
89 writeStream.emit('error', err)
90 writeStream.emit('close')
93 function moveIntoPlace () {
94 fs.rename(writeStream.__atomicTmp, writeStream.__atomicTarget, iferr(cleanup, end))
97 // We have to use our parent class directly because we suppress `finish`
98 // events fired via our own emit method.
99 Writable.prototype.emit.call(writeStream, 'finish')
101 // Delay the close to provide the same temporal separation a physical
102 // file operation would have– that is, the close event is emitted only
103 // after the async close operation completes.
104 setImmediate(function () {
105 writeStream.emit('close')
110 function handleError (writeStream) {
111 return function (er) {
113 writeStream.emit('error', er)
114 writeStream.__atomicClosed = true
115 writeStream.emit('close')
117 function cleanupSync () {
119 fs.unlinkSync(writeStream.__atomicTmp)