npm / fs-write-stream-atomic Goto Github PK
View Code? Open in Web Editor NEWLike `fs.createWriteStream(...)`, but atomic.
License: ISC License
Like `fs.createWriteStream(...)`, but atomic.
License: ISC License
Currently, if you're doing stuff using fsWriteStreamAtomic()
and cancels the process the temp file will remain in place. Optimally it'd clean up any temp files on exit.
Also, are there any reasons to not write the temp files to os.tmpdir()
?
It would be very useful to be able to set the location where the temp file is written to, could this be added as an option? And if it isn't specified it'll write to the default location where it currently writes.
When NodeJS is compiled for FIPS-compliance, fs-write-stream-atomic actually breaks NPM.
When NPM attempts to write the local cache file .cache.json using WriteStream from the library, it throws the following:
fips_md.c(146): OpenSSL internal error, assertion failed: Digest update previous FIPS forbidden algorithm error ignored
Aborted
Instead of hardcoding md5 hash for the temporary name, consider moving to something higher like sha1 at a minimum or check for FIPs compliance and make it variable. I tested by moving the function createhash() call to sha1 and it proceeds fine.
bower users are seeing a variety of issues since bumping the dependency on fs-write-stream-atomic up from 1.0.5 to 1.0.7:
bower/bower#2118 (known and proven)
bower/bower#2101 (suspected)
https://ci.appveyor.com/project/bodil/pulp/build/1.0.67 (suspected)
This is an example of the sorts of errors that are being seen, although there are several others mentioned. Note that they occur seemingly randomly, perhaps due to timing issues or an improperly handled async operation:
bower install
...
bower paper-menu#^1.0.0 extract archive.tar.gz
bower iron-validatable-behavior#^1.0.0 extract archive.tar.gz
bower platinum-https-redirect#^1.0.0 extract archive.tar.gz
events.js:85
throw er; // Unhandled 'error' event
^
Error: ENOENT, rename 'C:\Users\Ken\AppData\Local\Temp\mypc-Ken\bower\cefb8f774d98384ed30dec610f03a72d-174564-wZPTqe\archive.tar.gz.207942742'
at Error (native)
Downgrading to 1.0.5 resolves the issue (tested for bower/bower#2118)
Would be useful if this module could support appends { flags: 'a' }
. I'm not sure how this would be implemented though. I guess you'd have to copy the entire destination file each time?
When the readable stream source emit an error :
This file leaks and cannot be removed.
I've write a test to illustrate this issue :
var should = require('should');
var fs = require('fs');
var fsWriteStreamAtomic = require('fs-write-stream-atomic');
var stream = require('stream');
var util = require('util');
var uuid = require('node-uuid');
var path = require('path');
function ReadableStream(options) {
stream.Readable.call(this, options);
this.options = options;
this._total = options && options.total || 1000;
this._index = 1;
}
util.inherits(ReadableStream, stream.Readable);
ReadableStream.prototype._emitError = function() {
var self = this;
process.nextTick(function () {
self.emit('error', new Error('stoped stream'));
});
};
ReadableStream.prototype._read = function() {
var i = this._index++;
if (i > this._total) {
this.push(null);
} else if(i > this.options.stopAt) {
this._emitError();
} else {
var str = '' + i;
var buf = new Buffer(str, 'ascii');
this.push(buf);
}
};
var DIR = path.join(__dirname, 'samples');
var FILE_TO_WRITE = path.join(DIR, 'MyTestFile.txt');
describe('Fs-write-stream-atomic: ', function() {
it('works with no error', function(done){
var rs = new ReadableStream({total: 1000});
// A readable stream emit 1000 chars and finish with no error
var ws = fsWriteStreamAtomic(FILE_TO_WRITE);
rs.pipe(ws);
ws.on('finish', function(){
console.log('finished');
fs.unlinkSync(FILE_TO_WRITE);
fs.readdir(DIR, function(err, files){
if(!err){
for(var i=0; i< files.length; i++){
should(files[i]).not.containEql('MyTestFile.txt');
}
}
done();
});
});
});
it('leak temp file when source emit error', function(done){
var rs = new ReadableStream({total: 1000, stopAt:600});
// A readable stream emit error at 600 chars
var ws = fsWriteStreamAtomic(FILE_TO_WRITE);
rs.pipe(ws);
ws.on('finish', function(){
console.log('finished');
fs.unlinkSync(FILE_TO_WRITE);
done();
});
ws.on('error', function(err){
console.log('error write stream' + err);
done();
});
rs.on('error', function(err){
console.log('error readable stream' + err);
fs.readdir(DIR, function(err, files){
if(!err){
for(var i=0; i< files.length; i++){
should(files[i]).not.containEql('MyTestFile.txt');
}
}
done();
});
});
});
});
When I start the test.
npm test
Result :
Fs-write-stream-atomic:
finished
โ works with no error
error readable streamError: stoped stream
1) leak temp file when source emit error
1 passing (37ms)
1 failing
1) Fs-write-stream-atomic: leak temp file when source emit error:
Uncaught AssertionError: expected 'MyTestFile.txt.1272449043' not to contain 'MyTestFile.txt' (false negative fail)
at Assertion.fail (node_modules/should/lib/assertion.js:92:17)
at Assertion.Object.defineProperty.value [as containEql] (node_modules/should/lib/assertion.js:174:17)
fs-write-stream-atomic leave a file "MyTestFile.txt.1272449043" in the directory
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.