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

Handle blocked port #62

Merged
merged 2 commits into from
May 31, 2018
Merged
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
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
registry=https://registry.npmjs.org
package-lock=false
21 changes: 16 additions & 5 deletions lib/_inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ const [ InspectClient, createRepl ] =

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

class StartupError extends Error {
constructor(message) {
super(message);
this.name = 'StartupError';
}
}

function portIsFree(host, port, timeout = 2000) {
if (port === 0) return Promise.resolve(); // Binding to a random port.

Expand All @@ -51,7 +58,7 @@ function portIsFree(host, port, timeout = 2000) {
return new Promise((resolve, reject) => {
setTimeout(() => {
didTimeOut = true;
reject(new Error(
reject(new StartupError(
`Timeout (${timeout}) waiting for ${host}:${port} to be free`));
}, timeout);

Expand Down Expand Up @@ -346,10 +353,14 @@ function startInspect(argv = process.argv.slice(2),
stdin.resume();

function handleUnexpectedError(e) {
console.error('There was an internal error in node-inspect. ' +
'Please report this bug.');
console.error(e.message);
console.error(e.stack);
if (!(e instanceof StartupError)) {
console.error('There was an internal error in node-inspect. ' +
'Please report this bug.');
console.error(e.message);
console.error(e.stack);
} else {
console.error(e.message);
}
if (inspector.child) inspector.child.kill();
process.exit(1);
}
Expand Down
29 changes: 29 additions & 0 deletions test/cli/invalid-args.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';
const Path = require('path');
const { createServer } = require('net');

const { test } = require('tap');

const startCLI = require('./start-cli');
Expand All @@ -23,3 +26,29 @@ test('launch w/ invalid host:port', (t) => {
t.equal(code, 1, 'exits with non-zero exit code');
});
});

test('launch w/ unavailable port', async (t) => {
const blocker = createServer(socket => socket.end());
const port = await new Promise((resolve, reject) => {
blocker.on('error', reject);
blocker.listen(0, '127.0.0.1', () => resolve(blocker.address().port));
});

try {
const script = Path.join('examples', 'three-lines.js');
const cli = startCLI([`--port=${port}`, script]);
const code = await cli.quit();

t.notMatch(
cli.output,
'report this bug',
'Omits message about reporting this as a bug');
t.match(
cli.output,
`waiting for 127.0.0.1:${port} to be free`,
'Tells the user that the port wasn\'t available');
t.equal(code, 1, 'exits with non-zero exit code');
} finally {
blocker.close();
}
});