|
| 1 | +# Inspector |
| 2 | + |
| 3 | +> Stability: 1 - Experimental |
| 4 | +
|
| 5 | +The `inspector` module provides an API for interacting with the V8 inspector. |
| 6 | + |
| 7 | +It can be accessed using: |
| 8 | + |
| 9 | +```js |
| 10 | +const inspector = require('inspector'); |
| 11 | +``` |
| 12 | + |
| 13 | +## Class: inspector.Session |
| 14 | + |
| 15 | +The `inspector.Session` is used for dispatching messages to the V8 inspector |
| 16 | +back-end and receiving message responses and notifications. |
| 17 | + |
| 18 | +### Constructor: new inspector.Session() |
| 19 | +<!-- YAML |
| 20 | +added: REPLACEME |
| 21 | +--> |
| 22 | + |
| 23 | +Create a new instance of the `inspector.Session` class. The inspector session |
| 24 | +needs to be connected through [`session.connect()`][] before the messages |
| 25 | +can be dispatched to the inspector backend. |
| 26 | + |
| 27 | +`inspector.Session` is an [`EventEmitter`][] with the following events: |
| 28 | + |
| 29 | +### Event: 'inspectorNotification' |
| 30 | +<!-- YAML |
| 31 | +added: REPLACEME |
| 32 | +--> |
| 33 | + |
| 34 | +* {Object} The notification message object |
| 35 | + |
| 36 | +Emitted when any notification from the V8 Inspector is received. |
| 37 | + |
| 38 | +```js |
| 39 | +session.on('inspectorNotification', (message) => console.log(message.method)); |
| 40 | +// Debugger.paused |
| 41 | +// Debugger.resumed |
| 42 | +``` |
| 43 | + |
| 44 | +It is also possible to subscribe only to notifications with specific method: |
| 45 | + |
| 46 | +### Event: <inspector-protocol-method> |
| 47 | +<!-- YAML |
| 48 | +added: REPLACEME |
| 49 | +--> |
| 50 | + |
| 51 | +* {Object} The notification message object |
| 52 | + |
| 53 | +Emitted when an inspector notification is received that has its method field set |
| 54 | +to the `<inspector-protocol-method>` value. |
| 55 | + |
| 56 | +The following snippet installs a listener on the [`Debugger.paused`][] |
| 57 | +event, and prints the reason for program suspension whenever program |
| 58 | +execution is suspended (through breakpoints, for example): |
| 59 | + |
| 60 | +```js |
| 61 | +session.on('Debugger.paused', ({params}) => console.log(params.hitBreakpoints)); |
| 62 | +// [ '/node/test/inspector/test-bindings.js:11:0' ] |
| 63 | +``` |
| 64 | + |
| 65 | +### session.connect() |
| 66 | +<!-- YAML |
| 67 | +added: REPLACEME |
| 68 | +--> |
| 69 | + |
| 70 | +Connects a session to the inspector back-end. An exception will be thrown |
| 71 | +if there is already a connected session established either through the API or by |
| 72 | +a front-end connected to the Inspector WebSocket port. |
| 73 | + |
| 74 | +### session.post(method[, params][, callback]) |
| 75 | +<!-- YAML |
| 76 | +added: REPLACEME |
| 77 | +--> |
| 78 | + |
| 79 | +* method {string} |
| 80 | +* params {Object} |
| 81 | +* callback {Function} |
| 82 | + |
| 83 | +Posts a message to the inspector back-end. `callback` will be notified when |
| 84 | +a response is received. `callback` is a function that accepts two optional |
| 85 | +arguments - error and message-specific result. |
| 86 | + |
| 87 | +```js |
| 88 | +session.post('Runtime.evaluate', {'expression': '2 + 2'}, |
| 89 | + (error, {result}) => console.log(result.value)); |
| 90 | +// Output: { type: 'number', value: 4, description: '4' } |
| 91 | +``` |
| 92 | + |
| 93 | +The latest version of the V8 inspector protocol is published on the |
| 94 | +[Chrome DevTools Protocol Viewer][]. |
| 95 | + |
| 96 | +Node inspector supports all the Chrome DevTools Protocol domains declared |
| 97 | +by V8. Chrome DevTools Protocol domain provides an interface for interacting |
| 98 | +with one of the runtime agents used to inspect the application state and listen |
| 99 | +to the run-time events. |
| 100 | + |
| 101 | +### session.disconnect() |
| 102 | +<!-- YAML |
| 103 | +added: REPLACEME |
| 104 | +--> |
| 105 | + |
| 106 | +Immediately close the session. All pending message callbacks will be called |
| 107 | +with an error. [`session.connect()`] will need to be called to be able to send |
| 108 | +messages again. Reconnected session will lose all inspector state, such as |
| 109 | +enabled agents or configured breakpoints. |
| 110 | + |
| 111 | +[`session.connect()`]: #sessionconnect |
| 112 | +[`Debugger.paused`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger/#event-paused |
| 113 | +[`EventEmitter`]: events.html#events_class_eventemitter |
| 114 | +[Chrome DevTools Protocol Viewer]: https://chromedevtools.github.io/devtools-protocol/v8/ |
0 commit comments