detect-port
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -9,3 +9,3 @@ #!/usr/bin/env node | ||
const node_fs_1 = require("node:fs"); | ||
const detect_port_js_1 = __importDefault(require("../detect-port.js")); | ||
const detect_port_js_1 = require("../detect-port.js"); | ||
const pkgFile = node_path_1.default.join(__dirname, '../../../package.json'); | ||
@@ -33,3 +33,3 @@ const pkg = JSON.parse((0, node_fs_1.readFileSync)(pkgFile, 'utf-8')); | ||
const random = Math.floor(9000 + Math.random() * (65535 - 9000)); | ||
(0, detect_port_js_1.default)(random, (err, port) => { | ||
(0, detect_port_js_1.detectPort)(random, (err, port) => { | ||
if (isVerbose) { | ||
@@ -66,3 +66,3 @@ if (err) { | ||
else { | ||
(0, detect_port_js_1.default)(port, (err, _port) => { | ||
(0, detect_port_js_1.detectPort)(port, (err, _port) => { | ||
if (isVerbose) { | ||
@@ -82,2 +82,2 @@ if (err) { | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGV0ZWN0LXBvcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYmluL2RldGVjdC1wb3J0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUVBLDBEQUE2QjtBQUM3QixxQ0FBdUM7QUFDdkMsdUVBQTJDO0FBRTNDLE1BQU0sT0FBTyxHQUFHLG1CQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsRUFBRSx1QkFBdUIsQ0FBQyxDQUFDO0FBQzlELE1BQU0sR0FBRyxHQUFHLElBQUksQ0FBQyxLQUFLLENBQUMsSUFBQSxzQkFBWSxFQUFDLE9BQU8sRUFBRSxPQUFPLENBQUMsQ0FBQyxDQUFDO0FBRXZELE1BQU0sSUFBSSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxDQUFDO0FBQ25DLElBQUksS0FBSy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0FBQztJQUNuQyxPQUFPLENBQUMsR0FBRyxFQUFFLENBQUM7QUFDaEIsQ0FBQztLQUFNLENBQUM7SUFDTixJQUFBLDJCQUFVLEVBQUMsSUFBSSxFQUFFLENBQUMsR0FBRyxFQUFFLEtBQUssRUFBRSxFQUFFO1FBQzlCLElBQUksU0FBUyxFQUFFLENBQUM7WUFDZCxJQUFJLEdBQUcsRUFBRSxDQUFDO2dCQUNSLE9BQU8sQ0FBQyxHQUFHLENBQUMsa0NBQWtDLEdBQUcsRUFBRSxDQUFDLENBQUM7WUFDdkQsQ0FBQztZQUVELElBQUksSUFBSSxLQUFLLEtBQUssRUFBRSxDQUFDO2dCQUNuQixPQUFPLENBQUMsR0FBRyxDQUFDLFFBQVEsSUFBSSxlQUFlLENBQUMsQ0FBQztZQUMzQyxDQUFDO1lBRUQsT0FBTyxDQUFDLEdBQUcsQ0FBQyxzQkFBc0IsS0FBSyxFQUFFLENBQUMsQ0FBQztRQUM3QyxDQUFDO2FBQU0sQ0FBQztZQUNOLE9BQU8sQ0FBQyxHQUFHLENBQUMsS0FBSyxJQUFJLElBQUksQ0FBQyxDQUFDO1FBQzdCLENBQUM7SUFDSCxDQUFDLENBQUMsQ0FBQztBQUNMLENBQUMifQ== |
@@ -1,3 +0,3 @@ | ||
type DetectPortCallback = (err: Error | null, port?: number) => void; | ||
interface PortConfig { | ||
export type DetectPortCallback = (err: Error | null, port?: number) => void; | ||
export interface PortConfig { | ||
port?: number | string; | ||
@@ -7,5 +7,7 @@ hostname?: string | undefined; | ||
} | ||
export default function detectPort(port?: number | PortConfig | string): Promise<number>; | ||
export default function detectPort(callback: DetectPortCallback): void; | ||
export default function detectPort(port: number | PortConfig | string | undefined, callback: DetectPortCallback): void; | ||
export {}; | ||
export declare class IPAddressNotAvailableError extends Error { | ||
constructor(options?: ErrorOptions); | ||
} | ||
export declare function detectPort(port?: number | PortConfig | string): Promise<number>; | ||
export declare function detectPort(callback: DetectPortCallback): void; | ||
export declare function detectPort(port: number | PortConfig | string | undefined, callback: DetectPortCallback): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = detectPort; | ||
exports.IPAddressNotAvailableError = void 0; | ||
exports.detectPort = detectPort; | ||
const node_net_1 = require("node:net"); | ||
@@ -8,2 +9,10 @@ const node_util_1 = require("node:util"); | ||
const debug = (0, node_util_1.debuglog)('detect-port'); | ||
class IPAddressNotAvailableError extends Error { | ||
constructor(options) { | ||
super('The IP address is not available on this machine', options); | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
exports.IPAddressNotAvailableError = IPAddressNotAvailableError; | ||
function detectPort(port, callback) { | ||
@@ -29,86 +38,95 @@ let hostname = ''; | ||
if (typeof callback === 'function') { | ||
return tryListen(port, maxPort, hostname, callback); | ||
return tryListen(port, maxPort, hostname) | ||
.then(port => callback(null, port)) | ||
.catch(callback); | ||
} | ||
// promise | ||
return new Promise(resolve => { | ||
tryListen(port, maxPort, hostname, (_, realPort) => { | ||
resolve(realPort); | ||
}); | ||
}); | ||
return tryListen(port, maxPort, hostname); | ||
} | ||
function tryListen(port, maxPort, hostname, callback) { | ||
function handleError() { | ||
port++; | ||
if (port >= maxPort) { | ||
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort); | ||
port = 0; | ||
maxPort = 0; | ||
} | ||
tryListen(port, maxPort, hostname, callback); | ||
async function handleError(port, maxPort, hostname) { | ||
if (port >= maxPort) { | ||
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort); | ||
port = 0; | ||
maxPort = 0; | ||
} | ||
return await tryListen(port, maxPort, hostname); | ||
} | ||
async function tryListen(port, maxPort, hostname) { | ||
// use user hostname | ||
if (hostname) { | ||
listen(port, hostname, (err, realPort) => { | ||
if (err) { | ||
if (err.code === 'EADDRNOTAVAIL') { | ||
return callback(new Error('The IP address is not available on this machine')); | ||
} | ||
return handleError(); | ||
try { | ||
return await listen(port, hostname); | ||
} | ||
catch (err) { | ||
if (err.code === 'EADDRNOTAVAIL') { | ||
throw new IPAddressNotAvailableError({ cause: err }); | ||
} | ||
callback(null, realPort); | ||
}); | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
else { | ||
// 1. check null | ||
listen(port, void 0, (err, realPort) => { | ||
// ignore random listening | ||
if (port === 0) { | ||
return callback(err, realPort); | ||
} | ||
if (err) { | ||
return handleError(); | ||
} | ||
// 2. check 0.0.0.0 | ||
listen(port, '0.0.0.0', err => { | ||
if (err) { | ||
return handleError(); | ||
} | ||
// 3. check localhost | ||
listen(port, 'localhost', err => { | ||
// if localhost refer to the ip that is not unkonwn on the machine, you will see the error EADDRNOTAVAIL | ||
// https://stackoverflow.com/questions/10809740/listen-eaddrnotavail-error-in-node-js | ||
if (err && err.code !== 'EADDRNOTAVAIL') { | ||
return handleError(); | ||
} | ||
// 4. check current ip | ||
listen(port, (0, address_1.ip)(), (err, realPort) => { | ||
if (err) { | ||
return handleError(); | ||
} | ||
callback(null, realPort); | ||
}); | ||
}); | ||
}); | ||
}); | ||
// 1. check null / undefined | ||
try { | ||
await listen(port); | ||
} | ||
catch (err) { | ||
// ignore random listening | ||
if (port === 0) { | ||
throw err; | ||
} | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 2. check 0.0.0.0 | ||
try { | ||
await listen(port, '0.0.0.0'); | ||
} | ||
catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 3. check 127.0.0.1 | ||
try { | ||
await listen(port, '127.0.0.1'); | ||
} | ||
catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 4. check localhost | ||
try { | ||
await listen(port, 'localhost'); | ||
} | ||
catch (err) { | ||
// if localhost refer to the ip that is not unknown on the machine, you will see the error EADDRNOTAVAIL | ||
// https://stackoverflow.com/questions/10809740/listen-eaddrnotavail-error-in-node-js | ||
if (err.code !== 'EADDRNOTAVAIL') { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
// 5. check current ip | ||
try { | ||
return await listen(port, (0, address_1.ip)()); | ||
} | ||
catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
function listen(port, hostname, callback) { | ||
function listen(port, hostname) { | ||
const server = (0, node_net_1.createServer)(); | ||
server.once('error', err => { | ||
debug('listen %s:%s error: %s', hostname, port, err); | ||
server.close(); | ||
if (err.code === 'ENOTFOUND') { | ||
debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port); | ||
return callback(null, port); | ||
} | ||
return callback(err); | ||
return new Promise((resolve, reject) => { | ||
server.once('error', err => { | ||
debug('listen %s:%s error: %s', hostname, port, err); | ||
server.close(); | ||
if (err.code === 'ENOTFOUND') { | ||
debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port); | ||
return resolve(port); | ||
} | ||
return reject(err); | ||
}); | ||
debug('try listen %d on %s', port, hostname); | ||
server.listen(port, hostname, () => { | ||
port = server.address().port; | ||
debug('get free %s:%s', hostname, port); | ||
server.close(); | ||
return resolve(port); | ||
}); | ||
}); | ||
debug('try listen %d on %s', port, hostname); | ||
server.listen(port, hostname, () => { | ||
port = server.address().port; | ||
debug('get free %s:%s', hostname, port); | ||
server.close(); | ||
return callback(null, port); | ||
}); | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGV0ZWN0LXBvcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGV0ZWN0LXBvcnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFpQkEsNkJBNkJDO0FBOUNELHVDQUFxRDtBQUNyRCx5Q0FBcUM7QUFDckMscUNBQTZCO0FBRTdCLE1BQU0sS0FBSyxHQUFHLElBQUEsb0JBQVEsRUFBQyxhQUFhLENBQUMsQ0FBQztBQWF0QyxTQUF3QixVQUFVLENBQUMsSUFBd0QsRUFBRSxRQUE2QjtJQUN4SCxJQUFJLFFBQVEsR0FBdUIsRUFBR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LENBQUMsQ0FBQztJQUNMLENBQUMsQ0FBQyxDQUFDO0FBQ0wsQ0FBQyJ9 |
@@ -1,4 +0,5 @@ | ||
import detectPort from './detect-port.js'; | ||
import { detectPort } from './detect-port.js'; | ||
export default detectPort; | ||
export { detectPort }; | ||
export * from './detect-port.js'; | ||
export declare const detect: typeof detectPort; | ||
export * from './wait-port.js'; |
@@ -16,11 +16,10 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.detectPort = void 0; | ||
const detect_port_js_1 = __importDefault(require("./detect-port.js")); | ||
exports.detectPort = detect_port_js_1.default; | ||
exports.default = detect_port_js_1.default; | ||
exports.detect = void 0; | ||
const detect_port_js_1 = require("./detect-port.js"); | ||
exports.default = detect_port_js_1.detectPort; | ||
__exportStar(require("./detect-port.js"), exports); | ||
// keep alias detectPort to detect | ||
exports.detect = detect_port_js_1.detectPort; | ||
__exportStar(require("./wait-port.js"), exports); | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSxzRUFBMEM7QUFJakMscUJBSkYsd0JBQVUsQ0FJRTtBQUZuQixrQkFBZSx3QkFBVSxDQUFDO0FBRzFCLGlEQUErQiJ9 | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSxxREFBOEM7QUFFOUMsa0JBQWUsMkJBQVUsQ0FBQztBQUUxQixtREFBaUM7QUFDakMsa0NBQWtDO0FBQ3JCLFFBQUEsTUFBTSxHQUFHLDJCQUFVLENBQUM7QUFFakMsaURBQStCIn0= |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -9,3 +6,3 @@ exports.WaitPortRetryError = void 0; | ||
const node_util_1 = require("node:util"); | ||
const detect_port_js_1 = __importDefault(require("./detect-port.js")); | ||
const detect_port_js_1 = require("./detect-port.js"); | ||
const debug = (0, node_util_1.debuglog)('detect-port:wait-port'); | ||
@@ -39,3 +36,3 @@ function sleep(ms) { | ||
count++; | ||
const freePort = await (0, detect_port_js_1.default)(port); | ||
const freePort = await (0, detect_port_js_1.detectPort)(port); | ||
if (freePort === port) { | ||
@@ -49,2 +46,2 @@ await sleep(retryInterval); | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid2FpdC1wb3J0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3dhaXQtcG9ydC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUE2QkEsNEJBb0JDO0FBakRELHlDQUFxQztBQUNyQyxzRUFBMEM7QUFFMUMsTUFBTSxLQUFLLEdBQUcsSUFBQSxvQkFBUSxFQUFDLHVCQUF1QixDQUFDLENBQUM7QUFFaEQsU0FBUyxLQUFLLENBQUMsRUFBVTtJQUN2QixPQUFPLElBQUksT0FBTyxDQUFDLE9BQU8sQ0FBQyxFQUFFO1FBQzNCLFVBQVUsQ0FBQyxPQUFPLEVBQUUsRUFBRSxDQUFDLENBQUM7SUFDMUIsQ0FBQyxDQUFDLENBQUM7QUFDTCxDQUFDO0FBRUQsTUFBYSxrQkFBbUIsU0F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node:fs'; | ||
import detectPort from '../detect-port.js'; | ||
import { detectPort } from '../detect-port.js'; | ||
const pkgFile = path.join(__dirname, '../../../package.json'); | ||
@@ -74,2 +74,2 @@ const pkg = JSON.parse(readFileSync(pkgFile, 'utf-8')); | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGV0ZWN0LXBvcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYmluL2RldGVjdC1wb3J0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFFQSxPQUFPLElBQUksTUFBTSxXQUFXLENBQUM7QUFDN0IsT0FBTyxFQUFFLFlBQVksRUFBRSxNQUFNLFNBQVMsQ0FBQztBQUN2QyxPQUFPLFVBQVUsTUFBTSxtQkFBbUIsQ0FBQztBQUUzQyxNQUFNLE9BQU8sR0FBRyxJQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsRUFBRSx1QkFBdUIsQ0FBQyxDQUFDO0FBQzlELE1BQU0sR0FBRyxHQUFHLElBQUksQ0FBQyxLQUFLLENBQUMsWUFBWSxDQUFDLE9BQU8sRUFBRSxPQUFPLENBQUMsQ0FBQyxDQUFDO0FBRXZELE1BQU0sSUFBSSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxDQUFDO0FBQ25DLElBQUksS0FBSyxHQUFHLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQztBQUVwQixJQUFJLEtBQUssSUFBSSxDQUFFLElBQUksRUFBRSxXQUFXLENBQUUsQ0FBQyxRQUFRLENBQUMsS0FBSyxDQUFDLFdBQVcsRUFBRSxDQUFDLEVBQUUsQ0FBQztJQUNqRSxPQUFPLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxPQUFPLENBQUMsQ0FBQztJQUN6QixPQUFPLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQ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RyxDQUFDLGtDQUFrQyxHQUFHLEVBQUUsQ0FBQyxDQUFDO1lBQ3ZELENBQUM7WUFFRCxJQUFJLElBQUksS0FBSyxLQUFLLEVBQUUsQ0FBQztnQkFDbkIsT0FBTyxDQUFDLEdBQUcsQ0FBQyxRQUFRLElBQUksZUFBZSxDQUFDLENBQUM7WUFDM0MsQ0FBQztZQUVELE9BQU8sQ0FBQyxHQUFHLENBQUMsc0JBQXNCLEtBQUssRUFBRSxDQUFDLENBQUM7UUFDN0MsQ0FBQzthQUFNLENBQUM7WUFDTixPQUFPLENBQUMsR0FBRyxDQUFDLEtBQUssSUFBSSxJQUFJLENBQUMsQ0FBQztRQUM3QixDQUFDO0lBQ0gsQ0FBQyxDQUFDLENBQUM7QUFDTCxDQUFDIn0= |
@@ -1,3 +0,3 @@ | ||
type DetectPortCallback = (err: Error | null, port?: number) => void; | ||
interface PortConfig { | ||
export type DetectPortCallback = (err: Error | null, port?: number) => void; | ||
export interface PortConfig { | ||
port?: number | string; | ||
@@ -7,5 +7,7 @@ hostname?: string | undefined; | ||
} | ||
export default function detectPort(port?: number | PortConfig | string): Promise<number>; | ||
export default function detectPort(callback: DetectPortCallback): void; | ||
export default function detectPort(port: number | PortConfig | string | undefined, callback: DetectPortCallback): void; | ||
export {}; | ||
export declare class IPAddressNotAvailableError extends Error { | ||
constructor(options?: ErrorOptions); | ||
} | ||
export declare function detectPort(port?: number | PortConfig | string): Promise<number>; | ||
export declare function detectPort(callback: DetectPortCallback): void; | ||
export declare function detectPort(port: number | PortConfig | string | undefined, callback: DetectPortCallback): void; |
@@ -5,3 +5,10 @@ import { createServer } from 'node:net'; | ||
const debug = debuglog('detect-port'); | ||
export default function detectPort(port, callback) { | ||
export class IPAddressNotAvailableError extends Error { | ||
constructor(options) { | ||
super('The IP address is not available on this machine', options); | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
export function detectPort(port, callback) { | ||
let hostname = ''; | ||
@@ -26,86 +33,95 @@ if (port && typeof port === 'object') { | ||
if (typeof callback === 'function') { | ||
return tryListen(port, maxPort, hostname, callback); | ||
return tryListen(port, maxPort, hostname) | ||
.then(port => callback(null, port)) | ||
.catch(callback); | ||
} | ||
// promise | ||
return new Promise(resolve => { | ||
tryListen(port, maxPort, hostname, (_, realPort) => { | ||
resolve(realPort); | ||
}); | ||
}); | ||
return tryListen(port, maxPort, hostname); | ||
} | ||
function tryListen(port, maxPort, hostname, callback) { | ||
function handleError() { | ||
port++; | ||
if (port >= maxPort) { | ||
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort); | ||
port = 0; | ||
maxPort = 0; | ||
} | ||
tryListen(port, maxPort, hostname, callback); | ||
async function handleError(port, maxPort, hostname) { | ||
if (port >= maxPort) { | ||
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort); | ||
port = 0; | ||
maxPort = 0; | ||
} | ||
return await tryListen(port, maxPort, hostname); | ||
} | ||
async function tryListen(port, maxPort, hostname) { | ||
// use user hostname | ||
if (hostname) { | ||
listen(port, hostname, (err, realPort) => { | ||
if (err) { | ||
if (err.code === 'EADDRNOTAVAIL') { | ||
return callback(new Error('The IP address is not available on this machine')); | ||
} | ||
return handleError(); | ||
try { | ||
return await listen(port, hostname); | ||
} | ||
catch (err) { | ||
if (err.code === 'EADDRNOTAVAIL') { | ||
throw new IPAddressNotAvailableError({ cause: err }); | ||
} | ||
callback(null, realPort); | ||
}); | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
else { | ||
// 1. check null | ||
listen(port, void 0, (err, realPort) => { | ||
// ignore random listening | ||
if (port === 0) { | ||
return callback(err, realPort); | ||
} | ||
if (err) { | ||
return handleError(); | ||
} | ||
// 2. check 0.0.0.0 | ||
listen(port, '0.0.0.0', err => { | ||
if (err) { | ||
return handleError(); | ||
} | ||
// 3. check localhost | ||
listen(port, 'localhost', err => { | ||
// if localhost refer to the ip that is not unkonwn on the machine, you will see the error EADDRNOTAVAIL | ||
// https://stackoverflow.com/questions/10809740/listen-eaddrnotavail-error-in-node-js | ||
if (err && err.code !== 'EADDRNOTAVAIL') { | ||
return handleError(); | ||
} | ||
// 4. check current ip | ||
listen(port, ip(), (err, realPort) => { | ||
if (err) { | ||
return handleError(); | ||
} | ||
callback(null, realPort); | ||
}); | ||
}); | ||
}); | ||
}); | ||
// 1. check null / undefined | ||
try { | ||
await listen(port); | ||
} | ||
catch (err) { | ||
// ignore random listening | ||
if (port === 0) { | ||
throw err; | ||
} | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 2. check 0.0.0.0 | ||
try { | ||
await listen(port, '0.0.0.0'); | ||
} | ||
catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 3. check 127.0.0.1 | ||
try { | ||
await listen(port, '127.0.0.1'); | ||
} | ||
catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 4. check localhost | ||
try { | ||
await listen(port, 'localhost'); | ||
} | ||
catch (err) { | ||
// if localhost refer to the ip that is not unknown on the machine, you will see the error EADDRNOTAVAIL | ||
// https://stackoverflow.com/questions/10809740/listen-eaddrnotavail-error-in-node-js | ||
if (err.code !== 'EADDRNOTAVAIL') { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
// 5. check current ip | ||
try { | ||
return await listen(port, ip()); | ||
} | ||
catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
function listen(port, hostname, callback) { | ||
function listen(port, hostname) { | ||
const server = createServer(); | ||
server.once('error', err => { | ||
debug('listen %s:%s error: %s', hostname, port, err); | ||
server.close(); | ||
if (err.code === 'ENOTFOUND') { | ||
debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port); | ||
return callback(null, port); | ||
} | ||
return callback(err); | ||
return new Promise((resolve, reject) => { | ||
server.once('error', err => { | ||
debug('listen %s:%s error: %s', hostname, port, err); | ||
server.close(); | ||
if (err.code === 'ENOTFOUND') { | ||
debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port); | ||
return resolve(port); | ||
} | ||
return reject(err); | ||
}); | ||
debug('try listen %d on %s', port, hostname); | ||
server.listen(port, hostname, () => { | ||
port = server.address().port; | ||
debug('get free %s:%s', hostname, port); | ||
server.close(); | ||
return resolve(port); | ||
}); | ||
}); | ||
debug('try listen %d on %s', port, hostname); | ||
server.listen(port, hostname, () => { | ||
port = server.address().port; | ||
debug('get free %s:%s', hostname, port); | ||
server.close(); | ||
return callback(null, port); | ||
}); | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGV0ZWN0LXBvcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGV0ZWN0LXBvcnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFlBQVksRUFBZSxNQUFNLFVBQVUsQ0FBQztBQUNyRCxPQUFPLEVBQUUsUUFBUSxFQUFFLE1BQU0sV0FBVyxDQUFDO0FBQ3JDLE9BQU8sRUFBRSxFQUFFLEVBQUUsTUFBTSxTQUFTLENBQUM7QUFFN0IsTUFBTSxLQUFLLEdBQUcsUUFBUSxDQUFDLGFBQWEsQ0FBQyxDQUFDO0FBYXRDLE1BQU0sQ0FBQyxPQUFPLFVBQVUsVUFBVSxDQUFDLElBQXdELEVBQUUsUUFBNkI7SUFDeEgsSUFBSSxRQUFRLEdBQXVCLEVBQUUsQ0FBQztJQUV0QyxJQUFJLElBQUksSUFBSSxPQUFPLElBQUksS0FBSyxRQUFRLEVBQUUsQ0FBQztRQUNyQyxRQUFRLEdBQUcsSUFBSSxDQUFDLFFBQVEsQ0FBQztRQUN6QixRQUFRLEdBQUcsSUFBSSxDQUFDLFFBQVEsQ0FBQztRQUN6QixJQUFJLEdBQUcsSUFBSSxDQUFDLElBQUksQ0FBQztJQUNuQixDQUFDO1NBQU0sQ0FBQztRQUNOLElBQUksT0FBTyxJQUFJLEtBQUssVUFBVSxF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QUN6QixLQUFLLENBQUMsd0JBQXdCLEVBQUUsUUFBUSxFQUFFLElBQUksRUFBRSxHQUFHLENBQUMsQ0FBQztZQUNyRCxNQUFNLENBQUMsS0FBSyxFQUFFLENBQUM7WUFFZixJQUFLLEdBQVcsQ0FBQyxJQUFJLEtBQUssV0FBVyxFQUFFLENBQUM7Z0JBQ3RDLEtBQUssQ0FBQyw0Q0FBNEMsRUFBRSxRQUFRLEVBQUUsSUFBSSxDQUFDLENBQUM7Z0JBQ3BFLE9BQU8sT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFDO1lBQ3ZCLENBQUM7WUFFRCxPQUFPLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQztRQUNyQixDQUFDLENBQUMsQ0FBQztRQUVILEtBQUssQ0FBQyxxQkFBcUIsRUFBRSxJQUFJLEVBQUUsUUFBUSxDQUFDLENBQUM7UUFDN0MsTUFBTSxDQUFDLE1BQU0sQ0FBQyxJQUFJLEVBQUUsUUFBUSxFQUFFLEdBQUcsRUFBRTtZQUNqQyxJQUFJLEdBQUksTUFBTSxDQUFDLE9BQU8sRUFBa0IsQ0FBQyxJQUFJLENBQUM7WUFDOUMsS0FBSyxDQUFDLGdCQUFnQixFQUFFLFFBQVEsRUFBRSxJQUFJLENBQUMsQ0FBQztZQUN4QyxNQUFNLENBQUMsS0FBSyxFQUFFLENBQUM7WUFDZixPQUFPLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUN2QixDQUFDLENBQUMsQ0FBQztJQUNMLENBQUMsQ0FBQyxDQUFDO0FBQ0wsQ0FBQyJ9 |
@@ -1,4 +0,5 @@ | ||
import detectPort from './detect-port.js'; | ||
import { detectPort } from './detect-port.js'; | ||
export default detectPort; | ||
export { detectPort }; | ||
export * from './detect-port.js'; | ||
export declare const detect: typeof detectPort; | ||
export * from './wait-port.js'; |
@@ -1,5 +0,7 @@ | ||
import detectPort from './detect-port.js'; | ||
import { detectPort } from './detect-port.js'; | ||
export default detectPort; | ||
export { detectPort }; | ||
export * from './detect-port.js'; | ||
// keep alias detectPort to detect | ||
export const detect = detectPort; | ||
export * from './wait-port.js'; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxVQUFVLE1BQU0sa0JBQWtCLENBQUM7QUFFMUMsZUFBZSxVQUFVLENBQUM7QUFFMUIsT0FBTyxFQUFFLFVBQVUsRUFBRSxDQUFDO0FBQ3RCLGNBQWMsZ0JBQWdCLENBQUMifQ== | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLGtCQUFrQixDQUFDO0FBRTlDLGVBQWUsVUFBVSxDQUFDO0FBRTFCLGNBQWMsa0JBQWtCLENBQUM7QUFDakMsa0NBQWtDO0FBQ2xDLE1BQU0sQ0FBQyxNQUFNLE1BQU0sR0FBRyxVQUFVLENBQUM7QUFFakMsY0FBYyxnQkFBZ0IsQ0FBQyJ9 |
import { debuglog } from 'node:util'; | ||
import detectPort from './detect-port.js'; | ||
import { detectPort } from './detect-port.js'; | ||
const debug = debuglog('detect-port:wait-port'); | ||
@@ -39,2 +39,2 @@ function sleep(ms) { | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid2FpdC1wb3J0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3dhaXQtcG9ydC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjo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LLENBQUMsQ0FBQztZQUN2RSxNQUFNLEdBQUcsQ0FBQztRQUNaLENBQUM7UUFDRCxLQUFLLEVBQUUsQ0FBQztRQUNSLE1BQU0sUUFBUSxHQUFHLE1BQU0sVUFBVSxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3hDLElBQUksUUFBUSxLQUFLLElBQUksRUFBRSxDQUFDO1lBQ3RCLE1BQU0sS0FBSyxDQUFDLGFBQWEsQ0FBQyxDQUFDO1lBQzNCLE9BQU8sSUFBSSxFQUFFLENBQUM7UUFDaEIsQ0FBQztRQUNELE9BQU8sSUFBSSxDQUFDO0lBQ2QsQ0FBQztJQUVELE9BQU8sTUFBTSxJQUFJLEVBQUUsQ0FBQztBQUN0QixDQUFDIn0= |
{ | ||
"name": "detect-port", | ||
"version": "2.0.1" | ||
"version": "2.1.0" | ||
} |
{ | ||
"name": "detect-port", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Node.js implementation of port detector", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -45,14 +45,13 @@ # detect-port | ||
CommonJS | ||
```javascript | ||
const detect = require('detect-port'); | ||
/** | ||
* use as a promise | ||
*/ | ||
const { detect } = require('detect-port'); | ||
detect(port) | ||
.then(_port => { | ||
if (port == _port) { | ||
.then(realPort => { | ||
if (port == realPort) { | ||
console.log(`port: ${port} was not occupied`); | ||
} else { | ||
console.log(`port: ${port} was occupied, try port: ${_port}`); | ||
console.log(`port: ${port} was occupied, try port: ${realPort}`); | ||
} | ||
@@ -63,3 +62,20 @@ }) | ||
}); | ||
``` | ||
ESM and TypeScript | ||
```ts | ||
import { detect } from 'detect-port'; | ||
detect(port) | ||
.then(realPort => { | ||
if (port == realPort) { | ||
console.log(`port: ${port} was not occupied`); | ||
} else { | ||
console.log(`port: ${port} was occupied, try port: ${realPort}`); | ||
} | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
``` | ||
@@ -66,0 +82,0 @@ |
@@ -5,3 +5,3 @@ #!/usr/bin/env node | ||
import { readFileSync } from 'node:fs'; | ||
import detectPort from '../detect-port.js'; | ||
import { detectPort } from '../detect-port.js'; | ||
@@ -8,0 +8,0 @@ const pkgFile = path.join(__dirname, '../../../package.json'); |
@@ -7,5 +7,5 @@ import { createServer, AddressInfo } from 'node:net'; | ||
type DetectPortCallback = (err: Error | null, port?: number) => void; | ||
export type DetectPortCallback = (err: Error | null, port?: number) => void; | ||
interface PortConfig { | ||
export interface PortConfig { | ||
port?: number | string; | ||
@@ -16,6 +16,14 @@ hostname?: string | undefined; | ||
export default function detectPort(port?: number | PortConfig | string): Promise<number>; | ||
export default function detectPort(callback: DetectPortCallback): void; | ||
export default function detectPort(port: number | PortConfig | string | undefined, callback: DetectPortCallback): void; | ||
export default function detectPort(port?: number | string | PortConfig | DetectPortCallback, callback?: DetectPortCallback) { | ||
export class IPAddressNotAvailableError extends Error { | ||
constructor(options?: ErrorOptions) { | ||
super('The IP address is not available on this machine', options); | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
export function detectPort(port?: number | PortConfig | string): Promise<number>; | ||
export function detectPort(callback: DetectPortCallback): void; | ||
export function detectPort(port: number | PortConfig | string | undefined, callback: DetectPortCallback): void; | ||
export function detectPort(port?: number | string | PortConfig | DetectPortCallback, callback?: DetectPortCallback) { | ||
let hostname: string | undefined = ''; | ||
@@ -41,97 +49,100 @@ | ||
if (typeof callback === 'function') { | ||
return tryListen(port, maxPort, hostname, callback); | ||
return tryListen(port, maxPort, hostname) | ||
.then(port => callback(null, port)) | ||
.catch(callback); | ||
} | ||
// promise | ||
return new Promise(resolve => { | ||
tryListen(port as number, maxPort, hostname, (_, realPort) => { | ||
resolve(realPort); | ||
}); | ||
}); | ||
return tryListen(port as number, maxPort, hostname); | ||
} | ||
function tryListen(port: number, maxPort: number, hostname: string | undefined, callback: DetectPortCallback) { | ||
function handleError() { | ||
port++; | ||
if (port >= maxPort) { | ||
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort); | ||
port = 0; | ||
maxPort = 0; | ||
} | ||
tryListen(port, maxPort, hostname, callback); | ||
async function handleError(port: number, maxPort: number, hostname?: string) { | ||
if (port >= maxPort) { | ||
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort); | ||
port = 0; | ||
maxPort = 0; | ||
} | ||
return await tryListen(port, maxPort, hostname); | ||
} | ||
async function tryListen(port: number, maxPort: number, hostname?: string): Promise<number> { | ||
// use user hostname | ||
if (hostname) { | ||
listen(port, hostname, (err, realPort) => { | ||
if (err) { | ||
if ((err as any).code === 'EADDRNOTAVAIL') { | ||
return callback(new Error('The IP address is not available on this machine')); | ||
} | ||
return handleError(); | ||
try { | ||
return await listen(port, hostname); | ||
} catch (err: any) { | ||
if (err.code === 'EADDRNOTAVAIL') { | ||
throw new IPAddressNotAvailableError({ cause: err }); | ||
} | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
callback(null, realPort); | ||
}); | ||
} else { | ||
// 1. check null | ||
listen(port, void 0, (err, realPort) => { | ||
// ignore random listening | ||
if (port === 0) { | ||
return callback(err, realPort); | ||
} | ||
// 1. check null / undefined | ||
try { | ||
await listen(port); | ||
} catch (err) { | ||
// ignore random listening | ||
if (port === 0) { | ||
throw err; | ||
} | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
if (err) { | ||
return handleError(); | ||
} | ||
// 2. check 0.0.0.0 | ||
try { | ||
await listen(port, '0.0.0.0'); | ||
} catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 2. check 0.0.0.0 | ||
listen(port, '0.0.0.0', err => { | ||
if (err) { | ||
return handleError(); | ||
} | ||
// 3. check 127.0.0.1 | ||
try { | ||
await listen(port, '127.0.0.1'); | ||
} catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
// 3. check localhost | ||
listen(port, 'localhost', err => { | ||
// if localhost refer to the ip that is not unkonwn on the machine, you will see the error EADDRNOTAVAIL | ||
// https://stackoverflow.com/questions/10809740/listen-eaddrnotavail-error-in-node-js | ||
if (err && (err as any).code !== 'EADDRNOTAVAIL') { | ||
return handleError(); | ||
} | ||
// 4. check localhost | ||
try { | ||
await listen(port, 'localhost'); | ||
} catch (err: any) { | ||
// if localhost refer to the ip that is not unknown on the machine, you will see the error EADDRNOTAVAIL | ||
// https://stackoverflow.com/questions/10809740/listen-eaddrnotavail-error-in-node-js | ||
if (err.code !== 'EADDRNOTAVAIL') { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
// 4. check current ip | ||
listen(port, ip(), (err, realPort) => { | ||
if (err) { | ||
return handleError(); | ||
} | ||
callback(null, realPort); | ||
}); | ||
}); | ||
}); | ||
}); | ||
// 5. check current ip | ||
try { | ||
return await listen(port, ip()); | ||
} catch (err) { | ||
return await handleError(++port, maxPort, hostname); | ||
} | ||
} | ||
function listen(port: number, hostname: string | undefined, callback: DetectPortCallback) { | ||
function listen(port: number, hostname?: string) { | ||
const server = createServer(); | ||
server.once('error', err => { | ||
debug('listen %s:%s error: %s', hostname, port, err); | ||
server.close(); | ||
return new Promise<number>((resolve, reject) => { | ||
server.once('error', err => { | ||
debug('listen %s:%s error: %s', hostname, port, err); | ||
server.close(); | ||
if ((err as any).code === 'ENOTFOUND') { | ||
debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port); | ||
return callback(null, port); | ||
} | ||
if ((err as any).code === 'ENOTFOUND') { | ||
debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port); | ||
return resolve(port); | ||
} | ||
return callback(err); | ||
}); | ||
return reject(err); | ||
}); | ||
debug('try listen %d on %s', port, hostname); | ||
server.listen(port, hostname, () => { | ||
port = (server.address() as AddressInfo).port; | ||
debug('get free %s:%s', hostname, port); | ||
server.close(); | ||
return callback(null, port); | ||
debug('try listen %d on %s', port, hostname); | ||
server.listen(port, hostname, () => { | ||
port = (server.address() as AddressInfo).port; | ||
debug('get free %s:%s', hostname, port); | ||
server.close(); | ||
return resolve(port); | ||
}); | ||
}); | ||
} |
@@ -1,6 +0,9 @@ | ||
import detectPort from './detect-port.js'; | ||
import { detectPort } from './detect-port.js'; | ||
export default detectPort; | ||
export { detectPort }; | ||
export * from './detect-port.js'; | ||
// keep alias detectPort to detect | ||
export const detect = detectPort; | ||
export * from './wait-port.js'; |
import { debuglog } from 'node:util'; | ||
import detectPort from './detect-port.js'; | ||
import { detectPort } from './detect-port.js'; | ||
@@ -4,0 +4,0 @@ const debug = debuglog('detect-port:wait-port'); |
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
55342
815
122