@worker-tools/middleware
Version:
A suite of standalone HTTP server middlewares for Worker Runtimes.
32 lines (27 loc) • 1.27 kB
text/typescript
// deno-lint-ignore-file no-explicit-any
import type { Temporal } from 'temporal-spec';
import type { Awaitable } from "./utils/common-types.js";
import type { Context } from "./index.js";
import { format, CacheControl } from '@tusbar/cache-control'
/**
* The Cache-Control HTTP header field holds directives (instructions)
* — in both requests and responses — that control caching in browsers
* and shared caches (e.g. Proxies, CDNs).
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
*/
export type CacheOptions = {
[K in keyof CacheControl]: CacheControl[K] extends (number | null | undefined)
? number | Temporal.Duration | null
: CacheControl[K]
}
const SECOND = { unit: 'second', relativeTo: '1970-01-01' } as Temporal.DurationTotalOf;
const isDuration = (x?: unknown): x is Temporal.Duration => (<any>x)?.[Symbol.toStringTag] === 'Temporal.Duration'
export const caching = (options: CacheOptions = {}) => async <X extends Context>(ax: Awaitable<X>) => {
const x = await ax;
const opts: CacheControl = Object.fromEntries(
Object.entries(options).map(([k, v]) => [k, isDuration(v) ? v.total(SECOND) : v])
);
x.effects.push(res => res.headers.set('Cache-Control', format(opts)));
return x;
}