UNPKG

@launchdarkly/js-server-sdk-common

Version:
53 lines 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const js_sdk_common_1 = require("@launchdarkly/js-sdk-common"); /** * @internal */ class Requestor { constructor(config, _requests, baseHeaders) { this._requests = _requests; this._eTagCache = {}; this._headers = Object.assign({}, baseHeaders); this._uri = (0, js_sdk_common_1.getPollingUri)(config.serviceEndpoints, '/sdk/latest-all', []); } /** * Perform a request and utilize the ETag cache. The ETags are cached in the * requestor instance. */ async _requestWithETagCache(requestUrl, options) { const cacheEntry = this._eTagCache[requestUrl]; const cachedETag = cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.etag; const updatedOptions = cachedETag ? Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, options.headers), { 'if-none-match': cachedETag }) }) : options; const res = await this._requests.fetch(requestUrl, updatedOptions); if (res.status === 304 && cacheEntry) { return { res, body: cacheEntry.body }; } const etag = res.headers.get('etag'); const body = await res.text(); if (etag) { this._eTagCache[requestUrl] = { etag, body }; } return { res, body }; } async requestAllData(cb) { const options = { method: 'GET', headers: this._headers, }; try { const { res, body } = await this._requestWithETagCache(this._uri, options); if (res.status !== 200 && res.status !== 304) { const err = new js_sdk_common_1.LDPollingError(js_sdk_common_1.DataSourceErrorKind.ErrorResponse, `Unexpected status code: ${res.status}`, res.status); return cb(err, undefined); } return cb(undefined, res.status === 304 ? null : body); } catch (err) { return cb(err, undefined); } } } exports.default = Requestor; //# sourceMappingURL=Requestor.js.map