@commandkit/ratelimit 0.0.0-dev.20260317060555
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 +21 -0
- package/README.md +801 -0
- package/dist/api.d.ts +79 -0
- package/dist/api.js +266 -0
- package/dist/augmentation.d.ts +11 -0
- package/dist/augmentation.js +8 -0
- package/dist/configure.d.ts +28 -0
- package/dist/configure.js +85 -0
- package/dist/constants.d.ts +17 -0
- package/dist/constants.js +21 -0
- package/dist/directive/use-ratelimit-directive.d.ts +22 -0
- package/dist/directive/use-ratelimit-directive.js +38 -0
- package/dist/directive/use-ratelimit.d.ts +14 -0
- package/dist/directive/use-ratelimit.js +169 -0
- package/dist/engine/RateLimitEngine.d.ts +48 -0
- package/dist/engine/RateLimitEngine.js +137 -0
- package/dist/engine/algorithms/fixed-window.d.ts +44 -0
- package/dist/engine/algorithms/fixed-window.js +198 -0
- package/dist/engine/algorithms/leaky-bucket.d.ts +48 -0
- package/dist/engine/algorithms/leaky-bucket.js +119 -0
- package/dist/engine/algorithms/sliding-window.d.ts +45 -0
- package/dist/engine/algorithms/sliding-window.js +127 -0
- package/dist/engine/algorithms/token-bucket.d.ts +47 -0
- package/dist/engine/algorithms/token-bucket.js +118 -0
- package/dist/engine/violations.d.ts +55 -0
- package/dist/engine/violations.js +106 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.js +28 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +53 -0
- package/dist/plugin.d.ts +140 -0
- package/dist/plugin.js +796 -0
- package/dist/providers/fallback.d.ts +7 -0
- package/dist/providers/fallback.js +11 -0
- package/dist/providers/memory.d.ts +6 -0
- package/dist/providers/memory.js +11 -0
- package/dist/providers/redis.d.ts +7 -0
- package/dist/providers/redis.js +11 -0
- package/dist/runtime.d.ts +45 -0
- package/dist/runtime.js +67 -0
- package/dist/storage/fallback.d.ts +180 -0
- package/dist/storage/fallback.js +261 -0
- package/dist/storage/memory.d.ts +146 -0
- package/dist/storage/memory.js +304 -0
- package/dist/storage/redis.d.ts +130 -0
- package/dist/storage/redis.js +243 -0
- package/dist/types.d.ts +296 -0
- package/dist/types.js +40 -0
- package/dist/utils/config.d.ts +34 -0
- package/dist/utils/config.js +105 -0
- package/dist/utils/keys.d.ts +102 -0
- package/dist/utils/keys.js +304 -0
- package/dist/utils/locking.d.ts +17 -0
- package/dist/utils/locking.js +60 -0
- package/dist/utils/time.d.ts +23 -0
- package/dist/utils/time.js +72 -0
- package/package.json +65 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Time helpers for rate limits.
|
|
4
|
+
*
|
|
5
|
+
* Converts user-friendly durations into milliseconds and clamps values
|
|
6
|
+
* so storage and algorithms always receive safe inputs.
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.resolveDuration = resolveDuration;
|
|
13
|
+
exports.clampAtLeast = clampAtLeast;
|
|
14
|
+
const ms_1 = __importDefault(require("ms"));
|
|
15
|
+
const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
|
|
16
|
+
const MONTH_MS = 30 * 24 * 60 * 60 * 1000;
|
|
17
|
+
/**
|
|
18
|
+
* Resolve a duration input into milliseconds with a fallback.
|
|
19
|
+
*
|
|
20
|
+
* @param value - Duration input as ms or string.
|
|
21
|
+
* @param fallback - Fallback value used when parsing fails.
|
|
22
|
+
* @returns Parsed duration in milliseconds.
|
|
23
|
+
*/
|
|
24
|
+
function resolveDuration(value, fallback) {
|
|
25
|
+
if (value == null)
|
|
26
|
+
return fallback;
|
|
27
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
28
|
+
return value;
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
/**
|
|
31
|
+
* Allow week/month units so config can use human-friendly windows.
|
|
32
|
+
*/
|
|
33
|
+
const custom = parseExtendedDuration(value);
|
|
34
|
+
if (custom != null)
|
|
35
|
+
return custom;
|
|
36
|
+
const parsed = (0, ms_1.default)(value);
|
|
37
|
+
if (typeof parsed === 'number' && Number.isFinite(parsed))
|
|
38
|
+
return parsed;
|
|
39
|
+
}
|
|
40
|
+
return fallback;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Parse week/month duration strings that ms does not support.
|
|
44
|
+
*
|
|
45
|
+
* @param value - Raw duration string.
|
|
46
|
+
* @returns Parsed duration in ms or null when invalid.
|
|
47
|
+
*/
|
|
48
|
+
function parseExtendedDuration(value) {
|
|
49
|
+
const trimmed = value.trim();
|
|
50
|
+
if (!trimmed)
|
|
51
|
+
return null;
|
|
52
|
+
const match = trimmed.match(/^(\d+(?:\.\d+)?)\s*(w|week|weeks|mo|month|months)$/i);
|
|
53
|
+
if (!match)
|
|
54
|
+
return null;
|
|
55
|
+
const amount = Number(match[1]);
|
|
56
|
+
if (!Number.isFinite(amount) || amount <= 0)
|
|
57
|
+
return null;
|
|
58
|
+
const unit = match[2].toLowerCase();
|
|
59
|
+
const multiplier = unit === 'w' || unit === 'week' || unit === 'weeks' ? WEEK_MS : MONTH_MS;
|
|
60
|
+
return Math.round(amount * multiplier);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Clamp a number to a minimum value to avoid zero/negative windows.
|
|
64
|
+
*
|
|
65
|
+
* @param value - Value to clamp.
|
|
66
|
+
* @param min - Minimum allowed value.
|
|
67
|
+
* @returns The clamped value.
|
|
68
|
+
*/
|
|
69
|
+
function clampAtLeast(value, min) {
|
|
70
|
+
return value < min ? min : value;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGltZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlsc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+
"version": "0.0.0-dev.20260317060555",
|
|
4
|
+
"description": "CommandKit plugin that provides advanced rate limiting",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./redis": {
|
|
16
|
+
"import": "./dist/providers/redis.js",
|
|
17
|
+
"types": "./dist/providers/redis.d.ts"
|
|
18
|
+
},
|
|
19
|
+
"./memory": {
|
|
20
|
+
"import": "./dist/providers/memory.js",
|
|
21
|
+
"types": "./dist/providers/memory.d.ts"
|
|
22
|
+
},
|
|
23
|
+
"./fallback": {
|
|
24
|
+
"import": "./dist/providers/fallback.js",
|
|
25
|
+
"types": "./dist/providers/fallback.d.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/neplextech/commandkit.git",
|
|
31
|
+
"directory": "packages/ratelimit"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"commandkit",
|
|
35
|
+
"ratelimit",
|
|
36
|
+
"rate limiting"
|
|
37
|
+
],
|
|
38
|
+
"contributors": [
|
|
39
|
+
"Twilight <hello@twlite.dev>",
|
|
40
|
+
"Avraj <avraj@underctrl.io>"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/neplextech/commandkit/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://commandkit.dev",
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"ioredis": "^5.10.0",
|
|
49
|
+
"ms": "^2.1.3"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/ms": "^2.1.0",
|
|
53
|
+
"discord.js": "^14.25.1",
|
|
54
|
+
"directive-to-hof": "^0.0.3",
|
|
55
|
+
"typescript": "^5.9.3",
|
|
56
|
+
"vitest": "^4.0.18",
|
|
57
|
+
"commandkit": "1.2.1-dev.20260317060555",
|
|
58
|
+
"tsconfig": "0.0.0-dev.20260317060555"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"check-types": "tsc --noEmit",
|
|
62
|
+
"build": "tsc",
|
|
63
|
+
"test": "vitest"
|
|
64
|
+
}
|
|
65
|
+
}
|