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

Commit

Permalink
v3.1.0 (#85)
Browse files Browse the repository at this point in the history
* Adding getIpFromRequest method to get the request's IP (#84)

* upgrade to v3.1.0

Co-authored-by: Oleksandr Bordun <[email protected]>
  • Loading branch information
onur-ozkan and Borales committed Dec 1, 2021
1 parent b3b9c45 commit 8fe4589
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
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
6 changes: 5 additions & 1 deletion lib/rate-limiter.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,16 @@ export class RateLimiterGuard implements CanActivate {
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]
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-rate-limiter",
"version": "3.0.0",
"version": "3.1.0",
"description": "Highly configurable and extensible rate limiter library",
"repository": {
"type": "git",
Expand Down

0 comments on commit 8fe4589

Please sign in to comment.