UNPKG

@hirosystems/api-toolkit

Version:
63 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CACHE_CONTROL_MUST_REVALIDATE = void 0; exports.setResponseNonCacheable = setResponseNonCacheable; exports.parseIfNoneMatchHeader = parseIfNoneMatchHeader; const logger_1 = require("../logger"); /** * A `Cache-Control` header used for re-validation based caching. * * `public` == allow proxies/CDNs to cache as opposed to only local browsers. * * `no-cache` == clients can cache a resource but should revalidate each time before using it. * * `must-revalidate` == somewhat redundant directive to assert that cache must be revalidated, required by some CDNs */ exports.CACHE_CONTROL_MUST_REVALIDATE = 'public, no-cache, must-revalidate'; async function setResponseNonCacheable(reply) { await reply.removeHeader('Cache-Control'); await reply.removeHeader('ETag'); } /** * Parses the etag values from a raw `If-None-Match` request header value. * The wrapping double quotes (if any) and validation prefix (if any) are stripped. * The parsing is permissive to account for commonly non-spec-compliant clients, proxies, CDNs, etc. * E.g. the value: * ```js * `"a", W/"b", c,d, "e", "f"` * ``` * Would be parsed and returned as: * ```js * ['a', 'b', 'c', 'd', 'e', 'f'] * ``` * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match#syntax * ``` * If-None-Match: "etag_value" * If-None-Match: "etag_value", "etag_value", ... * If-None-Match: * * ``` * @param ifNoneMatchHeaderValue - raw header value * @returns an array of etag values */ function parseIfNoneMatchHeader(ifNoneMatchHeaderValue) { if (!ifNoneMatchHeaderValue) { return undefined; } // Strip wrapping double quotes like `"hello"` and the ETag validation-prefix like `W/"hello"`. // The API returns compliant, strong-validation ETags (double quoted ASCII), but can't control what // clients, proxies, CDNs, etc may provide. const normalized = /^(?:"|W\/")?(.*?)"?$/gi.exec(ifNoneMatchHeaderValue.trim())?.[1]; if (!normalized) { // This should never happen unless handling a buggy request with something like `If-None-Match: ""`, // or if there's a flaw in the above code. Log warning for now. logger_1.logger.warn(`Normalized If-None-Match header is falsy: ${ifNoneMatchHeaderValue}`); return undefined; } else if (normalized.includes(',')) { // Multiple etag values provided, likely irrelevant extra values added by a proxy/CDN. // Split on comma, also stripping quotes, weak-validation prefixes, and extra whitespace. return normalized.split(/(?:W\/"|")?(?:\s*),(?:\s*)(?:W\/"|")?/gi); } else { // Single value provided (the typical case) return [normalized]; } } //# sourceMappingURL=cache.js.map