@lzwme/sserver 0.0.8 → 0.0.10

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,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.proxyByExpress = void 0;
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
- const onProxyReq = config.onProxyReq;
14
- config.onProxyReq = (pReq, req, res, options) => {
15
- const now = new Date();
16
- 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.originalUrl), '=>', console_log_colors_1.color.cyanBright(`${pReq.protocol}${pReq.host}${pReq.path}`));
17
- if (onProxyReq)
18
- onProxyReq(pReq, req, res, options);
13
+ // @ts-expect-error 兼容 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;
@@ -1,3 +1,3 @@
1
1
  import type { Express } from 'express';
2
- import { type SSConfig } from '../config';
2
+ import type { SSConfig } from '../config';
3
3
  export declare function phpProxy(app: Express, config: SSConfig): Promise<Express>;
@@ -1,17 +1,16 @@
1
1
  "use strict";
2
2
  // @see https://www.npmjs.com/package/phpcgijs
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.phpProxy = void 0;
4
+ exports.phpProxy = phpProxy;
5
5
  const node_child_process_1 = require("node:child_process");
6
- const node_url_1 = require("node:url");
7
6
  const node_fs_1 = require("node:fs");
8
7
  const node_path_1 = require("node:path");
8
+ const node_url_1 = require("node:url");
9
9
  const fe_utils_1 = require("@lzwme/fe-utils");
10
10
  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) {
@@ -24,7 +23,7 @@ async function findPhpFile(pathname, rootDir) {
24
23
  let filepath = (0, node_path_1.join)(rootDir, pathname);
25
24
  if (!filepath.includes(rootDir) || !(0, node_fs_1.existsSync)(filepath))
26
25
  return false;
27
- let stat = await node_fs_1.promises.stat(filepath);
26
+ const stat = await node_fs_1.promises.stat(filepath);
28
27
  if (stat.isDirectory()) {
29
28
  filepath = (0, node_path_1.join)(filepath, 'index.php');
30
29
  if (!(0, node_fs_1.existsSync)(filepath))
@@ -37,7 +36,7 @@ function formatArgs(args) {
37
36
  }
38
37
  function runPhpCgi(req, response, url, filepath, phpConfig, rootDir) {
39
38
  const pathname = url.pathname || req.url;
40
- const pathinfo = pathname.includes('.php') ? pathname.split('.php')[0] + '.php' : pathname;
39
+ const pathinfo = pathname.includes('.php') ? `${pathname.split('.php')[0]}.php` : pathname;
41
40
  if (!phpConfig)
42
41
  return;
43
42
  const env = {
@@ -74,11 +73,11 @@ function runPhpCgi(req, response, url, filepath, phpConfig, rootDir) {
74
73
  // All HTTP headers sent by the client. Headers are separated by carriage return characters (ASCII 13 - \n) and each header name is prefixed by HTTP_, transformed to upper cases, and - characters it contains are replaced by _ characters.
75
74
  ALL_HTTP: Object.entries(req.headers)
76
75
  .map(([key, value]) => `HTTP__${key.toUpperCase().replace('-', '_')}: ${value}`)
77
- .reduce((a, b) => a + b + '\n', ''),
76
+ .reduce((a, b) => `${a + b}\n`, ''),
78
77
  // All HTTP headers as sent by the client in raw form. No transformation on the header names is applied.
79
78
  ALL_RAW: Object.entries(req.headers)
80
- .map(([key, value]) => `${key}: value`)
81
- .reduce((a, b) => a + b + '\n', ''),
79
+ .map(([key, value]) => `${key}: ${value}`)
80
+ .reduce((a, b) => `${a + b}\n`, ''),
82
81
  // The web server's software identity.
83
82
  SERVER_SOFTWARE: 'NodeJS SServer',
84
83
  // The host name or the IP address of the computer running the web server as given in the requested URL.
@@ -107,13 +106,13 @@ function runPhpCgi(req, response, url, filepath, phpConfig, rootDir) {
107
106
  IS_SUBREQ: '',
108
107
  REDIRECT_STATUS: '1',
109
108
  };
110
- Object.entries(req.headers).forEach(function ([key, value]) {
111
- env['HTTP_' + key.toUpperCase().replace('-', '_')] = value;
109
+ Object.entries(req.headers).forEach(([key, value]) => {
110
+ env[`HTTP_${key.toUpperCase().replace('-', '_')}`] = value;
112
111
  });
113
112
  if (filepath.endsWith('.php')) {
114
113
  let childProc;
115
114
  const cgiArgs = formatArgs(phpConfig.args);
116
- let cgipath = phpConfig.cgiPath || getPhpCgi();
115
+ const cgipath = phpConfig.cgiPath || getPhpCgi();
117
116
  let res = '';
118
117
  let err = '';
119
118
  if (!cgipath || !(0, node_fs_1.existsSync)(cgipath))
@@ -148,10 +147,10 @@ function runPhpCgi(req, response, url, filepath, phpConfig, rootDir) {
148
147
  const m = lines[line].split(': ');
149
148
  if (m[0] === '')
150
149
  break;
151
- if (m[0] == 'Status') {
152
- response.statusCode = parseInt(m[1]);
150
+ if (m[0] === 'Status') {
151
+ response.statusCode = parseInt(m[1], 10);
153
152
  }
154
- if (m.length == 2) {
153
+ if (m.length === 2) {
155
154
  response.setHeader(m[0], m[1]);
156
155
  }
157
156
  line++;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import net from 'node:net';
3
2
  export declare function socketProxy(config?: {
4
3
  localPort: number;
@@ -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 = void 0;
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 = {
@@ -15,9 +15,9 @@ function socketProxy(config = {
15
15
  remoteHost: '127.0.0.1',
16
16
  }) {
17
17
  const server = node_net_1.default
18
- .createServer(socket => {
18
+ .createServer((socket) => {
19
19
  console_log_colors_1.log.green('proxy for address', socket.address());
20
- let to = node_net_1.default.createConnection({
20
+ const to = node_net_1.default.createConnection({
21
21
  host: config.remoteHost,
22
22
  port: config.remotePort,
23
23
  });
@@ -27,4 +27,3 @@ function socketProxy(config = {
27
27
  .listen(config.localPort);
28
28
  return server;
29
29
  }
30
- exports.socketProxy = socketProxy;
@@ -1,3 +1,3 @@
1
1
  import { type Express } from 'express';
2
- import { SSConfig } from './config';
2
+ import { type SSConfig } from './config';
3
3
  export declare function initServer(c?: SSConfig): Promise<Express>;
@@ -2,8 +2,8 @@
2
2
  /*
3
3
  * @Author: lzw
4
4
  * @Date: 2021-05-28 16:24:40
5
- * @LastEditors: lzw
6
- * @LastEditTime: 2023-02-03 13:32:07
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,35 +22,48 @@ 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 (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
- };
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 = void 0;
37
- const node_https_1 = require("node:https");
46
+ exports.initServer = initServer;
38
47
  const node_child_process_1 = require("node:child_process");
48
+ const node_https_1 = require("node:https");
49
+ const fe_utils_1 = require("@lzwme/fe-utils");
39
50
  const console_log_colors_1 = require("console-log-colors");
40
51
  const express_1 = __importDefault(require("express"));
41
- const fe_utils_1 = require("@lzwme/fe-utils");
42
52
  const config_1 = require("./config");
43
53
  const get_logger_1 = require("./get-logger");
54
+ const utils_js_1 = require("./lib/utils.js");
55
+ const localsend_1 = require("./localsend");
44
56
  const express_proxy_1 = require("./proxy/express-proxy");
45
57
  const php_proxy_1 = require("./proxy/php-proxy");
46
- const utils_js_1 = require("./lib/utils.js");
47
58
  async function initServer(c) {
48
59
  const app = (0, express_1.default)();
49
60
  const config = await (0, config_1.getConfig)(!c, c);
50
61
  const { rootDir = '.', port = 8888, host = 'localhost' } = config;
51
62
  const url = `http${config.https ? 's' : ''}://${host}:${port}`;
52
63
  const ipUrl = `http://${(0, utils_js_1.getIp)()}:${port}`;
53
- app.use((_req, res, next) => {
64
+ app.use((req, res, next) => {
65
+ if (config.cors)
66
+ config.headers['Access-Control-Allow-Origin'] = req.header('origin') || '*';
54
67
  Object.entries(config.headers).forEach(([key, value]) => res.header(key, value));
55
68
  next();
56
69
  });
@@ -59,6 +72,10 @@ async function initServer(c) {
59
72
  }
60
73
  await (0, php_proxy_1.phpProxy)(app, config);
61
74
  await (0, express_proxy_1.proxyByExpress)(app);
75
+ // localsend 局域网文件传输
76
+ if (config.localsend) {
77
+ await (0, localsend_1.initLocalsend)(app);
78
+ }
62
79
  // @see https://www.expressjs.com.cn/4x/api.html#express.static
63
80
  app.use(express_1.default.static(rootDir, {
64
81
  extensions: ['html', 'htm'],
@@ -97,4 +114,3 @@ async function initServer(c) {
97
114
  }
98
115
  return app;
99
116
  }
100
- exports.initServer = initServer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lzwme/sserver",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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@8.2.0",
23
22
  "keywords": [
24
23
  "server",
25
24
  "web-server",
@@ -35,34 +34,38 @@
35
34
  "ss.config.sample.js"
36
35
  ],
37
36
  "scripts": {
38
- "prepare": "husky install",
39
37
  "ss": "node bin/sserver.js",
40
38
  "start": "npm run stop && pm2 start dist/index.js --name sserver",
41
39
  "stop": "pm2 stop sserver",
42
40
  "watch": "tsc -w",
41
+ "lint": "biome check",
42
+ "fix": "biome check --fix",
43
43
  "dev": "ts-node -T src/cli.ts",
44
- "build": "npm run clean && tsc",
44
+ "build": "npm run clean && tsc && node scripts/copy-assets.js",
45
45
  "clean": "flh rm -f dist",
46
46
  "version": "standard-version",
47
47
  "release": "npm run build"
48
48
  },
49
49
  "dependencies": {
50
- "@lzwme/fe-utils": "^1.5.8",
51
- "commander": "^11.1.0",
52
- "console-log-colors": "^0.4.0",
53
- "express": "^4.18.2",
54
- "http-proxy-middleware": "^2.0.6",
55
- "node-forge": "^1.3.1",
56
- "serve-index": "^1.9.1"
50
+ "@lzwme/fe-utils": "^1.9.2",
51
+ "commander": "^15.0.0",
52
+ "console-log-colors": "^0.5.0",
53
+ "express": "^5.2.1",
54
+ "http-proxy-middleware": "^4.2.0",
55
+ "multer": "^2.2.0",
56
+ "node-forge": "^1.4.0",
57
+ "serve-index": "^1.9.2"
57
58
  },
58
59
  "devDependencies": {
59
- "@lzwme/fed-lint-helper": "^2.5.1",
60
- "@types/express": "^4.17.21",
61
- "@types/node": "^20.10.5",
62
- "@types/node-forge": "^1.3.10",
63
- "husky": "^8.0.3",
60
+ "@biomejs/biome": "^2.5.6",
61
+ "@lzwme/fed-lint-helper": "^2.6.6",
62
+ "@types/express": "^5.0.6",
63
+ "@types/multer": "^2.2.0",
64
+ "@types/node": "^26.1.2",
65
+ "@types/node-forge": "^1.3.14",
66
+ "husky": "^9.1.7",
64
67
  "standard-version": "^9.5.0",
65
68
  "ts-node": "^10.9.2",
66
- "typescript": "^5.3.3"
69
+ "typescript": "^7.0.2"
67
70
  }
68
71
  }
@@ -4,6 +4,14 @@ module.exports = {
4
4
  https: false,
5
5
  sslCache: '',
6
6
  rootDir: '.',
7
+ // localsend 局域网文件传输。可设为 true 开启,或传入对象配置
8
+ // localsend: {
9
+ // enabled: true,
10
+ // // 文件存储目录。默认 ~/.static-server/localsend,可用环境变量 SS_LOCALSEND_DIR 覆盖
11
+ // dir: '~/localsend',
12
+ // // 单个文件上传大小限制(字节),默认 1GB
13
+ // maxSize: 1024 * 1024 * 1024,
14
+ // },
7
15
  proxyConfig: [
8
16
  // 内网代理企业微信机器人接口示例
9
17
  {