@maiyunnet/kebab 9.29.5 → 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.
- package/abox/proxy.js +24 -2
- package/abox/response.js +36 -17
- package/abox/session-login.js +1 -13
- package/abox/text.d.ts +2 -0
- package/abox/text.js +51 -0
- package/doc/kebab-rag.md +2700 -1270
- package/index.d.ts +12 -1
- package/index.js +1 -1
- package/lib/ai.d.ts +4 -0
- package/lib/ai.js +32 -16
- package/lib/cookie.d.ts +2 -0
- package/lib/cookie.js +47 -45
- package/lib/core.d.ts +26 -0
- package/lib/core.js +36 -13
- package/lib/cron.js +1 -8
- package/lib/crypto.js +20 -6
- package/lib/db/conn.d.ts +10 -1
- package/lib/db/conn.js +1 -5
- package/lib/db/pool.d.ts +10 -2
- package/lib/db/pool.js +0 -6
- package/lib/db/tran.d.ts +10 -1
- package/lib/db/tran.js +0 -5
- package/lib/db.d.ts +3 -3
- package/lib/fs.js +78 -31
- package/lib/kv.d.ts +216 -88
- package/lib/kv.js +520 -230
- package/lib/s3.d.ts +2 -0
- package/lib/s3.js +14 -27
- package/lib/session.d.ts +7 -0
- package/lib/session.js +23 -2
- package/lib/socket.d.ts +4 -1
- package/lib/socket.js +1 -0
- package/lib/sql.d.ts +0 -5
- package/lib/sql.js +48 -12
- package/lib/text.d.ts +32 -8
- package/lib/text.js +30 -17
- package/lib/turnstile.d.ts +6 -0
- package/lib/turnstile.js +16 -9
- package/lib/undici/response.d.ts +13 -3
- package/lib/undici/response.js +0 -1
- package/lib/undici.d.ts +33 -1
- package/lib/undici.js +72 -56
- package/lib/vector.d.ts +33 -19
- package/lib/vector.js +0 -1
- package/lib/ws.d.ts +1 -1
- package/lib/ws.js +85 -128
- package/lib/zip.js +46 -45
- package/package.json +2 -2
- package/sys/child.js +178 -146
- package/sys/cmd.js +196 -197
- package/sys/ctr.d.ts +14 -14
- package/sys/ctr.js +145 -59
- package/sys/master.js +111 -80
- package/sys/mod.d.ts +112 -51
- package/sys/mod.js +23 -51
- package/sys/monitor/files.d.ts +2 -1
- package/sys/monitor/files.js +77 -21
- package/sys/monitor/watchdog.d.ts +7 -0
- package/sys/monitor/watchdog.js +311 -203
- package/sys/monitor.d.ts +59 -54
- package/sys/monitor.js +742 -681
- package/sys/route.d.ts +4 -4
- package/sys/route.js +93 -52
- package/www/example/ctr/test.js +8 -8
package/sys/mod.js
CHANGED
|
@@ -22,7 +22,6 @@ export class Rows {
|
|
|
22
22
|
item(index) {
|
|
23
23
|
return this._items[index];
|
|
24
24
|
}
|
|
25
|
-
/** --- 转换为数组对象,获取的是新创建的数组 --- */
|
|
26
25
|
toArray() {
|
|
27
26
|
return this._items.map(i => i.toArray());
|
|
28
27
|
}
|
|
@@ -165,6 +164,9 @@ export default class Mod {
|
|
|
165
164
|
return r.packet.affected ? true : null;
|
|
166
165
|
}
|
|
167
166
|
// --- 可能是多行 ---
|
|
167
|
+
if (!vs.length) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
168
170
|
if (vs.some(item => !Array.isArray(item))) {
|
|
169
171
|
vs = [vs];
|
|
170
172
|
}
|
|
@@ -360,6 +362,7 @@ export default class Mod {
|
|
|
360
362
|
if (groups.size === 0) {
|
|
361
363
|
return true;
|
|
362
364
|
}
|
|
365
|
+
// --- 按组计算分批大小并逐批执行 UPDATE ---
|
|
363
366
|
const tableName = this._$table + (opt.index ? ('_' + opt.index) : '');
|
|
364
367
|
for (const [, groupItems] of groups) {
|
|
365
368
|
const cols = Object.keys(groupItems[0]).filter(k => k !== key).sort();
|
|
@@ -369,12 +372,11 @@ export default class Mod {
|
|
|
369
372
|
const maxBatchSize = Math.floor(65000 / allCols.length);
|
|
370
373
|
// --- 控制单条 SQL 的解析、临时数据和持锁规模;PostgreSQL 不应默认吃满协议参数上限 ---
|
|
371
374
|
const defaultBatchSize = service === lDb.ESERVICE.MYSQL ? 200 : 1_000;
|
|
372
|
-
const
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
const batchSize = Math.max(1, Math.min(normalizedBatchSize, maxBatchSize));
|
|
375
|
+
const customBatch = opt.batchSize;
|
|
376
|
+
const wantedBatch = (customBatch !== undefined && Number.isSafeInteger(customBatch) && customBatch > 0)
|
|
377
|
+
? customBatch
|
|
378
|
+
: defaultBatchSize;
|
|
379
|
+
const batchSize = Math.max(1, Math.min(wantedBatch, maxBatchSize));
|
|
378
380
|
for (let i = 0; i < groupItems.length; i += batchSize) {
|
|
379
381
|
const batch = groupItems.slice(i, i + batchSize);
|
|
380
382
|
const sq = lSql.get({
|
|
@@ -464,12 +466,6 @@ export default class Mod {
|
|
|
464
466
|
'hint': opt.hint,
|
|
465
467
|
}).first(opt.lock);
|
|
466
468
|
}
|
|
467
|
-
/**
|
|
468
|
-
* --- 通过 where 条件筛选单条数据 ---
|
|
469
|
-
* @param db 数据库对象
|
|
470
|
-
* @param s 筛选条件数组或字符串
|
|
471
|
-
* @param opt 选项(opt.pre: MySQL 表前缀/PostgreSQL Schema 名;lock 需确保 where 条件命中索引,否则可能退化为表锁)
|
|
472
|
-
*/
|
|
473
469
|
static async one(db, s, opt = {}) {
|
|
474
470
|
if (!opt.index) {
|
|
475
471
|
// --- 无 index ---
|
|
@@ -511,15 +507,8 @@ export default class Mod {
|
|
|
511
507
|
}
|
|
512
508
|
return null;
|
|
513
509
|
}
|
|
514
|
-
/**
|
|
515
|
-
* --- 通过 where 条件筛选单条数据返回原生对象 ---
|
|
516
|
-
* @param db 数据库对象
|
|
517
|
-
* @param s 筛选条件数组或字符串
|
|
518
|
-
* @param opt 选项(opt.pre: MySQL 表前缀/PostgreSQL Schema 名)
|
|
519
|
-
*/
|
|
520
510
|
static async oneArray(db, s, opt = {}) {
|
|
521
|
-
opt
|
|
522
|
-
return this.one(db, s, opt);
|
|
511
|
+
return this.one(db, s, { ...opt, 'array': true });
|
|
523
512
|
}
|
|
524
513
|
/**
|
|
525
514
|
* --- 根据 where 条件获取主键值列表 ---
|
|
@@ -545,10 +534,6 @@ export default class Mod {
|
|
|
545
534
|
}
|
|
546
535
|
return primarys;
|
|
547
536
|
}
|
|
548
|
-
/**
|
|
549
|
-
* --- 将 key val 组成的数据列表转换为原生对象模式,获取的是新创建的数组 ---
|
|
550
|
-
* @param obj 要转换的 kv 数据列表
|
|
551
|
-
*/
|
|
552
537
|
static toArrayByRecord(obj) {
|
|
553
538
|
const rtn = {};
|
|
554
539
|
for (const key in obj) {
|
|
@@ -622,10 +607,6 @@ export default class Mod {
|
|
|
622
607
|
this._setProperty(n, v);
|
|
623
608
|
}
|
|
624
609
|
}
|
|
625
|
-
/**
|
|
626
|
-
* --- 获取一个字段值
|
|
627
|
-
* @param n 字段名
|
|
628
|
-
*/
|
|
629
610
|
get(n) {
|
|
630
611
|
return this._data[n];
|
|
631
612
|
}
|
|
@@ -721,7 +702,10 @@ export default class Mod {
|
|
|
721
702
|
if (r.packet?.affected) {
|
|
722
703
|
this._updates = {};
|
|
723
704
|
this._jsonUpdates = {};
|
|
724
|
-
|
|
705
|
+
// --- 非自增主键(如 UUID)时 insertId 为 0,保留调用方已设置的主键值 ---
|
|
706
|
+
if (r.packet.insert || this._data[cstr._$primary] === undefined || this._data[cstr._$primary] === null) {
|
|
707
|
+
this._data[cstr._$primary] = r.packet.insert;
|
|
708
|
+
}
|
|
725
709
|
this[cstr._$primary] = this._data[cstr._$primary];
|
|
726
710
|
return true;
|
|
727
711
|
}
|
|
@@ -737,6 +721,7 @@ export default class Mod {
|
|
|
737
721
|
* @returns 是否成功;至少需要一个可更新字段,失败保留待写数据
|
|
738
722
|
*/
|
|
739
723
|
async upsert(conflict) {
|
|
724
|
+
// --- 校验冲突键并分离 WHERE 条件与待更新字段 ---
|
|
740
725
|
const cstr = this.constructor;
|
|
741
726
|
const conflicts = typeof conflict === 'string' ? [conflict] : conflict;
|
|
742
727
|
const inserts = this._getUpdates();
|
|
@@ -758,6 +743,7 @@ export default class Mod {
|
|
|
758
743
|
if (!Object.keys(updates).length) {
|
|
759
744
|
return false;
|
|
760
745
|
}
|
|
746
|
+
// --- 按数据库类型执行插入/锁定(MySQL 分步,PGSQL 原子 UPSERT) ---
|
|
761
747
|
const mysql = this._db.getService() === lDb.ESERVICE.MYSQL;
|
|
762
748
|
const table = cstr._$table + (this._index ? `_${this._index[0]}` : '');
|
|
763
749
|
const identity = [...new Set([cstr._$primary, cstr._$key, ...cstr._$partition].filter(field => field))];
|
|
@@ -776,6 +762,7 @@ export default class Mod {
|
|
|
776
762
|
this._sql.insert(table).values(inserts).upsert(updates, conflicts);
|
|
777
763
|
this._sql.append(` RETURNING ${identity.map(field => this._sql.field(field)).join(', ')}`);
|
|
778
764
|
}
|
|
765
|
+
// --- 读取目标身份并在 MySQL 下补做更新 ---
|
|
779
766
|
const result = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
780
767
|
if (result.error) {
|
|
781
768
|
lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${result.error.errno}`, '-error');
|
|
@@ -797,6 +784,7 @@ export default class Mod {
|
|
|
797
784
|
return false;
|
|
798
785
|
}
|
|
799
786
|
}
|
|
787
|
+
// --- 回填主键/自动键并清空待写脏字段 ---
|
|
800
788
|
for (const field of identity) {
|
|
801
789
|
this._setProperty(field, row[field]);
|
|
802
790
|
}
|
|
@@ -895,11 +883,6 @@ export default class Mod {
|
|
|
895
883
|
return false;
|
|
896
884
|
}
|
|
897
885
|
}
|
|
898
|
-
/**
|
|
899
|
-
* --- 获取数据库第一个对象 ---
|
|
900
|
-
* @param lock 是否加锁
|
|
901
|
-
* @param array 是否返回原生对象
|
|
902
|
-
*/
|
|
903
886
|
async first(lock = false, array = false) {
|
|
904
887
|
this._sql.limit(1);
|
|
905
888
|
if (lock) {
|
|
@@ -923,10 +906,6 @@ export default class Mod {
|
|
|
923
906
|
}
|
|
924
907
|
return this;
|
|
925
908
|
}
|
|
926
|
-
/**
|
|
927
|
-
* --- 获取数据库第一个原生对象 ---
|
|
928
|
-
* @param lock 是否加锁
|
|
929
|
-
*/
|
|
930
909
|
async firstArray(lock = false) {
|
|
931
910
|
return this.first(lock, true);
|
|
932
911
|
}
|
|
@@ -989,10 +968,6 @@ export default class Mod {
|
|
|
989
968
|
}
|
|
990
969
|
return this;
|
|
991
970
|
}
|
|
992
|
-
/**
|
|
993
|
-
* --- 获取列表 ---
|
|
994
|
-
* @param key 是否以某个字段为主键
|
|
995
|
-
*/
|
|
996
971
|
async all(key) {
|
|
997
972
|
this._total.length = 0;
|
|
998
973
|
if (this._index && this._index.length > 1) {
|
|
@@ -1034,6 +1009,7 @@ export default class Mod {
|
|
|
1034
1009
|
offset += count;
|
|
1035
1010
|
continue;
|
|
1036
1011
|
}
|
|
1012
|
+
offset = -1;
|
|
1037
1013
|
}
|
|
1038
1014
|
const lsql = sql.replace(/ LIMIT [0-9 ,]+(OFFSET [0-9]+)?/g, ` LIMIT ${remain} OFFSET ${cz}`);
|
|
1039
1015
|
const r = await this._db.query(lsql, this._sql.getData());
|
|
@@ -1153,10 +1129,6 @@ export default class Mod {
|
|
|
1153
1129
|
}
|
|
1154
1130
|
return new Rows(list);
|
|
1155
1131
|
}
|
|
1156
|
-
/**
|
|
1157
|
-
* --- 获取列表(得到的为原生对象或数组,不是模型) ---
|
|
1158
|
-
* @param key 是否以某个字段为主键
|
|
1159
|
-
*/
|
|
1160
1132
|
async allArray(key) {
|
|
1161
1133
|
this._total.length = 0;
|
|
1162
1134
|
if (this._index && this._index.length > 1) {
|
|
@@ -1389,6 +1361,7 @@ export default class Mod {
|
|
|
1389
1361
|
* @returns 列表、首末行游标和扫描方向上的 hasMore,数据库错误返回 false
|
|
1390
1362
|
*/
|
|
1391
1363
|
async allCursor(count, opt) {
|
|
1364
|
+
// --- 校验分页与游标参数 ---
|
|
1392
1365
|
if (!Number.isSafeInteger(count) || count < 1 || count >= Number.MAX_SAFE_INTEGER ||
|
|
1393
1366
|
this._index !== null || this._contain || (opt.before && !opt.cursor) ||
|
|
1394
1367
|
(opt.order !== undefined && !['ASC', 'DESC'].includes(opt.order))) {
|
|
@@ -1399,6 +1372,7 @@ export default class Mod {
|
|
|
1399
1372
|
if (keys.length !== fields.length || new Set(keys).size !== keys.length) {
|
|
1400
1373
|
throw new Error('Cursor result keys must match ordering fields');
|
|
1401
1374
|
}
|
|
1375
|
+
// --- 构造游标查询(多取 1 条判断 hasMore) ---
|
|
1402
1376
|
const order = opt.order ?? 'DESC';
|
|
1403
1377
|
const direction = opt.before ? (order === 'DESC' ? 'ASC' : 'DESC') : order;
|
|
1404
1378
|
const sql = this._sql.copy(undefined, { 'order': false, 'limit': false });
|
|
@@ -1408,6 +1382,7 @@ export default class Mod {
|
|
|
1408
1382
|
lCore.log(this._ctr ?? {}, '[MOD][allCursor] Query failed', '-error');
|
|
1409
1383
|
return false;
|
|
1410
1384
|
}
|
|
1385
|
+
// --- 裁剪结果并提取首末行游标 ---
|
|
1411
1386
|
const hasMore = result.rows.length > count;
|
|
1412
1387
|
const list = result.rows.slice(0, count);
|
|
1413
1388
|
if (opt.before) {
|
|
@@ -1648,9 +1623,6 @@ export default class Mod {
|
|
|
1648
1623
|
format(sql, data) {
|
|
1649
1624
|
return this._sql.format(sql, data);
|
|
1650
1625
|
}
|
|
1651
|
-
/**
|
|
1652
|
-
* --- 获取值对象,获取的是新创建的数组 ---
|
|
1653
|
-
*/
|
|
1654
1626
|
toArray() {
|
|
1655
1627
|
return { ...this._data };
|
|
1656
1628
|
}
|
|
@@ -1665,7 +1637,7 @@ export default class Mod {
|
|
|
1665
1637
|
return updates;
|
|
1666
1638
|
}
|
|
1667
1639
|
/**
|
|
1668
|
-
* --- 当前是否设置了未保存
|
|
1640
|
+
* --- 当前是否设置了未保存 ---
|
|
1669
1641
|
*/
|
|
1670
1642
|
unsaved() {
|
|
1671
1643
|
return Object.keys(this._updates).length ? true : false;
|
package/sys/monitor/files.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
/** --- master 读取落盘监控数据;只读取有界事件 JSON,Profile 和堆快照仅提供文件元数据 --- */
|
|
1
2
|
import type { Stats } from 'fs';
|
|
2
3
|
import type { IMonitorEvent, IMonitorEventSummary, IMonitorFile } from '#kebab/lib/core.js';
|
|
3
|
-
/** --- 小型 JSON
|
|
4
|
+
/** --- 小型 JSON/文本预览上限(1MB),Profile 和堆快照不预览 --- */
|
|
4
5
|
export declare const PREVIEW_LIMIT: number;
|
|
5
6
|
/**
|
|
6
7
|
* --- 获取一天的事件摘要,先分页目录再读取记录 ---
|
package/sys/monitor/files.js
CHANGED
|
@@ -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
|
-
/** ---
|
|
7
|
+
/** --- 单个事件记录读取上限(4MB),避免损坏或异常大的文件占用 master 内存 --- */
|
|
6
8
|
const RECORD_LIMIT = 4 * 1024 * 1024;
|
|
7
|
-
/** --- 小型 JSON
|
|
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
|
-
|
|
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(
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
150
|
-
|
|
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
|
|
163
|
-
.
|
|
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 {
|
|
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
|
-
|
|
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 {
|
|
205
|
-
|
|
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
|
|
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 {
|
|
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;
|