@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
@@ -1,13 +1,18 @@
1
1
  /** --- master 读取落盘监控数据;只读取有界事件 JSON,Profile 和堆快照仅提供文件元数据 --- */
2
+ // --- 系统 ---
2
3
  import * as fs from 'fs/promises';
3
4
  import * as path from 'path';
5
+ // --- 库和定义 ---
4
6
  import * as lText from '#kebab/lib/text.js';
5
- /** --- 单个事件记录读取上限,避免损坏或异常大的文件占用 master 内存 --- */
7
+ /** --- 单个事件记录读取上限(4MB),避免损坏或异常大的文件占用 master 内存 --- */
6
8
  const RECORD_LIMIT = 4 * 1024 * 1024;
7
- /** --- 小型 JSON/文本预览上限,Profile 和堆快照不预览 --- */
9
+ /** --- 小型 JSON/文本预览上限(1MB),Profile 和堆快照不预览 --- */
8
10
  export const PREVIEW_LIMIT = 1024 * 1024;
11
+ /** --- 日期目录格式,如 2026/10/05 --- */
9
12
  const DAY_PATTERN = /^\d{4}\/\d{2}\/\d{2}$/;
13
+ /** --- 事件目录名格式,如 123045-pid-1234-1700000000000(-blocked) --- */
10
14
  const EVENT_NAME_PATTERN = /^\d{6}-pid-\d+(?:-\d+)?(?:-blocked)?$/;
15
+ /** --- 允许读取的诊断文件名白名单 --- */
11
16
  const FILE_NAME_PATTERNS = [
12
17
  /^(?:event\.json|blocked-event\.json|(?:report|profile)-[\w.-]+\.json)$/,
13
18
  /^(?:cpu|blocked-cpu)-[\w.-]+\.cpuprofile$/,
@@ -30,10 +35,12 @@ function isFileName(name) {
30
35
  */
31
36
  async function resolveInside(root, relative) {
32
37
  try {
33
- if ((await fs.lstat(root)).isSymbolicLink()) {
38
+ // --- 去除末尾斜杠后再 lstat,避免 POSIX 跟随根目录符号链接 ---
39
+ const normalizedRoot = path.resolve(root);
40
+ if ((await fs.lstat(normalizedRoot)).isSymbolicLink()) {
34
41
  return false;
35
42
  }
36
- const rootReal = await fs.realpath(root);
43
+ const rootReal = await fs.realpath(normalizedRoot);
37
44
  const target = await fs.realpath(path.join(rootReal, relative));
38
45
  const difference = path.relative(rootReal, target);
39
46
  if (difference === '..' || difference.startsWith(`..${path.sep}`) || path.isAbsolute(difference)) {
@@ -66,15 +73,17 @@ async function readRecord(directory) {
66
73
  for (const name of ['event.json', 'blocked-event.json']) {
67
74
  let handle = null;
68
75
  try {
69
- // --- 拒绝事件记录本身的符号链接;读取过程中即使文件增长也不会超过上限 ---
70
- const stat = await fs.lstat(path.join(directory, name));
76
+ // --- 校验文件类型与大小,拒绝符号链接 ---
77
+ const filePath = path.join(directory, name);
78
+ const stat = await fs.lstat(filePath);
71
79
  if (!stat.isFile() || stat.isSymbolicLink()) {
72
80
  return { 'data': null, 'status': 'unreadable' };
73
81
  }
74
82
  if (stat.size > RECORD_LIMIT) {
75
83
  return { 'data': null, 'status': 'too-large' };
76
84
  }
77
- handle = await fs.open(path.join(directory, name), fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
85
+ // --- 读取有界内容并解析 JSON,防止读取过程中文件增长超出上限 ---
86
+ handle = await fs.open(filePath, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
78
87
  const buffer = Buffer.alloc(stat.size + 1);
79
88
  const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
80
89
  if (bytesRead > RECORD_LIMIT) {
@@ -104,11 +113,14 @@ async function readRecord(directory) {
104
113
  * @returns 事件摘要
105
114
  */
106
115
  function summarize(relative, record) {
116
+ // --- 从相对路径解析基础时间与事件来源 ---
107
117
  const [year, month, day, name] = relative.split('/');
108
118
  const parts = name.split('-');
109
- const time = Number(parts[3]) || new Date(`${year}-${month}-${day}T${parts[0].slice(0, 2)}:${parts[0].slice(2, 4)}:${parts[0].slice(4, 6)}`).getTime();
119
+ const parsedTime = new Date(`${year}-${month}-${day}T${parts[0].slice(0, 2)}:${parts[0].slice(2, 4)}:${parts[0].slice(4, 6)}`).getTime();
120
+ const time = Number(parts[3]) || (Number.isFinite(parsedTime) ? parsedTime : 0);
110
121
  const data = record.data;
111
122
  const source = name.endsWith('-blocked') ? 'watchdog' : 'main';
123
+ // --- 从事件记录中提取状态、确认时间与触发原因 ---
112
124
  let status = record.status === 'loaded' ? 'active' : record.status;
113
125
  if (typeof data?.endReason === 'string') {
114
126
  status = data.endReason;
@@ -134,7 +146,9 @@ function summarize(relative, record) {
134
146
  'path': relative,
135
147
  'pid': typeof data?.pid === 'number' ? data.pid : Number(parts[2]),
136
148
  'time': eventTime,
137
- source, status, reasons,
149
+ 'source': source,
150
+ 'status': status,
151
+ 'reasons': reasons,
138
152
  };
139
153
  }
140
154
  /**
@@ -146,10 +160,19 @@ function summarize(relative, record) {
146
160
  * @returns 列表和总数;目录不存在返回空列表,输入或读取错误返回 false
147
161
  */
148
162
  export async function getEvents(root, day, offset = 0, limit = 20) {
149
- if (typeof day !== 'string' || !DAY_PATTERN.test(day) || !Number.isSafeInteger(offset) ||
150
- typeof offset !== 'number' || offset < 0 || !Number.isSafeInteger(limit) || typeof limit !== 'number' || limit < 1 || limit > 100) {
163
+ // --- 校验日期和分页参数 ---
164
+ if (typeof day !== 'string' ||
165
+ !DAY_PATTERN.test(day) ||
166
+ typeof offset !== 'number' ||
167
+ !Number.isSafeInteger(offset) ||
168
+ offset < 0 ||
169
+ typeof limit !== 'number' ||
170
+ !Number.isSafeInteger(limit) ||
171
+ limit < 1 ||
172
+ limit > 100) {
151
173
  return false;
152
174
  }
175
+ // --- 定位日期目录 ---
153
176
  const directory = await resolveInside(root, day);
154
177
  if (directory === false) {
155
178
  return false;
@@ -157,17 +180,24 @@ export async function getEvents(root, day, offset = 0, limit = 20) {
157
180
  if (directory === null) {
158
181
  return { 'list': [], 'total': 0 };
159
182
  }
183
+ // --- 读取事件目录并按时间倒序分页生成摘要 ---
160
184
  try {
161
185
  const entries = await fs.readdir(directory, { 'withFileTypes': true });
162
- const names = entries.filter(entry => entry.isDirectory() && EVENT_NAME_PATTERN.test(entry.name))
163
- .map(entry => entry.name).sort().reverse();
186
+ const names = entries
187
+ .filter(entry => entry.isDirectory() && EVENT_NAME_PATTERN.test(entry.name))
188
+ .map(entry => entry.name)
189
+ .sort()
190
+ .reverse();
164
191
  const list = [];
165
192
  for (const name of names.slice(offset, offset + limit)) {
166
193
  const relative = `${day}/${name}`;
167
194
  const record = await readRecord(path.join(directory, name));
168
195
  list.push(summarize(relative, record));
169
196
  }
170
- return { list, 'total': names.length };
197
+ return {
198
+ 'list': list,
199
+ 'total': names.length,
200
+ };
171
201
  }
172
202
  catch {
173
203
  return false;
@@ -180,14 +210,23 @@ export async function getEvents(root, day, offset = 0, limit = 20) {
180
210
  * @returns 真实路径、文件状态和元数据;不存在返回 null,非法路径或读取错误返回 false
181
211
  */
182
212
  export async function getFile(root, relative) {
183
- if (typeof relative !== 'string' || !isEventPath(path.posix.dirname(relative)) || !isFileName(path.posix.basename(relative))) {
213
+ // --- 校验路径格式与允许的文件名 ---
214
+ if (typeof relative !== 'string' ||
215
+ !isEventPath(path.posix.dirname(relative)) ||
216
+ !isFileName(path.posix.basename(relative))) {
184
217
  return false;
185
218
  }
186
219
  const file = await resolveInside(root, relative);
187
220
  if (file === null || file === false) {
188
221
  return file;
189
222
  }
223
+ // --- 校验目标文件不是符号链接并读取文件状态 ---
190
224
  try {
225
+ const linkPath = path.join(path.resolve(root), relative);
226
+ const lstat = await fs.lstat(linkPath);
227
+ if (!lstat.isFile() || lstat.isSymbolicLink()) {
228
+ return false;
229
+ }
191
230
  const stat = await fs.stat(file);
192
231
  if (!stat.isFile()) {
193
232
  return false;
@@ -201,9 +240,17 @@ export async function getFile(root, relative) {
201
240
  preview = 'text';
202
241
  }
203
242
  }
204
- return { 'path': file, stat, 'info': {
205
- 'path': relative, 'name': path.basename(relative), 'size': stat.size, 'mtime': stat.mtimeMs, preview,
206
- } };
243
+ return {
244
+ 'path': file,
245
+ 'stat': stat,
246
+ 'info': {
247
+ 'path': relative,
248
+ 'name': path.basename(relative),
249
+ 'size': stat.size,
250
+ 'mtime': stat.mtimeMs,
251
+ 'preview': preview,
252
+ },
253
+ };
207
254
  }
208
255
  catch (error) {
209
256
  return error.code === 'ENOENT' ? null : false;
@@ -216,6 +263,7 @@ export async function getFile(root, relative) {
216
263
  * @returns 事件详情;不存在返回 null,非法路径或读取错误返回 false
217
264
  */
218
265
  export async function getEvent(root, relative) {
266
+ // --- 校验并定位事件目录 ---
219
267
  if (!isEventPath(relative)) {
220
268
  return false;
221
269
  }
@@ -224,12 +272,14 @@ export async function getEvent(root, relative) {
224
272
  return directory;
225
273
  }
226
274
  try {
275
+ // --- 读取事件记录与当前目录下的诊断文件 ---
227
276
  const record = await readRecord(directory);
228
277
  const entries = await fs.readdir(directory, { 'withFileTypes': true });
229
- const candidates = new Set(entries.filter(entry => entry.isFile() && isFileName(entry.name))
278
+ const candidates = new Set(entries
279
+ .filter(entry => entry.isFile() && isFileName(entry.name))
230
280
  .map(entry => `${relative}/${entry.name}`));
281
+ // --- 主线程事件会引用另一个阻塞目录,只将监控根目录内的文件加入下载清单 ---
231
282
  if (Array.isArray(record.data?.diagnostics)) {
232
- // --- 主线程事件会引用另一个阻塞目录;只将监控根目录内的文件加入下载清单 ---
233
283
  for (const diagnostic of record.data.diagnostics) {
234
284
  if (diagnostic === null || typeof diagnostic !== 'object') {
235
285
  continue;
@@ -245,6 +295,7 @@ export async function getEvent(root, relative) {
245
295
  }
246
296
  }
247
297
  }
298
+ // --- 逐项校验并收集文件元数据 ---
248
299
  const files = [];
249
300
  for (const candidate of [...candidates].sort()) {
250
301
  const file = await getFile(root, candidate);
@@ -252,7 +303,12 @@ export async function getEvent(root, relative) {
252
303
  files.push(file.info);
253
304
  }
254
305
  }
255
- return { 'summary': summarize(relative, record), 'data': record.data, 'recordStatus': record.status, files };
306
+ return {
307
+ 'summary': summarize(relative, record),
308
+ 'data': record.data,
309
+ 'recordStatus': record.status,
310
+ 'files': files,
311
+ };
256
312
  }
257
313
  catch {
258
314
  return false;
@@ -1 +1,8 @@
1
+ /**
2
+ * Project: Kebab, User: JianSuoQiYue
3
+ * Date: 2026-02-08
4
+ * Last: 2026-11-24
5
+ * --- 独立线程监测主线程心跳,阻塞期间保存资源采样、请求、调用栈和 CPU Profile ---
6
+ * --- 只依赖 Node.js;主线程无法运行时,日志与 Inspector 超时仍可独立处理 ---
7
+ */
1
8
  export {};