fs-write-stream-atomic
Advanced tools
Comparing version 1.0.6 to 1.0.7
27
index.js
@@ -34,2 +34,4 @@ var fs = require('graceful-fs') | ||
this.__atomicTmp = getTmpname(path) | ||
this.__atomicFinished = false | ||
this.__atomicMoved = false | ||
this.__atomicStream = fs.WriteStream(this.__atomicTmp, options) | ||
@@ -55,2 +57,22 @@ this.__atomicStream.on('error', handleError.bind(this)) | ||
function finish () { | ||
if (!this.__atomicFinished) return | ||
if (!this.__atomicMoved) return | ||
PassThrough.prototype.emit.call(this, 'finish') | ||
process.nextTick(function () { | ||
this.emit('close') | ||
}.bind(this)) | ||
} | ||
WriteStreamAtomic.prototype.emit = function (event) { | ||
// We'll emit this ourselves, as we need to hold off on emitting it | ||
// until after we've completed putting the final file into place. | ||
// To do otherwise creats a race between finish and close ;_; | ||
if (event === 'finish') { | ||
this.__atomicFinished = true | ||
return finish.call(this) | ||
} | ||
return PassThrough.prototype.emit.apply(this, arguments) | ||
} | ||
WriteStreamAtomic.prototype._flush = function (cb) { | ||
@@ -75,7 +97,6 @@ var writeStream = this | ||
cleanup(err) | ||
process.nextTick(function () { | ||
writeStream.emit('close') | ||
}) | ||
writeStream.__atomicMoved = true | ||
finish.call(writeStream) | ||
}) | ||
} | ||
} |
{ | ||
"name": "fs-write-stream-atomic", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Like `fs.createWriteStream(...)`, but atomic.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,6 +1,13 @@ | ||
var fs = require('graceful-fs') | ||
var test = require('tap').test | ||
var path = require('path') | ||
var writeStream = require('../index.js') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var rename = fs.rename | ||
fs.rename = function (from, to, cb) { | ||
setTimeout(function () { | ||
rename(from, to, cb) | ||
}, 100) | ||
} | ||
test('basic', function (t) { | ||
@@ -7,0 +14,0 @@ // open 10 write streams to the same file. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
15614
14
218
0