From 8f764b5adfa20c9558a97cbebed1390f44c6dcd2 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 17 Oct 2025 08:21:08 -0400 Subject: [PATCH 1/2] [Tests] install npm 5 before npm 6 in node 7 on windows --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 7937eac..4b4615c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -52,6 +52,7 @@ install: - IF %nodejs_version% EQU 4 npm -g install npm@5.3 - IF %nodejs_version% EQU 5 npm -g install npm@5.3 - IF %nodejs_version% EQU 6 npm -g install npm@6.9 + - IF %nodejs_version% EQU 7 npm -g install npm@5 - IF %nodejs_version% EQU 7 npm -g install npm@6 - IF %nodejs_version% EQU 8 npm -g install npm@6 - IF %nodejs_version% EQU 9 npm -g install npm@6.9 From a5e28aaa8c36dc2bae452fa195e60450b9fd2c3b Mon Sep 17 00:00:00 2001 From: Russell Horton Date: Wed, 15 Oct 2025 17:15:06 -0700 Subject: [PATCH 2/2] [Refactor] hoist regex expressions for efficiency --- .eslintrc | 2 +- lib/async.js | 12 ++++++++---- lib/node-modules-paths.js | 7 +++++-- lib/sync.js | 10 +++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.eslintrc b/.eslintrc index 94c956f..aa48f93 100644 --- a/.eslintrc +++ b/.eslintrc @@ -19,7 +19,7 @@ "func-style": 0, "global-require": 1, "id-length": [2, { "min": 1, "max": 40 }], - "max-lines": [2, 350], + "max-lines": [2, 360], "max-lines-per-function": 1, "max-nested-callbacks": 0, "max-params": 0, diff --git a/lib/async.js b/lib/async.js index 07f18a6..a9f51bc 100644 --- a/lib/async.js +++ b/lib/async.js @@ -8,6 +8,10 @@ var isCore = require('is-core-module'); var realpathFS = process.platform !== 'win32' && fs.realpath && typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath; +var relativePathRegex = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/; +var windowsDriveRegex = /^\w:[/\\]*$/; +var nodeModulesRegex = /[/\\]node_modules[/\\]*$/; + var homedir = getHomedir(); var defaultPaths = function () { return [ @@ -138,10 +142,10 @@ module.exports = function resolve(x, options, callback) { var res; function validBasedir(basedir) { - if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) { + if (relativePathRegex.test(x)) { res = path.resolve(basedir, x); if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/'; - if ((/\/$/).test(x) && res === basedir) { + if (x.slice(-1) === '/' && res === basedir) { loadAsDirectory(res, opts.package, onfile); } else loadAsFile(res, opts.package, onfile); } else if (includeCoreModules && isCore(x)) { @@ -229,10 +233,10 @@ module.exports = function resolve(x, options, callback) { function loadpkg(dir, cb) { if (dir === '' || dir === '/') return cb(null); - if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) { + if (process.platform === 'win32' && windowsDriveRegex.test(dir)) { return cb(null); } - if ((/[/\\]node_modules[/\\]*$/).test(dir)) return cb(null); + if (nodeModulesRegex.test(dir)) return cb(null); maybeRealpath(realpath, dir, opts, function (unwrapErr, pkgdir) { if (unwrapErr) return loadpkg(path.dirname(dir), cb); diff --git a/lib/node-modules-paths.js b/lib/node-modules-paths.js index 1cff010..81d02ca 100644 --- a/lib/node-modules-paths.js +++ b/lib/node-modules-paths.js @@ -1,11 +1,14 @@ var path = require('path'); var parse = path.parse || require('path-parse'); // eslint-disable-line global-require +var driveLetterRegex = /^([A-Za-z]:)/; +var uncPathRegex = /^\\\\/; + var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) { var prefix = '/'; - if ((/^([A-Za-z]:)/).test(absoluteStart)) { + if (driveLetterRegex.test(absoluteStart)) { prefix = ''; - } else if ((/^\\\\/).test(absoluteStart)) { + } else if (uncPathRegex.test(absoluteStart)) { prefix = '\\\\'; } diff --git a/lib/sync.js b/lib/sync.js index ddc468a..b4577b7 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -8,6 +8,10 @@ var normalizeOptions = require('./normalize-options'); var realpathFS = process.platform !== 'win32' && fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync; +var relativePathRegex = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/; +var windowsDriveRegex = /^\w:[/\\]*$/; +var nodeModulesRegex = /[/\\]node_modules[/\\]*$/; + var homedir = getHomedir(); var defaultPaths = function () { return [ @@ -98,7 +102,7 @@ module.exports = function resolveSync(x, options) { throw dirError; } - if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) { + if (relativePathRegex.test(x)) { var res = path.resolve(absoluteStart, x); if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/'; var m = loadAsFileSync(res) || loadAsDirectorySync(res); @@ -139,10 +143,10 @@ module.exports = function resolveSync(x, options) { function loadpkg(dir) { if (dir === '' || dir === '/') return; - if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) { + if (process.platform === 'win32' && windowsDriveRegex.test(dir)) { return; } - if ((/[/\\]node_modules[/\\]*$/).test(dir)) return; + if (nodeModulesRegex.test(dir)) return; var pkgfile = path.join(isDirectory(dir) ? maybeRealpathSync(realpathSync, dir, opts) : dir, 'package.json');