iamguard
Version:
A command-line tool for scanning and analyzing AWS IAM configurations for security risks
55 lines (44 loc) • 1.59 kB
JavaScript
import { setTimeout } from 'timers/promises';
export class RateLimiter {
constructor(maxRequests = 10, windowMs = 1000) {
this.maxRequests = maxRequests;
this.windowMs = windowMs;
this.requests = [];
}
async acquire() {
const now = Date.now();
// Remove old requests outside the window
this.requests = this.requests.filter(time => now - time < this.windowMs);
if (this.requests.length >= this.maxRequests) {
const oldestRequest = Math.min(...this.requests);
const waitTime = this.windowMs - (now - oldestRequest);
await setTimeout(waitTime);
return this.acquire();
}
this.requests.push(now);
}
}
export class RetryHandler {
constructor(maxRetries = 3, baseDelay = 1000) {
this.maxRetries = maxRetries;
this.baseDelay = baseDelay;
}
async execute(fn, context = '') {
let lastError;
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
if (attempt === this.maxRetries) {
throw new Error(`Failed after ${this.maxRetries + 1} attempts in ${context}: ${error.message}`);
}
// Exponential backoff with jitter
const delay = this.baseDelay * Math.pow(2, attempt) + Math.random() * 1000;
console.warn(`Attempt ${attempt + 1} failed for ${context}, retrying in ${Math.round(delay)}ms...`);
await setTimeout(delay);
}
}
throw lastError;
}
}