File size: 1,116 Bytes
edaa6b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | //#region src/early-hints.ts
/**
* Early Hints middleware for Node.js
* Automatically sends a 103 Early Hints informational response with the specified Link header(s).
*
* @param options EarlyHintsOptions
* @returns MiddlewareHandler
*/
const earlyHints = (options) => {
let warned = false;
return async (c, next) => {
const mode = c.req.header("Sec-Fetch-Mode");
const dest = c.req.header("Sec-Fetch-Dest");
if (mode && mode !== "navigate" || dest && dest !== "document") return next();
const env = c.env || {};
const outgoing = ("server" in env ? env.server : env)?.outgoing;
if (typeof outgoing?.writeEarlyHints !== "function") {
if (!warned) {
console.warn("Early Hints Middleware is not supported because writeEarlyHints is not defined.");
warned = true;
}
return await next();
}
if (!outgoing.headersSent) {
const link = typeof options.link === "function" ? options.link(c) : options.link;
if (link !== void 0 && (Array.isArray(link) ? link.length > 0 : Boolean(link))) outgoing.writeEarlyHints({ link });
}
await next();
};
};
//#endregion
export { earlyHints }; |