Skip to content

Commit

Permalink
Merge pull request #47 from cnpm/support-prebuild-binary-mirror
Browse files Browse the repository at this point in the history
feat: Auto set China mirror for prebuild binary packages
  • Loading branch information
dead-horse committed Mar 9, 2016
2 parents e6379c1 + fe0fb6b commit b6d35d1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
17 changes: 17 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const fs = require('mz/fs');
const parseArgs = require('minimist');
const utils = require('../lib/utils');
const config = require('../lib/config');
const get = require('../lib/get');
const installLocal = require('..').installLocal;
const installGlobal = require('..').installGlobal;

Expand Down Expand Up @@ -139,13 +140,29 @@ for (const key in argv) {
debug('argv: %j, env: %j', argv, env);

co(function*() {
let binaryMirrors = {};

if (inChina) {
const binaryMirrorUrl = registry + '/binary-mirror-config/latest';
try {
const res = yield get(binaryMirrorUrl, {
dataType: 'json',
followRedirect: true,
});
binaryMirrors = res.data.mirrors.china;
} catch (err) {
debug('Get %s error: %s', binaryMirrorUrl, err);
}
}

const config = {
root,
registry,
pkgs,
production,
cacheDir,
env,
binaryMirrors,
};
config.strictSSL = getStrictSSL();
// -g install to npm's global prefix
Expand Down
19 changes: 17 additions & 2 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,25 @@ function* download(pkg, options) {
yield checkShasumAndUngzip(ungzipDir, stream, pkg);
yield fs.writeFile(donefile, Date());

yield utils.addMetaToJSONFile(path.join(ungzipDir, 'package.json'), {
const pkgMeta = {
_from: `${pkg.name}@${pkg.version}`,
_resolved: pkg.dist.tarball,
});
};
const binaryMirror = options.binaryMirrors[pkg.name];
if (binaryMirror && pkg.scripts && pkg.scripts.install) {
// leveldown and sqlite3
if (/prebuild \-\-install/.test(pkg.scripts.install) ||
/prebuild \-\-download/.test(pkg.scripts.install) ||
/node\-pre\-gyp install/.test(pkg.scripts.install)) {
const newBinary = pkg.binary || {};
for (const key in binaryMirror) {
newBinary[key] = binaryMirror[key];
}
pkgMeta.binary = newBinary;
options.console.info(`[${pkg.name}@${pkg.version}] download from binary mirror: %j`, newBinary);
}
}
yield utils.addMetaToJSONFile(path.join(ungzipDir, 'package.json'), pkgMeta);

options.cache[key].done = true;
options.events.emit(key);
Expand Down
4 changes: 3 additions & 1 deletion lib/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ function* get(url, options, globalOptions) {
options.agent = httpKeepaliveAgent;
options.headers = options.headers || {};
options.headers['User-Agent'] = USER_AGENT;
options.rejectUnauthorized = globalOptions.strictSSL;
if (globalOptions) {
options.rejectUnauthorized = globalOptions.strictSSL;
}
const result = yield _get(url, options, 5);
debug('GET %s, headers: %j from %j', result.status, result.headers, url);
if (result.status < 100 || result.status >= 300) {
Expand Down
2 changes: 2 additions & 0 deletions lib/local_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const dependencies = require('./dependencies');
* - {Object} [env] - postinstall and preinstall scripts custom env.
* - {String} [cacheDir] - tarball cache store dir, default is `$HOME/.npminstall_tarball`.
* if `production` mode enable, `cacheDir` will be disable.
* - {Object} [binaryMirrors] - binary mirror config, default is `{}`
*/
module.exports = function*(options) {
options.events = new EventEmitter();
Expand Down Expand Up @@ -84,6 +85,7 @@ module.exports = function*(options) {
options.remotePackages = 0;
options.registryPackages = 0;
options.gitPackages = 0;
options.binaryMirrors = options.binaryMirrors || {};
if (options.production) {
options.cacheDir = '';
} else {
Expand Down

0 comments on commit b6d35d1

Please sign in to comment.