UNPKG

@remix-run/headers

Version:

A toolkit for working with HTTP headers in JavaScript

167 lines (166 loc) 6.02 kB
import {} from "./header-value.js"; import { parseParams } from "./param-values.js"; /** * The value of a `Cache-Control` HTTP header. * * [MDN `Cache-Control` Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) * * [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7234#section-5.2) */ export class CacheControl { maxAge; maxStale; minFresh; sMaxage; noCache; noStore; noTransform; onlyIfCached; mustRevalidate; proxyRevalidate; mustUnderstand; private; public; immutable; staleWhileRevalidate; staleIfError; /** * @param init A string or object to initialize the header */ constructor(init) { if (init) { if (typeof init === 'string') { let params = parseParams(init, ','); if (params.length > 0) { for (let [name, value] of params) { switch (name) { case 'max-age': this.maxAge = Number(value); break; case 'max-stale': this.maxStale = Number(value); break; case 'min-fresh': this.minFresh = Number(value); break; case 's-maxage': this.sMaxage = Number(value); break; case 'no-cache': this.noCache = true; break; case 'no-store': this.noStore = true; break; case 'no-transform': this.noTransform = true; break; case 'only-if-cached': this.onlyIfCached = true; break; case 'must-revalidate': this.mustRevalidate = true; break; case 'proxy-revalidate': this.proxyRevalidate = true; break; case 'must-understand': this.mustUnderstand = true; break; case 'private': this.private = true; break; case 'public': this.public = true; break; case 'immutable': this.immutable = true; break; case 'stale-while-revalidate': this.staleWhileRevalidate = Number(value); break; case 'stale-if-error': this.staleIfError = Number(value); break; } } } } else { this.maxAge = init.maxAge; this.maxStale = init.maxStale; this.minFresh = init.minFresh; this.sMaxage = init.sMaxage; this.noCache = init.noCache; this.noStore = init.noStore; this.noTransform = init.noTransform; this.onlyIfCached = init.onlyIfCached; this.mustRevalidate = init.mustRevalidate; this.proxyRevalidate = init.proxyRevalidate; this.mustUnderstand = init.mustUnderstand; this.private = init.private; this.public = init.public; this.immutable = init.immutable; this.staleWhileRevalidate = init.staleWhileRevalidate; this.staleIfError = init.staleIfError; } } } /** * Returns the string representation of the header value. * * @return The header value as a string */ toString() { let parts = []; if (this.public) { parts.push('public'); } if (this.private) { parts.push('private'); } if (typeof this.maxAge === 'number') { parts.push(`max-age=${this.maxAge}`); } if (typeof this.sMaxage === 'number') { parts.push(`s-maxage=${this.sMaxage}`); } if (this.noCache) { parts.push('no-cache'); } if (this.noStore) { parts.push('no-store'); } if (this.noTransform) { parts.push('no-transform'); } if (this.onlyIfCached) { parts.push('only-if-cached'); } if (this.mustRevalidate) { parts.push('must-revalidate'); } if (this.proxyRevalidate) { parts.push('proxy-revalidate'); } if (this.mustUnderstand) { parts.push('must-understand'); } if (this.immutable) { parts.push('immutable'); } if (typeof this.staleWhileRevalidate === 'number') { parts.push(`stale-while-revalidate=${this.staleWhileRevalidate}`); } if (typeof this.staleIfError === 'number') { parts.push(`stale-if-error=${this.staleIfError}`); } if (typeof this.maxStale === 'number') { parts.push(`max-stale=${this.maxStale}`); } if (typeof this.minFresh === 'number') { parts.push(`min-fresh=${this.minFresh}`); } return parts.join(', '); } }