@longzai-intelligence-auth/rate-limit 0.0.2 → 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/dist/index.cjs ADDED
@@ -0,0 +1 @@
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;
@@ -0,0 +1,17 @@
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 };
@@ -0,0 +1,17 @@
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 ADDED
@@ -0,0 +1 @@
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longzai-intelligence-auth/rate-limit",
3
- "version": "0.0.2",
3
+ "version": "0.0.5",
4
4
  "license": "UNLICENSED",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -31,7 +31,7 @@
31
31
  "directory": "packages/rate-limit"
32
32
  },
33
33
  "dependencies": {
34
- "@longzai-intelligence-auth/core": "0.0.1"
34
+ "@longzai-intelligence-auth/core": "0.0.5"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "bun build src/index.ts --outdir dist --target bun",
@@ -47,6 +47,11 @@
47
47
  "test": "bun test",
48
48
  "test:watch": "bun test --watch",
49
49
  "test:coverage": "bun test --coverage",
50
+ "test:unit": "bun test src/__tests__/unit/",
51
+ "test:integration": "bun test src/__tests__/integration/",
50
52
  "clean": "rm -rf dist out .cache"
53
+ },
54
+ "devDependencies": {
55
+ "@types/bun": "^1.3.14"
51
56
  }
52
57
  }
package/dist/index.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { RateLimitConfig } from "@longzai-intelligence-auth/core";
2
- type MemoryRateLimitRecord = {
3
- count: number;
4
- windowStart: number;
5
- };
6
- type MemoryRateLimiter = {
7
- check(key: string): Promise<boolean>;
8
- getCount(key: string): number;
9
- getResetTime(key: string): Date | null;
10
- cleanup(): void;
11
- getConfig(): RateLimitConfig;
12
- };
13
- declare function createMemoryRateLimiter(config?: RateLimitConfig): MemoryRateLimiter;
14
- export type { MemoryRateLimitRecord, MemoryRateLimiter };
15
- export { createMemoryRateLimiter };
package/dist/index.js DELETED
@@ -1,56 +0,0 @@
1
- const DEFAULT_RATE_LIMIT_CONFIG = {
2
- windowSeconds: 60,
3
- maxRequests: 100,
4
- };
5
- function createMemoryRateLimiter(config = DEFAULT_RATE_LIMIT_CONFIG) {
6
- const records = new Map();
7
- return {
8
- async check(key) {
9
- const now = Date.now();
10
- const windowMs = config.windowSeconds * 1000;
11
- const record = records.get(key);
12
- if (!record || now - record.windowStart >= windowMs) {
13
- records.set(key, {
14
- count: 1,
15
- windowStart: now,
16
- });
17
- return true;
18
- }
19
- if (record.count >= config.maxRequests) {
20
- return false;
21
- }
22
- record.count++;
23
- return true;
24
- },
25
- getCount(key) {
26
- const now = Date.now();
27
- const windowMs = config.windowSeconds * 1000;
28
- const record = records.get(key);
29
- if (!record || now - record.windowStart >= windowMs) {
30
- return 0;
31
- }
32
- return record.count;
33
- },
34
- getResetTime(key) {
35
- const record = records.get(key);
36
- if (!record) {
37
- return null;
38
- }
39
- const windowMs = config.windowSeconds * 1000;
40
- return new Date(record.windowStart + windowMs);
41
- },
42
- cleanup() {
43
- const now = Date.now();
44
- const windowMs = config.windowSeconds * 1000;
45
- for (const [key, record] of records.entries()) {
46
- if (now - record.windowStart >= windowMs) {
47
- records.delete(key);
48
- }
49
- }
50
- },
51
- getConfig() {
52
- return config;
53
- },
54
- };
55
- }
56
- export { createMemoryRateLimiter };