UNPKG

@devcycle/nodejs-server-sdk

Version:

The DevCycle NodeJS Server SDK used for feature management.

54 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidDate = void 0; exports.getEnvironmentConfig = getEnvironmentConfig; const server_request_1 = require("../server-request/src"); const isValidDate = (date) => date instanceof Date && !isNaN(date.getTime()); exports.isValidDate = isValidDate; async function getEnvironmentConfig({ logger, url, requestTimeout, currentEtag, currentLastModified, sseLastModified, }) { const headers = {}; let retries = 1; let retryOn; const sseLastModifiedDate = sseLastModified ? new Date(sseLastModified) : null; // Retry fetching config if the Last-Modified header is older than // the requiredLastModified from the SSE message if (sseLastModified && (0, exports.isValidDate)(sseLastModifiedDate)) { retries = 3; retryOn = (attempt, error, response) => { if (attempt >= retries) { return false; } else if (response && (response === null || response === void 0 ? void 0 : response.status) === 200) { const lastModifiedHeader = response.headers.get('Last-Modified'); const lastModifiedHeaderDate = lastModifiedHeader ? new Date(lastModifiedHeader) : null; if ((0, exports.isValidDate)(lastModifiedHeaderDate) && lastModifiedHeaderDate < sseLastModifiedDate) { logger.debug(`Retry fetching config, last modified is old: ${lastModifiedHeader}` + `, sse last modified: ${sseLastModified}`); return true; } return false; } else if (response && (response === null || response === void 0 ? void 0 : response.status) < 500) { return false; } return true; }; } if (currentEtag) { headers['If-None-Match'] = currentEtag; } if (currentLastModified) { headers['If-Modified-Since'] = currentLastModified; } return await (0, server_request_1.getWithTimeout)(url, { headers: headers, retries, retryOn, }, requestTimeout); } //# sourceMappingURL=request.js.map