freeport-async
Advanced tools
Comparing version 1.0.0 to 1.1.0
62
index.js
var net = require('net'); | ||
module.exports = function (rangeStart) { | ||
var DEFAULT_PORT_RANGE_START = 11000; | ||
function testPortAsync(port) { | ||
return new Promise(function (fulfill, reject) { | ||
var portrange = rangeStart || 11000; | ||
function getPort (cb) { | ||
var port = portrange | ||
portrange += 1 | ||
var server = net.createServer() | ||
server.listen(port, function (err) { | ||
server.once('close', function () { | ||
fulfill(true); | ||
}); | ||
server.close(); | ||
}); | ||
server.on('error', function (err) { | ||
fulfill(false); | ||
}); | ||
}); | ||
} | ||
var server = net.createServer() | ||
server.listen(port, function (err) { | ||
server.once('close', function () { | ||
cb(port) | ||
}) | ||
server.close() | ||
}) | ||
server.on('error', function (err) { | ||
getPort(cb) | ||
}) | ||
function freePortRangeAsync(rangeSize, rangeStart) { | ||
rangeSize = rangeSize || 1; | ||
return new Promise(function (fulfill, reject) { | ||
var lowPort = rangeStart || DEFAULT_PORT_RANGE_START; | ||
var awaitables = []; | ||
for (var i = 0; i < rangeSize; i++) { | ||
awaitables.push(testPortAsync(lowPort + i)); | ||
} | ||
getPort(fulfill); | ||
return Promise.all(awaitables).then(function (results) { | ||
var ports = []; | ||
for (var i = 0; i < results.length; i++) { | ||
if (!results[i]) { | ||
return freePortRangeAsync(rangeSize, lowPort + rangeSize).then(fulfill, reject); | ||
} | ||
ports.push(lowPort + i); | ||
} | ||
fulfill(ports); | ||
}); | ||
}); | ||
}; | ||
} | ||
function freePortAsync(rangeStart) { | ||
return freePortRangeAsync(1, rangeStart).then(function (result) { | ||
return result[0]; | ||
}); | ||
} | ||
module.exports = freePortAsync; | ||
module.exports.availableAsync = testPortAsync; | ||
module.exports.rangeAsync = freePortRangeAsync; |
{ | ||
"name": "freeport-async", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Uses mikeal's code to find an open port in a given range", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# freeport | ||
Uses mikeal's code to find an open port in a given range, | ||
and presents a Promise-based interface | ||
Finds an available port for your application to use. | ||
You can specify a range where to look for an available port. | ||
And can also find a range of available ports for you to use. | ||
You can also be used to test to see if a given port is available. | ||
All functions are async and return Promises. | ||
Usage: | ||
@@ -14,5 +19,11 @@ ```js | ||
var isPort5000Available = await freeportAsync.availableAsync(5000); | ||
var listOf5ConsecutiveAvailablePorts = await freeportAsync.rangeAsync(5); | ||
var freeRangeIn12000Range = await freeportAsync.rangeAsync(3, 12000); | ||
``` | ||
See https://gist.github.com/mikeal/1840641 | ||
See also https://gist.github.com/mikeal/1840641 | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4316
44
29