# nodejs-inspector

> inspector for Node.js

Latest version **1.0.1** (published 2022-09-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install nodejs-inspector
pnpm add nodejs-inspector
yarn add nodejs-inspector
bun add nodejs-inspector
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-09-07 |
| First published | 2022-09-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | theanarkh |
| Maintainers | theanarkh |

## Links

- npm: https://www.npmjs.com/package/nodejs-inspector
- Repository: https://github.com/theanarkh/nodejs-inspector
- Homepage: https://github.com/theanarkh/nodejs-inspector#readme
- Issues: https://github.com/theanarkh/nodejs-inspector/issues
- npm.io page: https://npm.io/package/nodejs-inspector

## Recent versions

- 1.0.1 (latest) — 2022-09-07
- 1.0.0 — 2022-09-07

## README

# Node.js inspector
Inspector for Node.js process & worker_threads

# 1. Inspector
Inspector is a wrapper for Node.js inspector module, but with promiseify API.

## 1.1 takeHeapSnapshot
```
const { Inspector } = require('nodejs-inspector');
const inspector = new Inspector();
inspector.start();
inspector.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
    //
});
await inspector.post('HeapProfiler.takeHeapSnapshot');
inspector.stop();
```

## 1.2 CPU Profile
```
const { Inspector, util } = require('nodejs-inspector');
const inspector = new Inspector();
inspector.start();
await inspector.post('Profiler.enable');
await inspector.post('Profiler.setSamplingInterval', { interval: 1000 });
await inspector.post('Profiler.start');
await util.sleep(1000);
const { profile } = await inspector.post('Profiler.stop');
await inspector.post('Profiler.disable');
inspector.stop();
```

# 2. Thread Inspector
Thread Inspector is a Inspector communicate with the `worker_thread`.

## 2.1 takeHeapSnapshot
```
const { ThreadInspector } = require('nodejs-inspector');
const { Worker } = require('worker_threads');
const worker = new Worker('setInterval(() => {}, 10000)', { eval: true });
const inspector = new ThreadInspector();
inspector.on('attachedToWorker', async (sessionContext) => {
    const { sessionId } = sessionContext.getWorkerInfo();
    sessionContext.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
        // 
    });
    await inspector.postToWorker(sessionId, { method: 'HeapProfiler.takeHeapSnapshot' });
    await inspector.stop();
    worker.terminate();
});
inspector.start();
```

## 2.2 CPU Profile
```
const { Worker } = require('worker_threads');
const { ThreadInspector, util } = require('nodejs-inspector');
const worker = new Worker('setInterval(() => {}, 10000)', { eval: true });
const inspector = new ThreadInspector();
inspector.on('attachedToWorker', async (sessionContext) => {
    const { sessionId } = sessionContext.getWorkerInfo();
    await inspector.postToWorker(sessionId, { method: 'Profiler.enable' });
    await inspector.postToWorker(sessionId, { 
        method: 'Profiler.setSamplingInterval', 
        params: { interval: 1000 }
    });
    await inspector.postToWorker(sessionId, { method: 'Profiler.start' });
    await util.sleep(1000);
    const { profile } = await inspector.postToWorker(sessionId, { method: 'Profiler.stop' });
    await inspector.postToWorker(sessionId, { method: 'Profiler.disable' });
    await inspector.stop();
    worker.terminate();
});
inspector.start();
```

# 3. API

## 3.1 Inspector
1. `post(method, params)`
Communicate with Node.js by inspector protocol.
2. `start`
Start the Inspector before using other API.
2. `stop`
Stop the Inspector If it is no longer needed.

## 3.2 ThreadInspector
1. `post(method, params)`
Communicate with Node.js by inspector protocol.
2. `postToWorker(sessionId, message)`
Communicate with Node.js worker_threads by inspector protocol.
3. `start`
Start the Inspector before using other API.
4. `stop`
Stop the Inspector If it is no longer needed.
5. `getSessions`
Get worker sessions.

See more informations in https://chromedevtools.github.io/devtools-protocol/v8/ .

---
_Source: https://npm.io/package/nodejs-inspector · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
