@@ -320,23 +320,20 @@ 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+ */
323328static void IsInsideNodeModules (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.
330+ CHECK_GE (args.Length (), 0 );
328331
329- int frames_limit = args[0 ] .As <v8::Int32>()->Value ();
332+ int frames_limit = args[1 ]-> IsInt32 () ? args[ 1 ] .As <v8::Int32>()->Value () : 10 ;
330333 Local<StackTrace> stack =
331334 StackTrace::CurrentStackTrace (isolate, frames_limit);
332335 int frame_count = stack->GetFrameCount ();
333336
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-
340337 bool result = false ;
341338 for (int i = 0 ; i < frame_count; ++i) {
342339 Local<StackFrame> stack_frame = stack->GetFrame (isolate, i);
@@ -350,13 +347,11 @@ static void IsInsideNodeModules(const FunctionCallbackInfo<Value>& args) {
350347 if (script_name_str.starts_with (" node:" )) {
351348 continue ;
352349 }
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- }
350+ result = script_name_str.find (" /node_modules/" ) != std::string::npos ||
351+ 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+ break ;
360355 }
361356
362357 args.GetReturnValue ().Set (result);
0 commit comments