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

Support array of preferred ports #21

Merged
merged 16 commits into from
Jul 24, 2018
56 changes: 44 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
'use strict';
const net = require('net');

const getPort = options => new Promise((resolve, reject) => {
// For backwards compatibility with number-only input
// TODO: Remove this in the next major version
if (typeof options === 'number') {
options = {
port: options
};
}

const isAvailable = options => new Promise((resolve, reject) => {
const server = net.createServer();

server.unref();
server.on('error', reject);

server.listen(options, () => {
const port = server.address().port;
const {port} = server.address();
server.close(() => {
resolve(port);
});
});
});

const getPort = options => new Promise((resolve, reject) => {
Copy link
Owner

Choose a reason for hiding this comment

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

No need to wrap it in new Promise. You can just return the promise instead.

https://twitter.com/sindresorhus/status/989804425584627712

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sindresorhus It looks like util.promisify function was included only in NodeJS version 8.

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

// @sindresorhus sorry. I am not sure how to use pify here. Could you please help me with some examples? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

The following should do:

const getPort = pify((options, callback) => { ... });

You will have to adapt the resolve and reject logic accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

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

@nagen010 what have you tried so far with pify?

// For backwards compatibility with number-only input
// TODO: Remove this in the next major version
if (typeof options === 'number') {
options = getOptions(options);
}

if (typeof options.port === 'number') {
options.port = [options.port];
}

options.port.reduce((seq, port) => {
return seq.catch(() => {
const input = getOptions(port, options.host);
return isAvailable(input)
Copy link
Contributor

Choose a reason for hiding this comment

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

Or simply:

return isAvailable(input).then(port => port).catch(Promise.reject.bind(Promise))

.then(port => {
return port;
})
.catch(() => {
return new Promise((resolve, reject) => reject());
});
});
}, Promise.reject())
.then(port => resolve(port))
Copy link
Contributor

Choose a reason for hiding this comment

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

Or simply: .then(resolve).catch(reject)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback!

.catch(() => reject());
});

function getOptions(portNumber, hostname) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Or simply:

function getOptions(port, host) {
   return { port, host };
}

which makes me wonder if this function is really that necessary.

let options;
if (hostname === undefined) {
options = {
port: portNumber
};
} else {
options = {
port: portNumber,
host: hostname
};
}
return options;
}

module.exports = options => options ?
getPort(options).catch(() => getPort(0)) :
getPort(0);
14 changes: 11 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ getPort().then(port => {
});
```

Optionally, pass in a preferred port:
To pass in a preferred port:

```js
getPort({port: 3000}).then(port => {
Expand All @@ -30,6 +30,14 @@ getPort({port: 3000}).then(port => {
});
```

To pass in an array of preferred ports:

```js
getPort({port: [3000, 3001, 3002]}).then(port => {
console.log(port);
// Will use any element in the preferred ports array if available, otherwise fall back to a random port
});
```

## API

Expand All @@ -43,9 +51,9 @@ Type: `Object`

##### port

Type: `number`
Type: `number` or `Array`

The preferred port to use.
A preferred port or an array of preferred ports to use.

##### host

Expand Down
41 changes: 41 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,44 @@ test('preferred port given IPv4 host', async t => {

t.is(port, desiredPort);
});

test('preferred ports', async t => {
const desiredPorts = [9910, 9912, 9913];
const port = await m({
port: desiredPorts,
host: '0.0.0.0'
});

t.is(port, desiredPorts[0]);
});

test('first port in preferred ports array is unavailable', async t => {
const desiredPorts = [9090, 9091];

const server = net.createServer();
await pify(server.listen.bind(server))(desiredPorts[0]);

const port = await m({
port: desiredPorts
});

t.is(port, desiredPorts[1]);
});

test('all preferred ports in array are unavailable', async t => {
const desiredPorts = [9990, 9991];

const server1 = net.createServer();
await pify(server1.listen.bind(server1))(desiredPorts[0]);
const server2 = net.createServer();
await pify(server2.listen.bind(server2))(desiredPorts[1]);

const port = await m({
port: desiredPorts
});

t.is(typeof port, 'number');
t.true(port > 0 && port < 65536);
t.not(port, desiredPorts[0]);
t.not(port, desiredPorts[1]);
});