@maiyunnet/kebab 10.0.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.
- package/abox/ratelimit.d.ts +3 -0
- package/abox/ratelimit.js +84 -0
- package/doc/kebab-rag.md +555 -381
- package/index.d.ts +3 -2
- package/index.js +1 -1
- package/lib/db/conn.d.ts +4 -2
- package/lib/db/pool.d.ts +17 -4
- package/lib/db/pool.js +41 -23
- package/lib/db/tran.d.ts +6 -4
- package/lib/db.d.ts +10 -5
- package/lib/db.js +36 -6
- package/lib/kv.d.ts +1 -1
- package/lib/kv.js +60 -6
- package/lib/ratelimit.d.ts +16 -22
- package/lib/ratelimit.js +14 -76
- package/package.json +1 -1
- package/sys/cmd.js +0 -13
- package/sys/ctr.d.ts +2 -2
- package/sys/ctr.js +2 -2
- package/sys/mod.d.ts +16 -0
- package/sys/mod.js +33 -15
- package/www/example/ctr/test.d.ts +1 -1
- package/www/example/ctr/test.js +26 -7
- package/www/example/mod/test.js +0 -1
|
@@ -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
|
+
}
|