Skip to content
This repository has been archived by the owner on Dec 24, 2018. It is now read-only.

Commit

Permalink
wget: Allow responses w/o content-length
Browse files Browse the repository at this point in the history
As is often the case for GitHub tarballs.
  • Loading branch information
TimothyGu committed Oct 6, 2015
1 parent 4234f62 commit a710272
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions wget.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,37 @@ function wget(uri, callback) {
}
var contentLength = parseInt(res.headers['content-length'], 10);
if (isNaN(contentLength)) {
console.log('Can\'t get \'content-length\'');
callback(null);
return;
console.warn('Can\'t get \'content-length\'');
} else {
console.log('Content length is %s', bytes(contentLength));
}
console.log('Content length is %s', bytes(contentLength));

var data = new Buffer(contentLength);
var data = [];
var offset = 0;

var start = Date.now();
console.log(''); // New line for ESC_UP_CLL
res.on('data', function (buf) {
buf.copy(data, offset);
data.push(buf);
offset += buf.length;
var use = Date.now() - start;
if (use === 0) {
use = 1;
}
console.log(ESC_UP_CLL + 'Download %d%, %s / %s, %s/s ...',
parseInt(offset / contentLength * 100, 10), bytes(offset), bytes(contentLength),
bytes(offset / use * 1000));
if (contentLength) {
console.log(ESC_UP_CLL + 'Download %d%, %s / %s, %s/s ...',
offset / contentLength * 100 | 0, bytes(offset), bytes(contentLength),
bytes(offset / use * 1000));
} else {
console.log(ESC_UP_CLL + 'Download %s, %s/s ...',
bytes(offset),
bytes(offset / use * 1000));
}
});

res.on('end', function () {
console.log('Donwload done');
callback(filename, data);
callback(filename, Buffer.concat(data, offset));
});
});

Expand Down

0 comments on commit a710272

Please sign in to comment.