• Stars
    star
    619
  • Rank 72,496 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A rate limiting module for NestJS to work with Fastify, Express, GQL, Websockets, and RPC 🧭

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Travis Linux Coverage Discord Backers on Open Collective Sponsors on Open Collective

Description

A Rate-Limiter for NestJS, regardless of the context.

For an overview of the community storage providers, see Community Storage Providers.

This package comes with a couple of goodies that should be mentioned, first is the ThrottlerModule.

Installation

$ npm i --save @nestjs/throttler

Versions

@nestjs/throttler@^1 is compatible with Nest v7 while @nestjs/throttler@^2 is compatible with Nest v7 and Nest v8, but it is suggested to be used with only v8 in case of breaking changes against v7 that are unseen.

For NestJS v10, please use version 4.1.0 or above

Table of Contents

Usage

ThrottlerModule

The ThrottleModule is the main entry point for this package, and can be used in a synchronous or asynchronous manner. All the needs to be passed is the ttl, the time to live in seconds for the request tracker, and the limit, or how many times an endpoint can be hit before returning a 429.

import { APP_GUARD } from '@nestjs/core';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';

@Module({
  imports: [
    ThrottlerModule.forRoot({
      ttl: 60,
      limit: 10,
    }),
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
  ],
})
export class AppModule {}

The above would mean that 10 requests from the same IP can be made to a single endpoint in 1 minute.

@Module({
  imports: [
    ThrottlerModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        ttl: config.get('THROTTLE_TTL'),
        limit: config.get('THROTTLE_LIMIT'),
      }),
    }),
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
  ],
})
export class AppModule {}

The above is also a valid configuration for asynchronous registration of the module.

NOTE: If you add the ThrottlerGuard to your AppModule as a global guard then all the incoming requests will be throttled by default. This can also be omitted in favor of @UseGuards(ThrottlerGuard). The global guard check can be skipped using the @SkipThrottle() decorator mentioned later.

Example with @UseGuards(ThrottlerGuard):

// app.module.ts
@Module({
  imports: [
    ThrottlerModule.forRoot({
      ttl: 60,
      limit: 10,
    }),
  ],
})
export class AppModule {}

// app.controller.ts
@Controller()
export class AppController {
  @UseGuards(ThrottlerGuard)
  @Throttle(5, 30)
  normal() {}
}

Decorators

@Throttle()

@Throttle(limit: number = 30, ttl: number = 60)

This decorator will set THROTTLER_LIMIT and THROTTLER_TTL metadatas on the route, for retrieval from the Reflector class. Can be applied to controllers and routes.

@SkipThrottle()

@SkipThrottle(skip = true)

This decorator can be used to skip a route or a class or to negate the skipping of a route in a class that is skipped.

@SkipThrottle()
@Controller()
export class AppController {
  @SkipThrottle(false)
  dontSkip() {}

  doSkip() {}
}

In the above controller, dontSkip would be counted against and rate-limited while doSkip would not be limited in any way.

Ignoring specific user agents

You can use the ignoreUserAgents key to ignore specific user agents.

@Module({
  imports: [
    ThrottlerModule.forRoot({
      ttl: 60,
      limit: 10,
      ignoreUserAgents: [
        // Don't throttle request that have 'googlebot' defined in them.
        // Example user agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
        /googlebot/gi,

        // Don't throttle request that have 'bingbot' defined in them.
        // Example user agent: Mozilla/5.0 (compatible; Bingbot/2.0; +http://www.bing.com/bingbot.htm)
        new RegExp('bingbot', 'gi'),
      ],
    }),
  ],
})
export class AppModule {}

ThrottlerStorage

Interface to define the methods to handle the details when it comes to keeping track of the requests.

Currently the key is seen as an MD5 hash of the IP the ClassName and the MethodName, to ensure that no unsafe characters are used and to ensure that the package works for contexts that don't have explicit routes (like Websockets and GraphQL).

The interface looks like this:

export interface ThrottlerStorage {
  storage: Record<string, ThrottlerStorageOptions>;
  increment(key: string, ttl: number): Promise<ThrottlerStorageRecord>;
}

So long as the Storage service implements this interface, it should be usable by the ThrottlerGuard.

Proxies

If you are working behind a proxy, check the specific HTTP adapter options (express and fastify) for the trust proxy option and enable it. Doing so will allow you to get the original IP address from the X-Forward-For header, and you can override the getTracker() method to pull the value from the header rather than from req.ip. The following example works with both express and fastify:

// throttler-behind-proxy.guard.ts
import { ThrottlerGuard } from '@nestjs/throttler';
import { Injectable } from '@nestjs/common';

@Injectable()
export class ThrottlerBehindProxyGuard extends ThrottlerGuard {
  protected getTracker(req: Record<string, any>): string {
    return req.ips.length ? req.ips[0] : req.ip; // individualize IP extraction to meet your own needs
  }
}

// app.controller.ts
import { ThrottlerBehindProxyGuard } from './throttler-behind-proxy.guard';
@UseGuards(ThrottlerBehindProxyGuard)

Working with Websockets

To work with Websockets you can extend the ThrottlerGuard and override the handleRequest method with something like the following method

@Injectable()
export class WsThrottlerGuard extends ThrottlerGuard {
  async handleRequest(context: ExecutionContext, limit: number, ttl: number): Promise<boolean> {
    const client = context.switchToWs().getClient();
    // this is a generic method to switch between `ws` and `socket.io`. You can choose what is appropriate for you
    const ip = ['conn', '_socket']
      .map((key) => client[key])
      .filter((obj) => obj)
      .shift().remoteAddress;
    const key = this.generateKey(context, ip);
    const { totalHits } = await this.storageService.increment(key, ttl);

    if (totalHits > limit) {
      throw new ThrottlerException();
    }

    return true;
  }
}

There are some things to take keep in mind when working with websockets:

  • You cannot bind the guard with APP_GUARD or app.useGlobalGuards() due to how Nest binds global guards.
  • When a limit is reached, Nest will emit an exception event, so make sure there is a listener ready for this.

Working with GraphQL

To get the ThrottlerModule to work with the GraphQL context, a couple of things must happen.

  • You must use Express and apollo-server-express as your GraphQL server engine. This is the default for Nest, but the apollo-server-fastify package does not currently support passing res to the context, meaning headers cannot be properly set.
  • When configuring your GraphQLModule, you need to pass an option for context in the form of ({ req, res}) => ({ req, res }). This will allow access to the Express Request and Response objects, allowing for the reading and writing of headers.
  • You must add in some additional context switching to get the ExecutionContext to pass back values correctly (or you can override the method entirely)
@Injectable()
export class GqlThrottlerGuard extends ThrottlerGuard {
  getRequestResponse(context: ExecutionContext) {
    const gqlCtx = GqlExecutionContext.create(context);
    const ctx = gqlCtx.getContext();
    return { req: ctx.req, res: ctx.res }; // ctx.request and ctx.reply for fastify
  }
}

Community Storage Providers

Feel free to submit a PR with your custom storage provider being added to this list.

License

Nest is MIT licensed.

More Repositories

1

nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
TypeScript
67,297
star
2

awesome-nestjs

A curated list of awesome things related to NestJS 😎
10,616
star
3

nest-cli

CLI tool for Nest applications 🍹
TypeScript
1,939
star
4

typeorm

TypeORM module for Nest framework (node.js) 🍇
TypeScript
1,906
star
5

typescript-starter

Nest framework TypeScript starter ☕
TypeScript
1,854
star
6

swagger

OpenAPI (Swagger) module for Nest framework (node.js) 🌎
TypeScript
1,671
star
7

graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
TypeScript
1,453
star
8

docs.nestjs.com

The official documentation https://docs.nestjs.com 📕
TypeScript
1,190
star
9

cqrs

A lightweight CQRS module for Nest framework (node.js) 🎈
TypeScript
827
star
10

terminus

Terminus module for Nest framework (node.js) 🤖
TypeScript
668
star
11

bull

Bull module for Nest framework (node.js) 🐮
TypeScript
602
star
12

jwt

JWT utilities module based on the jsonwebtoken package 🔓
TypeScript
599
star
13

mongoose

Mongoose module for Nest framework (node.js) 🍸
TypeScript
520
star
14

config

Configuration module for Nest framework (node.js) 🍓
TypeScript
513
star
15

passport

Passport module for Nest framework (node.js) 🔑
TypeScript
497
star
16

serve-static

Serve static websites (SPA's) using Nest framework (node.js) 🥦
TypeScript
454
star
17

ng-universal

Angular Universal module for Nest framework (node.js) 🌷
TypeScript
441
star
18

elasticsearch

Elasticsearch module based on the official elasticsearch package 🌿
TypeScript
382
star
19

schematics

Nest architecture element generation based on Angular schematics 🎬
TypeScript
382
star
20

mapped-types

Configuration module for Nest framework (node.js) 🐺
TypeScript
374
star
21

schedule

Schedule module for Nest framework (node.js) ⏰
TypeScript
350
star
22

sequelize

Sequelize module for Nest framework (node.js) 🍈
TypeScript
231
star
23

axios

Axios module for Nest framework (node.js) 🗂
TypeScript
219
star
24

event-emitter

Event Emitter module for Nest framework (node.js) 🦋
TypeScript
193
star
25

serverless-core-deprecated

[Deprecated] Serverless Core module for Nest framework (node.js) 🦊
TypeScript
171
star
26

azure-func-http

Azure Functions HTTP adapter for Nest framework (node.js) 🌥
TypeScript
151
star
27

nestjs.com

The official website https://nestjs.com 🏆
HTML
135
star
28

courses.nestjs.com

Official NestJS Courses website https://courses.nestjs.com 🏡
HTML
126
star
29

cache-manager

Cache manager module for Nest framework (node.js) 🗃
TypeScript
118
star
30

javascript-starter

Nest framework JavaScript (ES6, ES7, ES8) + Babel starter 🍰
JavaScript
117
star
31

azure-database

Azure CosmosDB Database module for Nest framework (node.js) ☁️
TypeScript
105
star
32

azure-storage

Azure Storage module for Nest framework (node.js) ☁️
TypeScript
86
star
33

enterprise.nestjs.com

The official website https://enterprise.nestjs.com 🌁
HTML
18
star
34

newsletter.nestjs.com

Official NestJS Newsletter website https://newsletter.nestjs.com 📩
HTML
12
star