From 3388969d0032a78ff0cdb8146f170b978ec13b7b Mon Sep 17 00:00:00 2001 From: Jan Krems Date: Thu, 31 May 2018 10:27:27 +0200 Subject: [PATCH 1/2] chore: Disable package-lock --- .npmrc | 1 + 1 file changed, 1 insertion(+) diff --git a/.npmrc b/.npmrc index 38f11c6..b7c8444 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1,2 @@ registry=https://registry.npmjs.org +package-lock=false From d278b233ae5e11a2b62d01ccbaae594f39b32a96 Mon Sep 17 00:00:00 2001 From: Jan Krems Date: Thu, 31 May 2018 11:30:38 +0200 Subject: [PATCH 2/2] fix: Stop asking to report a blocked port Fixes https://github.com/nodejs/node-inspect/issues/60 --- lib/_inspect.js | 21 ++++++++++++++++----- test/cli/invalid-args.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/_inspect.js b/lib/_inspect.js index d846efb..305e499 100644 --- a/lib/_inspect.js +++ b/lib/_inspect.js @@ -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. @@ -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); @@ -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); } diff --git a/test/cli/invalid-args.test.js b/test/cli/invalid-args.test.js index c831d79..112d030 100644 --- a/test/cli/invalid-args.test.js +++ b/test/cli/invalid-args.test.js @@ -1,4 +1,7 @@ 'use strict'; +const Path = require('path'); +const { createServer } = require('net'); + const { test } = require('tap'); const startCLI = require('./start-cli'); @@ -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(); + } +});