@trifrost/core
Version:
Blazingly fast, runtime-agnostic server framework for modern edge and node environments
83 lines (82 loc) • 3.27 kB
JavaScript
import { isObject } from '@valkyriestudios/utils/object';
import { isIntGt } from '@valkyriestudios/utils/number';
import { Sym_TriFrostDescription, Sym_TriFrostFingerPrint, Sym_TriFrostName } from '../types/constants';
/* Specific symbol attached to cache control mware to identify them by */
export const Sym_TriFrostMiddlewareCacheControl = Symbol('TriFrost.Middleware.CacheControl');
var CacheControlValues;
(function (CacheControlValues) {
/**
* Instructs the browser that it must revalidate with the server every time before using a cached version of the URL
*/
CacheControlValues["NoCache"] = "no-cache";
/**
* Instructs the browser and other intermediate caches (like CDNs) to never store any version of the file
*/
CacheControlValues["NoStore"] = "no-store";
/**
* Browsers can cache the file but intermediate caches cannot
*/
CacheControlValues["Private"] = "private";
/**
* Response can be stored by any cache
*/
CacheControlValues["Public"] = "public";
})(CacheControlValues || (CacheControlValues = {}));
const CacheControlSet = new Set([...Object.values(CacheControlValues)]);
/**
* Parse Cache Header options into a Cache-Control value
*
* @param {TriFrostCacheControlOptions} opts - Cache Header Options
*/
function parse(opts) {
const parts = [];
if (isObject(opts)) {
/* type: no-store, no-cache, public, private */
if (CacheControlSet.has(opts.type))
parts.push(opts.type);
/* max-age */
if (isIntGt(opts.maxage, 0))
parts.push('max-age=' + opts.maxage);
/* s-maxage */
if (isIntGt(opts.proxyMaxage, 0))
parts.push('s-maxage=' + opts.proxyMaxage);
/* immutable */
if (opts.immutable === true)
parts.push('immutable');
/* must-revalidate */
if (opts.mustRevalidate === true)
parts.push('must-revalidate');
/* proxy-revalidate */
if (opts.proxyRevalidate === true)
parts.push('proxy-revalidate');
}
return parts.length ? parts.join(', ') : null;
}
/**
* Utility function which applies cache headers such as Cache-Control to a provided context
*
* @param {TriFrostContext} ctx - Context the headers are to be applied to
* @param {TriFrostCacheControlOptions} opts - Cache Header Options
*/
export function ParseAndApplyCacheControl(ctx, opts) {
const val = parse(opts);
if (val)
ctx.setHeader('cache-control', val);
}
/**
* Middleware function which adds cache headers such as Cache-Control to any context passing through it
*
* @param {TriFrostCacheControlOptions} opts - Cache Header Options
*/
export function CacheControl(opts = {}) {
const val = parse(opts);
const mware = function TriFrostCacheControlMiddleware(ctx) {
if (val)
ctx.setHeader('cache-control', val);
};
/* Add symbols for introspection/use further down the line */
Reflect.set(mware, Sym_TriFrostName, 'TriFrostCacheControl');
Reflect.set(mware, Sym_TriFrostDescription, 'Middleware adding Cache-Control headers to contexts passing through it');
Reflect.set(mware, Sym_TriFrostFingerPrint, Sym_TriFrostMiddlewareCacheControl);
return mware;
}