From cf8701c866cb5b86ed3d85f0bab05f3e0fe0edf6 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 4 Oct 2024 19:29:34 +0200 Subject: [PATCH] module: use kNodeModulesRE to detect node_modules This is faster and more consistent with other places using the regular expression to detect node_modules. PR-URL: https://github.com/nodejs/node/pull/55243 Backport-PR-URL: https://github.com/nodejs/node/pull/56927 Reviewed-By: Antoine du Hamel Reviewed-By: Jacob Smith Reviewed-By: Richard Lau Reviewed-By: Marco Ippolito Refs: https://github.com/nodejs/node/issues/52697 --- lib/internal/modules/esm/load.js | 4 +++- lib/internal/util.js | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 5239bc8ed883a5..d5004f4495bacc 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -5,7 +5,9 @@ const { RegExpPrototypeExec, decodeURIComponent, } = primordials; -const { kEmptyObject } = require('internal/util'); +const { + kEmptyObject, +} = require('internal/util'); const { defaultGetFormat } = require('internal/modules/esm/get_format'); const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert'); diff --git a/lib/internal/util.js b/lib/internal/util.js index 40e6f5e5090295..ea1ef8ca90c626 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -477,6 +477,10 @@ function spliceOne(list, index) { const kNodeModulesRE = /^(?:.*)[\\/]node_modules[\\/]/; +function isUnderNodeModules(filename) { + return filename && (RegExpPrototypeExec(kNodeModulesRE, filename) !== null); +} + let getStructuredStackImpl; function lazyGetStructuredStack() { @@ -524,7 +528,7 @@ function isInsideNodeModules() { ) { continue; } - return RegExpPrototypeExec(kNodeModulesRE, filename) !== null; + return isUnderNodeModules(filename); } } return false; @@ -913,6 +917,7 @@ module.exports = { isArrayBufferDetached, isError, isInsideNodeModules, + isUnderNodeModules, join, lazyDOMException, lazyDOMExceptionClass,