@maiyunnet/kebab 9.29.6 → 10.0.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.
Files changed (52) hide show
  1. package/abox/proxy.js +24 -2
  2. package/abox/response.js +36 -17
  3. package/abox/session-login.js +1 -13
  4. package/doc/kebab-rag.md +1522 -1034
  5. package/index.d.ts +12 -1
  6. package/index.js +1 -1
  7. package/lib/ai.d.ts +4 -0
  8. package/lib/ai.js +32 -16
  9. package/lib/cookie.d.ts +2 -0
  10. package/lib/cookie.js +47 -45
  11. package/lib/core.d.ts +26 -0
  12. package/lib/core.js +36 -13
  13. package/lib/cron.js +1 -8
  14. package/lib/crypto.js +20 -6
  15. package/lib/db/conn.js +1 -0
  16. package/lib/fs.js +78 -31
  17. package/lib/kv.d.ts +182 -79
  18. package/lib/kv.js +518 -214
  19. package/lib/s3.d.ts +2 -0
  20. package/lib/s3.js +14 -27
  21. package/lib/session.d.ts +7 -0
  22. package/lib/session.js +23 -2
  23. package/lib/socket.d.ts +4 -1
  24. package/lib/socket.js +1 -0
  25. package/lib/sql.d.ts +0 -5
  26. package/lib/sql.js +48 -12
  27. package/lib/text.d.ts +10 -5
  28. package/lib/text.js +22 -12
  29. package/lib/turnstile.d.ts +6 -0
  30. package/lib/turnstile.js +16 -9
  31. package/lib/undici.d.ts +7 -1
  32. package/lib/undici.js +72 -36
  33. package/lib/ws.d.ts +1 -1
  34. package/lib/ws.js +85 -128
  35. package/lib/zip.js +46 -45
  36. package/package.json +2 -2
  37. package/sys/child.js +178 -146
  38. package/sys/cmd.js +196 -197
  39. package/sys/ctr.d.ts +14 -14
  40. package/sys/ctr.js +145 -59
  41. package/sys/master.js +111 -80
  42. package/sys/mod.d.ts +8 -2
  43. package/sys/mod.js +23 -11
  44. package/sys/monitor/files.d.ts +2 -1
  45. package/sys/monitor/files.js +77 -21
  46. package/sys/monitor/watchdog.d.ts +7 -0
  47. package/sys/monitor/watchdog.js +311 -203
  48. package/sys/monitor.d.ts +59 -54
  49. package/sys/monitor.js +742 -681
  50. package/sys/route.d.ts +4 -4
  51. package/sys/route.js +93 -52
  52. package/www/example/ctr/test.js +8 -8
package/index.d.ts CHANGED
@@ -3,8 +3,9 @@
3
3
  * Date: 2019-3-30 12:46:41
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
+ import type * as tls from 'node:tls';
6
7
  /** --- 当前系统版本号 --- */
7
- export declare const VER = "9.29.6";
8
+ export declare const VER = "10.0.0";
8
9
  /** --- 框架根目录,以 / 结尾 --- */
9
10
  export declare const ROOT_PATH: string;
10
11
  /** --- 框架的 LIB,以 / 结尾 --- */
@@ -130,12 +131,22 @@ export interface IConfigDns {
130
131
  }
131
132
  /** --- kv --- */
132
133
  export interface IConfigKv {
134
+ /** --- 服务端地址(IP 或域名) --- */
133
135
  'host': string;
136
+ /** --- 服务端端口 --- */
134
137
  'port': number;
138
+ /** --- 数据库编号(0 起) --- */
135
139
  'index': number;
140
+ /** --- 业务键名前缀 --- */
136
141
  'pre': string;
142
+ /** --- ACL 用户名(留空则走传统单密码认证) --- */
137
143
  'user': string;
144
+ /** --- 认证密码或 Kvrocks namespace token --- */
138
145
  'pwd': string;
146
+ /** --- 为 true 时将非 0 的 index 折算为键名前缀(如 db1:),不再向服务端发 SELECT --- */
147
+ 'dbPre'?: boolean;
148
+ /** --- 是否启用 TLS 连接;传入对象时自动启用 TLS 并透传证书/SNI 等参数(如 servername、ca) --- */
149
+ 'tls'?: boolean | tls.ConnectionOptions;
139
150
  }
140
151
  /** --- sql --- */
141
152
  export interface IConfigSql {
package/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
6
  /** --- 当前系统版本号 --- */
7
- export const VER = '9.29.6';
7
+ export const VER = '10.0.0';
8
8
  // --- 服务端用的路径 ---
9
9
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
10
10
  /** --- /xxx/xxx --- */
package/lib/ai.d.ts CHANGED
@@ -65,6 +65,8 @@ export declare class Ai {
65
65
  constructor(ctrEtc: sCtr.Ctr | kebab.IConfigAi, opt: IOptions);
66
66
  /** --- 获取当前服务商 --- */
67
67
  get service(): ESERVICE;
68
+ /** --- 获取阿里云 DashScope 对应区域的域名 --- */
69
+ private _getDashScopeOrigin;
68
70
  /** --- 创建非流式对话 --- */
69
71
  chat(body: openai.default.Chat.Completions.ChatCompletionCreateParamsNonStreaming): Promise<openai.APIPromise<openai.default.Chat.ChatCompletion> | false>;
70
72
  /** --- 创建流式对话 --- */
@@ -88,6 +90,7 @@ export declare class Ai {
88
90
  'negative'?: string;
89
91
  /** --- 长 x 宽,不同模型要求不同,如 [1664, 928] --- */
90
92
  'size': number[];
93
+ /** --- 生成图片的数量,默认为 1 --- */
91
94
  'n'?: number;
92
95
  /** --- 随机种子,默认为随机 --- */
93
96
  'seed'?: number;
@@ -110,6 +113,7 @@ export declare class Ai {
110
113
  'prompt': string;
111
114
  /** --- 文本(默认)、首尾帧、参考图 --- */
112
115
  'mode'?: 'text' | 'frame' | 'ref';
116
+ /** --- 参考图,1 张为首帧,2 张为首尾帧 --- */
113
117
  'imgs'?: string[];
114
118
  /** --- 负面提示词,用于引导模型避免生成某些内容 --- */
115
119
  'negative'?: string;
package/lib/ai.js CHANGED
@@ -3,6 +3,7 @@
3
3
  * Date: 2025-10-27 21:31:06
4
4
  * Last: 2025-10-27 21:31:08
5
5
  */
6
+ // --- 第三方 ---
6
7
  import * as openai from 'openai';
7
8
  import * as lCore from '#kebab/lib/core.js';
8
9
  import * as lText from '#kebab/lib/text.js';
@@ -148,6 +149,16 @@ export class Ai {
148
149
  get service() {
149
150
  return this._service;
150
151
  }
152
+ /** --- 获取阿里云 DashScope 对应区域的域名 --- */
153
+ _getDashScopeOrigin() {
154
+ if (this._service === ESERVICE.ALIAS) {
155
+ return 'https://dashscope-intl.aliyuncs.com';
156
+ }
157
+ if (this._service === ESERVICE.ALINE) {
158
+ return 'https://dashscope-us.aliyuncs.com';
159
+ }
160
+ return 'https://dashscope.aliyuncs.com';
161
+ }
151
162
  /** --- 创建对话 --- */
152
163
  async chat(body) {
153
164
  try {
@@ -198,8 +209,9 @@ export class Ai {
198
209
  case ESERVICE.ALICN:
199
210
  case ESERVICE.ALIAS:
200
211
  case ESERVICE.ALINE: {
212
+ // --- 阿里云 DashScope 图像生成 ---
201
213
  try {
202
- const res = await this._fetch(`https://dashscope${this._service === ESERVICE.ALIAS ? '-intl' : (this._service === ESERVICE.ALINE ? '-us' : '')}.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation`, {
214
+ const res = await this._fetch(`${this._getDashScopeOrigin()}/api/v1/services/aigc/multimodal-generation/generation`, {
203
215
  'method': 'POST',
204
216
  'headers': {
205
217
  'authorization': `Bearer ${this.link.apiKey}`,
@@ -430,8 +442,23 @@ export class Ai {
430
442
  }
431
443
  }
432
444
  }
445
+ // --- 按模式组装参考图/首尾帧参数 ---
446
+ const frameParams = {};
447
+ if (mode === 'frame') {
448
+ if (imgs.length > 1) {
449
+ frameParams['first_frame_url'] = imgs[0];
450
+ frameParams['last_frame_url'] = imgs[1];
451
+ }
452
+ else {
453
+ frameParams['img_url'] = imgs[0];
454
+ }
455
+ }
456
+ else if (mode !== 'text') {
457
+ frameParams['reference_urls'] = imgs;
458
+ }
459
+ // --- 提交异步视频生成任务 ---
433
460
  try {
434
- const res = await this._fetch(`https://dashscope${this._service === ESERVICE.ALIAS ? '-intl' : (this._service === ESERVICE.ALINE ? '-us' : '')}.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis`, {
461
+ const res = await this._fetch(`${this._getDashScopeOrigin()}/api/v1/services/aigc/video-generation/video-synthesis`, {
435
462
  'method': 'POST',
436
463
  'headers': {
437
464
  'authorization': `Bearer ${this.link.apiKey}`,
@@ -442,9 +469,7 @@ export class Ai {
442
469
  'model': opt.model,
443
470
  'input': {
444
471
  'prompt': opt.prompt.replace(/\[I(\d+)\]/g, 'Character$1'),
445
- ...(typeof audio === 'string' ? {
446
- 'audio_url': audio,
447
- } : {})
472
+ ...(typeof audio === 'string' ? { 'audio_url': audio } : {}),
448
473
  },
449
474
  'parameters': {
450
475
  'negative_prompt': opt.negative ?? ' ',
@@ -455,16 +480,7 @@ export class Ai {
455
480
  'duration': duration,
456
481
  'shot_type': shot,
457
482
  'prompt_extend': extend,
458
- ...((mode !== 'text') && (mode === 'frame' ? (imgs.length > 1 ? {
459
- // --- 首尾帧 ---
460
- 'first_frame_url': imgs[0],
461
- 'last_frame_url': imgs[1],
462
- } : {
463
- // --- 首帧 ---
464
- 'img_url': imgs[0],
465
- }) : {
466
- 'reference_urls': imgs,
467
- })),
483
+ ...frameParams,
468
484
  },
469
485
  }),
470
486
  });
@@ -497,7 +513,7 @@ export class Ai {
497
513
  return false;
498
514
  }
499
515
  try {
500
- const res = await this._fetch(`https://dashscope${this._service === ESERVICE.ALIAS ? '-intl' : (this._service === ESERVICE.ALINE ? '-us' : '')}.aliyuncs.com/api/v1/tasks/${opt.task}`, {
516
+ const res = await this._fetch(`${this._getDashScopeOrigin()}/api/v1/tasks/${opt.task}`, {
501
517
  'method': 'GET',
502
518
  'headers': {
503
519
  'authorization': `Bearer ${this.link.apiKey}`,
package/lib/cookie.d.ts CHANGED
@@ -41,4 +41,6 @@ export interface ICookie {
41
41
  'domain': string;
42
42
  'secure': boolean;
43
43
  'httponly': boolean;
44
+ /** --- 是否仅限当前主机(未指定 Domain 属性时为 true) --- */
45
+ 'hostOnly'?: boolean;
44
46
  }
package/lib/cookie.js CHANGED
@@ -11,7 +11,7 @@ import * as lTime from '#kebab/lib/time.js';
11
11
  export function setCookie(cookie, name, value, domain, opt = {}) {
12
12
  const tim = lTime.stamp();
13
13
  const ttl = opt.ttl ?? 0;
14
- domain = lText.parseHost(domain).hostname;
14
+ domain = lText.parseHost(domain).hostname.toLowerCase();
15
15
  const domainN = domain.startsWith('.') ? domain.slice(1) : domain;
16
16
  let exp = -1992199400;
17
17
  if (ttl) {
@@ -24,7 +24,8 @@ export function setCookie(cookie, name, value, domain, opt = {}) {
24
24
  'path': opt['path'] ?? '/',
25
25
  'domain': domainN,
26
26
  'secure': opt['ssl'] ? true : false,
27
- 'httponly': opt['httponly'] ? true : false
27
+ 'httponly': opt['httponly'] ? true : false,
28
+ 'hostOnly': !domain.startsWith('.'),
28
29
  };
29
30
  }
30
31
  /**
@@ -68,10 +69,15 @@ export async function buildCookieObject(cookie, setCookies, uri) {
68
69
  cookieTmp[key.toLowerCase()] = val;
69
70
  }
70
71
  }
72
+ if (!cookieTmp['name']) {
73
+ continue;
74
+ }
71
75
  // --- 获取定义的 domain ---
76
+ const reqHost = (uri.hostname ?? '').toLowerCase();
72
77
  let domain = '', domainN = '';
78
+ let hostOnly = false;
73
79
  if (cookieTmp['domain']) {
74
- cookieTmp['domain'] = lText.parseHost(cookieTmp['domain']).hostname;
80
+ cookieTmp['domain'] = lText.parseHost(cookieTmp['domain']).hostname.toLowerCase();
75
81
  if (!(cookieTmp['domain'].startsWith('.'))) {
76
82
  domain = '.' + cookieTmp['domain'];
77
83
  domainN = cookieTmp['domain'];
@@ -82,8 +88,9 @@ export async function buildCookieObject(cookie, setCookies, uri) {
82
88
  }
83
89
  }
84
90
  else {
85
- domain = '.' + (uri.hostname ?? '');
86
- domainN = uri.hostname ?? '';
91
+ hostOnly = true;
92
+ domain = '.' + reqHost;
93
+ domainN = reqHost;
87
94
  }
88
95
  // --- 判断有没有设置 domain 的权限 ---
89
96
  // --- uri.hostname vs domain(domainN) ---
@@ -91,18 +98,18 @@ export async function buildCookieObject(cookie, setCookies, uri) {
91
98
  // --- ok.xxx.com vs .xxx.com: true ---
92
99
  // --- z.ok.xxx.com vs .xxx.com: true ---
93
100
  // --- ok.xxx.com vs .zz.ok.xxx.com: false ---
94
- if (uri.hostname !== domainN) {
101
+ if (reqHost !== domainN) {
95
102
  // --- 设置的域名和当前 host 不相等,如果是 IP、无 . 域名,则直接失败 ---
96
- if (!lText.isDomain(uri.hostname ?? '')) {
103
+ if (!lText.isDomain(reqHost)) {
97
104
  continue;
98
105
  }
99
106
  const parseDomain = await lText.parseDomain(domainN);
100
- if (parseDomain.tld === domainN.toLowerCase()) {
107
+ if (parseDomain.tld === domainN) {
101
108
  // --- 不能给 tld 设置 cookie ---
102
109
  continue;
103
110
  }
104
111
  // --- 判断访问路径 (uri['host']) 是不是设置域名 (domain) 的孩子,domain 必须是 uriHost 的同级或者父辈 ---
105
- if (!((uri.hostname ?? '').endsWith(domain))) {
112
+ if (!(reqHost.endsWith(domain))) {
106
113
  // --- false 代表进入了,代表失败 ---
107
114
  // --- ok.xxx.com, .xxx.com: true ---
108
115
  // --- ok.xxx.com, .ppp.com: false ---
@@ -112,15 +119,13 @@ export async function buildCookieObject(cookie, setCookies, uri) {
112
119
  }
113
120
  const cookieKey = cookieTmp['name'] + '-' + domainN;
114
121
  let exp = -1992199400;
115
- if (cookieTmp['max-age'] !== undefined) {
122
+ if (cookieTmp['max-age'] !== undefined && /^-?\d+$/.test(cookieTmp['max-age'])) {
116
123
  const maxAge = Number(cookieTmp['max-age']);
117
- if (!(isNaN(maxAge))) {
118
- if (maxAge <= 0) {
119
- delete cookie[cookieKey];
120
- continue;
121
- }
122
- exp = tim + maxAge;
124
+ if (maxAge <= 0) {
125
+ delete cookie[cookieKey];
126
+ continue;
123
127
  }
128
+ exp = tim + maxAge;
124
129
  }
125
130
  if ((exp === -1992199400) && cookieTmp['expires']) {
126
131
  const expires = lTime.stamp(cookieTmp['expires']);
@@ -134,21 +139,19 @@ export async function buildCookieObject(cookie, setCookies, uri) {
134
139
  }
135
140
  // --- path ---
136
141
  let path = cookieTmp['path'] ?? '';
137
- if (path === '') {
138
- const srp = (uri.pathname ?? '').lastIndexOf('/');
139
- path = (uri.pathname ?? '').slice(0, srp + 1);
140
- }
141
- else if (!path.startsWith('/')) {
142
- path = '/' + path;
142
+ if (path === '' || !path.startsWith('/')) {
143
+ const srp = (uri.pathname ?? '/').lastIndexOf('/');
144
+ path = (uri.pathname ?? '/').slice(0, srp + 1) || '/';
143
145
  }
144
146
  cookie[cookieKey] = {
145
147
  'name': cookieTmp['name'],
146
- 'value': cookieTmp['value'],
148
+ 'value': cookieTmp['value'] ?? '',
147
149
  'exp': exp,
148
150
  'path': path,
149
151
  'domain': domainN,
150
152
  'secure': cookieTmp['secure'] !== undefined,
151
- 'httponly': cookieTmp['httponly'] !== undefined
153
+ 'httponly': cookieTmp['httponly'] !== undefined,
154
+ 'hostOnly': hostOnly,
152
155
  };
153
156
  }
154
157
  }
@@ -159,6 +162,9 @@ export async function buildCookieObject(cookie, setCookies, uri) {
159
162
  */
160
163
  export function buildCookieQuery(cookie, uri) {
161
164
  const tim = lTime.stamp();
165
+ const reqHost = (uri.hostname ?? '').toLowerCase();
166
+ const reqPath = uri.pathname ?? uri.path ?? '/';
167
+ const isSecure = uri.protocol === 'https:' || uri.protocol === 'wss:';
162
168
  let cookieStr = '';
163
169
  for (const key in cookie) {
164
170
  const item = cookie[key];
@@ -166,30 +172,26 @@ export function buildCookieQuery(cookie, uri) {
166
172
  delete cookie[key];
167
173
  continue;
168
174
  }
169
- uri.path ??= '/';
170
- if (item.secure && (uri.protocol !== 'https:')) {
175
+ if (item.secure && !isSecure) {
171
176
  continue;
172
177
  }
173
- // --- 判断 path 是否匹配 ---
174
- if (!uri.path.startsWith(item.path)) {
178
+ // --- 按 RFC 6265 路径段边界判断 path 是否匹配(/app 不匹配 /app-evil) ---
179
+ const cPath = item.path || '/';
180
+ const pathMatch = reqPath === cPath ||
181
+ (cPath.endsWith('/') ? reqPath.startsWith(cPath) : reqPath.startsWith(cPath + '/'));
182
+ if (!pathMatch) {
175
183
  continue;
176
184
  }
177
- const domain = '.' + item.domain;
178
- // --- 判断 $uri['host'] 必须是 $domain 的同级或子级 ---
179
- // --- $uri['host'] vs $domain ---
180
- // --- ok.xxx.com vs .ok.xxx.com: true ---
181
- // --- ok.xxx.com vs .xxx.com: true ---
182
- // --- z.ok.xxx.com vs .xxx.com: true ---
183
- // --- ok.xxx.com vs .zz.ok.xxx.com: false ---
184
- if ('.' + (uri.hostname ?? '') !== domain) {
185
- // --- 域名不相等,那么判断当前域名 host 是不是 domain 的孩子 ---
186
- if (!(uri.hostname.endsWith(domain))) {
187
- // --- false 代表进入,被排除了,因为 cookie 的 domain 和当前 host 后半部分,代表不是 domain 的孩子 ---
188
- // --- ok.xxx.com, .zz.ok.xxx.com: false ---
189
- // --- pp.ok.xxx.com, .zz.ok.xxx.com: false ---
190
- // --- q.b.ok.xx.com, .zz.ok.xxx.com: false ---
191
- // --- z.ok.xxx.com, .xxx.com: true ---
192
- // --- xx.xxx.com, .ok.xxx.com: false ---
185
+ const itemDomain = item.domain.toLowerCase();
186
+ if (item.hostOnly) {
187
+ // --- 未指定 Domain 属性的 host-only Cookie 仅限精确匹配主机 ---
188
+ if (reqHost !== itemDomain) {
189
+ continue;
190
+ }
191
+ }
192
+ else if (reqHost !== itemDomain) {
193
+ // --- 域名不相等时,判断当前 host 是不是 cookie domain 的子域 ---
194
+ if (!reqHost.endsWith('.' + itemDomain)) {
193
195
  continue;
194
196
  }
195
197
  }
@@ -209,7 +211,7 @@ export function buildCookieQuery(cookie, uri) {
209
211
  export function resetCookieSession(cookie) {
210
212
  for (const key in cookie) {
211
213
  const item = cookie[key];
212
- if (item.exp === -1992199400000) {
214
+ if (item.exp === -1992199400) {
213
215
  delete cookie[key];
214
216
  }
215
217
  }
package/lib/core.d.ts CHANGED
@@ -24,11 +24,17 @@ export declare const globalConfig: kebab.IConfig & {
24
24
  };
25
25
  /** --- Cookie 设置的选项 --- */
26
26
  export interface ICookieOptions {
27
+ /** --- 有效期秒数,不设或 undefined 表示关闭浏览器失效 --- */
27
28
  'ttl'?: number;
29
+ /** --- 生效路径,默认 / --- */
28
30
  'path'?: string;
31
+ /** --- 生效域名 --- */
29
32
  'domain'?: string;
33
+ /** --- 是否仅 HTTPS 发送 --- */
30
34
  'ssl'?: boolean;
35
+ /** --- 是否禁止前端脚本读取 --- */
31
36
  'httponly'?: boolean;
37
+ /** --- 跨站发送策略 --- */
32
38
  'samesite'?: 'None' | 'Lax' | 'Strict';
33
39
  }
34
40
  /**
@@ -302,11 +308,17 @@ export declare function sendVhost(files: Record<string, string>, hosts?: string[
302
308
  }>>;
303
309
  /** --- log 设置的选项 --- */
304
310
  export interface ILogOptions {
311
+ /** --- 当前请求路径 --- */
305
312
  'path'?: string;
313
+ /** --- 站点完整基础 URL --- */
306
314
  'urlFull'?: string;
315
+ /** --- 主机名,用于日志分目录,默认 system --- */
307
316
  'hostname'?: string;
317
+ /** --- 原始 HTTP 请求对象 --- */
308
318
  'req'?: http2.Http2ServerRequest | http.IncomingMessage | null;
319
+ /** --- GET 查询参数 --- */
309
320
  'get'?: Record<string, any>;
321
+ /** --- 请求头 --- */
310
322
  'headers'?: http.IncomingHttpHeaders;
311
323
  }
312
324
  /**
@@ -342,27 +354,41 @@ export declare function getLog(opt: {
342
354
  } | false>;
343
355
  /** --- 已保存监控事件的简短摘要,path 相对于 log/monitor/ --- */
344
356
  export interface IMonitorEventSummary {
357
+ /** --- 事件目录相对路径 --- */
345
358
  'path': string;
359
+ /** --- 触发事件的进程 PID --- */
346
360
  'pid': number;
361
+ /** --- 事件确认时间戳,单位 ms --- */
347
362
  'time': number;
363
+ /** --- 事件来源:主线程监控或看门狗线程 --- */
348
364
  'source': 'main' | 'watchdog';
365
+ /** --- 事件当前状态或结束原因 --- */
349
366
  'status': string;
367
+ /** --- 触发告警的指标原因列表 --- */
350
368
  'reasons': string[];
351
369
  }
352
370
  /** --- 监控文件元数据;复杂格式或超过 1 MiB 的文件不提供预览 --- */
353
371
  export interface IMonitorFile {
372
+ /** --- 文件相对路径 --- */
354
373
  'path': string;
374
+ /** --- 文件名 --- */
355
375
  'name': string;
376
+ /** --- 文件字节数 --- */
356
377
  'size': number;
357
378
  /** --- 最后修改时间,单位 ms --- */
358
379
  'mtime': number;
380
+ /** --- 支持的在线预览格式,不可预览时为 null --- */
359
381
  'preview': 'json' | 'text' | null;
360
382
  }
361
383
  /** --- 事件原始数据与可读取的文件;data 为 null 时可下载原始记录复盘 --- */
362
384
  export interface IMonitorEvent {
385
+ /** --- 事件摘要 --- */
363
386
  'summary': IMonitorEventSummary;
387
+ /** --- 事件记录 JSON 数据,未成功加载时为 null --- */
364
388
  'data': Record<string, unknown> | null;
389
+ /** --- 事件记录文件的读取状态 --- */
365
390
  'recordStatus': 'loaded' | 'missing' | 'too-large' | 'invalid' | 'unreadable';
391
+ /** --- 关联的可下载诊断文件清单 --- */
366
392
  'files': IMonitorFile[];
367
393
  }
368
394
  /**
package/lib/core.js CHANGED
@@ -3,15 +3,19 @@
3
3
  * Date: 2019-5-3 23:54
4
4
  * Last: 2020-4-11 22:34:58, 2022-10-2 14:13:06, 2022-12-28 20:33:24, 2023-12-15 11:49:02, 2024-7-2 15:23:35, 2025-6-13 19:45:53, 2026-05-20 09:50:00, 2026-8-22
5
5
  */
6
+ // --- 系统 ---
6
7
  import * as cp from 'child_process';
7
8
  import * as http2 from 'http2';
8
9
  import * as stream from 'stream';
9
10
  import * as os from 'os';
10
11
  import * as net from 'net';
11
12
  import * as crypto from 'crypto';
13
+ import * as nodeFs from 'node:fs';
14
+ // --- 第三方 ---
12
15
  import Ajv from 'ajv';
13
16
  import addFormats from 'ajv-formats';
14
17
  import * as valibot from 'valibot';
18
+ // --- 库和定义 ---
15
19
  import * as kebab from '#kebab/index.js';
16
20
  import * as lTime from '#kebab/lib/time.js';
17
21
  import * as lFs from '#kebab/lib/fs.js';
@@ -22,7 +26,6 @@ import * as lNetResponse from '#kebab/lib/net/response.js';
22
26
  import * as lUndiciResponse from '#kebab/lib/undici/response.js';
23
27
  import * as sCtr from '#kebab/sys/ctr.js';
24
28
  import * as rpc from '#kebab/lib/core/rpc.js';
25
- import * as nodeFs from 'node:fs';
26
29
  /** --- Valibot 数据校验工具,业务侧可通过 lCore.v 使用 --- */
27
30
  export { valibot as v };
28
31
  /** --- 全局参数 --- */
@@ -45,19 +48,26 @@ export function setCookie(ctr, name, value, opt = {}) {
45
48
  if (!res) {
46
49
  return;
47
50
  }
48
- /*
49
- const expires = lTime.get(ctr, {
50
- 'data': lTime.stamp() + ttl
51
- }).toUTCString();
52
- */
53
- const maxAge = opt.ttl === undefined ? '' : `; Max-Age=${opt.ttl}`;
54
- const path = `; path=${opt.path ?? '/'}`;
51
+ if (!name || /[;\r\n\u0000-\u001f\u007f=]/.test(name)) {
52
+ return;
53
+ }
54
+ const cleanPath = opt.path ?? '/';
55
+ if (/[;\r\n\u0000-\u001f\u007f]/.test(cleanPath)) {
56
+ return;
57
+ }
58
+ if (opt.domain && /[;\r\n\u0000-\u001f\u007f]/.test(opt.domain)) {
59
+ return;
60
+ }
61
+ const maxAge = (opt.ttl !== undefined && Number.isFinite(opt.ttl)) ? `; Max-Age=${Math.trunc(opt.ttl)}` : '';
62
+ const path = `; path=${cleanPath}`;
55
63
  const domain = opt.domain ? `; domain=${opt.domain}` : '';
56
64
  const secure = opt.ssl ? '; secure' : '';
57
65
  const httpOnly = opt.httponly ? '; HttpOnly' : '';
58
66
  const sameSite = opt.samesite ? '; SameSite=' + opt.samesite : '';
59
- const cookies = res.getHeader('set-cookie') ?? [];
60
- // cookies.push(`${name}=${encodeURIComponent(value)}; expires=${expires}; Max-Age=${ttl}${path}${domain}${secure}${httpOnly}`);
67
+ const existing = res.getHeader('set-cookie');
68
+ const cookies = Array.isArray(existing)
69
+ ? existing.map(String)
70
+ : (existing !== undefined ? [String(existing)] : []);
61
71
  cookies.push(`${name}=${encodeURIComponent(value)}${maxAge}${path}${domain}${secure}${httpOnly}${sameSite}`);
62
72
  res.setHeader('set-cookie', cookies);
63
73
  }
@@ -120,6 +130,7 @@ export function random(length = 8, source = RANDOM_LN, block = '') {
120
130
  }
121
131
  return temp;
122
132
  }
133
+ /** --- 62 进制编码字符表 --- */
123
134
  const CONVERT62_CHAR = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
124
135
  /**
125
136
  * --- 将 10 进制转换为 62 进制 ---
@@ -887,7 +898,12 @@ export async function updateCode(sourcePath, path, hosts, config = true, strict
887
898
  }
888
899
  /** --- 各主机使用独立上传流并行部署,结果保持输入顺序 --- */
889
900
  const results = await Promise.all(hosts.map(async (host) => {
890
- const command = rpc.sign({ 'action': 'code', path, config, strict: strict ? '1' : '0' }, globalConfig.rpcSecret, 'POST', bodyHash);
901
+ const command = rpc.sign({
902
+ 'action': 'code',
903
+ 'path': path,
904
+ 'config': config,
905
+ 'strict': strict ? '1' : '0',
906
+ }, globalConfig.rpcSecret, 'POST', bodyHash);
891
907
  const res = await lUndici.post(`http://${host}:${globalConfig.rpcPort}/${command}`, nodeFs.createReadStream(sourcePath), {
892
908
  'timeout': 180,
893
909
  });
@@ -1014,6 +1030,7 @@ export async function sendVhost(files, hosts) {
1014
1030
  */
1015
1031
  export function log(opt, msg, fend = '') {
1016
1032
  (async () => {
1033
+ // --- 解析请求上下文 ---
1017
1034
  let req;
1018
1035
  let headers;
1019
1036
  let get;
@@ -1041,6 +1058,7 @@ export function log(opt, msg, fend = '') {
1041
1058
  hostname = 'system';
1042
1059
  }
1043
1060
  hostname = hostname.replace(/:/g, '_');
1061
+ // --- 提取客户端 IP 与构建日志目录 ---
1044
1062
  const realIp = req?.socket.remoteAddress ?? '';
1045
1063
  const ipss = req ? ips(headers) : {
1046
1064
  'cf': '',
@@ -1054,6 +1072,7 @@ export function log(opt, msg, fend = '') {
1054
1072
  if (!rtn) {
1055
1073
  return;
1056
1074
  }
1075
+ // --- 按配置的日志格式追加写入 ---
1057
1076
  const format = globalConfig.logFormat ?? 'jsonl';
1058
1077
  if (format === 'jsonl') {
1059
1078
  // --- JSON Lines 格式 ---
@@ -1147,8 +1166,12 @@ async function requestMonitor(action, opt) {
1147
1166
  return false;
1148
1167
  }
1149
1168
  const command = rpc.sign({
1150
- action, 'time': lTime.stamp(), 'path': opt.path, 'offset': opt.offset,
1151
- 'limit': opt.limit, 'preview': opt.preview,
1169
+ 'action': action,
1170
+ 'time': lTime.stamp(),
1171
+ 'path': opt.path,
1172
+ 'offset': opt.offset,
1173
+ 'limit': opt.limit,
1174
+ 'preview': opt.preview,
1152
1175
  }, globalConfig.rpcSecret);
1153
1176
  const res = await lUndici.get(`http://${opt.host ?? '127.0.0.1'}:${globalConfig.rpcPort}/${command}`, {
1154
1177
  'timeout': action === 'monitor-file' ? 300 : 5,
package/lib/cron.js CHANGED
@@ -123,12 +123,5 @@ async function saveCronData(obj) {
123
123
  * --- 获取 Kv 实例(内部使用,用于分布式锁) ---
124
124
  */
125
125
  function get() {
126
- return lKv.get({
127
- 'host': lCore.globalConfig.kv.host,
128
- 'port': lCore.globalConfig.kv.port,
129
- 'index': lCore.globalConfig.kv.index,
130
- 'pre': lCore.globalConfig.kv.pre,
131
- 'user': lCore.globalConfig.kv.user,
132
- 'pwd': lCore.globalConfig.kv.pwd,
133
- });
126
+ return lKv.get({ ...lCore.globalConfig.kv });
134
127
  }
package/lib/crypto.js CHANGED
@@ -113,8 +113,15 @@ export const SM4_CFB = 'sm4-cfb';
113
113
  */
114
114
  export function cipherEncrypt(original, key, iv = '', method = AES_256_ECB, output = 'base64') {
115
115
  try {
116
- if ((typeof key === 'string') && (key.length !== 32)) {
117
- key = hashHmac('md5', key, 'MaiyunSalt');
116
+ if (typeof key === 'string') {
117
+ if (method.startsWith('sm4-')) {
118
+ if (Buffer.byteLength(key) !== 16) {
119
+ key = hashHmac('md5', key, 'MaiyunSalt', 'buffer');
120
+ }
121
+ }
122
+ else if (key.length !== 32) {
123
+ key = hashHmac('md5', key, 'MaiyunSalt');
124
+ }
118
125
  }
119
126
  if (iv) {
120
127
  if (method === AES_256_CTR) {
@@ -195,8 +202,15 @@ export function sm4Encrypt(original, key, iv = '', method = SM4_ECB, output = 'b
195
202
  */
196
203
  export function cipherDecrypt(encrypt, key, iv = '', method = AES_256_ECB, output = 'binary') {
197
204
  try {
198
- if ((typeof key === 'string') && (key.length !== 32)) {
199
- key = hashHmac('md5', key, 'MaiyunSalt');
205
+ if (typeof key === 'string') {
206
+ if (method.startsWith('sm4-')) {
207
+ if (Buffer.byteLength(key) !== 16) {
208
+ key = hashHmac('md5', key, 'MaiyunSalt', 'buffer');
209
+ }
210
+ }
211
+ else if (key.length !== 32) {
212
+ key = hashHmac('md5', key, 'MaiyunSalt');
213
+ }
200
214
  }
201
215
  if (iv) {
202
216
  if (method === AES_256_CTR) {
@@ -267,7 +281,7 @@ export function sm4Decrypt(encrypt, key, iv = '', method = SM4_ECB, output = 'bi
267
281
  return cipherDecrypt(encrypt, key, iv, method, output);
268
282
  }
269
283
  export function hashHmac(algorithm, data, key, format = 'hex') {
270
- const cry = key ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
284
+ const cry = key !== undefined ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
271
285
  cry.update(data);
272
286
  if (format === 'buffer') {
273
287
  return cry.digest();
@@ -278,7 +292,7 @@ export function hashHmac(algorithm, data, key, format = 'hex') {
278
292
  }
279
293
  export function hashHmacFile(algorithm, path, key, encoding = 'hex') {
280
294
  return new Promise(function (resolve) {
281
- const cry = key ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
295
+ const cry = key !== undefined ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
282
296
  const rs = lFs.createReadStream(path);
283
297
  rs.on('data', (chunk) => {
284
298
  cry.update(chunk);
package/lib/db/conn.js CHANGED
@@ -135,6 +135,7 @@ export class Connection {
135
135
  this.refreshLast();
136
136
  }
137
137
  try {
138
+ // --- 收窄 pg.Client 与 mysql2.Connection 的联合类型后再调用 query ---
138
139
  if (this._link instanceof pg.Client) {
139
140
  await this._link.query('SELECT 1');
140
141
  }