You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+134Lines changed: 134 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -247,6 +247,138 @@ const tap = mock.taps.inject(
247
247
);
248
248
```
249
249
250
+
# Rate Limiting
251
+
252
+
MockHttp supports rate limiting using [@fastify/rate-limit](https://github.com/fastify/fastify-rate-limit). Rate limiting is **disabled by default** and can be enabled by providing configuration options.
253
+
254
+
## Enabling Rate Limiting
255
+
256
+
To enable rate limiting, pass a `rateLimit` configuration object when creating your MockHttp instance:
257
+
258
+
```javascript
259
+
import { MockHttp } from'@jaredwray/mockhttp';
260
+
261
+
constmock=newMockHttp({
262
+
rateLimit: {
263
+
max:100, // Maximum 100 requests
264
+
timeWindow:'1 minute'// Per 1 minute window
265
+
}
266
+
});
267
+
268
+
awaitmock.start();
269
+
```
270
+
271
+
## Common Configuration Options
272
+
273
+
The `rateLimit` option accepts all [@fastify/rate-limit options](https://github.com/fastify/fastify-rate-limit#options):
274
+
275
+
### Basic Rate Limiting
276
+
277
+
```javascript
278
+
// Limit to 50 requests per minute
279
+
constmock=newMockHttp({
280
+
rateLimit: {
281
+
max:50,
282
+
timeWindow:'1 minute'
283
+
}
284
+
});
285
+
```
286
+
287
+
### Stricter Limits with Custom Error Response
288
+
289
+
```javascript
290
+
constmock=newMockHttp({
291
+
rateLimit: {
292
+
max:30,
293
+
timeWindow:60000, // 1 minute in milliseconds
294
+
errorResponseBuilder: (req, context) => ({
295
+
statusCode:429,
296
+
error:'Too Many Requests',
297
+
message:`Rate limit exceeded. Try again in ${context.after}`
298
+
})
299
+
}
300
+
});
301
+
```
302
+
303
+
### Allow List (Exclude Specific IPs)
304
+
305
+
```javascript
306
+
constmock=newMockHttp({
307
+
rateLimit: {
308
+
max:100,
309
+
timeWindow:'1 minute',
310
+
allowList: ['127.0.0.1', '192.168.1.100'] // These IPs bypass rate limiting
311
+
}
312
+
});
313
+
```
314
+
315
+
### Custom Key Generator (Rate Limit by Header)
316
+
317
+
```javascript
318
+
constmock=newMockHttp({
319
+
rateLimit: {
320
+
max:100,
321
+
timeWindow:'1 minute',
322
+
keyGenerator: (request) => {
323
+
// Rate limit by API key instead of IP
324
+
returnrequest.headers['x-api-key'] ||request.ip;
325
+
}
326
+
}
327
+
});
328
+
```
329
+
330
+
### Advanced Configuration
331
+
332
+
```javascript
333
+
constmock=newMockHttp({
334
+
rateLimit: {
335
+
global:true, // Apply to all routes
336
+
max:100, // Max requests
337
+
timeWindow:'1 minute', // Time window
338
+
cache:10000, // Cache size for tracking clients
339
+
skipOnError:false, // Don't skip on storage errors
340
+
ban:10, // Ban after 10 rate limit violations
341
+
continueExceeding:false, // Don't reset window on each request
342
+
enableDraftSpec:true, // Use IETF draft spec headers
343
+
addHeaders: { // Customize rate limit headers
344
+
'x-ratelimit-limit':true,
345
+
'x-ratelimit-remaining':true,
346
+
'x-ratelimit-reset':true
347
+
}
348
+
}
349
+
});
350
+
```
351
+
352
+
## Disabling Rate Limiting
353
+
354
+
Rate limiting is disabled by default. To explicitly disable it (or disable it after it was enabled):
355
+
356
+
```javascript
357
+
constmock=newMockHttp(); // No rateLimit option = disabled
358
+
359
+
// Or explicitly set to undefined
360
+
constmock2=newMockHttp({
361
+
rateLimit:undefined
362
+
});
363
+
```
364
+
365
+
## Available Options
366
+
367
+
| Option | Type | Default | Description |
368
+
|--------|------|---------|-------------|
369
+
|`max`| number \| function |`1000`| Maximum requests per time window |
370
+
|`timeWindow`| number \| string |`60000`| Duration of rate limit window (milliseconds or string like '1 minute') |
371
+
|`cache`| number |`5000`| LRU cache size for tracking clients |
372
+
|`allowList`| array \| function |`[]`| IPs or function to exclude from rate limiting |
373
+
|`keyGenerator`| function | IP-based | Function to generate unique client identifier |
374
+
|`errorResponseBuilder`| function | Default 429 | Custom error response function |
0 commit comments