Skip to content

Commit

Permalink
feat(decorator): add setThrottlerMetadata() function back
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen authored and jmcdo29 committed Jun 12, 2020
1 parent 4be7fab commit ea31a9c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/throttle.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { THROTTLER_LIMIT, THROTTLER_SKIP, THROTTLER_TTL } from './throttler.constants';

function setThrottlerMetadata(target: any, limit: number, ttl: number): void {
Reflect.defineMetadata(THROTTLER_TTL, ttl, target);
Reflect.defineMetadata(THROTTLER_LIMIT, limit, target);
}

/**
* Adds metadata to the target which will be handled by the ThrottlerGuard to
* handle incoming requests based on the given metadata.
Expand All @@ -12,12 +17,10 @@ export const Throttle = (limit = 20, ttl = 60): MethodDecorator & ClassDecorator
descriptor?: TypedPropertyDescriptor<any>,
) => {
if (descriptor) {
Reflect.defineMetadata(THROTTLER_LIMIT, limit, descriptor.value);
Reflect.defineMetadata(THROTTLER_TTL, ttl, descriptor.value);
setThrottlerMetadata(descriptor.value, limit, ttl);
return descriptor;
}
Reflect.defineMetadata(THROTTLER_LIMIT, limit, target);
Reflect.defineMetadata(THROTTLER_TTL, ttl, target);
setThrottlerMetadata(target, limit, ttl);
return target;
};
};
Expand Down
4 changes: 2 additions & 2 deletions src/throttler.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export class ThrottlerGuard implements CanActivate {
): Promise<boolean> {
const client = context.switchToWs().getClient();
const ip = ['conn', '_socket']
.map(key => client[key])
.filter(obj => obj)
.map((key) => client[key])
.filter((obj) => obj)
.shift().remoteAddress;
const key = this.generateKey(context, ip);
const ttls = await this.storageService.getRecord(key);
Expand Down

0 comments on commit ea31a9c

Please sign in to comment.