UNPKG

@canmertinyo/rate-limiter-core

Version:

A simple rate-limiting middleware for Express.js with support for in-memory, Redis, and MongoDB storage

48 lines (47 loc) 1.97 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InMemoryStorage = void 0; class InMemoryStorage { initialize() { return __awaiter(this, void 0, void 0, function* () { this.store = []; return this; }); } getRateLimitRecord(key) { return __awaiter(this, void 0, void 0, function* () { return this.store.find((x) => x.key === key); }); } createRateLimitRecord(record) { return __awaiter(this, void 0, void 0, function* () { let index = this.store.push(record); return this.store[index - 1]; }); } updateRateLimitRecord(key, timestamp, count) { return __awaiter(this, void 0, void 0, function* () { let index = this.store.findIndex((x) => x.key === key); this.store[index].count = count; this.store[index].timestamp = timestamp; return this.store[index]; }); } increment(key) { return __awaiter(this, void 0, void 0, function* () { let index = this.store.findIndex((x) => x.key === key); this.store[index].count += 1; return this.store[index]; }); } } exports.InMemoryStorage = InMemoryStorage;