@aomex/rate-limit 0.0.3 → 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/CHANGELOG.md +5 -23
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/package.json +7 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,29 +1,11 @@
|
|
|
1
1
|
# @aomex/rate-limit
|
|
2
2
|
|
|
3
|
-
## 0.0.
|
|
3
|
+
## 0.0.5
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- [`
|
|
7
|
+
- [`bbc8dda`](https://github.com/aomex/aomex/commit/bbc8dda9f0876c3402608133085350cc693def6f) Thanks [@geekact](https://github.com/geekact)! - refactor: rename package helper to utility
|
|
8
8
|
|
|
9
|
-
- Updated dependencies [[`
|
|
10
|
-
- @aomex/
|
|
11
|
-
- @aomex/
|
|
12
|
-
|
|
13
|
-
## 0.0.2
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- Updated dependencies [[`121253a`](https://github.com/aomex/aomex/commit/121253a6fc203e427bfd25e82d713bec6ac13b38), [`59dc183`](https://github.com/aomex/aomex/commit/59dc183bcec9b7221c18865563b38cba5d82869b), [`8cb74fd`](https://github.com/aomex/aomex/commit/8cb74fdd27626037aa9bc80cb2930e4f0d67f8e6), [`d834392`](https://github.com/aomex/aomex/commit/d834392b1e0f6333d66d11378d9f4ceb813a58a9), [`91feda4`](https://github.com/aomex/aomex/commit/91feda457dfa8bd8dd2c8f38bfaaf4f7f2f7702f)]:
|
|
18
|
-
- @aomex/web@0.0.4
|
|
19
|
-
- @aomex/core@0.0.3
|
|
20
|
-
|
|
21
|
-
## 0.0.1
|
|
22
|
-
|
|
23
|
-
### Patch Changes
|
|
24
|
-
|
|
25
|
-
- [`7bcb2fc`](https://github.com/aomex/aomex/commit/7bcb2fccbe99675b0edca32ff3af9599113e754b) Thanks [@geekact](https://github.com/geekact)! - 增加 web 插件 rate-limit
|
|
26
|
-
|
|
27
|
-
- Updated dependencies [[`4735157`](https://github.com/aomex/aomex/commit/4735157c52d0abecc41d6c7f416280ebfd6ee696), [`d6ac7b5`](https://github.com/aomex/aomex/commit/d6ac7b5403334b51fa15dbe82dd6b5b6d048f661), [`9254754`](https://github.com/aomex/aomex/commit/9254754d234fc0062eb5d600284ee8453825306c), [`d1ce4e2`](https://github.com/aomex/aomex/commit/d1ce4e2276f93c44e67ea06666d048917fa94e53), [`6056f55`](https://github.com/aomex/aomex/commit/6056f556577865220d6b5be2a4eafad9e4dbf8d4), [`1c67399`](https://github.com/aomex/aomex/commit/1c673992c4b313c2accb4cbc1fc66537a9886c29), [`352642a`](https://github.com/aomex/aomex/commit/352642abf2ccb3fe97e2d83a691d8428ccfc9758)]:
|
|
28
|
-
- @aomex/web@0.0.3
|
|
29
|
-
- @aomex/core@0.0.2
|
|
9
|
+
- Updated dependencies [[`bbc8dda`](https://github.com/aomex/aomex/commit/bbc8dda9f0876c3402608133085350cc693def6f), [`ceb2d82`](https://github.com/aomex/aomex/commit/ceb2d8269755dca3f79b755224259b8a99bc65d0)]:
|
|
10
|
+
- @aomex/core@0.0.6
|
|
11
|
+
- @aomex/web@0.0.7
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Cache } from '@aomex/core';
|
|
2
2
|
import { WebContext, WebMiddleware } from '@aomex/web';
|
|
3
3
|
|
|
4
4
|
interface RateLimitOptions {
|
|
5
5
|
/**
|
|
6
6
|
* The database powering the backing rate-limiter package.
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
cache?: Cache;
|
|
9
9
|
/**
|
|
10
10
|
* The length of a single limiting period. This value is expressed
|
|
11
11
|
* in milliseconds, defaulting to one hour.
|
|
@@ -33,7 +33,7 @@ interface RateLimitOptions {
|
|
|
33
33
|
errorMessage?: string;
|
|
34
34
|
/**
|
|
35
35
|
* Whether or not to throw an error upon being rate-limited. This uses
|
|
36
|
-
* the
|
|
36
|
+
* the `ctx.throw()`.
|
|
37
37
|
*/
|
|
38
38
|
throw?: boolean;
|
|
39
39
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { middleware,
|
|
2
|
+
import { middleware, MemoryCache } from "@aomex/core";
|
|
3
3
|
import microtime from "microtime";
|
|
4
4
|
import ms from "ms";
|
|
5
|
-
var getState = async (
|
|
5
|
+
var getState = async (cache, id, max, duration) => {
|
|
6
6
|
const key = `ratelimit:${id}`;
|
|
7
|
-
let state = await
|
|
7
|
+
let state = await cache.get(key);
|
|
8
8
|
const now = microtime.now();
|
|
9
9
|
const expired = state && state.reset * 1e6 < now;
|
|
10
10
|
if (!state || expired) {
|
|
@@ -17,12 +17,12 @@ var getState = async (storage, id, max, duration) => {
|
|
|
17
17
|
} else {
|
|
18
18
|
state.current = state.current > 0 ? state.current - 1 : 0;
|
|
19
19
|
}
|
|
20
|
-
await
|
|
20
|
+
await cache.set(key, state);
|
|
21
21
|
return state;
|
|
22
22
|
};
|
|
23
23
|
var rateLimit = (options = {}) => {
|
|
24
24
|
const {
|
|
25
|
-
|
|
25
|
+
cache = new MemoryCache(),
|
|
26
26
|
duration = 3600 * 1e3,
|
|
27
27
|
// 1 hour
|
|
28
28
|
max = 2500,
|
|
@@ -46,7 +46,7 @@ var rateLimit = (options = {}) => {
|
|
|
46
46
|
return next();
|
|
47
47
|
}
|
|
48
48
|
const { response } = ctx;
|
|
49
|
-
const state = await getState(
|
|
49
|
+
const state = await getState(cache, id, max, duration);
|
|
50
50
|
let headers2 = {};
|
|
51
51
|
if (!disableHeader) {
|
|
52
52
|
headers2 = {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { middleware,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { middleware, Cache, MemoryCache } from '@aomex/core';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport microtime from 'microtime';\nimport ms from 'ms';\n\nexport interface RateLimitOptions {\n /**\n * The database powering the backing rate-limiter package.\n */\n cache?: Cache;\n\n /**\n * The length of a single limiting period. This value is expressed\n * in milliseconds, defaulting to one hour.\n */\n duration?: number;\n\n /**\n * The maximum amount of requests a client (see the `id` field) may\n * make during a limiting period. (see `duration`)\n */\n max?: number;\n\n /**\n * Get the unique-identifier for a request. This defaults to the\n * client's IP address. Returning \"false\" will skip rate-limiting.\n */\n id?: (ctx: WebContext) => string | false;\n\n /**\n * Whether or not to disable the usage of rate limit headers. This defaults\n * to **false**.\n */\n disableHeader?: boolean;\n\n /**\n * The message used on the response body if a client is rate-limited. There is\n * a default message; which includes when they should try again.\n */\n errorMessage?: string;\n\n /**\n * Whether or not to throw an error upon being rate-limited. This uses\n * the `ctx.throw()`.\n */\n throw?: boolean;\n\n /**\n * A relation of header to the header's display name.\n */\n headers?: {\n /**\n * The amount of requests remaining in the current limiting period.\n */\n remaining?: string;\n\n /**\n * The time, expressed as a UNIX epoch timestamp, at which your rate-limit expires.\n */\n reset?: string;\n\n /**\n * The total amount of requests a client may make during a limiting period.\n */\n total?: string;\n };\n\n /**\n * If function returns true, middleware exits before limiting\n */\n allowList?: (ctx: WebContext) => boolean | Promise<boolean>;\n\n /**\n * If function returns true, 403 error is thrown\n */\n denyList?: (ctx: WebContext) => boolean | Promise<boolean>;\n}\n\ninterface LimitState {\n id: string;\n reset: number;\n current: number;\n total: number;\n}\n\nconst getState = async (\n cache: Cache,\n id: string,\n max: number,\n duration: number,\n) => {\n const key = `ratelimit:${id}`;\n let state = await cache.get<LimitState>(key);\n const now = microtime.now();\n const expired = state && state.reset * 1e6 < now;\n\n if (!state || expired) {\n state = {\n id,\n reset: (now + duration * 1e3) / 1e6,\n current: max,\n total: max,\n };\n } else {\n state.current = state.current > 0 ? state.current - 1 : 0;\n }\n\n await cache.set(key, state);\n return state!;\n};\n\nexport const rateLimit = (options: RateLimitOptions = {}): WebMiddleware => {\n const {\n cache = new MemoryCache(),\n duration = 3600 * 1000, // 1 hour\n max = 2500,\n id: getId = (ctx) => ctx.request.ip,\n headers = {},\n disableHeader = false,\n denyList,\n allowList,\n } = options;\n\n const {\n remaining = 'X-RateLimit-Remaining',\n reset = 'X-RateLimit-Reset',\n total = 'X-RateLimit-Limit',\n } = headers;\n\n return middleware.web(async (ctx, next) => {\n if (denyList && (await denyList(ctx))) {\n ctx.throw(403);\n }\n\n const id = getId(ctx);\n if (id === false || (allowList && (await allowList(ctx)))) {\n return next();\n }\n\n const { response } = ctx;\n const state = await getState(cache, id, max, duration);\n let headers: Record<string, number> = {};\n if (!disableHeader) {\n headers = {\n [remaining]: state.current > 0 ? state.current - 1 : 0,\n [reset]: state.reset,\n [total]: state.total,\n };\n response.setHeaders(headers);\n }\n\n if (state.current) return next();\n\n const now = Date.now();\n const delta = Math.round(state.reset * 1e3 - now);\n const after = Math.round(state.reset - now / 1e3);\n const status = 429;\n const body =\n options.errorMessage ||\n `Rate limit exceeded, retry in ${ms(delta, { long: true })}.`;\n\n response.setHeader('Retry-After', after);\n response.statusCode = status;\n\n if (options.throw) {\n ctx.throw(status, body, { headers });\n } else {\n ctx.send(body);\n }\n });\n};\n"],"mappings":";AAAA,SAAS,YAAmB,mBAAmB;AAE/C,OAAO,eAAe;AACtB,OAAO,QAAQ;AAkFf,IAAM,WAAW,OACf,OACA,IACA,KACA,aACG;AACH,QAAM,MAAM,aAAa;AACzB,MAAI,QAAQ,MAAM,MAAM,IAAgB,GAAG;AAC3C,QAAM,MAAM,UAAU,IAAI;AAC1B,QAAM,UAAU,SAAS,MAAM,QAAQ,MAAM;AAE7C,MAAI,CAAC,SAAS,SAAS;AACrB,YAAQ;AAAA,MACN;AAAA,MACA,QAAQ,MAAM,WAAW,OAAO;AAAA,MAChC,SAAS;AAAA,MACT,OAAO;AAAA,I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,QAAQ,MAAM,GAAG;AAChD,UAAM,SAAS;AACf,UAAM,OACJ,QAAQ,gBACR,iCAAiC,GAAG,OAAO,EAAE,MAAM,KAAK,CAAC;AAE3D,aAAS,UAAU,eAAe,KAAK;AACvC,aAAS,aAAa;AAEtB,QAAI,QAAQ,OAAO;AACjB,UAAI,MAAM,QAAQ,MAAM,EAAE,SAAAA,SAAQ,CAAC;AAAA,IACrC,OAAO;AACL,UAAI,KAAK,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AACH;","names":["headers"]}
|
package/package.json
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/rate-limit",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"keywords": [
|
|
6
|
-
"rate-limit"
|
|
7
|
-
],
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "Web request rate limit",
|
|
8
5
|
"repository": "git@github.com:aomex/aomex.git",
|
|
9
6
|
"homepage": "https://aomex.js.org",
|
|
10
7
|
"license": "MIT",
|
|
@@ -29,16 +26,17 @@
|
|
|
29
26
|
"access": "public"
|
|
30
27
|
},
|
|
31
28
|
"peerDependencies": {
|
|
32
|
-
"@aomex/core": "^0.0.
|
|
33
|
-
"@aomex/web": "^0.0.
|
|
29
|
+
"@aomex/core": "^0.0.6",
|
|
30
|
+
"@aomex/web": "^0.0.7"
|
|
34
31
|
},
|
|
35
32
|
"dependencies": {
|
|
36
33
|
"microtime": "^3.1.1",
|
|
37
34
|
"ms": "^2.1.3"
|
|
38
35
|
},
|
|
39
36
|
"devDependencies": {
|
|
40
|
-
"@aomex/core": "^0.0.
|
|
41
|
-
"@aomex/
|
|
37
|
+
"@aomex/core": "^0.0.6",
|
|
38
|
+
"@aomex/utility": "^0.0.4",
|
|
39
|
+
"@aomex/web": "^0.0.7",
|
|
42
40
|
"@types/microtime": "^2.1.0",
|
|
43
41
|
"@types/ms": "^0.7.31"
|
|
44
42
|
},
|