ensure-port
Advanced tools
Comparing version 1.0.0 to 1.1.0
import type { IFileSystem } from '@file-services/types'; | ||
export interface PortsParameters { | ||
/** | ||
* Port to start searching from. | ||
*/ | ||
startPort?: number; | ||
/** | ||
* Port to end searching at. | ||
*/ | ||
endPort?: number; | ||
/** | ||
* Search strategy to look for a new port if one is not available | ||
*/ | ||
strategy?: 'random' | 'sequential'; | ||
} | ||
export interface PortsOptions { | ||
/** | ||
* File system implementation, providing a subset of the original fs methods, a watch service, and path methods. | ||
* @default nodeFs | ||
* @see https://www.npmjs.com/package/@file-services/node | ||
*/ | ||
fs?: IFileSystem; | ||
/** | ||
* Root directory to use for the persistent ports data. | ||
*/ | ||
rootDir?: string; | ||
@@ -20,3 +38,4 @@ } | ||
private watchListener; | ||
constructor({ startPort, endPort }?: PortsParameters, options?: PortsOptions); | ||
private strategy; | ||
constructor({ startPort, endPort, strategy }?: PortsParameters, { fs, rootDir }?: PortsOptions); | ||
/** | ||
@@ -23,0 +42,0 @@ * Returns a reserved port. |
@@ -8,4 +8,3 @@ "use strict"; | ||
class Ports { | ||
constructor({ startPort = 8000, endPort = 9000 } = {}, options = {}) { | ||
var _a, _b; | ||
constructor({ startPort = 8000, endPort = 9000, strategy = 'random' } = {}, { fs = node_1.nodeFs, rootDir = process.cwd() } = {}) { | ||
this.totalPorts = new Set(); | ||
@@ -15,5 +14,6 @@ this.localPorts = new Set(); | ||
this.end = endPort; | ||
this.strategy = strategy; | ||
this.fs = fs; | ||
this.rootDir = rootDir; | ||
this.initialEdges = { start: this.start, end: this.end, reached: false }; | ||
this.fs = (_a = options.fs) !== null && _a !== void 0 ? _a : node_1.nodeFs; | ||
this.rootDir = (_b = options.rootDir) !== null && _b !== void 0 ? _b : process.cwd(); | ||
const tempDir = (0, find_cache_dir_1.findCacheDir)('ensure-port', { fs: this.fs, rootDir: this.rootDir }); | ||
@@ -96,5 +96,18 @@ if (tempDir) { | ||
async getNextPort() { | ||
const tried = new Set(); | ||
let port; | ||
do { | ||
port = randomNumberBetween(this.start, this.end); | ||
if (this.strategy === 'random') { | ||
port = randomNumberBetween(this.start, this.end); | ||
if (tried.size === this.end - this.start + 1) { | ||
throw new Error(`Could not find a free port between ${this.start} and ${this.end}`); | ||
} | ||
tried.add(port); | ||
} | ||
else if (this.strategy === 'sequential') { | ||
port = port ? port + 1 : this.start; | ||
} | ||
else { | ||
throw new Error(`Unknown strategy "${this.strategy}"`); | ||
} | ||
} while (await Promise.resolve(this.totalPorts.has(port))); | ||
@@ -101,0 +114,0 @@ return port; |
import type { IFileSystem } from '@file-services/types'; | ||
export interface PortsParameters { | ||
/** | ||
* Port to start searching from. | ||
*/ | ||
startPort?: number; | ||
/** | ||
* Port to end searching at. | ||
*/ | ||
endPort?: number; | ||
/** | ||
* Search strategy to look for a new port if one is not available | ||
*/ | ||
strategy?: 'random' | 'sequential'; | ||
} | ||
export interface PortsOptions { | ||
/** | ||
* File system implementation, providing a subset of the original fs methods, a watch service, and path methods. | ||
* @default nodeFs | ||
* @see https://www.npmjs.com/package/@file-services/node | ||
*/ | ||
fs?: IFileSystem; | ||
/** | ||
* Root directory to use for the persistent ports data. | ||
*/ | ||
rootDir?: string; | ||
@@ -20,3 +38,4 @@ } | ||
private watchListener; | ||
constructor({ startPort, endPort }?: PortsParameters, options?: PortsOptions); | ||
private strategy; | ||
constructor({ startPort, endPort, strategy }?: PortsParameters, { fs, rootDir }?: PortsOptions); | ||
/** | ||
@@ -23,0 +42,0 @@ * Returns a reserved port. |
@@ -5,4 +5,3 @@ import { nodeFs } from '@file-services/node'; | ||
export class Ports { | ||
constructor({ startPort = 8000, endPort = 9000 } = {}, options = {}) { | ||
var _a, _b; | ||
constructor({ startPort = 8000, endPort = 9000, strategy = 'random' } = {}, { fs = nodeFs, rootDir = process.cwd() } = {}) { | ||
this.totalPorts = new Set(); | ||
@@ -12,5 +11,6 @@ this.localPorts = new Set(); | ||
this.end = endPort; | ||
this.strategy = strategy; | ||
this.fs = fs; | ||
this.rootDir = rootDir; | ||
this.initialEdges = { start: this.start, end: this.end, reached: false }; | ||
this.fs = (_a = options.fs) !== null && _a !== void 0 ? _a : nodeFs; | ||
this.rootDir = (_b = options.rootDir) !== null && _b !== void 0 ? _b : process.cwd(); | ||
const tempDir = findCacheDir('ensure-port', { fs: this.fs, rootDir: this.rootDir }); | ||
@@ -93,5 +93,18 @@ if (tempDir) { | ||
async getNextPort() { | ||
const tried = new Set(); | ||
let port; | ||
do { | ||
port = randomNumberBetween(this.start, this.end); | ||
if (this.strategy === 'random') { | ||
port = randomNumberBetween(this.start, this.end); | ||
if (tried.size === this.end - this.start + 1) { | ||
throw new Error(`Could not find a free port between ${this.start} and ${this.end}`); | ||
} | ||
tried.add(port); | ||
} | ||
else if (this.strategy === 'sequential') { | ||
port = port ? port + 1 : this.start; | ||
} | ||
else { | ||
throw new Error(`Unknown strategy "${this.strategy}"`); | ||
} | ||
} while (await Promise.resolve(this.totalPorts.has(port))); | ||
@@ -98,0 +111,0 @@ return port; |
{ | ||
"name": "ensure-port", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Makes sure you get an available port", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78976
538