UNPKG

@canmertinyo/rate-limiter-mongo

Version:

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

84 lines (83 loc) 3.49 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MongoStorage = void 0; const mongoose_1 = require("mongoose"); const rate_limit_schema_1 = require("./schemas/rate-limit.schema"); class MongoStorage { constructor(uri = "mongodb://localhost:27017/rate-limits", options) { this.uri = uri; this.options = options; this.model = rate_limit_schema_1.RateLimitModel; } initialize() { return __awaiter(this, void 0, void 0, function* () { yield mongoose_1.default.connect(this.uri, this.options); return this; }); } getRateLimitRecord(key) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.model.findOne({ key }); if (!record) return; var _a = record.toObject(), { _id } = _a, object = __rest(_a, ["_id"]); return record ? object : undefined; }); } createRateLimitRecord(record) { return __awaiter(this, void 0, void 0, function* () { const newRecord = yield this.model.create(record); newRecord.save(); var _a = newRecord.toObject(), { _id } = _a, object = __rest(_a, ["_id"]); return object; }); } updateRateLimitRecord(key, timestamp, count) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.model.findOneAndUpdate({ key }, { timestamp, count, }, { new: true, }); if (!record) { throw new Error("No record found."); } var _a = record.toObject(), { _id } = _a, object = __rest(_a, ["_id"]); return object; }); } increment(key) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.model.findOneAndUpdate({ key }, { $inc: { count: 1 } }, { new: true, }); if (!record) { throw new Error("No record found to increment."); } var _a = record.toObject(), { _id } = _a, object = __rest(_a, ["_id"]); return object; }); } } exports.MongoStorage = MongoStorage;