-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inspector: allow --inspect=host:port from js #13228
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
'use strict'; | ||
|
||
const connect = process.binding('inspector').connect; | ||
const EventEmitter = require('events'); | ||
const util = require('util'); | ||
const { connect, open, url } = process.binding('inspector'); | ||
|
||
if (!connect) | ||
throw new Error('Inspector is not available'); | ||
|
@@ -83,5 +83,8 @@ class Session extends EventEmitter { | |
} | ||
|
||
module.exports = { | ||
open: (port, host, wait) => open(port, host, !!wait), | ||
close: process._debugEnd, | ||
url: url, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re: #9659 I now think this should have been a |
||
Session | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// Test inspector open()/close()/url() API. It uses ephemeral ports so can be | ||
// run safely in parallel. | ||
|
||
const assert = require('assert'); | ||
const fork = require('child_process').fork; | ||
const net = require('net'); | ||
const url = require('url'); | ||
|
||
common.skipIfInspectorDisabled(); | ||
|
||
if (process.env.BE_CHILD) | ||
return beChild(); | ||
|
||
const child = fork(__filename, {env: {BE_CHILD: 1}}); | ||
|
||
child.once('message', common.mustCall((msg) => { | ||
assert.strictEqual(msg.cmd, 'started'); | ||
|
||
child.send({cmd: 'open', args: [0]}); | ||
child.once('message', common.mustCall(firstOpen)); | ||
})); | ||
|
||
let firstPort; | ||
|
||
function firstOpen(msg) { | ||
assert.strictEqual(msg.cmd, 'url'); | ||
const port = url.parse(msg.url).port; | ||
ping(port, (err) => { | ||
assert.ifError(err); | ||
// Inspector is already open, and won't be reopened, so args don't matter. | ||
child.send({cmd: 'open', args: []}); | ||
child.once('message', common.mustCall(tryToOpenWhenOpen)); | ||
firstPort = port; | ||
}); | ||
} | ||
|
||
function tryToOpenWhenOpen(msg) { | ||
assert.strictEqual(msg.cmd, 'url'); | ||
const port = url.parse(msg.url).port; | ||
// Reopen didn't do anything, the port was already open, and has not changed. | ||
assert.strictEqual(port, firstPort); | ||
ping(port, (err) => { | ||
assert.ifError(err); | ||
child.send({cmd: 'close'}); | ||
child.once('message', common.mustCall(closeWhenOpen)); | ||
}); | ||
} | ||
|
||
function closeWhenOpen(msg) { | ||
assert.strictEqual(msg.cmd, 'url'); | ||
assert.strictEqual(msg.url, undefined); | ||
ping(firstPort, (err) => { | ||
assert(err); | ||
child.send({cmd: 'close'}); | ||
child.once('message', common.mustCall(tryToCloseWhenClosed)); | ||
}); | ||
} | ||
|
||
function tryToCloseWhenClosed(msg) { | ||
assert.strictEqual(msg.cmd, 'url'); | ||
assert.strictEqual(msg.url, undefined); | ||
child.send({cmd: 'open', args: []}); | ||
child.once('message', common.mustCall(reopenAfterClose)); | ||
} | ||
|
||
function reopenAfterClose(msg) { | ||
assert.strictEqual(msg.cmd, 'url'); | ||
const port = url.parse(msg.url).port; | ||
assert.notStrictEqual(port, firstPort); | ||
ping(port, (err) => { | ||
assert.ifError(err); | ||
process.exit(); | ||
}); | ||
} | ||
|
||
function ping(port, callback) { | ||
net.connect(port) | ||
.on('connect', function() { close(this); }) | ||
.on('error', function(err) { close(this, err); }); | ||
|
||
function close(self, err) { | ||
self.end(); | ||
self.on('close', () => callback(err)); | ||
} | ||
} | ||
|
||
function beChild() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it not worth moving this to a fixture? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code is used nowhere else, and is intimately tied to this specific test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
const inspector = require('inspector'); | ||
|
||
process.send({cmd: 'started'}); | ||
|
||
process.on('message', (msg) => { | ||
if (msg.cmd === 'open') { | ||
inspector.open(...msg.args); | ||
} | ||
if (msg.cmd === 'close') { | ||
inspector.close(); | ||
} | ||
process.send({cmd: 'url', url: inspector.url()}); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe document the signatures, a la
session.post
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I'll do