longcelot-sheet-db
Version:
Google Sheets-backed staging database adapter for Node.js with schema-first design
33 lines • 1.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRateLimitError = isRateLimitError;
exports.withBackoff = withBackoff;
const chalk_1 = __importDefault(require("chalk"));
function isRateLimitError(err) {
if (err instanceof Error) {
const msg = err.message.toLowerCase();
return msg.includes('429') || msg.includes('quota') || msg.includes('rate limit');
}
return false;
}
async function withBackoff(fn, maxRetries = 5) {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await fn();
}
catch (err) {
if (isRateLimitError(err) && attempt < maxRetries) {
const delayMs = Math.min(Math.pow(2, attempt) * 1000, 32000);
console.log(chalk_1.default.yellow(` ⏳ Rate limited — retrying in ${delayMs / 1000}s (attempt ${attempt + 1}/${maxRetries})...`));
await new Promise((r) => setTimeout(r, delayMs));
continue;
}
throw err;
}
}
throw new Error('Max retries exceeded');
}
//# sourceMappingURL=backoff.js.map