UNPKG

@yveskaufmann/koa2-ratelimit

Version:

IP rate-limiting middleware for Koajs 2. Use to limit repeated requests to APIs and/or endpoints such as password reset.

116 lines 3.61 kB
"use strict"; /** * RedisStore * * RedisStore for koa2-ratelimit * * @author Ashok Vishwakarma <akvlko@gmail.com> * */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisStore = void 0; const Time_1 = require("../Time"); const Store_1 = require("./Store"); /** * redis * * node-redis module */ const redis_1 = __importDefault(require("redis")); /** * RedisStore * * Class RedisStore */ class RedisStore extends Store_1.Store { /** * constructor * @param {*} config * * config is redis config */ constructor(config) { super(); this.client = redis_1.default.createClient(config); this.client.on("error", (err) => console.log("Redis Client Error", err)); this.client.connect(); } /** * _hit * @access private * @param {*} key * @param {*} options * @param {*} weight */ _hit(key, options, weight) { return __awaiter(this, void 0, void 0, function* () { let [counter, dateEnd] = yield this.client.multi().get(key).ttl(key).exec(); const interval = Time_1.Time.toMs(options.interval); if (counter === null) { counter = weight; dateEnd = Date.now() + interval; const seconds = Math.ceil(interval / 1000); yield this.client.setEx(key, seconds.toString(), counter.toString()); } else if (dateEnd === -2 || dateEnd === -1) { counter = counter + weight; dateEnd = Date.now() + interval; const seconds = Math.ceil(interval / 1000); yield this.client.setEx(key, seconds.toString(), counter.toString()); } else { counter = yield this.client.incrBy(key, weight); } return { counter, dateEnd, }; }); } /** * incr * * Override incr method from Store class * @param {*} key * @param {*} options * @param {*} weight */ incr(key, options, weight) { return __awaiter(this, void 0, void 0, function* () { return yield this._hit(key, options, weight); }); } /** * decrement * * Override decrement method from Store class * @param {*} key * @param {*} options * @param {*} weight */ decrement(key, options, weight) { return __awaiter(this, void 0, void 0, function* () { yield this.client.decrBy(key, weight); }); } /** * saveAbuse * * Override saveAbuse method from Store class */ saveAbuse() { } } exports.RedisStore = RedisStore; //# sourceMappingURL=RedisStore.js.map