@makerx/node-ipfs
Version:
A NodeJS package that makes interacting with IPFS easier
33 lines • 1.43 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchWithRetry = fetchWithRetry;
const async_retry_1 = __importDefault(require("async-retry"));
async function retryer(url, fetchOptions, retries = 3) {
const retryCodes = [403, 408, 500, 502, 503, 504, 522, 524];
return await (0, async_retry_1.default)(async (bail) => {
// if anything throws, it will retry if within the retry policy
const response = await fetch(url, fetchOptions);
if (response.ok)
return response;
if (retryCodes.includes(response.status)) {
throw new Error(`HTTP request: ${url} failed with HTTP response: ${response.statusText}.`);
}
else {
bail(new Error(`HTTP request: ${url} failed with HTTP response: ${response.statusText}, will not retry`));
throw new Error('Should never reach here');
}
}, {
retries: retries,
onRetry: (e, num) => {
// eslint-disable-next-line no-console
console.debug(`HTTP request failed. Retrying for #${num} time: ${e.message}`);
},
});
}
async function fetchWithRetry(url, fetchOptions, retries = 3) {
return await retryer(url, fetchOptions, retries);
}
//# sourceMappingURL=http.js.map
;