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 e679780900abc9f6b6d1d6baa52576df278be8c7..2da8174fe9e4209f4705af0a1cf8bca5928f088c 100644
--- a/src/env-inl.h
+++ b/src/env-inl.h
@@ -882,6 +882,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 7aa5822abf11f1858d1ef8551cfc7a8c3d931f1e..2554c530f2ca7078ed0cca03968b31f56027231b 100644
--- a/src/env.h
+++ b/src/env.h
@@ -1215,6 +1215,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 70518ba49b3bcbfaf2e46ba8ddc3f04236bc27b8..7ca7502e086190c87ae6a61dca2192253894e906 100644
--- a/src/node.h
+++ b/src/node.h
@@ -439,7 +439,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
 
