Compare commits

...

3 Commits

Author SHA1 Message Date
Adam Stankiewicz
a8c395325f Make download.js tests work on Windows VM, fixes #1972
Followup:
https://github.com/npm/fs-write-stream-atomic
2015-11-16 01:57:20 +01:00
Adam Stankiewicz
c8219ce30d Update fs-write-stream-atomic to fork with timing fix
It turned out that removing files synchronously on windows can cause
issues. This commit makes Appveyor build for Windows little better.
2015-11-16 00:58:40 +01:00
Adam Stankiewicz
4e80f5107f Properly destroy read stream for downloads 2015-11-15 14:25:27 +01:00
4 changed files with 48 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ var mout = require('mout');
var retry = require('retry');
var createError = require('./createError');
var createWriteStream = require('fs-write-stream-atomic');
var destroy = require('destroy');
var errorCodes = [
'EADDRINFO',
@@ -60,6 +61,7 @@ function download(url, file, options) {
}
function fetch(url, file, options) {
var response;
var deferred = Q.defer();
@@ -76,7 +78,7 @@ function fetch(url, file, options) {
var status = res.statusCode;
if (status < 200 || status >= 300) {
return writeStream.emit('error', (createError('Status code of ' + status, 'EHTTP')));
return req.emit('error', (createError('Status code of ' + status, 'EHTTP')));
}
response = res;
@@ -106,19 +108,33 @@ function fetch(url, file, options) {
writeStream = createWriteStream(file);
// Pipe read stream to write stream
writeStream.on('error', function (error) {
writeStream.destroy();
deferred.reject(error);
if (req) destroy(req);
if (writeStream) destroy(writeStream);
// For some reason we need to wait on Windows for streams to be closed..
// TODO: Debug this and remove timeout
setTimeout(function () {
deferred.reject(error);
}, 10);
});
writeStream.on('finish', function () {
deferred.resolve(response);
if (req) destroy(req);
if (writeStream) destroy(writeStream);
// For some reason we need to wait on Windows for streams to be closed..
// TODO: Debug this and remove timeout
setTimeout(function () {
deferred.resolve(response);
}, 10);
});
// Pipe read stream to write stream
req.pipe(writeStream);
return deferred.promise;
}
module.exports = download;
module.exports = download;

5
npm-shrinkwrap.json generated
View File

@@ -2,7 +2,7 @@
"name": "bower",
"version": "1.6.5",
"npm-shrinkwrap-version": "5.4.1",
"node-version": "v4.1.1",
"node-version": "v0.10.40",
"dependencies": {
"abbrev": {
"version": "1.0.7",
@@ -284,7 +284,8 @@
},
"fs-write-stream-atomic": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.4.tgz",
"from": "fs-write-stream-atomic@git://github.com/sheerun/fs-write-stream-atomic#81caa0fad60125bf9a208b77ae3217119a29f474",
"resolved": "git://github.com/sheerun/fs-write-stream-atomic#81caa0fad60125bf9a208b77ae3217119a29f474",
"dependencies": {
"graceful-fs": {
"version": "4.1.2",

View File

@@ -27,7 +27,7 @@
"configstore": "^0.3.2",
"decompress-zip": "^0.1.0",
"destroy": "^1.0.3",
"fs-write-stream-atomic": "^1.0.4",
"fs-write-stream-atomic": "git://github.com/sheerun/fs-write-stream-atomic",
"fstream": "^1.0.3",
"fstream-ignore": "^1.0.2",
"github": "^0.2.3",

View File

@@ -24,13 +24,28 @@ describe('download', function () {
download('https://bower.io/package.tar.gz', destination, opts.downloadOpts)
.then(function () {
opts.expect();
deferred.resolve();
}, function () {
opts.expectError();
deferred.resolve();
})
.done();
try {
if (opts.expect) {
opts.expect();
deferred.resolve();
} else {
deferred.reject('Expected command to error, it succeeded.');
}
} catch (e) {
deferred.reject(e);
}
}, function (reason) {
try {
if (opts.expectError) {
opts.expectError();
deferred.resolve();
} else {
deferred.reject(reason);
}
} catch (e) {
deferred.reject(e);
}
});
return deferred.promise;
}