@wnodex/rate-limit 0.2.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.
- package/.turbo/turbo-build.log +7 -0
- package/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/configure.d.ts +4 -0
- package/dist/configure.d.ts.map +1 -0
- package/dist/configure.js +16 -0
- package/dist/defaults.d.ts +9 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +8 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/parse-options.d.ts +3 -0
- package/dist/parse-options.d.ts.map +1 -0
- package/dist/parse-options.js +8 -0
- package/dist/schema.d.ts +9 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +23 -0
- package/package.json +49 -0
- package/rolldown.config.ts +20 -0
- package/src/configure.ts +27 -0
- package/src/defaults.ts +9 -0
- package/src/index.ts +4 -0
- package/src/parse-options.ts +17 -0
- package/src/schema.ts +33 -0
- package/tsconfig.json +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Davide Di Criscito
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @wnodex/rate-limit
|
|
2
|
+
|
|
3
|
+
wnodex rate-limit middleware
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Table of Content
|
|
8
|
+
|
|
9
|
+
- [License](#license)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
This project is licensed under the MIT License.
|
|
16
|
+
|
|
17
|
+
**Copyright (c) 2026 Davide Di Criscito**
|
|
18
|
+
|
|
19
|
+
For the full details, see the [LICENSE](LICENSE) file.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../src/configure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,SAAS,CAAC;AAK3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEpD,eAAO,MAAM,kBAAkB,GAC7B,KAAK,WAAW,EAChB,QAAQ,qBAAqB,4BAiB9B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import rateLimit from 'express-rate-limit';
|
|
2
|
+
import { parseRateLimitOptions } from './parse-options.js';
|
|
3
|
+
export const configureRateLimit = (app, config) => {
|
|
4
|
+
const logger = app.get('logger') ?? console;
|
|
5
|
+
const options = parseRateLimitOptions(config);
|
|
6
|
+
if (typeof options === 'boolean') {
|
|
7
|
+
if (!options) {
|
|
8
|
+
logger.info('Rate-limit disabled');
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
logger.info('Rate-limit enabled');
|
|
12
|
+
return app.use(rateLimit());
|
|
13
|
+
}
|
|
14
|
+
logger.info('Rate-limit enabled');
|
|
15
|
+
return app.use(rateLimit({ ...options }));
|
|
16
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const DEFAULT_RATE_LIMIT_WINDOW_MS: number;
|
|
2
|
+
export declare const DEFAULT_RATE_LIMIT_MAX = 100;
|
|
3
|
+
export declare const DEFAULT_RATE_LIMIT_MESSAGE = "Too many requests, please try again later.";
|
|
4
|
+
export declare const DEFAULT_RATE_LIMIT_OPTIONS: {
|
|
5
|
+
windowMs: number;
|
|
6
|
+
max: number;
|
|
7
|
+
message: string;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=defaults.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,4BAA4B,QAAiB,CAAC;AAC3D,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAC1C,eAAO,MAAM,0BAA0B,+CACO,CAAC;AAC/C,eAAO,MAAM,0BAA0B;;;;CAItC,CAAC"}
|
package/dist/defaults.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const DEFAULT_RATE_LIMIT_WINDOW_MS = 15 * 60 * 1000;
|
|
2
|
+
export const DEFAULT_RATE_LIMIT_MAX = 100;
|
|
3
|
+
export const DEFAULT_RATE_LIMIT_MESSAGE = 'Too many requests, please try again later.';
|
|
4
|
+
export const DEFAULT_RATE_LIMIT_OPTIONS = {
|
|
5
|
+
windowMs: DEFAULT_RATE_LIMIT_WINDOW_MS,
|
|
6
|
+
max: DEFAULT_RATE_LIMIT_MAX,
|
|
7
|
+
message: DEFAULT_RATE_LIMIT_MESSAGE,
|
|
8
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-options.d.ts","sourceRoot":"","sources":["../src/parse-options.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAE5B,MAAM,aAAa,CAAC;AAErB,eAAO,MAAM,qBAAqB,GAChC,UAAU,qBAAqB,KAC9B,sBAQF,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RateLimitOptionsSchema, } from './schema.js';
|
|
2
|
+
export const parseRateLimitOptions = (options) => {
|
|
3
|
+
const result = RateLimitOptionsSchema.safeParse(options);
|
|
4
|
+
if (!result.success) {
|
|
5
|
+
throw new Error('Invalid rate-limit options');
|
|
6
|
+
}
|
|
7
|
+
return result.data;
|
|
8
|
+
};
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const RateLimitOptionsSchema: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
3
|
+
windowMs: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
4
|
+
max: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
5
|
+
message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
6
|
+
}, z.core.$strip>]>>>;
|
|
7
|
+
export type RateLimitOptionsInput = z.input<typeof RateLimitOptionsSchema>;
|
|
8
|
+
export type RateLimitOptionsOutput = z.output<typeof RateLimitOptionsSchema>;
|
|
9
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,eAAO,MAAM,sBAAsB;;;;qBAoBU,CAAC;AAE9C,MAAM,MAAM,qBAAqB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAC3E,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,sBAAsB,CAAC,CAAC"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { DEFAULT_RATE_LIMIT_MAX, DEFAULT_RATE_LIMIT_MESSAGE, DEFAULT_RATE_LIMIT_OPTIONS, DEFAULT_RATE_LIMIT_WINDOW_MS, } from './defaults.js';
|
|
3
|
+
export const RateLimitOptionsSchema = z
|
|
4
|
+
.union([
|
|
5
|
+
z.boolean(),
|
|
6
|
+
z.object({
|
|
7
|
+
windowMs: z
|
|
8
|
+
.number()
|
|
9
|
+
.int()
|
|
10
|
+
.positive()
|
|
11
|
+
.optional()
|
|
12
|
+
.default(DEFAULT_RATE_LIMIT_WINDOW_MS),
|
|
13
|
+
max: z
|
|
14
|
+
.number()
|
|
15
|
+
.int()
|
|
16
|
+
.positive()
|
|
17
|
+
.optional()
|
|
18
|
+
.default(DEFAULT_RATE_LIMIT_MAX),
|
|
19
|
+
message: z.string().optional().default(DEFAULT_RATE_LIMIT_MESSAGE),
|
|
20
|
+
}),
|
|
21
|
+
])
|
|
22
|
+
.optional()
|
|
23
|
+
.default({ ...DEFAULT_RATE_LIMIT_OPTIONS });
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wnodex/rate-limit",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "wnodex rate-limit middleware",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"wnodex"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/wnodex/wnodex#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/wnodex/wnodex/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/wnodex/wnodex.git",
|
|
16
|
+
"directory": "packages/rate-limit"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
"./package.json": "./package.json",
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"module": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"express": "^5.2.1",
|
|
33
|
+
"express-rate-limit": "^8.2.1",
|
|
34
|
+
"zod": "^4.3.6"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/express": "^5.0.6",
|
|
38
|
+
"@types/node": "^25.0.10",
|
|
39
|
+
"rolldown": "1.0.0-rc.1",
|
|
40
|
+
"typescript": "5.9.2",
|
|
41
|
+
"@wnodex/typescript-config": "0.2.1"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "rolldown -c && tsc"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineConfig } from 'rolldown';
|
|
2
|
+
|
|
3
|
+
export default defineConfig([
|
|
4
|
+
{
|
|
5
|
+
input: ['src/index.ts'],
|
|
6
|
+
output: {
|
|
7
|
+
format: 'esm',
|
|
8
|
+
dir: 'dist',
|
|
9
|
+
entryFileNames: '[name].js',
|
|
10
|
+
chunkFileNames: '[name]-[hash].js',
|
|
11
|
+
assetFileNames: '[name]-[hash][extname]',
|
|
12
|
+
},
|
|
13
|
+
platform: 'node',
|
|
14
|
+
external: (id) => {
|
|
15
|
+
if (id.startsWith('node:')) return true;
|
|
16
|
+
if (id.startsWith('.') || id.startsWith('/')) return false;
|
|
17
|
+
return true;
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
]);
|
package/src/configure.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Application } from 'express';
|
|
2
|
+
|
|
3
|
+
import rateLimit from 'express-rate-limit';
|
|
4
|
+
|
|
5
|
+
import { parseRateLimitOptions } from './parse-options.js';
|
|
6
|
+
import { RateLimitOptionsInput } from './schema.js';
|
|
7
|
+
|
|
8
|
+
export const configureRateLimit = (
|
|
9
|
+
app: Application,
|
|
10
|
+
config: RateLimitOptionsInput
|
|
11
|
+
) => {
|
|
12
|
+
const logger = app.get('logger') ?? console;
|
|
13
|
+
const options = parseRateLimitOptions(config);
|
|
14
|
+
|
|
15
|
+
if (typeof options === 'boolean') {
|
|
16
|
+
if (!options) {
|
|
17
|
+
logger.info('Rate-limit disabled');
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
logger.info('Rate-limit enabled');
|
|
22
|
+
return app.use(rateLimit());
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
logger.info('Rate-limit enabled');
|
|
26
|
+
return app.use(rateLimit({ ...options }));
|
|
27
|
+
};
|
package/src/defaults.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const DEFAULT_RATE_LIMIT_WINDOW_MS = 15 * 60 * 1000;
|
|
2
|
+
export const DEFAULT_RATE_LIMIT_MAX = 100;
|
|
3
|
+
export const DEFAULT_RATE_LIMIT_MESSAGE =
|
|
4
|
+
'Too many requests, please try again later.';
|
|
5
|
+
export const DEFAULT_RATE_LIMIT_OPTIONS = {
|
|
6
|
+
windowMs: DEFAULT_RATE_LIMIT_WINDOW_MS,
|
|
7
|
+
max: DEFAULT_RATE_LIMIT_MAX,
|
|
8
|
+
message: DEFAULT_RATE_LIMIT_MESSAGE,
|
|
9
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RateLimitOptionsInput,
|
|
3
|
+
type RateLimitOptionsOutput,
|
|
4
|
+
RateLimitOptionsSchema,
|
|
5
|
+
} from './schema.js';
|
|
6
|
+
|
|
7
|
+
export const parseRateLimitOptions = (
|
|
8
|
+
options?: RateLimitOptionsInput
|
|
9
|
+
): RateLimitOptionsOutput => {
|
|
10
|
+
const result = RateLimitOptionsSchema.safeParse(options);
|
|
11
|
+
|
|
12
|
+
if (!result.success) {
|
|
13
|
+
throw new Error('Invalid rate-limit options');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return result.data;
|
|
17
|
+
};
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_RATE_LIMIT_MAX,
|
|
5
|
+
DEFAULT_RATE_LIMIT_MESSAGE,
|
|
6
|
+
DEFAULT_RATE_LIMIT_OPTIONS,
|
|
7
|
+
DEFAULT_RATE_LIMIT_WINDOW_MS,
|
|
8
|
+
} from './defaults.js';
|
|
9
|
+
|
|
10
|
+
export const RateLimitOptionsSchema = z
|
|
11
|
+
.union([
|
|
12
|
+
z.boolean(),
|
|
13
|
+
z.object({
|
|
14
|
+
windowMs: z
|
|
15
|
+
.number()
|
|
16
|
+
.int()
|
|
17
|
+
.positive()
|
|
18
|
+
.optional()
|
|
19
|
+
.default(DEFAULT_RATE_LIMIT_WINDOW_MS),
|
|
20
|
+
max: z
|
|
21
|
+
.number()
|
|
22
|
+
.int()
|
|
23
|
+
.positive()
|
|
24
|
+
.optional()
|
|
25
|
+
.default(DEFAULT_RATE_LIMIT_MAX),
|
|
26
|
+
message: z.string().optional().default(DEFAULT_RATE_LIMIT_MESSAGE),
|
|
27
|
+
}),
|
|
28
|
+
])
|
|
29
|
+
.optional()
|
|
30
|
+
.default({ ...DEFAULT_RATE_LIMIT_OPTIONS });
|
|
31
|
+
|
|
32
|
+
export type RateLimitOptionsInput = z.input<typeof RateLimitOptionsSchema>;
|
|
33
|
+
export type RateLimitOptionsOutput = z.output<typeof RateLimitOptionsSchema>;
|
package/tsconfig.json
ADDED