@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
package/lib/kv.d.ts CHANGED
@@ -1,34 +1,13 @@
1
1
  /**
2
2
  * Project: Kebab, User: JianSuoQiYue
3
3
  * Date: 2019-5-30 19:25:22
4
- * Last: 2020-3-28 18:54:04, 2022-09-12 23:24:45, 2022-09-22 01:06:22, 2024-2-21 13:32:56, 2024-8-21 16:59:57, 2025-11-6 14:56:45
5
4
  */
6
- import * as redis from '@litert/redis';
5
+ import type * as tls from 'node:tls';
6
+ import * as redis from 'redis';
7
7
  import * as kebab from '#kebab/index.js';
8
8
  import * as sCtr from '#kebab/sys/ctr.js';
9
- export interface IZRangeOptions {
10
- /**
11
- * Range query type.
12
- *
13
- * - SCORE: Query by score range
14
- * - LEX: Query by lexicographical range
15
- */
16
- 'by'?: 'SCORE' | 'LEX';
17
- /**
18
- * Whether to return results in reverse order.
19
- */
20
- 'rev'?: boolean;
21
- /**
22
- * Pagination offset. Must be used together with count.
23
- */
24
- 'offset'?: number;
25
- /**
26
- * Pagination count. Must be used together with offset.
27
- */
28
- 'count'?: number;
29
- }
30
9
  /**
31
- * --- 键值存储操作类 ---
10
+ * --- 键值存储操作类(兼容 Redis 与 Kvrocks) ---
32
11
  *
33
12
  * @example
34
13
  * ```ts
@@ -42,29 +21,46 @@ export interface IZRangeOptions {
42
21
  export declare class Kv {
43
22
  /** --- 当前的 kv 连接信息 --- */
44
23
  private readonly _etc;
24
+ /** --- 实际向服务端选择的数据库编号 --- */
25
+ private readonly _db;
26
+ /** --- 实际生效的键名前缀(启用 dbPre 且 index > 0 时自动附带 db{index}:) --- */
27
+ private readonly _pre;
45
28
  constructor(etc: kebab.IConfigKv);
46
29
  /**
47
- * --- 获取当前 Kv 配置的键名前缀 ---
48
- * @returns 原生 pipeline 不会自动添加的项目前缀
30
+ * --- 获取当前 Kv 实际生效的键名前缀 ---
31
+ * @returns 原生 pipeline / eval 不会自动添加的项目前缀
49
32
  */
50
33
  getPre(): string;
51
34
  /**
52
- * --- 获取一个 pipeline 操作对象 ---
35
+ * --- 获取底层官方 redis 客户端实例(用于执行原生扩展命令) ---
36
+ * @returns 连接失败返回 null
37
+ */
38
+ getClient(): Promise<TRedisClient | null>;
39
+ /**
40
+ * --- 获取一个绑定在共享连接上的原生 multi / pipeline 对象(通过 execAsPipeline() 或 exec() 执行,无需关闭) ---
53
41
  * @returns 失败则返回 false
54
42
  */
55
- pipeline(): Promise<redis.IPipelineClient | false>;
43
+ pipeline(): Promise<TRedisMulti | false>;
44
+ /**
45
+ * --- 在共享连接上执行一个 Redis Lua 脚本 ---
46
+ * @param script Lua 源码
47
+ * @param keys 传给 KEYS 的完整键名,调用方需自行添加项目前缀
48
+ * @param args 传给 ARGV 的参数
49
+ * @returns Redis 原始结果,nil 返回 null,连接或执行失败返回 false
50
+ */
51
+ eval(script: string, keys: string[], args: Array<string | Buffer>): Promise<unknown>;
56
52
  /**
57
53
  * --- 设定一个值 ---
58
- * @param key
59
- * @param val
54
+ * @param key 键
55
+ * @param val 值
60
56
  * @param ttl 秒,0 为不限制
61
57
  * @param mod 设置模式: 空,nx(key不存在才建立),xx(key存在才修改)
62
58
  */
63
59
  set(key: string, val: object | string | number, ttl?: number, mod?: '' | 'nx' | 'xx'): Promise<boolean>;
64
60
  /**
65
61
  * --- 添加一个值,存在则不变 ---
66
- * @param key
67
- * @param val
62
+ * @param key 键
63
+ * @param val 值
68
64
  * @param ttl 秒,0 为不限制
69
65
  */
70
66
  add(key: string, val: object | string | number, ttl?: number): Promise<boolean>;
@@ -83,8 +79,8 @@ export declare class Kv {
83
79
  append(key: string, val: string): Promise<boolean>;
84
80
  /**
85
81
  * --- 向已存在的值之前追加数据 ---
86
- * @param key
87
- * @param val
82
+ * @param key 键
83
+ * @param val 值
88
84
  */
89
85
  prepend(key: string, val: string): Promise<boolean>;
90
86
  /**
@@ -94,18 +90,18 @@ export declare class Kv {
94
90
  exists(keys: string | string[]): Promise<number>;
95
91
  /**
96
92
  * --- 获取字符串 ---
97
- * @param key
93
+ * @param key 键
98
94
  * @returns 字符串 / false / null(即使存入时是 number,这个方法也只会返回字符串)
99
95
  */
100
96
  get(key: string): Promise<string | false | null>;
101
97
  /**
102
98
  * --- 获取相应的剩余有效期秒数 ---
103
- * @param key
99
+ * @param key 键
104
100
  */
105
101
  ttl(key: string): Promise<number | null>;
106
102
  /**
107
103
  * --- 获取相应的剩余有效期毫秒数 ---
108
- * @param key
104
+ * @param key 键
109
105
  */
110
106
  pttl(key: string): Promise<number | null>;
111
107
  /**
@@ -114,8 +110,8 @@ export declare class Kv {
114
110
  */
115
111
  mGet(keys: string[]): Promise<Record<string, string | null> | false>;
116
112
  /**
117
- * --- 批量设置哈希值 ---
118
- * @param rows key / val 数组
113
+ * --- 批量设置键值 ---
114
+ * @param rows key / val 对象
119
115
  */
120
116
  mSet(rows: Record<string, string | Buffer>): Promise<boolean>;
121
117
  /**
@@ -146,39 +142,39 @@ export declare class Kv {
146
142
  getDelJson<T>(key: string): Promise<T | false | null>;
147
143
  /**
148
144
  * --- 删除已存在的值 ---
149
- * @param keys
145
+ * @param keys 单个或序列
150
146
  */
151
147
  del(keys: string | string[]): Promise<boolean>;
152
148
  /**
153
149
  * --- 自增 ---
154
- * @param key
150
+ * @param key 键
155
151
  * @param num 整数或浮点正数
156
152
  */
157
153
  incr(key: string, num?: number): Promise<number | false>;
158
154
  /**
159
155
  * --- 自减 ---
160
- * @param key
156
+ * @param key 键
161
157
  * @param num 整数或浮点正数
162
158
  */
163
159
  decr(key: string, num?: number): Promise<number | false>;
164
160
  /**
165
161
  * --- 仅修改过期时间不修改值 ---
166
- * @param key
167
- * @param ttl
162
+ * @param key 键
163
+ * @param ttl 秒
168
164
  */
169
165
  expire(key: string, ttl: number): Promise<boolean>;
170
166
  /**
171
167
  * --- 获取服务器上的所有 key 列表 ---
172
- * @param pattern
168
+ * @param pattern 匹配模式
173
169
  */
174
170
  keys(pattern: string): Promise<string[] | false>;
175
171
  /**
176
- * --- 根据条件获取服务器上的 keys ---
177
- * @param cursor
172
+ * --- 根据条件获取服务器上的 keys(兼容 Redis 数字串游标与 Kvrocks 字符串游标) ---
173
+ * @param cursor 游标,首次传 '0'(或 0)
178
174
  * @param pattern 例如 *
179
175
  * @param count 获取的条数
180
176
  */
181
- scan(cursor?: number, pattern?: string, count?: number): Promise<redis.IScanResult<string> | false>;
177
+ scan(cursor?: string | number, pattern?: string, count?: number): Promise<IScanResult | false>;
182
178
  /**
183
179
  * --- 清除当前所选数据库的所有内容 ---
184
180
  */
@@ -192,19 +188,19 @@ export declare class Kv {
192
188
  * @param key key 名
193
189
  * @param field 字段名
194
190
  * @param val 值
195
- * @param mod 空,nx(key不存在才建立)
191
+ * @param mod 空,nx(field不存在才建立)
196
192
  */
197
193
  hSet(key: string, field: string, val: object | string | number, mod?: '' | 'nx'): Promise<boolean>;
198
194
  /**
199
195
  * --- 批量设置哈希值 ---
200
196
  * @param key key 名
201
- * @param rows key / val 数组
197
+ * @param rows field / val 对象
202
198
  */
203
199
  hMSet(key: string, rows: Record<string, object | string | number>): Promise<boolean>;
204
200
  /**
205
201
  * --- 获取哈希值 ---
206
- * @param key
207
- * @param field
202
+ * @param key 键
203
+ * @param field 字段
208
204
  */
209
205
  hGet(key: string, field: string): Promise<string | false | null>;
210
206
  /**
@@ -224,25 +220,25 @@ export declare class Kv {
224
220
  hGetJson<T>(key: string, field: string): Promise<T | false | null>;
225
221
  /**
226
222
  * --- 批量获取哈希值 ---
227
- * @param key
228
- * @param fields
223
+ * @param key 键
224
+ * @param fields 字段列表
229
225
  */
230
226
  hMGet(key: string, fields: string[]): Promise<Record<string, string | null> | false>;
231
227
  /**
232
228
  * --- 批量获取哈希键值对 ---
233
- * @param key
229
+ * @param key 键
234
230
  */
235
231
  hGetAll(key: string): Promise<Record<string, string | null> | false>;
236
232
  /**
237
233
  * --- 删除哈希键 ---
238
- * @param key
239
- * @param fields 值序列
234
+ * @param key 键
235
+ * @param fields 字段序列
240
236
  */
241
237
  hDel(key: string, fields: string | string[]): Promise<number | false>;
242
238
  /**
243
239
  * --- 判断哈希字段是否存在 ---
244
- * @param key
245
- * @param field
240
+ * @param key 键
241
+ * @param field 字段
246
242
  */
247
243
  hExists(key: string, field: string): Promise<boolean>;
248
244
  /**
@@ -254,16 +250,57 @@ export declare class Kv {
254
250
  hIncr(key: string, field: string, increment: number): Promise<number | false>;
255
251
  /**
256
252
  * --- 获取哈希所有字段 ---
257
- * @param key
253
+ * @param key 键
258
254
  */
259
255
  hKeys(key: string): Promise<string[] | false>;
256
+ /**
257
+ * --- 从列表左侧推入元素 ---
258
+ * @param key 键
259
+ * @param values 元素数组
260
+ */
260
261
  lPush(key: string, values: Array<string | Buffer>): Promise<number | false>;
262
+ /**
263
+ * --- 从列表右侧推入元素 ---
264
+ * @param key 键
265
+ * @param values 元素数组
266
+ */
261
267
  rPush(key: string, values: Array<string | Buffer>): Promise<number | false>;
268
+ /**
269
+ * --- 阻塞式在两个列表间移动元素(使用独立连接,避免阻塞共享连接) ---
270
+ * @param sourceKey 源列表键
271
+ * @param destKey 目标列表键
272
+ * @param soo 源端方向
273
+ * @param deo 目标端方向
274
+ * @param timeout 超时秒数,0 为无限等待
275
+ */
262
276
  bLMove(sourceKey: string, destKey: string, soo: 'LEFT' | 'RIGHT', deo: 'LEFT' | 'RIGHT', timeout: number): Promise<string | null | false>;
277
+ /**
278
+ * --- 从列表左侧弹出元素 ---
279
+ * @param key 键
280
+ */
263
281
  lPop(key: string): Promise<string | null | false>;
282
+ /**
283
+ * --- 从列表右侧弹出元素 ---
284
+ * @param key 键
285
+ */
264
286
  rPop(key: string): Promise<string | null | false>;
287
+ /**
288
+ * --- 阻塞式从列表右侧弹出元素(使用独立连接,避免阻塞共享连接) ---
289
+ * @param key 单个或序列键
290
+ * @param timeout 超时秒数,0 为无限等待
291
+ */
265
292
  bRPop(key: string | string[], timeout: number): Promise<Record<string, string> | false>;
293
+ /**
294
+ * --- 获取列表指定区间元素 ---
295
+ * @param key 键
296
+ * @param start 起始位置
297
+ * @param stop 结束位置
298
+ */
266
299
  lRange(key: string, start: number, stop: number): Promise<string[] | false>;
300
+ /**
301
+ * --- 获取列表长度 ---
302
+ * @param key 键
303
+ */
267
304
  lLen(key: string): Promise<number | false>;
268
305
  /**
269
306
  * --- 添加有序集合元素(单个元素) ---
@@ -276,56 +313,122 @@ export declare class Kv {
276
313
  * --- 添加有序集合元素(多个元素,含 INCR 选项) ---
277
314
  * @param key key 名
278
315
  * @param elements 元素数组
279
- * @param options 选项,需要 INCR
316
+ * @param options 选项(INCR 为 true 时返回新分数,条件未满足时返回 null)
280
317
  */
281
318
  zAdd(key: string, elements: Array<{
282
319
  'score': number;
283
320
  'member': string | Buffer;
284
- }>, options: redis.IZAddOptionsIncr): Promise<number | false>;
321
+ }>, options: IZAddOptions & {
322
+ 'INCR': true;
323
+ }): Promise<number | null | false>;
285
324
  /**
286
- * --- 添加有序集合元素(多个元素,含 INCR 选项,可空) ---
325
+ * --- 添加有序集合元素(多个元素) ---
287
326
  * @param key key 名
288
327
  * @param elements 元素数组
289
- * @param options 选项,需要 INCR Nullable
328
+ * @param options 选项
290
329
  */
291
330
  zAdd(key: string, elements: Array<{
292
331
  'score': number;
293
332
  'member': string | Buffer;
294
- }>, options: redis.IZAddOptionsIncrNullable): Promise<number | null | false>;
333
+ }>, options?: IZAddOptions): Promise<number | false>;
295
334
  /**
296
- * --- 添加有序集合元素(多个元素) ---
297
- * @param key key 名
298
- * @param elements 元素数组
299
- * @param options 选项
335
+ * --- 查询有序集合成员及分数 ---
336
+ * @param key 键
337
+ * @param start 起点
338
+ * @param stop 终点
339
+ * @param options 范围与分页选项
300
340
  */
301
- zAdd(key: string, elements: Array<{
341
+ zRangeWithScores(key: string, start: number | string, stop: number | string, options?: IZRangeOptions): Promise<Array<{
342
+ 'member': string;
302
343
  'score': number;
303
- 'member': string | Buffer;
304
- }>, options?: redis.IZAddOptions): Promise<number | false>;
305
- zRangeWithScores(key: string, start: number, stop: number, options?: IZRangeOptions): Promise<Array<{
306
- member: string;
307
- score: number;
308
344
  }> | false>;
345
+ /**
346
+ * --- 移除有序集合成员 ---
347
+ * @param key 键
348
+ * @param members 成员列表
349
+ */
309
350
  zRem(key: string, members: Array<string | Buffer>): Promise<number | false>;
310
351
  /**
311
- * --- 从连接池中获取一个符合要求的连接 ---
352
+ * --- 在独立副本连接上执行阻塞命令,避免阻塞共享连接和触发默认 5 秒命令超时 ---
353
+ * @param timeout 阻塞超时秒数,0 为无限等待
354
+ * @param fn 实际执行函数
355
+ */
356
+ private _runBlocking;
357
+ /**
358
+ * --- 从连接池中获取一个符合要求的共享连接 ---
312
359
  */
313
360
  private _getConnection;
361
+ /**
362
+ * --- 建立共享连接,统一走 RESP2 兼容 Redis 与 Kvrocks,并在非 0 库时校验 Kvrocks 多库隔离 ---
363
+ */
364
+ private _connect;
314
365
  }
315
366
  /**
316
367
  * --- 获取 Kv 对象 ---
317
368
  * @param ctrEtc 控制器或配置信息
318
- * @param oetc 可用来覆盖 ctr 的一些选项,如 index
369
+ * @param oetc 可用来覆盖 ctr 的部分连接选项
319
370
  */
320
371
  export declare function get(ctrEtc: sCtr.Ctr | kebab.IConfigKv, oetc?: {
372
+ /** --- 数据库编号(0 起) --- */
321
373
  'index'?: number;
374
+ /** --- ACL 用户名(留空则走传统单密码认证) --- */
322
375
  'user'?: string;
376
+ /** --- 认证密码或 Kvrocks namespace token --- */
323
377
  'pwd'?: string;
378
+ /** --- 业务键名前缀 --- */
379
+ 'pre'?: string;
380
+ /** --- 为 true 时将非 0 的 index 折算为键名前缀(如 db1:),不再发 SELECT --- */
381
+ 'dbPre'?: boolean;
382
+ /** --- 启用 TLS;传对象时自动开启 TLS 并透传 servername、ca 等参数 --- */
383
+ 'tls'?: boolean | tls.ConnectionOptions;
324
384
  }): Kv;
325
- /** --- 连接信息 --- */
385
+ /** --- 底层官方 redis 客户端类型(固定 RESP2) --- */
386
+ export type TRedisClient = redis.RedisClientType<Record<string, never>, Record<string, never>, Record<string, never>, 2>;
387
+ /** --- 底层原生 multi / pipeline 类型 --- */
388
+ export type TRedisMulti = ReturnType<TRedisClient['multi']>;
389
+ /** --- SCAN 分页结果 --- */
390
+ export interface IScanResult<T = string> {
391
+ /** --- 下一页游标('0' 表示结束;兼容 Redis 数字串与 Kvrocks 原生字符串游标) --- */
392
+ 'cursor': string;
393
+ /** --- 当前页匹配到的键名列表(已去除项目前缀) --- */
394
+ 'items': T[];
395
+ }
396
+ /** --- ZRANGE 查询选项 --- */
397
+ export interface IZRangeOptions {
398
+ /** --- 查询方式:SCORE 按分数范围,LEX 按字典序 --- */
399
+ 'by'?: 'SCORE' | 'LEX';
400
+ /** --- 是否倒序返回 --- */
401
+ 'rev'?: boolean;
402
+ /** --- 分页偏移量,需与 count 同用 --- */
403
+ 'offset'?: number;
404
+ /** --- 分页条数,需与 offset 同用 --- */
405
+ 'count'?: number;
406
+ }
407
+ /** --- ZADD 选项 --- */
408
+ export interface IZAddOptions {
409
+ /** --- 写入条件:NX 仅添加不存在的成员,XX 仅更新已存在的成员 --- */
410
+ 'condition'?: 'NX' | 'XX';
411
+ /** --- 分数比较条件:GT 仅新分数大于旧分数时更新,LT 仅新分数小于旧分数时更新(不影响新增成员) --- */
412
+ 'comparison'?: 'GT' | 'LT';
413
+ /** --- 返回值改为受影响总数(新增 + 分数变化的成员数),默认仅返回新增成员数 --- */
414
+ 'CH'?: boolean;
415
+ /** --- 改为在原分数上累加(类似 ZINCRBY,仅支持单个成员),返回更新后的新分数 --- */
416
+ 'INCR'?: boolean;
417
+ }
418
+ /** --- Redis 连接缓存项 --- */
326
419
  export interface IConnectionInfo {
420
+ /** --- 服务端地址(IP 或域名) --- */
327
421
  'host': string;
422
+ /** --- 服务端端口 --- */
328
423
  'port': number;
424
+ /** --- 实际选择的数据库编号 --- */
329
425
  'index': number;
330
- 'conn': redis.ICommandClient;
426
+ /** --- ACL 用户名 --- */
427
+ 'user': string;
428
+ /** --- 认证密码或 Kvrocks namespace token --- */
429
+ 'pwd': string;
430
+ /** --- TLS 配置区分键(空串表示明文,'1' 表示默认 TLS,其余为对象序列化键) --- */
431
+ 'tls': string;
432
+ /** --- 并发请求等待同一次建连,成功返回客户端,失败返回 null --- */
433
+ 'ready': Promise<TRedisClient | null>;
331
434
  }