@botport/core
Version:
Unified framework for Discord bot products, published by BotPort. Combines docky and framework functionality.
44 lines (39 loc) • 1.39 kB
JavaScript
// src/lib/utils/protect.js
import requestTracker from './requestTracker.js';
import userLimiter from './userLimiter.js';
/**
* Protect a function with both request tracking and user rate limiting
* @param {string} userId - User ID
* @param {string} action - Action identifier
* @param {Function} fn - Function to execute
* @param {Object} [options={}] - Options
* @returns {Promise<any>}
*/
export async function protect(userId, action, fn, options = {}) {
const {
checkDuplicate = true,
checkRateLimit = true,
customLimit = null,
duration = 5000,
data = ''
} = options;
// Check 1: Duplicate request
if (checkDuplicate && requestTracker.isProcessing(userId, action, data)) {
const error = new Error('Request already being processed');
error.code = 'REQUEST_DUPLICATE';
throw error;
}
// Check 2: Rate limit
if (checkRateLimit) {
const rateCheck = userLimiter.check(userId, action, customLimit);
if (!rateCheck.allowed) {
const error = new Error(`Rate limited. Try again in ${rateCheck.retryAfter} seconds.`);
error.code = 'RATE_LIMITED';
error.retryAfter = rateCheck.retryAfter;
throw error;
}
}
// Execute with tracking
return await requestTracker.wrap(userId, action, fn, { duration, data, throwOnDuplicate: false });
}
export default protect;