Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Add a code to customize the key. #92

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- [omitResponseHeaders](https://github.com/ozkanonur/nestjs-rate-limiter#-omitResponseHeaders)
- [errorMessage](https://github.com/ozkanonur/nestjs-rate-limiter#-errorMessage)
- [customResponseSchema](https://github.com/ozkanonur/nestjs-rate-limiter#-customResponseSchema)
- [Override Functions](https://github.com/ozkanonur/nestjs-rate-limiter#override-functions)
- [Benchmarks](https://github.com/ozkanonur/nestjs-rate-limiter#benchmarks)
- [TODO List](https://github.com/ozkanonur/nestjs-rate-limiter#todo)

Expand Down Expand Up @@ -465,6 +466,22 @@ GraphQLModule.forRoot({
<br>

customResponseSchema option allows to provide customizable response schemas

# Override Functions

It's possible to override <code>getIpFromRequest</code> function by extending <code>RateLimiterGuard</code> class.

```ts
import { RateLimiterGuard } from 'nestjs-rate-limiter'
import type { Request } from 'express'

class ExampleRateLimiterGuard extends RateLimiterGuard {
protected getIpFromRequest(request: Request): string {
return request.get('x-forwarded-for');
}
}
```

# Benchmarks

1000 concurrent clients with maximum 2000 requests per sec during 30 seconds.
Expand Down
1 change: 1 addition & 0 deletions lib/default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const defaultRateLimiterOptions: RateLimiterOptions = {
for: 'Express',
type: 'Memory',
keyPrefix: 'global',
keyFactory: undefined,
points: 4,
pointsConsumed: 1,
inmemoryBlockOnConsumed: 0,
Expand Down
17 changes: 14 additions & 3 deletions lib/rate-limiter.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,24 @@ export class RateLimiterGuard implements CanActivate {
const request = this.httpHandler(context).req
const response = this.httpHandler(context).res

const rateLimiter: RateLimiterAbstract = await this.getRateLimiter(reflectedOptions)
const key = request.ip?.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/)?.[0]
if (reflectedOptions.keyFactory(request)) {
const rateLimiter: RateLimiterAbstract = await this.getRateLimiter(reflectedOptions)
const key = reflectedOptions.keyFactory(request)

await this.responseHandler(response, key, rateLimiter, points, pointsConsumed)
await this.responseHandler(response, key, rateLimiter, points, pointsConsumed)
} else {
const rateLimiter: RateLimiterAbstract = await this.getRateLimiter(reflectedOptions)
const key = this.getIpFromRequest(request)

await this.responseHandler(response, key, rateLimiter, points, pointsConsumed)
}
return true
}

protected getIpFromRequest(request: { ip: string }): string {
return request.ip?.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/)?.[0]
}

private httpHandler(context: ExecutionContext) {
if (this.options.for === 'ExpressGraphql') {
return {
Expand Down
1 change: 1 addition & 0 deletions lib/rate-limiter.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface RateLimiterOptions {
for?: 'Express' | 'Fastify' | 'Microservice' | 'ExpressGraphql' | 'FastifyGraphql'
type?: 'Memory' | 'Redis' | 'Memcache' | 'Postgres' | 'MySQL' | 'Mongo'
keyPrefix?: string
keyFactory?: (request: Request) => {}
points?: number
pointsConsumed?: number
inmemoryBlockDuration?: number
Expand Down
Loading