@lunora/ratelimit 1.0.0-alpha.3 → 1.0.0-alpha.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/LICENSE.md +6 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +4 -4
- package/dist/packem_shared/{RateLimitError-YOUfRzXc.mjs → RateLimitError-53bWrDuJ.mjs} +4 -3
- package/dist/packem_shared/{RateLimiter-CeElVjB0.mjs → RateLimiter-DQ9Peq-G.mjs} +9 -8
- package/dist/packem_shared/{availableAt-D5GyJtxT.mjs → availableAt-DN3Z6h_K.mjs} +4 -2
- package/dist/packem_shared/{dbRateLimit-C4nMF5cN.mjs → dbRateLimit-CXO7O3KO.mjs} +1 -1
- package/package.json +4 -1
package/LICENSE.md
CHANGED
|
@@ -103,3 +103,9 @@ Unless required by applicable law or agreed to in writing, software distributed
|
|
|
103
103
|
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
104
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
105
|
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
<!-- /DEPENDENCIES -->
|
|
109
|
+
|
|
110
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
111
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Middleware, Plugin } from '@lunora/server';
|
|
2
2
|
import { Id } from '@lunora/values';
|
|
3
|
+
import { LunoraError } from '@lunora/errors';
|
|
3
4
|
type RateLimitKind = "fixed window" | "sliding window" | "token bucket";
|
|
4
5
|
type RateLimitReason = "deny" | "rate";
|
|
5
6
|
interface RateLimitConfig {
|
|
@@ -125,8 +126,7 @@ declare const databaseRateLimit: <Context extends {
|
|
|
125
126
|
}, Names extends string = string>(config: RateLimitConfigMap<Names>, name: Names, options?: RateLimitMiddlewareOptions<Context> & {
|
|
126
127
|
store?: Omit<DatabaseStoreOptions, "db">;
|
|
127
128
|
}) => Middleware<Context, Context>;
|
|
128
|
-
declare class RateLimitError extends
|
|
129
|
-
readonly name = "RateLimitError";
|
|
129
|
+
declare class RateLimitError extends LunoraError {
|
|
130
130
|
readonly reason: RateLimitReason | undefined;
|
|
131
131
|
readonly retryAfter: number;
|
|
132
132
|
constructor(status: RateLimitStatus, message?: string);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Middleware, Plugin } from '@lunora/server';
|
|
2
2
|
import { Id } from '@lunora/values';
|
|
3
|
+
import { LunoraError } from '@lunora/errors';
|
|
3
4
|
type RateLimitKind = "fixed window" | "sliding window" | "token bucket";
|
|
4
5
|
type RateLimitReason = "deny" | "rate";
|
|
5
6
|
interface RateLimitConfig {
|
|
@@ -125,8 +126,7 @@ declare const databaseRateLimit: <Context extends {
|
|
|
125
126
|
}, Names extends string = string>(config: RateLimitConfigMap<Names>, name: Names, options?: RateLimitMiddlewareOptions<Context> & {
|
|
126
127
|
store?: Omit<DatabaseStoreOptions, "db">;
|
|
127
128
|
}) => Middleware<Context, Context>;
|
|
128
|
-
declare class RateLimitError extends
|
|
129
|
-
readonly name = "RateLimitError";
|
|
129
|
+
declare class RateLimitError extends LunoraError {
|
|
130
130
|
readonly reason: RateLimitReason | undefined;
|
|
131
131
|
readonly retryAfter: number;
|
|
132
132
|
constructor(status: RateLimitStatus, message?: string);
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { availableAt, evaluate } from './packem_shared/availableAt-
|
|
2
|
-
export { default as dbRateLimit } from './packem_shared/dbRateLimit-
|
|
3
|
-
export { default as RateLimitError } from './packem_shared/RateLimitError-
|
|
1
|
+
export { availableAt, evaluate } from './packem_shared/availableAt-DN3Z6h_K.mjs';
|
|
2
|
+
export { default as dbRateLimit } from './packem_shared/dbRateLimit-CXO7O3KO.mjs';
|
|
3
|
+
export { default as RateLimitError } from './packem_shared/RateLimitError-53bWrDuJ.mjs';
|
|
4
4
|
export { rateLimit } from './packem_shared/rateLimit-uQxVMZfh.mjs';
|
|
5
5
|
export { ratelimitPlugin } from './packem_shared/ratelimitPlugin-D5_-KV1T.mjs';
|
|
6
|
-
export { RateLimiter } from './packem_shared/RateLimiter-
|
|
6
|
+
export { RateLimiter } from './packem_shared/RateLimiter-DQ9Peq-G.mjs';
|
|
7
7
|
export { createDbStore, createMemoryStore, createSqlStore } from './packem_shared/createDbStore-L1kD1g1n.mjs';
|
|
8
8
|
|
|
9
9
|
const VERSION = "0.0.0";
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
import { LunoraError } from '@lunora/errors';
|
|
2
|
+
|
|
1
3
|
const describe = (status) => {
|
|
2
4
|
if (status.reason === "deny") {
|
|
3
5
|
return "request denied (deny list)";
|
|
4
6
|
}
|
|
5
7
|
return Number.isFinite(status.retryAfter) ? `rate limit exceeded; retry after ${String(Math.ceil(status.retryAfter))}ms` : "rate limit exceeded";
|
|
6
8
|
};
|
|
7
|
-
class RateLimitError extends
|
|
8
|
-
name = "RateLimitError";
|
|
9
|
+
class RateLimitError extends LunoraError {
|
|
9
10
|
reason;
|
|
10
11
|
retryAfter;
|
|
11
12
|
constructor(status, message) {
|
|
12
|
-
super(message ?? describe(status));
|
|
13
|
+
super("TOO_MANY_REQUESTS", message ?? describe(status), { name: "RateLimitError" });
|
|
13
14
|
this.reason = status.reason;
|
|
14
15
|
this.retryAfter = status.retryAfter;
|
|
15
16
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { LunoraError } from '@lunora/errors';
|
|
2
|
+
import { availableAt, evaluate } from './availableAt-DN3Z6h_K.mjs';
|
|
3
|
+
import RateLimitError from './RateLimitError-53bWrDuJ.mjs';
|
|
3
4
|
import { createMemoryStore } from './createDbStore-L1kD1g1n.mjs';
|
|
4
5
|
|
|
5
6
|
const storageKeyFor = (name, key) => key === void 0 ? encodeURIComponent(name) : `${encodeURIComponent(name)}:${encodeURIComponent(key)}`;
|
|
@@ -29,16 +30,16 @@ class RateLimiter {
|
|
|
29
30
|
this.store = options.store ?? createMemoryStore();
|
|
30
31
|
for (const [name, config] of Object.entries(this.config)) {
|
|
31
32
|
if (config.shards !== void 0 && (!Number.isInteger(config.shards) || config.shards < 1)) {
|
|
32
|
-
throw new
|
|
33
|
+
throw new LunoraError("INTERNAL", `rate limit "${name}": shards must be a positive integer`);
|
|
33
34
|
}
|
|
34
35
|
if (!Number.isFinite(config.period) || config.period <= 0) {
|
|
35
|
-
throw new
|
|
36
|
+
throw new LunoraError("INTERNAL", `rate limit "${name}": period must be a positive number`);
|
|
36
37
|
}
|
|
37
38
|
if (!Number.isFinite(config.rate) || config.rate <= 0) {
|
|
38
|
-
throw new
|
|
39
|
+
throw new LunoraError("INTERNAL", `rate limit "${name}": rate must be a positive number`);
|
|
39
40
|
}
|
|
40
41
|
if (config.capacity !== void 0 && (!Number.isFinite(config.capacity) || config.capacity < 0)) {
|
|
41
|
-
throw new
|
|
42
|
+
throw new LunoraError("INTERNAL", `rate limit "${name}": capacity must be a non-negative number`);
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
}
|
|
@@ -81,7 +82,7 @@ class RateLimiter {
|
|
|
81
82
|
resolve(name) {
|
|
82
83
|
const config = this.config[name];
|
|
83
84
|
if (!config) {
|
|
84
|
-
throw new
|
|
85
|
+
throw new LunoraError("INTERNAL", `rate limit "${name}" is not configured`);
|
|
85
86
|
}
|
|
86
87
|
return config;
|
|
87
88
|
}
|
|
@@ -97,7 +98,7 @@ class RateLimiter {
|
|
|
97
98
|
}
|
|
98
99
|
const count = args.count ?? 1;
|
|
99
100
|
if (!Number.isInteger(count) || count <= 0) {
|
|
100
|
-
throw new
|
|
101
|
+
throw new LunoraError("INTERNAL", `rate limit "${name}": count must be a positive integer`);
|
|
101
102
|
}
|
|
102
103
|
const shards = config.shards ?? 1;
|
|
103
104
|
const base = storageKeyFor(name, normalizedKey);
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { LunoraError } from '@lunora/errors';
|
|
2
|
+
|
|
1
3
|
const capacityOf = (config) => config.capacity ?? config.rate;
|
|
2
4
|
const tokenBucket = (config, prior, options) => {
|
|
3
5
|
const capacity = capacityOf(config);
|
|
@@ -36,7 +38,7 @@ const fixedWindow = (config, prior, options) => {
|
|
|
36
38
|
return { status: { ok: true, retryAfter }, value: { ts: base.ts, value: base.value - options.count } };
|
|
37
39
|
}
|
|
38
40
|
if (options.count > capacity) {
|
|
39
|
-
throw new
|
|
41
|
+
throw new LunoraError("INTERNAL", `@lunora/ratelimit: requested count ${String(options.count)} exceeds the limiter capacity ${String(capacity)}`);
|
|
40
42
|
}
|
|
41
43
|
return { status: { ok: false, reason: "rate", retryAfter }, value: void 0 };
|
|
42
44
|
};
|
|
@@ -71,7 +73,7 @@ const slidingWindow = (config, prior, options) => {
|
|
|
71
73
|
return { status: { ok: true, retryAfter: admit ? 0 : retryAfter() }, value: options.consume ? value : void 0 };
|
|
72
74
|
}
|
|
73
75
|
if (options.count > limit) {
|
|
74
|
-
throw new
|
|
76
|
+
throw new LunoraError("INTERNAL", `@lunora/ratelimit: requested count ${String(options.count)} exceeds the limiter capacity ${String(limit)}`);
|
|
75
77
|
}
|
|
76
78
|
return { status: { ok: false, reason: "rate", retryAfter: retryAfter() }, value: void 0 };
|
|
77
79
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { rateLimit } from './rateLimit-uQxVMZfh.mjs';
|
|
2
|
-
import { RateLimiter } from './RateLimiter-
|
|
2
|
+
import { RateLimiter } from './RateLimiter-DQ9Peq-G.mjs';
|
|
3
3
|
import { createDbStore as createDatabaseStore } from './createDbStore-L1kD1g1n.mjs';
|
|
4
4
|
|
|
5
5
|
const databaseRateLimit = (config, name, options = {}) => rateLimit((context) => new RateLimiter({ config, store: createDatabaseStore({ db: context.db, ...options.store }) }), name, options);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/ratelimit",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.5",
|
|
4
4
|
"description": "Rate limiting: token-bucket / fixed-window / sliding-window algorithms, deny list, sharding, pluggable stores, and procedure middleware",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -45,6 +45,9 @@
|
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@lunora/errors": "1.0.0-alpha.2"
|
|
50
|
+
},
|
|
48
51
|
"engines": {
|
|
49
52
|
"node": "^22.15.0 || >=24.11.0"
|
|
50
53
|
}
|