Skip to content

Commit dda927c

Browse files
committed
Introduced host configuration with object.
- added a standarize_config function. function is called by validate_config and mutates the object holding the parsed package.json, so that hosts are defined as objects with endpoint key, rather than strings. - modified versioning.evaluate to work with object format of hosts.
1 parent e365c1d commit dda927c

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

lib/util/versioning.js

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,51 @@ function get_runtime_abi(runtime, target_version) {
186186
}
187187
module.exports.get_runtime_abi = get_runtime_abi;
188188

189+
function standarize_config(package_json) {
190+
// backwards compatibility via mutation of user supplied configuration
191+
if (package_json.binary) {
192+
// the option of setting production_host was introduced in
193+
// https://github.com/mapbox/node-pre-gyp/pull/533
194+
// spec said that host should be falsey and production_host not empty.
195+
// legacy config will thus have production_host (and staging_host) defined
196+
// and will not have host defined.
197+
// to support legacy configuration with new spec:
198+
199+
// ** transfer the value of production_host to host
200+
if (package_json.binary.production_host && !package_json.binary.host) {
201+
package_json.binary.host = package_json.binary.production_host;
202+
}
203+
204+
if (package_json.binary.host) {
205+
// hosts used to be specified as string (and user may still do so)
206+
// to support legacy configuration with new spec:
207+
208+
// map string format of host to object key
209+
['host', 'staging_host', 'development_host'].filter((item) => package_json.binary[item]).forEach((item) => {
210+
if (typeof package_json.binary[item] === 'string') {
211+
package_json.binary[item] = { endpoint: package_json.binary[item] };
212+
}
213+
});
214+
215+
// the option to explicitly set buckt host properties was introduced in
216+
// https://github.com/mapbox/node-pre-gyp/pull/576
217+
// spec defined options as keys of binary relating to the string value of host.
218+
// legacy config will thus have bucket, region, s3ForcePathStyle defined under binary.
219+
// to support legacy configuration with new spec:
220+
221+
// map keys defined on binary to keys defined on host
222+
['bucket', 'region', 's3ForcePathStyle'].filter((item) => package_json.binary[item]).forEach((item) => {
223+
if (typeof package_json.binary[item] !== 'object') {
224+
package_json.binary.host[item] = package_json.binary[item];
225+
}
226+
});
227+
}
228+
}
229+
}
230+
189231
function validate_config(package_json, opts) {
232+
standarize_config(package_json); // the way hosts are defined changed overtime. make it standard.
233+
190234
const msg = package_json.name + ' package.json is not node-pre-gyp ready:\n';
191235
const missing = [];
192236
if (!package_json.main) {
@@ -209,9 +253,16 @@ function validate_config(package_json, opts) {
209253
if (!package_json.binary.module_path) {
210254
missing.push('binary.module_path');
211255
}
212-
if (!package_json.binary.host && !package_json.binary.production_host) {
256+
257+
if (!package_json.binary.host) {
213258
missing.push('binary.host');
214259
}
260+
261+
if (package_json.binary.host) {
262+
if (!package_json.binary.host.endpoint) {
263+
missing.push('binary.host.endpoint');
264+
}
265+
}
215266
}
216267

217268
if (missing.length >= 1) {
@@ -220,8 +271,8 @@ function validate_config(package_json, opts) {
220271

221272
if (package_json.binary) {
222273
// for all possible host definitions - verify https usage
223-
['host', 'production_host', 'staging_host', 'development_host'].filter((item) => package_json.binary[item]).forEach((item) => {
224-
const protocol = url.parse(package_json.binary[item]).protocol;
274+
['host', 'staging_host', 'development_host'].filter((item) => package_json.binary[item]).forEach((item) => {
275+
const protocol = url.parse(package_json.binary[item].endpoint).protocol;
225276
if (protocol === 'http:') {
226277
throw new Error(msg + "'" + item + "' protocol (" + protocol + ") is invalid - only 'https:' is accepted");
227278
}
@@ -278,7 +329,8 @@ const default_remote_path = '';
278329

279330
module.exports.evaluate = function(package_json, options, napi_build_version) {
280331
options = options || {};
281-
validate_config(package_json, options); // options is a suitable substitute for opts in this case
332+
standarize_config(package_json); // note: package_json is mutated
333+
validate_config(package_json, options);
282334
const v = package_json.version;
283335
const module_version = semver.parse(v);
284336
const runtime = options.runtime || get_process_runtime(process.versions);
@@ -306,10 +358,7 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
306358
target_arch: options.target_arch || process.arch,
307359
libc: options.target_libc || detect_libc.familySync() || 'unknown',
308360
module_main: package_json.main,
309-
toolset: options.toolset || '', // address https://github.com/mapbox/node-pre-gyp/issues/119
310-
bucket: package_json.binary.bucket,
311-
region: package_json.binary.region,
312-
s3ForcePathStyle: package_json.binary.s3ForcePathStyle || false
361+
toolset: options.toolset || '' // address https://github.com/mapbox/node-pre-gyp/issues/119
313362
};
314363

315364
// user can define a target host key to use (development_host, staging_host, production_host)
@@ -327,20 +376,24 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
327376
// the production host is as specified in 'host' key (default)
328377
// unless there is none and alias production_host is specified (backwards compatibility)
329378
// note: package.json is verified in validate_config to include at least one of the two.
330-
let host = package_json.binary.host || package_json.binary.production_host;
379+
let hostData = package_json.binary.host;
331380

332381
// when a valid target is specified by user, the host is from that target (or 'host')
333382
if (targetHost === 'staging') {
334-
host = package_json.binary.staging_host;
383+
hostData = package_json.binary.staging_host;
335384
} else if (targetHost === 'development') {
336-
host = package_json.binary.development_host;
385+
hostData = package_json.binary.development_host;
337386
} else if (!targetHost && (package_json.binary.development_host || package_json.binary.staging_host)) {
338387
// when host not specifically set via command line or environment variable
339388
// but staging and/or development host are present in package.json
340389
// for any command (or command chain) that includes publish or unpublish
341390
// default to lower host (development, and if not preset, staging).
342391
if (options.argv && options.argv.remain.some((item) => (item === 'publish' || item === 'unpublish'))) {
343-
host = package_json.binary.development_host || package_json.binary.staging_host;
392+
if (package_json.binary.development_host) {
393+
hostData = package_json.binary.development_host;
394+
} else if (package_json.binary.staging_host) {
395+
hostData = package_json.binary.staging_host;
396+
}
344397
}
345398
}
346399

@@ -349,9 +402,13 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
349402
// > npm install v8-profiler --profiler_binary_host_mirror=https://registry.npmmirror.com/node-inspector/
350403
const validModuleName = opts.module_name.replace('-', '_');
351404
// explicitly set mirror overrides everything set above
352-
host = process.env['npm_config_' + validModuleName + '_binary_host_mirror'] || host;
405+
hostData.endpoint = process.env['npm_config_' + validModuleName + '_binary_host_mirror'] || hostData.endpoint;
406+
407+
opts.host = fix_slashes(eval_template(hostData.endpoint, opts));
408+
opts.bucket = hostData.bucket;
409+
opts.region = hostData.region;
410+
opts.s3ForcePathStyle = hostData.s3ForcePathStyle || false;
353411

354-
opts.host = fix_slashes(eval_template(host, opts));
355412
opts.module_path = eval_template(package_json.binary.module_path, opts);
356413
// now we resolve the module_path to ensure it is absolute so that binding.gyp variables work predictably
357414
if (options.module_root) {
@@ -366,6 +423,7 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
366423
const package_name = package_json.binary.package_name ? package_json.binary.package_name : default_package_name;
367424
opts.package_name = eval_template(package_name, opts);
368425
opts.staged_tarball = path.join('build/stage', opts.remote_path, opts.package_name);
426+
369427
// when using s3ForcePathStyle the bucket is part of the http object path
370428
// add it
371429
if (opts.s3ForcePathStyle) {

0 commit comments

Comments
 (0)