@vouchington/rate-limit 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jonathan Ong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @vouchington/rate-limit
2
+
3
+ Generic [Valkey](https://valkey.io/) rate-limiting primitives backed by
4
+ [Valkyries](https://www.npmjs.com/package/valkyries). Applications provide their own keys,
5
+ thresholds, windows, clients, and response policy.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ pnpm add @vouchington/rate-limit
11
+ ```
12
+
13
+ ## Rate limiting
14
+
15
+ `RateLimiter` records and counts caller-provided identifiers. Construct it with an application
16
+ prefix and TTL, then choose the identifiers and threshold at each call site.
17
+
18
+ ```ts
19
+ import { RateLimiter } from '@vouchington/rate-limit'
20
+
21
+ const limiter = new RateLimiter({ prefix: 'api', ttlSeconds: 60 })
22
+ const { limited } = await limiter.addAndCheck(['account:123'], 100)
23
+ ```
24
+
25
+ `RateLimiter.addAndCheckWindows` supports atomic multi-window checks. `RateLimiterOptions`,
26
+ `RateLimiterWindow`, and `RateLimiterAddAndCheckWindowsOptions` describe the available options.
27
+ Pass a caller-managed client when the application owns connection lifecycle. Importing the package
28
+ also initializes Valkyries-managed lazy clients for the default-client path. Applications must call
29
+ `closeManagedValkeyClients()` during shutdown; it closes every package-managed Valkyries client in
30
+ the process, while caller-injected clients remain caller-owned.
31
+
32
+ ```ts
33
+ import { closeManagedValkeyClients } from '@vouchington/rate-limit'
34
+
35
+ await closeManagedValkeyClients()
36
+ ```
37
+
38
+ ## Saturation retries
39
+
40
+ `retryRateLimiterSaturation` retries only Valkey client inflight-saturation rejections. Callers can
41
+ set the attempt count and minimum delay; otherwise Valkyries defaults apply. The helper does not
42
+ choose key formats, thresholds, or how an application responds to a limit.
43
+
44
+ ```ts
45
+ import { retryRateLimiterSaturation } from '@vouchington/rate-limit'
46
+
47
+ const result = await retryRateLimiterSaturation(() => client.invokeScript(script, options), {
48
+ attempts: 3,
49
+ delayMs: 100,
50
+ })
51
+ ```
@@ -0,0 +1,2 @@
1
+ export { closeValkeyClients as closeManagedValkeyClients, retrySaturationError as retryRateLimiterSaturation, type RateLimiterAddAndCheckWindowsOptions, type RateLimiterOptions, type RateLimiterWindow, type RetrySaturationErrorOptions as RetryRateLimiterSaturationOptions, } from 'valkyries';
2
+ export { RateLimiter } from 'valkyries/rate-limiter';
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export { closeValkeyClients as closeManagedValkeyClients, retrySaturationError as retryRateLimiterSaturation, } from 'valkyries';
2
+ export { RateLimiter } from 'valkyries/rate-limiter';
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@vouchington/rate-limit",
3
+ "version": "0.0.0",
4
+ "description": "Valkey rate-limiting primitives.",
5
+ "homepage": "https://github.com/vouchington/vouchington-platform/tree/main/packages/rate-limit#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/vouchington/vouchington-platform/issues"
8
+ },
9
+ "license": "MIT",
10
+ "author": "Jonathan Ong",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/vouchington/vouchington-platform.git",
14
+ "directory": "packages/rate-limit"
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "type": "module",
22
+ "main": "./dist/index.mjs",
23
+ "types": "./dist/index.d.mts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.mts",
27
+ "import": "./dist/index.mjs",
28
+ "default": "./dist/index.mjs"
29
+ }
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc --project tsconfig.build.json",
36
+ "prepack": "pnpm run build"
37
+ },
38
+ "dependencies": {
39
+ "valkyries": "^0.8.0"
40
+ },
41
+ "engines": {
42
+ "node": ">=24.0.0"
43
+ }
44
+ }