freeport-async
Advanced tools
Comparing version 1.1.1 to 2.0.0
49
index.js
@@ -1,10 +0,10 @@ | ||
var net = require('net'); | ||
const net = require("net"); | ||
var DEFAULT_PORT_RANGE_START = 11000; | ||
const DEFAULT_PORT_RANGE_START = 11000; | ||
function testPortAsync(port) { | ||
return new Promise(function (fulfill, reject) { | ||
var server = net.createServer() | ||
server.listen(port, function (err) { | ||
server.once('close', function () { | ||
function testPortAsync(port, hostname) { | ||
return new Promise(function(fulfill, reject) { | ||
var server = net.createServer(); | ||
server.listen({ port: port, host: hostname }, function(err) { | ||
server.once("close", function() { | ||
setTimeout(() => fulfill(true), 0); | ||
@@ -14,3 +14,3 @@ }); | ||
}); | ||
server.on('error', function (err) { | ||
server.on("error", function(err) { | ||
setTimeout(() => fulfill(false), 0); | ||
@@ -21,16 +21,30 @@ }); | ||
async function availableAsync(port, options = {}) { | ||
const hostnames = | ||
options.hostnames && options.hostnames.length ? options.hostnames : [null]; | ||
for (const hostname of hostnames) { | ||
if (!(await testPortAsync(port, hostname))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
function freePortRangeAsync(rangeSize, rangeStart) { | ||
function freePortRangeAsync(rangeSize, rangeStart, options = {}) { | ||
rangeSize = rangeSize || 1; | ||
return new Promise(function (fulfill, reject) { | ||
return new Promise((fulfill, reject) => { | ||
var lowPort = rangeStart || DEFAULT_PORT_RANGE_START; | ||
var awaitables = []; | ||
for (var i = 0; i < rangeSize; i++) { | ||
awaitables.push(testPortAsync(lowPort + i)); | ||
awaitables.push(availableAsync(lowPort + i, options)); | ||
} | ||
return Promise.all(awaitables).then(function (results) { | ||
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); | ||
return freePortRangeAsync( | ||
rangeSize, | ||
lowPort + rangeSize, | ||
options | ||
).then(fulfill, reject); | ||
} | ||
@@ -44,6 +58,5 @@ ports.push(lowPort + i); | ||
function freePortAsync(rangeStart) { | ||
return freePortRangeAsync(1, rangeStart).then(function (result) { | ||
return result[0]; | ||
}); | ||
async function freePortAsync(rangeStart, options = {}) { | ||
const result = await freePortRangeAsync(1, rangeStart, options); | ||
return result[0]; | ||
} | ||
@@ -53,3 +66,3 @@ | ||
module.exports.availableAsync = testPortAsync; | ||
module.exports.availableAsync = availableAsync; | ||
module.exports.rangeAsync = freePortRangeAsync; |
{ | ||
"name": "freeport-async", | ||
"version": "1.1.1", | ||
"description": "Uses mikeal's code to find an open port in a given range", | ||
"version": "2.0.0", | ||
"description": "Finds an available port for your application to use.", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
@@ -11,3 +15,3 @@ "test": "echo \"Error: no test specified\" && exit 1" | ||
"type": "git", | ||
"url": "git+https://github.com/650Industries/freeport.git" | ||
"url": "https://github.com/expo/freeport-async.git" | ||
}, | ||
@@ -24,7 +28,10 @@ "keywords": [ | ||
], | ||
"author": "exponent.team@gmail.com", | ||
"author": "Expo", | ||
"bugs": { | ||
"url": "https://github.com/650Industries/freeport/issues" | ||
"url": "https://github.com/expo/freeport-async/issues" | ||
}, | ||
"homepage": "https://gist.github.com/mikeal/1840641" | ||
"homepage": "https://github.com/expo/freeport-async/blob/master/README.md", | ||
"devDependencies": { | ||
"project-repl": "^1.5.0" | ||
} | ||
} |
@@ -10,25 +10,37 @@ # freeport-async | ||
## Usage | ||
Usage: | ||
### Basic | ||
```js | ||
var freeportAsync = require('freeport-async'); | ||
let freeportAsync = require("freeport-async"); | ||
var portICanUse = await freeportAsync(); | ||
let portICanUse = await freeportAsync(); | ||
``` | ||
var portIn9000Range = await freeportAsync(9000); | ||
### Advanced | ||
var isPort5000Available = await freeportAsync.availableAsync(5000); | ||
```js | ||
let freeportAsync = require("freeport-async"); | ||
var listOf5ConsecutiveAvailablePorts = await freeportAsync.rangeAsync(5); | ||
let portIn9000Range = await freeportAsync(9000); | ||
var freeRangeIn12000Range = await freeportAsync.rangeAsync(3, 12000); | ||
let portAvailableForAnyOrLocalhost = await freeportAsync(9000, { | ||
hostnames: [null, "localhost"] | ||
}); | ||
let isPort5000Available = await freeportAsync.availableAsync(5000); | ||
let listOf5ConsecutiveAvailablePorts = await freeportAsync.rangeAsync(5); | ||
let freeRangeIn12000Range = await freeportAsync.rangeAsync(3, 12000); | ||
``` | ||
## Important Note | ||
Note that this code just finds available ports, but doesn't reserve them in any way. | ||
This means that if you have other code that might be looking for a port in the same range at the same time, you could run into issues. | ||
Also, if you call `freeportAsync` twice in a row, it will often return the same port number twice. If you want to find two (or more) ports you can use, you need to call `freeportAsync.rangeAsync(<number-of-ports>, [startSearchFrom])`. | ||
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
6886
6
57
46
1
2