-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create a package to maintain the throttler-storage-redis module
- Loading branch information
Showing
31 changed files
with
2,993 additions
and
2,488 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# NestJS Throttler Redis Storage | ||
|
||
 | ||
[](https://www.npmjs.com/package/nestjs-throttler-storage-redis) | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
17 changes: 17 additions & 0 deletions
17
packages/throttler-storage-redis/src/throttler-storage-redis.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Oops, something went wrong.