UNPKG

ab-helpers

Version:

A collection of helper functions for various tasks

28 lines (22 loc) 641 B
const Redis = require("ioredis"); const { RateLimiterRedis } = require("rate-limiter-flexible"); async function exec( fingerprint, // config { host, port, username, password }, // options { maxReq = 3, duration = 1200, keyPrefix = "rl" } ) { const redisConfig = { host, port }; if (username) redisConfig.username = username; if (password) redisConfig.password = password; const redisClient = new Redis(redisConfig); const rateLimiter = new RateLimiterRedis({ storeClient: redisClient, points: maxReq, duration, keyPrefix, }); return rateLimiter.consume(fingerprint); } module.exports = { exec };