Skip to content

Commit

Permalink
Fall back to a random port if preferred port is privileged (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgallag authored and sindresorhus committed Dec 18, 2019
1 parent 73e21d8 commit 45a71bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -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;
}
}
Expand Down
9 changes: 9 additions & 0 deletions test.js
Expand Up @@ -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');
Expand Down

0 comments on commit 45a71bc

Please sign in to comment.