@maiyunnet/kebab 9.26.4 → 9.27.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/response.d.ts +3 -0
- package/abox/response.js +59 -0
- package/abox/rpc.d.ts +14 -0
- package/abox/rpc.js +116 -0
- package/abox/text-path.d.ts +3 -0
- package/abox/text-path.js +94 -0
- package/doc/kebab-rag.md +645 -289
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/core/rpc.d.ts +61 -0
- package/lib/core/rpc.js +51 -0
- package/lib/core.d.ts +3 -4
- package/lib/core.js +36 -50
- package/lib/text.d.ts +6 -3
- package/lib/text.js +26 -61
- package/lib/undici.js +11 -0
- package/package.json +1 -1
- package/sys/child.js +19 -5
- package/sys/cmd.js +5 -4
- package/sys/ctr.js +1 -1
- package/sys/master.js +256 -260
- package/sys/mod.d.ts +16 -7
- package/sys/mod.js +111 -52
- package/sys/monitor/watchdog.js +3 -3
- package/sys/route.js +21 -22
- package/www/example/abox/core-updatecode.d.ts +2 -0
- package/www/example/abox/core-updatecode.js +43 -0
- package/www/example/ctr/test.d.ts +4 -0
- package/www/example/ctr/test.js +29 -4
- package/www/example/mod/test.js +2 -2
package/sys/mod.d.ts
CHANGED
|
@@ -45,8 +45,8 @@ export default class Mod {
|
|
|
45
45
|
protected static _$primary: string;
|
|
46
46
|
/** --- 设置后将由 _keyGenerator 函数生成唯一字段 --- */
|
|
47
47
|
protected static _$key: string;
|
|
48
|
-
/** ---
|
|
49
|
-
protected static _$index: string;
|
|
48
|
+
/** --- 自动 key 冲突目标:MySQL 为索引名;PGSQL 为唯一字段或字段数组,PG 留空使用 _$key,不支持部分或表达式索引 --- */
|
|
49
|
+
protected static _$index: string | string[];
|
|
50
50
|
/** --- 前缀,MySQL 时为表前缀(如 prefix_),PostgreSQL 时为 Schema 名。顺序:选项前缀 -> 本前缀 -> 配置文件前缀 --- */
|
|
51
51
|
protected static _$pre?: string;
|
|
52
52
|
/** --- 要 update 的内容 --- */
|
|
@@ -328,13 +328,21 @@ export default class Mod {
|
|
|
328
328
|
*/
|
|
329
329
|
get(n: string): any;
|
|
330
330
|
/**
|
|
331
|
-
* ---
|
|
332
|
-
* @
|
|
331
|
+
* --- 收集待写字段,保留模型内的原始 JSON 值 ---
|
|
332
|
+
* @returns 独立的 SQL 数据对象
|
|
333
|
+
*/
|
|
334
|
+
private _getUpdates;
|
|
335
|
+
/**
|
|
336
|
+
* --- 创建数据,自动 key 最多尝试 3 次;PGSQL 仅忽略指定字段的唯一冲突 ---
|
|
337
|
+
* @return true-成功,false-错误或重试耗尽,null-其他唯一冲突;PGSQL 的 SQL 错误需要回滚事务
|
|
333
338
|
*/
|
|
334
339
|
create(): Promise<boolean | null>;
|
|
335
340
|
/**
|
|
336
|
-
* ---
|
|
337
|
-
*
|
|
341
|
+
* --- 插入数据,如果业务唯一键已存在则更新,保留已有主键和自动 key ---
|
|
342
|
+
* --- PGSQL 为原子 UPSERT,自动 key 只生成一次,SQL 错误需回滚事务 ---
|
|
343
|
+
* --- MySQL 分步执行;自动 key 有多个唯一索引时须配置 _$index,整笔原子性由调用方事务保证 ---
|
|
344
|
+
* @param conflict 已通过 set 赋非 NULL 值的业务唯一字段,不得包含 _$key 或 _$primary;PGSQL 不支持部分或表达式索引
|
|
345
|
+
* @returns 是否成功;至少需要一个可更新字段,失败保留待写数据
|
|
338
346
|
*/
|
|
339
347
|
upsert(conflict: string | string[]): Promise<boolean>;
|
|
340
348
|
/**
|
|
@@ -344,7 +352,8 @@ export default class Mod {
|
|
|
344
352
|
refresh(lock?: boolean): Promise<boolean | null>;
|
|
345
353
|
/**
|
|
346
354
|
* --- 更新 set 的数据到数据库,有未保存数据时才保存 ---
|
|
347
|
-
* @param where
|
|
355
|
+
* @param where 非空筛选条件,省略时必须有主键
|
|
356
|
+
* @returns 是否成功,失败保留待写数据
|
|
348
357
|
*/
|
|
349
358
|
save(where?: any): Promise<boolean>;
|
|
350
359
|
/**
|
package/sys/mod.js
CHANGED
|
@@ -53,7 +53,7 @@ export default class Mod {
|
|
|
53
53
|
static _$primary = 'id';
|
|
54
54
|
/** --- 设置后将由 _keyGenerator 函数生成唯一字段 --- */
|
|
55
55
|
static _$key = '';
|
|
56
|
-
/** ---
|
|
56
|
+
/** --- 自动 key 冲突目标:MySQL 为索引名;PGSQL 为唯一字段或字段数组,PG 留空使用 _$key,不支持部分或表达式索引 --- */
|
|
57
57
|
static _$index = '';
|
|
58
58
|
/** --- 前缀,MySQL 时为表前缀(如 prefix_),PostgreSQL 时为 Schema 名。顺序:选项前缀 -> 本前缀 -> 配置文件前缀 --- */
|
|
59
59
|
static _$pre;
|
|
@@ -605,17 +605,34 @@ export default class Mod {
|
|
|
605
605
|
return this._data[n];
|
|
606
606
|
}
|
|
607
607
|
/**
|
|
608
|
-
* ---
|
|
609
|
-
* @
|
|
608
|
+
* --- 收集待写字段,保留模型内的原始 JSON 值 ---
|
|
609
|
+
* @returns 独立的 SQL 数据对象
|
|
610
610
|
*/
|
|
611
|
-
|
|
612
|
-
const cstr = this.constructor;
|
|
611
|
+
_getUpdates() {
|
|
613
612
|
const updates = {};
|
|
614
|
-
for (const
|
|
615
|
-
updates[
|
|
613
|
+
for (const field in this._updates) {
|
|
614
|
+
updates[field] = this._jsonUpdates[field] ? lSql.json(this._data[field]) : this._data[field];
|
|
616
615
|
}
|
|
616
|
+
return updates;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* --- 创建数据,自动 key 最多尝试 3 次;PGSQL 仅忽略指定字段的唯一冲突 ---
|
|
620
|
+
* @return true-成功,false-错误或重试耗尽,null-其他唯一冲突;PGSQL 的 SQL 错误需要回滚事务
|
|
621
|
+
*/
|
|
622
|
+
async create() {
|
|
623
|
+
const cstr = this.constructor;
|
|
624
|
+
const updates = this._getUpdates();
|
|
625
|
+
const service = this._db.getService();
|
|
626
|
+
const keyIndex = cstr._$index;
|
|
617
627
|
let r = null;
|
|
618
628
|
if ((cstr._$key !== '') && (updates[cstr._$key] === undefined)) {
|
|
629
|
+
const keyFields = Array.isArray(keyIndex) ? keyIndex : [keyIndex || cstr._$key];
|
|
630
|
+
if (service === lDb.ESERVICE.PGSQL && !keyFields.includes(cstr._$key)) {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
if (service === lDb.ESERVICE.MYSQL && Array.isArray(keyIndex)) {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
619
636
|
let count = 0;
|
|
620
637
|
while (true) {
|
|
621
638
|
if (count === 3) {
|
|
@@ -626,38 +643,32 @@ export default class Mod {
|
|
|
626
643
|
this[cstr._$key] = updates[cstr._$key];
|
|
627
644
|
this._sql.insert(cstr._$table + (this._index ? ('_' + this._index[0]) : ''));
|
|
628
645
|
this._sql.values(updates);
|
|
646
|
+
if (service === lDb.ESERVICE.PGSQL) {
|
|
647
|
+
this._sql.append(' ON CONFLICT (' + keyFields.map(field => this._sql.field(field)).join(', ') + ') DO NOTHING');
|
|
648
|
+
}
|
|
629
649
|
r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
630
650
|
++count;
|
|
631
651
|
if (!r.error) {
|
|
652
|
+
// --- PG 的自动 key 冲突不报错,未插入时换一个候选值 ---
|
|
653
|
+
if (service === lDb.ESERVICE.PGSQL && r.packet?.affected === 0) {
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
632
656
|
break;
|
|
633
657
|
}
|
|
634
658
|
if (r.error.errno === 1062) {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
// --- 没设置 index,那就当做是本次生成的 key 重复了 ---
|
|
638
|
-
continue;
|
|
639
|
-
}
|
|
640
|
-
if (this._db.getService() === lDb.ESERVICE.MYSQL) {
|
|
641
|
-
// --- MYSQL ---
|
|
642
|
-
const match = /for key '([\w.]+)'/.exec(r.error.message);
|
|
643
|
-
if (match?.[1].includes(cstr._$index)) {
|
|
644
|
-
// --- key 索引重复,继续生成 key ---
|
|
659
|
+
if (service === lDb.ESERVICE.MYSQL) {
|
|
660
|
+
if (!keyIndex) {
|
|
645
661
|
continue;
|
|
646
662
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
// --- PGSQL ---
|
|
650
|
-
const match = /constraint "([\w.]+)"/.exec(r.error.message);
|
|
651
|
-
if (match?.[1].includes(cstr._$index)) {
|
|
652
|
-
// --- key 索引重复,继续生成 key ---
|
|
663
|
+
const match = /for key '([\w.]+)'/.exec(r.error.message);
|
|
664
|
+
if (typeof keyIndex === 'string' && match?.[1].includes(keyIndex)) {
|
|
653
665
|
continue;
|
|
654
666
|
}
|
|
655
667
|
}
|
|
656
|
-
// ---
|
|
668
|
+
// --- PG 的目标冲突已经被 SQL 处理,剩下的是其他唯一约束冲突 ---
|
|
657
669
|
return null;
|
|
658
670
|
}
|
|
659
671
|
// --- 未处理的错误 ---
|
|
660
|
-
const service = this._db.getService();
|
|
661
672
|
lCore.log(this._ctr ?? {}, '[MOD][create0][' + cstr._$table + '] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
662
673
|
if (lCore.globalConfig.debug) {
|
|
663
674
|
lCore.debug('[MOD][create0][' + cstr._$table + ']', service !== null ? lDb.ESERVICE[service] : 'NONE', r);
|
|
@@ -694,34 +705,79 @@ export default class Mod {
|
|
|
694
705
|
}
|
|
695
706
|
}
|
|
696
707
|
/**
|
|
697
|
-
* ---
|
|
698
|
-
*
|
|
708
|
+
* --- 插入数据,如果业务唯一键已存在则更新,保留已有主键和自动 key ---
|
|
709
|
+
* --- PGSQL 为原子 UPSERT,自动 key 只生成一次,SQL 错误需回滚事务 ---
|
|
710
|
+
* --- MySQL 分步执行;自动 key 有多个唯一索引时须配置 _$index,整笔原子性由调用方事务保证 ---
|
|
711
|
+
* @param conflict 已通过 set 赋非 NULL 值的业务唯一字段,不得包含 _$key 或 _$primary;PGSQL 不支持部分或表达式索引
|
|
712
|
+
* @returns 是否成功;至少需要一个可更新字段,失败保留待写数据
|
|
699
713
|
*/
|
|
700
714
|
async upsert(conflict) {
|
|
701
|
-
|
|
702
|
-
// --- 因为还要处理 _$key 的自动生成 ---
|
|
703
|
-
const res = await this.create();
|
|
704
|
-
if (res) {
|
|
705
|
-
// --- 创建成功 ---
|
|
706
|
-
return true;
|
|
707
|
-
}
|
|
708
|
-
if (res === false) {
|
|
709
|
-
// --- 系统错误 ---
|
|
710
|
-
return false;
|
|
711
|
-
}
|
|
712
|
-
// --- res === null,唯一键冲突,执行更新 ---
|
|
713
|
-
/** --- 冲突字段列表 --- */
|
|
715
|
+
const cstr = this.constructor;
|
|
714
716
|
const conflicts = typeof conflict === 'string' ? [conflict] : conflict;
|
|
717
|
+
const inserts = this._getUpdates();
|
|
718
|
+
const updates = { ...inserts };
|
|
715
719
|
const where = {};
|
|
720
|
+
if (!conflicts.length) {
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
716
723
|
for (const field of conflicts) {
|
|
717
|
-
if (!
|
|
724
|
+
if (!field || field === cstr._$key || field === cstr._$primary ||
|
|
725
|
+
!this._updates[field] || inserts[field] === null) {
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
728
|
+
where[field] = inserts[field];
|
|
729
|
+
delete updates[field];
|
|
730
|
+
}
|
|
731
|
+
delete updates[cstr._$primary];
|
|
732
|
+
delete updates[cstr._$key];
|
|
733
|
+
if (!Object.keys(updates).length) {
|
|
734
|
+
return false;
|
|
735
|
+
}
|
|
736
|
+
const mysql = this._db.getService() === lDb.ESERVICE.MYSQL;
|
|
737
|
+
const table = cstr._$table + (this._index ? `_${this._index[0]}` : '');
|
|
738
|
+
const identity = [...new Set([cstr._$primary, cstr._$key].filter(field => field))];
|
|
739
|
+
if (mysql) {
|
|
740
|
+
const created = await this.create();
|
|
741
|
+
if (created !== null) {
|
|
742
|
+
return created;
|
|
743
|
+
}
|
|
744
|
+
// --- 按业务键读取最新目标,事务内锁住该行直到更新完成 ---
|
|
745
|
+
this._sql.select(identity, table).where(where).limit(1).lock();
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
if (cstr._$key && inserts[cstr._$key] === undefined) {
|
|
749
|
+
inserts[cstr._$key] = this._keyGenerator();
|
|
750
|
+
}
|
|
751
|
+
this._sql.insert(table).values(inserts).upsert(updates, conflicts);
|
|
752
|
+
this._sql.append(` RETURNING ${identity.map(field => this._sql.field(field)).join(', ')}`);
|
|
753
|
+
}
|
|
754
|
+
const result = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
755
|
+
if (result.error) {
|
|
756
|
+
lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${result.error.errno}`, '-error');
|
|
757
|
+
return false;
|
|
758
|
+
}
|
|
759
|
+
const row = result.rows?.[0];
|
|
760
|
+
if (!row) {
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
763
|
+
if (mysql) {
|
|
764
|
+
// --- 同时匹配身份字段和业务键,目标变化时不回填旧身份 ---
|
|
765
|
+
this._sql.update(table, updates).where({ ...where, ...row });
|
|
766
|
+
const saved = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
767
|
+
if (saved.error) {
|
|
768
|
+
lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${saved.error.errno}`, '-error');
|
|
718
769
|
return false;
|
|
719
770
|
}
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
771
|
+
if (!saved.packet?.affected) {
|
|
772
|
+
return false;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
for (const field of identity) {
|
|
776
|
+
this._setProperty(field, row[field]);
|
|
723
777
|
}
|
|
724
|
-
|
|
778
|
+
this._updates = {};
|
|
779
|
+
this._jsonUpdates = {};
|
|
780
|
+
return true;
|
|
725
781
|
}
|
|
726
782
|
/**
|
|
727
783
|
* --- 刷新当前模型获取最新数据 ---
|
|
@@ -756,24 +812,27 @@ export default class Mod {
|
|
|
756
812
|
}
|
|
757
813
|
/**
|
|
758
814
|
* --- 更新 set 的数据到数据库,有未保存数据时才保存 ---
|
|
759
|
-
* @param where
|
|
815
|
+
* @param where 非空筛选条件,省略时必须有主键
|
|
816
|
+
* @returns 是否成功,失败保留待写数据
|
|
760
817
|
*/
|
|
761
818
|
async save(where) {
|
|
762
819
|
if (Object.keys(this._updates).length === 0) {
|
|
763
820
|
return true;
|
|
764
821
|
}
|
|
765
822
|
const cstr = this.constructor;
|
|
766
|
-
if (!this._data[cstr._$primary]) {
|
|
823
|
+
if (!this._data[cstr._$primary] && (where === undefined || where === null)) {
|
|
767
824
|
lCore.log(this._ctr ?? {}, '[MOD][save] primary key not found', '-error');
|
|
768
825
|
return false;
|
|
769
826
|
}
|
|
770
|
-
const updates =
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [updates]).where(where ?? {
|
|
827
|
+
const updates = this._getUpdates();
|
|
828
|
+
this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [updates]);
|
|
829
|
+
const updateSql = this._sql.getSql();
|
|
830
|
+
this._sql.where(where ?? {
|
|
775
831
|
[cstr._$primary]: this._data[cstr._$primary]
|
|
776
832
|
});
|
|
833
|
+
if (this._sql.getSql() === updateSql) {
|
|
834
|
+
return false;
|
|
835
|
+
}
|
|
777
836
|
const r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
778
837
|
if (r.packet === null) {
|
|
779
838
|
lCore.log(this._ctr ?? {}, '[MOD][save] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
package/sys/monitor/watchdog.js
CHANGED
|
@@ -71,7 +71,7 @@ function writeLog(msg) {
|
|
|
71
71
|
const entry = {
|
|
72
72
|
'time': `${ts.slice(8, 10)}:${ts.slice(10, 12)}:${ts.slice(12, 14)}`,
|
|
73
73
|
'unix': Math.floor(now.getTime() / 1000),
|
|
74
|
-
'url': '', '
|
|
74
|
+
'url': '', 'userAgent': '',
|
|
75
75
|
'realIp': '', 'cfIp': '', 'xIp': '',
|
|
76
76
|
'osMem': `${Math.round((os.totalmem() - os.freemem()) / 1024 / 1024 * 100) / 100}MB`,
|
|
77
77
|
'procMem': `${Math.round(process.memoryUsage.rss() / 1024 / 1024 * 100) / 100}MB`,
|
|
@@ -83,10 +83,10 @@ function writeLog(msg) {
|
|
|
83
83
|
}
|
|
84
84
|
else {
|
|
85
85
|
if (!fs.existsSync(file)) {
|
|
86
|
-
fs.writeFileSync(file, 'TIME,UNIX,URL,
|
|
86
|
+
fs.writeFileSync(file, 'TIME,UNIX,URL,USER_AGENT,REALIP,CFIP,XIP,OS,PROCESS,MESSAGE\n', { 'mode': 0o600 });
|
|
87
87
|
}
|
|
88
88
|
const fields = [
|
|
89
|
-
entry.time, String(entry.unix), entry.url, '
|
|
89
|
+
entry.time, String(entry.unix), entry.url, 'No HTTP_USER_AGENT',
|
|
90
90
|
entry.realIp, entry.cfIp, entry.xIp, entry.osMem, entry.procMem, entry.message,
|
|
91
91
|
];
|
|
92
92
|
content = fields.map(field => `"${field.replace(/"/g, '""')}"`).join(',') + '\n';
|
package/sys/route.js
CHANGED
|
@@ -17,6 +17,9 @@ 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
|
import { getPublicProtocol as getPublicProtocolImpl } from '#kebab/abox/request.js';
|
|
20
|
+
import { pipeResponse as pipeResponseImpl } from '#kebab/abox/response.js';
|
|
21
|
+
/** --- 将片段依次输出;出错或断连时关闭所有源,回调在输入结束或失败时执行 --- */
|
|
22
|
+
const pipeResponse = pipeResponseImpl;
|
|
20
23
|
/**
|
|
21
24
|
* --- 只用于推导公开 URL;不改变真实 TLS 状态,也不验证转发头来源 ---
|
|
22
25
|
* @param req HTTP 或 HTTP/2 请求
|
|
@@ -329,7 +332,6 @@ export async function run(data) {
|
|
|
329
332
|
cctr.setPrototype('_headers', headers);
|
|
330
333
|
cctr.setPrototype('_get', get);
|
|
331
334
|
cctr.setPrototype('_cookie', cookies);
|
|
332
|
-
lCore.log(cctr, '', '-visit');
|
|
333
335
|
try {
|
|
334
336
|
rtn = await cctr.onLoad();
|
|
335
337
|
}
|
|
@@ -539,7 +541,6 @@ export async function run(data) {
|
|
|
539
541
|
cctr.setPrototype('_cacheTTL', middle.getPrototype('_cacheTTL'));
|
|
540
542
|
cctr.setPrototype('_xsrf', middle.getPrototype('_xsrf'));
|
|
541
543
|
cctr.setPrototype('_httpCode', middle.getPrototype('_httpCode'));
|
|
542
|
-
lCore.log(cctr, '', '-visit');
|
|
543
544
|
// --- 强制 HTTPS ---
|
|
544
545
|
if (config.set.mustHttps && !config.const.https) {
|
|
545
546
|
data.res.setHeader('location', data.req.url ?? '');
|
|
@@ -640,9 +641,7 @@ export async function run(data) {
|
|
|
640
641
|
}
|
|
641
642
|
if (!data.res.writableEnded) {
|
|
642
643
|
if (compress) {
|
|
643
|
-
compress.compress
|
|
644
|
-
compress.compress.pipe(data.res);
|
|
645
|
-
compress.compress.end();
|
|
644
|
+
pipeResponse([rtn], data.res, compress.compress);
|
|
646
645
|
}
|
|
647
646
|
else {
|
|
648
647
|
data.res.end(rtn);
|
|
@@ -676,10 +675,10 @@ export async function run(data) {
|
|
|
676
675
|
}
|
|
677
676
|
if (!data.res.writableEnded) {
|
|
678
677
|
if (compress) {
|
|
679
|
-
stm.
|
|
678
|
+
pipeResponse([stm], data.res, compress.compress);
|
|
680
679
|
}
|
|
681
680
|
else {
|
|
682
|
-
stm
|
|
681
|
+
pipeResponse([stm], data.res);
|
|
683
682
|
}
|
|
684
683
|
}
|
|
685
684
|
}
|
|
@@ -730,9 +729,7 @@ export async function run(data) {
|
|
|
730
729
|
}
|
|
731
730
|
if (!data.res.writableEnded) {
|
|
732
731
|
if (compress) {
|
|
733
|
-
compress.compress
|
|
734
|
-
compress.compress.pipe(data.res);
|
|
735
|
-
compress.compress.end();
|
|
732
|
+
pipeResponse([writeData], data.res, compress.compress);
|
|
736
733
|
}
|
|
737
734
|
else {
|
|
738
735
|
data.res.end(writeData);
|
|
@@ -754,15 +751,19 @@ export async function run(data) {
|
|
|
754
751
|
lCore.writeHead(data.res, httpCode);
|
|
755
752
|
}
|
|
756
753
|
if (!data.res.writableEnded) {
|
|
757
|
-
const
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
754
|
+
const parts = rtn.flatMap((item) => {
|
|
755
|
+
if (item instanceof stream.Readable || typeof item === 'string' || item instanceof Buffer) {
|
|
756
|
+
return [item];
|
|
757
|
+
}
|
|
758
|
+
if (item instanceof lNetResponse.Response || item instanceof lUndiciResponse.Response) {
|
|
759
|
+
const itemStream = item.getStream();
|
|
760
|
+
return itemStream ? [itemStream] : [];
|
|
761
|
+
}
|
|
762
|
+
return [];
|
|
763
|
+
});
|
|
764
|
+
await new Promise((resolve) => {
|
|
765
|
+
pipeResponse(parts, data.res, compress?.compress, resolve);
|
|
766
|
+
});
|
|
766
767
|
}
|
|
767
768
|
}
|
|
768
769
|
}
|
|
@@ -784,9 +785,7 @@ export async function run(data) {
|
|
|
784
785
|
}
|
|
785
786
|
if (!data.res.writableEnded) {
|
|
786
787
|
if (compress) {
|
|
787
|
-
compress.compress
|
|
788
|
-
compress.compress.pipe(data.res);
|
|
789
|
-
compress.compress.end();
|
|
788
|
+
pipeResponse([writeData], data.res, compress.compress);
|
|
790
789
|
}
|
|
791
790
|
else {
|
|
792
791
|
data.res.end(writeData);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
const prefix = 'core-updatecode-';
|
|
4
|
+
const directoryPattern = /^core-updatecode-[a-zA-Z0-9]{6}$/;
|
|
5
|
+
export async function prepare(root) {
|
|
6
|
+
let directory;
|
|
7
|
+
try {
|
|
8
|
+
const temporary = path.join(root, 'ftmp');
|
|
9
|
+
await fs.mkdir(temporary, { recursive: true });
|
|
10
|
+
directory = await fs.mkdtemp(path.join(temporary, prefix));
|
|
11
|
+
await fs.mkdir(path.join(directory, 'www'));
|
|
12
|
+
await fs.writeFile(path.join(directory, 'www/lock.txt'), 'Before updateCode test\n', { flag: 'wx' });
|
|
13
|
+
return path.relative(root, directory).split(path.sep).join('/') + '/';
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
if (directory)
|
|
17
|
+
await fs.rm(directory, { recursive: true, force: true }).catch(() => { });
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export async function clear(root) {
|
|
22
|
+
try {
|
|
23
|
+
const temporary = path.join(root, 'ftmp');
|
|
24
|
+
let entries;
|
|
25
|
+
try {
|
|
26
|
+
entries = await fs.readdir(temporary, { withFileTypes: true });
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error.code === 'ENOENT')
|
|
30
|
+
return true;
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
if (!entry.isDirectory() || !directoryPattern.test(entry.name))
|
|
35
|
+
continue;
|
|
36
|
+
await fs.rm(path.join(temporary, entry.name), { recursive: true, force: true });
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -56,6 +56,10 @@ export default class extends sCtr.Ctr {
|
|
|
56
56
|
coreRealiplimit(): string;
|
|
57
57
|
coreGetlog(): Promise<string>;
|
|
58
58
|
coreLs(): Promise<string>;
|
|
59
|
+
/**
|
|
60
|
+
* --- 在独立临时目录测试文件覆盖,或清理本示例的测试结果 ---
|
|
61
|
+
* @returns 测试或清理结果页面
|
|
62
|
+
*/
|
|
59
63
|
coreUpdatecode(): Promise<string>;
|
|
60
64
|
coreReload(): Promise<string>;
|
|
61
65
|
coreRestart(): Promise<string>;
|
package/www/example/ctr/test.js
CHANGED
|
@@ -29,6 +29,19 @@ import * as sCtr from '#kebab/sys/ctr.js';
|
|
|
29
29
|
// --- mod ---
|
|
30
30
|
import mTest from '../mod/test.js';
|
|
31
31
|
import mTestData from '../mod/testdata.js';
|
|
32
|
+
import * as coreUpdatecodeTest from '../abox/core-updatecode.js';
|
|
33
|
+
/**
|
|
34
|
+
* --- 创建独立测试目录并预置待覆盖文件 ---
|
|
35
|
+
* @param root 运行根目录
|
|
36
|
+
* @returns 相对运行根目录的测试路径;准备失败返回 false
|
|
37
|
+
*/
|
|
38
|
+
const prepareUpdatecodeTest = coreUpdatecodeTest.prepare;
|
|
39
|
+
/**
|
|
40
|
+
* --- 清除 ftmp 下本示例创建的测试目录,保留其他文件及软链接 ---
|
|
41
|
+
* @param root 运行根目录
|
|
42
|
+
* @returns 是否全部清理成功,无测试目录时也返回 true
|
|
43
|
+
*/
|
|
44
|
+
const clearUpdatecodeTest = coreUpdatecodeTest.clear;
|
|
32
45
|
/** --- Kebab 核心库提供的 Valibot 对象 --- */
|
|
33
46
|
const v = lCore.v;
|
|
34
47
|
/** --- Valibot 示例支持的语言 --- */
|
|
@@ -177,7 +190,7 @@ export default class extends sCtr.Ctr {
|
|
|
177
190
|
`<br><a href="${this._config.const.urlBase}test/core-pm2?name=cron&action=restart">View "test/core-pm2"</a>`,
|
|
178
191
|
`<br><a href="${this._config.const.urlBase}test/core-npm">View "test/core-npm"</a>`,
|
|
179
192
|
`<br><a href="${this._config.const.urlBase}test/core-global">View "test/core-global"</a>`,
|
|
180
|
-
`<br><a href="${this._config.const.urlBase}test/core-updatecode">View "test/core-updatecode"</a>`,
|
|
193
|
+
`<br><a href="${this._config.const.urlBase}test/core-updatecode">View "test/core-updatecode"</a> <a href="${this._config.const.urlBase}test/core-updatecode?action=clear">clear</a>`,
|
|
181
194
|
`<br><a href="${this._config.const.urlBase}test/core-env">View "test/core-env"</a>`,
|
|
182
195
|
'<br><br><b>Crypto:</b>',
|
|
183
196
|
`<br><br><a href="${this._config.const.urlBase}test/crypto">View "test/crypto"</a>`,
|
|
@@ -1648,15 +1661,27 @@ for (let i = 0; i < 30000; ++i) {
|
|
|
1648
1661
|
echo.push('</table>');
|
|
1649
1662
|
return echo.join('') + '<br>' + this._getEnd();
|
|
1650
1663
|
}
|
|
1664
|
+
/**
|
|
1665
|
+
* --- 在独立临时目录测试文件覆盖,或清理本示例的测试结果 ---
|
|
1666
|
+
* @returns 测试或清理结果页面
|
|
1667
|
+
*/
|
|
1651
1668
|
async coreUpdatecode() {
|
|
1669
|
+
if (this._get['action'] === 'clear') {
|
|
1670
|
+
const cleared = await clearUpdatecodeTest(kebab.ROOT_CWD);
|
|
1671
|
+
return (cleared ? 'Test files cleared.' : 'Failed to clear test files.') + '<br><br>' + this._getEnd();
|
|
1672
|
+
}
|
|
1652
1673
|
const zip = `${this._config.const.dataPath}test.zip`;
|
|
1653
|
-
const to =
|
|
1674
|
+
const to = await prepareUpdatecodeTest(kebab.ROOT_CWD);
|
|
1675
|
+
if (to === false) {
|
|
1676
|
+
return 'Failed to prepare test files.<br><br>' + this._getEnd();
|
|
1677
|
+
}
|
|
1654
1678
|
const echo = [
|
|
1655
1679
|
`zip: ${zip}<br>
|
|
1656
1680
|
to: ${to}`
|
|
1657
1681
|
];
|
|
1658
|
-
const list = await lCore.updateCode(zip, to);
|
|
1659
|
-
echo.push(`<pre>const list = lCore.updateCode(zip, to);</pre>${
|
|
1682
|
+
const list = await lCore.updateCode(zip, to, undefined, false, true);
|
|
1683
|
+
echo.push(`<pre>const list = await lCore.updateCode(zip, to, undefined, false, true);</pre>${lText.htmlescape(lText.stringifyJson(list))}`);
|
|
1684
|
+
echo.push(`<br><a href="${this._config.const.urlBase}test/core-updatecode?action=clear">clear</a>`);
|
|
1660
1685
|
return echo.join('') + '<br><br>' + this._getEnd();
|
|
1661
1686
|
}
|
|
1662
1687
|
async coreReload() {
|
package/www/example/mod/test.js
CHANGED
|
@@ -12,7 +12,7 @@ CREATE TABLE `m_test` (
|
|
|
12
12
|
`time_add` bigint NOT NULL,
|
|
13
13
|
PRIMARY KEY (`id`) USING BTREE,
|
|
14
14
|
INDEX `time_add`(`time_add` ASC) USING BTREE,
|
|
15
|
-
UNIQUE INDEX `
|
|
15
|
+
UNIQUE INDEX `token`(`token` ASC) USING BTREE,
|
|
16
16
|
UNIQUE INDEX `uname`(`name` ASC) USING BTREE
|
|
17
17
|
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
|
18
18
|
// --- PGSQL ---
|
|
@@ -41,7 +41,7 @@ export default class default_1 extends sMod {
|
|
|
41
41
|
static _$table = 'test';
|
|
42
42
|
static _$primary = 'id';
|
|
43
43
|
static _$key = 'token';
|
|
44
|
-
static _$index = '
|
|
44
|
+
static _$index = 'token';
|
|
45
45
|
/* eslint-enable */
|
|
46
46
|
_keyGenerator() {
|
|
47
47
|
return 'test_' + lCore.rand(0, 3).toString();
|