Skip to content

Commit

Permalink
util: improve isInsideNodeModules
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Mar 20, 2024
1 parent c714cda commit d08b72a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/internal/util.js
Expand Up @@ -35,6 +35,7 @@ const {
SafeSet,
SafeWeakMap,
SafeWeakRef,
StringPrototypeIncludes,
StringPrototypeReplace,
StringPrototypeToLowerCase,
StringPrototypeToUpperCase,
Expand Down Expand Up @@ -476,7 +477,7 @@ function spliceOne(list, index) {
list.pop();
}

const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/;
const kNodeModulesRE = /^(?:.*)[\\/]node_modules[\\/]/;

let getStructuredStack;

Expand All @@ -488,13 +489,13 @@ function isInsideNodeModules() {
// side-effect-free. Since this is currently only used for a deprecated API,
// the perf implications should be okay.
getStructuredStack = runInNewContext(`(function() {
try { Error.stackTraceLimit = Infinity; } catch {}
return function structuredStack() {
const e = new Error();
overrideStackTrace.set(e, (err, trace) => trace);
return e.stack;
};
})()`, { overrideStackTrace }, { filename: 'structured-stack' });
try { Error.stackTraceLimit = Infinity; } catch {}
return function structuredStack() {
const e = new Error();
overrideStackTrace.set(e, (err, trace) => trace);
return e.stack;
};
})()`, { overrideStackTrace }, { filename: 'structured-stack' });
}

const stack = getStructuredStack();
Expand All @@ -506,8 +507,10 @@ 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, '\\')) {
continue;
}
return RegExpPrototypeExec(kNodeModulesRE, filename) !== null;
}
}
Expand Down

0 comments on commit d08b72a

Please sign in to comment.