@thexjs/core 1.10.0 → 1.10.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/dist/index.js +41 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1615,19 +1615,37 @@ async function rateLimitMiddleware(limiter, req, server) {
|
|
|
1615
1615
|
}
|
|
1616
1616
|
function createBunRedisClient(options) {
|
|
1617
1617
|
return async () => {
|
|
1618
|
-
const
|
|
1619
|
-
const
|
|
1620
|
-
const instance = options.url ? new mod.RedisClient({ url: options.url }) : new mod.RedisClient();
|
|
1618
|
+
const BunRedis = globalThis.Bun.RedisClient;
|
|
1619
|
+
const instance = options.url ? new BunRedis(options.url) : new BunRedis();
|
|
1621
1620
|
await instance.connect();
|
|
1622
|
-
return
|
|
1621
|
+
return {
|
|
1622
|
+
sendCommand: (command, ...args) => instance.send(command, args)
|
|
1623
|
+
};
|
|
1623
1624
|
};
|
|
1624
1625
|
}
|
|
1625
1626
|
function createRedisRateLimitStore(options = {}) {
|
|
1626
1627
|
return createRedisRateLimitStoreFromClient(createBunRedisClient(options));
|
|
1627
1628
|
}
|
|
1629
|
+
var INCR_WITH_WINDOW_SCRIPT = `local c = redis.call('INCR', KEYS[1])
|
|
1630
|
+
if redis.call('PTTL', KEYS[1]) == -1 then redis.call('EXPIRE', KEYS[1], ARGV[1]) end
|
|
1631
|
+
return { c, redis.call('PTTL', KEYS[1]) }`;
|
|
1632
|
+
async function incrWithoutScript(c, redisKey, ttlSeconds) {
|
|
1633
|
+
const count = Number(await c.sendCommand("INCR", redisKey));
|
|
1634
|
+
let pttl = Number(await c.sendCommand("PTTL", redisKey));
|
|
1635
|
+
if (pttl === -1) {
|
|
1636
|
+
await c.sendCommand("EXPIRE", redisKey, String(ttlSeconds));
|
|
1637
|
+
pttl = Number(await c.sendCommand("PTTL", redisKey));
|
|
1638
|
+
}
|
|
1639
|
+
return [count, pttl];
|
|
1640
|
+
}
|
|
1641
|
+
function isScriptingDenied(err) {
|
|
1642
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1643
|
+
return /NOSCRIPT|unknown command|NOPERM|scripting is disabled|not allowed/i.test(message);
|
|
1644
|
+
}
|
|
1628
1645
|
function createRedisRateLimitStoreFromClient(clientFactory) {
|
|
1629
1646
|
let client = null;
|
|
1630
1647
|
let connecting = null;
|
|
1648
|
+
let scriptingDenied = false;
|
|
1631
1649
|
function getClient() {
|
|
1632
1650
|
if (client) return Promise.resolve(client);
|
|
1633
1651
|
if (!connecting) {
|
|
@@ -1649,12 +1667,26 @@ function createRedisRateLimitStoreFromClient(clientFactory) {
|
|
|
1649
1667
|
const c = await getClient();
|
|
1650
1668
|
const redisKey = `x:ratelimit:${key}`;
|
|
1651
1669
|
const ttlSeconds = Math.max(1, Math.ceil(windowMs / 1e3));
|
|
1652
|
-
|
|
1653
|
-
if (
|
|
1654
|
-
await c
|
|
1670
|
+
let reply;
|
|
1671
|
+
if (scriptingDenied) {
|
|
1672
|
+
reply = await incrWithoutScript(c, redisKey, ttlSeconds);
|
|
1673
|
+
} else {
|
|
1674
|
+
try {
|
|
1675
|
+
reply = await c.sendCommand(
|
|
1676
|
+
"EVAL",
|
|
1677
|
+
INCR_WITH_WINDOW_SCRIPT,
|
|
1678
|
+
"1",
|
|
1679
|
+
redisKey,
|
|
1680
|
+
String(ttlSeconds)
|
|
1681
|
+
);
|
|
1682
|
+
} catch (err) {
|
|
1683
|
+
if (!isScriptingDenied(err)) throw err;
|
|
1684
|
+
scriptingDenied = true;
|
|
1685
|
+
reply = await incrWithoutScript(c, redisKey, ttlSeconds);
|
|
1686
|
+
}
|
|
1655
1687
|
}
|
|
1656
|
-
const
|
|
1657
|
-
const remainingMs = Math.max(
|
|
1688
|
+
const count = Number(reply[0]);
|
|
1689
|
+
const remainingMs = Math.max(Number(reply[1]), 0);
|
|
1658
1690
|
return { count, resetAt: Date.now() + remainingMs };
|
|
1659
1691
|
}
|
|
1660
1692
|
};
|
package/package.json
CHANGED