Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a package to maintain the throttler-storage-redis module #48

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions .changeset/rare-dogs-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@nest-lab/throttler-storage-redis': major
---

Initial release of the @nest-lab/throttler-storage-redis package

This package was
[initially maintained by kkoomen](https://github.com/kkoomen/nestjs-throttler-storage-redis),
but has since been brought into the `@nest-lab/` repository for management.
Nothing about the usage of the module has changed, other than a slight variation
of the name.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"nrwl.angular-console",
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint"
"dbaeumer.vscode-eslint",
"firsttris.vscode-jest-runner"
]
}
5 changes: 5 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"@nx/vite:test": {
"cache": true,
"inputs": ["default", "^production"]
},
"@nx/js:tsc": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
}
},
"nxCloudAccessToken": "",
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
"private": true,
"devDependencies": {
"@changesets/cli": "^2.23.2",
"@nestjs/common": "10.0.3",
"@nestjs/core": "10.0.3",
"@nestjs/platform-express": "10.0.3",
"@nestjs/platform-fastify": "10.0.3",
"@nestjs/common": "10.4.1",
"@nestjs/core": "10.4.1",
"@nestjs/platform-express": "10.4.1",
"@nestjs/platform-fastify": "10.4.1",
"@nestjs/schematics": "10.0.1",
"@nestjs/testing": "10.0.3",
"@nestjs/testing": "10.4.1",
"@nestjs/throttler": "^6.2.1",
"@nrwl/tao": "19.6.1",
"@nx/eslint": "19.6.1",
"@nx/eslint-plugin": "19.6.1",
Expand Down Expand Up @@ -76,15 +77,16 @@
"fastify": "^4.18.0",
"fastify-multer": "^2.0.3",
"io-ts": "^2.2.20",
"ioredis": "^5.4.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jest-environment-node": "^29.4.1",
"jest-environment-node": "^29.7.0",
"joi": "^17.13.3",
"nx": "19.6.1",
"ow": "^0.28.2",
"pactum": "^3.1.13",
"prettier": "^2.7.1",
"reflect-metadata": "^0.1.13",
"reflect-metadata": "^0.2.2",
"runtypes": "^6.7.0",
"rxjs": "^7.8.0",
"superstruct": "^1.0.4",
Expand Down
25 changes: 25 additions & 0 deletions packages/throttler-storage-redis/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
84 changes: 84 additions & 0 deletions packages/throttler-storage-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# NestJS Throttler Redis Storage

Redis storage provider for the
[@nestjs/throttler](https://github.com/nestjs/throttler) package.

# Installation

### Yarn

- `yarn add @nest-lab/throttler-storage-redis ioredis`

### NPM

- `npm install --save @nest-lab/throttler-storage-redis ioredis`

# Usage

Basic usage:

```ts
import { ThrottlerModule, seconds } from '@nestjs/throttler';
import { ThrottlerStorageRedisService } from '@nest-lab/throttler-storage-redis';
import Redis from 'ioredis';

@Module({
imports: [
ThrottlerModule.forRoot({
throttlers: [{ limit: 5, ttl: seconds(60) }],

// Below are possible options on how to configure the storage service.

// default config (host = localhost, port = 6379)
storage: new ThrottlerStorageRedisService(),

// connection url
storage: new ThrottlerStorageRedisService('redis://'),

// redis object
storage: new ThrottlerStorageRedisService(new Redis()),

// redis clusters
storage: new ThrottlerStorageRedisService(
new Redis.Cluster(nodes, options)
),
}),
],
})
export class AppModule {}
```

Inject another config module and service:

```ts
import { ThrottlerModule } from '@nestjs/throttler';
import { ThrottlerStorageRedisService } from '@nest-lab/throttler-storage-redis';

@Module({
imports: [
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
throttlers: [
{
ttl: config.get('THROTTLE_TTL'),
limit: config.get('THROTTLE_LIMIT'),
},
],
storage: new ThrottlerStorageRedisService(),
}),
}),
],
})
export class AppModule {}
```

# Issues

Bugs and features related to the redis implementation are welcome in this
repository.

# License

NestJS Throttler Redis Storage is licensed under the MIT license.
15 changes: 15 additions & 0 deletions packages/throttler-storage-redis/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
redis:
image: redis
container_name: redis
ports:
- '6379:6379'

redis_cluster:
container_name: redis_cluster
image: grokzen/redis-cluster:7.0.10
environment:
- 'IP=0.0.0.0'
ports:
- '7000-7050:7000-7050'
- '5000-5010:5000-5010'
11 changes: 11 additions & 0 deletions packages/throttler-storage-redis/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'throttler-storage-redis',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/throttler-storage-redis',
};
56 changes: 56 additions & 0 deletions packages/throttler-storage-redis/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@nest-lab/throttler-storage-redis",
"description": "Redis storage provider for the @nestjs/throttler package",
"version": "0.0.1",
"keywords": [
"nestjs",
"rate-limit",
"throttle",
"express",
"fastify",
"redis"
],
"repository": {
"type": "github",
"url": "https://github.com/jmcdo29/nest-lab",
"directory": "packages/throttler-storage-redis"
},
"private": false,
"license": "MIT",
"dependencies": {
"tslib": "^2.3.0"
},
"devDependencies": {
"@nestjs/platform-express": "10.4.1",
"@nestjs/platform-fastify": "10.4.1",
"@nestjs/testing": "10.4.1",
"ioredis": "5.4.1"
},
"peerDependencies": {
"@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0",
"@nestjs/core": "^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0",
"@nestjs/throttler": ">=6.0.0",
"ioredis": ">=5.0.0",
"reflect-metadata": "^0.2.1"
},
"peerDependenciesMeta": {
"@nestjs/common": {
"optional": false
},
"@nestjs/core": {
"optional": false
},
"@nestjs/throttler": {
"optional": false
},
"ioredis": {
"optional": false
},
"reflect-metadata": {
"optional": false
}
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
69 changes: 69 additions & 0 deletions packages/throttler-storage-redis/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "throttler-storage-redis",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/throttler-storage-redis/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/throttler-storage-redis",
"tsConfig": "packages/throttler-storage-redis/tsconfig.lib.json",
"packageJson": "packages/throttler-storage-redis/package.json",
"main": "packages/throttler-storage-redis/src/index.ts",
"assets": ["packages/throttler-storage-redis/*.md"]
}
},
"publish": {
"executor": "nx:run-commands",
"options": {
"cwd": "dist/packages/throttler-storage-redis",
"command": "pnpm publish"
},
"dependsOn": [
{
"target": "build"
}
]
},
"lint": {
"executor": "@nx/eslint:lint"
},
"start-docker": {
"executor": "nx:run-commands",
"options": {
"cwd": "packages/throttler-storage-redis",
"command": "docker compose up -d"
},
"cache": false
},
"wait-for-docker": {
"executor": "nx:run-commands",
"options": {
"commands": ["nc -vzw 5000 localhost 5000-5010 6379 7000-7050"],
"parallel": true
},
"cache": false,
"dependsOn": ["start-docker"]
},
"run-tests": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/throttler-storage-redis/jest.config.ts"
},
"dependsOn": ["wait-for-docker"]
},
"test": {
"executor": "nx:run-commands",
"options": {
"cwd": "packages/throttler-storage-redis",
"command": "docker compose down"
},
"cache": false,
"dependsOn": ["run-tests"]
}
}
}
2 changes: 2 additions & 0 deletions packages/throttler-storage-redis/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './throttler-storage-redis.interface';
export * from './throttler-storage-redis.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ThrottlerStorage } from '@nestjs/throttler';
import Redis, { Cluster } from 'ioredis';

export interface ThrottlerStorageRedis {
/**
* The redis instance.
*/
redis: Redis | Cluster;

/**
* Increment the amount of requests for a given record. The record will
* automatically be removed from the storage once its TTL has been reached.
*/
increment: ThrottlerStorage['increment'];
}

export const ThrottlerStorageRedis = Symbol('ThrottlerStorageRedis');
Loading
Loading