From 75551e17e49a1c77ce22a53468bad100764bf0a2 Mon Sep 17 00:00:00 2001 From: Michael Gallagher Date: Tue, 17 Dec 2019 14:03:45 -0800 Subject: [PATCH 1/2] Fall back to a random port if prefered port is privileged Closes #38 --- index.js | 2 +- test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7451574..35fabb4 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ module.exports = async options => { try { return await getAvailablePort({...options, port}); // eslint-disable-line no-await-in-loop } catch (error) { - if (error.code !== 'EADDRINUSE') { + if (!['EADDRINUSE', 'EACCES'].includes(error.code)) { throw error; } } diff --git a/test.js b/test.js index 65f4ab7..3627a98 100644 --- a/test.js +++ b/test.js @@ -32,6 +32,15 @@ test('preferred port unavailable', async t => { t.not(port, desiredPort); }); +test('preferred port priviliged', async t => { + const desiredPort = 80; + const port = await getPort({host: '127.0.0.1', port: desiredPort}); + + t.is(typeof port, 'number'); + t.true(port > 0); + t.not(port, desiredPort); +}); + test('port can be bound to IPv4 host when promise resolves', async t => { const port = await getPort({host: '0.0.0.0'}); t.is(typeof port, 'number'); From 2df203dce27a0d887402cfcc68b5568b4f8539a4 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 18 Dec 2019 17:59:55 +0100 Subject: [PATCH 2/2] Update test.js --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index 3627a98..7c850dc 100644 --- a/test.js +++ b/test.js @@ -32,7 +32,7 @@ test('preferred port unavailable', async t => { t.not(port, desiredPort); }); -test('preferred port priviliged', async t => { +test('preferred port privileged', async t => { const desiredPort = 80; const port = await getPort({host: '127.0.0.1', port: desiredPort});