Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
cache-controlled
Advanced tools
Simplifies working with Cache-Control
headers
Parse a cache-control
header. Parsing is case insensitive and all spaces are ignored.
import { parse } from "cache-controlled";
parse( "max-age=10" );
// { "max-age": 10 }
parse( "public, max-age=10, stale-while-revalidate=600" );
// { "max-age": 10, public: true, "stale-while-revalidate": 600 }
// For convenience, also handles `undefined` and `string[]`
parse( undefined ); // {}
parse( [ "public", "max-age=10" ] ); // { "max-age": 10, public: true }
Note:
parse
also adds atimestamp
field used bycheck
.
check
will tell you if you can still use your cached content:
ok
: Cache is ok. No need to do anythingokStale
: Cache is ok, but stale. Fetch updateokError
: Cache is ok IFF you had/have a network errorconst status = check( parse( "max-age=10" ) ).ok; // true
// If the `immutable` directive is present, it will always return `true`
const status = check( parse( "immutable" ) ).ok; // true
const status = check( parse( "stale-while-revalidate=10" ) ).ok; // false
const status = check( parse( "stale-while-revalidate=10" ) ).okStale; // true
const status = check( parse( "stale-if-error=10" ) ).okError; // true
Note: check
only reads immutable
, max-age
, and stale-
directives.
const status =
check( parse( "immutable, must-revalidate, no-store, no-cache" ) ).ok; // true
Helper to write a typesafe cache control header
string( "public, max-age=10, stale-while-revalidate=600" );
// public, max-age=10, stale-while-revalidate=600
// Unknown keys won't be filtered out
// @ts-expect-error typo - should be `max-age`
string( "maxage=1" ); // never
// @ts-expect-error missing value
string( "maxage" ); // never
stringify( { public: true, "max-age": 10, "stale-while-revalidate": 600 } );
// public,max-age=10,stale-while-revalidate=600
// Unknown keys won't be filtered out
// @ts-expect-error Unknown key
stringify( { unknown: 42 } ); // unknown=42
If you need to handle all headers related to HTTP caching, check out http-cache-semantics.
This library is more compareable to cache-control-parser
FAQs
Simplifies working with `Cache-Control` headers
The npm package cache-controlled receives a total of 0 weekly downloads. As such, cache-controlled popularity was classified as not popular.
We found that cache-controlled demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.