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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '8'
- '6'
- '4'
58 changes: 47 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
'use strict';
const net = require('net');

const isAvailable = options => new Promise((resolve, reject) => {
const server = net.createServer();
server.unref();
server.on('error', reject);
server.listen(options, () => {
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
Expand All @@ -10,19 +22,43 @@ const getPort = options => new Promise((resolve, reject) => {
};
}

const server = net.createServer();

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

server.listen(options, () => {
const port = server.address().port;
server.close(() => {
resolve(port);
});
});
if (Array.isArray(options.port)) {
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 have different logic for array and number, just put the number in an array if it's not.

options.port.reduce((seq, port) => {
return seq.catch(() => {
const input = getOptions(port, options.host);
return isAvailable(input)
.then(port => {
return port;
})
.catch(() => {
return new Promise((resolve, reject) => 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.

});
});
}, Promise.reject())
.then(port => resolve(port))
.catch(() => reject());
} else {
isAvailable(options)
Copy link
Owner

Choose a reason for hiding this comment

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

This is not using getOptions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed if-else block.

.then(port => resolve(port))
.catch(() => reject());
}
});

function getOptions(portnumber, hostname) {
Copy link
Owner

Choose a reason for hiding this comment

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

portnumber => portNumber

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]);
});