@koralabs/kora-labs-common 6.18.1 → 6.18.3
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.
|
@@ -15,7 +15,11 @@ export declare const statedWaitMs: (headers: HeaderReader | undefined, body: unk
|
|
|
15
15
|
/** Record that `key` must not be called for `waitMs` (only ever extends an existing block). */
|
|
16
16
|
export declare const recordRateLimit: (key: string, waitMs: number, now?: number) => void;
|
|
17
17
|
export declare const rateLimitWaitRemainingMs: (key: string, now?: number) => number;
|
|
18
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* Wait out a recorded limit before calling `key`; throw (returning the wait) when it exceeds `maxWaitMs`.
|
|
20
|
+
* A timer runs on the monotonic clock while the block is wall-clock ms (Date.now()), so it can fire a
|
|
21
|
+
* millisecond or more before Date.now() reaches the stated time: re-check and keep waiting until it has.
|
|
22
|
+
*/
|
|
19
23
|
export declare const waitForRateLimit: (key: string, maxWaitMs: number) => Promise<void>;
|
|
20
24
|
/** Test seam: forget every recorded limit. */
|
|
21
25
|
export declare const resetRateLimits: () => void;
|
|
@@ -86,14 +86,17 @@ exports.recordRateLimit = recordRateLimit;
|
|
|
86
86
|
const rateLimitWaitRemainingMs = (key, now = Date.now()) => { var _a; return Math.max(0, ((_a = blockedUntilByKey.get(key)) !== null && _a !== void 0 ? _a : 0) - now); };
|
|
87
87
|
exports.rateLimitWaitRemainingMs = rateLimitWaitRemainingMs;
|
|
88
88
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
89
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* Wait out a recorded limit before calling `key`; throw (returning the wait) when it exceeds `maxWaitMs`.
|
|
91
|
+
* A timer runs on the monotonic clock while the block is wall-clock ms (Date.now()), so it can fire a
|
|
92
|
+
* millisecond or more before Date.now() reaches the stated time: re-check and keep waiting until it has.
|
|
93
|
+
*/
|
|
90
94
|
const waitForRateLimit = async (key, maxWaitMs) => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
await sleep(remaining);
|
|
95
|
+
for (let remaining = (0, exports.rateLimitWaitRemainingMs)(key); remaining > 0; remaining = (0, exports.rateLimitWaitRemainingMs)(key)) {
|
|
96
|
+
if (remaining > maxWaitMs)
|
|
97
|
+
throw new RateLimitedError(key, remaining);
|
|
98
|
+
await sleep(remaining);
|
|
99
|
+
}
|
|
97
100
|
};
|
|
98
101
|
exports.waitForRateLimit = waitForRateLimit;
|
|
99
102
|
/** Test seam: forget every recorded limit. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koralabs/kora-labs-common",
|
|
3
|
-
"version": "6.18.
|
|
3
|
+
"version": "6.18.3",
|
|
4
4
|
"description": "Kora Labs Common Utilities",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"pluralize-esm": "^9.0.5"
|
|
70
70
|
},
|
|
71
71
|
"overrides": {
|
|
72
|
-
"js-yaml": "4.3.
|
|
72
|
+
"js-yaml": "4.3.2",
|
|
73
73
|
"brace-expansion": "5.0.9"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|