molstar
Version:
A comprehensive macromolecular library.
39 lines • 1.8 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __awaiter, __generator } from "tslib";
import fetch from 'node-fetch';
import { retryIf } from '../../../mol-util/retry-if';
var RETRIABLE_NETWORK_ERRORS = [
'ECONNRESET', 'ENOTFOUND', 'ESOCKETTIMEDOUT', 'ETIMEDOUT',
'ECONNREFUSED', 'EHOSTUNREACH', 'EPIPE', 'EAI_AGAIN'
];
function isRetriableNetworkError(error) {
return error && RETRIABLE_NETWORK_ERRORS.includes(error.code);
}
export function fetchRetry(url, timeout, retryCount, onRetry) {
return __awaiter(this, void 0, void 0, function () {
var controller, id, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
controller = new AbortController();
id = setTimeout(function () { return controller.abort(); }, timeout);
return [4 /*yield*/, retryIf(function () { return fetch(url, { signal: controller.signal }); }, {
retryThenIf: function (r) { return r.status === 408 /** timeout */ || r.status === 429 /** too many requests */ || (r.status >= 500 && r.status < 600); },
// TODO test retryCatchIf
retryCatchIf: function (e) { return isRetriableNetworkError(e); },
onRetry: onRetry,
retryCount: retryCount
})];
case 1:
result = _a.sent();
clearTimeout(id);
return [2 /*return*/, result];
}
});
});
}
//# sourceMappingURL=fetch-retry.js.map