Skip to content

Commit

Permalink
Support custom binary hosting mirror
Browse files Browse the repository at this point in the history
Like we setup a mirror site for node-inspector at
https://npm.taobao.org/mirrors/node-inspector/

These mirrors try to help people download binary files from China.
  • Loading branch information
fengmk2 committed Nov 3, 2015
1 parent 906f8a1 commit 2e5590a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Now you need to publish builds for all the platforms and node versions you wish

#### 8) You're done!

Now publish your module to the npm registry. Users will now be able to install your module from a binary.
Now publish your module to the npm registry. Users will now be able to install your module from a binary.

What will happen is this:

Expand Down Expand Up @@ -521,7 +521,7 @@ First, unlike the Travis linux machines the OS X machines do not put `node-pre-g
export PATH=$(pwd)/node_modules/.bin:${PATH}
```

Second, the OS X machines doe not support using a matrix for installing node.js different versions. So you need to bootstrap the installation of node.js in a cross platform way.
Second, the OS X machines doe not support using a matrix for installing node.js different versions. So you need to bootstrap the installation of node.js in a cross platform way.

By doing:

Expand Down Expand Up @@ -579,3 +579,15 @@ The `binary` properties of `module_path`, `remote_path`, and `package_name` supp


The options are visible in the code at <https://github.com/mapbox/node-pre-gyp/blob/612b7bca2604508d881e1187614870ba19a7f0c5/lib/util/versioning.js#L114-L127>

# Download binary files from a mirror

S3 is broken in China for the well known reason.

Using the `npm` config argument: `--{module_name}_binary_host_mirror` can download binary files through a mirror.

e.g.: Install [v8-profiler](https://www.npmjs.com/package/v8-profiler) from `npm`.

```bash
$ npm install v8-profiler --profiler_binary_host_mirror=https://npm.taobao.org/mirrors/node-inspector/
```
6 changes: 5 additions & 1 deletion lib/util/versioning.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,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=https://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) {
Expand Down
22 changes: 21 additions & 1 deletion test/versioning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {});
Expand Down Expand Up @@ -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 = 'https://npm.taobao.org/mirrors/node-inspector/';
var opts = versioning.evaluate(mock_package_json, {});
assert.equal(opts.host,'https://npm.taobao.org/mirrors/node-inspector/');
delete process.env.npm_config_test_binary_host_mirror;
});

});

0 comments on commit 2e5590a

Please sign in to comment.