Skip to content

Commit

Permalink
inspector: add a document and a test for inspector.emitProtocolEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Jul 3, 2024
1 parent fbeab1d commit c31467f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
28 changes: 28 additions & 0 deletions doc/api/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,34 @@ require('node:inspector').console.log('a message');
The inspector console does not have API parity with Node.js
console.

### `inspector.emitProtocolEvent(eventName[, params])`

<!-- YAML
added:
- REPLACEME
-->

> Stability: 1 - Experimental
* `eventName` {string}
* `params` {Object}

Emits a protocol event with the specified `eventName` and optional `params`. This function allows you to send
custom events that conform to the DevTools protocol, facilitating integration with debugging and inspection tools.
The emitted events can be captured by connected DevTools Frontend instances, such as Chrome DevTools.

```js
inspector.emitProtocolEvent('NodeNetwork.requestWillBeSent', {
requestId: 'request-id-1',
timestamp: Date.now() / 1000,
wallTime: Date.now(),
request: {
url: 'https://nodejs.org/en',
method: 'GET',
}
});
```

### `inspector.open([port[, host[, wait]]])`

<!-- YAML
Expand Down
6 changes: 4 additions & 2 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ function inspectorWaitForDebugger() {

function inspectorEmitProtocolEvent(eventName, params) {
validateString(eventName, 'eventName');
validateObject(params, 'params');
emitProtocolEvent(eventName, JSONStringify(params));
if (params) {
validateObject(params, 'params');
}
emitProtocolEvent(eventName, JSONStringify(params ?? {}));
}

module.exports = {
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-inspector-emit-protocol-event.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Flags: --inspect=0

import * as common from '../common/index.mjs';

common.skipIfInspectorDisabled();

import inspector from 'node:inspector';
import assert from 'node:assert';

const session = new inspector.Session();
session.connect();

assert.throws(() => inspector.emitProtocolEvent(123), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "eventName" argument must be of type string. Received type number (123)'
});

assert.throws(() => inspector.emitProtocolEvent('NodeNetwork.requestWillBeSent', 'params'), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "params" argument must be of type object. Received type string (\'params\')'
});

session.on('NodeNetwork.requestWillBeSent', common.mustCall(({ params }) => {
assert.deepStrictEqual(params, {
requestId: 'request-id-1',
request: {
url: 'https://nodejs.org/en',
method: 'GET'
},
timestamp: 1000,
wallTime: 1000,
});
}));

inspector.emitProtocolEvent('NodeNetwork.requestWillBeSent', {
requestId: 'request-id-1',
request: {
url: 'https://nodejs.org/en',
method: 'GET'
},
timestamp: 1000,
wallTime: 1000,
unknown: 'unknown'
});

0 comments on commit c31467f

Please sign in to comment.