New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

find-free-ports

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-free-ports - npm Package Compare versions

Comparing version 2.0.3 to 3.0.0

.browserslistrc

13

index.d.ts

@@ -1,3 +0,10 @@

export { default } from "./src/findFreePorts"
export interface FindFreePortsOptions {
startPort?: number;
endPort?: number;
jobCount?: number;
isFree?: (port: number) => Promise<boolean>;
}
export declare function findFreePorts(count?: number, { endPort, startPort, jobCount, isFree }?: FindFreePortsOptions): Promise<number[]>;
export declare function isFreePort(port: number): Promise<boolean>;
export default findFreePorts;
//# sourceMappingURL=index.d.ts.map

@@ -1,2 +0,92 @@

module.exports=function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=2)}([function(e,t){e.exports=require("net")},function(e,t,r){"use strict";r.r(t),r.d(t,"isFree",(function(){return o}));var n=r(0);function o(e){return new Promise((t,r)=>{const o=n.createConnection(e);o.once("connect",()=>{o.end(),t(!1)}),o.once("error",e=>{o.destroy(),"ECONNREFUSED"===e.code?t(!0):r(e)})})}t.default=o},function(e,t,r){"use strict";r.r(t),r.d(t,"findFreePorts",(function(){return o}));var n=r(1);async function o(e=1,t={}){var r,o,u;const i=null!==(r=t.startPort)&&void 0!==r?r:1025,f=null!==(o=t.endPort)&&void 0!==o?o:65535,l=Math.min(e,null!==(u=t.jobCount)&&void 0!==u?u:10);if(e>f-i)throw new Error("Could not find free ports: the range of allowed ports is not large enough for the requested amount of ports to find.");const a=Math.ceil((f-i)/l),c=[],d=[];for(let e=0;e<l;e++)d.push(s(i+a*e,Math.min(f,i+a*(e+1))));if(await Promise.all(d),c.length<e)throw new Error("Could not find free ports: there were not enough ports available.");return c;async function s(t,r){for(let o=t;o<r&&!(c.length>=e);o++)if(await Object(n.default)(o)){if(c.length>=e)break;c.push(o)}}}t.default=o}]).default;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findFreePorts = findFreePorts;
exports.isFreePort = isFreePort;
exports.default = void 0;
var _os = _interopRequireDefault(require("os"));
var _net = _interopRequireDefault(require("net"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const MIN_PORT = 1025;
const MAX_PORT = 65535;
const DEFAULT_JOB_COUNT = _os.default.cpus().length;
function clamp(value, min, max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
async function findFreePorts(count = 1, {
endPort = MAX_PORT,
startPort = MIN_PORT,
jobCount = DEFAULT_JOB_COUNT,
isFree = isFreePort
} = {}) {
if (count > endPort - startPort) {
throw new Error(`Could not find free ports: the range of allowed ports is not large enough for the requested amount of ports to find.`);
}
const portInterval = Math.ceil((endPort - startPort) / jobCount);
const ports = [];
const jobPromises = [];
for (let i = 0; i < jobCount; i++) {
jobPromises.push(scanRange(startPort + portInterval * i, Math.min(endPort, startPort + portInterval * (i + 1))));
}
await Promise.all(jobPromises);
if (ports.length < count) {
throw new Error(`Could not find free ports: there were not enough ports available.`);
}
return ports;
async function scanRange(startPort, endPort) {
for (let port = startPort; port < endPort; port++) {
if (ports.length >= count) {
break;
}
if (await isFree(port)) {
if (ports.length >= count) {
break;
}
ports.push(port);
}
}
}
}
function isFreePort(port) {
return new Promise((accept, reject) => {
const sock = _net.default.createConnection(port);
sock.once('connect', () => {
sock.end();
accept(false);
});
sock.once('error', e => {
sock.destroy();
if (e.code === 'ECONNREFUSED') {
accept(true);
} else {
reject(e);
}
});
});
}
var _default = findFreePorts;
exports.default = _default;
//# sourceMappingURL=index.js.map
{
"name": "find-free-ports",
"version": "2.0.3",
"version": "3.0.0",
"description": "Find multiple free ports on localhost",
"main": "index.js",
"scripts": {
"prepare": "webpack --config webpack.config.js",
"watch": "webpack --watch --config webpack.config.js",
"test": "node test.js"
"prepare": "tsc && babel --extensions '.ts' index.ts tests.ts --out-dir . --source-maps",
"watch": "concurrently 'ava --watch' 'babel --watch --extensions .ts index.ts tests.ts --out-dir .'",
"test": "ava"
},

@@ -18,4 +18,6 @@ "repository": {

"port",
"networking",
"server",
"scanner"
"client",
"library"
],

@@ -29,19 +31,11 @@ "author": "Sam Vervaeck",

"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@types/chai": "^4.2.11",
"@types/chai-as-promised": "^7.1.2",
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.13",
"@types/tape": "^4.13.0",
"babel-loader": "^8.1.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"source-map-support": "^0.5.19",
"tape": "^5.0.1",
"ts-loader": "^7.0.5",
"typescript": "^3.9.5",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
"@babel/cli": "^7.14.8",
"@babel/core": "^7.14.8",
"@babel/preset-env": "^7.14.8",
"@babel/preset-typescript": "^7.14.5",
"@types/node": "^16.4.7",
"ava": "^3.15.0",
"concurrently": "^6.2.0",
"typescript": "^4.3.5"
}
}

@@ -52,3 +52,41 @@ [![Build Status](https://travis-ci.org/samvv/node-find-free-ports.svg?branch=master)](https://travis-ci.org/samvv/node-find-free-ports)

## API
### findFreePorts(count?, opts?)
```js
import { findFreePorts } from "find-free-ports"
```
Search for the specified amount of free ports on the local machine. If `count`
is left unspecified, it defaults to `1`. `opts` may be a dictionary containing one
of the following keys:
- `isFree`: custom function that is used to check whether the given port is free
- `startPort`: start scanning for free ports starting from this port number.
Defaults to `1025`.
- `endPort`: prevent the scanner from exceeding this port number. Defaults to
`65535`.
- `jobCount`: how much workers that may at most be looking for free ports
### isFreePort(port)
```js
import { isFreePort } from "find-free-ports"
```
Check whether the given port is free by trying to set up a socket.
This function returns a promise containing either `true` or `false` depending
on whether the port was available.
### FindFreePortsOptions
```js
import { FindFreePortsOptions } from "find-free-ports"
```
A TypeScript interface that lists all valid options that may be passed as the
`opts` parameter to `findFreePorts()`.
## Similar Packages

@@ -55,0 +93,0 @@

{
"compilerOptions": {
"module": "ES6",
"module": "ES2020",
"target": "ES2020",
"strict": true,
"moduleResolution": "Node",
"target": "ES2017",
"esModuleInterop": true,
"noEmit": true,
"declaration": true
}
"emitDeclarationOnly": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["index.ts", "tests.ts"],
"exclude": ["node_modules"]
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc