Skip to content

Commit a1b9330

Browse files
committed
use more sensible rate limiting
1 parent 5d129ea commit a1b9330

File tree

5 files changed

+268
-241
lines changed

5 files changed

+268
-241
lines changed

src/api/plugins/rateLimiter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FastifyPluginAsync, FastifyRequest, FastifyReply } from "fastify";
55
interface RateLimiterOptions {
66
limit?: number | ((request: FastifyRequest) => number);
77
duration?: number;
8-
rateLimitIdentifier?: string;
8+
rateLimitIdentifier?: string | ((request: FastifyRequest) => string);
99
}
1010

1111
const rateLimiterPlugin: FastifyPluginAsync<RateLimiterOptions> = async (
@@ -22,12 +22,16 @@ const rateLimiterPlugin: FastifyPluginAsync<RateLimiterOptions> = async (
2222
async (request: FastifyRequest, reply: FastifyReply) => {
2323
const userIdentifier = request.ip;
2424
let computedLimit = limit;
25+
let computedIdentifier = rateLimitIdentifier;
2526
if (typeof computedLimit === "function") {
2627
computedLimit = computedLimit(request);
2728
}
29+
if (typeof computedIdentifier === "function") {
30+
computedIdentifier = computedIdentifier(request);
31+
}
2832
const { limited, resetTime, used } = await isAtLimit({
2933
ddbClient: fastify.dynamoClient,
30-
rateLimitIdentifier,
34+
rateLimitIdentifier: computedIdentifier,
3135
duration,
3236
limit: computedLimit,
3337
userIdentifier,

0 commit comments

Comments
 (0)