| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -419,12 +419,20 @@ console. | |||
| 419 | 419 | ||
| 420 | 420 | ### `inspector.open([port[, host[, wait]]])` | |
| 421 | 421 | ||
| 422 | + <!-- YAML | ||
| 423 | + changes: | ||
| 424 | + - version: REPLACEME | ||
| 425 | + pr-url: https://github.com/nodejs/node/pull/48765 | ||
| 426 | + description: inspector.open() now returns a `Disposable` object. | ||
| 427 | + --> | ||
| 428 | + | ||
| 422 | 429 | * `port` {number} Port to listen on for inspector connections. Optional. | |
| 423 | 430 | **Default:** what was specified on the CLI. | |
| 424 | 431 | * `host` {string} Host to listen on for inspector connections. Optional. | |
| 425 | 432 | **Default:** what was specified on the CLI. | |
| 426 | 433 | * `wait` {boolean} Block until a client has connected. Optional. | |
| 427 | 434 | **Default:** `false`. | |
| 435 | + * Returns: {Disposable} that calls [`inspector.close()`][]. | ||
| 428 | 436 | ||
| 429 | 437 | Activate inspector on host and port. Equivalent to | |
| 430 | 438 | `node --inspect=[[host:]port]`, but can be done programmatically after node has | |
@@ -472,5 +480,6 @@ An exception will be thrown if there is no active inspector. | |||
| 472 | 480 | [Chrome DevTools Protocol Viewer]: https://chromedevtools.github.io/devtools-protocol/v8/ | |
| 473 | 481 | [Heap Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/HeapProfiler | |
| 474 | 482 | [`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused | |
| 483 | + [`inspector.close()`]: #inspectorclose | ||
| 475 | 484 | [`session.connect()`]: #sessionconnect | |
| 476 | 485 | [security warning]: cli.md#warning-binding-inspector-to-a-public-ipport-combination-is-insecure | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ const { | |||
| 5 | 5 | JSONStringify, | |
| 6 | 6 | SafeMap, | |
| 7 | 7 | Symbol, | |
| 8 | + SymbolDispose, | ||
| 8 | 9 | } = primordials; | |
| 9 | 10 | ||
| 10 | 11 | const { | |
@@ -181,6 +182,8 @@ function inspectorOpen(port, host, wait) { | |||
| 181 | 182 | open(port, host); | |
| 182 | 183 | if (wait) | |
| 183 | 184 | waitForDebugger(); | |
| 185 | + | ||
| 186 | + return { __proto__: null, [SymbolDispose]() { _debugEnd(); } }; | ||
| 184 | 187 | } | |
| 185 | 188 | ||
| 186 | 189 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,76 @@ | |||
| 1 | + import * as common from '../common/index.mjs'; | ||
| 2 | + import assert from 'node:assert'; | ||
| 3 | + import net from 'node:net'; | ||
| 4 | + import url from 'node:url'; | ||
| 5 | + import { fork } from 'node:child_process'; | ||
| 6 | + | ||
| 7 | + common.skipIfInspectorDisabled(); | ||
| 8 | + if (process.env.BE_CHILD) { | ||
| 9 | + await beChild(); | ||
| 10 | + } else { | ||
| 11 | + let firstPort; | ||
| 12 | + | ||
| 13 | + const filename = url.fileURLToPath(import.meta.url); | ||
| 14 | + const child = fork(filename, { env: { ...process.env, BE_CHILD: 1 } }); | ||
| 15 | + | ||
| 16 | + child.once('message', common.mustCall((msg) => { | ||
| 17 | + assert.strictEqual(msg.cmd, 'started'); | ||
| 18 | + | ||
| 19 | + child.send({ cmd: 'open', args: [] }); | ||
| 20 | + child.once('message', common.mustCall(wasOpenedHandler)); | ||
| 21 | + })); | ||
| 22 | + | ||
| 23 | + function wasOpenedHandler(msg) { | ||
| 24 | + assert.strictEqual(msg.cmd, 'url'); | ||
| 25 | + const port = url.parse(msg.url).port; | ||
| 26 | + ping(port, common.mustSucceed(() => { | ||
| 27 | + // Inspector is already open, and won't be reopened, so args don't matter. | ||
| 28 | + child.send({ cmd: 'dispose' }); | ||
| 29 | + child.once('message', common.mustCall(wasDisposedWhenOpenHandler)); | ||
| 30 | + firstPort = port; | ||
| 31 | + })); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + function wasDisposedWhenOpenHandler(msg) { | ||
| 35 | + assert.strictEqual(msg.cmd, 'url'); | ||
| 36 | + assert.strictEqual(msg.url, undefined); | ||
| 37 | + ping(firstPort, (err) => { | ||
| 38 | + assert(err); | ||
| 39 | + child.send({ cmd: 'dispose' }); | ||
| 40 | + child.once('message', common.mustCall(wasReDisposedHandler)); | ||
| 41 | + }); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + function wasReDisposedHandler(msg) { | ||
| 45 | + assert.strictEqual(msg.cmd, 'url'); | ||
| 46 | + assert.strictEqual(msg.url, undefined); | ||
| 47 | + process.exit(); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + function ping(port, callback) { | ||
| 52 | + net.connect({ port, family: 4 }) | ||
| 53 | + .on('connect', function() { close(this); }) | ||
| 54 | + .on('error', function(err) { close(this, err); }); | ||
| 55 | + | ||
| 56 | + function close(self, err) { | ||
| 57 | + self.end(); | ||
| 58 | + self.on('close', () => callback(err)); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + async function beChild() { | ||
| 63 | + const inspector = await import('node:inspector'); | ||
| 64 | + let inspectorDisposer; | ||
| 65 | + process.send({ cmd: 'started' }); | ||
| 66 | + | ||
| 67 | + process.on('message', (msg) => { | ||
| 68 | + if (msg.cmd === 'open') { | ||
| 69 | + inspectorDisposer = inspector.open(0, false, undefined); | ||
| 70 | + } | ||
| 71 | + if (msg.cmd === 'dispose') { | ||
| 72 | + inspectorDisposer[Symbol.dispose](); | ||
| 73 | + } | ||
| 74 | + process.send({ cmd: 'url', url: inspector.url() }); | ||
| 75 | + }); | ||
| 76 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments