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
Conversation
As the limitation has been submitted through another brach
supports array of preferred ports
Array.prototype.includes is not supported on Node js versions <= 4
@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", |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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:
Line 16 in 7644e85
server.on('error', reject); |
index.js
Outdated
@@ -1,6 +1,22 @@ | |||
'use strict'; | |||
const net = require('net'); | |||
|
|||
const isAvailable = (options, callback) => { |
There was a problem hiding this comment.
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
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); | |
}); | |
}); | |
}); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
@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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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
.
That should be done separately from this PR. |
/cc @nagen010 wait this for |
@evilebottnawi if I understand correctly, I think there is no dependency to webpack-serve right? |
/cc @nagen010 what is status? |
/cc @evilebottnawi Sorry. I thought you wanted me to wait. It is almost done, I will submit it by tomorrow. |
/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) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
.
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.