@aomex/rate-limit 0.0.4 → 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 -33
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,39 +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.3
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- [`d8debb9`](https://github.com/aomex/aomex/commit/d8debb9b6319d30e48cb77d97103b5559c57789c) Thanks [@geekact](https://github.com/geekact)! - refactor(web): response 的 header 均使用首字母大写的格式
|
|
18
|
-
|
|
19
|
-
- Updated dependencies [[`d8debb9`](https://github.com/aomex/aomex/commit/d8debb9b6319d30e48cb77d97103b5559c57789c), [`64c10e1`](https://github.com/aomex/aomex/commit/64c10e17ff92251bb2d9f45e0bddde5cba8a3b20), [`6a57463`](https://github.com/aomex/aomex/commit/6a57463c1107c38a8b8cf5fa00ff3289bf629c0a), [`8857115`](https://github.com/aomex/aomex/commit/88571153b7e84ec6d66d37970c3ef7b4e1924a03), [`68b1365`](https://github.com/aomex/aomex/commit/68b13655922a0714bcdd2101ce659da382d44e8b), [`b51a81a`](https://github.com/aomex/aomex/commit/b51a81a65313073dff5307b310b6656da2ad5186), [`ee021e3`](https://github.com/aomex/aomex/commit/ee021e342814f12777e538c06a19971e1020f201), [`b5aaec5`](https://github.com/aomex/aomex/commit/b5aaec540e3d732868a270a969a94ccd6ff9ac6a), [`fd6ae40`](https://github.com/aomex/aomex/commit/fd6ae40577bd9a62c211d4615483fc5dd58f5865)]:
|
|
20
|
-
- @aomex/web@0.0.5
|
|
21
|
-
- @aomex/core@0.0.4
|
|
22
|
-
|
|
23
|
-
## 0.0.2
|
|
24
|
-
|
|
25
|
-
### Patch Changes
|
|
26
|
-
|
|
27
|
-
- 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)]:
|
|
28
|
-
- @aomex/web@0.0.4
|
|
29
|
-
- @aomex/core@0.0.3
|
|
30
|
-
|
|
31
|
-
## 0.0.1
|
|
32
|
-
|
|
33
|
-
### Patch Changes
|
|
34
|
-
|
|
35
|
-
- [`7bcb2fc`](https://github.com/aomex/aomex/commit/7bcb2fccbe99675b0edca32ff3af9599113e754b) Thanks [@geekact](https://github.com/geekact)! - 增加 web 插件 rate-limit
|
|
36
|
-
|
|
37
|
-
- 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)]:
|
|
38
|
-
- @aomex/web@0.0.3
|
|
39
|
-
- @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
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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,IACT;AAAA,EACF,OAAO;AACL,UAAM,UAAU,MAAM,UAAU,IAAI,MAAM,UAAU,IAAI;AAAA,EAC1D;AAEA,QAAM,MA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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,17 +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/
|
|
42
|
-
"@aomex/web": "^0.0.
|
|
37
|
+
"@aomex/core": "^0.0.6",
|
|
38
|
+
"@aomex/utility": "^0.0.4",
|
|
39
|
+
"@aomex/web": "^0.0.7",
|
|
43
40
|
"@types/microtime": "^2.1.0",
|
|
44
41
|
"@types/ms": "^0.7.31"
|
|
45
42
|
},
|