From 45a71bcec06221870083e1d20cb959a225321bda Mon Sep 17 00:00:00 2001 From: Michael Gallagher Date: Wed, 18 Dec 2019 09:04:12 -0800 Subject: [PATCH] Fall back to a random port if preferred port is privileged (#39) --- index.js | 2 +- test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0e8b829..da4a967 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,7 @@ module.exports = async options => { lockedPorts.young.add(availablePort); return availablePort; } catch (error) { - if (error.code !== 'EADDRINUSE' && !(error instanceof Locked)) { + if (!['EADDRINUSE', 'EACCES'].includes(error.code) && !(error instanceof Locked)) { throw error; } } diff --git a/test.js b/test.js index 8981ae1..ba4c794 100644 --- a/test.js +++ b/test.js @@ -32,6 +32,15 @@ test('preferred port unavailable', async t => { t.not(port, desiredPort); }); +test('preferred port privileged', 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');