Skip to content

Commit

Permalink
lib: code de-duplication
Browse files Browse the repository at this point in the history
Modified the result of processRelease() to return uniform names `libUrl`,
`libPath` for Windows in objects named as target platforms (`ia32` and `x64`)

This allows to loop over the target platforms in downloadNodeLib() and to use
the universal names instead of two almost identical code parts with `libUrl32`,
`libPath32` and `libUrl64`, `libPath64` names.

PR-URL: #965
Reviewed-By: Rod Vagg <rod@vagg.org>
  • Loading branch information
pmed authored and rvagg committed Jun 22, 2019
1 parent d3478d7 commit 03683f0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 132 deletions.
97 changes: 33 additions & 64 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,75 +301,44 @@ function install (fs, gyp, argv, callback) {

function downloadNodeLib (done) {
log.verbose('on Windows; need to download `' + release.name + '.lib`...')
var dir32 = path.resolve(devDir, 'ia32')
, dir64 = path.resolve(devDir, 'x64')
, libPath32 = path.resolve(dir32, release.name + '.lib')
, libPath64 = path.resolve(dir64, release.name + '.lib')

log.verbose('32-bit ' + release.name + '.lib dir', dir32)
log.verbose('64-bit ' + release.name + '.lib dir', dir64)
log.verbose('`' + release.name + '.lib` 32-bit url', release.libUrl32)
log.verbose('`' + release.name + '.lib` 64-bit url', release.libUrl64)

var async = 2
mkdir(dir32, function (err) {
if (err) return done(err)
log.verbose('streaming 32-bit ' + release.name + '.lib to:', libPath32)

try {
var req = download(gyp, process.env, release.libUrl32, cb)
} catch (e) {
return cb(e)
}

req.on('error', done)
req.on('response', function (res) {
if (res.statusCode !== 200) {
done(new Error(res.statusCode + ' status code downloading 32-bit ' + release.name + '.lib'))
return
var archs = [ 'ia32', 'x64' ]
var async = archs.length
archs.forEach(function (arch) {
var dir = path.resolve(devDir, arch)
var targetLibPath = path.resolve(dir, release.name + '.lib')
var libUrl = release[arch].libUrl
var libPath = release[arch].libPath
var name = arch + ' ' + release.name + '.lib'
log.verbose(name, 'dir', dir)
log.verbose(name, 'url', libUrl)

mkdir(dir, function (err) {
if (err) return done(err)
log.verbose('streaming', name, 'to:', targetLibPath)

try {
var req = download(gyp, process.env, libUrl, cb)
} catch (e) {
return cb(e)
}

getContentSha(res, function (_, checksum) {
contentShasums[release.libPath32] = checksum
log.verbose('content checksum', release.libPath32, checksum)
})

var ws = fs.createWriteStream(libPath32)
ws.on('error', cb)
req.pipe(ws)
})
req.on('end', function () {
--async || done()
})
})
mkdir(dir64, function (err) {
if (err) return done(err)
log.verbose('streaming 64-bit ' + release.name + '.lib to:', libPath64)

try {
var req = download(gyp, process.env, release.libUrl64, cb)
} catch (e) {
return cb(e)
}
req.on('error', done)
req.on('response', function (res) {
if (res.statusCode !== 200) {
done(new Error(res.statusCode + ' status code downloading ' + name))
return
}

req.on('error', done)
req.on('response', function (res) {
if (res.statusCode !== 200) {
done(new Error(res.statusCode + ' status code downloading 64-bit ' + release.name + '.lib'))
return
}
getContentSha(res, function (_, checksum) {
contentShasums[libPath] = checksum
log.verbose('content checksum', libPath, checksum)
})

getContentSha(res, function (_, checksum) {
contentShasums[release.libPath64] = checksum
log.verbose('content checksum', release.libPath64, checksum)
var ws = fs.createWriteStream(targetLibPath)
ws.on('error', cb)
req.pipe(ws)
})

var ws = fs.createWriteStream(libPath64)
ws.on('error', cb)
req.pipe(ws)
})
req.on('end', function () {
--async || done()
req.on('end', function () { --async || done() })
})
})
} // downloadNodeLib()
Expand Down
6 changes: 2 additions & 4 deletions lib/process-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ function processRelease (argv, gyp, defaultVersion, defaultRelease) {
tarballUrl: tarballUrl,
shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'),
versionDir: (name !== 'node' ? name + '-' : '') + version,
libUrl32: libUrl32,
libUrl64: libUrl64,
libPath32: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl32).path)),
libPath64: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl64).path))
ia32: { libUrl: libUrl32, libPath: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl32).path)) },
x64: { libUrl: libUrl64, libPath: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl64).path)) },
}
}

Expand Down
96 changes: 32 additions & 64 deletions test/test-process-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ test('test process release - process.version = 0.8.20', function (t) {
tarballUrl: 'https://nodejs.org/dist/v0.8.20/node-v0.8.20.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v0.8.20/SHASUMS256.txt',
versionDir: '0.8.20',
libUrl32: 'https://nodejs.org/dist/v0.8.20/node.lib',
libUrl64: 'https://nodejs.org/dist/v0.8.20/x64/node.lib',
libPath32: 'node.lib',
libPath64: 'x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v0.8.20/node.lib', libPath: 'node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v0.8.20/x64/node.lib', libPath: 'x64/node.lib' },
})
})

Expand All @@ -38,10 +36,8 @@ test('test process release - process.version = 0.10.21', function (t) {
tarballUrl: 'https://nodejs.org/dist/v0.10.21/node-v0.10.21.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v0.10.21/SHASUMS256.txt',
versionDir: '0.10.21',
libUrl32: 'https://nodejs.org/dist/v0.10.21/node.lib',
libUrl64: 'https://nodejs.org/dist/v0.10.21/x64/node.lib',
libPath32: 'node.lib',
libPath64: 'x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v0.10.21/node.lib', libPath: 'node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v0.10.21/x64/node.lib', libPath: 'x64/node.lib' },
})
})

Expand All @@ -61,10 +57,8 @@ test('test process release - process.version = 0.12.9', function (t) {
tarballUrl: 'https://nodejs.org/dist/v0.12.9/node-v0.12.9.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v0.12.9/SHASUMS256.txt',
versionDir: '0.12.9',
libUrl32: 'https://nodejs.org/dist/v0.12.9/node.lib',
libUrl64: 'https://nodejs.org/dist/v0.12.9/x64/node.lib',
libPath32: 'node.lib',
libPath64: 'x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v0.12.9/node.lib', libPath: 'node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v0.12.9/x64/node.lib', libPath: 'x64/node.lib' },
})
})

Expand All @@ -84,10 +78,8 @@ test('test process release - process.version = 0.10.41', function (t) {
tarballUrl: 'https://nodejs.org/dist/v0.10.41/node-v0.10.41.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v0.10.41/SHASUMS256.txt',
versionDir: '0.10.41',
libUrl32: 'https://nodejs.org/dist/v0.10.41/node.lib',
libUrl64: 'https://nodejs.org/dist/v0.10.41/x64/node.lib',
libPath32: 'node.lib',
libPath64: 'x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v0.10.41/node.lib', libPath: 'node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v0.10.41/x64/node.lib', libPath: 'x64/node.lib' },
})
})

Expand All @@ -107,10 +99,8 @@ test('test process release - process.release ~ node@0.10.42', function (t) {
tarballUrl: 'https://nodejs.org/dist/v0.10.42/node-v0.10.42-headers.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v0.10.42/SHASUMS256.txt',
versionDir: '0.10.42',
libUrl32: 'https://nodejs.org/dist/v0.10.42/node.lib',
libUrl64: 'https://nodejs.org/dist/v0.10.42/x64/node.lib',
libPath32: 'node.lib',
libPath64: 'x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v0.10.42/node.lib', libPath: 'node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v0.10.42/x64/node.lib', libPath: 'x64/node.lib' },
})
})

Expand All @@ -130,10 +120,8 @@ test('test process release - process.release ~ node@0.12.10', function (t) {
tarballUrl: 'https://nodejs.org/dist/v0.12.10/node-v0.12.10-headers.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v0.12.10/SHASUMS256.txt',
versionDir: '0.12.10',
libUrl32: 'https://nodejs.org/dist/v0.12.10/node.lib',
libUrl64: 'https://nodejs.org/dist/v0.12.10/x64/node.lib',
libPath32: 'node.lib',
libPath64: 'x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v0.12.10/node.lib', libPath: 'node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v0.12.10/x64/node.lib', libPath: 'x64/node.lib' },
})
})

Expand All @@ -155,10 +143,8 @@ test('test process release - process.release ~ node@4.1.23', function (t) {
tarballUrl: 'https://nodejs.org/dist/v4.1.23/node-v4.1.23-headers.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v4.1.23/SHASUMS256.txt',
versionDir: '4.1.23',
libUrl32: 'https://nodejs.org/dist/v4.1.23/win-x86/node.lib',
libUrl64: 'https://nodejs.org/dist/v4.1.23/win-x64/node.lib',
libPath32: 'win-x86/node.lib',
libPath64: 'win-x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v4.1.23/win-x86/node.lib', libPath: 'win-x86/node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v4.1.23/win-x64/node.lib', libPath: 'win-x64/node.lib' },
})
})

Expand All @@ -180,10 +166,8 @@ test('test process release - process.release ~ node@4.1.23 / corp build', functi
tarballUrl: 'https://some.custom.location/node-v4.1.23-headers.tar.gz',
shasumsUrl: 'https://some.custom.location/SHASUMS256.txt',
versionDir: '4.1.23',
libUrl32: 'https://some.custom.location/win-x86/node.lib',
libUrl64: 'https://some.custom.location/win-x64/node.lib',
libPath32: 'win-x86/node.lib',
libPath64: 'win-x64/node.lib'
ia32: { libUrl: 'https://some.custom.location/win-x86/node.lib', libPath: 'win-x86/node.lib' },
x64: { libUrl: 'https://some.custom.location/win-x64/node.lib', libPath: 'win-x64/node.lib' },
})
})

Expand All @@ -205,10 +189,8 @@ test('test process release - process.release ~ node@4.1.23 --target=0.10.40', fu
tarballUrl: 'https://nodejs.org/dist/v0.10.40/node-v0.10.40.tar.gz',
shasumsUrl: 'https://nodejs.org/dist/v0.10.40/SHASUMS256.txt',
versionDir: '0.10.40',
libUrl32: 'https://nodejs.org/dist/v0.10.40/node.lib',
libUrl64: 'https://nodejs.org/dist/v0.10.40/x64/node.lib',
libPath32: 'node.lib',
libPath64: 'x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/dist/v0.10.40/node.lib', libPath: 'node.lib' },
x64: { libUrl: 'https://nodejs.org/dist/v0.10.40/x64/node.lib', libPath: 'x64/node.lib' },
})
})

Expand All @@ -230,10 +212,8 @@ test('test process release - process.release ~ node@4.1.23 --dist-url=https://fo
tarballUrl: 'https://foo.bar/baz/v4.1.23/node-v4.1.23-headers.tar.gz',
shasumsUrl: 'https://foo.bar/baz/v4.1.23/SHASUMS256.txt',
versionDir: '4.1.23',
libUrl32: 'https://foo.bar/baz/v4.1.23/win-x86/node.lib',
libUrl64: 'https://foo.bar/baz/v4.1.23/win-x64/node.lib',
libPath32: 'win-x86/node.lib',
libPath64: 'win-x64/node.lib'
ia32: { libUrl: 'https://foo.bar/baz/v4.1.23/win-x86/node.lib', libPath: 'win-x86/node.lib' },
x64: { libUrl: 'https://foo.bar/baz/v4.1.23/win-x64/node.lib', libPath: 'win-x64/node.lib' },
})
})

Expand All @@ -255,10 +235,8 @@ test('test process release - process.release ~ frankenstein@4.1.23', function (t
tarballUrl: 'https://frankensteinjs.org/dist/v4.1.23/frankenstein-v4.1.23-headers.tar.gz',
shasumsUrl: 'https://frankensteinjs.org/dist/v4.1.23/SHASUMS256.txt',
versionDir: 'frankenstein-4.1.23',
libUrl32: 'https://frankensteinjs.org/dist/v4.1.23/win-x86/frankenstein.lib',
libUrl64: 'https://frankensteinjs.org/dist/v4.1.23/win-x64/frankenstein.lib',
libPath32: 'win-x86/frankenstein.lib',
libPath64: 'win-x64/frankenstein.lib'
ia32: { libUrl: 'https://frankensteinjs.org/dist/v4.1.23/win-x86/frankenstein.lib', libPath: 'win-x86/frankenstein.lib' },
x64: { libUrl: 'https://frankensteinjs.org/dist/v4.1.23/win-x64/frankenstein.lib', libPath: 'win-x64/frankenstein.lib' },
})
})

Expand All @@ -281,10 +259,8 @@ test('test process release - process.release ~ frankenstein@4.1.23 --dist-url=ht
tarballUrl: 'http://foo.bar/baz/v4.1.23/frankenstein-v4.1.23-headers.tar.gz',
shasumsUrl: 'http://foo.bar/baz/v4.1.23/SHASUMS256.txt',
versionDir: 'frankenstein-4.1.23',
libUrl32: 'http://foo.bar/baz/v4.1.23/win-x86/frankenstein.lib',
libUrl64: 'http://foo.bar/baz/v4.1.23/win-x64/frankenstein.lib',
libPath32: 'win-x86/frankenstein.lib',
libPath64: 'win-x64/frankenstein.lib'
ia32: { libUrl: 'http://foo.bar/baz/v4.1.23/win-x86/frankenstein.lib', libPath: 'win-x86/frankenstein.lib' },
x64: { libUrl: 'http://foo.bar/baz/v4.1.23/win-x64/frankenstein.lib', libPath: 'win-x64/frankenstein.lib' },
})
})

Expand All @@ -306,10 +282,8 @@ test('test process release - process.release ~ node@4.0.0-rc.4', function (t) {
tarballUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/node-v4.0.0-rc.4-headers.tar.gz',
shasumsUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/SHASUMS256.txt',
versionDir: '4.0.0-rc.4',
libUrl32: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x86/node.lib',
libUrl64: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x64/node.lib',
libPath32: 'win-x86/node.lib',
libPath64: 'win-x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x86/node.lib', libPath: 'win-x86/node.lib' },
x64: { libUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x64/node.lib', libPath: 'win-x64/node.lib' },
})
})

Expand All @@ -334,10 +308,8 @@ test('test process release - process.release ~ node@4.0.0-rc.4 passed as argv[0]
tarballUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/node-v4.0.0-rc.4-headers.tar.gz',
shasumsUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/SHASUMS256.txt',
versionDir: '4.0.0-rc.4',
libUrl32: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x86/node.lib',
libUrl64: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x64/node.lib',
libPath32: 'win-x86/node.lib',
libPath64: 'win-x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x86/node.lib', libPath: 'win-x86/node.lib' },
x64: { libUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x64/node.lib', libPath: 'win-x64/node.lib' },
})
})

Expand All @@ -362,10 +334,8 @@ test('test process release - process.release ~ node@4.0.0-rc.4 - bogus string pa
tarballUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/node-v4.0.0-rc.4-headers.tar.gz',
shasumsUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/SHASUMS256.txt',
versionDir: '4.0.0-rc.4',
libUrl32: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x86/node.lib',
libUrl64: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x64/node.lib',
libPath32: 'win-x86/node.lib',
libPath64: 'win-x64/node.lib'
ia32: { libUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x86/node.lib', libPath: 'win-x86/node.lib' },
x64: { libUrl: 'https://nodejs.org/download/rc/v4.0.0-rc.4/win-x64/node.lib', libPath: 'win-x64/node.lib' },
})
})

Expand All @@ -389,10 +359,8 @@ test('test process release - NODEJS_ORG_MIRROR', function (t) {
tarballUrl: 'http://foo.bar/v4.1.23/node-v4.1.23-headers.tar.gz',
shasumsUrl: 'http://foo.bar/v4.1.23/SHASUMS256.txt',
versionDir: '4.1.23',
libUrl32: 'http://foo.bar/v4.1.23/win-x86/node.lib',
libUrl64: 'http://foo.bar/v4.1.23/win-x64/node.lib',
libPath32: 'win-x86/node.lib',
libPath64: 'win-x64/node.lib'
ia32: { libUrl: 'http://foo.bar/v4.1.23/win-x86/node.lib', libPath: 'win-x86/node.lib' },
x64: { libUrl: 'http://foo.bar/v4.1.23/win-x64/node.lib', libPath: 'win-x64/node.lib' },
})

delete process.env.NODEJS_ORG_MIRROR
Expand Down

0 comments on commit 03683f0

Please sign in to comment.