freeport-async
Advanced tools
Sorry, the diff of this file is not supported yet
+42
| # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
| # yarn lockfile v1 | ||
| balanced-match@^1.0.0: | ||
| version "1.0.0" | ||
| resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" | ||
| integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= | ||
| brace-expansion@^1.1.7: | ||
| version "1.1.11" | ||
| resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" | ||
| integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA== | ||
| dependencies: | ||
| balanced-match "^1.0.0" | ||
| concat-map "0.0.1" | ||
| concat-map@0.0.1: | ||
| version "0.0.1" | ||
| resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" | ||
| integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= | ||
| minimatch@3.0.4: | ||
| version "3.0.4" | ||
| resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" | ||
| integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== | ||
| dependencies: | ||
| brace-expansion "^1.1.7" | ||
| project-repl@^1.5.0: | ||
| version "1.5.0" | ||
| resolved "https://registry.yarnpkg.com/project-repl/-/project-repl-1.5.0.tgz#695d945235594eebe32d80c7ebea2318cb81487f" | ||
| integrity sha512-ss831p9dFzv8JEp3qFJSOsCOk66Z4afwMxvyXrkICpce5jytqyqncuaPJjQfVNpS3fxpOCwwqE2gZ3AcbrmdCA== | ||
| dependencies: | ||
| recursive-readdir "^2.2.2" | ||
| recursive-readdir@^2.2.2: | ||
| version "2.2.2" | ||
| resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f" | ||
| integrity sha512-nRCcW9Sj7NuZwa2XvH9co8NPeXUBhZP7CRKJtU+cS6PW9FpCIFoI5ib0NT1ZrbNuPoRy0ylyCaUL8Gih4LSyFg== | ||
| dependencies: | ||
| minimatch "3.0.4" |
+31
-18
@@ -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; |
+13
-6
| { | ||
| "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" | ||
| } | ||
| } |
+21
-9
@@ -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 | ||
Sorry, the diff of this file is not supported yet
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
6886
42.83%6
20%57
29.55%46
35.29%1
Infinity%2
100%