Skip to content

Commit

Permalink
update bindings cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Jun 5, 2014
1 parent a847bef commit de39c9e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 78 deletions.
84 changes: 7 additions & 77 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,8 @@

var step = require('step'),
fs = require('fs'),
path = require('path');

function mkdirP ( p, mode, made ) {
if (mode === undefined) {
mode = 0777 & (~process.umask());
}
if (!made) made = null;

if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);

try {
fs.mkdirSync(p, mode);
made = made || p;
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
made = mkdirP(path.dirname(p), mode, made);
mkdirP(p, mode, made);
break;

// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
var stat;
try {
stat = fs.statSync(p);
}
catch (err1) {
throw err0;
}
if (!stat.isDirectory()) throw err0;
break;
}
}

return made;
}
path = require('path'),
util = require('./util.js');

// Try to download binding.node from github.
function download() {
Expand Down Expand Up @@ -116,51 +78,19 @@ function download() {
}

// start to download.
var options = url.parse( downloadUrl + modPath ),
dest = './build/Release/binding.node',
client;
var dest = './build/Release/binding.node';

if ( fs.existsSync( dest ) ) {
console.log( 'The binding.node file exist, skip download.' );
done( false );
return;
}

console.log('Downloading', options.href );
client = https.get( options, function( res ) {
var count = 0,
notifiedCount = 0,
outFile;

if ( res.statusCode === 200 ) {
mkdirP( path.dirname( dest ) );
outFile = fs.openSync( dest, 'w' );

res.on('data', function( data ) {
fs.writeSync(outFile, data, 0, data.length, null);
count += data.length;
util.download({
remote: downloadUrl + modPath,
dest: './build/Release/binding.node'
}, done);

if ( (count - notifiedCount) > 1024 * 1024 ) {
console.log('Received ' + Math.floor( count / 1024 ) + 'K...');
notifiedCount = count;
}
});

res.addListener('end', function() {
console.log('Received ' + Math.floor(count / 1024) + 'K total.');
fs.closeSync( outFile );
done( false );
});

} else {
client.abort()
console.error('Error requesting archive');
done( true );
}
}).on('error', function(e) {
console.error( e.message );
done( true, e );
});
} else {
done( true );
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"dependencies": {
"step": "0.0.5"
},
"bindingsCDN": "https://raw.githubusercontent.com/fis-dev/fis-sass/master/bindings/"
"bindingsCDN": "http://git.oschina.net/2betop/static/raw/master/fis-sass/bindings/"
}
95 changes: 95 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
var _ = module.exports;
var path = require('path');
var fs = require('fs');

_.mkdirp = function mkdirP ( p, mode, made ) {
if (mode === undefined) {
mode = 0777 & (~process.umask());
}
if (!made) made = null;

if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);

try {
fs.mkdirSync(p, mode);
made = made || p;
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
made = mkdirP(path.dirname(p), mode, made);
mkdirP(p, mode, made);
break;

// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
var stat;
try {
stat = fs.statSync(p);
}
catch (err1) {
throw err0;
}
if (!stat.isDirectory()) throw err0;
break;
}
}

return made;
}

_.download = function(opt, done) {
var remote = opt.remote;
var dest = opt.dest;
var url = require('url');
var fs = require('fs');
var options = url.parse(remote);
var notifiedSize = opt.notifiedSize || 512 * 1024;
var http = options.protocol === 'https:' ? require('https') : require('http')
var client;

process.stdout.write('Downloading ' + remote + ' ...\n');

client = http.get( options, function( res ) {
var count = 0,
notifiedCount = 0,
outFile;

if ( res.statusCode === 200 ) {
_.mkdirp(path.dirname(dest));

outFile = fs.openSync( dest, 'w' );

res.on('data', function( data ) {
fs.writeSync(outFile, data, 0, data.length, null);
count += data.length;

if ( (count - notifiedCount) > notifiedSize ) {
process.stdout.write('Received ' + Math.floor( count / 1024 ) + 'K...\n');
notifiedCount = count;
}
});

res.addListener('end', function() {
process.stdout.write('Received ' + Math.floor(count / 1024) + 'K total.\n');
fs.closeSync( outFile );
done( false );
});

} else if (res.statusCode === 302 && res.headers.location) {
client.abort();
opt.remote = res.headers.location;

process.stdout.write('Redirct to ' + opt.remote + '\n');
_.download(opt, done);
} else {
client.abort();
done('Error requesting archive')
}
}).on('error', function(e) {
done(e.message || 'unkown error');
});
}

0 comments on commit de39c9e

Please sign in to comment.