auth0
Version:
Auth0 Node.js SDK for the Management API v2.
93 lines (92 loc) • 3.97 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.retry = retry;
const errors_js_1 = require("./errors.js");
const MAX_REQUEST_RETRY_JITTER = 250;
const MAX_REQUEST_RETRY_DELAY = 10000;
const DEFAULT_NUMBER_RETRIES = 3;
const MAX_NUMBER_RETRIES = 10;
const BASE_DELAY = 500;
/**
* @private
* Function that returns a random int between a configurable min and max.
* @param min The min value
* @param max The max value
* @returns {number} The random generated value
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}
/**
* @private
* Function that returns a promise which resolves after a configurable amount of milliseconds
* @param delay value to be used for the delay
* @returns {Promise} A delayed promise
*/
function pause(delay) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve) => setTimeout(resolve, delay));
});
}
// Transient network errors that are safe to retry — failures that can self-heal
// without any config change (socket reset, broken pipe, aborted connection).
// Deliberately excludes ENOTFOUND, ECONNREFUSED, cert errors — those won't self-heal.
const RETRYABLE_ERROR_CODES = new Set(["ECONNRESET", "EPIPE", "ECONNABORTED"]);
function isRetryableNetworkError(e) {
var _a, _b, _c;
if (typeof e !== "object" || e === null)
return false;
// Check both e.code (old request-lib / nock shape) and e.cause.code
// (native fetch / undici shape: TypeError: fetch failed { cause: { code } })
const err = e;
const code = (_c = (_a = err.code) !== null && _a !== void 0 ? _a : (_b = err.cause) === null || _b === void 0 ? void 0 : _b.code) !== null && _c !== void 0 ? _c : "";
return RETRYABLE_ERROR_CODES.has(code);
}
function calculateWait(nrOfTries) {
let wait = BASE_DELAY * Math.pow(2, nrOfTries - 1);
wait = getRandomInt(wait + 1, wait + MAX_REQUEST_RETRY_JITTER);
return Math.min(wait, MAX_REQUEST_RETRY_DELAY);
}
/**
* @private
* Function that retries the provided action callback for a configurable amount of time, defaults to 3.
*/
function retry(action, { maxRetries, retryWhen }) {
const nrOfTriesToAttempt = Math.min(MAX_NUMBER_RETRIES, maxRetries !== null && maxRetries !== void 0 ? maxRetries : DEFAULT_NUMBER_RETRIES);
let nrOfTries = 0;
const retryAndWait = () => __awaiter(this, void 0, void 0, function* () {
let result;
try {
result = yield action();
}
catch (e) {
if (e instanceof errors_js_1.TimeoutError) {
throw e;
}
if (isRetryableNetworkError(e) && nrOfTries < nrOfTriesToAttempt) {
nrOfTries++;
yield pause(calculateWait(nrOfTries));
return retryAndWait();
}
throw e;
}
if ((retryWhen || [429]).includes(result.status) && nrOfTries < nrOfTriesToAttempt) {
nrOfTries++;
yield pause(calculateWait(nrOfTries));
result = yield retryAndWait();
}
return result;
});
return retryAndWait();
}