parseCacheControl

import { parseCacheControl } from "jsr:@ende/http/unstable-cache-control";

function · experimental

function parseCacheControl(value: string | null): CacheControl

Parses a Cache-Control header value into a typed object. Returns an empty object for null or empty string. Directive names are case-insensitive.

Parsing is lenient and never throws: unknown directives are ignored per RFC 9111 §5.2.3, and known directives with a malformed or missing value (e.g. max-age=abc or bare max-age) are ignored as well. The one exception is bare max-stale, which is valid per RFC 9111 §5.2.1.2 and parses as true. When a directive appears more than once, only the first occurrence counts, even if it is malformed (max-age=abc, max-age=100 yields no maxAge). Callers implementing a cache may want to treat a response whose freshness directives do not survive parsing as stale, as encouraged by RFC 9111 §4.2.1.

value: string | null

The header value (e.g. headers.get("cache-control")).

Returns

Parsed directives as a union of request/response types.

Example: Usage

import { parseCacheControl } from "@ende/http/unstable-cache-control";
import { assertEquals } from "@ende/assert";

const cc = parseCacheControl("max-age=3600, no-store");
assertEquals(cc.maxAge, 3600);
assertEquals(cc.noStore, true);

Source