@maiyunnet/kebab 10.1.0 → 11.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.
@@ -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
+ }