@6thbridge/utils 1.10.3 → 1.10.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/lib/middlewares/index.d.ts +1 -0
- package/lib/middlewares/index.js +3 -1
- package/lib/middlewares/index.js.map +1 -1
- package/lib/middlewares/rate-limiter.d.ts +18 -0
- package/lib/middlewares/rate-limiter.js +43 -0
- package/lib/middlewares/rate-limiter.js.map +1 -0
- package/lib/middlewares/rate-limiter.test.d.ts +1 -0
- package/lib/middlewares/rate-limiter.test.js +208 -0
- package/lib/middlewares/rate-limiter.test.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -1
- package/readme.md +50 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@6thbridge/utils",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.5",
|
|
4
4
|
"description": "simple interface for the our commonly used functions",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"bignumber.js": "^11.1.5",
|
|
32
32
|
"csv-parse": "^7.0.2",
|
|
33
33
|
"dayjs": "^1.11.23",
|
|
34
|
+
"express-rate-limit": "8.7.1",
|
|
34
35
|
"ioredis": "^6.0.0",
|
|
35
36
|
"joi": "^18.3.1",
|
|
36
37
|
"libphonenumber-js": "^1.13.13",
|
|
@@ -39,6 +40,7 @@
|
|
|
39
40
|
"mongoose-paginate-v2": "^1.9.5",
|
|
40
41
|
"path-to-regexp": "^8.4.2",
|
|
41
42
|
"randomstring": "^1.3.1",
|
|
43
|
+
"rate-limit-redis": "6.0.1",
|
|
42
44
|
"redlock": "^5.0.0-beta.2",
|
|
43
45
|
"rollbar": "^3.1.0",
|
|
44
46
|
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz",
|
package/readme.md
CHANGED
|
@@ -90,6 +90,56 @@ Express.js middlewares for common functionality.
|
|
|
90
90
|
- **Authentication**: JWT and API key validation
|
|
91
91
|
- **Validation**: Request/response validation
|
|
92
92
|
- **Error Handling**: Centralized error processing
|
|
93
|
+
- **Rate Limiting**: Redis-backed request limits with a service-owned ioredis connection
|
|
94
|
+
|
|
95
|
+
#### Shared rate limiter
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import Redis from 'ioredis';
|
|
99
|
+
import { Ratelimiter } from '@6thbridge/utils';
|
|
100
|
+
|
|
101
|
+
const redisClient = new Redis(process.env.REDIS_URL);
|
|
102
|
+
Ratelimiter.configure({
|
|
103
|
+
redisClient,
|
|
104
|
+
serviceName: 'my-service',
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const loginLimiter = Ratelimiter.limit({
|
|
108
|
+
keyPrefix: 'login',
|
|
109
|
+
numberOfTries: 5,
|
|
110
|
+
skipSuccessfulRequests: true,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Use loginLimiter as Express middleware on your login route.
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The default limit is five attempts per IP in a 30-minute window. Exceeding it
|
|
117
|
+
returns HTTP 429 with `errorCode: 'RateLimitExceeded'`. Set `skipSuccessfulRequests`
|
|
118
|
+
to exclude successful responses from the count; it defaults to `false`.
|
|
119
|
+
|
|
120
|
+
Call `Ratelimiter.configure({ redisClient, serviceName })` during startup before
|
|
121
|
+
creating limiters. It accepts an existing standard ioredis `Redis` connection;
|
|
122
|
+
the calling service owns connection setup and shutdown. Calling `limit` before
|
|
123
|
+
configuration throws an error. Individual `limit` calls no longer take a client.
|
|
124
|
+
|
|
125
|
+
`serviceName` is optional and defaults to `process.env.APP_NAME`, so
|
|
126
|
+
`Ratelimiter.configure({ redisClient })` is enough when `APP_NAME` is set.
|
|
127
|
+
An explicit `serviceName` takes precedence. Configuration throws if neither
|
|
128
|
+
provides a service name.
|
|
129
|
+
|
|
130
|
+
Every Redis key starts with `rl:<serviceName>:`. Set a route-specific `keyPrefix`
|
|
131
|
+
to isolate counters within a service; its default is `rate-limit-key`. For example,
|
|
132
|
+
the login limiter above uses `rl:my-service:login:<normalized-ip>`. Services with
|
|
133
|
+
different names can safely share Redis. Existing limiters retain the configuration
|
|
134
|
+
they were created with if `configure` is called again.
|
|
135
|
+
|
|
136
|
+
An optional synchronous `keyGenerator(req, res)` replaces the default key,
|
|
137
|
+
including `keyPrefix`, while preserving the service prefix. Without it, the
|
|
138
|
+
limiter uses `res.locals.ip`, then
|
|
139
|
+
`cf-connecting-ip`, `x-forwarded-for`, the `ip` header, or `req.ip`, in that order,
|
|
140
|
+
and normalizes IPv6 addresses. Configure trusted proxies to supply these values.
|
|
141
|
+
|
|
142
|
+
The class is also available as `middlewares.Ratelimiter` from the package root.
|
|
93
143
|
|
|
94
144
|
### 📊 [Monitoring](./src/monitoring/README.md)
|
|
95
145
|
Application monitoring and observability tools.
|