Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to a random port if preferred port is privileged #39

Merged
merged 3 commits into from Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -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;
}
}
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 => {
Copy link
Contributor Author

@mjgallag mjgallag Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thanks for catching this typo @sindresorhus, and thanks for the quick merge!

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