@maiyunnet/kebab 9.27.0 → 9.28.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/crypto-password.d.ts +3 -0
- package/abox/crypto-password.js +60 -0
- package/doc/kebab-rag.md +307 -113
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/crypto.d.ts +25 -1
- package/lib/crypto.js +21 -1
- package/lib/sql.d.ts +2 -1
- package/lib/sql.js +8 -7
- package/package.json +1 -1
- package/sys/mod.d.ts +16 -3
- package/sys/mod.js +43 -19
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
6
6
|
*/
|
|
7
7
|
/** --- 当前系统版本号 --- */
|
|
8
|
-
export const VER = '9.
|
|
8
|
+
export const VER = '9.28.0';
|
|
9
9
|
// --- 服务端用的路径 ---
|
|
10
10
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
11
11
|
/** --- /xxx/xxx --- */
|
package/lib/crypto.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2019-4-2 14:01:06
|
|
4
|
-
* Last: 2020-3-12 14:05:24, 2022-09-12 11:52:35, 2024-9-8 17:09:39, 2024-11-11 00:21:58, 2025-6-18 20:27:47, 2026-07-29 11:51
|
|
5
4
|
*/
|
|
6
5
|
import * as crypto from 'crypto';
|
|
7
6
|
/**
|
|
@@ -182,3 +181,28 @@ export declare function base64Decode(data: string, encoding?: 'utf8'): string;
|
|
|
182
181
|
* @param options 选项
|
|
183
182
|
*/
|
|
184
183
|
export declare function uuid(options?: crypto.RandomUUIDOptions): string;
|
|
184
|
+
/** --- 密码散列的计算成本,未指定的选项使用默认值 --- */
|
|
185
|
+
export interface IPasswordOptions {
|
|
186
|
+
/** --- 内存成本,单位 KiB,默认 65536(64 MiB);整数,至少为并行度的 8 倍,最多 262144 --- */
|
|
187
|
+
'memory'?: number;
|
|
188
|
+
/** --- 计算轮数,默认 3;整数,范围 1~10 --- */
|
|
189
|
+
'passes'?: number;
|
|
190
|
+
/** --- 并行度,默认 1;整数,范围 1~8 --- */
|
|
191
|
+
'parallelism'?: number;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* --- 使用 Argon2id 生成包含随机盐和计算参数的密码散列 ---
|
|
195
|
+
* 需要 Node.js 24.7.0+;固定使用 16 字节随机盐和 32 字节散列,不修改或截断密码。
|
|
196
|
+
* @param password 原始密码,按 UTF-8 编码;长度和强度由应用校验
|
|
197
|
+
* @param options 计算成本,省略时使用 64 MiB 内存、3 轮和并行度 1
|
|
198
|
+
* @returns 可直接保存的 PHC 字符串;参数无效、运行环境不支持或计算失败时返回 false
|
|
199
|
+
*/
|
|
200
|
+
export declare function passwordHash(password: string, options?: IPasswordOptions): Promise<string | false>;
|
|
201
|
+
/**
|
|
202
|
+
* --- 使用散列中保存的参数验证密码 ---
|
|
203
|
+
* 接受 Argon2id v19,成本范围与 IPasswordOptions 相同,盐为 8~64 字节、散列为 16~64 字节。
|
|
204
|
+
* @param password 原始密码,按 UTF-8 编码
|
|
205
|
+
* @param encoded 数据库存储的 PHC 字符串,不接受客户端指定的散列
|
|
206
|
+
* @returns 匹配返回 true;不匹配、格式不支持或参数越界返回 false;环境不支持或计算失败时返回 null
|
|
207
|
+
*/
|
|
208
|
+
export declare function passwordVerify(password: string, encoded: string): Promise<boolean | null>;
|
package/lib/crypto.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2019-4-2 14:01:06
|
|
4
|
-
* Last: 2020-3-12 14:05:24, 2022-09-12 11:52:35, 2024-9-8 17:09:39, 2024-11-11 00:21:58, 2025-6-18 20:27:47, 2026-07-29 11:51
|
|
5
4
|
*/
|
|
6
5
|
import * as crypto from 'crypto';
|
|
7
6
|
// --- 库和定义 ---
|
|
8
7
|
import * as lFs from '#kebab/lib/fs.js';
|
|
9
8
|
import * as lCore from '#kebab/lib/core.js';
|
|
9
|
+
import * as aPassword from '#kebab/abox/crypto-password.js';
|
|
10
10
|
// --- 非对称加密 ---
|
|
11
11
|
/**
|
|
12
12
|
* --- 创建非对称秘钥 ---
|
|
@@ -320,3 +320,23 @@ export function base64Decode(data, encoding = 'utf8') {
|
|
|
320
320
|
export function uuid(options) {
|
|
321
321
|
return crypto.randomUUID(options);
|
|
322
322
|
}
|
|
323
|
+
/**
|
|
324
|
+
* --- 使用 Argon2id 生成包含随机盐和计算参数的密码散列 ---
|
|
325
|
+
* 需要 Node.js 24.7.0+;固定使用 16 字节随机盐和 32 字节散列,不修改或截断密码。
|
|
326
|
+
* @param password 原始密码,按 UTF-8 编码;长度和强度由应用校验
|
|
327
|
+
* @param options 计算成本,省略时使用 64 MiB 内存、3 轮和并行度 1
|
|
328
|
+
* @returns 可直接保存的 PHC 字符串;参数无效、运行环境不支持或计算失败时返回 false
|
|
329
|
+
*/
|
|
330
|
+
export function passwordHash(password, options = {}) {
|
|
331
|
+
return aPassword.hash(password, options);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* --- 使用散列中保存的参数验证密码 ---
|
|
335
|
+
* 接受 Argon2id v19,成本范围与 IPasswordOptions 相同,盐为 8~64 字节、散列为 16~64 字节。
|
|
336
|
+
* @param password 原始密码,按 UTF-8 编码
|
|
337
|
+
* @param encoded 数据库存储的 PHC 字符串,不接受客户端指定的散列
|
|
338
|
+
* @returns 匹配返回 true;不匹配、格式不支持或参数越界返回 false;环境不支持或计算失败时返回 null
|
|
339
|
+
*/
|
|
340
|
+
export function passwordVerify(password, encoded) {
|
|
341
|
+
return aPassword.verify(password, encoded);
|
|
342
|
+
}
|
package/lib/sql.d.ts
CHANGED
|
@@ -194,7 +194,8 @@ export declare class Sql {
|
|
|
194
194
|
private _whereDataPosition;
|
|
195
195
|
/**
|
|
196
196
|
* --- 筛选器 ---
|
|
197
|
-
* ---
|
|
197
|
+
* --- 等值条件:'city': 'bj';字符串、数字、布尔值、bigint、Date 和 Buffer 均作为单个参数 ---
|
|
198
|
+
* --- null 生成 IS NULL,数组生成 IN 条件 ---
|
|
198
199
|
* --- 运算符条件:['type', '>', '1'] ---
|
|
199
200
|
* --- 集合条件:['type', 'in', ['1', '2']] 或 'type': ['1', '2'] ---
|
|
200
201
|
* --- 逻辑分组:'$or': [{'city': 'bj'}, {'city': 'sh'}, [['age', '>', '10']]] ---
|
package/lib/sql.js
CHANGED
|
@@ -504,7 +504,8 @@ export class Sql {
|
|
|
504
504
|
_whereDataPosition = [0, 0];
|
|
505
505
|
/**
|
|
506
506
|
* --- 筛选器 ---
|
|
507
|
-
* ---
|
|
507
|
+
* --- 等值条件:'city': 'bj';字符串、数字、布尔值、bigint、Date 和 Buffer 均作为单个参数 ---
|
|
508
|
+
* --- null 生成 IS NULL,数组生成 IN 条件 ---
|
|
508
509
|
* --- 运算符条件:['type', '>', '1'] ---
|
|
509
510
|
* --- 集合条件:['type', 'in', ['1', '2']] 或 'type': ['1', '2'] ---
|
|
510
511
|
* --- 逻辑分组:'$or': [{'city': 'bj'}, {'city': 'sh'}, [['age', '>', '10']]] ---
|
|
@@ -700,11 +701,6 @@ export class Sql {
|
|
|
700
701
|
// --- NULL 判断 ---
|
|
701
702
|
sql += this.field(k) + ' IS NULL AND ';
|
|
702
703
|
}
|
|
703
|
-
else if (typeof v === 'string' || typeof v === 'number') {
|
|
704
|
-
// --- 标量相等 ---
|
|
705
|
-
sql += this.field(k) + ' = ' + this._placeholder() + ' AND ';
|
|
706
|
-
data.push(v);
|
|
707
|
-
}
|
|
708
704
|
else if (lSqlValue.isJson(v)) {
|
|
709
705
|
// --- JSON 相等 ---
|
|
710
706
|
const result = this._processValue(v);
|
|
@@ -715,7 +711,7 @@ export class Sql {
|
|
|
715
711
|
// --- 字段相等 ---
|
|
716
712
|
sql += this.field(k) + ' = ' + this.field(v.value) + ' AND ';
|
|
717
713
|
}
|
|
718
|
-
else {
|
|
714
|
+
else if (Array.isArray(v)) {
|
|
719
715
|
// --- IN 查询,如 'type': ['1', '2'] ---
|
|
720
716
|
if (v.length > 0) {
|
|
721
717
|
sql += this.field(k) + ' IN (';
|
|
@@ -739,6 +735,11 @@ export class Sql {
|
|
|
739
735
|
sql += this.field(k) + ' IN (NULL) AND ';
|
|
740
736
|
}
|
|
741
737
|
}
|
|
738
|
+
else {
|
|
739
|
+
// --- 普通值使用等号比较,原值交给数据库驱动处理 ---
|
|
740
|
+
sql += this.field(k) + ' = ' + this._placeholder() + ' AND ';
|
|
741
|
+
data.push(v);
|
|
742
|
+
}
|
|
742
743
|
}
|
|
743
744
|
}
|
|
744
745
|
}
|
package/package.json
CHANGED
package/sys/mod.d.ts
CHANGED
|
@@ -43,6 +43,12 @@ export default class Mod {
|
|
|
43
43
|
protected static _$table: string;
|
|
44
44
|
/** --- 主键字段名 --- */
|
|
45
45
|
protected static _$primary: string;
|
|
46
|
+
/**
|
|
47
|
+
* --- 表的分区字段名,如 ['time_push'] ---
|
|
48
|
+
* 只填分区字段,不用额外填写主键;save、remove、refresh 默认会把主键一起加入 WHERE。
|
|
49
|
+
* 读取记录后不要修改主键或分区字段;留空时只使用主键。
|
|
50
|
+
*/
|
|
51
|
+
protected static _$partition: string[];
|
|
46
52
|
/** --- 设置后将由 _keyGenerator 函数生成唯一字段 --- */
|
|
47
53
|
protected static _$key: string;
|
|
48
54
|
/** --- 自动 key 冲突目标:MySQL 为索引名;PGSQL 为唯一字段或字段数组,PG 留空使用 _$key,不支持部分或表达式索引 --- */
|
|
@@ -314,6 +320,11 @@ export default class Mod {
|
|
|
314
320
|
* @param obj 要转换的 kv 数据列表
|
|
315
321
|
*/
|
|
316
322
|
static toArrayByRecord<T extends Mod>(obj: Record<string, T>): Record<string, Record<string, any>>;
|
|
323
|
+
/**
|
|
324
|
+
* --- 构造包含主键和分区字段的 WHERE 条件 ---
|
|
325
|
+
* @returns 主键及分区字段条件,缺少字段时返回 false
|
|
326
|
+
*/
|
|
327
|
+
private _getIdentityWhere;
|
|
317
328
|
/**
|
|
318
329
|
* --- 设置单个模型属性,并记录是否需要在数据库边界序列化 ---
|
|
319
330
|
* @param key 字段名
|
|
@@ -348,16 +359,18 @@ export default class Mod {
|
|
|
348
359
|
/**
|
|
349
360
|
* --- 刷新当前模型获取最新数据 ---
|
|
350
361
|
* @param lock 是否加锁
|
|
362
|
+
* @returns true 成功,null 记录不存在,false 缺少定位字段或查询失败
|
|
351
363
|
*/
|
|
352
364
|
refresh(lock?: boolean): Promise<boolean | null>;
|
|
353
365
|
/**
|
|
354
366
|
* --- 更新 set 的数据到数据库,有未保存数据时才保存 ---
|
|
355
|
-
* @param where
|
|
356
|
-
* @returns
|
|
367
|
+
* @param where 显式非空筛选条件;省略时使用主键和分区字段值
|
|
368
|
+
* @returns 是否成功,缺少默认定位字段或写入失败返回 false 并保留待写数据
|
|
357
369
|
*/
|
|
358
370
|
save(where?: any): Promise<boolean>;
|
|
359
371
|
/**
|
|
360
|
-
* ---
|
|
372
|
+
* --- 按主键和分区字段删除当前记录 ---
|
|
373
|
+
* @returns 是否成功,缺少定位字段、记录不存在或删除失败返回 false
|
|
361
374
|
*/
|
|
362
375
|
remove(): Promise<boolean>;
|
|
363
376
|
first(lock: boolean, array: true): Promise<false | null | Record<string, any>>;
|
package/sys/mod.js
CHANGED
|
@@ -51,6 +51,12 @@ export default class Mod {
|
|
|
51
51
|
static _$table = '';
|
|
52
52
|
/** --- 主键字段名 --- */
|
|
53
53
|
static _$primary = 'id';
|
|
54
|
+
/**
|
|
55
|
+
* --- 表的分区字段名,如 ['time_push'] ---
|
|
56
|
+
* 只填分区字段,不用额外填写主键;save、remove、refresh 默认会把主键一起加入 WHERE。
|
|
57
|
+
* 读取记录后不要修改主键或分区字段;留空时只使用主键。
|
|
58
|
+
*/
|
|
59
|
+
static _$partition = [];
|
|
54
60
|
/** --- 设置后将由 _keyGenerator 函数生成唯一字段 --- */
|
|
55
61
|
static _$key = '';
|
|
56
62
|
/** --- 自动 key 冲突目标:MySQL 为索引名;PGSQL 为唯一字段或字段数组,PG 留空使用 _$key,不支持部分或表达式索引 --- */
|
|
@@ -551,6 +557,25 @@ export default class Mod {
|
|
|
551
557
|
return rtn;
|
|
552
558
|
}
|
|
553
559
|
// --- 动态方法 ---
|
|
560
|
+
/**
|
|
561
|
+
* --- 构造包含主键和分区字段的 WHERE 条件 ---
|
|
562
|
+
* @returns 主键及分区字段条件,缺少字段时返回 false
|
|
563
|
+
*/
|
|
564
|
+
_getIdentityWhere() {
|
|
565
|
+
const cstr = this.constructor;
|
|
566
|
+
if (!cstr._$partition.length) {
|
|
567
|
+
return this._data[cstr._$primary] ? { [cstr._$primary]: this._data[cstr._$primary] } : false;
|
|
568
|
+
}
|
|
569
|
+
const where = {};
|
|
570
|
+
for (const field of new Set([cstr._$primary, ...cstr._$partition])) {
|
|
571
|
+
const value = this._data[field];
|
|
572
|
+
if (value === undefined || (field === cstr._$primary && value === null)) {
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
where[field] = value;
|
|
576
|
+
}
|
|
577
|
+
return where;
|
|
578
|
+
}
|
|
554
579
|
/**
|
|
555
580
|
* --- 设置单个模型属性,并记录是否需要在数据库边界序列化 ---
|
|
556
581
|
* @param key 字段名
|
|
@@ -735,7 +760,7 @@ export default class Mod {
|
|
|
735
760
|
}
|
|
736
761
|
const mysql = this._db.getService() === lDb.ESERVICE.MYSQL;
|
|
737
762
|
const table = cstr._$table + (this._index ? `_${this._index[0]}` : '');
|
|
738
|
-
const identity = [...new Set([cstr._$primary, cstr._$key].filter(field => field))];
|
|
763
|
+
const identity = [...new Set([cstr._$primary, cstr._$key, ...cstr._$partition].filter(field => field))];
|
|
739
764
|
if (mysql) {
|
|
740
765
|
const created = await this.create();
|
|
741
766
|
if (created !== null) {
|
|
@@ -782,16 +807,16 @@ export default class Mod {
|
|
|
782
807
|
/**
|
|
783
808
|
* --- 刷新当前模型获取最新数据 ---
|
|
784
809
|
* @param lock 是否加锁
|
|
810
|
+
* @returns true 成功,null 记录不存在,false 缺少定位字段或查询失败
|
|
785
811
|
*/
|
|
786
812
|
async refresh(lock = false) {
|
|
787
813
|
const cstr = this.constructor;
|
|
788
|
-
|
|
789
|
-
|
|
814
|
+
const where = this._getIdentityWhere();
|
|
815
|
+
if (where === false) {
|
|
816
|
+
lCore.log(this._ctr ?? {}, '[MOD][refresh] primary or partition field not loaded', '-error');
|
|
790
817
|
return false;
|
|
791
818
|
}
|
|
792
|
-
this._sql.select('*', cstr._$table + (this._index ? ('_' + this._index[0]) : '')).where(
|
|
793
|
-
[cstr._$primary]: this._data[cstr._$primary]
|
|
794
|
-
}]);
|
|
819
|
+
this._sql.select('*', cstr._$table + (this._index ? ('_' + this._index[0]) : '')).where(where);
|
|
795
820
|
if (lock) {
|
|
796
821
|
this._sql.lock();
|
|
797
822
|
}
|
|
@@ -812,24 +837,23 @@ export default class Mod {
|
|
|
812
837
|
}
|
|
813
838
|
/**
|
|
814
839
|
* --- 更新 set 的数据到数据库,有未保存数据时才保存 ---
|
|
815
|
-
* @param where
|
|
816
|
-
* @returns
|
|
840
|
+
* @param where 显式非空筛选条件;省略时使用主键和分区字段值
|
|
841
|
+
* @returns 是否成功,缺少默认定位字段或写入失败返回 false 并保留待写数据
|
|
817
842
|
*/
|
|
818
843
|
async save(where) {
|
|
819
844
|
if (Object.keys(this._updates).length === 0) {
|
|
820
845
|
return true;
|
|
821
846
|
}
|
|
822
847
|
const cstr = this.constructor;
|
|
823
|
-
|
|
824
|
-
|
|
848
|
+
const filter = where ?? this._getIdentityWhere();
|
|
849
|
+
if (filter === false) {
|
|
850
|
+
lCore.log(this._ctr ?? {}, '[MOD][save] primary or partition field not loaded', '-error');
|
|
825
851
|
return false;
|
|
826
852
|
}
|
|
827
853
|
const updates = this._getUpdates();
|
|
828
854
|
this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [updates]);
|
|
829
855
|
const updateSql = this._sql.getSql();
|
|
830
|
-
this._sql.where(
|
|
831
|
-
[cstr._$primary]: this._data[cstr._$primary]
|
|
832
|
-
});
|
|
856
|
+
this._sql.where(filter);
|
|
833
857
|
if (this._sql.getSql() === updateSql) {
|
|
834
858
|
return false;
|
|
835
859
|
}
|
|
@@ -848,17 +872,17 @@ export default class Mod {
|
|
|
848
872
|
}
|
|
849
873
|
}
|
|
850
874
|
/**
|
|
851
|
-
* ---
|
|
875
|
+
* --- 按主键和分区字段删除当前记录 ---
|
|
876
|
+
* @returns 是否成功,缺少定位字段、记录不存在或删除失败返回 false
|
|
852
877
|
*/
|
|
853
878
|
async remove() {
|
|
854
879
|
const cstr = this.constructor;
|
|
855
|
-
|
|
856
|
-
|
|
880
|
+
const where = this._getIdentityWhere();
|
|
881
|
+
if (where === false) {
|
|
882
|
+
lCore.log(this._ctr ?? {}, '[MOD][remove] primary or partition field not loaded', '-error');
|
|
857
883
|
return false;
|
|
858
884
|
}
|
|
859
|
-
this._sql.delete(cstr._$table + (this._index ? ('_' + this._index[0]) : '')).where(
|
|
860
|
-
[cstr._$primary]: this._data[cstr._$primary]
|
|
861
|
-
}]);
|
|
885
|
+
this._sql.delete(cstr._$table + (this._index ? ('_' + this._index[0]) : '')).where(where);
|
|
862
886
|
const r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
863
887
|
if (r.packet === null) {
|
|
864
888
|
lCore.log(this._ctr ?? {}, '[MOD][remove] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|