interface Options { /** * If `true`, then the response is evaluated from a perspective of a shared cache (i.e. `private` is not * cacheable and `s-maxage` is respected). If `false`, then the response is evaluated from a perspective * of a single-user cache (i.e. `private` is cacheable and `s-maxage` is ignored). * `true` is recommended for HTTP clients. * @default true */ shared?: boolean; /** * A fraction of response's age that is used as a fallback cache duration. The default is 0.1 (10%), * e.g. if a file hasn't been modified for 100 days, it'll be cached for 100*0.1 = 10 days. * @default 0.1 */ cacheHeuristic?: number; /** * A number of milliseconds to assume as the default time to cache responses with `Cache-Control: immutable`. * Note that [per RFC](https://httpwg.org/specs/rfc8246.html#the-immutable-cache-control-extension) * these can become stale, so `max-age` still overrides the default. * @default 24*3600*1000 (24h) */ immutableMinTimeToLive?: number; /** * If `true`, common anti-cache directives will be completely ignored if the non-standard `pre-check` * and `post-check` directives are present. These two useless directives are most commonly found * in bad StackOverflow answers and PHP's "session limiter" defaults. * @default false */ ignoreCargoCult?: boolean; _fromObject?: CachePolicyObject; } interface CachePolicyObject { v: number; t: number; sh: boolean; ch: number; imm: number; icc?: boolean; st: number; resh: Record; rescc: Record; m: string; u: string; h: string | null; a: boolean; reqh: Record | null; reqcc: Record; } interface RevalidationPolicy { /** * A new `CachePolicy` with HTTP headers updated from `revalidationResponse`. You can always replace * the old cached `CachePolicy` with the new one. */ policy: CachePolicy; /** * Boolean indicating whether the response body has changed. * * - If `false`, then a valid 304 Not Modified response has been received, and you can reuse the old * cached response body. * - If `true`, you should use new response's body (if present), or make another request to the origin * server without any conditional headers (i.e. don't use `revalidationHeaders()` this time) to get * the new resource. */ modified: boolean; matches: boolean; } interface EvaluateRequestRevalidation { headers: Headers; synchronous: boolean; } interface EvaluateRequestResponse { headers: Headers; } interface EvaluateRequestResult { revalidation?: EvaluateRequestRevalidation | undefined; response?: EvaluateRequestResponse | undefined; } declare class CachePolicy { #private; constructor(req: Request, res: Response, { shared, cacheHeuristic, immutableMinTimeToLive, ignoreCargoCult, _fromObject, }?: Options); /** * @returns Date.now() */ now(): number; /** * Returns `true` if the response can be stored in a cache. * If it's `false` then you MUST NOT store either the request or the response. */ storable(): boolean; /** * Checks if the request matches the cache and can be satisfied from the cache immediately, * without having to make a request to the server. * * This doesn't support `stale-while-revalidate`. See `evaluateRequest()` for a more complete solution. */ satisfiesWithoutRevalidation(req: Request): boolean; /** * Checks if the given request matches this cache entry, and how the cache can be used to satisfy it. */ evaluateRequest(req: Request): EvaluateRequestResult; /** * Returns updated, filtered set of response headers to return to clients receiving the cached response. * This function is necessary, because proxies MUST always remove hop-by-hop headers (such as `TE` and `Connection`) * and update response's `Age` to avoid doubling cache time. * * @example * cachedResponse.headers = cachePolicy.responseHeaders(cachedResponse); */ responseHeaders(): Headers; /** * Value of the Date response header or current time if Date was invalid */ date(): number; /** * Value of the Age header, in seconds, updated for the current time. * May be fractional. */ age(): number; /** * Value of applicable max-age (or heuristic equivalent) in seconds. This counts since response's `Date`. * * For an up-to-date value, see `timeToLive()`. */ maxAge(): number; /** * Remaining time this cache entry may be useful for, in *milliseconds*. * You can use this as an expiration time for your cache storage. * * Prefer this method over `maxAge()`, because it includes other factors like `age` and `stale-while-revalidate`. */ timeToLive(): number; /** * If true, this cache entry is past its expiration date. * Note that stale cache may be useful sometimes, see `evaluateRequest()`. */ stale(): boolean; /** See `evaluateRequest()` for a more complete solution. */ useStaleWhileRevalidate(): boolean; /** * `policy = CachePolicy.fromObject(obj)` creates an instance from object created by `toObject()`. */ static fromObject(obj: CachePolicyObject): CachePolicy; /** * Chances are you'll want to store the `CachePolicy` object along with the cached response. * `obj = policy.toObject()` gives a plain JSON-serializable object. */ toObject(): CachePolicyObject; /** * Returns updated, filtered set of request headers to send to the origin server to check if the cached * response can be reused. These headers allow the origin server to return status 304 indicating the * response is still fresh. All headers unrelated to caching are passed through as-is. * * Use this method when updating cache from the origin server. * * @example * updateRequest.headers = cachePolicy.revalidationHeaders(updateRequest); */ revalidationHeaders(req: Request): Headers; /** * Creates new CachePolicy with information combined from the previews response, * and the new revalidation response. * * Returns {policy, modified} where modified is a boolean indicating * whether the response body has been modified, and old cached body can't be used. * * Use this method to update the cache after receiving a new response from the origin server. */ revalidatedPolicy(request: Request, response: Response): RevalidationPolicy; } export { type CachePolicyObject, type EvaluateRequestResponse, type EvaluateRequestResult, type EvaluateRequestRevalidation, type Options, type RevalidationPolicy, CachePolicy as default };