Skip to content

Commit

Permalink
feat: get rid of most var
Browse files Browse the repository at this point in the history
  • Loading branch information
imatlopez committed Nov 3, 2020
1 parent 472982e commit fe9d1d3
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getProxyFromURI = require('./proxy')
*/

async function install (fs, gyp, argv) {
var release = processRelease(argv, gyp, process.version, process.release)
const release = processRelease(argv, gyp, process.version, process.release)

// Determine which node dev files version we are installing
log.verbose('install', 'input version string %j', release.version)
Expand Down Expand Up @@ -47,7 +47,7 @@ async function install (fs, gyp, argv) {
log.verbose('install', 'installing version: %s', release.versionDir)

// the directory where the dev files will be installed
var devDir = path.resolve(gyp.devDir, release.versionDir)
const devDir = path.resolve(gyp.devDir, release.versionDir)

// If '--ensure' was passed, then don't *always* install the version;
// check if it is already installed, and only install when needed
Expand All @@ -66,7 +66,7 @@ async function install (fs, gyp, argv) {
throw err
}
log.verbose('install', 'version is already installed, need to check "installVersion"')
var installVersionFile = path.resolve(devDir, 'installVersion')
const installVersionFile = path.resolve(devDir, 'installVersion')
let installVersion = 0
try {
const ver = await fs.promises.readFile(installVersionFile, 'ascii')
Expand Down Expand Up @@ -106,15 +106,15 @@ async function install (fs, gyp, argv) {
}

// now download the node tarball
var tarPath = gyp.opts.tarball
var extractCount = 0
var contentShasums = {}
var expectShasums = {}
const tarPath = gyp.opts.tarball
let extractCount = 0
const contentShasums = {}
const expectShasums = {}

// checks if a file to be extracted from the tarball is valid.
// only .h header files and the gyp files get extracted
function isValid (path) {
var isValid = valid(path)
const isValid = valid(path)
if (isValid) {
log.verbose('extracted file from tarball', path)
extractCount++
Expand Down Expand Up @@ -164,7 +164,7 @@ async function install (fs, gyp, argv) {
}
// content checksum
const sha256 = new Sha256(function (_, checksum) {
var filename = path.basename(release.tarballUrl).trim()
const filename = path.basename(release.tarballUrl).trim()
contentShasums[filename] = checksum
log.verbose('content checksum', filename, checksum)
})
Expand All @@ -186,7 +186,7 @@ async function install (fs, gyp, argv) {
}
log.verbose('tarball', 'done parsing tarball')

var installVersionPath = path.resolve(devDir, 'installVersion')
const installVersionPath = path.resolve(devDir, 'installVersion')
await Promise.all([
// need to download node.lib
...(win ? downloadNodeLib() : []),
Expand All @@ -200,7 +200,7 @@ async function install (fs, gyp, argv) {

log.verbose('download contents checksum', JSON.stringify(contentShasums))
// check content shasums
for (var k in contentShasums) {
for (const k in contentShasums) {
log.verbose('validating download checksum for ' + k, '(%s == %s)', contentShasums[k], expectShasums[k])
if (contentShasums[k] !== expectShasums[k]) {
throw new Error(k + ' local checksum ' + contentShasums[k] + ' not match remote ' + expectShasums[k])
Expand All @@ -225,20 +225,20 @@ async function install (fs, gyp, argv) {
return
}

var chunks = []
const chunks = []
res.on('data', function (chunk) {
chunks.push(chunk)
})
res.on('end', function () {
var lines = Buffer.concat(chunks).toString().trim().split('\n')
const lines = Buffer.concat(chunks).toString().trim().split('\n')
lines.forEach(function (line) {
var items = line.trim().split(/\s+/)
const items = line.trim().split(/\s+/)
if (items.length !== 2) {
return
}

// 0035d18e2dcf9aad669b1c7c07319e17abfe3762 ./node-v0.11.4.tar.gz
var name = items[1].replace(/^\.\//, '')
const name = items[1].replace(/^\.\//, '')
expectShasums[name] = items[0]
})

Expand All @@ -251,13 +251,12 @@ async function install (fs, gyp, argv) {

function downloadNodeLib () {
log.verbose('on Windows; need to download `' + release.name + '.lib`...')
var archs = ['ia32', 'x64', 'arm64']
const archs = ['ia32', 'x64', 'arm64']
return archs.map(async 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'
const dir = path.resolve(devDir, arch)
const targetLibPath = path.resolve(dir, release.name + '.lib')
const { libUrl, libPath } = release[arch]
const name = arch + ' ' + release.name + '.lib'
log.verbose(name, 'dir', dir)
log.verbose(name, 'url', libUrl)

Expand Down Expand Up @@ -291,7 +290,7 @@ async function install (fs, gyp, argv) {
log.verbose('content checksum', libPath, checksum)
})

var ws = fs.createWriteStream(targetLibPath)
const ws = fs.createWriteStream(targetLibPath)
ws.on('close', resolve).on('error', reject)
res.pipe(sha256.pipe(ws))
})
Expand All @@ -306,7 +305,7 @@ async function install (fs, gyp, argv) {

function valid (file) {
// header files
var extname = path.extname(file)
const extname = path.extname(file)
return extname === '.h' || extname === '.gypi'
}

Expand All @@ -327,13 +326,13 @@ async function install (fs, gyp, argv) {
*/

async function eaccesFallback (err) {
var noretry = '--node_gyp_internal_noretry'
const noretry = '--node_gyp_internal_noretry'
if (argv.indexOf(noretry) !== -1) {
throw err
}
var tmpdir = os.tmpdir()
const tmpdir = os.tmpdir()
gyp.devDir = path.resolve(tmpdir, '.node-gyp')
var userString = ''
let userString = ''
try {
// os.userInfo can fail on some systems, it's not critical here
userString = ` ("${os.userInfo().username}")`
Expand Down Expand Up @@ -369,21 +368,21 @@ class Sha256 extends stream.Transform {
function download (gyp, env, url) {
log.http('GET', url)

var requestOpts = {
const requestOpts = {
uri: url,
headers: {
'User-Agent': 'node-gyp v' + gyp.version + ' (node ' + process.version + ')',
Connection: 'keep-alive'
}
}

var cafile = gyp.opts.cafile
const cafile = gyp.opts.cafile
if (cafile) {
requestOpts.ca = readCAFile(cafile)
}

// basic support for a proxy server
var proxyUrl = getProxyFromURI(gyp, env, url)
const proxyUrl = getProxyFromURI(gyp, env, url)
if (proxyUrl) {
if (/^https?:\/\//i.test(proxyUrl)) {
log.verbose('download', 'using proxy url: "%s"', proxyUrl)
Expand All @@ -393,7 +392,7 @@ function download (gyp, env, url) {
}
}

var req = request(requestOpts)
const req = request(requestOpts)
req.on('response', function (res) {
log.http(res.statusCode, url)
})
Expand All @@ -404,8 +403,8 @@ function download (gyp, env, url) {
function readCAFile (filename) {
// The CA file can contain multiple certificates so split on certificate
// boundaries. [\S\s]*? is used to match everything including newlines.
var ca = fs.readFileSync(filename, 'utf8')
var re = /(-----BEGIN CERTIFICATE-----[\S\s]*?-----END CERTIFICATE-----)/g
const ca = fs.readFileSync(filename, 'utf8')
const re = /(-----BEGIN CERTIFICATE-----[\S\s]*?-----END CERTIFICATE-----)/g
return ca.match(re)
}

Expand Down

0 comments on commit fe9d1d3

Please sign in to comment.