Files
SnowLuma/.pnpm-store/v11/files/13/c8f5bf39a4cfdd158c997c0dce42b0d2e76c4206a957b765d6dbf200a0f3b8d5edbe76a21e477d6daec8772f5a7561e639c597224a5b60f10e8d370795a7de
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

33 lines
1.1 KiB
Plaintext

//#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 };