Skip to content

Commit

Permalink
inspector: add promisified result for session.post
Browse files Browse the repository at this point in the history
  • Loading branch information
ErickWendel committed Aug 16, 2022
1 parent 472edc7 commit b1947d2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
10 changes: 10 additions & 0 deletions doc/api/inspector.md
Expand Up @@ -183,15 +183,25 @@ changes:
* `method` {string}
* `params` {Object}
* `callback` {Function}
* Returns: {Promise|undefined}

Posts a message to the inspector back-end. `callback` will be notified when
a response is received. `callback` is a function that accepts two optional
arguments: error and message-specific result.

If the `callback` is not passed, then it'll return a Promise result.

```js
session.post('Runtime.evaluate', { expression: '2 + 2' },
(error, { result }) => console.log(result));
// Output: { type: 'number', value: 4, description: '4' }

// or for promise
session.post('Runtime.evaluate', { expression: '2 + 2' })
.then((result) => console.log(result))
.catch((error) => console.error(error));

// Output: { type: 'number', value: 4, description: '4' }
```

The latest version of the V8 inspector protocol is published on the
Expand Down
20 changes: 17 additions & 3 deletions lib/inspector.js
Expand Up @@ -22,6 +22,7 @@ const { hasInspector } = internalBinding('config');
if (!hasInspector)
throw new ERR_INSPECTOR_NOT_AVAILABLE();

const { promisify } = require('util');
const EventEmitter = require('events');
const { queueMicrotask } = require('internal/process/task_queues');
const {
Expand Down Expand Up @@ -108,7 +109,7 @@ class Session extends EventEmitter {
* @param {string} method
* @param {Record<unknown, unknown>} [params]
* @param {Function} [callback]
* @returns {void}
* @returns {void | Promise}
*/
post(method, params, callback) {
validateString(method, 'method');
Expand All @@ -128,13 +129,26 @@ class Session extends EventEmitter {
}
const id = this[nextIdSymbol]++;
const message = { id, method };
const configureCbAndDispatch = (cb) => {
this[messageCallbacksSymbol].set(id, cb);
this[connectionSymbol].dispatch(JSONStringify(message));
};

if (params) {
message.params = params;
}
if (callback) {
this[messageCallbacksSymbol].set(id, callback);
configureCbAndDispatch(callback);
return;
}
this[connectionSymbol].dispatch(JSONStringify(message));

const event = new EventEmitter();
const kOnFinish = Symbol('kOnFinish');
const promisifiedResult = promisify(event.once.bind(event))(kOnFinish);
const cb = (err, res) => event.emit(kOnFinish, err, res);
configureCbAndDispatch(cb);

return promisifiedResult;
}

/**
Expand Down
61 changes: 61 additions & 0 deletions test/parallel/test-inspector-session-post-promisified.js
@@ -0,0 +1,61 @@
'use strict';

const common = require('../common');
common.skipIfInspectorDisabled();

const assert = require('assert');
const {
Session
} = require('inspector');
const {
basename
} = require('path');
const currentFilename = basename(__filename);

{
// Ensure that given a callback to session.post it won't return a promise result
const session = new Session();
session.connect();

const postResult = session.post('Profiler.enable', () => {
session.post('Profiler.start', () => {
session.post('Profiler.stop', (err, { profile }) => {

const { callFrame: { url } } = profile.nodes.find(({
callFrame
}) => callFrame.url.includes(currentFilename));
session.disconnect();
assert.deepStrictEqual(basename(url), currentFilename);
});
});
});

assert.strictEqual(postResult, undefined);
}

(async () => {
{
// Ensure that session.post returns a valid promisified result
const session = new Session();
session.connect();

await session.post('Profiler.enable');
await session.post('Profiler.start');

const {
profile
} = await session.post('Profiler.stop');

const {
callFrame: {
url
}
} = profile.nodes.find(({
callFrame
}) => {
return callFrame.url.includes(currentFilename);
});
session.disconnect();
assert.deepStrictEqual(basename(url), currentFilename);
}
})().then(common.mustCall());

0 comments on commit b1947d2

Please sign in to comment.