-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor for clarity, fix dist-url, add env var dist-url functionality
PR-URL: #711 Reviewed-By: Ben Noordhuis <[email protected]>
- Loading branch information
Showing
7 changed files
with
305 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
gyp/test | ||
node_modules | ||
test/.node-gyp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,75 @@ | ||
var semver = require('semver') | ||
var url = require('url') | ||
var path = require('path') | ||
, url = require('url') | ||
, path = require('path') | ||
|
||
var bitsre = /(x86|x64)/ | ||
var bitsreV3 = /(x86|ia32|x64)/ // io.js v3.x.x shipped with "ia32" but should | ||
// have been "x86" | ||
, bitsre = /\/win-(x86|x64)\// | ||
, bitsreV3 = /\/win-(x86|ia32|x64)\// // io.js v3.x.x shipped with "ia32" but should | ||
// have been "x86" | ||
|
||
// Captures all the logic required to determine download URLs, local directory and | ||
// file names and whether we need to use SHA-1 or 2. Inputs come from command-line | ||
// switches (--target), `process.version` and `process.release` where it exists. | ||
// file names. Inputs come from command-line switches (--target, --dist-url), | ||
// `process.version` and `process.release` where it exists. | ||
function processRelease (argv, gyp, defaultVersion, defaultRelease) { | ||
var version = argv[0] || gyp.opts.target || defaultVersion | ||
, isDefaultVersion = version === defaultVersion | ||
, versionSemver = semver.parse(version) | ||
, overrideDistUrl = gyp.opts['dist-url'] || gyp.opts.disturl | ||
, isIojs | ||
, name | ||
, distBaseUrl | ||
, baseUrl | ||
, libUrl32 | ||
, libUrl64 | ||
|
||
// parse the version to normalize and ensure it's valid | ||
var versionSemver = semver.parse(version) | ||
if (!versionSemver) { | ||
// not a valid semver string, nothing we can do | ||
return { version: version } | ||
} | ||
// flatten version into String | ||
version = versionSemver.version | ||
|
||
var overrideDistUrl = gyp.opts['dist-url'] || gyp.opts.disturl | ||
var iojs = !overrideDistUrl && !defaultRelease && semver.satisfies(version, '>=1.0.0 <4.0.0') | ||
var name = (defaultRelease && defaultRelease.name.replace(/\./g, '')) || (iojs ? 'iojs' : 'node') // io.js -> iojs | ||
var defaultDirUrl = (overrideDistUrl || iojs ? 'https://iojs.org/download/release' : 'https://nodejs.org/dist') + '/v' + version + '/' | ||
var baseUrl | ||
var libUrl32 | ||
var libUrl64 | ||
// can't use process.release if we're using --target=x.y.z | ||
if (!isDefaultVersion) | ||
defaultRelease = null | ||
|
||
if (defaultRelease) { | ||
// v3 onward, has process.release | ||
name = defaultRelease.name.replace(/io\.js/, 'iojs') // remove the '.' for directory naming purposes | ||
isIojs = name === 'iojs' | ||
} else { | ||
// old node or alternative --target= | ||
isIojs = semver.satisfies(version, '>=1.0.0 <4.0.0') | ||
name = isIojs ? 'iojs' : 'node' | ||
} | ||
|
||
// check for the nvm.sh standard mirror env variables | ||
if (!overrideDistUrl) { | ||
if (isIojs && process.env.NVM_IOJS_ORG_MIRROR) | ||
overrideDistUrl = process.env.NVM_IOJS_ORG_MIRROR | ||
else if (process.env.NVM_NODEJS_ORG_MIRROR) | ||
overrideDistUrl = process.env.NVM_NODEJS_ORG_MIRROR | ||
} | ||
|
||
|
||
if (overrideDistUrl) | ||
distBaseUrl = overrideDistUrl.replace(/\/+$/, '') | ||
else | ||
distBaseUrl = isIojs ? 'https://iojs.org/download/release' : 'https://nodejs.org/dist' | ||
distBaseUrl += '/v' + version + '/' | ||
|
||
// new style, based on process.release so we have a lot of the data we need | ||
if (defaultRelease && defaultRelease.headersUrl && !overrideDistUrl) { | ||
baseUrl = url.resolve(defaultRelease.headersUrl, './') | ||
libUrl32 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || defaultDirUrl, 'x86', version) | ||
libUrl64 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || defaultDirUrl, 'x64', version) | ||
libUrl32 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || distBaseUrl, 'x86', version) | ||
libUrl64 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || distBaseUrl, 'x64', version) | ||
|
||
return { | ||
version: version, | ||
semver: versionSemver, | ||
name: name, | ||
baseUrl: baseUrl, | ||
tarballUrl: defaultRelease.headersUrl, | ||
shasumsFile: 'SHASUMS256.txt', | ||
shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'), | ||
checksumAlgo: 'sha256', | ||
versionDir: (name !== 'node' ? name + '-' : '') + version, | ||
libUrl32: libUrl32, | ||
libUrl64: libUrl64, | ||
|
@@ -52,25 +79,24 @@ function processRelease (argv, gyp, defaultVersion, defaultRelease) { | |
} | ||
|
||
// older versions without process.release are captured here and we have to make | ||
// a lot of assumptions | ||
|
||
// distributions starting with 0.10.0 contain sha256 checksums | ||
var checksumAlgo = semver.gte(version, '0.10.0') ? 'sha256' : 'sha1' | ||
var shasumsFile = (checksumAlgo === 'sha256') ? 'SHASUMS256.txt' : 'SHASUMS.txt' | ||
// a lot of assumptions, additionally if you --target=x.y.z then we can't use the | ||
// current process.release | ||
|
||
baseUrl = defaultDirUrl | ||
baseUrl = distBaseUrl | ||
libUrl32 = resolveLibUrl(name, baseUrl, 'x86', version) | ||
libUrl64 = resolveLibUrl(name, baseUrl, 'x64', version) | ||
// making the bold assumption that anything with a version number >3.0.0 will | ||
// have a *-headers.tar.gz file in its dist location, even some frankenstein | ||
// custom version | ||
tarballUrl = url.resolve(baseUrl, name + '-v' + version + (semver.satisfies(version, '>=3') ? '-headers' : '') + '.tar.gz') | ||
|
||
return { | ||
version: version, | ||
semver: versionSemver, | ||
name: name, | ||
baseUrl: baseUrl, | ||
tarballUrl: baseUrl + name + '-v' + version + '.tar.gz', | ||
shasumsFile: shasumsFile, | ||
shasumsUrl: baseUrl + shasumsFile, | ||
checksumAlgo: checksumAlgo, | ||
tarballUrl: tarballUrl, | ||
shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'), | ||
versionDir: (name !== 'node' ? name + '-' : '') + version, | ||
libUrl32: libUrl32, | ||
libUrl64: libUrl64, | ||
|
@@ -85,19 +111,19 @@ function normalizePath (p) { | |
|
||
function resolveLibUrl (name, defaultUrl, arch, version) { | ||
var base = url.resolve(defaultUrl, './') | ||
var isV3 = semver.satisfies(version, '^3') | ||
var hasLibUrl = bitsre.test(defaultUrl) || (isV3 && bitsreV3.test(defaultUrl)) | ||
, isV3 = semver.satisfies(version, '^3') | ||
, hasLibUrl = bitsre.test(defaultUrl) || (isV3 && bitsreV3.test(defaultUrl)) | ||
|
||
if (!hasLibUrl) { | ||
// let's assume it's a baseUrl then | ||
if (semver.gte(version, '1.0.0')) | ||
return url.resolve(base, 'win-' + arch +'/' + name + '.lib') | ||
// prior to [email protected] 32-bit node.lib lives in /, 64-bit lives in /x64/ | ||
return url.resolve(base, (arch == 'x64' ? 'x64/' : '') + name + '.lib') | ||
return url.resolve(base, (arch === 'x64' ? 'x64/' : '') + name + '.lib') | ||
} | ||
|
||
// else we have a proper url to a .lib, just make sure it's the right arch | ||
return defaultUrl.replace(isV3 ? bitsreV3 : bitsre, arch) | ||
return defaultUrl.replace(isV3 ? bitsreV3 : bitsre, '/win-' + arch + '/') | ||
} | ||
|
||
module.exports = processRelease |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var http = require('http') | ||
, https = require('https') | ||
, server = http.createServer(handler) | ||
, port = +process.argv[2] | ||
, prefix = process.argv[3] | ||
, upstream = process.argv[4] | ||
, calls = 0 | ||
|
||
server.listen(port) | ||
|
||
function handler (req, res) { | ||
if (req.url.indexOf(prefix) != 0) | ||
throw new Error('request url [' + req.url + '] does not start with [' + prefix + ']') | ||
|
||
var upstreamUrl = upstream + req.url.substring(prefix.length) | ||
console.log(req.url + ' -> ' + upstreamUrl) | ||
https.get(upstreamUrl, function (ures) { | ||
ures.on('end', function () { | ||
if (++calls == 2) | ||
server.close() | ||
}) | ||
ures.pipe(res) | ||
}) | ||
} |
Oops, something went wrong.