This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to not use net-cluster and underscore
- Loading branch information
Showing
5 changed files
with
189 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var net = require('net'); | ||
|
||
var DEFAULT_MIN = 1025; | ||
var DEFAULT_MAX = 65535; | ||
var DEFAULT_MAX_ATTEMPTS = 50; | ||
|
||
function getRandomPort (opts, cb) { | ||
var min = DEFAULT_MIN; | ||
var max = DEFAULT_MAX; | ||
var maxAttempts = DEFAULT_MAX_ATTEMPTS; | ||
var options = opts || {}; | ||
var host; | ||
var port; | ||
var server; | ||
|
||
if (options.min != null) { | ||
min = options.min; | ||
} | ||
|
||
if (options.max != null) { | ||
max = options.max; | ||
} | ||
|
||
if (options.maxAttempts != null) { | ||
maxAttempts = options.maxAttempts; | ||
} | ||
|
||
if (options.host != null) { | ||
host = options.host; | ||
} | ||
|
||
port = getRandomNumber(min, max); | ||
|
||
server = net.createServer(); | ||
|
||
server.listen(port, host, function () { | ||
server.once('close', function () { cb(null, port) }); | ||
server.close(); | ||
}); | ||
|
||
server.on('error', function () { | ||
if (--maxAttempts) { | ||
return getRandomPort({ | ||
min: min, | ||
max: max, | ||
maxAttempts: maxAttempts | ||
}, cb); | ||
} | ||
|
||
cb(new Error('Could not find an available port')); | ||
}); | ||
} | ||
|
||
function getRandomNumber (minimum, maximum) { | ||
if (maximum === undefined) { | ||
maximum = minimum; | ||
minimum = 0; | ||
} | ||
|
||
if (typeof minimum !== 'number' || typeof maximum !== 'number') { | ||
throw new TypeError('Expected all arguments to be numbers'); | ||
} | ||
|
||
return Math.floor( | ||
(Math.random() * (maximum - minimum + 1)) + minimum | ||
); | ||
} | ||
|
||
module.exports = getRandomPort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.