@nathapp/nestjs-throttler 4.2.2 → 4.2.4
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.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.js +18 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/redis-throttler.storage.d.ts +20 -0
- package/dist/storage/redis-throttler.storage.js +126 -0
- package/dist/storage/redis-throttler.storage.js.map +1 -0
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -18,4 +18,5 @@ __exportStar(require("./module"), exports);
|
|
|
18
18
|
__exportStar(require("./throttler.guard"), exports);
|
|
19
19
|
__exportStar(require("./interface"), exports);
|
|
20
20
|
__exportStar(require("./decorator"), exports);
|
|
21
|
+
__exportStar(require("./storage"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,oDAAkC;AAClC,8CAA4B;AAC5B,8CAA4B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,oDAAkC;AAClC,8CAA4B;AAC5B,8CAA4B;AAC5B,4CAA0B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './redis-throttler.storage';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./redis-throttler.storage"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/storage/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ThrottlerStorage } from '@nestjs/throttler';
|
|
2
|
+
type ThrottlerStorageRecord = Awaited<ReturnType<ThrottlerStorage['increment']>>;
|
|
3
|
+
export declare const DEFAULT_THROTTLER_KEY_PREFIX = "throttler:";
|
|
4
|
+
export interface RedisThrottlerClient {
|
|
5
|
+
eval(script: string, numKeys: number, ...args: (string | number)[]): Promise<unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface RedisThrottlerStorageOptions {
|
|
8
|
+
keyPrefix?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const THROTTLER_INCREMENT_SCRIPT = "local function toSeconds(ms)\n if ms < 0 then return 0 end\n return math.ceil(ms / 1000)\nend\nlocal limit = tonumber(ARGV[2])\nlocal blockDuration = tonumber(ARGV[3])\nlocal blockTtl = redis.call('PTTL', KEYS[2])\nif blockTtl >= 0 then\n return { limit + 1, toSeconds(redis.call('PTTL', KEYS[1])), 1, toSeconds(blockTtl) }\nend\nlocal totalHits = redis.call('INCR', KEYS[1])\nif totalHits == 1 then\n redis.call('PEXPIRE', KEYS[1], ARGV[1])\nend\nif totalHits > limit then\n if blockDuration > 0 then\n redis.call('SET', KEYS[2], '1', 'PX', ARGV[3])\n redis.call('DEL', KEYS[1])\n return { limit + 1, 0, 1, toSeconds(blockDuration) }\n end\n return { totalHits, toSeconds(redis.call('PTTL', KEYS[1])), 1, 0 }\nend\nreturn { totalHits, toSeconds(redis.call('PTTL', KEYS[1])), 0, 0 }";
|
|
11
|
+
export declare class RedisThrottlerStorage implements ThrottlerStorage {
|
|
12
|
+
private readonly _client;
|
|
13
|
+
private readonly _keyPrefix;
|
|
14
|
+
constructor(_client: RedisThrottlerClient, options?: RedisThrottlerStorageOptions);
|
|
15
|
+
private hitsKey;
|
|
16
|
+
private blockKey;
|
|
17
|
+
increment(key: string, ttl: number, limit: number, blockDuration: number, throttlerName: string): Promise<ThrottlerStorageRecord>;
|
|
18
|
+
private parseReply;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RedisThrottlerStorage = exports.THROTTLER_INCREMENT_SCRIPT = exports.DEFAULT_THROTTLER_KEY_PREFIX = void 0;
|
|
4
|
+
exports.DEFAULT_THROTTLER_KEY_PREFIX = 'throttler:';
|
|
5
|
+
const MAX_REPLY_DEPTH = 2;
|
|
6
|
+
const MAX_REPLY_TEXT = 180;
|
|
7
|
+
function describeReply(reply) {
|
|
8
|
+
const rendered = renderReply(reply, 0);
|
|
9
|
+
return rendered.length > MAX_REPLY_TEXT ? `${rendered.slice(0, MAX_REPLY_TEXT)}…` : rendered;
|
|
10
|
+
}
|
|
11
|
+
function renderReply(reply, depth) {
|
|
12
|
+
try {
|
|
13
|
+
if (Array.isArray(reply)) {
|
|
14
|
+
const length = readLength(reply);
|
|
15
|
+
if (!Number.isFinite(length))
|
|
16
|
+
return '<array of unreadable length>';
|
|
17
|
+
const slots = [];
|
|
18
|
+
for (let index = 0; index < length; index += 1) {
|
|
19
|
+
try {
|
|
20
|
+
slots.push(depth >= MAX_REPLY_DEPTH ? '<nested>' : renderReply(reply[index], depth + 1));
|
|
21
|
+
}
|
|
22
|
+
catch (_unreadableSlot) {
|
|
23
|
+
slots.push('<unreadable>');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return `[${slots.join(',')}]`;
|
|
27
|
+
}
|
|
28
|
+
if (reply === null)
|
|
29
|
+
return 'null';
|
|
30
|
+
if (reply === undefined)
|
|
31
|
+
return 'undefined';
|
|
32
|
+
const kind = typeof reply;
|
|
33
|
+
if (kind === 'bigint')
|
|
34
|
+
return `${String(reply)}n`;
|
|
35
|
+
if (kind !== 'object')
|
|
36
|
+
return String(reply);
|
|
37
|
+
const rendered = JSON.stringify(reply);
|
|
38
|
+
if (typeof rendered === 'string')
|
|
39
|
+
return rendered;
|
|
40
|
+
}
|
|
41
|
+
catch (_unrenderable) {
|
|
42
|
+
}
|
|
43
|
+
return `<unrenderable ${typeof reply}>`;
|
|
44
|
+
}
|
|
45
|
+
function readSlotAsNumber(reply, index) {
|
|
46
|
+
try {
|
|
47
|
+
return Number(reply[index]);
|
|
48
|
+
}
|
|
49
|
+
catch (_unreadable) {
|
|
50
|
+
return Number.NaN;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function readLength(reply) {
|
|
54
|
+
try {
|
|
55
|
+
return reply.length;
|
|
56
|
+
}
|
|
57
|
+
catch (_unreadable) {
|
|
58
|
+
return Number.NaN;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.THROTTLER_INCREMENT_SCRIPT = `local function toSeconds(ms)
|
|
62
|
+
if ms < 0 then return 0 end
|
|
63
|
+
return math.ceil(ms / 1000)
|
|
64
|
+
end
|
|
65
|
+
local limit = tonumber(ARGV[2])
|
|
66
|
+
local blockDuration = tonumber(ARGV[3])
|
|
67
|
+
local blockTtl = redis.call('PTTL', KEYS[2])
|
|
68
|
+
if blockTtl >= 0 then
|
|
69
|
+
return { limit + 1, toSeconds(redis.call('PTTL', KEYS[1])), 1, toSeconds(blockTtl) }
|
|
70
|
+
end
|
|
71
|
+
local totalHits = redis.call('INCR', KEYS[1])
|
|
72
|
+
if totalHits == 1 then
|
|
73
|
+
redis.call('PEXPIRE', KEYS[1], ARGV[1])
|
|
74
|
+
end
|
|
75
|
+
if totalHits > limit then
|
|
76
|
+
if blockDuration > 0 then
|
|
77
|
+
redis.call('SET', KEYS[2], '1', 'PX', ARGV[3])
|
|
78
|
+
redis.call('DEL', KEYS[1])
|
|
79
|
+
return { limit + 1, 0, 1, toSeconds(blockDuration) }
|
|
80
|
+
end
|
|
81
|
+
return { totalHits, toSeconds(redis.call('PTTL', KEYS[1])), 1, 0 }
|
|
82
|
+
end
|
|
83
|
+
return { totalHits, toSeconds(redis.call('PTTL', KEYS[1])), 0, 0 }`;
|
|
84
|
+
class RedisThrottlerStorage {
|
|
85
|
+
constructor(_client, options) {
|
|
86
|
+
var _a;
|
|
87
|
+
this._client = _client;
|
|
88
|
+
this._keyPrefix = (_a = options === null || options === void 0 ? void 0 : options.keyPrefix) !== null && _a !== void 0 ? _a : exports.DEFAULT_THROTTLER_KEY_PREFIX;
|
|
89
|
+
}
|
|
90
|
+
hitsKey(key, throttlerName) {
|
|
91
|
+
return `${this._keyPrefix}{${throttlerName}:${key}}:hits`;
|
|
92
|
+
}
|
|
93
|
+
blockKey(key, throttlerName) {
|
|
94
|
+
return `${this._keyPrefix}{${throttlerName}:${key}}:block`;
|
|
95
|
+
}
|
|
96
|
+
async increment(key, ttl, limit, blockDuration, throttlerName) {
|
|
97
|
+
const reply = await this._client.eval(exports.THROTTLER_INCREMENT_SCRIPT, 2, this.hitsKey(key, throttlerName), this.blockKey(key, throttlerName), String(ttl), String(limit), String(blockDuration));
|
|
98
|
+
return this.parseReply(reply, key);
|
|
99
|
+
}
|
|
100
|
+
parseReply(reply, key) {
|
|
101
|
+
if (!Array.isArray(reply) || readLength(reply) !== 4) {
|
|
102
|
+
throw new Error(`RedisThrottlerStorage: unexpected EVAL reply for key "${key}": ` +
|
|
103
|
+
`expected an array of four numbers, received ${describeReply(reply)}`);
|
|
104
|
+
}
|
|
105
|
+
const elements = reply;
|
|
106
|
+
const values = [
|
|
107
|
+
readSlotAsNumber(elements, 0),
|
|
108
|
+
readSlotAsNumber(elements, 1),
|
|
109
|
+
readSlotAsNumber(elements, 2),
|
|
110
|
+
readSlotAsNumber(elements, 3),
|
|
111
|
+
];
|
|
112
|
+
if (!values.every((value) => Number.isFinite(value))) {
|
|
113
|
+
throw new Error(`RedisThrottlerStorage: unexpected EVAL reply for key "${key}": ` +
|
|
114
|
+
`every one of the four values must be a finite number, received ${describeReply(reply)}`);
|
|
115
|
+
}
|
|
116
|
+
const [totalHits, timeToExpire, isBlocked, timeToBlockExpire] = values;
|
|
117
|
+
return {
|
|
118
|
+
totalHits,
|
|
119
|
+
timeToExpire,
|
|
120
|
+
isBlocked: isBlocked === 1,
|
|
121
|
+
timeToBlockExpire,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.RedisThrottlerStorage = RedisThrottlerStorage;
|
|
126
|
+
//# sourceMappingURL=redis-throttler.storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-throttler.storage.js","sourceRoot":"","sources":["../../lib/storage/redis-throttler.storage.ts"],"names":[],"mappings":";;;AASa,QAAA,4BAA4B,GAAG,YAAY,CAAC;AAyBzD,MAAM,eAAe,GAAG,CAAC,CAAC;AAG1B,MAAM,cAAc,GAAG,GAAG,CAAC;AAS3B,SAAS,aAAa,CAAC,KAAc;IACnC,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACvC,OAAO,QAAQ,CAAC,MAAM,GAAG,cAAc,CAAC,CAAC,CAAC,GAAG,QAAQ,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,YAAY;YACZ,SAAS,EAAE,SAAS,KAAK,CAAC;YAC1B,iBAAiB;SAClB,CAAC;IACJ,CAAC;CACF;AArFD,sDAqFC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-throttler",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.4",
|
|
4
4
|
"description": "NestJS Throttler with trusted proxy-aware IP resolution",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@nathapp/eslint-config": "1.0.1",
|
|
32
|
-
"@nathapp/nestjs-common": "^4.2.
|
|
32
|
+
"@nathapp/nestjs-common": "^4.2.4",
|
|
33
33
|
"@nathapp/typescript-config": "1.0.1",
|
|
34
34
|
"@nestjs/common": "^11.0.0",
|
|
35
35
|
"@nestjs/core": "^11.0.0",
|
|
@@ -41,7 +41,8 @@
|
|
|
41
41
|
"@types/supertest": "^6.0.3",
|
|
42
42
|
"eslint": "^8.57.0",
|
|
43
43
|
"express": "^5.2.1",
|
|
44
|
-
"fastify": "^5.12.
|
|
44
|
+
"fastify": "^5.12.8",
|
|
45
|
+
"ioredis": "^5.4.1",
|
|
45
46
|
"jest": "^30.5.2",
|
|
46
47
|
"reflect-metadata": "^0.1.13",
|
|
47
48
|
"rxjs": "^7.8.0",
|