@@ -448,30 +448,58 @@ void CreateHeapSnapshotStream(const FunctionCallbackInfo<Value>& args) {
448448void TriggerHeapSnapshot (const FunctionCallbackInfo<Value>& args) {
449449 Environment* env = Environment::GetCurrent (args);
450450 Isolate* isolate = args.GetIsolate ();
451- CHECK_EQ (args.Length (), 2 );
451+ CHECK_EQ (args.Length (), 3 );
452452 Local<Value> filename_v = args[0 ];
453453 auto options = GetHeapSnapshotOptions (args[1 ]);
454+ Local<String> path_v = args[2 ].As <String>();
455+ bool has_path = !path_v->IsNullOrUndefined () && path_v->IsString ();
456+ std::string final_filename;
457+
454458
455459 if (filename_v->IsUndefined ()) {
456460 DiagnosticFilename name (env, " Heap" , " heapsnapshot" );
457- THROW_IF_INSUFFICIENT_PERMISSIONS (
461+ if (!has_path) {
462+ THROW_IF_INSUFFICIENT_PERMISSIONS (
458463 env,
459464 permission::PermissionScope::kFileSystemWrite ,
460465 Environment::GetCwd (env->exec_path ()));
461- if (WriteSnapshot (env, *name, options).IsNothing ()) return ;
462- if (String::NewFromUtf8 (isolate, *name).ToLocal (&filename_v)) {
463- args.GetReturnValue ().Set (filename_v);
466+ final_filename = *name;
467+ } else {
468+ BufferValue path (isolate, path_v);
469+ CHECK_NOT_NULL (*path);
470+ ToNamespacedPath (env, &path);
471+ THROW_IF_INSUFFICIENT_PERMISSIONS (
472+ env, permission::PermissionScope::kFileSystemWrite ,
473+ path.ToStringView ());
474+
475+ final_filename = std::string (*path) + " /" +
476+ *name; // NOLINT(readability/pointer_notation)
477+ }
478+ } else {
479+ BufferValue filename (isolate, filename_v);
480+ CHECK_NOT_NULL (*filename);
481+
482+ if (has_path) {
483+ BufferValue path (isolate, path_v);
484+ CHECK_NOT_NULL (*path);
485+ ToNamespacedPath (env, &path);
486+ THROW_IF_INSUFFICIENT_PERMISSIONS (
487+ env, permission::PermissionScope::kFileSystemWrite ,
488+ std::string (*path) + " /" + std::string (*filename));
489+ final_filename = std::string (*path) + " /" + std::string (*filename);
490+ } else {
491+ ToNamespacedPath (env, &filename);
492+ THROW_IF_INSUFFICIENT_PERMISSIONS (
493+ env, permission::PermissionScope::kFileSystemWrite ,
494+ filename.ToStringView ());
495+ final_filename = *filename;
464496 }
465- return ;
466497 }
467498
468- BufferValue path (isolate, filename_v);
469- CHECK_NOT_NULL (*path);
470- ToNamespacedPath (env, &path);
471- THROW_IF_INSUFFICIENT_PERMISSIONS (
472- env, permission::PermissionScope::kFileSystemWrite , path.ToStringView ());
473- if (WriteSnapshot (env, *path, options).IsNothing ()) return ;
474- return args.GetReturnValue ().Set (filename_v);
499+ if (WriteSnapshot (env, final_filename.c_str (), options).IsNothing ()) return ;
500+
501+ args.GetReturnValue ().Set (
502+ String::NewFromUtf8 (isolate, final_filename.c_str ()).ToLocalChecked ());
475503}
476504
477505void Initialize (Local<Object> target,
0 commit comments