UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

74 lines (73 loc) 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("@rjweb/utils"); class RateLimit { constructor() { this.data = { sortTo: utils_1.number.generate(1, 10000000), penalty: (0, utils_1.time)(10).s(), timeWindow: (0, utils_1.time)(10).s(), maxHits: Infinity }; } /** * Set the Ratelimit Identifier * * This is useful for when you want to make two code seperate rate limit rules act like one, just assign them the * same Identifier and they are interlinked. be wary, this *might* cause issues if the rate limits arent the same * rules. * @default number.generate(1, 10000000) * @warn ONLY USE IF YOU KNOW WHAT YOU ARE DOING * @since 8.7.2 */ identifier(identifier) { this.data.sortTo = identifier; return (0, utils_1.as)(this); } /** * Set the Penalty when hitting a rate limit in ms * * When the User hits the endpoint(s) more than `<maxHits>` in `<timeWindow>ms`, the penalty will be applied to * the user and the user wont be able to access the endpoint for `<penalty>ms`, after that the users limits are reset * for the endpoint(s). If the User hits the endpoint(s) less than `<maxHits>` in `<timeWindow>ms`, the penalty wont be applied * and if `<timeWindow>ms` has passed, the limits will reset without any penalty applying. * * You can always prevent a request / message from counting towards the ratelimit by calling `<HTTPRequest | WSMessage>.skipRateLimit()` * @default time(10).s() * @since 8.6.0 */ penalty(ms) { this.data.penalty = ms; return (0, utils_1.as)(this); } /** * Set the Time Window when hitting an endpoint (/ endpoints) in ms * * When the User hits the endpoint(s) more than `<maxHits>` in `<timeWindow>ms`, the penalty will be applied to * the user and the user wont be able to access the endpoint for `<penalty>ms`, after that the users limits are reset * for the endpoint(s). If the User hits the endpoint(s) less than `<maxHits>` in `<timeWindow>ms`, the penalty wont be applied * and if `<timeWindow>ms` has passed, the limits will reset without any penalty applying. * * You can always prevent a request / message from counting towards the ratelimit by calling `<HTTPRequest | WSMessage>.skipRateLimit()` * @default time(10).s() * @since 8.6.0 */ window(ms) { this.data.timeWindow = ms; return (0, utils_1.as)(this); } /** * Set the Max Hits in a Time Window * * When the User hits the endpoint(s) more than `<maxHits>` in `<timeWindow>ms`, the penalty will be applied to * the user and the user wont be able to access the endpoint for `<penalty>ms`, after that the users limits are reset * for the endpoint(s). If the User hits the endpoint(s) less than `<maxHits>` in `<timeWindow>ms`, the penalty wont be applied * and if `<timeWindow>ms` has passed, the limits will reset without any penalty applying. Max Hits being `Infinity` means that the * ratelimit is disabled (if wanting to remove router limit). * * You can always prevent a request / message from counting towards the ratelimit by calling `<HTTPRequest | WSMessage>.skipRateLimit()` * @default Infinity * @since 8.6.0 */ hits(amount) { this.data.maxHits = amount; return (0, utils_1.as)(this); } } exports.default = RateLimit;