This commit is contained in:
2022-07-21 03:28:35 +00:00
parent d7c883d6df
commit 51b34b0e1d
30103 changed files with 4152204 additions and 23 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+86
View File
@@ -0,0 +1,86 @@
![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
[![npm](https://img.shields.io/npm/dt/get-port-please.svg?style=flat-square)](https://npmjs.com/package/get-port-please)
[![npm (scoped with tag)](https://img.shields.io/npm/v/get-port-please/latest.svg?style=flat-square)](https://npmjs.com/package/get-port-please)
## Usage
Install package:
```bash
yarn add get-port-please
# or
or npm install get-port-please
```
```js
// ESM
import { getPort, checkPort, getRandomPort, waitForPort } from 'get-port-please'
// CommonJS
const { getPort, checkPort, getRandomPort, waitForPort } = require('get-port-please')
```
```ts
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
## Options
```ts
interface GetPortOptions {
name?: string
random?: boolean
port?: number
portRange?: [from: number, to: number]
ports?: number[]
host?: string
memoDir?: string
memoName?: string
}
```
### `name`
Unique name for port memorizing. Default is `default`.
### `random`
If enabled, `port` and `ports` will be ignored. Default is `false`.
### `port`
First port to check. Default is `process.env.PORT || 3000`
### `ports`
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/unjs/fs-memo)
- Default dir: `node_modules/get-port/dist`
- Defalt name: `.get-port`
## License
MIT
+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
View File
@@ -0,0 +1,27 @@
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 };
+73
View File
@@ -0,0 +1,73 @@
{
"_args": [
[
"get-port-please@2.5.0",
"/home/node/nuxt"
]
],
"_from": "get-port-please@2.5.0",
"_id": "get-port-please@2.5.0",
"_inBundle": false,
"_integrity": "sha512-NblPebBznYARC1R2r1qmusbJAAgBr954gWhEZgwTerzR8r3ud6U5PI1SG4Lue43r87aikPPjObs85VieIDK99A==",
"_location": "/get-port-please",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "get-port-please@2.5.0",
"name": "get-port-please",
"escapedName": "get-port-please",
"rawSpec": "2.5.0",
"saveSpec": null,
"fetchSpec": "2.5.0"
},
"_requiredBy": [
"/@nuxt/loading-screen"
],
"_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/unjs/get-port-please/issues"
},
"dependencies": {
"fs-memo": "^1.2.0"
},
"description": "Get an available TCP port to listen",
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
"@types/node": "latest",
"c8": "latest",
"eslint": "latest",
"standard-version": "latest",
"typescript": "latest",
"unbuild": "latest",
"vitest": "^0.5.5"
},
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"files": [
"dist"
],
"homepage": "https://github.com/unjs/get-port-please#readme",
"license": "MIT",
"main": "./dist/index.cjs",
"name": "get-port-please",
"packageManager": "pnpm@6.32.3",
"repository": {
"type": "git",
"url": "git+https://github.com/unjs/get-port-please.git"
},
"scripts": {
"build": "unbuild",
"lint": "eslint --ext ts .",
"release": "pnpm build && standard-version && pnpm publish && git push --follow-tags",
"test": "pnpm lint && vitest run"
},
"types": "./dist/index.d.ts",
"version": "2.5.0"
}