@lzwme/sserver 0.0.5 → 0.0.7

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.
@@ -1,50 +1,97 @@
1
- "use strict";
2
- /*
3
- * @Author: lzw
4
- * @Date: 2021-05-28 16:24:40
5
- * @LastEditors: lzw
6
- * @LastEditTime: 2022-09-23 17:21:36
7
- * @Description:
8
- */
9
- var __importDefault = (this && this.__importDefault) || function (mod) {
10
- return (mod && mod.__esModule) ? mod : { "default": mod };
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.initServer = void 0;
14
- const node_path_1 = require("node:path");
15
- const node_https_1 = require("node:https");
16
- const console_log_colors_1 = require("console-log-colors");
17
- const express_1 = __importDefault(require("express"));
18
- const child_process_1 = require("child_process");
19
- const config_1 = require("./config");
20
- const express_proxy_1 = require("./proxy/express-proxy");
21
- const get_logger_1 = require("./get-logger");
22
- async function initServer(options) {
23
- options = await (0, config_1.getConfig)(!options, options);
24
- const app = (0, express_1.default)();
25
- const { port = 8888, host = 'localhost' } = options;
26
- const url = `http${options.https ? 's' : ''}://${host}:${port}`;
27
- const rootDir = (0, node_path_1.resolve)(process.cwd(), typeof options.rootDir === 'string' ? options.rootDir : '.');
28
- // @see https://www.expressjs.com.cn/4x/api.html#express.static
29
- app.use(express_1.default.static(rootDir, {
30
- // extensions: ['html', 'htm'],
31
- }));
32
- await (0, express_proxy_1.proxyByExpress)(app);
33
- const onListen = () => {
34
- get_logger_1.logger.log(console_log_colors_1.color.green(`START : `.padStart(15, ' ')), console_log_colors_1.color.cyanBright(url));
35
- get_logger_1.logger.log(console_log_colors_1.color.green(`ROOT DIR : `.padStart(15, ' ')), console_log_colors_1.color.cyanBright(rootDir));
36
- };
37
- if (options.https) {
38
- (0, node_https_1.createServer)(options.ssl, app).listen(port, onListen);
39
- }
40
- else {
41
- app.listen(port, onListen);
42
- }
43
- get_logger_1.logger.debug(options);
44
- if (options.open) {
45
- const openPageCmd = `${process.platform === 'win32' ? `start` : `open`} ${url}`;
46
- (0, child_process_1.execSync)(openPageCmd);
47
- }
48
- return app;
49
- }
50
- exports.initServer = initServer;
1
+ "use strict";
2
+ /*
3
+ * @Author: lzw
4
+ * @Date: 2021-05-28 16:24:40
5
+ * @LastEditors: lzw
6
+ * @LastEditTime: 2023-02-03 13:32:07
7
+ * @Description:
8
+ */
9
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ var desc = Object.getOwnPropertyDescriptor(m, k);
12
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13
+ desc = { enumerable: true, get: function() { return m[k]; } };
14
+ }
15
+ Object.defineProperty(o, k2, desc);
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
22
+ }) : function(o, v) {
23
+ o["default"] = v;
24
+ });
25
+ var __importStar = (this && this.__importStar) || function (mod) {
26
+ if (mod && mod.__esModule) return mod;
27
+ var result = {};
28
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29
+ __setModuleDefault(result, mod);
30
+ return result;
31
+ };
32
+ var __importDefault = (this && this.__importDefault) || function (mod) {
33
+ return (mod && mod.__esModule) ? mod : { "default": mod };
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.initServer = void 0;
37
+ const node_https_1 = require("node:https");
38
+ const node_child_process_1 = require("node:child_process");
39
+ const console_log_colors_1 = require("console-log-colors");
40
+ const express_1 = __importDefault(require("express"));
41
+ const fe_utils_1 = require("@lzwme/fe-utils");
42
+ const config_1 = require("./config");
43
+ const get_logger_1 = require("./get-logger");
44
+ const express_proxy_1 = require("./proxy/express-proxy");
45
+ const php_proxy_1 = require("./proxy/php-proxy");
46
+ async function initServer(c) {
47
+ const app = (0, express_1.default)();
48
+ const config = await (0, config_1.getConfig)(!c, c);
49
+ const { rootDir = '.', port = 8888, host = 'localhost' } = config;
50
+ const url = `http${config.https ? 's' : ''}://${host}:${port}`;
51
+ app.use((_req, res, next) => {
52
+ Object.entries(config.headers).forEach(([key, value]) => res.header(key, value));
53
+ next();
54
+ });
55
+ if (typeof config.custom === 'function') {
56
+ await config.custom(app, config, get_logger_1.logger, console_log_colors_1.color);
57
+ }
58
+ await (0, php_proxy_1.phpProxy)(app, config);
59
+ await (0, express_proxy_1.proxyByExpress)(app);
60
+ // @see https://www.expressjs.com.cn/4x/api.html#express.static
61
+ app.use(express_1.default.static(rootDir, {
62
+ extensions: ['html', 'htm'],
63
+ setHeaders: (res, path, stat) => {
64
+ if (typeof config.setHeaders === 'function') {
65
+ const headers = { ...config.headers };
66
+ get_logger_1.logger.debug('[setHeader]', path, headers);
67
+ (0, fe_utils_1.assign)(headers, config.setHeaders(res, path, stat));
68
+ Object.entries(headers).forEach(([key, value]) => res.header(key, value));
69
+ }
70
+ },
71
+ }));
72
+ if (config.autoindex) {
73
+ // @ts-expect-error
74
+ const serveIndex = await Promise.resolve().then(() => __importStar(require('serve-index')));
75
+ app.use(serveIndex.default(rootDir, {
76
+ icons: true,
77
+ hidden: true,
78
+ }));
79
+ }
80
+ const onListen = () => {
81
+ get_logger_1.logger.log(console_log_colors_1.color.green(`Runing at : `.padStart(15, ' ')), console_log_colors_1.color.cyanBright(url));
82
+ get_logger_1.logger.log(console_log_colors_1.color.green(`ROOT DIR : `.padStart(15, ' ')), console_log_colors_1.color.cyanBright(rootDir));
83
+ };
84
+ if (config.https) {
85
+ (0, node_https_1.createServer)(config.ssl, app).listen(port, onListen);
86
+ }
87
+ else {
88
+ app.listen(port, onListen);
89
+ }
90
+ get_logger_1.logger.debug(config);
91
+ if (config.open) {
92
+ const openPageCmd = `${process.platform === 'win32' ? `start` : `open`} ${url}`;
93
+ (0, node_child_process_1.execSync)(openPageCmd);
94
+ }
95
+ return app;
96
+ }
97
+ exports.initServer = initServer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lzwme/sserver",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "一个基于 Node.js 的简易静态服务器,用于辅助开发调试随启随用",
5
5
  "main": "dist/index.js",
6
6
  "type": "commonjs",
@@ -47,21 +47,22 @@
47
47
  "release": "npm run build"
48
48
  },
49
49
  "dependencies": {
50
- "@lzwme/fe-utils": "^1.3.2",
51
- "commander": "^9.4.1",
52
- "console-log-colors": "^0.3.3",
50
+ "@lzwme/fe-utils": "^1.5.1",
51
+ "commander": "^10.0.1",
52
+ "console-log-colors": "^0.4.0",
53
53
  "express": "^4.18.2",
54
54
  "http-proxy-middleware": "^2.0.6",
55
- "node-forge": "^1.3.1"
55
+ "node-forge": "^1.3.1",
56
+ "serve-index": "^1.9.1"
56
57
  },
57
58
  "devDependencies": {
58
- "@lzwme/fed-lint-helper": "^2.1.5",
59
- "@types/express": "^4.17.15",
60
- "@types/node": "^18.11.16",
61
- "@types/node-forge": "^1.3.1",
62
- "husky": "^8.0.2",
59
+ "@lzwme/fed-lint-helper": "^2.3.2",
60
+ "@types/express": "^4.17.17",
61
+ "@types/node": "^20.3.1",
62
+ "@types/node-forge": "^1.3.2",
63
+ "husky": "^8.0.3",
63
64
  "standard-version": "^9.5.0",
64
65
  "ts-node": "^10.9.1",
65
- "typescript": "^4.9.4"
66
+ "typescript": "^5.1.3"
66
67
  }
67
68
  }