Parse and format HTTP Cache-Control headers with structured diagnostics.
http-cache-control-kit is a clean-room TypeScript utility for small browser, worker, CLI and server tooling. It has no runtime dependencies and does not use Node-only APIs.
Try the browser demo: packages.wasta-wocket.fr/http-cache-control-kit.
- TypeScript types are generated from the source.
- ESM-only package with no runtime dependencies.
- Marked as side-effect free for bundlers.
- CI runs
npm ci,typecheck,build, andtest. - Tested on Node.js 20 and 22 with GitHub Actions.
- Browser-friendly implementation with no Node-only APIs.
npm install http-cache-control-kitimport{formatCacheControl,getCacheControlDeltaSeconds,parseCacheControl}from"http-cache-control-kit";constparsed=parseCacheControl("public, max-age=3600, stale-while-revalidate=30");if(parsed.ok){console.log(parsed.values.public);// trueconsole.log(getCacheControlDeltaSeconds(parsed,"max-age"));// 3600}constheader=formatCacheControl({public: true,"max-age": "3600","stale-while-revalidate": "30"});Use this package when you need to inspect a Cache-Control header and explain what is wrong with it. The parser returns directives plus stable diagnostic codes for duplicate directives, missing delta-seconds values, invalid quoted strings and unknown directives.
It intentionally does not evaluate full HTTP cache semantics across Expires, Age, ETag, request method or request-vs-response context. For that broader job, use a full cache semantics library.
Returns a result object. Invalid input and invalid directives are reported in diagnostics instead of throwing.
constresult=parseCacheControl('private="Authorization, Cookie", max-age=60');result.values.private;// "Authorization, Cookie"result.values["max-age"];// "60"The parser accepts both request and response directives. For example, max-stale is valid without a value in request headers, but if a value is provided it must be valid delta-seconds.
parseCacheControl("max-stale").values["max-stale"];// truegetCacheControlDeltaSeconds(parseCacheControl("max-stale=120"),"max-stale");// 120Formats a directive map or parsed directive array back into a header string.
formatCacheControl({"max-age": "60",private: "Authorization, Cookie"},{sort: true});// 'max-age=60, private="Authorization, Cookie"'Case-insensitive presence check against a parse result.
Returns a numeric delta-seconds value when the directive exists and contains a non-negative integer.
Diagnostic codes are stable strings for tests, UI hints and logs:
empty-inputexpected-stringempty-directiveinvalid-directive-namemissing-valueduplicate-directiveinvalid-quoted-stringinvalid-delta-secondsunknown-directive
| Option | Default | Description |
|---|---|---|
allowUnknown | false | Keep unknown directives but report them as diagnostics unless enabled. |
allowDuplicates | false | Keep repeated directives instead of reporting and ignoring later values. |
sort | false | Sort formatted directives by name. |
quoteValues | "auto" | Quote formatted values automatically, always or never. |
The core only uses strings, arrays, objects and regular expressions. It performs no I/O and has no dependency on fs, path, Buffer, process or network APIs.
MPL-2.0