@ooneex/rate-limit 1.0.0 → 1.0.2
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 +3 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +3 -3
- package/package.json +8 -7
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Exception } from "@ooneex/exception";
|
|
|
2
2
|
declare class RateLimitException extends Exception {
|
|
3
3
|
constructor(message: string, data?: Record<string, unknown>);
|
|
4
4
|
}
|
|
5
|
+
import { AppEnv } from "@ooneex/app-env";
|
|
5
6
|
type RateLimiterClassType = new (...args: any[]) => IRateLimiter;
|
|
6
7
|
type RedisRateLimiterOptionsType = {
|
|
7
8
|
connectionString?: string;
|
|
@@ -25,8 +26,9 @@ interface IRateLimiter {
|
|
|
25
26
|
getCount: (key: string) => Promise<number>;
|
|
26
27
|
}
|
|
27
28
|
declare class RedisRateLimiter implements IRateLimiter {
|
|
29
|
+
private readonly env;
|
|
28
30
|
private client;
|
|
29
|
-
constructor(options?: RedisRateLimiterOptionsType);
|
|
31
|
+
constructor(env: AppEnv, options?: RedisRateLimiterOptionsType);
|
|
30
32
|
private connect;
|
|
31
33
|
private getKey;
|
|
32
34
|
check(key: string, limit: number, windowSeconds: number): Promise<RateLimitResultType>;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var
|
|
2
|
+
var g=function(o,e,r,s){var t=arguments.length,n=t<3?e:s===null?s=Object.getOwnPropertyDescriptor(e,r):s,u;if(typeof Reflect==="object"&&typeof Reflect.decorate==="function")n=Reflect.decorate(o,e,r,s);else for(var a=o.length-1;a>=0;a--)if(u=o[a])n=(t<3?u(n):t>3?u(e,r,n):u(e,r))||n;return t>3&&n&&Object.defineProperty(e,r,n),n},l=(o,e)=>(r,s)=>e(r,s,o),x=(o,e)=>{if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(o,e)};import{Exception as p}from"@ooneex/exception";import{HttpStatus as R}from"@ooneex/http-status";class b extends p{constructor(o,e={}){super(o,{status:R.Code.TooManyRequests,data:e});this.name="RateLimitException"}}import{AppEnv as f}from"@ooneex/app-env";import{inject as T,injectable as P}from"@ooneex/container";class m{env;client;constructor(o,e={}){this.env=o;let r=e.connectionString||this.env.RATE_LIMIT_REDIS_URL;if(!r)throw new b("Redis connection string is required. Please provide a connection string either through the constructor options or set the RATE_LIMIT_REDIS_URL environment variable.");let{connectionString:s,...t}=e,u={...{connectionTimeout:1e4,idleTimeout:30000,autoReconnect:!0,maxRetries:3,enableOfflineQueue:!0,enableAutoPipelining:!0},...t};this.client=new Bun.RedisClient(r,u)}async connect(){if(!this.client.connected)await this.client.connect()}getKey(o){return`ratelimit:${o}`}async check(o,e,r){try{await this.connect();let s=this.getKey(o),t=await this.client.incr(s);if(t===1)await this.client.expire(s,r);let n=await this.client.ttl(s),u=new Date(Date.now()+n*1000);return{limited:t>e,remaining:Math.max(0,e-t),total:e,resetAt:u}}catch(s){throw new b(`Failed to check rate limit for key "${o}": ${s}`)}}async reset(o){try{await this.connect();let e=this.getKey(o);return await this.client.del(e)>0}catch(e){throw new b(`Failed to reset rate limit for key "${o}": ${e}`)}}async getCount(o){try{await this.connect();let e=this.getKey(o),r=await this.client.get(e);if(r===null)return 0;return Number.parseInt(r,10)}catch(e){throw new b(`Failed to get count for key "${o}": ${e}`)}}}m=g([P(),l(0,T(f)),x("design:paramtypes",[typeof f==="undefined"?Object:f,typeof RedisRateLimiterOptionsType==="undefined"?Object:RedisRateLimiterOptionsType])],m);export{m as RedisRateLimiter,b as RateLimitException};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=CDAEEEAB4BEE49B564756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
"sources": ["src/RateLimitException.ts", "src/RedisRateLimiter.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class RateLimitException extends Exception {\n constructor(message: string, data: Record<string, unknown> = {}) {\n super(message, {\n status: HttpStatus.Code.TooManyRequests,\n data,\n });\n this.name = \"RateLimitException\";\n }\n}\n",
|
|
6
|
-
"import { injectable } from \"@ooneex/container\";\nimport { RateLimitException } from \"./RateLimitException\";\nimport type { IRateLimiter, RateLimitResultType, RedisRateLimiterOptionsType } from \"./types\";\n\n@injectable()\nexport class RedisRateLimiter implements IRateLimiter {\n private client: Bun.RedisClient;\n\n constructor(options: RedisRateLimiterOptionsType = {}) {\n const connectionString = options.connectionString ||
|
|
6
|
+
"import { AppEnv } from \"@ooneex/app-env\";\nimport { inject, injectable } from \"@ooneex/container\";\nimport { RateLimitException } from \"./RateLimitException\";\nimport type { IRateLimiter, RateLimitResultType, RedisRateLimiterOptionsType } from \"./types\";\n\n@injectable()\nexport class RedisRateLimiter implements IRateLimiter {\n private client: Bun.RedisClient;\n\n constructor(\n @inject(AppEnv) private readonly env: AppEnv,\n options: RedisRateLimiterOptionsType = {},\n ) {\n const connectionString = options.connectionString || this.env.RATE_LIMIT_REDIS_URL;\n\n if (!connectionString) {\n throw new RateLimitException(\n \"Redis connection string is required. Please provide a connection string either through the constructor options or set the RATE_LIMIT_REDIS_URL environment variable.\",\n );\n }\n\n const { connectionString: _, ...userOptions } = options;\n\n const defaultOptions = {\n connectionTimeout: 10_000,\n idleTimeout: 30_000,\n autoReconnect: true,\n maxRetries: 3,\n enableOfflineQueue: true,\n enableAutoPipelining: true,\n };\n\n const clientOptions = { ...defaultOptions, ...userOptions };\n\n this.client = new Bun.RedisClient(connectionString, clientOptions);\n }\n\n private async connect(): Promise<void> {\n if (!this.client.connected) {\n await this.client.connect();\n }\n }\n\n private getKey(key: string): string {\n return `ratelimit:${key}`;\n }\n\n public async check(key: string, limit: number, windowSeconds: number): Promise<RateLimitResultType> {\n try {\n await this.connect();\n\n const rateLimitKey = this.getKey(key);\n\n // Increment counter\n const count = await this.client.incr(rateLimitKey);\n\n // Set expiry if this is the first request in window\n if (count === 1) {\n await this.client.expire(rateLimitKey, windowSeconds);\n }\n\n // Get TTL for reset time calculation\n const ttl = await this.client.ttl(rateLimitKey);\n const resetAt = new Date(Date.now() + ttl * 1000);\n\n return {\n limited: count > limit,\n remaining: Math.max(0, limit - count),\n total: limit,\n resetAt,\n };\n } catch (error) {\n throw new RateLimitException(`Failed to check rate limit for key \"${key}\": ${error}`);\n }\n }\n\n public async reset(key: string): Promise<boolean> {\n try {\n await this.connect();\n\n const rateLimitKey = this.getKey(key);\n const result = await this.client.del(rateLimitKey);\n\n return result > 0;\n } catch (error) {\n throw new RateLimitException(`Failed to reset rate limit for key \"${key}\": ${error}`);\n }\n }\n\n public async getCount(key: string): Promise<number> {\n try {\n await this.connect();\n\n const rateLimitKey = this.getKey(key);\n const value = await this.client.get(rateLimitKey);\n\n if (value === null) {\n return 0;\n }\n\n return Number.parseInt(value, 10);\n } catch (error) {\n throw new RateLimitException(`Failed to get count for key \"${key}\": ${error}`);\n }\n }\n}\n"
|
|
7
7
|
],
|
|
8
|
-
"mappings": ";
|
|
9
|
-
"debugId": "
|
|
8
|
+
"mappings": ";8cAAA,oBAAS,0BACT,qBAAS,4BAEF,MAAM,UAA2B,CAAU,CAChD,WAAW,CAAC,EAAiB,EAAgC,CAAC,EAAG,CAC/D,MAAM,EAAS,CACb,OAAQ,EAAW,KAAK,gBACxB,MACF,CAAC,EACD,KAAK,KAAO,qBAEhB,CCXA,iBAAS,wBACT,iBAAS,gBAAQ,0BAKV,MAAM,CAAyC,CAIjB,IAH3B,OAER,WAAW,CACwB,EACjC,EAAuC,CAAC,EACxC,CAFiC,WAGjC,IAAM,EAAmB,EAAQ,kBAAoB,KAAK,IAAI,qBAE9D,GAAI,CAAC,EACH,MAAM,IAAI,EACR,sKACF,EAGF,IAAQ,iBAAkB,KAAM,GAAgB,EAW1C,EAAgB,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S,GAAO,GAGnF,CAnGa,EAAN,GADN,EAAW,EAKP,MAAO,CAAM,GAJX,8IAAM",
|
|
9
|
+
"debugId": "CDAEEEAB4BEE49B564756E2164756E21",
|
|
10
10
|
"names": []
|
|
11
11
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/rate-limit",
|
|
3
3
|
"description": "API rate limiting middleware with configurable throttling strategies, sliding window counters, and per-client request quota enforcement",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -25,16 +25,17 @@
|
|
|
25
25
|
"test": "bun test tests",
|
|
26
26
|
"build": "bunup",
|
|
27
27
|
"lint": "tsgo --noEmit && bunx biome lint",
|
|
28
|
-
"npm:publish": "bun publish --tolerate-republish --access public"
|
|
28
|
+
"npm:publish": "bun publish --tolerate-republish --force --production --access public"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@ooneex/exception": "
|
|
32
|
-
"@ooneex/http-status": "
|
|
31
|
+
"@ooneex/exception": "1.0.1",
|
|
32
|
+
"@ooneex/http-status": "1.0.1"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@ooneex/
|
|
36
|
-
"@ooneex/
|
|
37
|
-
"@ooneex/
|
|
35
|
+
"@ooneex/app-env": "1.0.2",
|
|
36
|
+
"@ooneex/exception": "1.0.1",
|
|
37
|
+
"@ooneex/container": "1.0.1",
|
|
38
|
+
"@ooneex/http-status": "1.0.1"
|
|
38
39
|
},
|
|
39
40
|
"keywords": [
|
|
40
41
|
"api",
|