diff --git a/lib/util/versioning.js b/lib/util/versioning.js index 292a4a72..db6e122f 100644 --- a/lib/util/versioning.js +++ b/lib/util/versioning.js @@ -255,7 +255,11 @@ module.exports.evaluate = function(package_json,options) { module_main: package_json.main, toolset : options.toolset || '' // address https://github.com/mapbox/node-pre-gyp/issues/119 }; - opts.host = fix_slashes(eval_template(package_json.binary.host,opts)); + // support host mirror with npm config `--{module_name}_binary_host_mirror` + // e.g.: https://github.com/node-inspector/v8-profiler/blob/master/package.json#L25 + // > npm install v8-profiler --profiler_binary_host_mirror=http://npm.taobao.org/mirrors/node-inspector/ + var host = process.env['npm_config_' + opts.module_name + '_binary_host_mirror'] || package_json.binary.host; + opts.host = fix_slashes(eval_template(host,opts)); opts.module_path = eval_template(package_json.binary.module_path,opts); // now we resolve the module_path to ensure it is absolute so that binding.gyp variables work predictably if (options.module_root) { diff --git a/test/versioning.test.js b/test/versioning.test.js index d9f56d2c..58e6daaf 100644 --- a/test/versioning.test.js +++ b/test/versioning.test.js @@ -15,7 +15,7 @@ describe('versioning', function() { "module_path" : "./lib/binding/{configuration}/{toolset}/{name}", "remote_path" : "./{name}/v{version}/{configuration}/{version}/{toolset}/", "package_name": "{module_name}-v{major}.{minor}.{patch}-{prerelease}+{build}-{toolset}-{node_abi}-{platform}-{arch}.tar.gz", - "host" : "https://node-pre-gyp-tests.s3-us-west-1.amazonaws.com" + "host" : "https://node-pre-gyp-tests.s3-us-west-1.amazonaws.com" } }; var opts = versioning.evaluate(mock_package_json, {}); @@ -64,4 +64,24 @@ describe('versioning', function() { assert.equal(versioning.get_runtime_abi('node-webkit','0.10.5'),versioning.get_node_webkit_abi('node-webkit','0.10.5')); }); + it('should detect custom binary host from env', function() { + var mock_package_json = { + "name" : "test", + "main" : "test.js", + "version": "0.1.0", + "binary" : { + "module_name" : "test", + "module_path" : "./lib/binding/{configuration}/{toolset}/{name}", + "remote_path" : "./{name}/v{version}/{configuration}/{version}/{toolset}/", + "package_name": "{module_name}-v{major}.{minor}.{patch}-{prerelease}+{build}-{toolset}-{node_abi}-{platform}-{arch}.tar.gz", + "host" : "https://node-pre-gyp-tests.s3-us-west-1.amazonaws.com" + } + }; + // mock npm_config_test_binary_host_mirror env + process.env['npm_config_test_binary_host_mirror'] = "http://npm.taobao.org/mirrors/node-inspector/"; + var opts = versioning.evaluate(mock_package_json, {}); + assert.equal(opts.host,"http://npm.taobao.org/mirrors/node-inspector/"); + delete process.env['npm_config_test_binary_host_mirror']; + }); + });