From a42b93b9aab9eacb695a6e95fe35accc903fd487 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 21 Mar 2024 02:08:02 +0100 Subject: [PATCH] util: improve `isInsideNodeModules` PR-URL: https://github.com/nodejs/node/pull/52147 Reviewed-By: Antoine du Hamel Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca --- lib/internal/util.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index 29e425e86ca67c..1cdceb1b065fb5 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -35,6 +35,7 @@ const { SafeSet, SafeWeakMap, SafeWeakRef, + StringPrototypeIncludes, StringPrototypeReplace, StringPrototypeToLowerCase, StringPrototypeToUpperCase, @@ -476,7 +477,7 @@ function spliceOne(list, index) { list.pop(); } -const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/; +const kNodeModulesRE = /^(?:.*)[\\/]node_modules[\\/]/; let getStructuredStack; @@ -506,8 +507,12 @@ function isInsideNodeModules() { const filename = frame.getFileName(); // If a filename does not start with / or contain \, // it's likely from Node.js core. - if (RegExpPrototypeExec(/^\/|\\/, filename) === null) + if ( + filename[0] !== '/' && + StringPrototypeIncludes(filename, '\\') === false + ) { continue; + } return RegExpPrototypeExec(kNodeModulesRE, filename) !== null; } }