Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #41 from nodejs/custom-port
Browse files Browse the repository at this point in the history
Verify custom port support
  • Loading branch information
jkrems authored Apr 3, 2017
2 parents 7b31acf + e3a489f commit 8612100
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
15 changes: 7 additions & 8 deletions lib/_inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const [ InspectClient, createRepl ] =

const debuglog = util.debuglog('inspect');

const DEBUG_PORT_PATTERN = /^--(?:debug|inspect)-port=(\d+)$/;
const DEBUG_PORT_PATTERN = /^--(?:debug|inspect)(?:-port|-brk)?=(\d{1,5})$/;
function getDefaultPort() {
for (const arg of process.execArgv) {
const match = arg.match(DEBUG_PORT_PATTERN);
Expand All @@ -56,8 +56,7 @@ function getDefaultPort() {
function runScript(script, scriptArgs, inspectPort, childPrint) {
return new Promise((resolve) => {
const args = [
'--inspect',
`--debug-brk=${inspectPort}`,
`--inspect-brk=${inspectPort}`,
].concat([script], scriptArgs);
const child = spawn(process.execPath, args);
child.stdout.setEncoding('utf8');
Expand All @@ -68,7 +67,7 @@ function runScript(script, scriptArgs, inspectPort, childPrint) {
let output = '';
function waitForListenHint(text) {
output += text;
if (/chrome-devtools:\/\//.test(output)) {
if (/^Debugger listening on/.test(output)) {
child.stderr.removeListener('data', waitForListenHint);
resolve(child);
}
Expand Down Expand Up @@ -295,6 +294,7 @@ function parseArgv([target, ...args]) {

const hostMatch = target.match(/^([^:]+):(\d+)$/);
const portMatch = target.match(/^--port=(\d+)$/);

if (hostMatch) {
// Connecting to remote debugger
// `node-inspect localhost:9229`
Expand All @@ -303,16 +303,15 @@ function parseArgv([target, ...args]) {
isRemote = true;
script = null;
} else if (portMatch) {
// Start debugger on custom port
// `node debug --port=8058 app.js`
// start debugee on custom port
// `node inspect --port=9230 script.js`
port = parseInt(portMatch[1], 10);
script = args[0];
scriptArgs = args.slice(1);
}

return {
host, port,
isRemote, script, scriptArgs,
host, port, isRemote, script, scriptArgs,
};
}

Expand Down
25 changes: 25 additions & 0 deletions test/cli/launch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ const { test } = require('tap');

const startCLI = require('./start-cli');

test('custom port', (t) => {
const CUSTOM_PORT = '9230';
const script = Path.join('examples', 'three-lines.js');

const cli = startCLI([`--port=${CUSTOM_PORT}`, script]);

return cli.waitForInitialBreak()
.then(() => cli.waitForPrompt())
.then(() => {
t.match(cli.output, 'debug>', 'prints a prompt');
t.match(
cli.output,
new RegExp(`< Debugger listening on [^\n]*${CUSTOM_PORT}`),
'forwards child output');
})
.then(() => cli.quit())
.then((code) => {
t.equal(code, 0, 'exits with success');
});
});

test('examples/three-lines.js', (t) => {
const script = Path.join('examples', 'three-lines.js');
const cli = startCLI([script]);
Expand All @@ -13,6 +34,10 @@ test('examples/three-lines.js', (t) => {
.then(() => cli.waitForPrompt())
.then(() => {
t.match(cli.output, 'debug>', 'prints a prompt');
t.match(
cli.output,
new RegExp(`< Debugger listening on [^\n]*9229`),
'forwards child output');
})
.then(() => cli.command('["hello", "world"].join(" ")'))
.then(() => {
Expand Down
8 changes: 5 additions & 3 deletions test/cli/start-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function startCLI(args) {
return this.waitFor(/break (?:on start )?in/i, timeout)
.then(() => {
if (/Break on start/.test(this.output)) {
return this.command('n')
return this.command('next', false)
.then(() => this.waitFor(/break in/, timeout));
}
});
Expand All @@ -127,8 +127,10 @@ function startCLI(args) {
.map((match) => +match[1]);
},

command(input) {
this.flushOutput();
command(input, flush = true) {
if (flush) {
this.flushOutput();
}
child.stdin.write(input);
child.stdin.write('\n');
return this.waitForPrompt();
Expand Down

0 comments on commit 8612100

Please sign in to comment.