@stimulcross/rate-limiter 0.0.3 → 0.0.5
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/lib/errors/rate-limit.error.d.ts +2 -1
- package/lib/errors/rate-limit.error.js +2 -1
- package/lib/errors/rate-limiter-destroyed.error.d.ts +2 -1
- package/lib/errors/rate-limiter-destroyed.error.js +2 -1
- package/lib/interfaces/rate-limiter-options.d.ts +1 -1
- package/lib/limiters/abstract-rate-limiter.js +2 -2
- package/lib/limiters/http-response-based/http-response-based.limiter.js +1 -1
- package/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CustomError } from './custom.error.js';
|
|
1
2
|
import { RateLimitErrorCode } from '../enums/rate-limit-error-code.js';
|
|
2
3
|
export interface RateLimitErrorPlainObject extends Error {
|
|
3
4
|
code: RateLimitErrorCode;
|
|
@@ -17,7 +18,7 @@ export interface RateLimitErrorPlainObject extends Error {
|
|
|
17
18
|
* - `DESTROYED` - When the task is destroyed due to the rate limiter's `clear()` or `destroy()` methods.
|
|
18
19
|
* - `CANCELLED` - When the task is cancelled using an abort signal.
|
|
19
20
|
*/
|
|
20
|
-
export declare class RateLimitError extends
|
|
21
|
+
export declare class RateLimitError extends CustomError {
|
|
21
22
|
private readonly _code;
|
|
22
23
|
private readonly _retryAt;
|
|
23
24
|
/** @internal */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CustomError } from './custom.error.js';
|
|
1
2
|
import { RateLimitErrorCode } from '../enums/rate-limit-error-code.js';
|
|
2
3
|
/**
|
|
3
4
|
* An error thrown when a rate limit is exceeded.
|
|
@@ -13,7 +14,7 @@ import { RateLimitErrorCode } from '../enums/rate-limit-error-code.js';
|
|
|
13
14
|
* - `DESTROYED` - When the task is destroyed due to the rate limiter's `clear()` or `destroy()` methods.
|
|
14
15
|
* - `CANCELLED` - When the task is cancelled using an abort signal.
|
|
15
16
|
*/
|
|
16
|
-
export class RateLimitError extends
|
|
17
|
+
export class RateLimitError extends CustomError {
|
|
17
18
|
_code;
|
|
18
19
|
_retryAt;
|
|
19
20
|
/** @internal */
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { CustomError } from './custom.error.js';
|
|
1
2
|
/**
|
|
2
3
|
* An error thrown when running a task on a destroyed rate limiter.
|
|
3
4
|
*/
|
|
4
|
-
export declare class RateLimiterDestroyedError extends
|
|
5
|
+
export declare class RateLimiterDestroyedError extends CustomError {
|
|
5
6
|
constructor();
|
|
6
7
|
}
|
|
7
8
|
//# sourceMappingURL=rate-limiter-destroyed.error.d.ts.map
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { CustomError } from './custom.error.js';
|
|
1
2
|
/**
|
|
2
3
|
* An error thrown when running a task on a destroyed rate limiter.
|
|
3
4
|
*/
|
|
4
|
-
export class RateLimiterDestroyedError extends
|
|
5
|
+
export class RateLimiterDestroyedError extends CustomError {
|
|
5
6
|
constructor() {
|
|
6
7
|
super('Rate limiter has been destroyed and cannot be used.');
|
|
7
8
|
}
|
|
@@ -18,7 +18,7 @@ export class AbstractRateLimiter {
|
|
|
18
18
|
_generateId;
|
|
19
19
|
_isDestroyed = false;
|
|
20
20
|
constructor(options) {
|
|
21
|
-
this._logger = createLogger(new.target.name,
|
|
21
|
+
this._logger = createLogger({ context: new.target.name, minLevel: 'WARNING', ...options?.loggerOptions });
|
|
22
22
|
this._clock = options?.clock ?? defaultClock;
|
|
23
23
|
this._store = options?.store ?? new InMemoryStateStore(this._clock);
|
|
24
24
|
this._executor = new RateLimiterExecutor(this._logger, this._clock, options?.queue);
|
|
@@ -71,8 +71,8 @@ export class AbstractRateLimiter {
|
|
|
71
71
|
return this._logger.minLevel >= LogLevel.DEBUG;
|
|
72
72
|
}
|
|
73
73
|
async _execute(fn, runAt, storeTtlMs, ctx, expiresAt) {
|
|
74
|
-
await this._ensureCanExecute(ctx);
|
|
75
74
|
try {
|
|
75
|
+
await this._ensureCanExecute(ctx);
|
|
76
76
|
return await this._executor.execute(fn, runAt, {
|
|
77
77
|
id: ctx.id,
|
|
78
78
|
key: ctx.key,
|
|
@@ -45,7 +45,7 @@ export class HttpResponseBasedLimiter {
|
|
|
45
45
|
_fallbackResetDelayMs;
|
|
46
46
|
_isDestroyed = false;
|
|
47
47
|
constructor(options) {
|
|
48
|
-
this._logger = createLogger(new.target.name,
|
|
48
|
+
this._logger = createLogger({ context: new.target.name, minLevel: 'WARNING', ...options.loggerOptions });
|
|
49
49
|
this._clock = options.clock ?? defaultClock;
|
|
50
50
|
this._store = options.store ?? new InMemoryStateStore(this._clock);
|
|
51
51
|
this._executor = new RateLimiterExecutor(this._logger, this._clock, options.queue);
|