Skip to content

Commit d08b72a

Browse files
committed
util: improve isInsideNodeModules
1 parent c714cda commit d08b72a

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/internal/util.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const {
3535
SafeSet,
3636
SafeWeakMap,
3737
SafeWeakRef,
38+
StringPrototypeIncludes,
3839
StringPrototypeReplace,
3940
StringPrototypeToLowerCase,
4041
StringPrototypeToUpperCase,
@@ -476,7 +477,7 @@ function spliceOne(list, index) {
476477
list.pop();
477478
}
478479

479-
const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/;
480+
const kNodeModulesRE = /^(?:.*)[\\/]node_modules[\\/]/;
480481

481482
let getStructuredStack;
482483

@@ -488,13 +489,13 @@ function isInsideNodeModules() {
488489
// side-effect-free. Since this is currently only used for a deprecated API,
489490
// the perf implications should be okay.
490491
getStructuredStack = runInNewContext(`(function() {
491-
try { Error.stackTraceLimit = Infinity; } catch {}
492-
return function structuredStack() {
493-
const e = new Error();
494-
overrideStackTrace.set(e, (err, trace) => trace);
495-
return e.stack;
496-
};
497-
})()`, { overrideStackTrace }, { filename: 'structured-stack' });
492+
try { Error.stackTraceLimit = Infinity; } catch {}
493+
return function structuredStack() {
494+
const e = new Error();
495+
overrideStackTrace.set(e, (err, trace) => trace);
496+
return e.stack;
497+
};
498+
})()`, { overrideStackTrace }, { filename: 'structured-stack' });
498499
}
499500

500501
const stack = getStructuredStack();
@@ -506,8 +507,10 @@ function isInsideNodeModules() {
506507
const filename = frame.getFileName();
507508
// If a filename does not start with / or contain \,
508509
// it's likely from Node.js core.
509-
if (RegExpPrototypeExec(/^\/|\\/, filename) === null)
510+
if (
511+
filename[0] !== '/' || !StringPrototypeIncludes(filename, '\\')) {
510512
continue;
513+
}
511514
return RegExpPrototypeExec(kNodeModulesRE, filename) !== null;
512515
}
513516
}

0 commit comments

Comments
 (0)