@lzwme/sserver 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +6 -1
- package/dist/cli.js +2 -0
- package/dist/config.d.ts +3 -3
- package/dist/config.js +12 -9
- package/dist/index.d.ts +1 -0
- package/dist/lib/get-cert.js +1 -2
- package/dist/lib/mkcert.js +3 -4
- package/dist/lib/utils.d.ts +1 -0
- package/dist/lib/utils.js +22 -0
- package/dist/proxy/express-proxy.js +11 -8
- package/dist/proxy/php-proxy.js +1 -2
- package/dist/proxy/socket-proxy.d.ts +0 -1
- package/dist/proxy/socket-proxy.js +1 -2
- package/dist/static-server.d.ts +1 -1
- package/dist/static-server.js +28 -14
- package/package.json +13 -14
package/README.MD
CHANGED
|
@@ -11,12 +11,17 @@
|
|
|
11
11
|
|
|
12
12
|
一个基于 Node.js 的简易静态服务器,用于辅助开发调试随启随用。
|
|
13
13
|
|
|
14
|
+
```bash
|
|
15
|
+
# 快速启动一个静态服务器
|
|
16
|
+
npx @lzwme/sserver -d ./dist
|
|
17
|
+
```
|
|
18
|
+
|
|
14
19
|
## 安装
|
|
15
20
|
|
|
16
21
|
```bash
|
|
17
22
|
# 全局安装
|
|
18
23
|
npm i -g @lzwme/sserver
|
|
19
|
-
|
|
24
|
+
ss -h
|
|
20
25
|
|
|
21
26
|
# 或者在项目中安装
|
|
22
27
|
npm i -D @lzwme/sserver
|
package/dist/cli.js
CHANGED
|
@@ -23,6 +23,8 @@ commander_1.program
|
|
|
23
23
|
.option('--ssl-cache <dir>', '随机生成 cert 证书缓存的路径。未指定则不缓存')
|
|
24
24
|
.option('-o, --open', '启动后是否打开静态服务器首页')
|
|
25
25
|
.option('--coop-coep', '启用跨域隔离', false)
|
|
26
|
+
.option('--cors', '是否启用 cors。会全局设置 cors 的 header')
|
|
27
|
+
.option('--no-cors', '是否禁用 cors')
|
|
26
28
|
.option('--log [dirpath]', `指定日志路径`)
|
|
27
29
|
.option('--debug', `开启调试模式。`, false)
|
|
28
30
|
.action(async (opts) => {
|
package/dist/config.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
1
|
import { Stats } from 'node:fs';
|
|
4
2
|
import { type ServerOptions } from 'node:https';
|
|
5
3
|
import { type Options } from 'http-proxy-middleware';
|
|
@@ -9,7 +7,7 @@ export interface SSConfig {
|
|
|
9
7
|
/** 是否开启目录列表 */
|
|
10
8
|
autoindex?: boolean;
|
|
11
9
|
/** 自定义 app 应用逻辑 */
|
|
12
|
-
custom?(app: Express, config: SSConfig, logger: NLogger, c: typeof color): void;
|
|
10
|
+
custom?(app: Express, config: SSConfig, logger: NLogger, c: Partial<typeof color>): void;
|
|
13
11
|
/** host 地址。默认为 127.0.0.1 */
|
|
14
12
|
host?: string;
|
|
15
13
|
/** 是否开启日志记录。默认为 false。若设置为 true 则默认为 ~/.sserver/log.log;若为字符串,则设置为日志路径 */
|
|
@@ -41,6 +39,8 @@ export interface SSConfig {
|
|
|
41
39
|
ssl?: ServerOptions;
|
|
42
40
|
/** 自动生成 ssl 的缓存目录 */
|
|
43
41
|
sslCache?: string;
|
|
42
|
+
/** 是否默认开启 cors 设置 */
|
|
43
|
+
cors?: boolean;
|
|
44
44
|
/** 设置公共自定义 headers */
|
|
45
45
|
headers?: Record<string, string | string[] | undefined>;
|
|
46
46
|
/** 按请求自定义 headers。返回结果与 headers 内容合并 */
|
package/dist/config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getConfig =
|
|
3
|
+
exports.getConfig = getConfig;
|
|
4
4
|
const node_path_1 = require("node:path");
|
|
5
5
|
const node_os_1 = require("node:os");
|
|
6
6
|
const node_fs_1 = require("node:fs");
|
|
@@ -16,17 +16,12 @@ const ssConfig = {
|
|
|
16
16
|
},
|
|
17
17
|
proxyConfig: [],
|
|
18
18
|
rootDir: '.',
|
|
19
|
+
cors: true,
|
|
19
20
|
ssl: {
|
|
20
21
|
rejectUnauthorized: false,
|
|
21
22
|
requestCert: true,
|
|
22
23
|
},
|
|
23
|
-
headers: {
|
|
24
|
-
'Access-Control-Allow-Credentials': 'true',
|
|
25
|
-
'Access-Control-Allow-Headers': '*',
|
|
26
|
-
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
|
|
27
|
-
'Access-Control-Allow-Origin': '*',
|
|
28
|
-
'Cross-Origin-Opener-Policy': 'cross-origin',
|
|
29
|
-
},
|
|
24
|
+
headers: {},
|
|
30
25
|
};
|
|
31
26
|
async function getConfig(useCache = true, cfg) {
|
|
32
27
|
if (ssConfig.port && useCache)
|
|
@@ -40,6 +35,15 @@ async function getConfig(useCache = true, cfg) {
|
|
|
40
35
|
if (ssConfig.log) {
|
|
41
36
|
get_logger_1.logger.setLogDir(typeof ssConfig.log === 'boolean' ? (0, node_path_1.resolve)((0, node_os_1.homedir)(), '.sserver/log.log') : ssConfig.log);
|
|
42
37
|
}
|
|
38
|
+
if (ssConfig.cors) {
|
|
39
|
+
Object.assign(ssConfig.headers, {
|
|
40
|
+
'Access-Control-Allow-Credentials': 'true',
|
|
41
|
+
'Access-Control-Allow-Headers': '*',
|
|
42
|
+
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
|
|
43
|
+
'Access-Control-Allow-Origin': '*',
|
|
44
|
+
'Cross-Origin-Opener-Policy': 'cross-origin',
|
|
45
|
+
});
|
|
46
|
+
}
|
|
43
47
|
if (ssConfig.https && ssConfig.ssl) {
|
|
44
48
|
const { ssl } = ssConfig;
|
|
45
49
|
if (ssConfig.https && !ssl.cert) {
|
|
@@ -58,4 +62,3 @@ async function getConfig(useCache = true, cfg) {
|
|
|
58
62
|
ssConfig.port = ssConfig.https ? 443 : 80;
|
|
59
63
|
return ssConfig;
|
|
60
64
|
}
|
|
61
|
-
exports.getConfig = getConfig;
|
package/dist/index.d.ts
CHANGED
package/dist/lib/get-cert.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCert =
|
|
3
|
+
exports.getCert = getCert;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const promises_1 = require("fs/promises");
|
|
6
6
|
const mkcert_1 = require("./mkcert");
|
|
@@ -76,4 +76,3 @@ async function getCert(host = 'localhost', cacheDir = '') {
|
|
|
76
76
|
}
|
|
77
77
|
return result;
|
|
78
78
|
}
|
|
79
|
-
exports.getCert = getCert;
|
package/dist/lib/mkcert.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// @see https://github.com/Subash/mkcert
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
4
|
+
exports.generateCert = generateCert;
|
|
5
|
+
exports.createCA = createCA;
|
|
6
|
+
exports.createCert = createCert;
|
|
5
7
|
const node_forge_1 = require("node-forge");
|
|
6
8
|
const util_1 = require("util");
|
|
7
9
|
const generateKeyPair = (0, util_1.promisify)(node_forge_1.pki.rsa.generateKeyPair.bind(node_forge_1.pki.rsa));
|
|
@@ -28,7 +30,6 @@ async function generateCert(options) {
|
|
|
28
30
|
cert: node_forge_1.pki.certificateToPem(cert),
|
|
29
31
|
};
|
|
30
32
|
}
|
|
31
|
-
exports.generateCert = generateCert;
|
|
32
33
|
function createCA(options) {
|
|
33
34
|
if (!options.extensions)
|
|
34
35
|
options.extensions = [];
|
|
@@ -40,7 +41,6 @@ function createCA(options) {
|
|
|
40
41
|
];
|
|
41
42
|
return generateCert(options);
|
|
42
43
|
}
|
|
43
|
-
exports.createCA = createCA;
|
|
44
44
|
function createCert(options) {
|
|
45
45
|
// certificate Attributes: https://git.io/fptna
|
|
46
46
|
if (!options.subject)
|
|
@@ -75,7 +75,6 @@ function createCert(options) {
|
|
|
75
75
|
signWith: options.caKey,
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
-
exports.createCert = createCert;
|
|
79
78
|
function isIP(ip) {
|
|
80
79
|
if (!ip)
|
|
81
80
|
return false;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getIp(): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getIp = getIp;
|
|
4
|
+
const os_1 = require("os");
|
|
5
|
+
function getIp() {
|
|
6
|
+
const nif = (0, os_1.networkInterfaces)();
|
|
7
|
+
let list = [];
|
|
8
|
+
for (const key of Object.keys(nif)) {
|
|
9
|
+
for (const item of nif[key]) {
|
|
10
|
+
if (!item.internal && item.address && item.family === 'IPv4')
|
|
11
|
+
list.push(item);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
list = list.sort((a, b) => {
|
|
15
|
+
if (b.address.startsWith('172.'))
|
|
16
|
+
return -1;
|
|
17
|
+
return 0;
|
|
18
|
+
});
|
|
19
|
+
// console.log(list);
|
|
20
|
+
return list[0]?.address;
|
|
21
|
+
}
|
|
22
|
+
// console.log(getIp());
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.proxyByExpress =
|
|
3
|
+
exports.proxyByExpress = proxyByExpress;
|
|
4
4
|
const console_log_colors_1 = require("console-log-colors");
|
|
5
5
|
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
6
6
|
const config_1 = require("../config");
|
|
@@ -10,15 +10,18 @@ async function proxyByExpress(app) {
|
|
|
10
10
|
if (!proxyConfig?.length)
|
|
11
11
|
return;
|
|
12
12
|
proxyConfig.forEach(({ api, config }) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
// @ts-ignore 兼容 2.0 api
|
|
14
|
+
const onProxyReq = config.on?.proxyReq || config.onProxyReq;
|
|
15
|
+
config.on = {
|
|
16
|
+
...(config.on || {}),
|
|
17
|
+
proxyReq: (pReq, req, res, options) => {
|
|
18
|
+
const now = new Date();
|
|
19
|
+
get_logger_1.logger.log(now.toTimeString().slice(0, 8) + '.' + String(now.getMilliseconds()).padStart(3, '0'), console_log_colors_1.color.gray('[proxyReq]'), console_log_colors_1.color.cyan(req.url), '=>', console_log_colors_1.color.cyanBright(`${pReq.protocol}${pReq.host}${pReq.path}`));
|
|
20
|
+
if (onProxyReq)
|
|
21
|
+
onProxyReq(pReq, req, res, options);
|
|
22
|
+
},
|
|
19
23
|
};
|
|
20
24
|
app.use(api, (0, http_proxy_middleware_1.createProxyMiddleware)(config));
|
|
21
25
|
get_logger_1.logger.log(`Init proxy for ${console_log_colors_1.color.cyanBright(api)}`);
|
|
22
26
|
});
|
|
23
27
|
}
|
|
24
|
-
exports.proxyByExpress = proxyByExpress;
|
package/dist/proxy/php-proxy.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// @see https://www.npmjs.com/package/phpcgijs
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.phpProxy =
|
|
4
|
+
exports.phpProxy = phpProxy;
|
|
5
5
|
const node_child_process_1 = require("node:child_process");
|
|
6
6
|
const node_url_1 = require("node:url");
|
|
7
7
|
const node_fs_1 = require("node:fs");
|
|
@@ -11,7 +11,6 @@ const get_logger_1 = require("../get-logger");
|
|
|
11
11
|
async function phpProxy(app, config) {
|
|
12
12
|
return app.use('/', createPhpMiddleware(config.rootDir, config.phpConfig));
|
|
13
13
|
}
|
|
14
|
-
exports.phpProxy = phpProxy;
|
|
15
14
|
let PHP_CGI;
|
|
16
15
|
function getPhpCgi() {
|
|
17
16
|
if (PHP_CGI == null) {
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.socketProxy =
|
|
6
|
+
exports.socketProxy = socketProxy;
|
|
7
7
|
const node_net_1 = __importDefault(require("node:net"));
|
|
8
8
|
const console_log_colors_1 = require("console-log-colors");
|
|
9
9
|
function socketProxy(config = {
|
|
@@ -27,4 +27,3 @@ function socketProxy(config = {
|
|
|
27
27
|
.listen(config.localPort);
|
|
28
28
|
return server;
|
|
29
29
|
}
|
|
30
|
-
exports.socketProxy = socketProxy;
|
package/dist/static-server.d.ts
CHANGED
package/dist/static-server.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
/*
|
|
3
3
|
* @Author: lzw
|
|
4
4
|
* @Date: 2021-05-28 16:24:40
|
|
5
|
-
* @LastEditors:
|
|
6
|
-
* @LastEditTime:
|
|
5
|
+
* @LastEditors: renxia
|
|
6
|
+
* @LastEditTime: 2024-06-18 11:25:13
|
|
7
7
|
* @Description:
|
|
8
8
|
*/
|
|
9
9
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
@@ -22,18 +22,28 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
22
22
|
}) : function(o, v) {
|
|
23
23
|
o["default"] = v;
|
|
24
24
|
});
|
|
25
|
-
var __importStar = (this && this.__importStar) || function (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
};
|
|
25
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
26
|
+
var ownKeys = function(o) {
|
|
27
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
28
|
+
var ar = [];
|
|
29
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
30
|
+
return ar;
|
|
31
|
+
};
|
|
32
|
+
return ownKeys(o);
|
|
33
|
+
};
|
|
34
|
+
return function (mod) {
|
|
35
|
+
if (mod && mod.__esModule) return mod;
|
|
36
|
+
var result = {};
|
|
37
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
38
|
+
__setModuleDefault(result, mod);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
})();
|
|
32
42
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
33
43
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
34
44
|
};
|
|
35
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.initServer =
|
|
46
|
+
exports.initServer = initServer;
|
|
37
47
|
const node_https_1 = require("node:https");
|
|
38
48
|
const node_child_process_1 = require("node:child_process");
|
|
39
49
|
const console_log_colors_1 = require("console-log-colors");
|
|
@@ -43,12 +53,16 @@ const config_1 = require("./config");
|
|
|
43
53
|
const get_logger_1 = require("./get-logger");
|
|
44
54
|
const express_proxy_1 = require("./proxy/express-proxy");
|
|
45
55
|
const php_proxy_1 = require("./proxy/php-proxy");
|
|
56
|
+
const utils_js_1 = require("./lib/utils.js");
|
|
46
57
|
async function initServer(c) {
|
|
47
58
|
const app = (0, express_1.default)();
|
|
48
59
|
const config = await (0, config_1.getConfig)(!c, c);
|
|
49
60
|
const { rootDir = '.', port = 8888, host = 'localhost' } = config;
|
|
50
61
|
const url = `http${config.https ? 's' : ''}://${host}:${port}`;
|
|
51
|
-
|
|
62
|
+
const ipUrl = `http://${(0, utils_js_1.getIp)()}:${port}`;
|
|
63
|
+
app.use((req, res, next) => {
|
|
64
|
+
if (config.cors)
|
|
65
|
+
config.headers['Access-Control-Allow-Origin'] = req.header('origin') || '*';
|
|
52
66
|
Object.entries(config.headers).forEach(([key, value]) => res.header(key, value));
|
|
53
67
|
next();
|
|
54
68
|
});
|
|
@@ -78,8 +92,9 @@ async function initServer(c) {
|
|
|
78
92
|
}));
|
|
79
93
|
}
|
|
80
94
|
const onListen = () => {
|
|
81
|
-
get_logger_1.logger.log(console_log_colors_1.color.green(`
|
|
82
|
-
get_logger_1.logger.log(console_log_colors_1.color.green(`
|
|
95
|
+
get_logger_1.logger.log(console_log_colors_1.color.green(`ROOT DIR : `.padStart(15, ' ')), console_log_colors_1.color.greenBright(rootDir));
|
|
96
|
+
get_logger_1.logger.log(console_log_colors_1.color.green(`Local/Host : `.padStart(15, ' ')), console_log_colors_1.color.cyanBright(url));
|
|
97
|
+
get_logger_1.logger.log(console_log_colors_1.color.green(`Network[IP] : `.padStart(15, ' ')), console_log_colors_1.color.cyanBright(ipUrl));
|
|
83
98
|
};
|
|
84
99
|
if (config.https) {
|
|
85
100
|
(0, node_https_1.createServer)(config.ssl, app).listen(port, onListen);
|
|
@@ -94,4 +109,3 @@ async function initServer(c) {
|
|
|
94
109
|
}
|
|
95
110
|
return app;
|
|
96
111
|
}
|
|
97
|
-
exports.initServer = initServer;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lzwme/sserver",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "一个基于 Node.js 的简易静态服务器,用于辅助开发调试随启随用",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": "^14.18.0 || >=16.15.0"
|
|
21
21
|
},
|
|
22
|
-
"packageManager": "pnpm@7.2.0",
|
|
23
22
|
"keywords": [
|
|
24
23
|
"server",
|
|
25
24
|
"web-server",
|
|
@@ -47,22 +46,22 @@
|
|
|
47
46
|
"release": "npm run build"
|
|
48
47
|
},
|
|
49
48
|
"dependencies": {
|
|
50
|
-
"@lzwme/fe-utils": "^1.
|
|
51
|
-
"commander": "^
|
|
52
|
-
"console-log-colors": "^0.
|
|
53
|
-
"express": "^
|
|
54
|
-
"http-proxy-middleware": "^
|
|
49
|
+
"@lzwme/fe-utils": "^1.9.2",
|
|
50
|
+
"commander": "^14.0.2",
|
|
51
|
+
"console-log-colors": "^0.5.0",
|
|
52
|
+
"express": "^5.1.0",
|
|
53
|
+
"http-proxy-middleware": "^3.0.5",
|
|
55
54
|
"node-forge": "^1.3.1",
|
|
56
55
|
"serve-index": "^1.9.1"
|
|
57
56
|
},
|
|
58
57
|
"devDependencies": {
|
|
59
|
-
"@lzwme/fed-lint-helper": "^2.
|
|
60
|
-
"@types/express": "^
|
|
61
|
-
"@types/node": "^
|
|
62
|
-
"@types/node-forge": "^1.3.
|
|
63
|
-
"husky": "^
|
|
58
|
+
"@lzwme/fed-lint-helper": "^2.6.6",
|
|
59
|
+
"@types/express": "^5.0.5",
|
|
60
|
+
"@types/node": "^24.10.0",
|
|
61
|
+
"@types/node-forge": "^1.3.14",
|
|
62
|
+
"husky": "^9.1.7",
|
|
64
63
|
"standard-version": "^9.5.0",
|
|
65
|
-
"ts-node": "^10.9.
|
|
66
|
-
"typescript": "^5.
|
|
64
|
+
"ts-node": "^10.9.2",
|
|
65
|
+
"typescript": "^5.9.3"
|
|
67
66
|
}
|
|
68
67
|
}
|