-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Changes from 13 commits
b81421a
68cc4b5
65df48b
c771754
e2ac69b
97fe639
2c911e3
11b3f68
d584354
57c876d
50fa4d1
225fca3
785095a
c8e3194
78ae021
cce4b2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) => { | ||
// 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or simply:
|
||
.then(port => { | ||
return port; | ||
}) | ||
.catch(() => { | ||
return new Promise((resolve, reject) => reject()); | ||
}); | ||
}); | ||
}, Promise.reject()) | ||
.then(port => resolve(port)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or simply: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback! |
||
.catch(() => reject()); | ||
}); | ||
|
||
function getOptions(portNumber, hostname) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or simply:
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); |
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.https://twitter.com/sindresorhus/status/989804425584627712
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.
Use https://github.com/sindresorhus/pify
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:
You will have to adapt the
resolve
andreject
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
?