sync-worktrees
Version:
Automatically synchronize Git worktrees with remote branches - perfect for multi-branch development workflows
78 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retry = retry;
const lfs_error_1 = require("./lfs-error");
const DEFAULT_OPTIONS = {
maxAttempts: "unlimited",
maxLfsRetries: 2,
initialDelayMs: 1000,
maxDelayMs: 600000, // 10 minutes
backoffMultiplier: 2,
shouldRetry: (error, context) => {
const err = error;
// Check for LFS errors
if ((0, lfs_error_1.isLfsErrorFromError)(error)) {
if (context) {
context.isLfsError = true;
}
return true;
}
if (err.code === "ENOTFOUND" || err.code === "ECONNREFUSED" || err.code === "ETIMEDOUT") {
return true;
}
if (err.code === "EBUSY" || err.code === "ENOENT" || err.code === "EACCES") {
return true;
}
if (err.message?.includes("Could not read from remote repository")) {
return true;
}
if (err.message?.includes("fatal: unable to access")) {
return true;
}
return false;
},
onRetry: () => { },
lfsRetryHandler: (_context) => { },
};
async function retry(fn, options = {}) {
const opts = { ...DEFAULT_OPTIONS, ...options };
let attempt = 1;
let lfsAttempt = 0;
const lfsContext = { isLfsError: false };
while (true) {
try {
return await fn();
}
catch (error) {
// Reset LFS error flag for each attempt
lfsContext.isLfsError = false;
// Check if we should retry
if (!opts.shouldRetry(error, lfsContext)) {
throw error;
}
// Track LFS attempts separately
if (lfsContext.isLfsError) {
lfsAttempt++;
// Check if we've exceeded LFS retry limit
if (lfsAttempt > opts.maxLfsRetries) {
const err = error;
throw new Error(`LFS error retry limit exceeded (${opts.maxLfsRetries} attempts). ` +
`Consider using --skip-lfs option to bypass LFS downloads.`, { cause: err });
}
}
const isLastAttempt = opts.maxAttempts !== "unlimited" && attempt >= opts.maxAttempts;
if (isLastAttempt) {
throw error;
}
// Handle LFS errors specifically
if (lfsContext.isLfsError && opts.lfsRetryHandler) {
opts.lfsRetryHandler(lfsContext);
}
const delay = Math.min(opts.initialDelayMs * Math.pow(opts.backoffMultiplier, attempt - 1), opts.maxDelayMs);
opts.onRetry(error, attempt, lfsContext);
await new Promise((resolve) => setTimeout(resolve, delay));
attempt++;
}
}
}
//# sourceMappingURL=retry.js.map