Skip to content

Commit

Permalink
inspector: expose original console
Browse files Browse the repository at this point in the history
Adds require('inspector').console, mapping it to the original
global.console of V8. This enables applications to send messages to
the inspector console programmatically.

Fixes: nodejs#21651
  • Loading branch information
mcollina committed Jul 5, 2018
1 parent e021ca2 commit 18fff38
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
11 changes: 11 additions & 0 deletions doc/api/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ started.
If wait is `true`, will block until a client has connected to the inspect port
and flow control has been passed to the debugger client.

### inspector.console

An object to send messages to the remote inspector console.

```js
require('inspector').console.log('a message');
```

The inspector console does not have API parity with Node.js
console.

### inspector.close()

Deactivate the inspector. Blocks until there are no active connections.
Expand Down
2 changes: 2 additions & 0 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
} = require('internal/errors').codes;
const util = require('util');
const { Connection, open, url } = process.binding('inspector');
const { originalConsole } = require('internal/process/per_thread');

if (!Connection || !require('internal/worker').isMainThread)
throw new ERR_INSPECTOR_NOT_AVAILABLE();
Expand Down Expand Up @@ -103,5 +104,6 @@ module.exports = {
open: (port, host, wait) => open(port, host, !!wait),
close: process._debugEnd,
url: url,
console: originalConsole,
Session
};
2 changes: 2 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@

const browserGlobals = !process._noBrowserGlobals;
if (browserGlobals) {
// we are setting this here to foward it to the inspector later
perThreadSetup.originalConsole = global.console;
setupGlobalTimeouts();

This comment has been minimized.

Copy link
@jdalton

jdalton Jul 5, 2018

@mcollina
Can you explain this bit some more.
Is the original console (the one not produced by the lib/console) automatically piped to the inspector?

This comment has been minimized.

Copy link
@mcollina

mcollina Jul 5, 2018

Author Owner

The original console is only tied to the inspector.

This comment has been minimized.

Copy link
@jdalton

jdalton Jul 5, 2018

Ah! How does it work today where I can have a file foo.js, that does console.log("foo"), and node --inspect-brk foo.js, then continue execution in the inspector and have it log foo to the terminal and the chrome inspector? (The console.log from foo.js use is the lib/console one.)

This comment has been minimized.

Copy link
@mcollina

mcollina Jul 5, 2018

Author Owner

setupInspector  function in https://github.com/nodejs/node/blob/master/lib/internal/bootstrap/node.js#L417-L449 sets up a double-routing path for that call, so that it ends both in our console and in the inspector one.

setupGlobalConsole();
setupGlobalURL();
Expand Down
10 changes: 8 additions & 2 deletions test/common/inspector-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fixtures = require('../common/fixtures');
const { spawn } = require('child_process');
const { parse: parseURL } = require('url');
const { getURLFromFilePath } = require('internal/url');
const { EventEmitter } = require('events');

const _MAINSCRIPT = fixtures.path('loop.js');
const DEBUG = false;
Expand Down Expand Up @@ -311,10 +312,12 @@ class InspectorSession {
}
}

class NodeInstance {
class NodeInstance extends EventEmitter {
constructor(inspectorFlags = ['--inspect-brk=0'],
scriptContents = '',
scriptFile = _MAINSCRIPT) {
super();

this._scriptPath = scriptFile;
this._script = scriptFile ? null : scriptContents;
this._portCallback = null;
Expand All @@ -326,7 +329,10 @@ class NodeInstance {
this._unprocessedStderrLines = [];

this._process.stdout.on('data', makeBufferingDataCallback(
(line) => console.log('[out]', line)));
(line) => {
this.emit('stdout', line);
console.log('[out]', line);
}));

this._process.stderr.on('data', makeBufferingDataCallback(
(message) => this.onStderrLine(message)));
Expand Down
39 changes: 39 additions & 0 deletions test/sequential/test-inspector-console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Flags: --expose-internals
'use strict';

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

const { NodeInstance } = require('../common/inspector-helper.js');
const assert = require('assert');

async function runTest() {
const script = 'require(\'inspector\').console.log(\'hello world\');';
const child = new NodeInstance('--inspect-brk=0', script, '');

let out = '';
child.on('stdout', (line) => out += line);

const session = await child.connectInspectorSession();

const commands = [
{ 'method': 'Runtime.enable' },
{ 'method': 'Runtime.runIfWaitingForDebugger' }
];

session.send(commands);

const msg = await session.waitForNotification('Runtime.consoleAPICalled');

assert.strictEqual(msg.params.type, 'log');
assert.deepStrictEqual(msg.params.args, [{
type: 'string',
value: 'hello world'
}]);
assert.strictEqual(out, '');

session.disconnect();
}

common.crashOnUnhandledRejection();
runTest();

0 comments on commit 18fff38

Please sign in to comment.