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

Conversation

nagen010
Copy link
Contributor

@nagen010 nagen010 commented May 4, 2018

I have added new key ports to options that accept a list of preferred port numbers. Updated tests and documentation. It closes #19. Please review and let me know if I need to update something. Thanks.

@nagen010
Copy link
Contributor Author

nagen010 commented May 7, 2018

@sindresorhus Please review and let me know if it is okay :)

package.json Outdated
@@ -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.

readme.md Outdated
@@ -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

index.js Outdated

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

index.js Outdated
@@ -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.

index.js Outdated
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.

@nagen010
Copy link
Contributor Author

@sindresorhus xo latest version v0.21.0 dropped support for Node.js 4 https://github.com/xojs/xo/releases/tag/v0.21.0. Shall I drop Node.js 4 support?

Dropping Node.js 4 support as xo doesn't support it anymore.
index.js Outdated
.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.

index.js Outdated
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.

index.js Outdated
});
});
});

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?

index.js Outdated
});

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

index.js Outdated
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.

@sindresorhus
Copy link
Owner

sindresorhus commented May 15, 2018

Shall I drop Node.js 4 support?

That should be done separately from this PR.

@alexander-akait
Copy link

alexander-akait commented May 18, 2018

/cc @nagen010 wait this for webpack-serve (next modular and plugable webpack dev server), thanks for work!

@nagen010
Copy link
Contributor Author

@evilebottnawi if I understand correctly, I think there is no dependency to webpack-serve right?

@alexander-akait
Copy link

@nagen010 https://github.com/webpack-contrib/webpack-serve/blob/master/package.json#L39

@alexander-akait
Copy link

/cc @nagen010 what is status?

@nagen010
Copy link
Contributor Author

/cc @evilebottnawi Sorry. I thought you wanted me to wait. It is almost done, I will submit it by tomorrow.

@alexander-akait
Copy link

/cc @sindresorhus will be great if you find time on review 👍

index.js Outdated
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))

index.js Outdated
});
});
}, 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!

index.js Outdated
.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.

index.js Outdated
}, Promise.reject()).then(resolve).catch(reject);
});

function getOptions(port, host) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you think this function can be removed after all?

The following will do just fine:

if (typeof options === 'number') {
   options = { port: options };
}

and

isAvailable(Object.assign({}, options, { port }));

With Object.assign, you retain the previously implemented behavior which is the ability to eventually pass more options to server.listen.

@sindresorhus sindresorhus merged commit f5c9753 into sindresorhus:master Jul 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array of preferred ports instead of just one number
4 participants