UNPKG

@typespec/ts-http-runtime

Version:

Isomorphic client library for making HTTP requests in node.js and browser.

69 lines (68 loc) 2.61 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var throttlingRetryStrategy_exports = {}; __export(throttlingRetryStrategy_exports, { isThrottlingRetryResponse: () => isThrottlingRetryResponse, throttlingRetryStrategy: () => throttlingRetryStrategy }); module.exports = __toCommonJS(throttlingRetryStrategy_exports); var import_helpers = require("../util/helpers.js"); const RetryAfterHeader = "Retry-After"; const AllRetryAfterHeaders = ["retry-after-ms", "x-ms-retry-after-ms", RetryAfterHeader]; function getRetryAfterInMs(response) { if (!(response && [429, 503].includes(response.status))) return void 0; try { for (const header of AllRetryAfterHeaders) { const retryAfterValue = (0, import_helpers.parseHeaderValueAsNumber)(response, header); if (retryAfterValue === 0 || retryAfterValue) { const multiplyingFactor = header === RetryAfterHeader ? 1e3 : 1; return retryAfterValue * multiplyingFactor; } } const retryAfterHeader = response.headers.get(RetryAfterHeader); if (!retryAfterHeader) return; const date = Date.parse(retryAfterHeader); const diff = date - Date.now(); return Number.isFinite(diff) ? Math.max(0, diff) : void 0; } catch { return void 0; } } function isThrottlingRetryResponse(response) { return Number.isFinite(getRetryAfterInMs(response)); } function throttlingRetryStrategy() { return { name: "throttlingRetryStrategy", retry({ response }) { const retryAfterInMs = getRetryAfterInMs(response); if (!Number.isFinite(retryAfterInMs)) { return { skipStrategy: true }; } return { retryAfterInMs }; } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { isThrottlingRetryResponse, throttlingRetryStrategy }); //# sourceMappingURL=throttlingRetryStrategy.js.map