UNPKG

semantic-network

Version:

A utility library for manipulating a list of links that form a semantic interface to a network of resources.

96 lines 4.86 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestHeaders = void 0; var trackedRepresentationUtil_1 = require("../utils/trackedRepresentationUtil"); var status_1 = require("./status"); var checkHeaders_1 = require("./checkHeaders"); var dateToGMTHeader_1 = require("../utils/dateToGMTHeader"); /** * The goal is to leave all heavy lifting to the browser (ie implement caching rules). The key issue * here is whether to return the in-memory resource or push through to the browser request (ie xhr). */ var RequestHeaders = exports.RequestHeaders = /** @class */ (function () { function RequestHeaders() { } /** * Add eTag detection for when feed items had the eTag included. Provides the request with a no-cache directive for http 1.0/1.1/2. * * Note: this directive used when a new resource based on eTag is detected the client requires the origin * server to provide a new resource. This is most likely to occur because of an out-of-band mechanism (ie another * process outside the direct context of the resource) * * In effect, this strategy mimics the "disable cache" mechanism if performing a manual intervention */ RequestHeaders.noCacheOnStaleEtagStatusHeaderStrategy = function (resource, options) { var _a; var useStaleEtagStrategy = (_a = __assign({}, options).useStaleEtagStrategy, _a === void 0 ? false : _a); if (useStaleEtagStrategy) { var trackedState = trackedRepresentationUtil_1.TrackedRepresentationUtil.getState(resource); if (trackedState.status === status_1.Status.staleFromETag) { return RequestHeaders.noCacheHeader; } } return RequestHeaders.emptyHeaders; }; RequestHeaders.ifNoneMatchesOnStaleEtagStatusHeaderStrategy = function (resource, options) { var _a; var useStaleEtagStrategy = (_a = __assign({}, options).useStaleEtagStrategy, _a === void 0 ? false : _a); if (useStaleEtagStrategy) { var trackedState = trackedRepresentationUtil_1.TrackedRepresentationUtil.getState(resource); if (trackedState.status === status_1.Status.staleFromETag && trackedRepresentationUtil_1.TrackedRepresentationUtil.hasFeedETag(resource)) { return RequestHeaders.conditionalGetHeaders(resource); } } return RequestHeaders.emptyHeaders; }; /** * Detect that the resource has a stale 'expires' header */ RequestHeaders.noCacheOnStaleExpiresHeaderStrategy = function (resource) { var _a; var headers = (_a = trackedRepresentationUtil_1.TrackedRepresentationUtil.getState(resource).headers, _a === void 0 ? undefined : _a); if (headers && checkHeaders_1.CheckHeaders.checkExpiresBeforeDateHeaderStrategy(headers)) { return RequestHeaders.noCacheHeader; } return RequestHeaders.emptyHeaders; }; /** * WARNING: if this is a CORS request that both pragma and cache-control must be registered as exposed * Access-Control-Allow-Headers (otherwise failed CORS request in browser) * * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Headers */ RequestHeaders.noCacheHeader = { // http 1.0 - @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching 'pragma': 'no-cache', // http 1.1/2 'cache-control': 'no-cache', }; RequestHeaders.emptyHeaders = {}; RequestHeaders.defaultStrategies = [ RequestHeaders.noCacheOnStaleEtagStatusHeaderStrategy, RequestHeaders.noCacheOnStaleExpiresHeaderStrategy, ]; RequestHeaders.conditionalGetHeaders = function (documentResource) { return ({ 'if-none-match': trackedRepresentationUtil_1.TrackedRepresentationUtil.getFeedETag(documentResource), 'if-modified-since': trackedRepresentationUtil_1.TrackedRepresentationUtil.getFeedLastModified(documentResource) || trackedRepresentationUtil_1.TrackedRepresentationUtil.getState(documentResource).headers['last-modified'] || trackedRepresentationUtil_1.TrackedRepresentationUtil.getState(documentResource).headers['date'] || (0, dateToGMTHeader_1.dateToGMTHeader)(new Date().toUTCString()), }); }; return RequestHeaders; }()); //# sourceMappingURL=requestHeaders.js.map