GeezerVison is showing github.com — src: implement IsInsideNodeModules() in C++ · nodejs/node@ececd22
File treeExpand file treeCollapse file tree Open diff view settings fixtures/warning_node_modules
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,10 +77,10 @@ const { | 77 | 77 | ONLY_ENUMERABLE, | 78 | 78 | }, | 79 | 79 | getOwnNonIndexProperties, | | 80 | + isInsideNodeModules, | 80 | 81 | } = internalBinding('util'); | 81 | 82 | const { | 82 | 83 | customInspectSymbol, | 83 | | - isInsideNodeModules, | 84 | 84 | lazyDOMException, | 85 | 85 | normalizeEncoding, | 86 | 86 | kIsEncodingSymbol, | @@ -176,13 +176,15 @@ function showFlaggedDeprecation() { | 176 | 176 | if (bufferWarningAlreadyEmitted || | 177 | 177 | ++nodeModulesCheckCounter > 10000 || | 178 | 178 | (!require('internal/options').getOptionValue('--pending-deprecation') && | 179 | | - isInsideNodeModules())) { | | 179 | + isInsideNodeModules(100, true))) { | 180 | 180 | // We don't emit a warning, because we either: | 181 | 181 | // - Already did so, or | 182 | 182 | // - Already checked too many times whether a call is coming | 183 | 183 | // from node_modules and want to stop slowing down things, or | 184 | 184 | // - We aren't running with `--pending-deprecation` enabled, | 185 | 185 | // and the code is inside `node_modules`. | | 186 | + // - We found node_modules in up to the topmost 100 frames, or | | 187 | + // there are more than 100 frames and we don't want to search anymore. | 186 | 188 | return; | 187 | 189 | } | 188 | 190 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js -29Lines changed: 0 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const { | 4 | 4 | ArrayBufferPrototypeGetByteLength, | 5 | 5 | ArrayFrom, | 6 | | - ArrayIsArray, | 7 | 6 | ArrayPrototypePush, | 8 | 7 | ArrayPrototypeSlice, | 9 | 8 | ArrayPrototypeSort, | @@ -35,9 +34,7 @@ const { | 35 | 34 | SafeSet, | 36 | 35 | SafeWeakMap, | 37 | 36 | SafeWeakRef, | 38 | | - StringPrototypeIncludes, | 39 | 37 | StringPrototypeReplace, | 40 | | - StringPrototypeStartsWith, | 41 | 38 | StringPrototypeToLowerCase, | 42 | 39 | StringPrototypeToUpperCase, | 43 | 40 | Symbol, | @@ -509,31 +506,6 @@ function getStructuredStack() { | 509 | 506 | return getStructuredStackImpl(); | 510 | 507 | } | 511 | 508 |
| 512 | | -function isInsideNodeModules() { | 513 | | - const stack = getStructuredStack(); | 514 | | - | 515 | | - // Iterate over all stack frames and look for the first one not coming | 516 | | - // from inside Node.js itself: | 517 | | - if (ArrayIsArray(stack)) { | 518 | | - for (const frame of stack) { | 519 | | - const filename = frame.getFileName(); | 520 | | - | 521 | | - if ( | 522 | | - filename == null || | 523 | | - StringPrototypeStartsWith(filename, 'node:') === true || | 524 | | - ( | 525 | | - filename[0] !== '/' && | 526 | | - StringPrototypeIncludes(filename, '\\') === false | 527 | | - ) | 528 | | - ) { | 529 | | - continue; | 530 | | - } | 531 | | - return isUnderNodeModules(filename); | 532 | | - } | 533 | | - } | 534 | | - return false; | 535 | | -} | 536 | | - | 537 | 509 | function once(callback, { preserveReturnValue = false } = kEmptyObject) { | 538 | 510 | let called = false; | 539 | 511 | let returnValue; | @@ -916,7 +888,6 @@ module.exports = { | 916 | 888 | guessHandleType, | 917 | 889 | isArrayBufferDetached, | 918 | 890 | isError, | 919 | | - isInsideNodeModules, | 920 | 891 | isUnderNodeModules, | 921 | 892 | join, | 922 | 893 | lazyDOMException, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -254,6 +254,54 @@ static void ParseEnv(const FunctionCallbackInfo<Value>& args) { | 254 | 254 | args.GetReturnValue().Set(dotenv.ToObject(env)); | 255 | 255 | } | 256 | 256 |
| | 257 | +bool starts_with(std::string_view view, std::string_view prefix) { | | 258 | + return view.size() >= prefix.size() && | | 259 | + view.substr(0, prefix.size()) == prefix; | | 260 | +} | | 261 | + | | 262 | +static void IsInsideNodeModules(const FunctionCallbackInfo<Value>& args) { | | 263 | + Isolate* isolate = args.GetIsolate(); | | 264 | + CHECK_EQ(args.Length(), 2); | | 265 | + CHECK(args[0]->IsInt32()); // frame_limit | | 266 | + // The second argument is the default value. | | 267 | + | | 268 | + int frames_limit = args[0].As<v8::Int32>()->Value(); | | 269 | + Local<StackTrace> stack = | | 270 | + StackTrace::CurrentStackTrace(isolate, frames_limit); | | 271 | + int frame_count = stack->GetFrameCount(); | | 272 | + | | 273 | + // If the search requires looking into more than |frames_limit| frames, give | | 274 | + // up and return the specified default value. | | 275 | + if (frame_count == frames_limit) { | | 276 | + return args.GetReturnValue().Set(args[1]); | | 277 | + } | | 278 | + | | 279 | + bool result = false; | | 280 | + for (int i = 0; i < frame_count; ++i) { | | 281 | + Local<StackFrame> stack_frame = stack->GetFrame(isolate, i); | | 282 | + Local<String> script_name = stack_frame->GetScriptName(); | | 283 | + | | 284 | + if (script_name.IsEmpty() || script_name->Length() == 0) { | | 285 | + continue; | | 286 | + } | | 287 | + Utf8Value script_name_utf8(isolate, script_name); | | 288 | + std::string_view script_name_str = script_name_utf8.ToStringView(); | | 289 | + if (starts_with(script_name_str, | | 290 | + "node:")) { // Ported to work with C++17 on 20.x. | | 291 | + continue; | | 292 | + } | | 293 | + if (script_name_str.find("/node_modules/") != std::string::npos || | | 294 | + script_name_str.find("\\node_modules\\") != std::string::npos || | | 295 | + script_name_str.find("/node_modules\\") != std::string::npos || | | 296 | + script_name_str.find("\\node_modules/") != std::string::npos) { | | 297 | + result = true; | | 298 | + break; | | 299 | + } | | 300 | + } | | 301 | + | | 302 | + args.GetReturnValue().Set(result); | | 303 | +} | | 304 | + | 257 | 305 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | 258 | 306 | registry->Register(GetPromiseDetails); | 259 | 307 | registry->Register(GetProxyDetails); | @@ -269,6 +317,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | 269 | 317 | registry->Register(FastGuessHandleType); | 270 | 318 | registry->Register(fast_guess_handle_type_.GetTypeInfo()); | 271 | 319 | registry->Register(ParseEnv); | | 320 | + registry->Register(IsInsideNodeModules); | 272 | 321 | } | 273 | 322 |
| 274 | 323 | void Initialize(Local<Object> target, | @@ -338,6 +387,7 @@ void Initialize(Local<Object> target, | 338 | 387 | target->Set(context, env->constants_string(), constants).Check(); | 339 | 388 | } | 340 | 389 |
| | 390 | + SetMethod(context, target, "isInsideNodeModules", IsInsideNodeModules); | 341 | 391 | SetMethodNoSideEffect( | 342 | 392 | context, target, "getPromiseDetails", GetPromiseDetails); | 343 | 393 | SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails); | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/warning_node_modules/new-buffer-cjs.js +1Lines changed: 1 addition & 0 deletions Collapse file Copy file name to clipboardExpand all lines: test/fixtures/warning_node_modules/new-buffer-esm.mjs +1Lines changed: 1 addition & 0 deletions Collapse file Copy file name to clipboardExpand all lines: test/fixtures/warning_node_modules/node_modules/new-buffer-cjs/index.js +1Lines changed: 1 addition & 0 deletions Collapse file Copy file name to clipboardExpand all lines: test/fixtures/warning_node_modules/node_modules/new-buffer-cjs/package.json +3Lines changed: 3 additions & 0 deletions Collapse file Copy file name to clipboardExpand all lines: test/fixtures/warning_node_modules/node_modules/new-buffer-esm/index.js +2Lines changed: 2 additions & 0 deletions Collapse file Copy file name to clipboardExpand all lines: test/fixtures/warning_node_modules/node_modules/new-buffer-esm/package.json +4Lines changed: 4 additions & 0 deletions Collapse file Copy file name to clipboard +48Lines changed: 48 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const fixtures = require('../common/fixtures'); | | 5 | +const { spawnSyncAndAssert } = require('../common/child_process'); | | 6 | + | | 7 | +if (process.env.NODE_PENDING_DEPRECATION) | | 8 | + common.skip('test does not work when NODE_PENDING_DEPRECATION is set'); | | 9 | + | | 10 | +spawnSyncAndAssert( | | 11 | + process.execPath, | | 12 | + [ fixtures.path('warning_node_modules', 'new-buffer-cjs.js') ], | | 13 | + { | | 14 | + trim: true, | | 15 | + stderr: '', | | 16 | + } | | 17 | +); | | 18 | + | | 19 | +spawnSyncAndAssert( | | 20 | + process.execPath, | | 21 | + [ fixtures.path('warning_node_modules', 'new-buffer-esm.mjs') ], | | 22 | + { | | 23 | + trim: true, | | 24 | + stderr: '', | | 25 | + } | | 26 | +); | | 27 | + | | 28 | +spawnSyncAndAssert( | | 29 | + process.execPath, | | 30 | + [ | | 31 | + '--pending-deprecation', | | 32 | + fixtures.path('warning_node_modules', 'new-buffer-cjs.js'), | | 33 | + ], | | 34 | + { | | 35 | + stderr: /DEP0005/ | | 36 | + } | | 37 | +); | | 38 | + | | 39 | +spawnSyncAndAssert( | | 40 | + process.execPath, | | 41 | + [ | | 42 | + '--pending-deprecation', | | 43 | + fixtures.path('warning_node_modules', 'new-buffer-esm.mjs'), | | 44 | + ], | | 45 | + { | | 46 | + stderr: /DEP0005/ | | 47 | + } | | 48 | +); |
|