This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017 Pooya Parsa
Copyright (c) UnJS - Pooya Parsa
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+22 -6
View File
@@ -1,3 +1,5 @@
![get-port-please](https://user-images.githubusercontent.com/904724/101664848-9bc16380-3a4c-11eb-9e3a-faad60c86b2e.png)
# get-port-please
> Get an available TCP port to listen
@@ -16,13 +18,17 @@ or npm install get-port-please
```
```js
const getPort = require('get-port-please')
// or
import getPort from 'get-port-please'
// ESM
import { getPort, checkPort, getRandomPort, waitForPort } from 'get-port-please'
// CommonJS
const { getPort, checkPort, getRandomPort, waitForPort } = require('get-port-please')
```
```ts
function getPort(options?: GetPortOptions): Promise<number>
getPort(options?: GetPortOptions): Promise<number>
checkPort(port: number, host?: string): Promise<number | false>
waitForPort(port: number, options): Promise<number | false>
```
Try sequence is: port > ports > memo > random
@@ -35,7 +41,9 @@ interface GetPortOptions {
random?: boolean
port?: number
portRange?: [from: number, to: number]
ports?: number[]
host?: string
memoDir?: string
memoName?: string
@@ -56,11 +64,19 @@ First port to check. Default is `process.env.PORT || 3000`
### `ports`
Alternative ports to check. Default is `[4000, 5000, 6000, 7000]`
Alternative ports to check.
### `portRange`
Alternative port range to check. Deefault is `[3000,3100]`
### `host`
The host to check. Default is `process.env.HOST` otherwise all available hosts will be checked.
### `memoDir` / `memoName`
Options passed to [fs-memo](https://github.com/nuxt-contrib/fs-memo)
Options passed to [fs-memo](https://github.com/unjs/fs-memo)
- Default dir: `node_modules/get-port/dist`
- Defalt name: `.get-port`
+219
View File
@@ -0,0 +1,219 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const net = require('net');
const os = require('os');
const fsMemo = require('fs-memo');
const unsafePorts = /* @__PURE__ */ new Set([
1,
7,
9,
11,
13,
15,
17,
19,
20,
21,
22,
23,
25,
37,
42,
43,
53,
69,
77,
79,
87,
95,
101,
102,
103,
104,
109,
110,
111,
113,
115,
117,
119,
123,
135,
137,
139,
143,
161,
179,
389,
427,
465,
512,
513,
514,
515,
526,
530,
531,
532,
540,
548,
554,
556,
563,
587,
601,
636,
989,
990,
993,
995,
1719,
1720,
1723,
2049,
3659,
4045,
5060,
5061,
6e3,
6566,
6665,
6666,
6667,
6668,
6669,
6697,
10080
]);
function isUnsafePort(port) {
return unsafePorts.has(port);
}
function isSafePort(port) {
return !isUnsafePort(port);
}
async function getPort(config) {
if (typeof config === "number" || typeof config === "string") {
config = { port: parseInt(config + "") };
}
const options = {
name: "default",
random: false,
port: parseInt(process.env.PORT || "") || 3e3,
ports: [],
portRange: [3e3, 3100],
host: void 0,
memoName: "port",
...config
};
if (options.random) {
return getRandomPort(options.host);
}
const portsToCheck = [
options.port,
...options.ports,
...generateRange(options.portRange[0], options.portRange[1])
].filter((port) => port && isSafePort(port));
const memoOptions = { name: options.memoName, dir: options.memoDir };
const memoKey = "port_" + options.name;
const memo = await fsMemo.getMemo(memoOptions);
if (memo[memoKey]) {
portsToCheck.push(memo[memoKey]);
}
const availablePort = await findPort(portsToCheck, options.host);
await fsMemo.setMemo({ [memoKey]: availablePort }, memoOptions);
return availablePort;
}
async function getRandomPort(host) {
const port = await checkPort(0, host);
if (port === false) {
throw new Error("Unable to obtain an available random port number!");
}
return port;
}
async function waitForPort(port, opts = {}) {
const delay = opts.delay || 500;
const retries = opts.retries || 4;
for (let i = retries; i > 0; i--) {
if (await checkPort(port, opts.host) === false) {
return;
}
await new Promise((resolve) => setTimeout(resolve, delay));
}
throw new Error(`Timeout waiting for port ${port} after ${retries} retries with ${delay}ms interval.`);
}
async function checkPort(port, host = process.env.HOST) {
if (!host) {
host = getLocalHosts([void 0, "0.0.0.0"]);
}
if (!Array.isArray(host)) {
return _checkPort(port, host);
}
for (const _host of host) {
const _port = await _checkPort(port, _host);
if (_port === false) {
return false;
}
if (port === 0 && _port !== 0) {
port = _port;
}
}
return port;
}
function generateRange(from, to) {
if (to < from) {
return [];
}
const r = [];
for (let i = from; i < to; i++) {
r.push(i);
}
return r;
}
function _checkPort(port, host) {
return new Promise((resolve) => {
const server = net.createServer();
server.unref();
server.on("error", (err) => {
if (err.code === "EINVAL" || err.code === "EADDRNOTAVAIL") {
resolve(port !== 0 && isSafePort(port) && port);
} else {
resolve(false);
}
});
server.listen({ port, host }, () => {
const { port: port2 } = server.address();
server.close(() => {
resolve(isSafePort(port2) && port2);
});
});
});
}
function getLocalHosts(additional) {
const hosts = new Set(additional);
for (const _interface of Object.values(os.networkInterfaces())) {
for (const config of _interface) {
hosts.add(config.address);
}
}
return Array.from(hosts);
}
async function findPort(ports, host) {
for (const port of ports) {
const r = await checkPort(port, host);
if (r) {
return r;
}
}
return getRandomPort(host);
}
exports.checkPort = checkPort;
exports.getPort = getPort;
exports.getRandomPort = getRandomPort;
exports.isSafePort = isSafePort;
exports.isUnsafePort = isUnsafePort;
exports.waitForPort = waitForPort;
+27 -10
View File
@@ -1,10 +1,27 @@
interface GetPortOptions {
name?: string;
random?: boolean;
port?: number;
ports?: number[];
memoDir?: string;
memoName?: string;
}
export default function getPort(options?: GetPortOptions): Promise<number>;
export {};
declare function isUnsafePort(port: number): boolean;
declare function isSafePort(port: number): boolean;
interface GetPortOptions {
name: string;
random: boolean;
port: number;
ports: number[];
portRange: [from: number, to: number];
host: string;
memoDir: string;
memoName: string;
}
declare type GetPortInput = Partial<GetPortOptions> | number | string;
declare type HostAddress = undefined | string;
declare type PortNumber = number;
declare function getPort(config?: GetPortInput): Promise<PortNumber>;
declare function getRandomPort(host?: HostAddress): Promise<number>;
interface WaitForPortOptions {
host?: HostAddress;
delay?: number;
retries?: number;
}
declare function waitForPort(port: PortNumber, opts?: WaitForPortOptions): Promise<void>;
declare function checkPort(port: PortNumber, host?: HostAddress | HostAddress[]): Promise<PortNumber | false>;
export { GetPortInput, GetPortOptions, HostAddress, PortNumber, WaitForPortOptions, checkPort, getPort, getRandomPort, isSafePort, isUnsafePort, waitForPort };
+210
View File
@@ -0,0 +1,210 @@
import { createServer } from 'net';
import { networkInterfaces } from 'os';
import { getMemo, setMemo } from 'fs-memo';
const unsafePorts = /* @__PURE__ */ new Set([
1,
7,
9,
11,
13,
15,
17,
19,
20,
21,
22,
23,
25,
37,
42,
43,
53,
69,
77,
79,
87,
95,
101,
102,
103,
104,
109,
110,
111,
113,
115,
117,
119,
123,
135,
137,
139,
143,
161,
179,
389,
427,
465,
512,
513,
514,
515,
526,
530,
531,
532,
540,
548,
554,
556,
563,
587,
601,
636,
989,
990,
993,
995,
1719,
1720,
1723,
2049,
3659,
4045,
5060,
5061,
6e3,
6566,
6665,
6666,
6667,
6668,
6669,
6697,
10080
]);
function isUnsafePort(port) {
return unsafePorts.has(port);
}
function isSafePort(port) {
return !isUnsafePort(port);
}
async function getPort(config) {
if (typeof config === "number" || typeof config === "string") {
config = { port: parseInt(config + "") };
}
const options = {
name: "default",
random: false,
port: parseInt(process.env.PORT || "") || 3e3,
ports: [],
portRange: [3e3, 3100],
host: void 0,
memoName: "port",
...config
};
if (options.random) {
return getRandomPort(options.host);
}
const portsToCheck = [
options.port,
...options.ports,
...generateRange(options.portRange[0], options.portRange[1])
].filter((port) => port && isSafePort(port));
const memoOptions = { name: options.memoName, dir: options.memoDir };
const memoKey = "port_" + options.name;
const memo = await getMemo(memoOptions);
if (memo[memoKey]) {
portsToCheck.push(memo[memoKey]);
}
const availablePort = await findPort(portsToCheck, options.host);
await setMemo({ [memoKey]: availablePort }, memoOptions);
return availablePort;
}
async function getRandomPort(host) {
const port = await checkPort(0, host);
if (port === false) {
throw new Error("Unable to obtain an available random port number!");
}
return port;
}
async function waitForPort(port, opts = {}) {
const delay = opts.delay || 500;
const retries = opts.retries || 4;
for (let i = retries; i > 0; i--) {
if (await checkPort(port, opts.host) === false) {
return;
}
await new Promise((resolve) => setTimeout(resolve, delay));
}
throw new Error(`Timeout waiting for port ${port} after ${retries} retries with ${delay}ms interval.`);
}
async function checkPort(port, host = process.env.HOST) {
if (!host) {
host = getLocalHosts([void 0, "0.0.0.0"]);
}
if (!Array.isArray(host)) {
return _checkPort(port, host);
}
for (const _host of host) {
const _port = await _checkPort(port, _host);
if (_port === false) {
return false;
}
if (port === 0 && _port !== 0) {
port = _port;
}
}
return port;
}
function generateRange(from, to) {
if (to < from) {
return [];
}
const r = [];
for (let i = from; i < to; i++) {
r.push(i);
}
return r;
}
function _checkPort(port, host) {
return new Promise((resolve) => {
const server = createServer();
server.unref();
server.on("error", (err) => {
if (err.code === "EINVAL" || err.code === "EADDRNOTAVAIL") {
resolve(port !== 0 && isSafePort(port) && port);
} else {
resolve(false);
}
});
server.listen({ port, host }, () => {
const { port: port2 } = server.address();
server.close(() => {
resolve(isSafePort(port2) && port2);
});
});
});
}
function getLocalHosts(additional) {
const hosts = new Set(additional);
for (const _interface of Object.values(networkInterfaces())) {
for (const config of _interface) {
hosts.add(config.address);
}
}
return Array.from(hosts);
}
async function findPort(ports, host) {
for (const port of ports) {
const r = await checkPort(port, host);
if (r) {
return r;
}
}
return getRandomPort(host);
}
export { checkPort, getPort, getRandomPort, isSafePort, isUnsafePort, waitForPort };
+32 -23
View File
@@ -1,64 +1,73 @@
{
"_args": [
[
"get-port-please@1.0.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
"get-port-please@2.5.0",
"/home/node/nuxt"
]
],
"_from": "get-port-please@1.0.0",
"_id": "get-port-please@1.0.0",
"_from": "get-port-please@2.5.0",
"_id": "get-port-please@2.5.0",
"_inBundle": false,
"_integrity": "sha512-4hylsMS0Uqa4jjwXVS+QWMntWL7CQouAlv4BEmiycavPqOp3ytGf4V4yqy8FnbBboHq5orCXuZyftuXbANk+Mg==",
"_integrity": "sha512-NblPebBznYARC1R2r1qmusbJAAgBr954gWhEZgwTerzR8r3ud6U5PI1SG4Lue43r87aikPPjObs85VieIDK99A==",
"_location": "/get-port-please",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "get-port-please@1.0.0",
"raw": "get-port-please@2.5.0",
"name": "get-port-please",
"escapedName": "get-port-please",
"rawSpec": "1.0.0",
"rawSpec": "2.5.0",
"saveSpec": null,
"fetchSpec": "1.0.0"
"fetchSpec": "2.5.0"
},
"_requiredBy": [
"/@nuxt/loading-screen"
],
"_resolved": "https://registry.npmjs.org/get-port-please/-/get-port-please-1.0.0.tgz",
"_spec": "1.0.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"_resolved": "https://registry.npmjs.org/get-port-please/-/get-port-please-2.5.0.tgz",
"_spec": "2.5.0",
"_where": "/home/node/nuxt",
"bugs": {
"url": "https://github.com/nuxt-contrib/get-port-please/issues"
"url": "https://github.com/unjs/get-port-please/issues"
},
"dependencies": {
"fs-memo": "^1.0.0"
"fs-memo": "^1.2.0"
},
"description": "Get an available TCP port to listen",
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
"@types/node": "latest",
"bili": "latest",
"c8": "latest",
"eslint": "latest",
"rollup-plugin-typescript2": "latest",
"standard-version": "latest",
"typescript": "latest"
"typescript": "latest",
"unbuild": "latest",
"vitest": "^0.5.5"
},
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"files": [
"dist"
],
"homepage": "https://github.com/nuxt-contrib/get-port-please#readme",
"homepage": "https://github.com/unjs/get-port-please#readme",
"license": "MIT",
"main": "dist/index.js",
"main": "./dist/index.cjs",
"name": "get-port-please",
"packageManager": "pnpm@6.32.3",
"repository": {
"type": "git",
"url": "git+https://github.com/nuxt-contrib/get-port-please.git"
"url": "git+https://github.com/unjs/get-port-please.git"
},
"scripts": {
"build": "bili src/index.ts --minimal",
"build": "unbuild",
"lint": "eslint --ext ts .",
"release": "yarn build && standard-version && npm publish && git push --follow-tags"
"release": "pnpm build && standard-version && pnpm publish && git push --follow-tags",
"test": "pnpm lint && vitest run"
},
"types": "dist/index.d.ts",
"version": "1.0.0"
"types": "./dist/index.d.ts",
"version": "2.5.0"
}