@maiyunnet/kebab 10.1.0 → 10.1.1

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.
@@ -0,0 +1,3 @@
1
+ import type { Kv } from '#kebab/lib/kv.js';
2
+ import type { ICheckOptions, ICheckResult } from '#kebab/lib/ratelimit.js';
3
+ export declare function checkRate(kv: Kv, key: string, opt: ICheckOptions, mode: 'fixed' | 'sliding'): Promise<ICheckResult>;
@@ -0,0 +1,84 @@
1
+ const FIXED = `
2
+ local ttl = redis.call('PTTL', KEYS[1])
3
+ local count = 1
4
+ if ttl < 0 then
5
+ if ttl == -1 and not string.match(redis.call('GET', KEYS[1]), '^%d+$') then
6
+ return redis.error_reply('ERR invalid fixed rate limit counter')
7
+ end
8
+ redis.call('SET', KEYS[1], '1', 'PX', ARGV[1])
9
+ ttl = tonumber(ARGV[1])
10
+ else
11
+ count = redis.call('INCR', KEYS[1])
12
+ end
13
+ local time = redis.call('TIME')
14
+ local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
15
+ return { count, math.ceil((now + ttl) / 1000) }
16
+ `;
17
+ const SLIDING = `
18
+ if redis.replicate_commands then
19
+ redis.replicate_commands()
20
+ end
21
+ local window = tonumber(ARGV[1])
22
+ local limit = tonumber(ARGV[2])
23
+ local width = math.floor(window / 6)
24
+ local time = redis.call('TIME')
25
+ local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
26
+ local first = math.floor((now - window) / width)
27
+ local current = math.floor(now / width)
28
+ local stored = redis.call('GET', KEYS[1])
29
+ local previous = {}
30
+ if stored then
31
+ local ok, decoded = pcall(cjson.decode, stored)
32
+ if not ok or type(decoded) ~= 'table' then
33
+ return redis.error_reply('ERR invalid sliding rate limit counter')
34
+ end
35
+ previous = decoded
36
+ end
37
+ local segments = {}
38
+ local total = 0
39
+ for id = first, current do
40
+ local field = tostring(id)
41
+ local count = previous[field] or 0
42
+ if id == current then
43
+ count = count + 1
44
+ end
45
+ if count > 0 then
46
+ segments[field] = count
47
+ total = total + count
48
+ end
49
+ end
50
+ redis.call('SET', KEYS[1], cjson.encode(segments), 'PX', (current + 1) * width + window - now)
51
+
52
+ local required = math.max(1, total - limit + 1)
53
+ for id = first, current do
54
+ required = required - (segments[tostring(id)] or 0)
55
+ if required <= 0 then
56
+ return { total, math.ceil(((id + 1) * width + window) / 1000) }
57
+ end
58
+ end
59
+ `;
60
+ function normalize(value, scale) {
61
+ const integer = Math.floor(value ?? 60);
62
+ return Number.isSafeInteger(integer * scale) ? Math.max(1, integer) : 60;
63
+ }
64
+ export async function checkRate(kv, key, opt, mode) {
65
+ const window = normalize(opt.window, 1000);
66
+ const limit = normalize(opt.max, 1);
67
+ const subject = (opt.pre ?? 'rl:') + key;
68
+ const prefix = kv.getPre();
69
+ const milliseconds = String(window * 1000);
70
+ let result;
71
+ if (mode === 'fixed') {
72
+ result = await kv.eval(FIXED, [prefix + subject], [milliseconds]);
73
+ }
74
+ else {
75
+ result = await kv.eval(SLIDING, [`${prefix}${subject}:sliding:${window}`], [milliseconds, String(limit)]);
76
+ }
77
+ if (Array.isArray(result) && result.length === 2 &&
78
+ Number.isSafeInteger(result[0]) && result[0] > 0 && Number.isSafeInteger(result[1]) && result[1] > 0) {
79
+ const [count, reset] = result;
80
+ return { allowed: count <= limit, remaining: Math.max(0, limit - count), limit, reset };
81
+ }
82
+ const allowed = !opt.failClosed;
83
+ return { allowed, remaining: allowed ? limit : 0, limit, reset: Math.ceil(Date.now() / 1000) + window, unavailable: true };
84
+ }
package/doc/kebab-rag.md CHANGED
@@ -1945,7 +1945,7 @@ index/variables/VER.md
1945
1945
 
1946
1946
  # Variable: VER
1947
1947
 
1948
- > `const` **VER**: `"10.1.0"` = `'10.1.0'`
1948
+ > `const` **VER**: `"10.1.1"` = `'10.1.1'`
1949
1949
 
1950
1950
  Defined in: [index.ts:10](https://github.com/maiyunnet/kebab/blob/master/index.ts#L10)
1951
1951
 
@@ -120252,7 +120252,7 @@ lib/kv/classes/Kv.md
120252
120252
 
120253
120253
  # Class: Kv
120254
120254
 
120255
- Defined in: [lib/kv.ts:126](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L126)
120255
+ Defined in: [lib/kv.ts:161](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L161)
120256
120256
 
120257
120257
  键值存储操作类(兼容 Redis 与 Kvrocks)
120258
120258
 
@@ -120272,7 +120272,7 @@ const res = await kv.replace('test', 111);
120272
120272
 
120273
120273
  > **new Kv**(`etc`): `Kv`
120274
120274
 
120275
- Defined in: [lib/kv.ts:137](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L137)
120275
+ Defined in: [lib/kv.ts:172](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L172)
120276
120276
 
120277
120277
  #### Parameters
120278
120278
 
@@ -120290,7 +120290,7 @@ Defined in: [lib/kv.ts:137](https://github.com/maiyunnet/kebab/blob/master/lib/k
120290
120290
 
120291
120291
  > **add**(`key`, `val`, `ttl?`): `Promise`\<`boolean`\>
120292
120292
 
120293
- Defined in: [lib/kv.ts:250](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L250)
120293
+ Defined in: [lib/kv.ts:306](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L306)
120294
120294
 
120295
120295
  添加一个值,存在则不变
120296
120296
 
@@ -120324,7 +120324,7 @@ Defined in: [lib/kv.ts:250](https://github.com/maiyunnet/kebab/blob/master/lib/k
120324
120324
 
120325
120325
  > **append**(`key`, `val`): `Promise`\<`boolean`\>
120326
120326
 
120327
- Defined in: [lib/kv.ts:277](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L277)
120327
+ Defined in: [lib/kv.ts:333](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L333)
120328
120328
 
120329
120329
  向已存在的值后追加数据
120330
120330
 
@@ -120352,7 +120352,7 @@ Defined in: [lib/kv.ts:277](https://github.com/maiyunnet/kebab/blob/master/lib/k
120352
120352
 
120353
120353
  > **bLMove**(`sourceKey`, `destKey`, `soo`, `deo`, `timeout`): `Promise`\<`string` \| `false` \| `null`\>
120354
120354
 
120355
- Defined in: [lib/kv.ts:943](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L943)
120355
+ Defined in: [lib/kv.ts:999](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L999)
120356
120356
 
120357
120357
  阻塞式在两个列表间移动元素(使用独立连接,避免阻塞共享连接)
120358
120358
 
@@ -120398,7 +120398,7 @@ Defined in: [lib/kv.ts:943](https://github.com/maiyunnet/kebab/blob/master/lib/k
120398
120398
 
120399
120399
  > **bRPop**(`key`, `timeout`): `Promise`\<`false` \| `Record`\<`string`, `string`\>\>
120400
120400
 
120401
- Defined in: [lib/kv.ts:994](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L994)
120401
+ Defined in: [lib/kv.ts:1050](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1050)
120402
120402
 
120403
120403
  阻塞式从列表右侧弹出元素(使用独立连接,避免阻塞共享连接)
120404
120404
 
@@ -120426,7 +120426,7 @@ Defined in: [lib/kv.ts:994](https://github.com/maiyunnet/kebab/blob/master/lib/k
120426
120426
 
120427
120427
  > **decr**(`key`, `num?`): `Promise`\<`number` \| `false`\>
120428
120428
 
120429
- Defined in: [lib/kv.ts:534](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L534)
120429
+ Defined in: [lib/kv.ts:590](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L590)
120430
120430
 
120431
120431
  自减
120432
120432
 
@@ -120454,7 +120454,7 @@ Defined in: [lib/kv.ts:534](https://github.com/maiyunnet/kebab/blob/master/lib/k
120454
120454
 
120455
120455
  > **del**(`keys`): `Promise`\<`boolean`\>
120456
120456
 
120457
- Defined in: [lib/kv.ts:484](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L484)
120457
+ Defined in: [lib/kv.ts:540](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L540)
120458
120458
 
120459
120459
  删除已存在的值
120460
120460
 
@@ -120476,9 +120476,9 @@ Defined in: [lib/kv.ts:484](https://github.com/maiyunnet/kebab/blob/master/lib/k
120476
120476
 
120477
120477
  > **eval**(`script`, `keys`, `args`): `Promise`\<`unknown`\>
120478
120478
 
120479
- Defined in: [lib/kv.ts:185](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L185)
120479
+ Defined in: [lib/kv.ts:220](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L220)
120480
120480
 
120481
- 在共享连接上执行一个 Redis Lua 脚本
120481
+ 在共享连接上执行一个 Redis Lua 脚本,首次走 EVAL 加载,后续走 EVALSHA 并在 NOSCRIPT 时自动回退
120482
120482
 
120483
120483
  #### Parameters
120484
120484
 
@@ -120512,7 +120512,7 @@ Redis 原始结果,nil 返回 null,连接或执行失败返回 false
120512
120512
 
120513
120513
  > **exists**(`keys`): `Promise`\<`number`\>
120514
120514
 
120515
- Defined in: [lib/kv.ts:309](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L309)
120515
+ Defined in: [lib/kv.ts:365](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L365)
120516
120516
 
120517
120517
  检测 key 是否存在
120518
120518
 
@@ -120534,7 +120534,7 @@ Defined in: [lib/kv.ts:309](https://github.com/maiyunnet/kebab/blob/master/lib/k
120534
120534
 
120535
120535
  > **expire**(`key`, `ttl`): `Promise`\<`boolean`\>
120536
120536
 
120537
- Defined in: [lib/kv.ts:562](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L562)
120537
+ Defined in: [lib/kv.ts:618](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L618)
120538
120538
 
120539
120539
  仅修改过期时间不修改值
120540
120540
 
@@ -120562,7 +120562,7 @@ Defined in: [lib/kv.ts:562](https://github.com/maiyunnet/kebab/blob/master/lib/k
120562
120562
 
120563
120563
  > **flushDb**(): `Promise`\<`boolean`\>
120564
120564
 
120565
- Defined in: [lib/kv.ts:632](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L632)
120565
+ Defined in: [lib/kv.ts:688](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L688)
120566
120566
 
120567
120567
  清除当前所选数据库的所有内容
120568
120568
 
@@ -120576,7 +120576,7 @@ Defined in: [lib/kv.ts:632](https://github.com/maiyunnet/kebab/blob/master/lib/k
120576
120576
 
120577
120577
  > **get**(`key`): `Promise`\<`string` \| `false` \| `null`\>
120578
120578
 
120579
- Defined in: [lib/kv.ts:331](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L331)
120579
+ Defined in: [lib/kv.ts:387](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L387)
120580
120580
 
120581
120581
  获取字符串
120582
120582
 
@@ -120600,7 +120600,7 @@ Defined in: [lib/kv.ts:331](https://github.com/maiyunnet/kebab/blob/master/lib/k
120600
120600
 
120601
120601
  > **getClient**(): `Promise`\<[`TRedisClient`](../type-aliases/TRedisClient.md) \| `null`\>
120602
120602
 
120603
- Defined in: [lib/kv.ts:157](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L157)
120603
+ Defined in: [lib/kv.ts:192](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L192)
120604
120604
 
120605
120605
  获取底层官方 redis 客户端实例(用于执行原生扩展命令)
120606
120606
 
@@ -120618,7 +120618,7 @@ Defined in: [lib/kv.ts:157](https://github.com/maiyunnet/kebab/blob/master/lib/k
120618
120618
 
120619
120619
  > **getDelJson**(`key`): `Promise`\<`any`\>
120620
120620
 
120621
- Defined in: [lib/kv.ts:455](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L455)
120621
+ Defined in: [lib/kv.ts:511](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L511)
120622
120622
 
120623
120623
  原子读取并删除键,再解析 JSON
120624
120624
 
@@ -120640,7 +120640,7 @@ Defined in: [lib/kv.ts:455](https://github.com/maiyunnet/kebab/blob/master/lib/k
120640
120640
 
120641
120641
  > **getDelJson**\<`T`\>(`key`): `Promise`\<`false` \| `T` \| `null`\>
120642
120642
 
120643
- Defined in: [lib/kv.ts:462](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L462)
120643
+ Defined in: [lib/kv.ts:518](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L518)
120644
120644
 
120645
120645
  原子读取并删除键,再解析 JSON
120646
120646
 
@@ -120674,7 +120674,7 @@ Defined in: [lib/kv.ts:462](https://github.com/maiyunnet/kebab/blob/master/lib/k
120674
120674
 
120675
120675
  > **getJson**(`key`): `Promise`\<`any`\>
120676
120676
 
120677
- Defined in: [lib/kv.ts:434](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L434)
120677
+ Defined in: [lib/kv.ts:490](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L490)
120678
120678
 
120679
120679
  读取并解析键中的 JSON
120680
120680
 
@@ -120696,7 +120696,7 @@ Defined in: [lib/kv.ts:434](https://github.com/maiyunnet/kebab/blob/master/lib/k
120696
120696
 
120697
120697
  > **getJson**\<`T`\>(`key`): `Promise`\<`false` \| `T` \| `null`\>
120698
120698
 
120699
- Defined in: [lib/kv.ts:441](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L441)
120699
+ Defined in: [lib/kv.ts:497](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L497)
120700
120700
 
120701
120701
  读取并解析键中的 JSON
120702
120702
 
@@ -120728,7 +120728,7 @@ Defined in: [lib/kv.ts:441](https://github.com/maiyunnet/kebab/blob/master/lib/k
120728
120728
 
120729
120729
  > **getPre**(): `string`
120730
120730
 
120731
- Defined in: [lib/kv.ts:149](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L149)
120731
+ Defined in: [lib/kv.ts:184](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L184)
120732
120732
 
120733
120733
  获取当前 Kv 实际生效的键名前缀
120734
120734
 
@@ -120744,7 +120744,7 @@ Defined in: [lib/kv.ts:149](https://github.com/maiyunnet/kebab/blob/master/lib/k
120744
120744
 
120745
120745
  > **hDel**(`key`, `fields`): `Promise`\<`number` \| `false`\>
120746
120746
 
120747
- Defined in: [lib/kv.ts:818](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L818)
120747
+ Defined in: [lib/kv.ts:874](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L874)
120748
120748
 
120749
120749
  删除哈希键
120750
120750
 
@@ -120772,7 +120772,7 @@ Defined in: [lib/kv.ts:818](https://github.com/maiyunnet/kebab/blob/master/lib/k
120772
120772
 
120773
120773
  > **hExists**(`key`, `field`): `Promise`\<`boolean`\>
120774
120774
 
120775
- Defined in: [lib/kv.ts:839](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L839)
120775
+ Defined in: [lib/kv.ts:895](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L895)
120776
120776
 
120777
120777
  判断哈希字段是否存在
120778
120778
 
@@ -120800,7 +120800,7 @@ Defined in: [lib/kv.ts:839](https://github.com/maiyunnet/kebab/blob/master/lib/k
120800
120800
 
120801
120801
  > **hGet**(`key`, `field`): `Promise`\<`string` \| `false` \| `null`\>
120802
120802
 
120803
- Defined in: [lib/kv.ts:729](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L729)
120803
+ Defined in: [lib/kv.ts:785](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L785)
120804
120804
 
120805
120805
  获取哈希值
120806
120806
 
@@ -120828,7 +120828,7 @@ Defined in: [lib/kv.ts:729](https://github.com/maiyunnet/kebab/blob/master/lib/k
120828
120828
 
120829
120829
  > **hGetAll**(`key`): `Promise`\<`false` \| `Record`\<`string`, `string` \| `null`\>\>
120830
120830
 
120831
- Defined in: [lib/kv.ts:800](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L800)
120831
+ Defined in: [lib/kv.ts:856](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L856)
120832
120832
 
120833
120833
  批量获取哈希键值对
120834
120834
 
@@ -120852,7 +120852,7 @@ Defined in: [lib/kv.ts:800](https://github.com/maiyunnet/kebab/blob/master/lib/k
120852
120852
 
120853
120853
  > **hGetJson**(`key`, `field`): `Promise`\<`any`\>
120854
120854
 
120855
- Defined in: [lib/kv.ts:749](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L749)
120855
+ Defined in: [lib/kv.ts:805](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L805)
120856
120856
 
120857
120857
  读取并解析哈希字段中的 JSON
120858
120858
 
@@ -120880,7 +120880,7 @@ Defined in: [lib/kv.ts:749](https://github.com/maiyunnet/kebab/blob/master/lib/k
120880
120880
 
120881
120881
  > **hGetJson**\<`T`\>(`key`, `field`): `Promise`\<`false` \| `T` \| `null`\>
120882
120882
 
120883
- Defined in: [lib/kv.ts:757](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L757)
120883
+ Defined in: [lib/kv.ts:813](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L813)
120884
120884
 
120885
120885
  读取并解析哈希字段中的 JSON
120886
120886
 
@@ -120918,7 +120918,7 @@ Defined in: [lib/kv.ts:757](https://github.com/maiyunnet/kebab/blob/master/lib/k
120918
120918
 
120919
120919
  > **hIncr**(`key`, `field`, `increment`): `Promise`\<`number` \| `false`\>
120920
120920
 
120921
- Defined in: [lib/kv.ts:858](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L858)
120921
+ Defined in: [lib/kv.ts:914](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L914)
120922
120922
 
120923
120923
  设置哈希自增自减
120924
120924
 
@@ -120952,7 +120952,7 @@ key
120952
120952
 
120953
120953
  > **hKeys**(`key`): `Promise`\<`false` \| `string`[]\>
120954
120954
 
120955
- Defined in: [lib/kv.ts:880](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L880)
120955
+ Defined in: [lib/kv.ts:936](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L936)
120956
120956
 
120957
120957
  获取哈希所有字段
120958
120958
 
@@ -120974,7 +120974,7 @@ Defined in: [lib/kv.ts:880](https://github.com/maiyunnet/kebab/blob/master/lib/k
120974
120974
 
120975
120975
  > **hMGet**(`key`, `fields`): `Promise`\<`false` \| `Record`\<`string`, `string` \| `null`\>\>
120976
120976
 
120977
- Defined in: [lib/kv.ts:775](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L775)
120977
+ Defined in: [lib/kv.ts:831](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L831)
120978
120978
 
120979
120979
  批量获取哈希值
120980
120980
 
@@ -121002,7 +121002,7 @@ Defined in: [lib/kv.ts:775](https://github.com/maiyunnet/kebab/blob/master/lib/k
121002
121002
 
121003
121003
  > **hMSet**(`key`, `rows`): `Promise`\<`boolean`\>
121004
121004
 
121005
- Defined in: [lib/kv.ts:699](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L699)
121005
+ Defined in: [lib/kv.ts:755](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L755)
121006
121006
 
121007
121007
  批量设置哈希值
121008
121008
 
@@ -121030,7 +121030,7 @@ field / val 对象
121030
121030
 
121031
121031
  > **hSet**(`key`, `field`, `val`, `mod?`): `Promise`\<`boolean`\>
121032
121032
 
121033
- Defined in: [lib/kv.ts:669](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L669)
121033
+ Defined in: [lib/kv.ts:725](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L725)
121034
121034
 
121035
121035
  设置哈希表值
121036
121036
 
@@ -121070,7 +121070,7 @@ key 名
121070
121070
 
121071
121071
  > **incr**(`key`, `num?`): `Promise`\<`number` \| `false`\>
121072
121072
 
121073
- Defined in: [lib/kv.ts:506](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L506)
121073
+ Defined in: [lib/kv.ts:562](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L562)
121074
121074
 
121075
121075
  自增
121076
121076
 
@@ -121098,7 +121098,7 @@ Defined in: [lib/kv.ts:506](https://github.com/maiyunnet/kebab/blob/master/lib/k
121098
121098
 
121099
121099
  > **keys**(`pattern`): `Promise`\<`false` \| `string`[]\>
121100
121100
 
121101
- Defined in: [lib/kv.ts:579](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L579)
121101
+ Defined in: [lib/kv.ts:635](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L635)
121102
121102
 
121103
121103
  获取服务器上的所有 key 列表
121104
121104
 
@@ -121120,7 +121120,7 @@ Defined in: [lib/kv.ts:579](https://github.com/maiyunnet/kebab/blob/master/lib/k
121120
121120
 
121121
121121
  > **lLen**(`key`): `Promise`\<`number` \| `false`\>
121122
121122
 
121123
- Defined in: [lib/kv.ts:1035](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1035)
121123
+ Defined in: [lib/kv.ts:1091](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1091)
121124
121124
 
121125
121125
  获取列表长度
121126
121126
 
@@ -121142,7 +121142,7 @@ Defined in: [lib/kv.ts:1035](https://github.com/maiyunnet/kebab/blob/master/lib/
121142
121142
 
121143
121143
  > **lPop**(`key`): `Promise`\<`string` \| `false` \| `null`\>
121144
121144
 
121145
- Defined in: [lib/kv.ts:959](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L959)
121145
+ Defined in: [lib/kv.ts:1015](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1015)
121146
121146
 
121147
121147
  从列表左侧弹出元素
121148
121148
 
@@ -121164,7 +121164,7 @@ Defined in: [lib/kv.ts:959](https://github.com/maiyunnet/kebab/blob/master/lib/k
121164
121164
 
121165
121165
  > **lPush**(`key`, `values`): `Promise`\<`number` \| `false`\>
121166
121166
 
121167
- Defined in: [lib/kv.ts:898](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L898)
121167
+ Defined in: [lib/kv.ts:954](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L954)
121168
121168
 
121169
121169
  从列表左侧推入元素
121170
121170
 
@@ -121192,7 +121192,7 @@ Defined in: [lib/kv.ts:898](https://github.com/maiyunnet/kebab/blob/master/lib/k
121192
121192
 
121193
121193
  > **lRange**(`key`, `start`, `stop`): `Promise`\<`false` \| `string`[]\>
121194
121194
 
121195
- Defined in: [lib/kv.ts:1018](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1018)
121195
+ Defined in: [lib/kv.ts:1074](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1074)
121196
121196
 
121197
121197
  获取列表指定区间元素
121198
121198
 
@@ -121226,7 +121226,7 @@ Defined in: [lib/kv.ts:1018](https://github.com/maiyunnet/kebab/blob/master/lib/
121226
121226
 
121227
121227
  > **mGet**(`keys`): `Promise`\<`false` \| `Record`\<`string`, `string` \| `null`\>\>
121228
121228
 
121229
- Defined in: [lib/kv.ts:382](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L382)
121229
+ Defined in: [lib/kv.ts:438](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L438)
121230
121230
 
121231
121231
  批量获取值
121232
121232
 
@@ -121248,7 +121248,7 @@ key 序列
121248
121248
 
121249
121249
  > **mSet**(`rows`): `Promise`\<`boolean`\>
121250
121250
 
121251
- Defined in: [lib/kv.ts:408](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L408)
121251
+ Defined in: [lib/kv.ts:464](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L464)
121252
121252
 
121253
121253
  批量设置键值
121254
121254
 
@@ -121270,7 +121270,7 @@ key / val 对象
121270
121270
 
121271
121271
  > **ping**(): `Promise`\<`string` \| `false`\>
121272
121272
 
121273
- Defined in: [lib/kv.ts:649](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L649)
121273
+ Defined in: [lib/kv.ts:705](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L705)
121274
121274
 
121275
121275
  发送 ping
121276
121276
 
@@ -121284,7 +121284,7 @@ Defined in: [lib/kv.ts:649](https://github.com/maiyunnet/kebab/blob/master/lib/k
121284
121284
 
121285
121285
  > **pipeline**(): `Promise`\<`any`\>
121286
121286
 
121287
- Defined in: [lib/kv.ts:165](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L165)
121287
+ Defined in: [lib/kv.ts:200](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L200)
121288
121288
 
121289
121289
  获取一个绑定在共享连接上的原生 multi / pipeline 对象(通过 execAsPipeline() 或 exec() 执行,无需关闭)
121290
121290
 
@@ -121300,7 +121300,7 @@ Defined in: [lib/kv.ts:165](https://github.com/maiyunnet/kebab/blob/master/lib/k
121300
121300
 
121301
121301
  > **prepend**(`key`, `val`): `Promise`\<`boolean`\>
121302
121302
 
121303
- Defined in: [lib/kv.ts:295](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L295)
121303
+ Defined in: [lib/kv.ts:351](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L351)
121304
121304
 
121305
121305
  向已存在的值之前追加数据
121306
121306
 
@@ -121328,7 +121328,7 @@ Defined in: [lib/kv.ts:295](https://github.com/maiyunnet/kebab/blob/master/lib/k
121328
121328
 
121329
121329
  > **pttl**(`key`): `Promise`\<`number` \| `null`\>
121330
121330
 
121331
- Defined in: [lib/kv.ts:365](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L365)
121331
+ Defined in: [lib/kv.ts:421](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L421)
121332
121332
 
121333
121333
  获取相应的剩余有效期毫秒数
121334
121334
 
@@ -121350,7 +121350,7 @@ Defined in: [lib/kv.ts:365](https://github.com/maiyunnet/kebab/blob/master/lib/k
121350
121350
 
121351
121351
  > **replace**(`key`, `val`, `ttl?`): `Promise`\<`boolean`\>
121352
121352
 
121353
- Defined in: [lib/kv.ts:264](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L264)
121353
+ Defined in: [lib/kv.ts:320](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L320)
121354
121354
 
121355
121355
  替换一个存在的值
121356
121356
 
@@ -121384,7 +121384,7 @@ Defined in: [lib/kv.ts:264](https://github.com/maiyunnet/kebab/blob/master/lib/k
121384
121384
 
121385
121385
  > **rPop**(`key`): `Promise`\<`string` \| `false` \| `null`\>
121386
121386
 
121387
- Defined in: [lib/kv.ts:976](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L976)
121387
+ Defined in: [lib/kv.ts:1032](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1032)
121388
121388
 
121389
121389
  从列表右侧弹出元素
121390
121390
 
@@ -121406,7 +121406,7 @@ Defined in: [lib/kv.ts:976](https://github.com/maiyunnet/kebab/blob/master/lib/k
121406
121406
 
121407
121407
  > **rPush**(`key`, `values`): `Promise`\<`number` \| `false`\>
121408
121408
 
121409
- Defined in: [lib/kv.ts:919](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L919)
121409
+ Defined in: [lib/kv.ts:975](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L975)
121410
121410
 
121411
121411
  从列表右侧推入元素
121412
121412
 
@@ -121434,7 +121434,7 @@ Defined in: [lib/kv.ts:919](https://github.com/maiyunnet/kebab/blob/master/lib/k
121434
121434
 
121435
121435
  > **scan**(`cursor?`, `pattern?`, `count?`): `Promise`\<`false` \| [`IScanResult`](../interfaces/IScanResult.md)\<`string`\>\>
121436
121436
 
121437
- Defined in: [lib/kv.ts:603](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L603)
121437
+ Defined in: [lib/kv.ts:659](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L659)
121438
121438
 
121439
121439
  根据条件获取服务器上的 keys(兼容 Redis 数字串游标与 Kvrocks 字符串游标)
121440
121440
 
@@ -121468,7 +121468,7 @@ Defined in: [lib/kv.ts:603](https://github.com/maiyunnet/kebab/blob/master/lib/k
121468
121468
 
121469
121469
  > **set**(`key`, `val`, `ttl?`, `mod?`): `Promise`\<`boolean`\>
121470
121470
 
121471
- Defined in: [lib/kv.ts:208](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L208)
121471
+ Defined in: [lib/kv.ts:264](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L264)
121472
121472
 
121473
121473
  设定一个值
121474
121474
 
@@ -121508,7 +121508,7 @@ Defined in: [lib/kv.ts:208](https://github.com/maiyunnet/kebab/blob/master/lib/k
121508
121508
 
121509
121509
  > **ttl**(`key`): `Promise`\<`number` \| `null`\>
121510
121510
 
121511
- Defined in: [lib/kv.ts:348](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L348)
121511
+ Defined in: [lib/kv.ts:404](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L404)
121512
121512
 
121513
121513
  获取相应的剩余有效期秒数
121514
121514
 
@@ -121534,7 +121534,7 @@ Defined in: [lib/kv.ts:348](https://github.com/maiyunnet/kebab/blob/master/lib/k
121534
121534
 
121535
121535
  > **zAdd**(`key`, `score`, `member`): `Promise`\<`boolean`\>
121536
121536
 
121537
- Defined in: [lib/kv.ts:1054](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1054)
121537
+ Defined in: [lib/kv.ts:1110](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1110)
121538
121538
 
121539
121539
  添加有序集合元素(单个元素)
121540
121540
 
@@ -121566,7 +121566,7 @@ key 名
121566
121566
 
121567
121567
  > **zAdd**(`key`, `elements`, `options`): `Promise`\<`number` \| `false` \| `null`\>
121568
121568
 
121569
- Defined in: [lib/kv.ts:1061](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1061)
121569
+ Defined in: [lib/kv.ts:1117](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1117)
121570
121570
 
121571
121571
  添加有序集合元素(多个元素,含 INCR 选项)
121572
121572
 
@@ -121598,7 +121598,7 @@ key 名
121598
121598
 
121599
121599
  > **zAdd**(`key`, `elements`, `options?`): `Promise`\<`number` \| `false`\>
121600
121600
 
121601
- Defined in: [lib/kv.ts:1072](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1072)
121601
+ Defined in: [lib/kv.ts:1128](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1128)
121602
121602
 
121603
121603
  添加有序集合元素(多个元素)
121604
121604
 
@@ -121632,7 +121632,7 @@ key 名
121632
121632
 
121633
121633
  > **zRangeWithScores**(`key`, `start`, `stop`, `options?`): `Promise`\<`false` \| `object`[]\>
121634
121634
 
121635
- Defined in: [lib/kv.ts:1136](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1136)
121635
+ Defined in: [lib/kv.ts:1192](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1192)
121636
121636
 
121637
121637
  查询有序集合成员及分数
121638
121638
 
@@ -121672,7 +121672,7 @@ Defined in: [lib/kv.ts:1136](https://github.com/maiyunnet/kebab/blob/master/lib/
121672
121672
 
121673
121673
  > **zRem**(`key`, `members`): `Promise`\<`number` \| `false`\>
121674
121674
 
121675
- Defined in: [lib/kv.ts:1183](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1183)
121675
+ Defined in: [lib/kv.ts:1239](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1239)
121676
121676
 
121677
121677
  移除有序集合成员
121678
121678
 
@@ -121707,7 +121707,7 @@ lib/kv/functions/get.md
121707
121707
 
121708
121708
  > **get**(`ctrEtc`, `oetc?`): [`Kv`](../classes/Kv.md)
121709
121709
 
121710
- Defined in: [lib/kv.ts:1348](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1348)
121710
+ Defined in: [lib/kv.ts:1404](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1404)
121711
121711
 
121712
121712
  获取 Kv 对象
121713
121713
 
@@ -121805,7 +121805,7 @@ lib/kv/interfaces/IConnectionInfo.md
121805
121805
 
121806
121806
  # Interface: IConnectionInfo
121807
121807
 
121808
- Defined in: [lib/kv.ts:1412](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1412)
121808
+ Defined in: [lib/kv.ts:1468](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1468)
121809
121809
 
121810
121810
  Redis 连接缓存项
121811
121811
 
@@ -121815,7 +121815,7 @@ Redis 连接缓存项
121815
121815
 
121816
121816
  > **host**: `string`
121817
121817
 
121818
- Defined in: [lib/kv.ts:1414](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1414)
121818
+ Defined in: [lib/kv.ts:1470](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1470)
121819
121819
 
121820
121820
  服务端地址(IP 或域名)
121821
121821
 
@@ -121825,7 +121825,7 @@ Defined in: [lib/kv.ts:1414](https://github.com/maiyunnet/kebab/blob/master/lib/
121825
121825
 
121826
121826
  > **index**: `number`
121827
121827
 
121828
- Defined in: [lib/kv.ts:1418](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1418)
121828
+ Defined in: [lib/kv.ts:1474](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1474)
121829
121829
 
121830
121830
  实际选择的数据库编号
121831
121831
 
@@ -121835,7 +121835,7 @@ Defined in: [lib/kv.ts:1418](https://github.com/maiyunnet/kebab/blob/master/lib/
121835
121835
 
121836
121836
  > **port**: `number`
121837
121837
 
121838
- Defined in: [lib/kv.ts:1416](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1416)
121838
+ Defined in: [lib/kv.ts:1472](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1472)
121839
121839
 
121840
121840
  服务端端口
121841
121841
 
@@ -121845,7 +121845,7 @@ Defined in: [lib/kv.ts:1416](https://github.com/maiyunnet/kebab/blob/master/lib/
121845
121845
 
121846
121846
  > **pwd**: `string`
121847
121847
 
121848
- Defined in: [lib/kv.ts:1422](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1422)
121848
+ Defined in: [lib/kv.ts:1478](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1478)
121849
121849
 
121850
121850
  认证密码或 Kvrocks namespace token
121851
121851
 
@@ -121855,7 +121855,7 @@ Defined in: [lib/kv.ts:1422](https://github.com/maiyunnet/kebab/blob/master/lib/
121855
121855
 
121856
121856
  > **ready**: `Promise`\<[`TRedisClient`](../type-aliases/TRedisClient.md) \| `null`\>
121857
121857
 
121858
- Defined in: [lib/kv.ts:1426](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1426)
121858
+ Defined in: [lib/kv.ts:1482](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1482)
121859
121859
 
121860
121860
  并发请求等待同一次建连,成功返回客户端,失败返回 null
121861
121861
 
@@ -121865,7 +121865,7 @@ Defined in: [lib/kv.ts:1426](https://github.com/maiyunnet/kebab/blob/master/lib/
121865
121865
 
121866
121866
  > **tls**: `string`
121867
121867
 
121868
- Defined in: [lib/kv.ts:1424](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1424)
121868
+ Defined in: [lib/kv.ts:1480](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1480)
121869
121869
 
121870
121870
  TLS 配置区分键(空串表示明文,'1' 表示默认 TLS,其余为对象序列化键)
121871
121871
 
@@ -121875,7 +121875,7 @@ TLS 配置区分键(空串表示明文,'1' 表示默认 TLS,其余为对
121875
121875
 
121876
121876
  > **user**: `string`
121877
121877
 
121878
- Defined in: [lib/kv.ts:1420](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1420)
121878
+ Defined in: [lib/kv.ts:1476](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1476)
121879
121879
 
121880
121880
  ACL 用户名
121881
121881
 
@@ -121890,7 +121890,7 @@ lib/kv/interfaces/IScanResult.md
121890
121890
 
121891
121891
  # Interface: IScanResult\<T\>
121892
121892
 
121893
- Defined in: [lib/kv.ts:1380](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1380)
121893
+ Defined in: [lib/kv.ts:1436](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1436)
121894
121894
 
121895
121895
  SCAN 分页结果
121896
121896
 
@@ -121906,7 +121906,7 @@ SCAN 分页结果
121906
121906
 
121907
121907
  > **cursor**: `string`
121908
121908
 
121909
- Defined in: [lib/kv.ts:1382](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1382)
121909
+ Defined in: [lib/kv.ts:1438](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1438)
121910
121910
 
121911
121911
  下一页游标('0' 表示结束;兼容 Redis 数字串与 Kvrocks 原生字符串游标)
121912
121912
 
@@ -121916,7 +121916,7 @@ Defined in: [lib/kv.ts:1382](https://github.com/maiyunnet/kebab/blob/master/lib/
121916
121916
 
121917
121917
  > **items**: `T`[]
121918
121918
 
121919
- Defined in: [lib/kv.ts:1384](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1384)
121919
+ Defined in: [lib/kv.ts:1440](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1440)
121920
121920
 
121921
121921
  当前页匹配到的键名列表(已去除项目前缀)
121922
121922
 
@@ -121931,7 +121931,7 @@ lib/kv/interfaces/IZAddOptions.md
121931
121931
 
121932
121932
  # Interface: IZAddOptions
121933
121933
 
121934
- Defined in: [lib/kv.ts:1400](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1400)
121934
+ Defined in: [lib/kv.ts:1456](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1456)
121935
121935
 
121936
121936
  ZADD 选项
121937
121937
 
@@ -121941,7 +121941,7 @@ ZADD 选项
121941
121941
 
121942
121942
  > `optional` **CH?**: `boolean`
121943
121943
 
121944
- Defined in: [lib/kv.ts:1406](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1406)
121944
+ Defined in: [lib/kv.ts:1462](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1462)
121945
121945
 
121946
121946
  返回值改为受影响总数(新增 + 分数变化的成员数),默认仅返回新增成员数
121947
121947
 
@@ -121951,7 +121951,7 @@ Defined in: [lib/kv.ts:1406](https://github.com/maiyunnet/kebab/blob/master/lib/
121951
121951
 
121952
121952
  > `optional` **comparison?**: `"GT"` \| `"LT"`
121953
121953
 
121954
- Defined in: [lib/kv.ts:1404](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1404)
121954
+ Defined in: [lib/kv.ts:1460](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1460)
121955
121955
 
121956
121956
  分数比较条件:GT 仅新分数大于旧分数时更新,LT 仅新分数小于旧分数时更新(不影响新增成员)
121957
121957
 
@@ -121961,7 +121961,7 @@ Defined in: [lib/kv.ts:1404](https://github.com/maiyunnet/kebab/blob/master/lib/
121961
121961
 
121962
121962
  > `optional` **condition?**: `"NX"` \| `"XX"`
121963
121963
 
121964
- Defined in: [lib/kv.ts:1402](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1402)
121964
+ Defined in: [lib/kv.ts:1458](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1458)
121965
121965
 
121966
121966
  写入条件:NX 仅添加不存在的成员,XX 仅更新已存在的成员
121967
121967
 
@@ -121971,7 +121971,7 @@ Defined in: [lib/kv.ts:1402](https://github.com/maiyunnet/kebab/blob/master/lib/
121971
121971
 
121972
121972
  > `optional` **INCR?**: `boolean`
121973
121973
 
121974
- Defined in: [lib/kv.ts:1408](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1408)
121974
+ Defined in: [lib/kv.ts:1464](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1464)
121975
121975
 
121976
121976
  改为在原分数上累加(类似 ZINCRBY,仅支持单个成员),返回更新后的新分数
121977
121977
 
@@ -121986,7 +121986,7 @@ lib/kv/interfaces/IZRangeOptions.md
121986
121986
 
121987
121987
  # Interface: IZRangeOptions
121988
121988
 
121989
- Defined in: [lib/kv.ts:1388](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1388)
121989
+ Defined in: [lib/kv.ts:1444](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1444)
121990
121990
 
121991
121991
  ZRANGE 查询选项
121992
121992
 
@@ -121996,7 +121996,7 @@ ZRANGE 查询选项
121996
121996
 
121997
121997
  > `optional` **by?**: `"SCORE"` \| `"LEX"`
121998
121998
 
121999
- Defined in: [lib/kv.ts:1390](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1390)
121999
+ Defined in: [lib/kv.ts:1446](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1446)
122000
122000
 
122001
122001
  查询方式:SCORE 按分数范围,LEX 按字典序
122002
122002
 
@@ -122006,7 +122006,7 @@ Defined in: [lib/kv.ts:1390](https://github.com/maiyunnet/kebab/blob/master/lib/
122006
122006
 
122007
122007
  > `optional` **count?**: `number`
122008
122008
 
122009
- Defined in: [lib/kv.ts:1396](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1396)
122009
+ Defined in: [lib/kv.ts:1452](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1452)
122010
122010
 
122011
122011
  分页条数,需与 offset 同用
122012
122012
 
@@ -122016,7 +122016,7 @@ Defined in: [lib/kv.ts:1396](https://github.com/maiyunnet/kebab/blob/master/lib/
122016
122016
 
122017
122017
  > `optional` **offset?**: `number`
122018
122018
 
122019
- Defined in: [lib/kv.ts:1394](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1394)
122019
+ Defined in: [lib/kv.ts:1450](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1450)
122020
122020
 
122021
122021
  分页偏移量,需与 count 同用
122022
122022
 
@@ -122026,7 +122026,7 @@ Defined in: [lib/kv.ts:1394](https://github.com/maiyunnet/kebab/blob/master/lib/
122026
122026
 
122027
122027
  > `optional` **rev?**: `boolean`
122028
122028
 
122029
- Defined in: [lib/kv.ts:1392](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1392)
122029
+ Defined in: [lib/kv.ts:1448](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1448)
122030
122030
 
122031
122031
  是否倒序返回
122032
122032
 
@@ -122043,7 +122043,7 @@ lib/kv/type-aliases/TRedisClient.md
122043
122043
 
122044
122044
  > **TRedisClient** = `redis.RedisClientType`\<`Record`\<`string`, `never`\>, `Record`\<`string`, `never`\>, `Record`\<`string`, `never`\>, `2`\>
122045
122045
 
122046
- Defined in: [lib/kv.ts:1369](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1369)
122046
+ Defined in: [lib/kv.ts:1425](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1425)
122047
122047
 
122048
122048
  底层官方 redis 客户端类型(固定 RESP2)
122049
122049
 
@@ -122060,7 +122060,7 @@ lib/kv/type-aliases/TRedisMulti.md
122060
122060
 
122061
122061
  > **TRedisMulti** = `ReturnType`\<[`TRedisClient`](TRedisClient.md)\[`"multi"`\]\>
122062
122062
 
122063
- Defined in: [lib/kv.ts:1377](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1377)
122063
+ Defined in: [lib/kv.ts:1433](https://github.com/maiyunnet/kebab/blob/master/lib/kv.ts#L1433)
122064
122064
 
122065
122065
  底层原生 multi / pipeline 类型
122066
122066
 
@@ -123696,9 +123696,9 @@ lib/ratelimit/functions/checkFixed.md
123696
123696
 
123697
123697
  > **checkFixed**(`kv`, `key`, `opt?`): `Promise`\<[`ICheckResult`](../interfaces/ICheckResult.md)\>
123698
123698
 
123699
- Defined in: [lib/ratelimit.ts:77](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L77)
123699
+ Defined in: [lib/ratelimit.ts:27](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L27)
123700
123700
 
123701
- 简易固定窗口限速检查(性能更高,精度较低)
123701
+ 检查指定 key 是否超出固定窗口限额,窗口从首次请求开始且超限请求同样计入次数
123702
123702
 
123703
123703
  ## Parameters
123704
123704
 
@@ -123716,30 +123716,16 @@ KV 实例
123716
123716
 
123717
123717
  ### opt?
123718
123718
 
123719
- 限速选项
123720
-
123721
- #### max?
123722
-
123723
- `number`
123724
-
123725
- 窗口内最大请求数,默认 60
123726
-
123727
- #### pre?
123728
-
123729
- `string`
123730
-
123731
- key 前缀,默认 rl:
123732
-
123733
- #### window?
123734
-
123735
- `number`
123719
+ [`ICheckOptions`](../interfaces/ICheckOptions.md) = `{}`
123736
123720
 
123737
- 窗口时间(秒),默认 60
123721
+ 限速选项
123738
123722
 
123739
123723
  ## Returns
123740
123724
 
123741
123725
  `Promise`\<[`ICheckResult`](../interfaces/ICheckResult.md)\>
123742
123726
 
123727
+ 检查结果;后续请求沿用首次请求设置的过期时间,Redis 无法检查时默认放行并标记 unavailable,failClosed 为 true 时拒绝
123728
+
123743
123729
  lib/ratelimit/functions/check.md
123744
123730
  ---
123745
123731
 
@@ -123753,9 +123739,9 @@ lib/ratelimit/functions/check.md
123753
123739
 
123754
123740
  > **check**(`kv`, `key`, `opt?`): `Promise`\<[`ICheckResult`](../interfaces/ICheckResult.md)\>
123755
123741
 
123756
- Defined in: [lib/ratelimit.ts:18](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L18)
123742
+ Defined in: [lib/ratelimit.ts:16](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L16)
123757
123743
 
123758
- 检查指定 key 是否超速,使用多段近似滑动窗口算法
123744
+ 检查指定 key 是否超出近似滑动窗口限额,超限请求同样计入次数
123759
123745
 
123760
123746
  ## Parameters
123761
123747
 
@@ -123773,31 +123759,15 @@ KV 实例
123773
123759
 
123774
123760
  ### opt?
123775
123761
 
123776
- 限速选项
123777
-
123778
- #### max?
123779
-
123780
- `number`
123781
-
123782
- 窗口内最大请求数,默认 60
123783
-
123784
- #### pre?
123785
-
123786
- `string`
123787
-
123788
- key 前缀,默认 rl:
123789
-
123790
- #### window?
123762
+ [`ICheckOptions`](../interfaces/ICheckOptions.md) = `{}`
123791
123763
 
123792
- `number`
123793
-
123794
- 窗口时间(秒),默认 60
123764
+ 限速选项
123795
123765
 
123796
123766
  ## Returns
123797
123767
 
123798
123768
  `Promise`\<[`ICheckResult`](../interfaces/ICheckResult.md)\>
123799
123769
 
123800
- 返回结果对象
123770
+ 检查结果;窗口开头最多多计一个时间段,Redis 无法检查时默认放行并标记 unavailable,failClosed 为 true 时拒绝
123801
123771
 
123802
123772
  lib/ratelimit/index.md
123803
123773
  ---
@@ -123812,6 +123782,7 @@ lib/ratelimit/index.md
123812
123782
 
123813
123783
  ## Interfaces
123814
123784
 
123785
+ - [ICheckOptions](interfaces/ICheckOptions.md)
123815
123786
  - [ICheckResult](interfaces/ICheckResult.md)
123816
123787
 
123817
123788
  ## Functions
@@ -123819,6 +123790,61 @@ lib/ratelimit/index.md
123819
123790
  - [check](functions/check.md)
123820
123791
  - [checkFixed](functions/checkFixed.md)
123821
123792
 
123793
+ lib/ratelimit/interfaces/ICheckOptions.md
123794
+ ---
123795
+
123796
+ [**Documents for @maiyunnet/kebab**](../../../index.md)
123797
+
123798
+ ***
123799
+
123800
+ [Documents for @maiyunnet/kebab](../../../index.md) / [lib/ratelimit](../index.md) / ICheckOptions
123801
+
123802
+ # Interface: ICheckOptions
123803
+
123804
+ Defined in: [lib/ratelimit.ts:34](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L34)
123805
+
123806
+ 限速检查选项
123807
+
123808
+ ## Properties
123809
+
123810
+ ### failClosed?
123811
+
123812
+ > `optional` **failClosed?**: `boolean`
123813
+
123814
+ Defined in: [lib/ratelimit.ts:42](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L42)
123815
+
123816
+ Redis 无法检查时是否拒绝请求,默认 false(放行)
123817
+
123818
+ ***
123819
+
123820
+ ### max?
123821
+
123822
+ > `optional` **max?**: `number`
123823
+
123824
+ Defined in: [lib/ratelimit.ts:38](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L38)
123825
+
123826
+ 窗口内最大请求数,默认 60
123827
+
123828
+ ***
123829
+
123830
+ ### pre?
123831
+
123832
+ > `optional` **pre?**: `string`
123833
+
123834
+ Defined in: [lib/ratelimit.ts:40](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L40)
123835
+
123836
+ key 前缀,默认 rl:
123837
+
123838
+ ***
123839
+
123840
+ ### window?
123841
+
123842
+ > `optional` **window?**: `number`
123843
+
123844
+ Defined in: [lib/ratelimit.ts:36](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L36)
123845
+
123846
+ 窗口时间(秒),默认 60
123847
+
123822
123848
  lib/ratelimit/interfaces/ICheckResult.md
123823
123849
  ---
123824
123850
 
@@ -123830,7 +123856,7 @@ lib/ratelimit/interfaces/ICheckResult.md
123830
123856
 
123831
123857
  # Interface: ICheckResult
123832
123858
 
123833
- Defined in: [lib/ratelimit.ts:117](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L117)
123859
+ Defined in: [lib/ratelimit.ts:46](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L46)
123834
123860
 
123835
123861
  限速检查结果
123836
123862
 
@@ -123840,7 +123866,7 @@ Defined in: [lib/ratelimit.ts:117](https://github.com/maiyunnet/kebab/blob/maste
123840
123866
 
123841
123867
  > **allowed**: `boolean`
123842
123868
 
123843
- Defined in: [lib/ratelimit.ts:119](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L119)
123869
+ Defined in: [lib/ratelimit.ts:48](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L48)
123844
123870
 
123845
123871
  是否允许通过
123846
123872
 
@@ -123850,7 +123876,7 @@ Defined in: [lib/ratelimit.ts:119](https://github.com/maiyunnet/kebab/blob/maste
123850
123876
 
123851
123877
  > **limit**: `number`
123852
123878
 
123853
- Defined in: [lib/ratelimit.ts:123](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L123)
123879
+ Defined in: [lib/ratelimit.ts:52](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L52)
123854
123880
 
123855
123881
  总限额
123856
123882
 
@@ -123860,9 +123886,9 @@ Defined in: [lib/ratelimit.ts:123](https://github.com/maiyunnet/kebab/blob/maste
123860
123886
 
123861
123887
  > **remaining**: `number`
123862
123888
 
123863
- Defined in: [lib/ratelimit.ts:121](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L121)
123889
+ Defined in: [lib/ratelimit.ts:50](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L50)
123864
123890
 
123865
- 剩余可用次数
123891
+ 当前剩余可用次数
123866
123892
 
123867
123893
  ***
123868
123894
 
@@ -123870,9 +123896,19 @@ Defined in: [lib/ratelimit.ts:121](https://github.com/maiyunnet/kebab/blob/maste
123870
123896
 
123871
123897
  > **reset**: `number`
123872
123898
 
123873
- Defined in: [lib/ratelimit.ts:125](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L125)
123899
+ Defined in: [lib/ratelimit.ts:54](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L54)
123900
+
123901
+ 预计恢复额度的 Unix 秒,滑动窗口按此后没有新请求估算
123902
+
123903
+ ***
123904
+
123905
+ ### unavailable?
123906
+
123907
+ > `optional` **unavailable?**: `true`
123908
+
123909
+ Defined in: [lib/ratelimit.ts:56](https://github.com/maiyunnet/kebab/blob/master/lib/ratelimit.ts#L56)
123874
123910
 
123875
- 窗口重置时间(Unix 时间戳秒)
123911
+ Redis 无法检查限额时为 true
123876
123912
 
123877
123913
  lib/s3/classes/S3.md
123878
123914
  ---
package/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import type * as tls from 'node:tls';
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "10.1.0";
8
+ export declare const VER = "10.1.1";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
6
  /** --- 当前系统版本号 --- */
7
- export const VER = '10.1.0';
7
+ export const VER = '10.1.1';
8
8
  // --- 服务端用的路径 ---
9
9
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
10
10
  /** --- /xxx/xxx --- */
package/lib/kv.d.ts CHANGED
@@ -42,7 +42,7 @@ export declare class Kv {
42
42
  */
43
43
  pipeline(): Promise<TRedisMulti | false>;
44
44
  /**
45
- * --- 在共享连接上执行一个 Redis Lua 脚本 ---
45
+ * --- 在共享连接上执行一个 Redis Lua 脚本,首次走 EVAL 加载,后续走 EVALSHA 并在 NOSCRIPT 时自动回退 ---
46
46
  * @param script Lua 源码
47
47
  * @param keys 传给 KEYS 的完整键名,调用方需自行添加项目前缀
48
48
  * @param args 传给 ARGV 的参数
package/lib/kv.js CHANGED
@@ -2,6 +2,8 @@
2
2
  * Project: Kebab, User: JianSuoQiYue
3
3
  * Date: 2019-5-30 19:25:22
4
4
  */
5
+ // --- 原生 ---
6
+ import * as crypto from 'node:crypto';
5
7
  // --- 第三方 ---
6
8
  import * as redis from 'redis';
7
9
  import * as lText from '#kebab/lib/text.js';
@@ -9,6 +11,37 @@ import * as lCore from '#kebab/lib/core.js';
9
11
  import * as sCtr from '#kebab/sys/ctr.js';
10
12
  /** --- 相同地址、数据库和凭据共用一次连接初始化 --- */
11
13
  const connections = [];
14
+ /** --- 缓存已加载的 Lua 源码与其 SHA-1(LRU 上限 256 条),后续直接走 EVALSHA --- */
15
+ const evalShaCache = new Map();
16
+ /**
17
+ * --- 读取并刷新 Lua 源码的缓存 SHA-1(LRU) ---
18
+ * @param script Lua 源码
19
+ * @returns 已缓存的 40 位 SHA-1,未缓存返回 null
20
+ */
21
+ function getCachedScriptSha(script) {
22
+ const cached = evalShaCache.get(script);
23
+ if (!cached) {
24
+ return null;
25
+ }
26
+ evalShaCache.delete(script);
27
+ evalShaCache.set(script, cached);
28
+ return cached;
29
+ }
30
+ /**
31
+ * --- 计算并记录 Lua 源码的 SHA-1 到定长 LRU 缓存 ---
32
+ * @param script Lua 源码
33
+ */
34
+ function cacheScriptSha(script) {
35
+ const sha = crypto.createHash('sha1').update(script).digest('hex');
36
+ evalShaCache.delete(script);
37
+ if (evalShaCache.size >= 256) {
38
+ const oldest = evalShaCache.keys().next().value;
39
+ if (oldest !== undefined) {
40
+ evalShaCache.delete(oldest);
41
+ }
42
+ }
43
+ evalShaCache.set(script, sha);
44
+ }
12
45
  /**
13
46
  * --- 生成 TLS 配置的缓存区分键,避免不同证书或 servername 误用同一连接 ---
14
47
  * @param tlsOpt TLS 配置
@@ -154,7 +187,7 @@ export class Kv {
154
187
  }
155
188
  }
156
189
  /**
157
- * --- 在共享连接上执行一个 Redis Lua 脚本 ---
190
+ * --- 在共享连接上执行一个 Redis Lua 脚本,首次走 EVAL 加载,后续走 EVALSHA 并在 NOSCRIPT 时自动回退 ---
158
191
  * @param script Lua 源码
159
192
  * @param keys 传给 KEYS 的完整键名,调用方需自行添加项目前缀
160
193
  * @param args 传给 ARGV 的参数
@@ -165,13 +198,34 @@ export class Kv {
165
198
  if (!conn) {
166
199
  return false;
167
200
  }
201
+ const options = {
202
+ 'keys': keys,
203
+ 'arguments': args,
204
+ };
205
+ const sha = getCachedScriptSha(script);
206
+ if (!sha) {
207
+ try {
208
+ const res = await conn.eval(script, options);
209
+ cacheScriptSha(script);
210
+ return res;
211
+ }
212
+ catch {
213
+ return false;
214
+ }
215
+ }
168
216
  try {
169
- return await conn.eval(script, {
170
- 'keys': keys,
171
- 'arguments': args,
172
- });
217
+ return await conn.evalSha(sha, options);
173
218
  }
174
- catch {
219
+ catch (err) {
220
+ // --- 服务端重启或清理脚本缓存后返回 NOSCRIPT,回退 EVAL 重新加载 ---
221
+ if (err instanceof Error && err.message.startsWith('NOSCRIPT')) {
222
+ try {
223
+ return await conn.eval(script, options);
224
+ }
225
+ catch {
226
+ return false;
227
+ }
228
+ }
175
229
  return false;
176
230
  }
177
231
  }
@@ -1,47 +1,41 @@
1
+ import type * as lKv from '#kebab/lib/kv.js';
1
2
  /**
2
- * Project: Kebab
3
- * Date: 2026-3-22 00:00:00
4
- * --- Rate Limiting 限速库,支持令牌桶和滑动窗口两种策略 ---
5
- * --- 基于 KV(Redis)实现,天然支持多进程和多机部署 ---
6
- */
7
- import * as lKv from '#kebab/lib/kv.js';
8
- /**
9
- * --- 检查指定 key 是否超速,使用多段近似滑动窗口算法 ---
3
+ * --- 检查指定 key 是否超出近似滑动窗口限额,超限请求同样计入次数 ---
10
4
  * @param kv KV 实例
11
5
  * @param key 限速标识(如 IP、用户 UID 等)
12
6
  * @param opt 限速选项
13
- * @returns 返回结果对象
7
+ * @returns 检查结果;窗口开头最多多计一个时间段,Redis 无法检查时默认放行并标记 unavailable,failClosed 为 true 时拒绝
14
8
  */
15
- export declare function check(kv: lKv.Kv, key: string, opt?: {
16
- /** --- 窗口时间(秒),默认 60 --- */
17
- 'window'?: number;
18
- /** --- 窗口内最大请求数,默认 60 --- */
19
- 'max'?: number;
20
- /** --- key 前缀,默认 rl: --- */
21
- 'pre'?: string;
22
- }): Promise<ICheckResult>;
9
+ export declare function check(kv: lKv.Kv, key: string, opt?: ICheckOptions): Promise<ICheckResult>;
23
10
  /**
24
- * --- 简易固定窗口限速检查(性能更高,精度较低) ---
11
+ * --- 检查指定 key 是否超出固定窗口限额,窗口从首次请求开始且超限请求同样计入次数 ---
25
12
  * @param kv KV 实例
26
13
  * @param key 限速标识
27
14
  * @param opt 限速选项
15
+ * @returns 检查结果;后续请求沿用首次请求设置的过期时间,Redis 无法检查时默认放行并标记 unavailable,failClosed 为 true 时拒绝
28
16
  */
29
- export declare function checkFixed(kv: lKv.Kv, key: string, opt?: {
17
+ export declare function checkFixed(kv: lKv.Kv, key: string, opt?: ICheckOptions): Promise<ICheckResult>;
18
+ /** --- 限速检查选项 --- */
19
+ export interface ICheckOptions {
30
20
  /** --- 窗口时间(秒),默认 60 --- */
31
21
  'window'?: number;
32
22
  /** --- 窗口内最大请求数,默认 60 --- */
33
23
  'max'?: number;
34
24
  /** --- key 前缀,默认 rl: --- */
35
25
  'pre'?: string;
36
- }): Promise<ICheckResult>;
26
+ /** --- Redis 无法检查时是否拒绝请求,默认 false(放行) --- */
27
+ 'failClosed'?: boolean;
28
+ }
37
29
  /** --- 限速检查结果 --- */
38
30
  export interface ICheckResult {
39
31
  /** --- 是否允许通过 --- */
40
32
  'allowed': boolean;
41
- /** --- 剩余可用次数 --- */
33
+ /** --- 当前剩余可用次数 --- */
42
34
  'remaining': number;
43
35
  /** --- 总限额 --- */
44
36
  'limit': number;
45
- /** --- 窗口重置时间(Unix 时间戳秒) --- */
37
+ /** --- 预计恢复额度的 Unix 秒,滑动窗口按此后没有新请求估算 --- */
46
38
  'reset': number;
39
+ /** --- Redis 无法检查限额时为 true --- */
40
+ 'unavailable'?: true;
47
41
  }
package/lib/ratelimit.js CHANGED
@@ -1,88 +1,26 @@
1
- import * as lTime from '#kebab/lib/time.js';
2
- import * as lCore from '#kebab/lib/core.js';
3
1
  /**
4
- * --- 检查指定 key 是否超速,使用多段近似滑动窗口算法 ---
2
+ * Project: Kebab
3
+ * Date: 2026-3-22 00:00:00
4
+ * --- 基于 Redis 的固定窗口和近似滑动窗口限速 ---
5
+ */
6
+ import { checkRate } from '#kebab/abox/ratelimit.js';
7
+ /**
8
+ * --- 检查指定 key 是否超出近似滑动窗口限额,超限请求同样计入次数 ---
5
9
  * @param kv KV 实例
6
10
  * @param key 限速标识(如 IP、用户 UID 等)
7
11
  * @param opt 限速选项
8
- * @returns 返回结果对象
12
+ * @returns 检查结果;窗口开头最多多计一个时间段,Redis 无法检查时默认放行并标记 unavailable,failClosed 为 true 时拒绝
9
13
  */
10
- export async function check(kv, key, opt = {}) {
11
- const window = Math.max(Math.floor(opt.window ?? 60), 1);
12
- const max = Math.max(Math.floor(opt.max ?? 60), 1);
13
- const pre = opt.pre ?? 'rl:';
14
- const now = lTime.stamp();
15
- /** --- 将窗口分为 6 个子段 --- */
16
- const segSize = Math.max(Math.floor(window / 6), 1);
17
- /** --- 当前段标识 --- */
18
- const segId = Math.floor(now / segSize);
19
- /** --- 需要统计的段数 --- */
20
- const segCount = Math.ceil(window / segSize);
21
- /** --- 统计各段请求数总和 --- */
22
- let total = 0;
23
- for (let i = 1; i < segCount; ++i) {
24
- const segKey = `${pre}${key}:${segId - i}`;
25
- const val = await kv.get(segKey);
26
- if (val) {
27
- total += parseInt(val) || 0;
28
- }
29
- }
30
- // --- 当前段 +1 ---
31
- const currentSegKey = `${pre}${key}:${segId}`;
32
- const count = await kv.incr(currentSegKey);
33
- if (count === false) {
34
- // --- KV 连接失败,放行 ---
35
- lCore.debug('[RATELIMIT] KV incr failed, allow by default');
36
- return {
37
- 'allowed': true,
38
- 'remaining': max,
39
- 'limit': max,
40
- 'reset': now + window,
41
- };
42
- }
43
- if (count === 1) {
44
- await kv.expire(currentSegKey, window + segSize);
45
- }
46
- total += count;
47
- const allowed = total <= max;
48
- return {
49
- 'allowed': allowed,
50
- 'remaining': allowed ? max - total : 0,
51
- 'limit': max,
52
- 'reset': (segId + 1) * segSize,
53
- };
14
+ export function check(kv, key, opt = {}) {
15
+ return checkRate(kv, key, opt, 'sliding');
54
16
  }
55
17
  /**
56
- * --- 简易固定窗口限速检查(性能更高,精度较低) ---
18
+ * --- 检查指定 key 是否超出固定窗口限额,窗口从首次请求开始且超限请求同样计入次数 ---
57
19
  * @param kv KV 实例
58
20
  * @param key 限速标识
59
21
  * @param opt 限速选项
22
+ * @returns 检查结果;后续请求沿用首次请求设置的过期时间,Redis 无法检查时默认放行并标记 unavailable,failClosed 为 true 时拒绝
60
23
  */
61
- export async function checkFixed(kv, key, opt = {}) {
62
- const window = Math.max(Math.floor(opt.window ?? 60), 1);
63
- const max = Math.max(Math.floor(opt.max ?? 60), 1);
64
- const pre = opt.pre ?? 'rl:';
65
- const now = lTime.stamp();
66
- const rkey = pre + key;
67
- const count = await kv.incr(rkey);
68
- if (count === false) {
69
- // --- KV 连接失败,放行 ---
70
- return {
71
- 'allowed': true,
72
- 'remaining': max,
73
- 'limit': max,
74
- 'reset': now + window,
75
- };
76
- }
77
- if (count === 1) {
78
- // --- 首次请求,设置过期时间 ---
79
- await kv.expire(rkey, window);
80
- }
81
- const allowed = count <= max;
82
- return {
83
- 'allowed': allowed,
84
- 'remaining': allowed ? max - count : 0,
85
- 'limit': max,
86
- 'reset': now + window,
87
- };
24
+ export function checkFixed(kv, key, opt = {}) {
25
+ return checkRate(kv, key, opt, 'fixed');
88
26
  }
@@ -41,7 +41,7 @@ export declare class Response {
41
41
  /**
42
42
  * --- 获取响应读取流对象 ---
43
43
  */
44
- getStream(): zlib.BrotliDecompress | zlib.Gunzip | zlib.Inflate | (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | null;
44
+ getStream(): (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | zlib.Gunzip | zlib.Inflate | zlib.BrotliDecompress | null;
45
45
  /**
46
46
  * --- 获取原生响应读取流对象 ---
47
47
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "10.1.0",
3
+ "version": "10.1.1",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [