-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './client.exception'; | ||
export * from './restricted.exception'; | ||
export * from './validation.exception'; | ||
export * from './throttler.exception'; |
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,7 @@ | ||
import { ClientException } from './client.exception'; | ||
|
||
export class ThrottlerException extends ClientException { | ||
constructor() { | ||
super('rate-limited: slow down there chief'); | ||
} | ||
} |
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 @@ | ||
export * from './ws-throttler.guard'; |
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,39 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { ThrottlerStorageService } from '@nestjs/throttler'; | ||
import { WsThrottlerGuard } from './ws-throttler.guard'; | ||
|
||
describe('WsThrottlerGuard', () => { | ||
const context = createMock<ExecutionContext>({ | ||
getClass: jest.fn().mockReturnValue({ name: 'Test' }), | ||
getHandler: jest.fn().mockReturnValue({ name: 'test' }), | ||
switchToWs: jest.fn().mockReturnValue({ | ||
getClient: jest.fn().mockReturnValue({ id: 'test' }), | ||
}), | ||
}); | ||
|
||
let storageService: ThrottlerStorageService; | ||
|
||
beforeEach(() => { | ||
storageService = new ThrottlerStorageService(); | ||
}); | ||
|
||
afterEach(() => { | ||
storageService.onApplicationShutdown(); | ||
}); | ||
|
||
it('should be fine', async () => { | ||
const guard = new WsThrottlerGuard( | ||
{ limit: 2, ttl: 2 }, | ||
storageService, | ||
new Reflector(), | ||
); | ||
|
||
await expect(guard.canActivate(context)).resolves.toBe(true); | ||
await expect(guard.canActivate(context)).resolves.toBe(true); | ||
await expect(guard.canActivate(context)).rejects.toThrowError( | ||
'rate-limited: slow down there chief', | ||
); | ||
}); | ||
}); |
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,23 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { ThrottlerGuard } from '@nestjs/throttler'; | ||
import { WebSocket } from 'ws'; | ||
import { ThrottlerException } from '../exceptions'; | ||
|
||
@Injectable() | ||
export class WsThrottlerGuard extends ThrottlerGuard { | ||
protected async handleRequest( | ||
context: ExecutionContext, | ||
limit: number, | ||
ttl: number, | ||
): Promise<boolean> { | ||
const client = context.switchToWs().getClient<WebSocket>(); | ||
const key = this.generateKey(context, client.id); | ||
const { totalHits } = await this.storageService.increment(key, ttl); | ||
|
||
if (totalHits > limit) { | ||
throw new ThrottlerException(); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
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,8 @@ | ||
import { Environment } from './environment'; | ||
|
||
export function throttlerConfig(env: Environment) { | ||
return { | ||
limit: env.THROTTLER_LIMIT ?? 100, | ||
ttl: env.THROTTLER_TTL ?? 1, | ||
}; | ||
} |
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