@longzai-intelligence-auth/rate-limit 0.0.1

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,15 @@
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 ADDED
@@ -0,0 +1,56 @@
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 };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@longzai-intelligence-auth/rate-limit",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
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
+ }
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/longzai/longzai-intelligence-auth",
31
+ "directory": "packages/rate-limit"
32
+ },
33
+ "dependencies": {
34
+ "@longzai-intelligence-auth/core": "0.0.1"
35
+ },
36
+ "scripts": {
37
+ "build": "tsgo --build",
38
+ "typecheck": "tsgo --noEmit",
39
+ "lint": "oxlint && oxfmt --check",
40
+ "lint:fix": "oxlint --fix && oxfmt",
41
+ "test": "bun test",
42
+ "clean": "rm -rf dist out .cache"
43
+ }
44
+ }