From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Tue, 22 Sep 2020 19:44:30 -0700
Subject: fix: allow preventing InitializeInspector in env

https://github.com/nodejs/node/commit/8c5ad1392f30cfe6b107e9bd85f4cb918ba04aab
made it such that env->InitializeInspector was called in CreateEnvironment
no matter what, which creates an issue for Electron, as the V8 inspector
already exists in the renderer process and therefore we only want to
initialize it in the browser process. This adds a new
EnvironmentFlags option which allows preventing that invocation.

diff --git a/src/api/environment.cc b/src/api/environment.cc
index 523d252e08974a10f9a53fb46d3345669cec3380..5bf19a0dda42849159d954181058897c45d280fd 100644
--- a/src/api/environment.cc
+++ b/src/api/environment.cc
@@ -344,12 +344,14 @@ Environment* CreateEnvironment(
   Environment* env = new Environment(
       isolate_data, context, args, exec_args, nullptr, flags, thread_id);
 #if HAVE_INSPECTOR
-  if (inspector_parent_handle) {
-    env->InitializeInspector(
-        std::move(static_cast<InspectorParentHandleImpl*>(
-            inspector_parent_handle.get())->impl));
-  } else {
-    env->InitializeInspector({});
+  if (env->should_initialize_inspector()) {
+    if (inspector_parent_handle) {
+      env->InitializeInspector(
+          std::move(static_cast<InspectorParentHandleImpl*>(
+              inspector_parent_handle.get())->impl));
+    } else {
+      env->InitializeInspector({});
+    }
   }
 #endif
 
diff --git a/src/env-inl.h b/src/env-inl.h
index 1e85bc07a4cc29f3b380da3c0e217a85309eb3a5..845e00208af4b12960ed8b3f3926323af7685185 100644
--- a/src/env-inl.h
+++ b/src/env-inl.h
@@ -892,6 +892,10 @@ inline bool Environment::no_global_search_paths() const {
          !options_->global_search_paths;
 }
 
+inline bool Environment::should_initialize_inspector() const {
+  return (flags_ & EnvironmentFlags::kNoInitializeInspector) == 0;
+}
+
 bool Environment::filehandle_close_warning() const {
   return emit_filehandle_warning_;
 }
diff --git a/src/env.h b/src/env.h
index 0c3715151488f425a723618252e1277b78fafe5f..e0deca497feb111622b257b952c9ed9161c7d001 100644
--- a/src/env.h
+++ b/src/env.h
@@ -1204,6 +1204,7 @@ class Environment : public MemoryRetainer {
   inline bool tracks_unmanaged_fds() const;
   inline bool hide_console_windows() const;
   inline bool no_global_search_paths() const;
+  inline bool should_initialize_inspector() const;
   inline uint64_t thread_id() const;
   inline worker::Worker* worker_context() const;
   Environment* worker_parent_env() const;
diff --git a/src/node.h b/src/node.h
index 364f789fbcbec8e3234961294698d8e69b04a310..85b5ac6a5a5cb5e4388a92a1d07c9afe17140a8c 100644
--- a/src/node.h
+++ b/src/node.h
@@ -420,7 +420,11 @@ enum Flags : uint64_t {
   // $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that
   // do not expect to have their behaviors changed because of globally
   // installed modules.
-  kNoGlobalSearchPaths = 1 << 7
+  kNoGlobalSearchPaths = 1 << 7,
+  // Controls whether or not the Environment should call InitializeInspector.
+  // This control is needed by embedders who may not want to initialize the V8
+  // inspector in situations where it already exists.
+  kNoInitializeInspector = 1 << 8
 };
 }  // namespace EnvironmentFlags
 
