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
61 changes: 51 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
'use strict';
const net = require('net');

const isAvailable = (options, callback) => {
Copy link
Owner

Choose a reason for hiding this comment

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

This should be kept a promise, so it would be exactly like

get-port/index.js

Lines 4 to 24 in 7644e85

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 server = net.createServer();
server.unref();
server.on('error', reject);
server.listen(options, () => {
const port = server.address().port;
server.close(() => {
resolve(port);
});
});
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I will keep the promise.

const server = net.createServer();

server.unref();
server.on('error', () => {
callback(false);
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 an unintended change. Before it would reject the promise with an error:

server.on('error', reject);

});

server.listen(options, () => {
const port = server.address().port;
server.close(() => {
callback(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 +26,44 @@ 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 ('ports' in options) {
options.ports.forEach((e, index) => {
const input = getOptions(e, options.host);
Copy link
Owner

Choose a reason for hiding this comment

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

You can't iterate like this. isAvailable is async, so if item 2 takes longer than item 3, you might get it resolved with item 3 even though item 2 is eventually available.

isAvailable(input, res => {
if (res === false) {
if (index !== options.ports.length - 1) {
return;
}
reject();
}
resolve(res);
});
});
});
} else {
isAvailable(options, res => {
if (res === false) {
reject();
}
resolve(res);
});
}
});

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 (typeof hostname === undefined) {
options = {
port: portnumber
};
} else {
options = {
port: portnumber,
host: hostname
};
}
return options;
}

module.exports = options => options ?
getPort(options).catch(() => getPort(0)) :
getPort(0);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "get-port",
"version": "3.2.0",
"version": "3.3.0",
Copy link
Owner

Choose a reason for hiding this comment

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

Don't bump versions in a PR. It's up to the maintainer to do this.

"description": "Get an available port",
"license": "MIT",
"repository": "sindresorhus/get-port",
Expand Down
16 changes: 15 additions & 1 deletion 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 arrays of preferred ports:

```js
getPort({ports: [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 @@ -47,6 +55,12 @@ Type: `number`

The preferred port to use.

##### ports
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think it should be a separate option. Just let the port option accept either a number or array of numbers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I will just use the port option to handle both inputs


Type: `Array`

The array of preferred ports to use

##### host

Type: `string`
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({
ports: 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({
ports: 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({
ports: desiredPorts
});

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