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

test: custom port #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 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', 'empty.js');

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

return cli.waitFor(/break/)
.then(() => cli.waitForPrompt())
.then(() => {
t.match(cli.output, 'debug>', 'prints a prompt');
t.match(
cli.output,
`< Debugger listening on port ${CUSTOM_PORT}`,
'forwards child output');
})
.then(() => cli.quit())
.then((code) => {
t.equal(code, 0, 'exits with success');
});
});

test('examples/empty.js', (t) => {
const script = Path.join('examples', 'empty.js');
const cli = startCLI([script]);
Expand Down