@maiyunnet/kebab 3.1.7 → 3.1.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/index.d.ts +1 -1
- package/index.js +2 -2
- package/lib/core.d.ts +1 -1
- package/lib/core.js +5 -3
- package/lib/lang.js +8 -8
- package/lib/net.d.ts +3 -3
- package/lib/net.js +11 -7
- package/lib/text.d.ts +7 -3
- package/lib/text.js +22 -4
- package/package.json +1 -1
- package/sys/ctr.d.ts +2 -0
- package/sys/ctr.js +4 -2
- package/sys/mod.d.ts +2 -2
- package/sys/mod.js +2 -2
- package/sys/route.d.ts +8 -4
- package/sys/route.js +43 -36
- package/www/example/ctr/middle.d.ts +1 -0
- package/www/example/ctr/middle.js +9 -0
- package/www/example/ctr/test.js +2 -2
- package/tsconfig.json +0 -26
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
* ------------------------
|
|
10
10
|
*/
|
|
11
11
|
/** --- 当前系统版本号 --- */
|
|
12
|
-
export const VER = '3.1.
|
|
12
|
+
export const VER = '3.1.9';
|
|
13
13
|
// --- 服务端用的路径 ---
|
|
14
|
-
const imu = import.meta.url.replace('file://', '').replace(/^\/(\w:)/, '$1')
|
|
14
|
+
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
15
15
|
/** --- /xxx/xxx --- */
|
|
16
16
|
const dirname = imu.slice(0, imu.lastIndexOf('/'));
|
|
17
17
|
/** --- 框架根目录,以 / 结尾 --- */
|
package/lib/core.d.ts
CHANGED
package/lib/core.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import * as cp from 'child_process';
|
|
7
7
|
import * as stream from 'stream';
|
|
8
|
+
import * as os from 'os';
|
|
8
9
|
import * as kebab from '#kebab/index.js';
|
|
9
10
|
import * as lTime from '#kebab/lib/time.js';
|
|
10
11
|
import * as lFs from '#kebab/lib/fs.js';
|
|
@@ -304,7 +305,6 @@ export function realIP(ctr, name = '') {
|
|
|
304
305
|
const req = ctr.getPrototype('_req');
|
|
305
306
|
return req.socket.remoteAddress ?? '';
|
|
306
307
|
}
|
|
307
|
-
// --- 以下 Mutton 没有 ---
|
|
308
308
|
/**
|
|
309
309
|
* --- 间隔一段时间 ---
|
|
310
310
|
* @param ms 间隔毫秒
|
|
@@ -625,7 +625,7 @@ export function log(opt, msg, fend = '') {
|
|
|
625
625
|
}
|
|
626
626
|
path += h + '.csv';
|
|
627
627
|
if (!await lFs.isFile(path)) {
|
|
628
|
-
if (!await lFs.putContent(path, 'TIME,UNIX,URL,COOKIE,SESSION,JWT,USER_AGENT,REALIP,CLIENTIP,MESSAGE\n', {
|
|
628
|
+
if (!await lFs.putContent(path, 'TIME,UNIX,URL,COOKIE,SESSION,JWT,USER_AGENT,REALIP,CLIENTIP,OS,PROCESS,MESSAGE\n', {
|
|
629
629
|
'encoding': 'utf8',
|
|
630
630
|
'mode': 0o777
|
|
631
631
|
})) {
|
|
@@ -642,6 +642,8 @@ export function log(opt, msg, fend = '') {
|
|
|
642
642
|
(headers['user-agent']?.replace(/"/g, '""') ?? 'No HTTP_USER_AGENT') + '","' +
|
|
643
643
|
realIp.replace(/"/g, '""') + '","' +
|
|
644
644
|
clientIp.replace(/"/g, '""') + '","' +
|
|
645
|
+
lText.sizeFormat(os.totalmem() - os.freemem(), '') + '","' +
|
|
646
|
+
lText.sizeFormat(process.memoryUsage().rss, '') + '","' +
|
|
645
647
|
JSON.stringify(msg).slice(1, -1).replace(/"/g, '""') + '"\n', {
|
|
646
648
|
'encoding': 'utf8',
|
|
647
649
|
'mode': 0o777,
|
|
@@ -711,7 +713,7 @@ export async function ls(opt) {
|
|
|
711
713
|
return j.data;
|
|
712
714
|
}
|
|
713
715
|
/**
|
|
714
|
-
* ---
|
|
716
|
+
* --- 完整的克隆一份数组/对象 ---
|
|
715
717
|
* @param obj 要克隆的对象
|
|
716
718
|
*/
|
|
717
719
|
export function clone(obj) {
|
package/lib/lang.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/** --- 支持的语言缩写列表 --- */
|
|
2
2
|
export const codes = [
|
|
3
|
-
'sc', 'tc', 'ja', 'ko', '
|
|
4
|
-
'
|
|
3
|
+
'sc', 'tc', 'ja', 'ko', 'th', 'vi', 'ar', 'id',
|
|
4
|
+
'en', 'es', 'de', 'fr', 'pt', 'ru', 'it', 'tr',
|
|
5
5
|
];
|
|
6
6
|
export const names = [
|
|
7
|
-
'简体中文', '繁體中文', '日本語', '한국어', '
|
|
8
|
-
'
|
|
7
|
+
'简体中文', '繁體中文', '日本語', '한국어', 'ไทย', 'Tiếng việt', 'العربية', 'Bahasa Indonesia',
|
|
8
|
+
'English', 'Español', 'Deutsch', 'Français', 'Português', 'Русский', 'Italiano', 'Türkçe',
|
|
9
9
|
];
|
|
10
10
|
/** --- 浏览器常用映射为本语言 --- */
|
|
11
11
|
export const map = {
|
|
@@ -13,16 +13,16 @@ export const map = {
|
|
|
13
13
|
'zh': 'tc',
|
|
14
14
|
'ja': 'ja',
|
|
15
15
|
'ko': 'ko',
|
|
16
|
-
'en': 'en',
|
|
17
|
-
'es': 'es',
|
|
18
16
|
'th': 'th',
|
|
19
17
|
'vi': 'vi',
|
|
18
|
+
'ar': 'ar',
|
|
19
|
+
'id': 'id',
|
|
20
|
+
'en': 'en',
|
|
21
|
+
'es': 'es',
|
|
20
22
|
'de': 'de',
|
|
21
23
|
'fr': 'fr',
|
|
22
24
|
'pt': 'pt',
|
|
23
25
|
'ru': 'ru',
|
|
24
|
-
'ar': 'ar',
|
|
25
|
-
'id': 'id',
|
|
26
26
|
'it': 'it',
|
|
27
27
|
'tr': 'tr',
|
|
28
28
|
};
|
package/lib/net.d.ts
CHANGED
|
@@ -70,7 +70,7 @@ export declare function buildCookieQuery(cookie: Record<string, ICookie>, uri: k
|
|
|
70
70
|
*/
|
|
71
71
|
export declare function resetCookieSession(cookie: Record<string, ICookie>): void;
|
|
72
72
|
/**
|
|
73
|
-
* --- 创建 FormData
|
|
73
|
+
* --- 创建 FormData 对象 ---
|
|
74
74
|
*/
|
|
75
75
|
export declare function getFormData(): fd.FormData;
|
|
76
76
|
/**
|
|
@@ -80,7 +80,7 @@ export declare function getFormData(): fd.FormData;
|
|
|
80
80
|
*/
|
|
81
81
|
export declare function filterProxyHeaders(headers: http.IncomingHttpHeaders | http2.IncomingHttpHeaders | THttpHeaders, res?: http2.Http2ServerResponse | http.ServerResponse): Record<string, string | string[]>;
|
|
82
82
|
/**
|
|
83
|
-
* --- 正向 mproxy
|
|
83
|
+
* --- 正向 mproxy 代理,注意提前处理不要自动处理 post 数据,读取 get 的 url 为实际请求地址 ---
|
|
84
84
|
* --- get: url, auth ---
|
|
85
85
|
* @param ctr 当前控制器
|
|
86
86
|
* @param auth 校验字符串,读取 get 的 auth 和本参数做比对
|
|
@@ -93,7 +93,7 @@ export declare function mproxy(ctr: sCtr.Ctr, auth: string, opt?: IMproxyOptions
|
|
|
93
93
|
*/
|
|
94
94
|
export declare function mproxyData(ctr: sCtr.Ctr): any;
|
|
95
95
|
/**
|
|
96
|
-
* ---
|
|
96
|
+
* --- 反向代理,注意提前处理不要自动处理 post 数据,将本服务器的某个路由反代到其他网址 ---
|
|
97
97
|
* @param ctr 当前控制器
|
|
98
98
|
* @param route 要反代的路由
|
|
99
99
|
* @param opt 参数
|
package/lib/net.js
CHANGED
|
@@ -423,7 +423,7 @@ export function resetCookieSession(cookie) {
|
|
|
423
423
|
}
|
|
424
424
|
}
|
|
425
425
|
/**
|
|
426
|
-
* --- 创建 FormData
|
|
426
|
+
* --- 创建 FormData 对象 ---
|
|
427
427
|
*/
|
|
428
428
|
export function getFormData() {
|
|
429
429
|
return new fd.FormData();
|
|
@@ -457,7 +457,7 @@ export function filterProxyHeaders(headers, res) {
|
|
|
457
457
|
return heads;
|
|
458
458
|
}
|
|
459
459
|
/**
|
|
460
|
-
* --- 正向 mproxy
|
|
460
|
+
* --- 正向 mproxy 代理,注意提前处理不要自动处理 post 数据,读取 get 的 url 为实际请求地址 ---
|
|
461
461
|
* --- get: url, auth ---
|
|
462
462
|
* @param ctr 当前控制器
|
|
463
463
|
* @param auth 校验字符串,读取 get 的 auth 和本参数做比对
|
|
@@ -466,9 +466,11 @@ export function filterProxyHeaders(headers, res) {
|
|
|
466
466
|
export async function mproxy(ctr, auth, opt = {}) {
|
|
467
467
|
const req = ctr.getPrototype('_req');
|
|
468
468
|
const res = ctr.getPrototype('_res');
|
|
469
|
-
const input = ctr.getPrototype('_input');
|
|
470
469
|
/** --- 客户端请求中的 get 的数据 --- */
|
|
471
470
|
const get = ctr.getPrototype('_get');
|
|
471
|
+
if (req.readableEnded) {
|
|
472
|
+
return -3;
|
|
473
|
+
}
|
|
472
474
|
if (get['auth'] !== auth) {
|
|
473
475
|
return 0;
|
|
474
476
|
}
|
|
@@ -479,7 +481,7 @@ export async function mproxy(ctr, auth, opt = {}) {
|
|
|
479
481
|
opt.headers ??= {};
|
|
480
482
|
Object.assign(opt.headers, filterProxyHeaders(req.headers));
|
|
481
483
|
// --- 发起请求 ---
|
|
482
|
-
const rres = await request(get['url'], req
|
|
484
|
+
const rres = await request(get['url'], req, opt);
|
|
483
485
|
if (rres.error) {
|
|
484
486
|
return -2;
|
|
485
487
|
}
|
|
@@ -510,7 +512,7 @@ export function mproxyData(ctr) {
|
|
|
510
512
|
return data;
|
|
511
513
|
}
|
|
512
514
|
/**
|
|
513
|
-
* ---
|
|
515
|
+
* --- 反向代理,注意提前处理不要自动处理 post 数据,将本服务器的某个路由反代到其他网址 ---
|
|
514
516
|
* @param ctr 当前控制器
|
|
515
517
|
* @param route 要反代的路由
|
|
516
518
|
* @param opt 参数
|
|
@@ -519,8 +521,10 @@ export async function rproxy(ctr, route, opt = {}) {
|
|
|
519
521
|
const req = ctr.getPrototype('_req');
|
|
520
522
|
const res = ctr.getPrototype('_res');
|
|
521
523
|
const config = ctr.getPrototype('_config');
|
|
522
|
-
const input = ctr.getPrototype('_input');
|
|
523
524
|
const path = config.const.path + (config.const.qs ? '?' + config.const.qs : '');
|
|
525
|
+
if (req.readableEnded) {
|
|
526
|
+
return false;
|
|
527
|
+
}
|
|
524
528
|
for (const key in route) {
|
|
525
529
|
if (!path.startsWith(key)) {
|
|
526
530
|
continue;
|
|
@@ -534,7 +538,7 @@ export async function rproxy(ctr, route, opt = {}) {
|
|
|
534
538
|
opt.headers ??= {};
|
|
535
539
|
Object.assign(opt.headers, filterProxyHeaders(req.headers));
|
|
536
540
|
// --- 发起请求 ---
|
|
537
|
-
const rres = await request(route[key] + lpath, req
|
|
541
|
+
const rres = await request(route[key] + lpath, req, opt);
|
|
538
542
|
if (rres.error) {
|
|
539
543
|
return false;
|
|
540
544
|
}
|
package/lib/text.d.ts
CHANGED
|
@@ -118,17 +118,17 @@ export declare function isRealPath(path: string): boolean;
|
|
|
118
118
|
*/
|
|
119
119
|
export declare function getFilename(path: string): string;
|
|
120
120
|
/**
|
|
121
|
-
* --- 将普通的返回 JSON
|
|
121
|
+
* --- 将普通的返回 JSON 对象序列化为字符串 ---
|
|
122
122
|
* @param o 返回 JSON 对象
|
|
123
123
|
*/
|
|
124
124
|
export declare function stringifyResult(rtn: kebab.Json): string;
|
|
125
125
|
/**
|
|
126
|
-
* --- 将字符串解析为对象,返回 false 代表解析失败,支持 BigInt
|
|
126
|
+
* --- 将字符串解析为对象,返回 false 代表解析失败,支持 BigInt ---
|
|
127
127
|
* @param str 要解析的 json 字符串
|
|
128
128
|
*/
|
|
129
129
|
export declare function parseJson(str: string): any;
|
|
130
130
|
/**
|
|
131
|
-
* --- 将对象转换为 json 字符串,返回 false 代表解析失败,支持 BigInt
|
|
131
|
+
* --- 将对象转换为 json 字符串,返回 false 代表解析失败,支持 BigInt ---
|
|
132
132
|
* @param obj 要转换的 json 对象
|
|
133
133
|
* @param space 美化方式
|
|
134
134
|
*/
|
|
@@ -138,6 +138,10 @@ export declare function stringifyJson(obj: kebab.Json, space?: string | number):
|
|
|
138
138
|
* @param buf 原始 buffer
|
|
139
139
|
*/
|
|
140
140
|
export declare function stringifyBuffer(buf: Buffer): string;
|
|
141
|
+
/**
|
|
142
|
+
* --- 递归删除 json 中的字符串首尾空格,会返回一个新的对象 ---
|
|
143
|
+
*/
|
|
144
|
+
export declare function trimJson(json: kebab.Json): kebab.Json;
|
|
141
145
|
type TFalsy = false | '' | 0 | null | undefined | typeof NaN;
|
|
142
146
|
/**
|
|
143
147
|
* --- 判断一个值是否是虚假的(为 null/undefined/空字符串/false/0) ---
|
package/lib/text.js
CHANGED
|
@@ -339,7 +339,6 @@ export function isIdCardCN(idcard) {
|
|
|
339
339
|
return false;
|
|
340
340
|
}
|
|
341
341
|
}
|
|
342
|
-
// --- 以下 Mutton: false, Kebab: true ---
|
|
343
342
|
/**
|
|
344
343
|
* --- 将对象转换为 query string ---
|
|
345
344
|
* @param query 要转换的对象
|
|
@@ -430,7 +429,7 @@ export function getFilename(path) {
|
|
|
430
429
|
return path.slice(lio + 1);
|
|
431
430
|
}
|
|
432
431
|
/**
|
|
433
|
-
* --- 将普通的返回 JSON
|
|
432
|
+
* --- 将普通的返回 JSON 对象序列化为字符串 ---
|
|
434
433
|
* @param o 返回 JSON 对象
|
|
435
434
|
*/
|
|
436
435
|
export function stringifyResult(rtn) {
|
|
@@ -469,7 +468,7 @@ export function stringifyResult(rtn) {
|
|
|
469
468
|
return JSON.stringify(rtn);
|
|
470
469
|
}
|
|
471
470
|
/**
|
|
472
|
-
* --- 将字符串解析为对象,返回 false 代表解析失败,支持 BigInt
|
|
471
|
+
* --- 将字符串解析为对象,返回 false 代表解析失败,支持 BigInt ---
|
|
473
472
|
* @param str 要解析的 json 字符串
|
|
474
473
|
*/
|
|
475
474
|
export function parseJson(str) {
|
|
@@ -497,7 +496,7 @@ export function parseJson(str) {
|
|
|
497
496
|
}
|
|
498
497
|
}
|
|
499
498
|
/**
|
|
500
|
-
* --- 将对象转换为 json 字符串,返回 false 代表解析失败,支持 BigInt
|
|
499
|
+
* --- 将对象转换为 json 字符串,返回 false 代表解析失败,支持 BigInt ---
|
|
501
500
|
* @param obj 要转换的 json 对象
|
|
502
501
|
* @param space 美化方式
|
|
503
502
|
*/
|
|
@@ -517,6 +516,25 @@ export function stringifyBuffer(buf) {
|
|
|
517
516
|
const arr = buf.toString('hex').match(/.{1,2}/g);
|
|
518
517
|
return `<Buffer ${arr ? arr.join(' ') : ''}>`;
|
|
519
518
|
}
|
|
519
|
+
/**
|
|
520
|
+
* --- 递归删除 json 中的字符串首尾空格,会返回一个新的对象 ---
|
|
521
|
+
*/
|
|
522
|
+
export function trimJson(json) {
|
|
523
|
+
json = Object.assign({}, json);
|
|
524
|
+
trimJsonRecursion(json);
|
|
525
|
+
return json;
|
|
526
|
+
}
|
|
527
|
+
function trimJsonRecursion(json) {
|
|
528
|
+
for (const key in json) {
|
|
529
|
+
const val = json[key];
|
|
530
|
+
if (typeof val === 'string') {
|
|
531
|
+
json[key] = val.trim();
|
|
532
|
+
}
|
|
533
|
+
else if (typeof val === 'object') {
|
|
534
|
+
trimJsonRecursion(val);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
520
538
|
/**
|
|
521
539
|
* --- 判断一个值是否是虚假的(为 null/undefined/空字符串/false/0) ---
|
|
522
540
|
* @param val 要判断的值
|
package/package.json
CHANGED
package/sys/ctr.d.ts
CHANGED
|
@@ -124,6 +124,8 @@ export declare class Ctr {
|
|
|
124
124
|
* --- WebSocket 下连接被终止后会自动被调用的事件,可重写此方法 ---
|
|
125
125
|
*/
|
|
126
126
|
onClose(): void | Promise<void>;
|
|
127
|
+
/** --- 请求发送开始时调用,返回 false 则框架不会自动获取任何 post 内容(仅会在 middle 内触发) --- */
|
|
128
|
+
onReqStart(): boolean | Promise<boolean>;
|
|
127
129
|
/**
|
|
128
130
|
* --- 获取截止当前时间的总运行时间 ---
|
|
129
131
|
* @param ms 为 true 为毫秒,否则为秒
|
package/sys/ctr.js
CHANGED
|
@@ -47,7 +47,6 @@ export class Ctr {
|
|
|
47
47
|
this._xsrf = '';
|
|
48
48
|
/** --- 自定义 http code --- */
|
|
49
49
|
this._httpCode = 0;
|
|
50
|
-
// --- Kebab: true,Mutton: false,全局常量等对象 ---
|
|
51
50
|
/** --- 当前语言名 --- */
|
|
52
51
|
this._locale = 'en';
|
|
53
52
|
/** --- 本 ctr 已加载的语言文件列表 --- */
|
|
@@ -164,6 +163,10 @@ export class Ctr {
|
|
|
164
163
|
onClose() {
|
|
165
164
|
return;
|
|
166
165
|
}
|
|
166
|
+
/** --- 请求发送开始时调用,返回 false 则框架不会自动获取任何 post 内容(仅会在 middle 内触发) --- */
|
|
167
|
+
onReqStart() {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
167
170
|
/**
|
|
168
171
|
* --- 获取截止当前时间的总运行时间 ---
|
|
169
172
|
* @param ms 为 true 为毫秒,否则为秒
|
|
@@ -658,7 +661,6 @@ export class Ctr {
|
|
|
658
661
|
}
|
|
659
662
|
return true;
|
|
660
663
|
}
|
|
661
|
-
// --- 以下:Mutton: false, Kebab: true ---
|
|
662
664
|
/**
|
|
663
665
|
* --- 获取语言包值 ---
|
|
664
666
|
* @param key
|
package/sys/mod.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Project:
|
|
2
|
+
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2019-6-4 21:35
|
|
4
4
|
* Last: 2020-4-14 13:33:51, 2022-07-23 16:01:34, 2022-09-06 22:59:26, 2023-5-24 19:11:37, 2023-6-13 21:47:58, 2023-7-10 18:54:03, 2023-8-23 17:03:16, 2023-12-11 15:21:22, 2023-12-20 23:12:03, 2024-3-8 16:05:29, 2024-3-20 19:58:15, 2024-8-11 21:14:54, 2024-10-5 14:00:22, 2024-12-14 19:58:34, 2025-9-23 11:01:36
|
|
5
5
|
*/
|
|
@@ -58,7 +58,7 @@ export default class Mod {
|
|
|
58
58
|
protected _db: lDb.Pool | lDb.Transaction;
|
|
59
59
|
/** --- Sql 对象 --- */
|
|
60
60
|
protected _sql: lSql.Sql;
|
|
61
|
-
/** --- ctr
|
|
61
|
+
/** --- ctr 对象 --- */
|
|
62
62
|
protected _ctr?: sCtr.Ctr;
|
|
63
63
|
/**
|
|
64
64
|
* --- 构造函数 ---
|
package/sys/mod.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Project:
|
|
2
|
+
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2019-6-4 21:35
|
|
4
4
|
* Last: 2020-4-14 13:33:51, 2022-07-23 16:01:34, 2022-09-06 22:59:26, 2023-5-24 19:11:37, 2023-6-13 21:47:58, 2023-7-10 18:54:03, 2023-8-23 17:03:16, 2023-12-11 15:21:22, 2023-12-20 23:12:03, 2024-3-8 16:05:29, 2024-3-20 19:58:15, 2024-8-11 21:14:54, 2024-10-5 14:00:22, 2024-12-14 19:58:34, 2025-9-23 11:01:36
|
|
5
5
|
*/
|
|
@@ -89,7 +89,7 @@ class Mod {
|
|
|
89
89
|
this._contain = null;
|
|
90
90
|
/** --- 已算出的 total --- */
|
|
91
91
|
this._total = [];
|
|
92
|
-
/** --- ctr
|
|
92
|
+
/** --- ctr 对象 --- */
|
|
93
93
|
this._ctr = undefined;
|
|
94
94
|
/** --- 设置的 limit --- */
|
|
95
95
|
this._limit = [0, 0];
|
package/sys/route.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ export declare function run(data: {
|
|
|
35
35
|
};
|
|
36
36
|
}): Promise<boolean>;
|
|
37
37
|
/**
|
|
38
|
-
* ---
|
|
38
|
+
* --- 删除本次请求所有已上传的临时文件 ---
|
|
39
39
|
* @param cctr Ctr 对象 或 files
|
|
40
40
|
*/
|
|
41
41
|
export declare function unlinkUploadFiles(cctr: sCtr.Ctr | Record<string, kebab.IPostFile | kebab.IPostFile[]>): Promise<void>;
|
|
@@ -46,10 +46,14 @@ export declare function unlinkUploadFiles(cctr: sCtr.Ctr | Record<string, kebab.
|
|
|
46
46
|
*/
|
|
47
47
|
export declare function waitCtr(cctr: sCtr.Ctr): Promise<void>;
|
|
48
48
|
/**
|
|
49
|
-
* ---
|
|
50
|
-
* @param
|
|
49
|
+
* --- 获取 post 对象(通常已自动获取),如果是文件上传(formdata)的情况则不获取 ---
|
|
50
|
+
* @param req 请求对象
|
|
51
51
|
*/
|
|
52
|
-
export declare function
|
|
52
|
+
export declare function getPost(req: http2.Http2ServerRequest | http.IncomingMessage): Promise<{
|
|
53
|
+
'input': string;
|
|
54
|
+
'raw': Record<string, any>;
|
|
55
|
+
'post': Record<string, any>;
|
|
56
|
+
}>;
|
|
53
57
|
/**
|
|
54
58
|
* --- 获取 formdata 的 post ---
|
|
55
59
|
* @param req 请求头
|
package/sys/route.js
CHANGED
|
@@ -365,18 +365,18 @@ export async function run(data) {
|
|
|
365
365
|
middle.setPrototype('_action', pathRight);
|
|
366
366
|
middle.setPrototype('_headers', headers);
|
|
367
367
|
middle.setPrototype('_get', get);
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
368
|
+
/** --- 是否让框架自动获取 post,true 为自动(默认) --- */
|
|
369
|
+
const reqStartRtn = await middle.onReqStart();
|
|
370
|
+
if (reqStartRtn) {
|
|
371
|
+
const rawPost = await getPost(data.req);
|
|
372
|
+
// --- 原始 POST ---
|
|
373
|
+
middle.setPrototype('_rawPost', rawPost.raw);
|
|
374
|
+
// --- 原始 input ---
|
|
375
|
+
middle.setPrototype('_input', rawPost.input);
|
|
376
|
+
// --- 处理后的 post ---
|
|
377
|
+
middle.setPrototype('_post', rawPost.post);
|
|
378
|
+
// --- form data 格式交由用户自行获取,可以直接获取文件流,然后做他想做的事情 ---
|
|
379
|
+
}
|
|
380
380
|
// --- 执行中间控制器的 onLoad ---
|
|
381
381
|
try {
|
|
382
382
|
rtn = await middle.onLoad();
|
|
@@ -717,7 +717,7 @@ function getPathLeftRight(path) {
|
|
|
717
717
|
return [path.slice(0, pathLio), right === '' ? 'index' : right];
|
|
718
718
|
}
|
|
719
719
|
/**
|
|
720
|
-
* --- 根据 path 获取 ws
|
|
720
|
+
* --- 根据 path 获取 ws 的控制器类名 ---
|
|
721
721
|
* @param path 路径
|
|
722
722
|
*/
|
|
723
723
|
function getWsCtrName(path) {
|
|
@@ -728,7 +728,7 @@ function getWsCtrName(path) {
|
|
|
728
728
|
return path.slice(pathLio + 1).toLowerCase();
|
|
729
729
|
}
|
|
730
730
|
/**
|
|
731
|
-
* ---
|
|
731
|
+
* --- 删除本次请求所有已上传的临时文件 ---
|
|
732
732
|
* @param cctr Ctr 对象 或 files
|
|
733
733
|
*/
|
|
734
734
|
export async function unlinkUploadFiles(cctr) {
|
|
@@ -765,29 +765,18 @@ export async function waitCtr(cctr) {
|
|
|
765
765
|
await unlinkUploadFiles(cctr);
|
|
766
766
|
}
|
|
767
767
|
/**
|
|
768
|
-
* ---
|
|
769
|
-
* @param post
|
|
770
|
-
*/
|
|
771
|
-
export function trimPost(post) {
|
|
772
|
-
for (const key in post) {
|
|
773
|
-
const val = post[key];
|
|
774
|
-
if (typeof val === 'string') {
|
|
775
|
-
post[key] = post[key].trim();
|
|
776
|
-
}
|
|
777
|
-
else if (typeof val === 'object') {
|
|
778
|
-
trimPost(post[key]);
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
/**
|
|
783
|
-
* --- 内部使用,获取 post 对象,如果是文件上传(formdata)的情况则不获取 ---
|
|
768
|
+
* --- 获取 post 对象(通常已自动获取),如果是文件上传(formdata)的情况则不获取 ---
|
|
784
769
|
* @param req 请求对象
|
|
785
770
|
*/
|
|
786
|
-
function getPost(req) {
|
|
771
|
+
export function getPost(req) {
|
|
787
772
|
return new Promise(function (resolve) {
|
|
788
773
|
const ct = req.headers['content-type'] ?? '';
|
|
789
774
|
if (ct.includes('form-data')) {
|
|
790
|
-
resolve(
|
|
775
|
+
resolve({
|
|
776
|
+
'input': '',
|
|
777
|
+
'raw': {},
|
|
778
|
+
'post': {},
|
|
779
|
+
});
|
|
791
780
|
return;
|
|
792
781
|
}
|
|
793
782
|
// --- json 或普通 post ---
|
|
@@ -798,20 +787,38 @@ function getPost(req) {
|
|
|
798
787
|
req.on('end', function () {
|
|
799
788
|
const s = buffer.toString();
|
|
800
789
|
if (!s) {
|
|
801
|
-
resolve(
|
|
790
|
+
resolve({
|
|
791
|
+
'input': '',
|
|
792
|
+
'raw': {},
|
|
793
|
+
'post': {},
|
|
794
|
+
});
|
|
802
795
|
return;
|
|
803
796
|
}
|
|
804
797
|
// --- 判断 json 还是普通 ---
|
|
805
798
|
if (ct.includes('json')) {
|
|
806
799
|
try {
|
|
807
|
-
|
|
800
|
+
const raw = lText.parseJson(s);
|
|
801
|
+
resolve({
|
|
802
|
+
'input': s,
|
|
803
|
+
'raw': raw,
|
|
804
|
+
'post': lText.trimJson(raw),
|
|
805
|
+
});
|
|
808
806
|
}
|
|
809
807
|
catch {
|
|
810
|
-
resolve(
|
|
808
|
+
resolve({
|
|
809
|
+
'input': '',
|
|
810
|
+
'raw': {},
|
|
811
|
+
'post': {},
|
|
812
|
+
});
|
|
811
813
|
}
|
|
812
814
|
return;
|
|
813
815
|
}
|
|
814
|
-
|
|
816
|
+
const raw = lText.queryParse(s);
|
|
817
|
+
resolve({
|
|
818
|
+
'input': s,
|
|
819
|
+
'raw': raw,
|
|
820
|
+
'post': lText.trimJson(raw),
|
|
821
|
+
});
|
|
815
822
|
});
|
|
816
823
|
});
|
|
817
824
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as kebab from '#kebab/index.js';
|
|
2
2
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
3
3
|
export default class extends sCtr.Ctr {
|
|
4
|
+
onReqStart(): boolean;
|
|
4
5
|
onLoad(): string | boolean;
|
|
5
6
|
onUnload(rtn: string | boolean | kebab.DbValue[]): string | boolean | kebab.DbValue[];
|
|
6
7
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
2
2
|
export default class extends sCtr.Ctr {
|
|
3
|
+
onReqStart() {
|
|
4
|
+
if (this._config.const.path === 'test/net-mproxy1') {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
if (this._config.const.path.startsWith('test/net-rproxy/')) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
3
12
|
onLoad() {
|
|
4
13
|
if (this._config.const.path !== 'test/middle') {
|
|
5
14
|
return true;
|
package/www/example/ctr/test.js
CHANGED
|
@@ -1785,14 +1785,14 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
1785
1785
|
'mproxy': {
|
|
1786
1786
|
'url': this._internalUrl + 'test/net-mproxy1',
|
|
1787
1787
|
'auth': '123456',
|
|
1788
|
-
'data': { 'test': '123' }
|
|
1788
|
+
'data': { 'test': '123' },
|
|
1789
1789
|
}
|
|
1790
1790
|
});
|
|
1791
1791
|
echo.push(`<pre>lNet.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json', {
|
|
1792
1792
|
'mproxy': {
|
|
1793
1793
|
'url': '${this._internalUrl}test/net-mproxy1',
|
|
1794
1794
|
'auth': '123456',
|
|
1795
|
-
'data': { 'test': '123' }
|
|
1795
|
+
'data': { 'test': '123' },
|
|
1796
1796
|
}
|
|
1797
1797
|
});</pre>
|
|
1798
1798
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
package/tsconfig.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "esnext",
|
|
4
|
-
"removeComments": false,
|
|
5
|
-
"preserveConstEnums": true, // 保留 const 和 enum 声明
|
|
6
|
-
"outDir": "./",
|
|
7
|
-
"sourceMap": false,
|
|
8
|
-
"target": "ES2021",
|
|
9
|
-
"strict": true, // 严格模式
|
|
10
|
-
"declaration": true, // d.ts
|
|
11
|
-
"declarationMap": false, // d.ts 的 map
|
|
12
|
-
"newLine": "LF",
|
|
13
|
-
"paths": {
|
|
14
|
-
"#kebab/*": ["./*"]
|
|
15
|
-
},
|
|
16
|
-
"moduleResolution": "node",
|
|
17
|
-
"resolveJsonModule": true,
|
|
18
|
-
"esModuleInterop": true
|
|
19
|
-
},
|
|
20
|
-
"include": [
|
|
21
|
-
"./**/*.ts"
|
|
22
|
-
],
|
|
23
|
-
"exclude": [
|
|
24
|
-
"node_modules"
|
|
25
|
-
]
|
|
26
|
-
}
|