@maiyunnet/kebab 9.22.4 → 9.23.0
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/abox/master-log.d.ts +1 -0
- package/abox/master-log.js +26 -0
- package/abox/text.d.ts +2 -0
- package/abox/text.js +21 -0
- package/doc/kebab-rag.md +186 -162
- package/index.d.ts +7 -2
- package/index.js +1 -1
- package/lib/text.d.ts +6 -4
- package/lib/text.js +11 -34
- package/lib/undici/response.d.ts +1 -1
- package/lib/undici.d.ts +5 -5
- package/lib/undici.js +7 -6
- package/package.json +3 -2
- package/sys/master.js +185 -279
- package/sys/route.d.ts +5 -5
- package/sys/route.js +33 -20
package/sys/route.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import * as net from 'net';
|
|
|
9
9
|
import * as sCtr from './ctr.js';
|
|
10
10
|
import * as kebab from '#kebab/index.js';
|
|
11
11
|
/**
|
|
12
|
-
* ---
|
|
12
|
+
* --- 清空缓存的项目配置,下次请求时重新读取文件 ---
|
|
13
13
|
*/
|
|
14
14
|
export declare function clearKebabConfigs(): void;
|
|
15
15
|
/**
|
|
@@ -58,10 +58,10 @@ export declare function getPost(req: http2.Http2ServerRequest | http.IncomingMes
|
|
|
58
58
|
'raw': Record<string, any>;
|
|
59
59
|
'post': Record<string, any>;
|
|
60
60
|
} | false>;
|
|
61
|
-
/** ---
|
|
61
|
+
/** --- 解析上传数据时的失败原因,只提供错误码和限制值,不带临时路径或异常对象 --- */
|
|
62
62
|
export interface IFormDataError {
|
|
63
63
|
'code': 'INVALID_BOUNDARY' | 'FILE_TOO_LARGE' | 'TOTAL_TOO_LARGE' | 'FIELD_TOO_LARGE' | 'HEADER_TOO_LARGE' | 'TOO_MANY_PARTS' | 'FILE_TYPE' | 'TIMEOUT' | 'TEMP_FILE_ERROR' | 'REQUEST_ERROR' | 'REQUEST_ABORTED';
|
|
64
|
-
/** ---
|
|
64
|
+
/** --- 超出的限制值:大小用字节,超时用毫秒,字段和文件数量用个 --- */
|
|
65
65
|
'limit'?: number;
|
|
66
66
|
}
|
|
67
67
|
/** --- multipart 文件处理及失败通知 --- */
|
|
@@ -72,7 +72,7 @@ export interface IFormDataEvents {
|
|
|
72
72
|
onfiledata?: (chunk: Buffer) => void;
|
|
73
73
|
/** --- 文件上传结束时触发,仅 start 返回 true 时触发 --- */
|
|
74
74
|
onfileend?: () => void;
|
|
75
|
-
/** ---
|
|
75
|
+
/** --- 首次失败时调用一次,getFormData 返回 false;若连接已断开,可能无法回复客户端 --- */
|
|
76
76
|
onerror?: (error: IFormDataError) => void;
|
|
77
77
|
}
|
|
78
78
|
/** --- multipart 上传限制 --- */
|
|
@@ -85,7 +85,7 @@ export interface IFormDataLimits {
|
|
|
85
85
|
'maxFieldSize'?: number;
|
|
86
86
|
/** --- 整体请求最大字节数,不设置或设为 0 则不限制 --- */
|
|
87
87
|
'maxTotalSize'?: number;
|
|
88
|
-
/** ---
|
|
88
|
+
/** --- 每个字段或文件的 multipart 头部最多多少字节,默认 16 KB --- */
|
|
89
89
|
'maxHeaderSize'?: number;
|
|
90
90
|
/** --- multipart 字段与文件总数量,默认 1000 --- */
|
|
91
91
|
'maxParts'?: number;
|
package/sys/route.js
CHANGED
|
@@ -16,10 +16,10 @@ import * as lWs from '#kebab/lib/ws.js';
|
|
|
16
16
|
import * as lLang from '#kebab/lib/lang.js';
|
|
17
17
|
import * as sCtr from './ctr.js';
|
|
18
18
|
import * as kebab from '#kebab/index.js';
|
|
19
|
-
/** ---
|
|
19
|
+
/** --- 已加载的项目配置,按 kebab.json 的文件路径缓存,已补齐全局默认值 --- */
|
|
20
20
|
let kebabConfigs = {};
|
|
21
21
|
/**
|
|
22
|
-
* ---
|
|
22
|
+
* --- 清空缓存的项目配置,下次请求时重新读取文件 ---
|
|
23
23
|
*/
|
|
24
24
|
export function clearKebabConfigs() {
|
|
25
25
|
kebabConfigs = {};
|
|
@@ -40,7 +40,7 @@ function respond500(res) {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
|
-
* ---
|
|
43
|
+
* --- 保存用户输入的字段名和值,__proto__ 也只作为普通字段,不会改变对象原型 ---
|
|
44
44
|
* @param target 目标对象
|
|
45
45
|
* @param key 键
|
|
46
46
|
* @param value 值
|
|
@@ -106,7 +106,7 @@ export async function run(data) {
|
|
|
106
106
|
if (routeContent) {
|
|
107
107
|
kebabConfigs[data.rootPath + 'kebab.json'].route = lText.parseJson(routeContent);
|
|
108
108
|
}
|
|
109
|
-
// ---
|
|
109
|
+
// --- 用全局配置补齐项目中没设置的项,已有设置不覆盖 ---
|
|
110
110
|
for (const name in lCore.globalConfig) {
|
|
111
111
|
if (typeof lCore.globalConfig[name] !== 'object') {
|
|
112
112
|
if (kebabConfigs[data.rootPath + 'kebab.json'][name] === undefined) {
|
|
@@ -131,6 +131,18 @@ export async function run(data) {
|
|
|
131
131
|
for (const name in configData) {
|
|
132
132
|
config[name] = configData[name];
|
|
133
133
|
}
|
|
134
|
+
// --- 复制一份 set,后面改资源地址时就不会改动原来的配置 ---
|
|
135
|
+
config.set = { ...config.set };
|
|
136
|
+
/** --- host 不带默认端口(HTTP 的 80、HTTPS 的 443)--- */
|
|
137
|
+
let hostCanonical = data.uri.host ?? '';
|
|
138
|
+
if (data.uri.protocol === 'https:' && Number(data.uri.port) === 443) {
|
|
139
|
+
hostCanonical = lText.parseHost(hostCanonical).rawHostname;
|
|
140
|
+
}
|
|
141
|
+
else if (data.uri.protocol === 'http:' && Number(data.uri.port) === 80) {
|
|
142
|
+
hostCanonical = lText.parseHost(hostCanonical).rawHostname;
|
|
143
|
+
}
|
|
144
|
+
/** --- 网址开头的协议和 host,如 https://example.com --- */
|
|
145
|
+
const urlOrigin = `${data.uri.protocol ?? ''}//${hostCanonical}`;
|
|
134
146
|
config.const = {
|
|
135
147
|
'path': data.path,
|
|
136
148
|
'qs': data.uri.query ?? '',
|
|
@@ -145,7 +157,8 @@ export async function run(data) {
|
|
|
145
157
|
'host': data.uri.host ?? '',
|
|
146
158
|
'hostname': data.uri.hostname ?? '',
|
|
147
159
|
'hostport': data.uri.port ? parseInt(data.uri.port) : (data.uri.protocol === 'https:' ? 443 : 80),
|
|
148
|
-
'
|
|
160
|
+
'hostCanonical': hostCanonical,
|
|
161
|
+
'urlOrigin': urlOrigin,
|
|
149
162
|
'uri': data.uri,
|
|
150
163
|
// --- 服务端用的路径 ---
|
|
151
164
|
'rootPath': data.rootPath,
|
|
@@ -157,7 +170,7 @@ export async function run(data) {
|
|
|
157
170
|
// --- 前端用的路径 ---
|
|
158
171
|
'urlBase': data.urlBase,
|
|
159
172
|
'urlStc': data.urlBase + 'stc/',
|
|
160
|
-
'urlFull':
|
|
173
|
+
'urlFull': `${urlOrigin}${data.urlBase}`,
|
|
161
174
|
'urlStcFull': ''
|
|
162
175
|
};
|
|
163
176
|
config.const.urlStcFull = config.const.urlFull + 'stc/';
|
|
@@ -173,7 +186,7 @@ export async function run(data) {
|
|
|
173
186
|
if (path === '') {
|
|
174
187
|
path = '@';
|
|
175
188
|
}
|
|
176
|
-
/** ---
|
|
189
|
+
/** --- 这些静态资源路径不自动跳转到语言页 --- */
|
|
177
190
|
const stcPaths = /^(stc\/.*|favicon.\w+?\??.*|apple[\w-]+?\.png\??.*|[\w-]+?\.(?:txt|html|xml)\??.*)/;
|
|
178
191
|
// --- 检测是否托管语言页面 ---
|
|
179
192
|
const param = [];
|
|
@@ -581,7 +594,7 @@ export async function run(data) {
|
|
|
581
594
|
data.res.setHeader('expires', new Date(Date.now() + cacheTTL * 1_000).toUTCString());
|
|
582
595
|
data.res.setHeader('cache-control', 'max-age=' + cacheTTL.toString());
|
|
583
596
|
}
|
|
584
|
-
// ---
|
|
597
|
+
// --- 记录控制器有没有设置状态码;没设置时,带 Location 且无返回内容就用 302 ---
|
|
585
598
|
const hasCustomHttpCode = httpCode !== 0;
|
|
586
599
|
// --- 设置自定义 hcode ---
|
|
587
600
|
if (httpCode === 0) {
|
|
@@ -649,7 +662,7 @@ export async function run(data) {
|
|
|
649
662
|
if (compress) {
|
|
650
663
|
// --- Node.js 自动设置 transfer-encoding: chunked ---
|
|
651
664
|
data.res.setHeader('content-encoding', compress.type);
|
|
652
|
-
// ---
|
|
665
|
+
// --- 压缩后大小会变,删掉原来的 content-length ---
|
|
653
666
|
data.res.removeHeader('content-length');
|
|
654
667
|
}
|
|
655
668
|
lCore.writeHead(data.res, httpCode);
|
|
@@ -995,7 +1008,7 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
995
1008
|
resolve(false);
|
|
996
1009
|
return;
|
|
997
1010
|
}
|
|
998
|
-
// ---
|
|
1011
|
+
// --- 上传一直没结束时,按超时时间返回失败,避免一直等下去 ---
|
|
999
1012
|
/** --- 超时定时器 --- */
|
|
1000
1013
|
let timer;
|
|
1001
1014
|
/** --- 是否已经结束(防止重复 resolve) --- */
|
|
@@ -1007,7 +1020,7 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
1007
1020
|
timer = undefined;
|
|
1008
1021
|
}
|
|
1009
1022
|
}
|
|
1010
|
-
//
|
|
1023
|
+
// --- 下面的变量记录上传进度,每次收到数据时继续使用 ---
|
|
1011
1024
|
/** --- 当前临时读入的还未处理的 buffer --- */
|
|
1012
1025
|
let buffer = Buffer.from('');
|
|
1013
1026
|
/** --- 状态机 --- */
|
|
@@ -1030,13 +1043,13 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
1030
1043
|
let ftmpName = '';
|
|
1031
1044
|
/** --- 当前正在写入的临时文件流 --- */
|
|
1032
1045
|
let ftmpStream;
|
|
1033
|
-
/** ---
|
|
1046
|
+
/** --- 当前临时文件已写入的字节数 --- */
|
|
1034
1047
|
let ftmpSize = 0;
|
|
1035
1048
|
/** --- 当前正在写入的文件数量 --- */
|
|
1036
1049
|
let writeFileLength = 0;
|
|
1037
1050
|
/** --- 当前读取是否已经完全结束 --- */
|
|
1038
1051
|
let readEnd = false;
|
|
1039
|
-
/** ---
|
|
1052
|
+
/** --- 本次上传是否已经失败,失败后返回 false --- */
|
|
1040
1053
|
let rejected = false;
|
|
1041
1054
|
/**
|
|
1042
1055
|
* --- 拒绝本次上传,继续读取请求以便正常返回错误响应 ---
|
|
@@ -1058,12 +1071,12 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
1058
1071
|
let totalSize = 0;
|
|
1059
1072
|
/** --- 已解析的字段与文件数量 --- */
|
|
1060
1073
|
let partCount = 0;
|
|
1061
|
-
/** ---
|
|
1074
|
+
/** --- 临时文件对应的写入流,删除文件前先关闭 --- */
|
|
1062
1075
|
const fileStreams = new Map();
|
|
1063
|
-
/** ---
|
|
1076
|
+
/** --- 已安排删除的文件,避免给同一个文件重复添加 close 回调 --- */
|
|
1064
1077
|
const discardedFiles = new Set();
|
|
1065
1078
|
/**
|
|
1066
|
-
* ---
|
|
1079
|
+
* --- 等文件流关闭后再删临时文件,避免文件还没创建就先删 ---
|
|
1067
1080
|
* @param filePath 本次上传创建的临时文件路径
|
|
1068
1081
|
* @returns 无返回值
|
|
1069
1082
|
*/
|
|
@@ -1104,7 +1117,7 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
1104
1117
|
ftmpName = '';
|
|
1105
1118
|
--writeFileLength;
|
|
1106
1119
|
}
|
|
1107
|
-
// ---
|
|
1120
|
+
// --- 上面的变量准备好后再启动定时器,超时回调会用到它们 ---
|
|
1108
1121
|
const timeoutMs = limits.timeout ?? 300_000;
|
|
1109
1122
|
if (timeoutMs > 0) {
|
|
1110
1123
|
timer = setTimeout(() => {
|
|
@@ -1119,7 +1132,7 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
1119
1132
|
resolve(false);
|
|
1120
1133
|
}, timeoutMs);
|
|
1121
1134
|
}
|
|
1122
|
-
/** ---
|
|
1135
|
+
/** --- 文件太大时,停止保存、删除临时文件,并将本次上传标为失败 --- */
|
|
1123
1136
|
function rejectFile() {
|
|
1124
1137
|
reject({ 'code': 'FILE_TOO_LARGE', 'limit': limits.maxFileSize });
|
|
1125
1138
|
cleanupActiveFile();
|
|
@@ -1222,7 +1235,7 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
1222
1235
|
});
|
|
1223
1236
|
ftmpStream = activeStream;
|
|
1224
1237
|
activeStream.on('error', (error) => {
|
|
1225
|
-
// ---
|
|
1238
|
+
// --- 失败或结束后忽略后续 error,避免重复报错和重复减 writeFileLength ---
|
|
1226
1239
|
if (rejected || finished) {
|
|
1227
1240
|
return;
|
|
1228
1241
|
}
|
|
@@ -1319,7 +1332,7 @@ export function getFormData(req, events = {}, limits = {}) {
|
|
|
1319
1332
|
ftmpStream.write(writeBuffer);
|
|
1320
1333
|
ftmpSize += Buffer.byteLength(writeBuffer);
|
|
1321
1334
|
ftmpStream.end((error) => {
|
|
1322
|
-
// ---
|
|
1335
|
+
// --- 写入报错时交给 error 回调处理,不把这个文件算作已保存 ---
|
|
1323
1336
|
if (error) {
|
|
1324
1337
|
return;
|
|
1325
1338
|
}
|