import Topic from './Topic' import Request from './Request' import { ValidatorHandleType } from './Validator' export interface RawThrottleType { [path: string]: ValidatorHandleType } export type DirectionType = 'In' | 'Out' export default class Throttle { private name: string private filterList: Array<{ topic: Topic, validator: ValidatorHandleType }> = [] public disable: boolean = false public filterMap: { [path: string]: ValidatorHandleType } = {} constructor(name: string, throttle: RawThrottleType = null) { if (throttle === null) { this.disable = true } else { this.filterMap = throttle Object.keys(this.filterMap).map(path => { this.filterList.push({ topic: new Topic(path), validator: this.filterMap[path] }) }) } } check(request: Request): boolean { if (this.disable) return true for(let i = 0; i < this.filterList.length; i++) { const filter = this.filterList[i] if (filter.topic.equal(request.path).isEqual && filter.validator(request.data)) return true } return false } }