@maiyunnet/kebab 9.29.6 → 10.1.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/doc/kebab-rag.md +1687 -1061
- package/index.d.ts +14 -2
- 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 +4 -2
- package/lib/db/conn.js +1 -0
- package/lib/db/pool.d.ts +17 -4
- package/lib/db/pool.js +41 -23
- package/lib/db/tran.d.ts +6 -4
- package/lib/db.d.ts +10 -5
- package/lib/db.js +36 -6
- package/lib/fs.js +78 -31
- package/lib/kv.d.ts +182 -79
- package/lib/kv.js +518 -214
- 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 +10 -5
- package/lib/text.js +22 -12
- package/lib/turnstile.d.ts +6 -0
- package/lib/turnstile.js +16 -9
- package/lib/undici/response.d.ts +1 -1
- package/lib/undici.d.ts +7 -1
- package/lib/undici.js +72 -36
- 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 -210
- package/sys/ctr.d.ts +16 -16
- package/sys/ctr.js +147 -61
- package/sys/master.js +111 -80
- package/sys/mod.d.ts +24 -2
- package/sys/mod.js +56 -26
- 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.d.ts +1 -1
- package/www/example/ctr/test.js +34 -15
- package/www/example/mod/test.js +0 -1
package/sys/mod.js
CHANGED
|
@@ -82,6 +82,8 @@ export default class Mod {
|
|
|
82
82
|
_ctr = undefined;
|
|
83
83
|
/** --- 主表筛选前缀,优先 alias,其次表名 --- */
|
|
84
84
|
_fieldPrefix = '';
|
|
85
|
+
/** --- 当前查询是否优先使用只读从库 --- */
|
|
86
|
+
_read = false;
|
|
85
87
|
/**
|
|
86
88
|
* --- 构造函数 ---
|
|
87
89
|
* @param opt 选项
|
|
@@ -91,6 +93,7 @@ export default class Mod {
|
|
|
91
93
|
this._ctr = opt.ctr;
|
|
92
94
|
/** --- 导入数据库连接 --- */
|
|
93
95
|
this._db = opt.db;
|
|
96
|
+
this._read = Boolean(opt.read);
|
|
94
97
|
/** --- 新建 sql 对象 --- */
|
|
95
98
|
this._sql = lSql.get({
|
|
96
99
|
'service': this._db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
@@ -164,6 +167,9 @@ export default class Mod {
|
|
|
164
167
|
return r.packet.affected ? true : null;
|
|
165
168
|
}
|
|
166
169
|
// --- 可能是多行 ---
|
|
170
|
+
if (!vs.length) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
167
173
|
if (vs.some(item => !Array.isArray(item))) {
|
|
168
174
|
vs = [vs];
|
|
169
175
|
}
|
|
@@ -359,6 +365,7 @@ export default class Mod {
|
|
|
359
365
|
if (groups.size === 0) {
|
|
360
366
|
return true;
|
|
361
367
|
}
|
|
368
|
+
// --- 按组计算分批大小并逐批执行 UPDATE ---
|
|
362
369
|
const tableName = this._$table + (opt.index ? ('_' + opt.index) : '');
|
|
363
370
|
for (const [, groupItems] of groups) {
|
|
364
371
|
const cols = Object.keys(groupItems[0]).filter(k => k !== key).sort();
|
|
@@ -368,12 +375,11 @@ export default class Mod {
|
|
|
368
375
|
const maxBatchSize = Math.floor(65000 / allCols.length);
|
|
369
376
|
// --- 控制单条 SQL 的解析、临时数据和持锁规模;PostgreSQL 不应默认吃满协议参数上限 ---
|
|
370
377
|
const defaultBatchSize = service === lDb.ESERVICE.MYSQL ? 200 : 1_000;
|
|
371
|
-
const
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const batchSize = Math.max(1, Math.min(normalizedBatchSize, maxBatchSize));
|
|
378
|
+
const customBatch = opt.batchSize;
|
|
379
|
+
const wantedBatch = (customBatch !== undefined && Number.isSafeInteger(customBatch) && customBatch > 0)
|
|
380
|
+
? customBatch
|
|
381
|
+
: defaultBatchSize;
|
|
382
|
+
const batchSize = Math.max(1, Math.min(wantedBatch, maxBatchSize));
|
|
377
383
|
for (let i = 0; i < groupItems.length; i += batchSize) {
|
|
378
384
|
const batch = groupItems.slice(i, i + batchSize);
|
|
379
385
|
const sq = lSql.get({
|
|
@@ -408,6 +414,7 @@ export default class Mod {
|
|
|
408
414
|
'alias': opt.alias,
|
|
409
415
|
'contain': opt.contain,
|
|
410
416
|
'hint': opt.hint,
|
|
417
|
+
'read': opt.read,
|
|
411
418
|
});
|
|
412
419
|
}
|
|
413
420
|
/**
|
|
@@ -426,6 +433,7 @@ export default class Mod {
|
|
|
426
433
|
'contain': opt.contain,
|
|
427
434
|
'alias': opt.alias,
|
|
428
435
|
'hint': opt.hint,
|
|
436
|
+
'read': opt.read,
|
|
429
437
|
});
|
|
430
438
|
}
|
|
431
439
|
/**
|
|
@@ -461,6 +469,7 @@ export default class Mod {
|
|
|
461
469
|
}],
|
|
462
470
|
'index': opt.index,
|
|
463
471
|
'hint': opt.hint,
|
|
472
|
+
'read': opt.read,
|
|
464
473
|
}).first(opt.lock);
|
|
465
474
|
}
|
|
466
475
|
static async one(db, s, opt = {}) {
|
|
@@ -473,6 +482,7 @@ export default class Mod {
|
|
|
473
482
|
'pre': opt.pre,
|
|
474
483
|
'where': s,
|
|
475
484
|
'hint': opt.hint,
|
|
485
|
+
'read': opt.read,
|
|
476
486
|
});
|
|
477
487
|
if (opt.by) {
|
|
478
488
|
o.by(opt.by[0], opt.by[1]);
|
|
@@ -489,6 +499,7 @@ export default class Mod {
|
|
|
489
499
|
'where': s,
|
|
490
500
|
'index': item,
|
|
491
501
|
'hint': opt.hint,
|
|
502
|
+
'read': opt.read,
|
|
492
503
|
});
|
|
493
504
|
if (opt.by) {
|
|
494
505
|
row.by(opt.by[0], opt.by[1]);
|
|
@@ -505,9 +516,7 @@ export default class Mod {
|
|
|
505
516
|
return null;
|
|
506
517
|
}
|
|
507
518
|
static async oneArray(db, s, opt = {}) {
|
|
508
|
-
|
|
509
|
-
queryOptions.array = true;
|
|
510
|
-
return this.one(db, s, queryOptions);
|
|
519
|
+
return this.one(db, s, { ...opt, 'array': true });
|
|
511
520
|
}
|
|
512
521
|
/**
|
|
513
522
|
* --- 根据 where 条件获取主键值列表 ---
|
|
@@ -522,7 +531,7 @@ export default class Mod {
|
|
|
522
531
|
'pre': opt.pre ?? this._$pre,
|
|
523
532
|
});
|
|
524
533
|
sq.select(this._$primary, this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
|
|
525
|
-
const r = await db.query(sq.getSql(), sq.getData());
|
|
534
|
+
const r = await db.query(sq.getSql(), sq.getData(), { 'read': opt.read });
|
|
526
535
|
if (r.rows === null) {
|
|
527
536
|
lCore.log(opt.ctr ?? {}, '[MOD][PRIMARYS] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + sq.format()).replaceAll('"', '""'), '-error');
|
|
528
537
|
return false;
|
|
@@ -701,7 +710,10 @@ export default class Mod {
|
|
|
701
710
|
if (r.packet?.affected) {
|
|
702
711
|
this._updates = {};
|
|
703
712
|
this._jsonUpdates = {};
|
|
704
|
-
|
|
713
|
+
// --- 非自增主键(如 UUID)时 insertId 为 0,保留调用方已设置的主键值 ---
|
|
714
|
+
if (r.packet.insert || this._data[cstr._$primary] === undefined || this._data[cstr._$primary] === null) {
|
|
715
|
+
this._data[cstr._$primary] = r.packet.insert;
|
|
716
|
+
}
|
|
705
717
|
this[cstr._$primary] = this._data[cstr._$primary];
|
|
706
718
|
return true;
|
|
707
719
|
}
|
|
@@ -717,6 +729,7 @@ export default class Mod {
|
|
|
717
729
|
* @returns 是否成功;至少需要一个可更新字段,失败保留待写数据
|
|
718
730
|
*/
|
|
719
731
|
async upsert(conflict) {
|
|
732
|
+
// --- 校验冲突键并分离 WHERE 条件与待更新字段 ---
|
|
720
733
|
const cstr = this.constructor;
|
|
721
734
|
const conflicts = typeof conflict === 'string' ? [conflict] : conflict;
|
|
722
735
|
const inserts = this._getUpdates();
|
|
@@ -738,6 +751,7 @@ export default class Mod {
|
|
|
738
751
|
if (!Object.keys(updates).length) {
|
|
739
752
|
return false;
|
|
740
753
|
}
|
|
754
|
+
// --- 按数据库类型执行插入/锁定(MySQL 分步,PGSQL 原子 UPSERT) ---
|
|
741
755
|
const mysql = this._db.getService() === lDb.ESERVICE.MYSQL;
|
|
742
756
|
const table = cstr._$table + (this._index ? `_${this._index[0]}` : '');
|
|
743
757
|
const identity = [...new Set([cstr._$primary, cstr._$key, ...cstr._$partition].filter(field => field))];
|
|
@@ -756,6 +770,7 @@ export default class Mod {
|
|
|
756
770
|
this._sql.insert(table).values(inserts).upsert(updates, conflicts);
|
|
757
771
|
this._sql.append(` RETURNING ${identity.map(field => this._sql.field(field)).join(', ')}`);
|
|
758
772
|
}
|
|
773
|
+
// --- 读取目标身份并在 MySQL 下补做更新 ---
|
|
759
774
|
const result = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
760
775
|
if (result.error) {
|
|
761
776
|
lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${result.error.errno}`, '-error');
|
|
@@ -777,6 +792,7 @@ export default class Mod {
|
|
|
777
792
|
return false;
|
|
778
793
|
}
|
|
779
794
|
}
|
|
795
|
+
// --- 回填主键/自动键并清空待写脏字段 ---
|
|
780
796
|
for (const field of identity) {
|
|
781
797
|
this._setProperty(field, row[field]);
|
|
782
798
|
}
|
|
@@ -875,12 +891,20 @@ export default class Mod {
|
|
|
875
891
|
return false;
|
|
876
892
|
}
|
|
877
893
|
}
|
|
894
|
+
/**
|
|
895
|
+
* --- 设置当前查询是否优先使用只读从库(加锁查询或事务内仍走主库) ---
|
|
896
|
+
* @param enable 是否使用只读从库,默认 true
|
|
897
|
+
*/
|
|
898
|
+
read(enable = true) {
|
|
899
|
+
this._read = enable;
|
|
900
|
+
return this;
|
|
901
|
+
}
|
|
878
902
|
async first(lock = false, array = false) {
|
|
879
903
|
this._sql.limit(1);
|
|
880
904
|
if (lock) {
|
|
881
905
|
this._sql.lock();
|
|
882
906
|
}
|
|
883
|
-
const r = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
907
|
+
const r = await this._db.query(this._sql.getSql(), this._sql.getData(), { 'read': !lock && this._read });
|
|
884
908
|
if (r.rows === null) {
|
|
885
909
|
lCore.log(this._ctr ?? {}, '[MOD][first] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
886
910
|
return false;
|
|
@@ -896,6 +920,8 @@ export default class Mod {
|
|
|
896
920
|
this._data[k] = v;
|
|
897
921
|
this[k] = v;
|
|
898
922
|
}
|
|
923
|
+
// --- 返回实体后清除只读查询标记,避免后续实例操作沿用 ---
|
|
924
|
+
this._read = false;
|
|
899
925
|
return this;
|
|
900
926
|
}
|
|
901
927
|
async firstArray(lock = false) {
|
|
@@ -979,7 +1005,7 @@ export default class Mod {
|
|
|
979
1005
|
sql = sql.replace(/(FROM [a-zA-Z0-9`"_.]+?_)[\w]+/, '$1' + this._index[i]);
|
|
980
1006
|
}
|
|
981
1007
|
const tsql = this._formatTotal(sql);
|
|
982
|
-
const tr = await this._db.query(tsql, this._sql.getData());
|
|
1008
|
+
const tr = await this._db.query(tsql, this._sql.getData(), { 'read': this._read });
|
|
983
1009
|
if (tr.rows === null) {
|
|
984
1010
|
return false;
|
|
985
1011
|
}
|
|
@@ -1001,9 +1027,10 @@ export default class Mod {
|
|
|
1001
1027
|
offset += count;
|
|
1002
1028
|
continue;
|
|
1003
1029
|
}
|
|
1030
|
+
offset = -1;
|
|
1004
1031
|
}
|
|
1005
1032
|
const lsql = sql.replace(/ LIMIT [0-9 ,]+(OFFSET [0-9]+)?/g, ` LIMIT ${remain} OFFSET ${cz}`);
|
|
1006
|
-
const r = await this._db.query(lsql, this._sql.getData());
|
|
1033
|
+
const r = await this._db.query(lsql, this._sql.getData(), { 'read': this._read });
|
|
1007
1034
|
if (r.rows === null) {
|
|
1008
1035
|
lCore.log(this._ctr ?? {}, '[MOD][all] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
1009
1036
|
return false;
|
|
@@ -1040,7 +1067,7 @@ export default class Mod {
|
|
|
1040
1067
|
// --- 单表 ---
|
|
1041
1068
|
/** --- 要追加的 values --- */
|
|
1042
1069
|
const contain = this._contain ? lCore.clone(this._contain.list) : null;
|
|
1043
|
-
const r = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
1070
|
+
const r = await this._db.query(this._sql.getSql(), this._sql.getData(), { 'read': this._read });
|
|
1044
1071
|
if (r.rows === null) {
|
|
1045
1072
|
lCore.log(this._ctr ?? {}, '[MOD][all] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
1046
1073
|
return false;
|
|
@@ -1062,7 +1089,7 @@ export default class Mod {
|
|
|
1062
1089
|
[this._getContainWhereKey()]: contain,
|
|
1063
1090
|
}
|
|
1064
1091
|
});
|
|
1065
|
-
cr = await this._db.query(csql.getSql(), csql.getData());
|
|
1092
|
+
cr = await this._db.query(csql.getSql(), csql.getData(), { 'read': this._read });
|
|
1066
1093
|
if (cr.rows === null) {
|
|
1067
1094
|
lCore.log(this._ctr ?? {}, '[MOD][all] ' + (lText.stringifyJson(cr.error?.message ?? '').slice(1, -1) + ' - ' + csql.format()).replaceAll('"', '""'), '-error');
|
|
1068
1095
|
}
|
|
@@ -1139,7 +1166,7 @@ export default class Mod {
|
|
|
1139
1166
|
sql = sql.replace(/(FROM [a-zA-Z0-9`"_.]+?_)[\w]+/, '$1' + this._index[i]);
|
|
1140
1167
|
}
|
|
1141
1168
|
const tsql = this._formatTotal(sql);
|
|
1142
|
-
const tr = await this._db.query(tsql, this._sql.getData());
|
|
1169
|
+
const tr = await this._db.query(tsql, this._sql.getData(), { 'read': this._read });
|
|
1143
1170
|
if (tr.rows === null) {
|
|
1144
1171
|
return false;
|
|
1145
1172
|
}
|
|
@@ -1165,7 +1192,7 @@ export default class Mod {
|
|
|
1165
1192
|
offset = -1;
|
|
1166
1193
|
}
|
|
1167
1194
|
const lsql = sql.replace(/ LIMIT [0-9 ,]+(OFFSET [0-9]+)?/g, ` LIMIT ${remain} OFFSET ${cz}`);
|
|
1168
|
-
const r = await this._db.query(lsql, this._sql.getData());
|
|
1195
|
+
const r = await this._db.query(lsql, this._sql.getData(), { 'read': this._read });
|
|
1169
1196
|
if (r.rows === null) {
|
|
1170
1197
|
lCore.log(this._ctr ?? {}, '[MOD][allArray] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
1171
1198
|
return false;
|
|
@@ -1187,7 +1214,7 @@ export default class Mod {
|
|
|
1187
1214
|
}
|
|
1188
1215
|
// --- 单表 ---
|
|
1189
1216
|
const contain = this._contain ? lCore.clone(this._contain.list) : null;
|
|
1190
|
-
const r = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
1217
|
+
const r = await this._db.query(this._sql.getSql(), this._sql.getData(), { 'read': this._read });
|
|
1191
1218
|
if (r.rows === null) {
|
|
1192
1219
|
lCore.log(this._ctr ?? {}, '[MOD][allArray] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
1193
1220
|
return false;
|
|
@@ -1209,7 +1236,7 @@ export default class Mod {
|
|
|
1209
1236
|
[this._getContainWhereKey()]: contain,
|
|
1210
1237
|
}
|
|
1211
1238
|
});
|
|
1212
|
-
cr = await this._db.query(csql.getSql(), csql.getData());
|
|
1239
|
+
cr = await this._db.query(csql.getSql(), csql.getData(), { 'read': this._read });
|
|
1213
1240
|
if (cr.rows === null) {
|
|
1214
1241
|
lCore.log(this._ctr ?? {}, '[MOD][allArray] ' + (lText.stringifyJson(cr.error?.message ?? '').slice(1, -1) + ' - ' + csql.format()).replaceAll('"', '""'), '-error');
|
|
1215
1242
|
}
|
|
@@ -1240,7 +1267,7 @@ export default class Mod {
|
|
|
1240
1267
|
* @param all 是否获取完全的情况,默认不获取,只返回扫描情况
|
|
1241
1268
|
*/
|
|
1242
1269
|
async explain(all = false) {
|
|
1243
|
-
const r = await this._db.query('EXPLAIN ' + this._sql.getSql(), this._sql.getData());
|
|
1270
|
+
const r = await this._db.query('EXPLAIN ' + this._sql.getSql(), this._sql.getData(), { 'read': this._read });
|
|
1244
1271
|
if (r.rows === null) {
|
|
1245
1272
|
lCore.log(this._ctr ?? {}, '[MOD][explain] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
1246
1273
|
return false;
|
|
@@ -1297,7 +1324,7 @@ export default class Mod {
|
|
|
1297
1324
|
return count;
|
|
1298
1325
|
}
|
|
1299
1326
|
const sql = this._formatTotal(this._sql.getSql(), f);
|
|
1300
|
-
const r = await this._db.query(sql, this._sql.getData());
|
|
1327
|
+
const r = await this._db.query(sql, this._sql.getData(), { 'read': this._read });
|
|
1301
1328
|
if (r.rows === null) {
|
|
1302
1329
|
lCore.log(this._ctr ?? {}, '[MOD][total] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
1303
1330
|
return 0;
|
|
@@ -1318,7 +1345,7 @@ export default class Mod {
|
|
|
1318
1345
|
return false;
|
|
1319
1346
|
}
|
|
1320
1347
|
const sql = this._sql.copy(undefined, { 'order': false, 'limit': false });
|
|
1321
|
-
const result = await this._db.query('EXPLAIN (FORMAT JSON) ' + sql.getSql(), sql.getData());
|
|
1348
|
+
const result = await this._db.query('EXPLAIN (FORMAT JSON) ' + sql.getSql(), sql.getData(), { 'read': this._read });
|
|
1322
1349
|
if (result.rows === null) {
|
|
1323
1350
|
lCore.log(this._ctr ?? {}, '[MOD][totalEstimate] Query failed', '-error');
|
|
1324
1351
|
return false;
|
|
@@ -1352,6 +1379,7 @@ export default class Mod {
|
|
|
1352
1379
|
* @returns 列表、首末行游标和扫描方向上的 hasMore,数据库错误返回 false
|
|
1353
1380
|
*/
|
|
1354
1381
|
async allCursor(count, opt) {
|
|
1382
|
+
// --- 校验分页与游标参数 ---
|
|
1355
1383
|
if (!Number.isSafeInteger(count) || count < 1 || count >= Number.MAX_SAFE_INTEGER ||
|
|
1356
1384
|
this._index !== null || this._contain || (opt.before && !opt.cursor) ||
|
|
1357
1385
|
(opt.order !== undefined && !['ASC', 'DESC'].includes(opt.order))) {
|
|
@@ -1362,15 +1390,17 @@ export default class Mod {
|
|
|
1362
1390
|
if (keys.length !== fields.length || new Set(keys).size !== keys.length) {
|
|
1363
1391
|
throw new Error('Cursor result keys must match ordering fields');
|
|
1364
1392
|
}
|
|
1393
|
+
// --- 构造游标查询(多取 1 条判断 hasMore) ---
|
|
1365
1394
|
const order = opt.order ?? 'DESC';
|
|
1366
1395
|
const direction = opt.before ? (order === 'DESC' ? 'ASC' : 'DESC') : order;
|
|
1367
1396
|
const sql = this._sql.copy(undefined, { 'order': false, 'limit': false });
|
|
1368
1397
|
sql.seek(fields, opt.cursor, direction).limit(count + 1);
|
|
1369
|
-
const result = await this._db.query(sql.getSql(), sql.getData());
|
|
1398
|
+
const result = await this._db.query(sql.getSql(), sql.getData(), { 'read': this._read });
|
|
1370
1399
|
if (result.rows === null) {
|
|
1371
1400
|
lCore.log(this._ctr ?? {}, '[MOD][allCursor] Query failed', '-error');
|
|
1372
1401
|
return false;
|
|
1373
1402
|
}
|
|
1403
|
+
// --- 裁剪结果并提取首末行游标 ---
|
|
1374
1404
|
const hasMore = result.rows.length > count;
|
|
1375
1405
|
const list = result.rows.slice(0, count);
|
|
1376
1406
|
if (opt.before) {
|
|
@@ -1399,7 +1429,7 @@ export default class Mod {
|
|
|
1399
1429
|
.replace(/SELECT .+? FROM/is, this._db.getService() === lDb.ESERVICE.MYSQL ?
|
|
1400
1430
|
'SELECT COUNT(0) AS `count` FROM' :
|
|
1401
1431
|
'SELECT COUNT(0) AS "count" FROM');
|
|
1402
|
-
const r = await this._db.query(sql, this._sql.getData());
|
|
1432
|
+
const r = await this._db.query(sql, this._sql.getData(), { 'read': this._read });
|
|
1403
1433
|
if (r.rows === null) {
|
|
1404
1434
|
lCore.log(this._ctr ?? {}, '[MOD][count] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
1405
1435
|
return 0;
|
|
@@ -1625,7 +1655,7 @@ export default class Mod {
|
|
|
1625
1655
|
return updates;
|
|
1626
1656
|
}
|
|
1627
1657
|
/**
|
|
1628
|
-
* --- 当前是否设置了未保存
|
|
1658
|
+
* --- 当前是否设置了未保存 ---
|
|
1629
1659
|
*/
|
|
1630
1660
|
unsaved() {
|
|
1631
1661
|
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;
|