@vite-hub/rate-limit 0.0.4 → 0.3.0

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/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "@vite-hub/rate-limit",
3
- "version": "0.0.4",
3
+ "version": "0.3.0",
4
4
  "description": "Provider-neutral Rate Limit primitives and Vite deployment integration for ViteHub.",
5
5
  "keywords": [
6
6
  "cloudflare",
7
7
  "h3",
8
8
  "rate-limit",
9
+ "rate-limiting",
10
+ "typescript",
9
11
  "vite",
10
12
  "vitehub"
11
13
  ],
12
- "homepage": "https://github.com/vite-hub/vitehub#readme",
14
+ "homepage": "https://vitehub.dev",
13
15
  "bugs": "https://github.com/vite-hub/vitehub/issues",
14
16
  "license": "Apache-2.0",
15
17
  "author": "onmax <maximogarciamtnez@gmail.com>",
@@ -26,26 +28,63 @@
26
28
  "sideEffects": false,
27
29
  "types": "./dist/index.d.ts",
28
30
  "exports": {
29
- ".": "./dist/index.js",
30
- "./drivers/cloudflare": "./dist/drivers/cloudflare.js",
31
- "./drivers/memory": "./dist/drivers/memory.js",
32
- "./runtime": "./dist/runtime.js",
33
- "./vite": "./dist/vite.js",
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js",
34
+ "default": "./dist/index.js"
35
+ },
36
+ "./drivers/cloudflare": {
37
+ "types": "./dist/drivers/cloudflare.d.ts",
38
+ "import": "./dist/drivers/cloudflare.js",
39
+ "default": "./dist/drivers/cloudflare.js"
40
+ },
41
+ "./drivers/kv": {
42
+ "types": "./dist/drivers/kv.d.ts",
43
+ "import": "./dist/drivers/kv.js",
44
+ "default": "./dist/drivers/kv.js"
45
+ },
46
+ "./drivers/memory": {
47
+ "types": "./dist/drivers/memory.d.ts",
48
+ "import": "./dist/drivers/memory.js",
49
+ "default": "./dist/drivers/memory.js"
50
+ },
51
+ "./durability": {
52
+ "types": "./dist/durability.d.ts",
53
+ "import": "./dist/durability.js",
54
+ "default": "./dist/durability.js"
55
+ },
56
+ "./runtime": {
57
+ "types": "./dist/runtime.d.ts",
58
+ "import": "./dist/runtime.js",
59
+ "default": "./dist/runtime.js"
60
+ },
61
+ "./vite": {
62
+ "types": "./dist/vite.d.ts",
63
+ "import": "./dist/vite.js",
64
+ "default": "./dist/vite.js"
65
+ },
34
66
  "./package.json": "./package.json"
35
67
  },
36
68
  "dependencies": {
37
- "h3": "2.0.1-rc.25"
69
+ "@valibot/to-json-schema": "^1.8.0",
70
+ "@vite-hub/runtime": "0.3.0",
71
+ "birpc": "^4.2.0",
72
+ "h3": "^2.0.8",
73
+ "nostics": "1.4.0",
74
+ "valibot": "^1.5.0"
38
75
  },
39
76
  "devDependencies": {
40
- "@types/node": "^24.13.3",
41
- "publint": "^0.3.21",
77
+ "@types/node": "^24.19.1",
78
+ "@vite-hub/internal": "0.0.1",
79
+ "nitro": "^3.0.261103-rc",
80
+ "publint": "^0.3.25",
42
81
  "typescript": "^6.0.3",
43
- "vite": "^8.0.8",
44
- "vite-plus": "^0.1.24",
45
- "vitest": "^4.1.4",
46
- "@vite-hub/internal": "0.0.1"
82
+ "vite": "^8.2.2",
83
+ "vite-plus": "^0.3.3",
84
+ "vitest": "^4.1.11"
47
85
  },
48
86
  "peerDependencies": {
87
+ "nitro": "3.0.261102-rc || ^3.0.261103-rc",
49
88
  "vite": ">=8.0.0"
50
89
  },
51
90
  "peerDependenciesMeta": {
@@ -54,12 +93,12 @@
54
93
  }
55
94
  },
56
95
  "engines": {
57
- "node": ">=24.0.0"
96
+ "node": ">=22.18.0"
58
97
  },
59
98
  "scripts": {
60
99
  "build": "vp pack",
61
100
  "dev": "vp pack --watch",
62
- "typecheck": "tsc --noEmit -p ./tsconfig.json",
63
- "test": "vp run -t @vite-hub/rate-limit#build && vp test"
101
+ "typecheck": "vp run --cache -t @vite-hub/rate-limit#build && node ../../test/package-typecheck.mjs --noEmit -p ./tsconfig.json",
102
+ "test": "vp run --cache -t @vite-hub/rate-limit#build && vp test"
64
103
  }
65
104
  }
@@ -1,118 +0,0 @@
1
- //#region src/policy.ts
2
- const rateLimitPolicyKeys = new Set([
3
- "enforcement",
4
- "failure",
5
- "limit",
6
- "window"
7
- ]);
8
- const unitMilliseconds = {
9
- d: 864e5,
10
- h: 36e5,
11
- m: 6e4,
12
- ms: 1,
13
- s: 1e3
14
- };
15
- function parseRateLimitWindow(value) {
16
- const match = /^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/.exec(value);
17
- if (!match) throw new TypeError("[vitehub] Rate Limit window must use a duration such as \"10s\", \"1m\", \"1h\", or \"1d\".");
18
- const amount = Number(match[1]);
19
- if (!Number.isFinite(amount) || amount <= 0) throw new TypeError("[vitehub] Rate Limit window must be greater than zero.");
20
- return Math.ceil(amount * unitMilliseconds[match[2]]);
21
- }
22
- function normalizeRateLimitPolicy(policy) {
23
- if (!policy || typeof policy !== "object" || Array.isArray(policy)) throw new TypeError("[vitehub] Rate Limit policy must be an object.");
24
- if (!Number.isInteger(policy.limit) || policy.limit <= 0) throw new TypeError("[vitehub] Rate Limit limit must be a positive integer.");
25
- if (policy.enforcement !== void 0 && policy.enforcement !== "best-effort" && policy.enforcement !== "strict") throw new TypeError("[vitehub] Rate Limit enforcement must be \"best-effort\" or \"strict\".");
26
- if (policy.failure !== void 0 && policy.failure !== "allow" && policy.failure !== "deny") throw new TypeError("[vitehub] Rate Limit failure must be \"allow\" or \"deny\".");
27
- return {
28
- enforcement: policy.enforcement ?? "best-effort",
29
- failure: policy.failure ?? "deny",
30
- limit: policy.limit,
31
- window: policy.window,
32
- windowMs: parseRateLimitWindow(policy.window)
33
- };
34
- }
35
- function declaredRateLimitPolicy(policy) {
36
- return {
37
- enforcement: policy.enforcement,
38
- failure: policy.failure,
39
- limit: policy.limit,
40
- window: policy.window
41
- };
42
- }
43
- //#endregion
44
- //#region src/limiter.ts
45
- function resolveDriverCapabilities(options) {
46
- const capabilities = options.driver.capabilities;
47
- if (!capabilities || typeof capabilities !== "object") throw new TypeError(`[vitehub] Rate Limit driver "${options.driver.name}" must declare capabilities.`);
48
- if (capabilities.enforcement !== "best-effort" && capabilities.enforcement !== "strict") throw new TypeError(`[vitehub] Rate Limit driver "${options.driver.name}" must declare valid enforcement.`);
49
- if (capabilities.scope !== "process" && capabilities.scope !== "location" && capabilities.scope !== "global") throw new TypeError(`[vitehub] Rate Limit driver "${options.driver.name}" must declare counter scope.`);
50
- if (capabilities.rejectedAttempts !== "counted" && capabilities.rejectedAttempts !== "not-counted" && capabilities.rejectedAttempts !== "unknown") throw new TypeError(`[vitehub] Rate Limit driver "${options.driver.name}" must declare rejected-attempt behavior.`);
51
- if (capabilities.windows?.some((window) => !Number.isInteger(window) || window <= 0)) throw new TypeError(`[vitehub] Rate Limit driver "${options.driver.name}" windows must contain positive integer milliseconds.`);
52
- return {
53
- enforcement: capabilities.enforcement,
54
- rejectedAttempts: capabilities.rejectedAttempts,
55
- scope: capabilities.scope,
56
- ...capabilities.windows ? { windows: [...capabilities.windows] } : {}
57
- };
58
- }
59
- function normalizeOptionalInteger(value, label) {
60
- if (value === void 0) return;
61
- if (!Number.isInteger(value) || value < 0) throw new TypeError(`[vitehub] Rate Limit driver result ${label} must be a non-negative integer.`);
62
- return value;
63
- }
64
- function normalizeDriverResult(result, limit, windowMs) {
65
- if (!result || typeof result !== "object" || typeof result.allowed !== "boolean") throw new TypeError("[vitehub] Rate Limit driver consume() must return an object with an allowed boolean.");
66
- const resetAt = result.resetAt;
67
- if (resetAt !== void 0 && (!Number.isFinite(resetAt) || resetAt <= 0)) throw new TypeError("[vitehub] Rate Limit driver result resetAt must be a positive timestamp.");
68
- const decision = {
69
- limit,
70
- remaining: normalizeOptionalInteger(result.remaining, "remaining"),
71
- resetAt,
72
- retryAfter: normalizeOptionalInteger(result.retryAfter, "retryAfter"),
73
- used: normalizeOptionalInteger(result.used, "used"),
74
- windowMs
75
- };
76
- return result.allowed ? {
77
- ...decision,
78
- allowed: true
79
- } : {
80
- ...decision,
81
- allowed: false,
82
- reason: "limited"
83
- };
84
- }
85
- function assertDriverSupportsPolicy(options, capabilities, windowMs) {
86
- if ((options.enforcement ?? "best-effort") === "strict" && capabilities.enforcement !== "strict") throw new Error(`[vitehub] Rate Limit driver "${options.driver.name}" provides best-effort enforcement, but this policy requires strict enforcement.`);
87
- const windows = capabilities.windows;
88
- if (windows?.length && !windows.includes(windowMs)) throw new Error(`[vitehub] Rate Limit driver "${options.driver.name}" does not support a ${windowMs}ms window. Supported windows: ${windows.join(", ")}ms.`);
89
- }
90
- function createRateLimiter(options) {
91
- if (!options.driver || typeof options.driver.consume !== "function") throw new TypeError("[vitehub] createRateLimiter() requires a Rate Limit driver.");
92
- const policy = normalizeRateLimitPolicy(options);
93
- const capabilities = resolveDriverCapabilities(options);
94
- assertDriverSupportsPolicy(options, capabilities, policy.windowMs);
95
- return {
96
- capabilities,
97
- async consume(input) {
98
- if (!input || typeof input.key !== "string" || input.key.length === 0) throw new TypeError("[vitehub] Rate Limiter consume() requires a non-empty key.");
99
- const [error, result] = await options.driver.consume({
100
- key: input.key,
101
- limit: policy.limit,
102
- name: options.name,
103
- windowMs: policy.windowMs
104
- });
105
- if (error) return {
106
- allowed: policy.failure === "allow",
107
- cause: error.cause ?? error,
108
- limit: policy.limit,
109
- reason: "unavailable",
110
- windowMs: policy.windowMs
111
- };
112
- return normalizeDriverResult(result, policy.limit, policy.windowMs);
113
- },
114
- policy
115
- };
116
- }
117
- //#endregion
118
- export { rateLimitPolicyKeys as i, declaredRateLimitPolicy as n, normalizeRateLimitPolicy as r, createRateLimiter as t };
@@ -1,15 +0,0 @@
1
- //#region src/runtime/state.ts
2
- let runtimeConfig = { provider: "memory" };
3
- const limiterCache = /* @__PURE__ */ new Map();
4
- function setRateLimitRuntimeConfig(config) {
5
- runtimeConfig = config;
6
- limiterCache.clear();
7
- }
8
- function getRateLimitRuntimeConfig() {
9
- return runtimeConfig;
10
- }
11
- function getRateLimitLimiterCache() {
12
- return limiterCache;
13
- }
14
- //#endregion
15
- export { getRateLimitRuntimeConfig as n, setRateLimitRuntimeConfig as r, getRateLimitLimiterCache as t };
@@ -1,106 +0,0 @@
1
- //#region src/types.d.ts
2
- type MaybePromise<T> = Promise<T> | T;
3
- type RateLimitWindow = `${number}ms` | `${number}s` | `${number}m` | `${number}h` | `${number}d`;
4
- type RateLimitEnforcement = "best-effort" | "strict";
5
- type RateLimitFailurePolicy = "allow" | "deny";
6
- type RateLimitCounterScope = "global" | "location" | "process";
7
- type RateLimitRejectedAttemptBehavior = "counted" | "not-counted" | "unknown";
8
- interface RateLimitPolicy {
9
- enforcement?: RateLimitEnforcement;
10
- failure?: RateLimitFailurePolicy;
11
- limit: number;
12
- window: RateLimitWindow;
13
- }
14
- interface RequireRateLimitOptions extends RateLimitPolicy {
15
- key?: string;
16
- }
17
- interface RateLimitRequestEvent {
18
- readonly req: {
19
- readonly headers: {
20
- get: (name: string) => string | null;
21
- };
22
- };
23
- }
24
- interface ResolvedRateLimitPolicy {
25
- readonly enforcement: RateLimitEnforcement;
26
- readonly failure: RateLimitFailurePolicy;
27
- readonly limit: number;
28
- readonly window: RateLimitWindow;
29
- readonly windowMs: number;
30
- }
31
- interface RateLimitConsumeInput {
32
- key: string;
33
- }
34
- interface RateLimitDriverInput extends RateLimitConsumeInput {
35
- limit: number;
36
- name?: string;
37
- windowMs: number;
38
- }
39
- interface RateLimitDriverResult {
40
- allowed: boolean;
41
- remaining?: number;
42
- resetAt?: number;
43
- retryAfter?: number;
44
- used?: number;
45
- }
46
- type RateLimitDriverOutcome = [error: null, value: RateLimitDriverResult] | [error: Error, value: undefined];
47
- interface RateLimitDecisionBase extends RateLimitDriverResult {
48
- limit: number;
49
- windowMs: number;
50
- }
51
- interface RateLimitAllowedDecision extends RateLimitDecisionBase {
52
- allowed: true;
53
- cause?: never;
54
- reason?: never;
55
- }
56
- interface RateLimitLimitedDecision extends RateLimitDecisionBase {
57
- allowed: false;
58
- cause?: never;
59
- reason: "limited";
60
- }
61
- interface RateLimitUnavailableDecision extends RateLimitDecisionBase {
62
- cause: unknown;
63
- reason: "unavailable";
64
- }
65
- type RateLimitDecision = RateLimitAllowedDecision | RateLimitLimitedDecision | RateLimitUnavailableDecision;
66
- interface RateLimitDriverCapabilities {
67
- readonly enforcement: RateLimitEnforcement;
68
- readonly rejectedAttempts: RateLimitRejectedAttemptBehavior;
69
- readonly scope: RateLimitCounterScope;
70
- readonly windows?: readonly number[];
71
- }
72
- interface RateLimitDriver {
73
- capabilities: RateLimitDriverCapabilities;
74
- consume: (input: RateLimitDriverInput) => MaybePromise<RateLimitDriverOutcome>;
75
- name: string;
76
- }
77
- interface RateLimiter {
78
- readonly capabilities: RateLimitDriverCapabilities;
79
- consume: (input: RateLimitConsumeInput) => Promise<RateLimitDecision>;
80
- policy: ResolvedRateLimitPolicy;
81
- }
82
- interface RateLimitDeclaration {
83
- name: string;
84
- policy: RateLimitPolicy;
85
- source: {
86
- column: number;
87
- file: string;
88
- line: number;
89
- };
90
- }
91
- interface CreateRateLimiterOptions extends RateLimitPolicy {
92
- driver: RateLimitDriver;
93
- name?: string;
94
- }
95
- type RateLimitProvider = "auto" | "cloudflare" | "memory";
96
- interface RateLimitModuleOptions {
97
- namespace?: string;
98
- provider?: RateLimitProvider;
99
- projectRoot?: string;
100
- scanDirs?: string[];
101
- }
102
- interface RateLimitRuntimeConfig {
103
- provider: Exclude<RateLimitProvider, "auto">;
104
- }
105
- //#endregion
106
- export { ResolvedRateLimitPolicy as S, RateLimitRequestEvent as _, RateLimitDeclaration as a, RateLimiter as b, RateLimitDriverInput as c, RateLimitEnforcement as d, RateLimitFailurePolicy as f, RateLimitRejectedAttemptBehavior as g, RateLimitProvider as h, RateLimitDecision as i, RateLimitDriverOutcome as l, RateLimitPolicy as m, RateLimitConsumeInput as n, RateLimitDriver as o, RateLimitModuleOptions as p, RateLimitCounterScope as r, RateLimitDriverCapabilities as s, CreateRateLimiterOptions as t, RateLimitDriverResult as u, RateLimitRuntimeConfig as v, RequireRateLimitOptions as x, RateLimitWindow as y };