@longzai-intelligence-auth/rate-limit 0.0.5 → 0.0.6

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.
@@ -0,0 +1,61 @@
1
+ import { RateLimitConfig, RateLimitResult, RateLimiterPort } from "@longzai-intelligence-auth/core";
2
+
3
+ //#region src/rate-limiter.d.ts
4
+ /**
5
+ * 基于内存的速率限制器实现
6
+ */
7
+ declare class RateLimiter implements RateLimiterPort {
8
+ /**
9
+ * 速率限制记录存储
10
+ */
11
+ private records;
12
+ /**
13
+ * 速率限制配置
14
+ */
15
+ private config;
16
+ /**
17
+ * 创建速率限制器实例
18
+ *
19
+ * @param config - 速率限制配置
20
+ */
21
+ constructor(config?: RateLimitConfig);
22
+ /**
23
+ * 检查速率限制
24
+ *
25
+ * @param key - 限制键(如用户ID、IP地址等)
26
+ * @returns 限制检查结果
27
+ */
28
+ checkLimit(key: string): Promise<RateLimitResult>;
29
+ /**
30
+ * 重置速率限制
31
+ *
32
+ * @param key - 限制键
33
+ */
34
+ reset(key: string): Promise<void>;
35
+ /**
36
+ * 清理过期的速率限制记录
37
+ */
38
+ cleanup(): void;
39
+ /**
40
+ * 获取当前配置
41
+ *
42
+ * @returns 速率限制配置
43
+ */
44
+ getConfig(): RateLimitConfig;
45
+ }
46
+ //#endregion
47
+ //#region src/index.d.ts
48
+ type MemoryRateLimitRecord = {
49
+ count: number;
50
+ windowStart: number;
51
+ };
52
+ type MemoryRateLimiter = {
53
+ check(key: string): Promise<boolean>;
54
+ getCount(key: string): number;
55
+ getResetTime(key: string): Date | null;
56
+ cleanup(): void;
57
+ getConfig(): RateLimitConfig;
58
+ };
59
+ declare function createMemoryRateLimiter(config?: RateLimitConfig): MemoryRateLimiter;
60
+ //#endregion
61
+ export { type MemoryRateLimitRecord, type MemoryRateLimiter, RateLimiter, createMemoryRateLimiter };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ const e={windowSeconds:60,maxRequests:100};var t=class{records=new Map;config;constructor(t=e){this.config=t}async checkLimit(e){let t=Date.now(),n=this.config.windowSeconds*1e3,r=this.records.get(e);return!r||t-r.windowStart>=n?(this.records.set(e,{count:1,windowStart:t}),{allowed:!0,remaining:this.config.maxRequests-1,resetAt:new Date(t+n)}):r.count>=this.config.maxRequests?{allowed:!1,remaining:0,resetAt:new Date(r.windowStart+n)}:(r.count++,{allowed:!0,remaining:this.config.maxRequests-r.count,resetAt:new Date(r.windowStart+n)})}async reset(e){this.records.delete(e)}cleanup(){let e=Date.now(),t=this.config.windowSeconds*1e3;for(let[n,r]of this.records.entries())e-r.windowStart>=t&&this.records.delete(n)}getConfig(){return this.config}};const n={windowSeconds:60,maxRequests:100};function r(e=n){let t=new Map;return{async check(n){let r=Date.now(),i=e.windowSeconds*1e3,a=t.get(n);return!a||r-a.windowStart>=i?(t.set(n,{count:1,windowStart:r}),!0):a.count>=e.maxRequests?!1:(a.count++,!0)},getCount(n){let r=Date.now(),i=e.windowSeconds*1e3,a=t.get(n);return!a||r-a.windowStart>=i?0:a.count},getResetTime(n){let r=t.get(n);if(!r)return null;let i=e.windowSeconds*1e3;return new Date(r.windowStart+i)},cleanup(){let n=Date.now(),r=e.windowSeconds*1e3;for(let[e,i]of t.entries())n-i.windowStart>=r&&t.delete(e)},getConfig(){return e}}}export{t as RateLimiter,r as createMemoryRateLimiter};
package/package.json CHANGED
@@ -1,22 +1,15 @@
1
1
  {
2
2
  "name": "@longzai-intelligence-auth/rate-limit",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "license": "UNLICENSED",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
- "main": "./dist/index.cjs",
8
- "module": "./dist/index.js",
7
+ "main": "./dist/index.js",
9
8
  "types": "./dist/index.d.ts",
10
9
  "exports": {
11
10
  ".": {
12
- "import": {
13
- "types": "./dist/index.d.ts",
14
- "default": "./dist/index.js"
15
- },
16
- "require": {
17
- "types": "./dist/index.d.cts",
18
- "default": "./dist/index.cjs"
19
- }
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
20
13
  }
21
14
  },
22
15
  "files": [
@@ -31,11 +24,10 @@
31
24
  "directory": "packages/rate-limit"
32
25
  },
33
26
  "dependencies": {
34
- "@longzai-intelligence-auth/core": "0.0.5"
27
+ "@longzai-intelligence-auth/core": "0.0.6"
35
28
  },
36
29
  "scripts": {
37
- "build": "bun build src/index.ts --outdir dist --target bun",
38
- "build:declaration": "tsgo --declaration --emitDeclarationOnly --outDir dist -p tsconfig/app.json",
30
+ "build": "tsgo --build tsconfig/build.json && resolve-aliases -p tsconfig/build.json",
39
31
  "build:prod": "NODE_ENV=production tsdown",
40
32
  "prepublishOnly": "bun run build:prod",
41
33
  "typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
@@ -49,9 +41,6 @@
49
41
  "test:coverage": "bun test --coverage",
50
42
  "test:unit": "bun test src/__tests__/unit/",
51
43
  "test:integration": "bun test src/__tests__/integration/",
52
- "clean": "rm -rf dist out .cache"
53
- },
54
- "devDependencies": {
55
- "@types/bun": "^1.3.14"
44
+ "clean": "rimraf dist out .cache"
56
45
  }
57
46
  }
package/dist/index.cjs DELETED
@@ -1 +0,0 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e={windowSeconds:60,maxRequests:100};function t(t=e){let n=new Map;return{async check(e){let r=Date.now(),i=t.windowSeconds*1e3,a=n.get(e);return!a||r-a.windowStart>=i?(n.set(e,{count:1,windowStart:r}),!0):a.count>=t.maxRequests?!1:(a.count++,!0)},getCount(e){let r=Date.now(),i=t.windowSeconds*1e3,a=n.get(e);return!a||r-a.windowStart>=i?0:a.count},getResetTime(e){let r=n.get(e);if(!r)return null;let i=t.windowSeconds*1e3;return new Date(r.windowStart+i)},cleanup(){let e=Date.now(),r=t.windowSeconds*1e3;for(let[t,i]of n.entries())e-i.windowStart>=r&&n.delete(t)},getConfig(){return t}}}exports.createMemoryRateLimiter=t;
package/dist/index.d.cts DELETED
@@ -1,17 +0,0 @@
1
- import { RateLimitConfig } from "@longzai-intelligence-auth/core";
2
-
3
- //#region src/index.d.ts
4
- type MemoryRateLimitRecord = {
5
- count: number;
6
- windowStart: number;
7
- };
8
- type MemoryRateLimiter = {
9
- check(key: string): Promise<boolean>;
10
- getCount(key: string): number;
11
- getResetTime(key: string): Date | null;
12
- cleanup(): void;
13
- getConfig(): RateLimitConfig;
14
- };
15
- declare function createMemoryRateLimiter(config?: RateLimitConfig): MemoryRateLimiter;
16
- //#endregion
17
- export { type MemoryRateLimitRecord, type MemoryRateLimiter, createMemoryRateLimiter };
package/dist/index.d.mts DELETED
@@ -1,17 +0,0 @@
1
- import { RateLimitConfig } from "@longzai-intelligence-auth/core";
2
-
3
- //#region src/index.d.ts
4
- type MemoryRateLimitRecord = {
5
- count: number;
6
- windowStart: number;
7
- };
8
- type MemoryRateLimiter = {
9
- check(key: string): Promise<boolean>;
10
- getCount(key: string): number;
11
- getResetTime(key: string): Date | null;
12
- cleanup(): void;
13
- getConfig(): RateLimitConfig;
14
- };
15
- declare function createMemoryRateLimiter(config?: RateLimitConfig): MemoryRateLimiter;
16
- //#endregion
17
- export { type MemoryRateLimitRecord, type MemoryRateLimiter, createMemoryRateLimiter };
package/dist/index.mjs DELETED
@@ -1 +0,0 @@
1
- const e={windowSeconds:60,maxRequests:100};function t(t=e){let n=new Map;return{async check(e){let r=Date.now(),i=t.windowSeconds*1e3,a=n.get(e);return!a||r-a.windowStart>=i?(n.set(e,{count:1,windowStart:r}),!0):a.count>=t.maxRequests?!1:(a.count++,!0)},getCount(e){let r=Date.now(),i=t.windowSeconds*1e3,a=n.get(e);return!a||r-a.windowStart>=i?0:a.count},getResetTime(e){let r=n.get(e);if(!r)return null;let i=t.windowSeconds*1e3;return new Date(r.windowStart+i)},cleanup(){let e=Date.now(),r=t.windowSeconds*1e3;for(let[t,i]of n.entries())e-i.windowStart>=r&&n.delete(t)},getConfig(){return t}}}export{t as createMemoryRateLimiter};