-
-
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 8 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,6 +1,22 @@ | ||||
'use strict'; | ||||
const net = require('net'); | ||||
|
||||
const isAvailable = (options, callback) => { | ||||
const server = net.createServer(); | ||||
|
||||
server.unref(); | ||||
server.on('error', () => { | ||||
callback(false); | ||||
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. This is an unintended change. Before it would reject the promise with an error: Line 16 in 7644e85
|
||||
}); | ||||
|
||||
server.listen(options, () => { | ||||
const port = server.address().port; | ||||
server.close(() => { | ||||
callback(port); | ||||
}); | ||||
}); | ||||
}; | ||||
|
||||
const getPort = options => new Promise((resolve, reject) => { | ||||
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. No need to wrap it in 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. @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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. The following should do:
You will have to adapt the 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. @nagen010 what have you tried so far with |
||||
// For backwards compatibility with number-only input | ||||
// TODO: Remove this in the next major version | ||||
|
@@ -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); | ||||
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. You can't iterate like this. |
||||
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) { | ||||
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.
|
||||
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); |
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", | ||
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. 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", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 => { | ||
|
@@ -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 | ||
|
||
|
@@ -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 commentThe 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 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. Sure. I will just use the port option to handle both inputs |
||
|
||
Type: `Array` | ||
|
||
The array of preferred ports to use | ||
|
||
##### host | ||
|
||
Type: `string` | ||
|
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
get-port/index.js
Lines 4 to 24 in 7644e85
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.