mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-02 07:43:42 +08:00
@hono/node-server replaces the global Request and Response with lightweight objects and reparents prototypes, tricks that only pay off under V8 and that native compilation cannot follow. scriptc now compiles the package from a built-in implementation (shims/hono-node-server/v2.ts) over node:http and the native Request/Response/Headers: serve, createAdaptorServer, getRequestListener and RequestError. Responses are written the way the package writes them, byte for byte on the wire: the general path (header record in Headers order, Set-Cookie gathered, default content type, content length for bodies that finish within the first reads, streamed bodies, error responses) and the package's cached-response path for a Response that is still exactly its constructor arguments. The runtime tracks that lightweight state on each constructed Response (header form, raw plain-record headers, body length; cleared by body and metadata reads, turned into Headers form by a headers read) and exposes it to the shim through scriptcResponseLight. The declaration entry of a shimmed package may now be a .d.mts/.d.cts file. The npm case hono-node-server drives buffered, streamed, cookie, header-form, HEAD, error-handler and URL cases against the real package under Node. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
|
//#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
|
|
exports.earlyHints = earlyHints; |