Skip to content

Commit 42666c2

Browse files
legendecasRafaelGSS
authored andcommitted
lib,src: isInsideNodeModules should test on the first non-internal frame
PR-URL: #60991 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 376056e commit 42666c2

8 files changed

Lines changed: 58 additions & 29 deletions

File tree

‎benchmark/internal/util_isinsidenodemodules.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ const assert = require('assert');
44

55
constbench=common.createBenchmark(main,{
66
n: [1e6],
7-
stackLimit: [100],
87
stackCount: [99,101],
98
method: ['isInsideNodeModules','noop'],
109
},{
1110
flags: ['--expose-internals','--disable-warning=internal/test/binding'],
1211
});
1312

14-
functionmain({ n,stackLimit,stackCount, method }){
13+
functionmain({ n, stackCount, method }){
1514
const{ internalBinding }=require('internal/test/binding');
1615
const{ isInsideNodeModules }=internalBinding('util');
1716

@@ -30,7 +29,7 @@ function main({ n, stackLimit, stackCount, method }) {
3029

3130
bench.start();
3231
for(leti=0;i<n;i++){
33-
isInsideNodeModules(stackLimit,true);
32+
isInsideNodeModules();
3433
}
3534
bench.end(n);
3635
};

‎lib/buffer.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,14 @@ function showFlaggedDeprecation() {
178178
if(bufferWarningAlreadyEmitted||
179179
++nodeModulesCheckCounter>10000||
180180
(!require('internal/options').getOptionValue('--pending-deprecation')&&
181-
isInsideNodeModules(100,true))){
181+
isInsideNodeModules(3))){
182182
// We don't emit a warning, because we either:
183183
// - Already did so, or
184184
// - Already checked too many times whether a call is coming
185185
// from node_modules and want to stop slowing down things, or
186186
// - We aren't running with `--pending-deprecation` enabled,
187187
// and the code is inside `node_modules`.
188-
// - We found node_modules in up to the topmost 100 frames, or
189-
// there are more than 100 frames and we don't want to search anymore.
188+
// - If the topmost non-internal frame is not inside `node_modules`.
190189
return;
191190
}
192191

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,9 +1537,8 @@ function loadESMFromCJS(mod, filename, format, source) {
15371537
}elseif(mod[kIsCachedByESMLoader]){
15381538
// It comes from the require() built for `import cjs` and doesn't have a parent recorded
15391539
// in the CJS module instance. Inspect the stack trace to see if the require()
1540-
// comes from node_modules and reduce the noise. If there are more than 100 frames,
1541-
// just give up and assume it is under node_modules.
1542-
shouldEmitWarning=!isInsideNodeModules(100,true);
1540+
// comes from node_modules as a direct call and reduce the noise.
1541+
shouldEmitWarning=!isInsideNodeModules();
15431542
}
15441543
}else{
15451544
shouldEmitWarning=true;

‎lib/punycode.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const {
33
isInsideNodeModules,
44
}=internalBinding('util');
55

6-
if(!isInsideNodeModules(100,true)){
6+
if(!isInsideNodeModules()){
77
process.emitWarning(
88
'The `punycode` module is deprecated. Please use a userland '+
99
'alternative instead.',

‎lib/url.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const {
132132
leturlParseWarned=false;
133133

134134
functionurlParse(url,parseQueryString,slashesDenoteHost){
135-
if(!urlParseWarned&&!isInsideNodeModules(100,true)){
135+
if(!urlParseWarned&&!isInsideNodeModules(2)){
136136
urlParseWarned=true;
137137
process.emitWarning(
138138
'`url.parse()` behavior is not standardized and prone to '+

‎src/node_util.cc‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -320,23 +320,21 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) {
320320
args.GetReturnValue().Set(callsites);
321321
}
322322

323+
/**
324+
* Checks whether the current call directly initiated from a file inside
325+
* node_modules. This checks up to `frame_limit` stack frames, until it finds
326+
* a frame that is not part of node internal modules.
327+
*/
323328
staticvoidIsInsideNodeModules(const FunctionCallbackInfo<Value>& args) {
324329
Isolate* isolate = args.GetIsolate();
325-
CHECK_EQ(args.Length(), 2);
326-
CHECK(args[0]->IsInt32()); // frame_limit
327-
// The second argument is the default value.
328330

329-
int frames_limit = args[0].As<v8::Int32>()->Value();
331+
int frames_limit = (args.Length() > 0 && args[0]->IsInt32())
332+
? args[0].As<v8::Int32>()->Value()
333+
: 10;
330334
Local<StackTrace> stack =
331335
StackTrace::CurrentStackTrace(isolate, frames_limit);
332336
int frame_count = stack->GetFrameCount();
333337

334-
// If the search requires looking into more than |frames_limit| frames, give
335-
// up and return the specified default value.
336-
if (frame_count == frames_limit) {
337-
return args.GetReturnValue().Set(args[1]);
338-
}
339-
340338
bool result = false;
341339
for (int i = 0; i < frame_count; ++i) {
342340
Local<StackFrame> stack_frame = stack->GetFrame(isolate, i);
@@ -350,13 +348,11 @@ static void IsInsideNodeModules(const FunctionCallbackInfo<Value>& args) {
350348
if (script_name_str.starts_with("node:")) {
351349
continue;
352350
}
353-
if (script_name_str.find("/node_modules/") != std::string::npos ||
354-
script_name_str.find("\\node_modules\\") != std::string::npos ||
355-
script_name_str.find("/node_modules\\") != std::string::npos ||
356-
script_name_str.find("\\node_modules/") != std::string::npos) {
357-
result = true;
358-
break;
359-
}
351+
result = script_name_str.find("/node_modules/") != std::string::npos ||
352+
script_name_str.find("\\node_modules\\") != std::string::npos ||
353+
script_name_str.find("/node_modules\\") != std::string::npos ||
354+
script_name_str.find("\\node_modules/") != std::string::npos;
355+
break;
360356
}
361357

362358
args.GetReturnValue().Set(result);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// Flags: --expose-internals
4+
5+
constcommon=require('../common');
6+
constassert=require('assert');
7+
constvm=require('vm');
8+
9+
const{ internalBinding }=require('internal/test/binding');
10+
const{ isInsideNodeModules }=internalBinding('util');
11+
12+
constscript=newvm.Script(`
13+
const runInsideNodeModules = (cb) => {
14+
return cb();
15+
};
16+
17+
runInsideNodeModules;
18+
`,{
19+
filename: '/workspace/node_modules/test.js',
20+
});
21+
construnInsideNodeModules=script.runInThisContext();
22+
23+
// Test when called directly inside node_modules
24+
assert.strictEqual(runInsideNodeModules(isInsideNodeModules),true);
25+
26+
// Test when called inside a user callback from node_modules
27+
runInsideNodeModules(common.mustCall(()=>{
28+
functionnonNodeModulesFunction(){
29+
assert.strictEqual(isInsideNodeModules(),false);
30+
}
31+
32+
nonNodeModulesFunction();
33+
}));
34+
35+
// Test when called outside node_modules
36+
assert.strictEqual(isInsideNodeModules(),false);

‎typings/internalBinding/util.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface UtilBinding {
4545
guessHandleType(fd: number): 'TCP'|'TTY'|'UDP'|'FILE'|'PIPE'|'UNKNOWN';
4646
parseEnv(content: string): Record<string,string>;
4747
styleText(format: Array<string>|string,text: string): string;
48-
isInsideNodeModules(frameLimit: number,defaultValue: unknown): boolean;
48+
isInsideNodeModules(frameLimit?: number): boolean;
4949
constructSharedArrayBuffer(length?: number): SharedArrayBuffer;
5050

5151
constants: {

0 commit comments

Comments
 (0)