Turn inspect_client.js
into a public API #81
Description
Even though the API is already accessible today, it's under internal/
, so it shouldn't be used by dependencies. I think it makes sense to expose this API though, as it is useful to fulfill the use case described here. As an example, here's a short script that allows users to connect to a remote process and take a CPU Profile:
#!/usr/bin/env node
'use strict';
const InspectorClient = require('node-inspect/lib/internal/inspect_client.js');
async function main() {
try {
const client = new InspectorClient();
await client.connect(9229, 'localhost');
await client.callMethod("Profiler.enable");
await client.callMethod("Profiler.start");
await new Promise((res) => setTimeout(res, 1000));
const { profile } = await client.callMethod("Profiler.stop");
await client.callMethod("Profiler.disable");
await new Promise((res) => process.stdout.write(JSON.stringify(profile), res));
client.reset();
} catch (e) {
console.error(e);
process.exit(1);
}
}
main();
This is the easier way to non-interactively run inspector methods on a remote Node.js instance. The only alternative available is to use a Websocket client such as ws
or websocat
(and both offer subpar experience with the inspector protocol compared to this API), since the Inspector API available on core doesn't allow users to connect to a remote process.
It's also worth noting the API is fairly stable, there has been no substantial changes to that file since 2016.