Skip to content

Commit

Permalink
Merge pull request #11 from tschaub/updates
Browse files Browse the repository at this point in the history
Dev dependency updates and lint removal
  • Loading branch information
tschaub committed Jun 28, 2017
2 parents 74723c8 + 87e9cc9 commit 854d44f
Show file tree
Hide file tree
Showing 16 changed files with 1,954 additions and 515 deletions.
20 changes: 0 additions & 20 deletions .jshintrc

This file was deleted.

10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: node_js

node_js:
- "5"
- "6"
- "7"
before_install:
- "npm install -g npm"
- "8"

cache:
directories:
- node_modules
62 changes: 0 additions & 62 deletions gruntfile.js

This file was deleted.

12 changes: 6 additions & 6 deletions lib/bower-util/createError.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
var mout = require('mout');

function createError(msg, code, props) {
var err = new Error(msg);
err.code = code;
var err = new Error(msg);
err.code = code;

if (props) {
mout.object.mixIn(err, props);
}
if (props) {
mout.object.mixIn(err, props);
}

return err;
return err;
}

module.exports = createError;
200 changes: 104 additions & 96 deletions lib/bower-util/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,104 +28,112 @@ var retry = require('retry');
var fs = require('graceful-fs');
var createError = require('./createError');

var errorCodes = [
'EADDRINFO',
'ETIMEDOUT',
'ECONNRESET',
'ESOCKETTIMEDOUT'
];
var errorCodes = ['EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT'];

function download(url, file, options) {
var operation;
var response;
var deferred = Q.defer();
var progressDelay = 8000;

options = mout.object.mixIn({
retries: 5,
factor: 2,
minTimeout: 1000,
maxTimeout: 35000,
randomize: true
}, options || {});

// Retry on network errors
operation = retry.operation(options);
operation.attempt(function () {
var req;
var writeStream;
var contentLength;
var bytesDownloaded = 0;

req = progress(request(url, options), {
delay: progressDelay
})
.on('response', function (res) {
var status = res.statusCode;

if (status < 200 || status >= 300) {
return deferred.reject(createError('Status code of ' + status, 'EHTTP'));
}

response = res;
contentLength = Number(res.headers['content-length']);
})
.on('data', function (data) {
bytesDownloaded += data.length;
})
.on('progress', function (state) {
deferred.notify(state);
})
.on('end', function () {
// Check if the whole file was downloaded
// In some unstable connections the ACK/FIN packet might be sent in the
// middle of the download
// See: https://github.com/joyent/node/issues/6143
if (contentLength && bytesDownloaded < contentLength) {
req.emit('error', createError('Transfer closed with ' + (contentLength - bytesDownloaded) + ' bytes remaining to read', 'EINCOMPLETE'));
}
})
.on('error', function (error) {
var timeout = operation._timeouts[0];

// Reject if error is not a network error
if (errorCodes.indexOf(error.code) === -1) {
return deferred.reject(error);
}

// Next attempt will start reporting download progress immediately
progressDelay = 0;

// Check if there are more retries
if (operation.retry(error)) {
// Ensure that there are no more events from this request
req.removeAllListeners();
req.on('error', function () {});
// Ensure that there are no more events from the write stream
writeStream.removeAllListeners();
writeStream.on('error', function () {});

return deferred.notify({
retry: true,
delay: timeout,
error: error
});
}

// No more retries, reject!
deferred.reject(error);
});

// Pipe read stream to write stream
writeStream = req
.pipe(fs.createWriteStream(file))
.on('error', deferred.reject)
.on('close', function () {
deferred.resolve(response);
});
});

return deferred.promise;
var operation;
var response;
var deferred = Q.defer();
var progressDelay = 8000;

options = mout.object.mixIn(
{
retries: 5,
factor: 2,
minTimeout: 1000,
maxTimeout: 35000,
randomize: true
},
options || {}
);

// Retry on network errors
operation = retry.operation(options);
operation.attempt(function() {
var req;
var writeStream;
var contentLength;
var bytesDownloaded = 0;

req = progress(request(url, options), {
delay: progressDelay
})
.on('response', function(res) {
var status = res.statusCode;

if (status < 200 || status >= 300) {
return deferred.reject(
createError('Status code of ' + status, 'EHTTP')
);
}

response = res;
contentLength = Number(res.headers['content-length']);
})
.on('data', function(data) {
bytesDownloaded += data.length;
})
.on('progress', function(state) {
deferred.notify(state);
})
.on('end', function() {
// Check if the whole file was downloaded
// In some unstable connections the ACK/FIN packet might be sent in the
// middle of the download
// See: https://github.com/joyent/node/issues/6143
if (contentLength && bytesDownloaded < contentLength) {
req.emit(
'error',
createError(
'Transfer closed with ' +
(contentLength - bytesDownloaded) +
' bytes remaining to read',
'EINCOMPLETE'
)
);
}
})
.on('error', function(error) {
var timeout = operation._timeouts[0];

// Reject if error is not a network error
if (errorCodes.indexOf(error.code) === -1) {
return deferred.reject(error);
}

// Next attempt will start reporting download progress immediately
progressDelay = 0;

// Check if there are more retries
if (operation.retry(error)) {
// Ensure that there are no more events from this request
req.removeAllListeners();
req.on('error', function() {});
// Ensure that there are no more events from the write stream
writeStream.removeAllListeners();
writeStream.on('error', function() {});

return deferred.notify({
retry: true,
delay: timeout,
error: error
});
}

// No more retries, reject!
deferred.reject(error);
});

// Pipe read stream to write stream
writeStream = req
.pipe(fs.createWriteStream(file))
.on('error', deferred.reject)
.on('close', function() {
deferred.resolve(response);
});
});

return deferred.promise;
}

module.exports = download;
Loading

0 comments on commit 854d44f

Please sign in to comment.