Skip to content

Commit

Permalink
Adding GZIP support nwutils#74
Browse files Browse the repository at this point in the history
Addresses: nwutils#74
  • Loading branch information
chrisronline committed Feb 19, 2015
1 parent 7949af8 commit 5f5d4b3
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions app/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var ncp = require('ncp');
var del = require('del');
var semver = require('semver');
var zlib = require('zlib');
var gui = global.window.nwDispatcher.requireNwGui();

var platform = process.platform;
Expand Down Expand Up @@ -68,30 +69,38 @@
updater.prototype.download = function(cb, newManifest){
var manifest = newManifest || this.manifest;
var url = manifest.packages[platform].url;
var filename = path.basename(url);
var destinationPath = path.join(os.tmpdir(), filename);

var pkg = request(url, function(err, response){
if(err){
cb(err);
}
if(response && (response.statusCode < 200 || response.statusCode >= 300)){
pkg.abort();
return cb(new Error(response.statusCode));
}
if(err){
cb(err);
}
if(response && (response.statusCode < 200 || response.statusCode >= 300)){
pkg.abort();
return cb(new Error(response.statusCode));
}
});
pkg.on('response', function(response){
if(response && response.headers && response.headers['content-length']){


pkg.on('response', function(response) {
if (response && response.headers) {
if (response.headers['content-length']) {
pkg['content-length'] = response.headers['content-length'];
}
});
var filename = path.basename(url),
destinationPath = path.join(os.tmpdir(), filename);
// download the package to template folder
fs.unlink(path.join(os.tmpdir(), filename), function(){
pkg.pipe(fs.createWriteStream(destinationPath));
pkg.resume();

fs.unlink(destinationPath, function(err) {
var destination = fs.createWriteStream(destinationPath);
if (response.headers['content-encoding'] === 'gzip') {
response.pipe(zlib.createGunzip()).pipe(destination)
} else {
response.pipe(destination);
}
});
}
});
pkg.on('error', cb);
pkg.on('end', appDownloaded);
pkg.pause();

function appDownloaded(){
process.nextTick(function(){
Expand Down

0 comments on commit 5f5d4b3

Please sign in to comment.