diff --git a/doc/api/errors.md b/doc/api/errors.md
index fe81168e40a0fa..6147f768c821d1 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -1186,6 +1186,11 @@ after the session had already closed.
An error occurred while issuing a command via the `inspector` module.
+
+### ERR_INSPECTOR_NOT_ACTIVE
+
+The `inspector` is not active when `inspector.waitForDebugger()` is called.
+
### ERR_INSPECTOR_NOT_AVAILABLE
diff --git a/doc/api/inspector.md b/doc/api/inspector.md
index 63d2a702724970..e2980a278d1ef1 100644
--- a/doc/api/inspector.md
+++ b/doc/api/inspector.md
@@ -52,6 +52,16 @@ parameter usage.
Return the URL of the active inspector, or `undefined` if there is none.
+## inspector.waitForDebugger()
+
+
+Blocks until a client (existing or connected later) has sent
+`Runtime.runIfWaitingForDebugger` command.
+
+An exception will be thrown if there is no active inspector.
+
## Class: inspector.Session
The `inspector.Session` is used for dispatching messages to the V8 inspector
diff --git a/lib/inspector.js b/lib/inspector.js
index 4bec628b7d789e..198d87ae44ccb0 100644
--- a/lib/inspector.js
+++ b/lib/inspector.js
@@ -8,6 +8,7 @@ const {
ERR_INSPECTOR_COMMAND,
ERR_INSPECTOR_NOT_AVAILABLE,
ERR_INSPECTOR_NOT_CONNECTED,
+ ERR_INSPECTOR_NOT_ACTIVE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK
} = require('internal/errors').codes;
@@ -19,7 +20,12 @@ if (!hasInspector)
const EventEmitter = require('events');
const { validateString } = require('internal/validators');
const util = require('util');
-const { Connection, open, url } = internalBinding('inspector');
+const {
+ Connection,
+ open,
+ url,
+ waitForDebugger
+} = internalBinding('inspector');
const connectionSymbol = Symbol('connectionProperty');
const messageCallbacksSymbol = Symbol('messageCallbacks');
@@ -105,10 +111,22 @@ class Session extends EventEmitter {
}
}
+function inspectorOpen(port, host, wait) {
+ open(port, host);
+ if (wait)
+ waitForDebugger();
+}
+
+function inspectorWaitForDebugger() {
+ if (!waitForDebugger())
+ throw new ERR_INSPECTOR_NOT_ACTIVE();
+}
+
module.exports = {
- open: (port, host, wait) => open(port, host, !!wait),
+ open: inspectorOpen,
close: process._debugEnd,
url: url,
+ waitForDebugger: inspectorWaitForDebugger,
// This is dynamically added during bootstrap,
// where the console from the VM is still available
console: require('internal/util/inspector').consoleFromVM,
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 8fa56e315e6a1a..23dbb79196c752 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -882,6 +882,7 @@ E('ERR_INPUT_TYPE_NOT_ALLOWED', '--input-type can only be used with string ' +
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
E('ERR_INSPECTOR_COMMAND', 'Inspector error %d: %s', Error);
+E('ERR_INSPECTOR_NOT_ACTIVE', 'Inspector is not active', Error);
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
E('ERR_INTERNAL_ASSERTION', (message) => {
diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc
index 3131c6ad373d11..8b891a2d611ece 100644
--- a/src/inspector_js_api.cc
+++ b/src/inspector_js_api.cc
@@ -237,7 +237,6 @@ void IsEnabled(const FunctionCallbackInfo& args) {
void Open(const FunctionCallbackInfo& args) {
Environment* env = Environment::GetCurrent(args);
Agent* agent = env->inspector_agent();
- bool wait_for_connect = false;
if (args.Length() > 0 && args[0]->IsUint32()) {
uint32_t port = args[0].As()->Value();
@@ -249,12 +248,15 @@ void Open(const FunctionCallbackInfo& args) {
agent->host_port()->set_host(*host);
}
- if (args.Length() > 2 && args[2]->IsBoolean()) {
- wait_for_connect = args[2]->BooleanValue(args.GetIsolate());
- }
agent->StartIoThread();
- if (wait_for_connect)
+}
+
+void WaitForDebugger(const FunctionCallbackInfo& args) {
+ Environment* env = Environment::GetCurrent(args);
+ Agent* agent = env->inspector_agent();
+ if (agent->IsActive())
agent->WaitForConnect();
+ args.GetReturnValue().Set(agent->IsActive());
}
void Url(const FunctionCallbackInfo& args) {
@@ -285,6 +287,7 @@ void Initialize(Local