| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 376056e commit 42666c2
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,14 +4,13 @@ const assert = require('assert'); | |||
| 4 | 4 | ||
| 5 | 5 | const bench = common.createBenchmark(main, { | |
| 6 | 6 | n: [1e6], | |
| 7 | - stackLimit: [100], | ||
| 8 | 7 | stackCount: [99, 101], | |
| 9 | 8 | method: ['isInsideNodeModules', 'noop'], | |
| 10 | 9 | }, { | |
| 11 | 10 | flags: ['--expose-internals', '--disable-warning=internal/test/binding'], | |
| 12 | 11 | }); | |
| 13 | 12 | ||
| 14 | - function main({ n, stackLimit, stackCount, method }) { | ||
| 13 | + function main({ n, stackCount, method }) { | ||
| 15 | 14 | const { internalBinding } = require('internal/test/binding'); | |
| 16 | 15 | const { isInsideNodeModules } = internalBinding('util'); | |
| 17 | 16 | ||
@@ -30,7 +29,7 @@ function main({ n, stackLimit, stackCount, method }) { | |||
| 30 | 29 | ||
| 31 | 30 | bench.start(); | |
| 32 | 31 | for (let i = 0; i < n; i++) { | |
| 33 | - isInsideNodeModules(stackLimit, true); | ||
| 32 | + isInsideNodeModules(); | ||
| 34 | 33 | } | |
| 35 | 34 | bench.end(n); | |
| 36 | 35 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -178,15 +178,14 @@ function showFlaggedDeprecation() { | |||
| 178 | 178 | if (bufferWarningAlreadyEmitted || | |
| 179 | 179 | ++nodeModulesCheckCounter > 10000 || | |
| 180 | 180 | (!require('internal/options').getOptionValue('--pending-deprecation') && | |
| 181 | - isInsideNodeModules(100, true))) { | ||
| 181 | + isInsideNodeModules(3))) { | ||
| 182 | 182 | // We don't emit a warning, because we either: | |
| 183 | 183 | // - Already did so, or | |
| 184 | 184 | // - Already checked too many times whether a call is coming | |
| 185 | 185 | // from node_modules and want to stop slowing down things, or | |
| 186 | 186 | // - We aren't running with `--pending-deprecation` enabled, | |
| 187 | 187 | // 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`. | ||
| 190 | 189 | return; | |
| 191 | 190 | } | |
| 192 | 191 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1537,9 +1537,8 @@ function loadESMFromCJS(mod, filename, format, source) { | |||
| 1537 | 1537 | } else if (mod[kIsCachedByESMLoader]) { | |
| 1538 | 1538 | // It comes from the require() built for `import cjs` and doesn't have a parent recorded | |
| 1539 | 1539 | // 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(); | ||
| 1543 | 1542 | } | |
| 1544 | 1543 | } else { | |
| 1545 | 1544 | shouldEmitWarning = true; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ const { | |||
| 3 | 3 | isInsideNodeModules, | |
| 4 | 4 | } = internalBinding('util'); | |
| 5 | 5 | ||
| 6 | - if (!isInsideNodeModules(100, true)) { | ||
| 6 | + if (!isInsideNodeModules()) { | ||
| 7 | 7 | process.emitWarning( | |
| 8 | 8 | 'The `punycode` module is deprecated. Please use a userland ' + | |
| 9 | 9 | 'alternative instead.', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,7 +132,7 @@ const { | |||
| 132 | 132 | let urlParseWarned = false; | |
| 133 | 133 | ||
| 134 | 134 | function urlParse(url, parseQueryString, slashesDenoteHost) { | |
| 135 | - if (!urlParseWarned && !isInsideNodeModules(100, true)) { | ||
| 135 | + if (!urlParseWarned && !isInsideNodeModules(2)) { | ||
| 136 | 136 | urlParseWarned = true; | |
| 137 | 137 | process.emitWarning( | |
| 138 | 138 | '`url.parse()` behavior is not standardized and prone to ' + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -320,23 +320,21 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) { | |||
| 320 | 320 | args.GetReturnValue().Set(callsites); | |
| 321 | 321 | } | |
| 322 | 322 | ||
| 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 | + */ | ||
| 323 | 328 | static void IsInsideNodeModules(const FunctionCallbackInfo<Value>& args) { | |
| 324 | 329 | 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. | ||
| 328 | 330 | ||
| 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; | ||
| 330 | 334 | Local<StackTrace> stack = | |
| 331 | 335 | StackTrace::CurrentStackTrace(isolate, frames_limit); | |
| 332 | 336 | int frame_count = stack->GetFrameCount(); | |
| 333 | 337 | ||
| 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 | - | ||
| 340 | 338 | bool result = false; | |
| 341 | 339 | for (int i = 0; i < frame_count; ++i) { | |
| 342 | 340 | Local<StackFrame> stack_frame = stack->GetFrame(isolate, i); | |
@@ -350,13 +348,11 @@ static void IsInsideNodeModules(const FunctionCallbackInfo<Value>& args) { | |||
| 350 | 348 | if (script_name_str.starts_with("node:")) { | |
| 351 | 349 | continue; | |
| 352 | 350 | } | |
| 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; | ||
| 360 | 356 | } | |
| 361 | 357 | ||
| 362 | 358 | args.GetReturnValue().Set(result); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,36 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // Flags: --expose-internals | ||
| 4 | + | ||
| 5 | + const common = require('../common'); | ||
| 6 | + const assert = require('assert'); | ||
| 7 | + const vm = require('vm'); | ||
| 8 | + | ||
| 9 | + const { internalBinding } = require('internal/test/binding'); | ||
| 10 | + const { isInsideNodeModules } = internalBinding('util'); | ||
| 11 | + | ||
| 12 | + const script = new vm.Script(` | ||
| 13 | + const runInsideNodeModules = (cb) => { | ||
| 14 | + return cb(); | ||
| 15 | + }; | ||
| 16 | + | ||
| 17 | + runInsideNodeModules; | ||
| 18 | + `, { | ||
| 19 | + filename: '/workspace/node_modules/test.js', | ||
| 20 | + }); | ||
| 21 | + const runInsideNodeModules = 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 | + function nonNodeModulesFunction() { | ||
| 29 | + assert.strictEqual(isInsideNodeModules(), false); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + nonNodeModulesFunction(); | ||
| 33 | + })); | ||
| 34 | + | ||
| 35 | + // Test when called outside node_modules | ||
| 36 | + assert.strictEqual(isInsideNodeModules(), false); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,7 +45,7 @@ export interface UtilBinding { | |||
| 45 | 45 | guessHandleType(fd: number): 'TCP' | 'TTY' | 'UDP' | 'FILE' | 'PIPE' | 'UNKNOWN'; | |
| 46 | 46 | parseEnv(content: string): Record<string, string>; | |
| 47 | 47 | styleText(format: Array<string> | string, text: string): string; | |
| 48 | - isInsideNodeModules(frameLimit: number, defaultValue: unknown): boolean; | ||
| 48 | + isInsideNodeModules(frameLimit?: number): boolean; | ||
| 49 | 49 | constructSharedArrayBuffer(length?: number): SharedArrayBuffer; | |
| 50 | 50 | ||
| 51 | 51 | constants: { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments