@sapphire/ratelimits 2.4.7-next.104f2507.0 → 2.4.7-next.141fc267.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/dist/index.d.mts +92 -0
- package/dist/index.global.js +7 -6
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +7 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
declare class RateLimitManager<K = string> extends Map<K, RateLimit<K>> {
|
|
2
|
+
/**
|
|
3
|
+
* The amount of milliseconds for the {@link RateLimit ratelimits} from this manager to expire.
|
|
4
|
+
*/
|
|
5
|
+
readonly time: number;
|
|
6
|
+
/**
|
|
7
|
+
* The amount of times a {@link RateLimit} can drip before it's limited.
|
|
8
|
+
*/
|
|
9
|
+
readonly limit: number;
|
|
10
|
+
/**
|
|
11
|
+
* The interval to sweep expired {@link RateLimit ratelimits}.
|
|
12
|
+
*/
|
|
13
|
+
private sweepInterval;
|
|
14
|
+
/**
|
|
15
|
+
* @param time The amount of milliseconds for the ratelimits from this manager to expire.
|
|
16
|
+
* @param limit The amount of times a {@link RateLimit} can drip before it's limited.
|
|
17
|
+
*/
|
|
18
|
+
constructor(time: number, limit?: number);
|
|
19
|
+
/**
|
|
20
|
+
* Gets a {@link RateLimit} from this manager or creates it if it does not exist.
|
|
21
|
+
* @param id The id for the {@link RateLimit}
|
|
22
|
+
*/
|
|
23
|
+
acquire(id: K): RateLimit<K>;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a {@link RateLimit} for this manager.
|
|
26
|
+
* @param id The id the {@link RateLimit} belongs to
|
|
27
|
+
*/
|
|
28
|
+
create(id: K): RateLimit<K>;
|
|
29
|
+
/**
|
|
30
|
+
* Wraps Collection's set method to set interval to sweep inactive {@link RateLimit}s.
|
|
31
|
+
* @param id The id the {@link RateLimit} belongs to
|
|
32
|
+
* @param value The {@link RateLimit} to set
|
|
33
|
+
*/
|
|
34
|
+
set(id: K, value: RateLimit<K>): this;
|
|
35
|
+
/**
|
|
36
|
+
* Wraps Collection's sweep method to clear the interval when this manager is empty.
|
|
37
|
+
*/
|
|
38
|
+
sweep(): void;
|
|
39
|
+
/**
|
|
40
|
+
* The delay in milliseconds for {@link RateLimitManager.sweepInterval}.
|
|
41
|
+
*/
|
|
42
|
+
static sweepIntervalDuration: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare class RateLimit<K = string> {
|
|
46
|
+
/**
|
|
47
|
+
* The remaining amount of times this entry can be dripped before the bucket is empty.
|
|
48
|
+
*/
|
|
49
|
+
remaining: number;
|
|
50
|
+
/**
|
|
51
|
+
* The timestamp that represents when this entry will reset back to a available state.
|
|
52
|
+
*/
|
|
53
|
+
expires: number;
|
|
54
|
+
/**
|
|
55
|
+
* The {@link RateLimitManager} this entry is for.
|
|
56
|
+
*/
|
|
57
|
+
private manager;
|
|
58
|
+
/**
|
|
59
|
+
* @param manager The manager for this entry.
|
|
60
|
+
*/
|
|
61
|
+
constructor(manager: RateLimitManager<K>);
|
|
62
|
+
/**
|
|
63
|
+
* Whether this entry is expired or not, allowing the bucket to be reset.
|
|
64
|
+
*/
|
|
65
|
+
get expired(): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Whether this entry is limited or not.
|
|
68
|
+
*/
|
|
69
|
+
get limited(): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* The remaining time in milliseconds before resetting.
|
|
72
|
+
*/
|
|
73
|
+
get remainingTime(): number;
|
|
74
|
+
/**
|
|
75
|
+
* Consumes {@link RateLimit.remaining} by one if it's not limited, calling {@link RateLimit.reset} first if {@link RateLimit.expired} is true.
|
|
76
|
+
*/
|
|
77
|
+
consume(): this;
|
|
78
|
+
/**
|
|
79
|
+
* Resets the entry back to it's full state.
|
|
80
|
+
*/
|
|
81
|
+
reset(): this;
|
|
82
|
+
/**
|
|
83
|
+
* Resets the entry's {@link RateLimit.remaining} uses back to full state.
|
|
84
|
+
*/
|
|
85
|
+
resetRemaining(): this;
|
|
86
|
+
/**
|
|
87
|
+
* Resets the entry's {@link RateLimit.expires} to the current time plus {@link RateLimitManager.time}.
|
|
88
|
+
*/
|
|
89
|
+
resetTime(): this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { RateLimit, RateLimitManager };
|
package/dist/index.global.js
CHANGED
|
@@ -10,7 +10,7 @@ var SapphireRatelimits = (function (exports) {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// src/lib/RateLimit.ts
|
|
13
|
-
var
|
|
13
|
+
var _RateLimit = class _RateLimit {
|
|
14
14
|
/**
|
|
15
15
|
* @param manager The manager for this entry.
|
|
16
16
|
*/
|
|
@@ -80,10 +80,11 @@ var SapphireRatelimits = (function (exports) {
|
|
|
80
80
|
return this;
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
|
-
__name(
|
|
83
|
+
__name(_RateLimit, "RateLimit");
|
|
84
|
+
var RateLimit = _RateLimit;
|
|
84
85
|
|
|
85
86
|
// src/lib/RateLimitManager.ts
|
|
86
|
-
var _RateLimitManager = class extends Map {
|
|
87
|
+
var _RateLimitManager = class _RateLimitManager extends Map {
|
|
87
88
|
/**
|
|
88
89
|
* @param time The amount of milliseconds for the ratelimits from this manager to expire.
|
|
89
90
|
* @param limit The amount of times a {@link RateLimit} can drip before it's limited.
|
|
@@ -144,12 +145,12 @@ var SapphireRatelimits = (function (exports) {
|
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
147
|
};
|
|
147
|
-
|
|
148
|
-
__name(RateLimitManager, "RateLimitManager");
|
|
148
|
+
__name(_RateLimitManager, "RateLimitManager");
|
|
149
149
|
/**
|
|
150
150
|
* The delay in milliseconds for {@link RateLimitManager.sweepInterval}.
|
|
151
151
|
*/
|
|
152
|
-
__publicField(
|
|
152
|
+
__publicField(_RateLimitManager, "sweepIntervalDuration", 3e4);
|
|
153
|
+
var RateLimitManager = _RateLimitManager;
|
|
153
154
|
|
|
154
155
|
exports.RateLimit = RateLimit;
|
|
155
156
|
exports.RateLimitManager = RateLimitManager;
|
package/dist/index.global.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/RateLimit.ts","../src/lib/RateLimitManager.ts"],"names":[],"mappings":";;;;;;;;;AAEO,IAAM,
|
|
1
|
+
{"version":3,"sources":["../src/lib/RateLimit.ts","../src/lib/RateLimitManager.ts"],"names":[],"mappings":";;;;;;;;;AAEO,IAAM,aAAN,MAAM,WAAsB;AAAA;AAAA;AAAA;AAAA,EAmB3B,YAAY,SAA8B;AAfjD;AAAA;AAAA;AAAA,wBAAO;AAKP;AAAA;AAAA;AAAA,wBAAO;AAKP;AAAA;AAAA;AAAA,wBAAQ;AAMP,SAAK,UAAU;AACf,SAAK,MAAM;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,UAAmB;AAC7B,WAAO,KAAK,kBAAkB;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,UAAmB;AAC7B,WAAO,KAAK,cAAc,KAAK,CAAC,KAAK;AAAA,EACtC;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,gBAAwB;AAClC,WAAO,KAAK,IAAI,KAAK,UAAU,KAAK,IAAI,GAAG,CAAC;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA,EAKO,UAAgB;AACtB,QAAI,KAAK;AAAS,YAAM,IAAI,MAAM,iCAAiC;AACnE,QAAI,KAAK;AAAS,WAAK,MAAM;AAE7B,SAAK;AACL,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA,EAKO,QAAc;AACpB,WAAO,KAAK,eAAe,EAAE,UAAU;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA,EAKO,iBAAuB;AAC7B,SAAK,YAAY,KAAK,QAAQ;AAC9B,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA,EA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class RateLimit<K = string> {\n\t/**\n\t * The remaining amount of times this entry can be dripped before the bucket is empty.\n\t */\n\tpublic remaining!: number;\n\n\t/**\n\t * The timestamp that represents when this entry will reset back to a available state.\n\t */\n\tpublic expires!: number;\n\n\t/**\n\t * The {@link RateLimitManager} this entry is for.\n\t */\n\tprivate manager: RateLimitManager<K>;\n\n\t/**\n\t * @param manager The manager for this entry.\n\t */\n\tpublic constructor(manager: RateLimitManager<K>) {\n\t\tthis.manager = manager;\n\t\tthis.reset();\n\t}\n\n\t/**\n\t * Whether this entry is expired or not, allowing the bucket to be reset.\n\t */\n\tpublic get expired(): boolean {\n\t\treturn this.remainingTime === 0;\n\t}\n\n\t/**\n\t * Whether this entry is limited or not.\n\t */\n\tpublic get limited(): boolean {\n\t\treturn this.remaining === 0 && !this.expired;\n\t}\n\n\t/**\n\t * The remaining time in milliseconds before resetting.\n\t */\n\tpublic get remainingTime(): number {\n\t\treturn Math.max(this.expires - Date.now(), 0);\n\t}\n\n\t/**\n\t * Consumes {@link RateLimit.remaining} by one if it's not limited, calling {@link RateLimit.reset} first if {@link RateLimit.expired} is true.\n\t */\n\tpublic consume(): this {\n\t\tif (this.limited) throw new Error('Cannot consume a limited bucket');\n\t\tif (this.expired) this.reset();\n\n\t\tthis.remaining--;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Resets the entry back to it's full state.\n\t */\n\tpublic reset(): this {\n\t\treturn this.resetRemaining().resetTime();\n\t}\n\n\t/**\n\t * Resets the entry's {@link RateLimit.remaining} uses back to full state.\n\t */\n\tpublic resetRemaining(): this {\n\t\tthis.remaining = this.manager.limit;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Resets the entry's {@link RateLimit.expires} to the current time plus {@link RateLimitManager.time}.\n\t */\n\tpublic resetTime(): this {\n\t\tthis.expires = Date.now() + this.manager.time;\n\t\treturn this;\n\t}\n}\n","import { RateLimit } from './RateLimit';\n\nexport class RateLimitManager<K = string> extends Map<K, RateLimit<K>> {\n\t/**\n\t * The amount of milliseconds for the {@link RateLimit ratelimits} from this manager to expire.\n\t */\n\tpublic readonly time: number;\n\n\t/**\n\t * The amount of times a {@link RateLimit} can drip before it's limited.\n\t */\n\tpublic readonly limit: number;\n\n\t/**\n\t * The interval to sweep expired {@link RateLimit ratelimits}.\n\t */\n\tprivate sweepInterval!: NodeJS.Timer | null;\n\n\t/**\n\t * @param time The amount of milliseconds for the ratelimits from this manager to expire.\n\t * @param limit The amount of times a {@link RateLimit} can drip before it's limited.\n\t */\n\tpublic constructor(time: number, limit = 1) {\n\t\tsuper();\n\n\t\tthis.time = time;\n\t\tthis.limit = limit;\n\t}\n\n\t/**\n\t * Gets a {@link RateLimit} from this manager or creates it if it does not exist.\n\t * @param id The id for the {@link RateLimit}\n\t */\n\tpublic acquire(id: K): RateLimit<K> {\n\t\treturn this.get(id) ?? this.create(id);\n\t}\n\n\t/**\n\t * Creates a {@link RateLimit} for this manager.\n\t * @param id The id the {@link RateLimit} belongs to\n\t */\n\tpublic create(id: K): RateLimit<K> {\n\t\tconst value = new RateLimit(this);\n\t\tthis.set(id, value);\n\t\treturn value;\n\t}\n\n\t/**\n\t * Wraps Collection's set method to set interval to sweep inactive {@link RateLimit}s.\n\t * @param id The id the {@link RateLimit} belongs to\n\t * @param value The {@link RateLimit} to set\n\t */\n\tpublic override set(id: K, value: RateLimit<K>): this {\n\t\tthis.sweepInterval ??= setInterval(this.sweep.bind(this), RateLimitManager.sweepIntervalDuration);\n\t\treturn super.set(id, value);\n\t}\n\n\t/**\n\t * Wraps Collection's sweep method to clear the interval when this manager is empty.\n\t */\n\tpublic sweep(): void {\n\t\tfor (const [id, value] of this.entries()) {\n\t\t\tif (value.expired) this.delete(id);\n\t\t}\n\n\t\tif (this.size === 0 && this.sweepInterval !== null) {\n\t\t\tclearInterval(this.sweepInterval);\n\t\t\tthis.sweepInterval = null;\n\t\t}\n\t}\n\n\t/**\n\t * The delay in milliseconds for {@link RateLimitManager.sweepInterval}.\n\t */\n\tpublic static sweepIntervalDuration = 30_000;\n}\n"]}
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ var __publicField = (obj, key, value) => {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
// src/lib/RateLimit.ts
|
|
12
|
-
var
|
|
12
|
+
var _RateLimit = class _RateLimit {
|
|
13
13
|
/**
|
|
14
14
|
* @param manager The manager for this entry.
|
|
15
15
|
*/
|
|
@@ -79,10 +79,11 @@ var RateLimit = class {
|
|
|
79
79
|
return this;
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
|
-
__name(
|
|
82
|
+
__name(_RateLimit, "RateLimit");
|
|
83
|
+
var RateLimit = _RateLimit;
|
|
83
84
|
|
|
84
85
|
// src/lib/RateLimitManager.ts
|
|
85
|
-
var _RateLimitManager = class extends Map {
|
|
86
|
+
var _RateLimitManager = class _RateLimitManager extends Map {
|
|
86
87
|
/**
|
|
87
88
|
* @param time The amount of milliseconds for the ratelimits from this manager to expire.
|
|
88
89
|
* @param limit The amount of times a {@link RateLimit} can drip before it's limited.
|
|
@@ -143,12 +144,12 @@ var _RateLimitManager = class extends Map {
|
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
};
|
|
146
|
-
|
|
147
|
-
__name(RateLimitManager, "RateLimitManager");
|
|
147
|
+
__name(_RateLimitManager, "RateLimitManager");
|
|
148
148
|
/**
|
|
149
149
|
* The delay in milliseconds for {@link RateLimitManager.sweepInterval}.
|
|
150
150
|
*/
|
|
151
|
-
__publicField(
|
|
151
|
+
__publicField(_RateLimitManager, "sweepIntervalDuration", 3e4);
|
|
152
|
+
var RateLimitManager = _RateLimitManager;
|
|
152
153
|
|
|
153
154
|
exports.RateLimit = RateLimit;
|
|
154
155
|
exports.RateLimitManager = RateLimitManager;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/RateLimit.ts","../src/lib/RateLimitManager.ts"],"names":[],"mappings":";;;;;;;;;AAEO,IAAM,
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A,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAQ;AASP,SAAK,OAAO;AACZ,SAAK,QAAQ;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,QAAQ,IAAqB;AACnC,WAAO,KAAK,IAAI,EAAE,KAAK,KAAK,OAAO,EAAE;AAAA,EACtC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,OAAO,IAAqB;AAClC,UAAM,QAAQ,IAAI,UAAU,IAAI;AAChC,SAAK,IAAI,IAAI,KAAK;AAClB,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOgB,IAAI,IAAO,OAA2B;AACrD,SAAK,kBAAL,KAAK,gBAAkB,YAAY,KAAK,MAAM,KAAK,IAAI,GAAG,kBAAiB,qBAAqB;AAChG,WAAO,MAAM,IAAI,IAAI,KAAK;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKO,QAAc;AACpB,eAAW,CAAC,IAAI,KAAK,KAAK,KAAK,QAAQ,GAAG;AACzC,UAAI,MAAM;AAAS,aAAK,OAAO,EAAE;AAAA,IAClC;AAEA,QAAI,KAAK,SAAS,KAAK,KAAK,kBAAkB,MAAM;AACnD,oBAAc,KAAK,aAAa;AAChC,WAAK,gBAAgB;AAAA,IACtB;AAAA,EACD;AAMD;AAzEuE;AAAA;AAAA;AAAA;AAwEtE,cAxEY,mBAwEE,yBAAwB;AAxEhC,IAAM,mBAAN","sourcesContent":["import type { RateLimitManager } from './RateLimitManager';\n\nexport class RateLimit<K = string> {\n\t/**\n\t * The remaining amount of times this entry can be dripped before the bucket is empty.\n\t */\n\tpublic remaining!: number;\n\n\t/**\n\t * The timestamp that represents when this entry will reset back to a available state.\n\t */\n\tpublic expires!: number;\n\n\t/**\n\t * The {@link RateLimitManager} this entry is for.\n\t */\n\tprivate manager: RateLimitManager<K>;\n\n\t/**\n\t * @param manager The manager for this entry.\n\t */\n\tpublic constructor(manager: RateLimitManager<K>) {\n\t\tthis.manager = manager;\n\t\tthis.reset();\n\t}\n\n\t/**\n\t * Whether this entry is expired or not, allowing the bucket to be reset.\n\t */\n\tpublic get expired(): boolean {\n\t\treturn this.remainingTime === 0;\n\t}\n\n\t/**\n\t * Whether this entry is limited or not.\n\t */\n\tpublic get limited(): boolean {\n\t\treturn this.remaining === 0 && !this.expired;\n\t}\n\n\t/**\n\t * The remaining time in milliseconds before resetting.\n\t */\n\tpublic get remainingTime(): number {\n\t\treturn Math.max(this.expires - Date.now(), 0);\n\t}\n\n\t/**\n\t * Consumes {@link RateLimit.remaining} by one if it's not limited, calling {@link RateLimit.reset} first if {@link RateLimit.expired} is true.\n\t */\n\tpublic consume(): this {\n\t\tif (this.limited) throw new Error('Cannot consume a limited bucket');\n\t\tif (this.expired) this.reset();\n\n\t\tthis.remaining--;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Resets the entry back to it's full state.\n\t */\n\tpublic reset(): this {\n\t\treturn this.resetRemaining().resetTime();\n\t}\n\n\t/**\n\t * Resets the entry's {@link RateLimit.remaining} uses back to full state.\n\t */\n\tpublic resetRemaining(): this {\n\t\tthis.remaining = this.manager.limit;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Resets the entry's {@link RateLimit.expires} to the current time plus {@link RateLimitManager.time}.\n\t */\n\tpublic resetTime(): this {\n\t\tthis.expires = Date.now() + this.manager.time;\n\t\treturn this;\n\t}\n}\n","import { RateLimit } from './RateLimit';\n\nexport class RateLimitManager<K = string> extends Map<K, RateLimit<K>> {\n\t/**\n\t * The amount of milliseconds for the {@link RateLimit ratelimits} from this manager to expire.\n\t */\n\tpublic readonly time: number;\n\n\t/**\n\t * The amount of times a {@link RateLimit} can drip before it's limited.\n\t */\n\tpublic readonly limit: number;\n\n\t/**\n\t * The interval to sweep expired {@link RateLimit ratelimits}.\n\t */\n\tprivate sweepInterval!: NodeJS.Timer | null;\n\n\t/**\n\t * @param time The amount of milliseconds for the ratelimits from this manager to expire.\n\t * @param limit The amount of times a {@link RateLimit} can drip before it's limited.\n\t */\n\tpublic constructor(time: number, limit = 1) {\n\t\tsuper();\n\n\t\tthis.time = time;\n\t\tthis.limit = limit;\n\t}\n\n\t/**\n\t * Gets a {@link RateLimit} from this manager or creates it if it does not exist.\n\t * @param id The id for the {@link RateLimit}\n\t */\n\tpublic acquire(id: K): RateLimit<K> {\n\t\treturn this.get(id) ?? this.create(id);\n\t}\n\n\t/**\n\t * Creates a {@link RateLimit} for this manager.\n\t * @param id The id the {@link RateLimit} belongs to\n\t */\n\tpublic create(id: K): RateLimit<K> {\n\t\tconst value = new RateLimit(this);\n\t\tthis.set(id, value);\n\t\treturn value;\n\t}\n\n\t/**\n\t * Wraps Collection's set method to set interval to sweep inactive {@link RateLimit}s.\n\t * @param id The id the {@link RateLimit} belongs to\n\t * @param value The {@link RateLimit} to set\n\t */\n\tpublic override set(id: K, value: RateLimit<K>): this {\n\t\tthis.sweepInterval ??= setInterval(this.sweep.bind(this), RateLimitManager.sweepIntervalDuration);\n\t\treturn super.set(id, value);\n\t}\n\n\t/**\n\t * Wraps Collection's sweep method to clear the interval when this manager is empty.\n\t */\n\tpublic sweep(): void {\n\t\tfor (const [id, value] of this.entries()) {\n\t\t\tif (value.expired) this.delete(id);\n\t\t}\n\n\t\tif (this.size === 0 && this.sweepInterval !== null) {\n\t\t\tclearInterval(this.sweepInterval);\n\t\t\tthis.sweepInterval = null;\n\t\t}\n\t}\n\n\t/**\n\t * The delay in milliseconds for {@link RateLimitManager.sweepInterval}.\n\t */\n\tpublic static sweepIntervalDuration = 30_000;\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,7 @@ var __publicField = (obj, key, value) => {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
// src/lib/RateLimit.ts
|
|
10
|
-
var
|
|
10
|
+
var _RateLimit = class _RateLimit {
|
|
11
11
|
/**
|
|
12
12
|
* @param manager The manager for this entry.
|
|
13
13
|
*/
|
|
@@ -77,10 +77,11 @@ var RateLimit = class {
|
|
|
77
77
|
return this;
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
|
-
__name(
|
|
80
|
+
__name(_RateLimit, "RateLimit");
|
|
81
|
+
var RateLimit = _RateLimit;
|
|
81
82
|
|
|
82
83
|
// src/lib/RateLimitManager.ts
|
|
83
|
-
var _RateLimitManager = class extends Map {
|
|
84
|
+
var _RateLimitManager = class _RateLimitManager extends Map {
|
|
84
85
|
/**
|
|
85
86
|
* @param time The amount of milliseconds for the ratelimits from this manager to expire.
|
|
86
87
|
* @param limit The amount of times a {@link RateLimit} can drip before it's limited.
|
|
@@ -141,12 +142,12 @@ var _RateLimitManager = class extends Map {
|
|
|
141
142
|
}
|
|
142
143
|
}
|
|
143
144
|
};
|
|
144
|
-
|
|
145
|
-
__name(RateLimitManager, "RateLimitManager");
|
|
145
|
+
__name(_RateLimitManager, "RateLimitManager");
|
|
146
146
|
/**
|
|
147
147
|
* The delay in milliseconds for {@link RateLimitManager.sweepInterval}.
|
|
148
148
|
*/
|
|
149
|
-
__publicField(
|
|
149
|
+
__publicField(_RateLimitManager, "sweepIntervalDuration", 3e4);
|
|
150
|
+
var RateLimitManager = _RateLimitManager;
|
|
150
151
|
|
|
151
152
|
export { RateLimit, RateLimitManager };
|
|
152
153
|
//# sourceMappingURL=out.js.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/RateLimit.ts","../src/lib/RateLimitManager.ts"],"names":[],"mappings":";;;;;;;;;AAEO,IAAM,
|
|
1
|
+
{"version":3,"sources":["../src/lib/RateLimit.ts","../src/lib/RateLimitManager.ts"],"names":[],"mappings":";;;;;;;;;AAEO,IAAM,aAAN,MAAM,WAAsB;AAAA;AAAA;AAAA;AAAA,EAmB3B,YAAY,SAA8B;AAfjD;AAAA;AAAA;AAAA,wBAAO;AAKP;AAAA;AAAA;AAAA,wBAAO;AAKP;AAAA;AAAA;AAAA,wBA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AA;AAAA;AAAA,EAMO,OAAO,IAAqB;AAClC,UAAM,QAAQ,IAAI,UAAU,IAAI;AAChC,SAAK,IAAI,IAAI,KAAK;AAClB,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOgB,IAAI,IAAO,OAA2B;AACrD,SAAK,kBAAL,KAAK,gBAAkB,YAAY,KAAK,MAAM,KAAK,IAAI,GAAG,kBAAiB,qBAAqB;AAChG,WAAO,MAAM,IAAI,IAAI,KAAK;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKO,QAAc;AACpB,eAAW,CAAC,IAAI,KAAK,KAAK,KAAK,QAAQ,GAAG;AACzC,UAAI,MAAM;AAAS,aAAK,OAAO,EAAE;AAAA,IAClC;AAEA,QAAI,KAAK,SAAS,KAAK,KAAK,kBAAkB,MAAM;AACnD,oBAAc,KAAK,aAAa;AAChC,WAAK,gBAAgB;AAAA,IACtB;AAAA,EACD;AAMD;AAzEuE;AAAA;AAAA;AAAA;AAwEtE,cAxEY,mBAwEE,yBAAwB;AAxEhC,IAAM,mBAAN","sourcesContent":["import type { RateLimitManager } from './RateLimitManager';\n\nexport class RateLimit<K = string> {\n\t/**\n\t * The remaining amount of times this entry can be dripped before the bucket is empty.\n\t */\n\tpublic remaining!: number;\n\n\t/**\n\t * The timestamp that represents when this entry will reset back to a available state.\n\t */\n\tpublic expires!: number;\n\n\t/**\n\t * The {@link RateLimitManager} this entry is for.\n\t */\n\tprivate manager: RateLimitManager<K>;\n\n\t/**\n\t * @param manager The manager for this entry.\n\t */\n\tpublic constructor(manager: RateLimitManager<K>) {\n\t\tthis.manager = manager;\n\t\tthis.reset();\n\t}\n\n\t/**\n\t * Whether this entry is expired or not, allowing the bucket to be reset.\n\t */\n\tpublic get expired(): boolean {\n\t\treturn this.remainingTime === 0;\n\t}\n\n\t/**\n\t * Whether this entry is limited or not.\n\t */\n\tpublic get limited(): boolean {\n\t\treturn this.remaining === 0 && !this.expired;\n\t}\n\n\t/**\n\t * The remaining time in milliseconds before resetting.\n\t */\n\tpublic get remainingTime(): number {\n\t\treturn Math.max(this.expires - Date.now(), 0);\n\t}\n\n\t/**\n\t * Consumes {@link RateLimit.remaining} by one if it's not limited, calling {@link RateLimit.reset} first if {@link RateLimit.expired} is true.\n\t */\n\tpublic consume(): this {\n\t\tif (this.limited) throw new Error('Cannot consume a limited bucket');\n\t\tif (this.expired) this.reset();\n\n\t\tthis.remaining--;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Resets the entry back to it's full state.\n\t */\n\tpublic reset(): this {\n\t\treturn this.resetRemaining().resetTime();\n\t}\n\n\t/**\n\t * Resets the entry's {@link RateLimit.remaining} uses back to full state.\n\t */\n\tpublic resetRemaining(): this {\n\t\tthis.remaining = this.manager.limit;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Resets the entry's {@link RateLimit.expires} to the current time plus {@link RateLimitManager.time}.\n\t */\n\tpublic resetTime(): this {\n\t\tthis.expires = Date.now() + this.manager.time;\n\t\treturn this;\n\t}\n}\n","import { RateLimit } from './RateLimit';\n\nexport class RateLimitManager<K = string> extends Map<K, RateLimit<K>> {\n\t/**\n\t * The amount of milliseconds for the {@link RateLimit ratelimits} from this manager to expire.\n\t */\n\tpublic readonly time: number;\n\n\t/**\n\t * The amount of times a {@link RateLimit} can drip before it's limited.\n\t */\n\tpublic readonly limit: number;\n\n\t/**\n\t * The interval to sweep expired {@link RateLimit ratelimits}.\n\t */\n\tprivate sweepInterval!: NodeJS.Timer | null;\n\n\t/**\n\t * @param time The amount of milliseconds for the ratelimits from this manager to expire.\n\t * @param limit The amount of times a {@link RateLimit} can drip before it's limited.\n\t */\n\tpublic constructor(time: number, limit = 1) {\n\t\tsuper();\n\n\t\tthis.time = time;\n\t\tthis.limit = limit;\n\t}\n\n\t/**\n\t * Gets a {@link RateLimit} from this manager or creates it if it does not exist.\n\t * @param id The id for the {@link RateLimit}\n\t */\n\tpublic acquire(id: K): RateLimit<K> {\n\t\treturn this.get(id) ?? this.create(id);\n\t}\n\n\t/**\n\t * Creates a {@link RateLimit} for this manager.\n\t * @param id The id the {@link RateLimit} belongs to\n\t */\n\tpublic create(id: K): RateLimit<K> {\n\t\tconst value = new RateLimit(this);\n\t\tthis.set(id, value);\n\t\treturn value;\n\t}\n\n\t/**\n\t * Wraps Collection's set method to set interval to sweep inactive {@link RateLimit}s.\n\t * @param id The id the {@link RateLimit} belongs to\n\t * @param value The {@link RateLimit} to set\n\t */\n\tpublic override set(id: K, value: RateLimit<K>): this {\n\t\tthis.sweepInterval ??= setInterval(this.sweep.bind(this), RateLimitManager.sweepIntervalDuration);\n\t\treturn super.set(id, value);\n\t}\n\n\t/**\n\t * Wraps Collection's sweep method to clear the interval when this manager is empty.\n\t */\n\tpublic sweep(): void {\n\t\tfor (const [id, value] of this.entries()) {\n\t\t\tif (value.expired) this.delete(id);\n\t\t}\n\n\t\tif (this.size === 0 && this.sweepInterval !== null) {\n\t\t\tclearInterval(this.sweepInterval);\n\t\t\tthis.sweepInterval = null;\n\t\t}\n\t}\n\n\t/**\n\t * The delay in milliseconds for {@link RateLimitManager.sweepInterval}.\n\t */\n\tpublic static sweepIntervalDuration = 30_000;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/ratelimits",
|
|
3
|
-
"version": "2.4.7-next.
|
|
3
|
+
"version": "2.4.7-next.141fc267.0",
|
|
4
4
|
"description": "Bucket implementation for Ratelimits.",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
"access": "public"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@favware/cliff-jumper": "^2.
|
|
60
|
-
"@vitest/coverage-
|
|
61
|
-
"tsup": "^
|
|
62
|
-
"typedoc": "^0.24.
|
|
63
|
-
"typedoc-json-parser": "^
|
|
64
|
-
"typescript": "^5.
|
|
65
|
-
"vitest": "^0.
|
|
59
|
+
"@favware/cliff-jumper": "^2.1.1",
|
|
60
|
+
"@vitest/coverage-v8": "^0.34.1",
|
|
61
|
+
"tsup": "^7.2.0",
|
|
62
|
+
"typedoc": "^0.24.8",
|
|
63
|
+
"typedoc-json-parser": "^8.2.0",
|
|
64
|
+
"typescript": "^5.1.6",
|
|
65
|
+
"vitest": "^0.34.1"
|
|
66
66
|
}
|
|
67
67
|
}
|