diff --git a/.gitignore b/.gitignore index cd44fe11d..b271af8e1 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ output/ /packages/runtime-*/runtime-pack.json /packages/runtime-*/.runtime-pack-* !tests/fixtures/npm/node_modules/*/dist/ +!tests/fixtures/npm/node_modules/@*/*/dist/ !tests/fixtures/npm/workspace/*/dist/ !tests/fixtures/npm/cases/workspace-copied/members/*/dist/ !tests/fixtures/npm/cases/workspace-copied/node_modules/*/dist/ diff --git a/docs/content/docs/dependencies.mdx b/docs/content/docs/dependencies.mdx index 409b9cb30..14130b234 100644 --- a/docs/content/docs/dependencies.mdx +++ b/docs/content/docs/dependencies.mdx @@ -92,6 +92,13 @@ scriptc implements a few packages itself and compiles that implementation in pla - graceful-fs's retry queue, which fs-extra uses under Node.js, isn't part of the implementation, so a program that runs out of file descriptors fails at the first `EMFILE` error instead of retrying. - An ES module that imports fs-extra can use any of its exports by name. Node.js only exposes the names its CommonJS lexer finds in fs-extra's source, so a name like `readFile` throws a `SyntaxError` there. +`@hono/node-server` 2.x is built in. `serve`, `createAdaptorServer`, `getRequestListener` and `RequestError` serve a fetch handler such as Hono's `app.fetch` over `node:http` with native `Request` and `Response` objects, and write each response the way the package does under Node.js: the same status line, header names, header order and content length, for buffered and streamed bodies, including the package's shortcut for a response that is still exactly what its constructor received. Request bodies stream to the handler as it reads them, and a body the handler leaves unread is drained after the response, up to 64 MiB or 500 ms, as the package does. Known differences: + +- Only HTTP/1.1 servers are declared: the `createServer` and `serverOptions` options for `node:https` and `node:http2`, the `websocket` option and `upgradeWebSocket` aren't declared, so using one is a type error at build time. Subpath modules such as `@hono/node-server/serve-static` compile from the package's own files. +- The fetch handler must return a `Response` or a promise of one; the handler's second argument holds `incoming` and `outgoing`. +- The global `Request` and `Response` stay the built-in classes, so `overrideGlobalObjects` has no effect, and a `Response` reads its default text content type as `text/plain;charset=UTF-8` where the package's replacement reads `text/plain; charset=UTF-8`. Written responses carry the package's spelling. +- A streamed response is written without waiting for `drain`, and a failed handler answers 504 only for an error whose `name` is `TimeoutError`. + Small polyfill packages whose module is exactly a Node.js built-in on Node.js 24 compile as that built-in, at the `require()` that loads them, from your program's CommonJS files, from compiled packages, and from esbuild bundles whose module paths name the package and its version (as pnpm installs do). The package's own files aren't compiled. diff --git a/packages/compiler/ambient/scriptc-node-fallback.d.ts b/packages/compiler/ambient/scriptc-node-fallback.d.ts index b195c3fbb..09a3fdd8e 100644 --- a/packages/compiler/ambient/scriptc-node-fallback.d.ts +++ b/packages/compiler/ambient/scriptc-node-fallback.d.ts @@ -5561,6 +5561,7 @@ declare module "http" { readonly trailersDistinct: { [name: string]: string[] | undefined }; readonly rawTrailers: string[]; resume(): void; + pause(): void; destroy(): void; setTimeout(msecs: number, callback?: () => void): this; /* setEncoding('utf8'): 'data' delivers strings (other real encodings diff --git a/packages/compiler/shims/hono-node-server/v2.ts b/packages/compiler/shims/hono-node-server/v2.ts new file mode 100644 index 000000000..a4dcc6805 --- /dev/null +++ b/packages/compiler/shims/hono-node-server/v2.ts @@ -0,0 +1,645 @@ +// scriptc's built-in implementation of `@hono/node-server` 2.x: the Node.js +// adapter that serves a fetch-style handler (Hono's `app.fetch`) over +// node:http. The published package swaps the global Request and Response +// for lightweight objects and reparents prototypes, which only pays off +// under V8; this port builds native Request objects from the incoming +// message, with a body streamed from it as the app reads it, and writes +// the handler's Response the way the package's listener does (header +// record, content length for bodies that complete within the first reads, +// streamed bodies, error responses), so a client sees the same status +// lines, headers and bodies. A body the app leaves unread is drained after +// the response, within the package's limits. +// +// HTTP/1.1 only: the http2/https server options and WebSocket upgrades are +// not declared. A Response that is still exactly its constructor arguments +// takes the package's cached-response path (its lightweight Response), read +// from the runtime through scriptcResponseLight; every other Response takes +// the general path. +import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http"; +import type { AddressInfo } from "node:net"; + +export type HttpBindings = { + incoming: IncomingMessage; + outgoing: ServerResponse; +}; + +export type ServerType = Server; + +export type FetchCallback = (request: Request, env: HttpBindings) => Response | Promise; + +export type CustomErrorHandler = (err: unknown) => void | Response | Promise; + +export type Options = { + fetch: FetchCallback; + overrideGlobalObjects?: boolean; + autoCleanupIncoming?: boolean; + port?: number; + hostname?: string; +}; + +export interface RequestListenerOptions { + hostname?: string; + errorHandler?: CustomErrorHandler; + overrideGlobalObjects?: boolean; + autoCleanupIncoming?: boolean; +} + +export class RequestError extends Error { + constructor(message: string, options?: { cause?: unknown }) { + super(message, options); + this.name = "RequestError"; + } +} + +const DEFAULT_CONTENT_TYPE = "text/plain; charset=UTF-8"; +const X_ALREADY_SENT = "x-hono-already-sent"; + +const reValidRequestUrl = /^\/[!#$&-;=?-[\]_a-z~]*$/; +const reDotSegment = /\/\.\.?(?:[/?#]|$)/; +const reValidHost = /^[a-z0-9._-]+(?::(?:[1-5]\d{3,4}|[6-9]\d{3}))?$/; + +function buildUrl(scheme: string, host: string, incomingUrl: string): string { + const url = `${scheme}://${host}${incomingUrl}`; + if (!reValidHost.test(host)) { + const urlObj = new URL(url); + if ( + urlObj.hostname.length !== host.length && + urlObj.hostname !== (host.includes(":") ? host.replace(/:\d+$/, "") : host).toLowerCase() + ) + throw new RequestError("Invalid host header"); + return urlObj.href; + } + if (incomingUrl.length === 0) return url + "/"; + if (incomingUrl.charCodeAt(0) !== 47) throw new RequestError("Invalid URL"); + if (!reValidRequestUrl.test(incomingUrl) || reDotSegment.test(incomingUrl)) + return new URL(url).href; + return url; +} + +function normalizeIncomingMethod(method: string | undefined): string { + if (method === undefined || method.length === 0) return "GET"; + switch (method) { + case "DELETE": + case "GET": + case "HEAD": + case "OPTIONS": + case "PATCH": + case "POST": + case "PUT": + case "QUERY": + return method; + } + const upper = method.toUpperCase(); + switch (upper) { + case "DELETE": + case "GET": + case "HEAD": + case "OPTIONS": + case "POST": + case "PUT": + return upper; + default: + return method; + } +} + +/** The request URL the package builds: an absolute request target as is, + * otherwise the Host header (or the configured hostname) over http. */ +function requestUrl(incoming: IncomingMessage, defaultHostname: string | undefined): string { + const incomingUrl = incoming.url ?? ""; + if ( + incomingUrl[0] !== "/" && + (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://")) + ) { + try { + return new URL(incomingUrl).href; + } catch (e) { + throw new RequestError("Invalid absolute URL", { cause: e }); + } + } + const hostHeader = incoming.headers.host; + const host = hostHeader !== undefined && hostHeader !== "" ? hostHeader : (defaultHostname ?? ""); + if (host === "") throw new RequestError("Missing host header"); + try { + return buildUrl("http", host, incomingUrl); + } catch (e) { + if (e instanceof RequestError) throw e; + throw new RequestError("Invalid URL", { cause: e }); + } +} + +function requestHeaders(incoming: IncomingMessage): Headers { + const headers = new Headers(); + const raw = incoming.rawHeaders; + for (let i = 0; i < raw.length; i += 2) { + const name = raw[i]!; + if (!name.startsWith(":")) headers.append(name, raw[i + 1]!); + } + return headers; +} + +/** What the listener knows about the incoming body: whether it ended + * (the package's `readableEnded`) and whether a drain started. */ +type IncomingState = { ended: boolean; draining: boolean }; + +/** The incoming message's readableHighWaterMark: Readable.toWeb queues + * this many bytes before it pauses the message. */ +const BODY_HIGH_WATER_MARK = 64 * 1024; + +/** The request body as the package streams it: Readable.toWeb over the + * incoming message, read one chunk per pull. Chunks queue until + * BODY_HIGH_WATER_MARK bytes wait unread, then the message pauses until a + * pull takes them, so the socket holds the rest. The stream pulls once on + * its own when it is constructed; the package builds its stream only when + * the app reads the body, so until the app's first read a drain discards + * the chunks instead of queueing them. */ +function incomingBody(incoming: IncomingMessage, state: IncomingState): ReadableStream { + let pulls = 0; + let listening = false; + let ended = false; + let failure: Error | undefined = undefined; + const queue: Buffer[] = []; + let queued = 0; + let wake: (() => void) | undefined = undefined; + const woken = (): void => { + const resolve = wake; + wake = undefined; + if (resolve !== undefined) resolve(); + }; + const listen = (): void => { + listening = true; + incoming.on("data", (chunk: Buffer) => { + if (state.draining && pulls === 1) return; + queue.push(chunk); + queued += chunk.length; + if (queued >= BODY_HIGH_WATER_MARK) incoming.pause(); + woken(); + }); + incoming.on("end", () => { + ended = true; + woken(); + }); + incoming.on("error", (err: Error) => { + failure = err; + woken(); + }); + }; + return new ReadableStream({ + async pull(controller) { + pulls++; + if (!listening) listen(); + for (;;) { + const chunk = queue.shift(); + if (chunk !== undefined) { + queued -= chunk.length; + if (queued < BODY_HIGH_WATER_MARK) incoming.resume(); + controller.enqueue(chunk); + return; + } + if (failure !== undefined) { + controller.error(failure); + return; + } + if (ended) { + controller.close(); + return; + } + await new Promise((resolve) => { + wake = resolve; + }); + } + }, + }); +} + +function newRequest( + incoming: IncomingMessage, + defaultHostname: string | undefined, + state: IncomingState, +): Request { + const method = normalizeIncomingMethod(incoming.method); + const url = requestUrl(incoming, defaultHostname); + const headers = requestHeaders(incoming); + if (method === "GET" || method === "HEAD") return new Request(url, { method, headers }); + return new Request(url, { + method, + headers, + body: incomingBody(incoming, state), + duplex: "half", + }); +} + +const DRAIN_TIMEOUT_MS = 500; +const MAX_DRAIN_BYTES = 64 * 1024 * 1024; + +/** The package's drain of a body the app left unread: read and discard + * up to MAX_DRAIN_BYTES or DRAIN_TIMEOUT_MS, then close the connection + * once its writes are out. */ +function drainIncoming(incoming: IncomingMessage, state: IncomingState): void { + const socket = incoming.socket; + if (socket.destroyed || state.draining) return; + state.draining = true; + let bytesRead = 0; + let done = false; + const cleanup = (): void => { + if (done) return; + done = true; + clearTimeout(timer); + }; + const forceClose = (): void => { + if (done) return; + cleanup(); + if (!socket.destroyed) socket.destroySoon(); + }; + const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS); + timer.unref(); + incoming.on("data", (chunk: Buffer) => { + if (done) return; + bytesRead += chunk.length; + if (bytesRead > MAX_DRAIN_BYTES) forceClose(); + }); + incoming.on("end", cleanup); + incoming.on("error", cleanup); + incoming.resume(); +} + +/** The package's close handler cleanup: an unread body drains two timer + * turns after the response closes. */ +function drainAfterClose(incoming: IncomingMessage, state: IncomingState): void { + if (state.ended) return; + setTimeout(() => { + if (!state.ended) + setTimeout(() => { + drainIncoming(incoming, state); + }); + }); +} + +/** The package's outgoing header record: Headers order (lowercase, + * sorted), Set-Cookie values gathered into one array entry at the end, + * and the default content type for a response with a body. */ +function buildOutgoingHttpHeaders( + headers: Headers, + hasBody: boolean, +): Record { + const res: Record = {}; + if (headers.has("set-cookie")) { + const cookies: string[] = []; + headers.forEach((v, k) => { + if (k === "set-cookie") cookies.push(v); + else res[k] = v; + }); + // Headers.forEach joins nothing but set-cookie, which getSetCookie + // lists value by value, as the package's iteration does. + if (cookies.length > 0) res["set-cookie"] = headers.getSetCookie(); + } else { + headers.forEach((v, k) => { + res[k] = v; + }); + } + if (hasBody && res["content-type"] === undefined) res["content-type"] = DEFAULT_CONTENT_TYPE; + return res; +} + +/* Spelled out rather than ReadableStreamReadResult, whose + * declaration depends on the program's lib and @types/node settings: the + * shim compiles inside every program that imports the package. */ +type ReadResult = { done: boolean; value?: Uint8Array }; + +function readWithoutBlocking(read: Promise): Promise { + return Promise.race([read, Promise.resolve().then(() => Promise.resolve(undefined))]); +} + +function nextTick(): Promise { + return new Promise((resolve) => { + setTimeout(resolve); + }); +} + +/** Writes the rest of a body stream. As in the package, a response that + * closes before the stream ends (the client disconnected) cancels the + * stream, so its producer stops; the pending read then answers done. A + * response that fails closes too, and its stream is cancelled the same way + * (the package passes the error as the reason). The listener stays attached + * and does nothing once the stream has ended. */ +async function pump( + reader: ReadableStreamDefaultReader, + outgoing: ServerResponse, + pending: Promise | undefined, +): Promise { + let finished = false; + outgoing.on("close", () => { + if (!finished) reader.cancel().catch(() => {}); + }); + try { + let read = pending ?? reader.read(); + for (;;) { + const { done, value } = await read; + if (done) { + outgoing.end(); + return; + } + outgoing.write(value!); + read = reader.read(); + } + } finally { + finished = true; + } +} + +function handleResponseError(e: unknown, outgoing: ServerResponse): void { + const err = e instanceof Error ? e : new Error("unknown error", { cause: e }); + console.error(e); + if (!outgoing.headersSent) outgoing.writeHead(500, { "Content-Type": "text/plain" }); + outgoing.end(`Error: ${err.message}`); + outgoing.destroy(); +} + +function emptyResponse(status: number): Response { + return new Response(null, { status }); +} + +/** The lightweight state of a constructed Response (the runtime's + * scr_fetch_response_light): [form, flags, byte length, ...record pairs]. */ +declare function scriptcResponseLight(res: Response): string[]; + +const UNDICI_TEXT_TYPE = "text/plain;charset=UTF-8"; + +/** Content-Length as Node's implicit header: set the headers one by one and + * let end(body) compute the length, which Node then writes after its own + * Date/Connection headers, as the package's preset length does. */ +function endWithImplicitLength( + outgoing: ServerResponse, + status: number, + header: Record, + body: Uint8Array, +): void { + outgoing.statusCode = status; + for (const name of Object.keys(header)) outgoing.setHeader(name, header[name]!); + outgoing.end(body); +} + +/** The package's cached-response path: a Response that is still exactly + * its constructor arguments (no body or metadata read since) is written + * from them. Form 1 has no init headers, form 2 Headers, form 3 a plain + * record kept verbatim. */ +async function writeLightResponse( + res: Response, + outgoing: ServerResponse, + light: string[], + head: boolean, +): Promise { + const form = light[0]!; + const flags = light[1]!; + const length = Number(light[2]!); + const status = res.status; + const sized = length >= 0; + if (form === "1") { + if (length === -1) { + outgoing.writeHead(status); + outgoing.end(); + } else if (sized) { + const body = await res.bytes(); + if (head) { + outgoing.writeHead(status, { + "Content-Type": DEFAULT_CONTENT_TYPE, + "Content-Length": String(length), + }); + outgoing.end(body); + } else + endWithImplicitLength(outgoing, status, { "Content-Type": DEFAULT_CONTENT_TYPE }, body); + } else { + outgoing.writeHead(status, { "Content-Type": DEFAULT_CONTENT_TYPE }); + outgoing.flushHeaders(); + await pump(res.body!.getReader(), outgoing, undefined); + } + return; + } + let header: Record; + let hasContentLength = false; + let plain = false; + let canAutoLength = true; + if (form === "2") { + const headers = new Headers(res.headers); + if (flags.includes("a") && headers.get("content-type") === UNDICI_TEXT_TYPE) { + if (flags.includes("d")) headers.set("content-type", DEFAULT_CONTENT_TYPE); + else headers.delete("content-type"); + } else if (flags.includes("d") && !headers.has("content-type")) { + headers.set("content-type", DEFAULT_CONTENT_TYPE); + } + hasContentLength = headers.has("content-length"); + header = buildOutgoingHttpHeaders(headers, length !== -1); + } else { + plain = true; + header = {}; + for (let i = 3; i + 1 < light.length; i += 2) { + const name = light[i]!; + const value = light[i + 1]!; + const previous = header[name]; + if (previous === undefined) header[name] = value; + else if (typeof previous === "string") header[name] = [previous, value]; + else previous.push(value); + const lower = name.toLowerCase(); + if (lower === "content-length") hasContentLength = true; + else if (lower === "transfer-encoding" || lower === "trailer") canAutoLength = false; + } + } + const body = sized ? await res.bytes() : undefined; + if (!hasContentLength && body !== undefined) { + if (plain && canAutoLength && !head && status !== 204 && status !== 304) { + endWithImplicitLength(outgoing, status, header, body); + return; + } + header["Content-Length"] = String(length); + } + outgoing.writeHead(status, header); + if (length === -1) outgoing.end(); + else if (body !== undefined) outgoing.end(body); + else { + outgoing.flushHeaders(); + await pump(res.body!.getReader(), outgoing, undefined); + } +} + +async function writeResponse( + res: Response, + outgoing: ServerResponse, + head: boolean, +): Promise { + const light = scriptcResponseLight(res); + if (light[0] !== "0") return writeLightResponse(res, outgoing, light, head); + const body = res.body; + const record = buildOutgoingHttpHeaders(res.headers, body !== null); + if (body !== null) { + const reader: ReadableStreamDefaultReader = body.getReader(); + const values: Uint8Array[] = []; + let done = false; + let pending: Promise | undefined = undefined; + if (record["transfer-encoding"] !== "chunked") { + let maxReadCount = 2; + for (let i = 0; i < maxReadCount; i++) { + pending ??= reader.read(); + let chunk: ReadResult | undefined = undefined; + try { + chunk = await readWithoutBlocking(pending); + } catch (e) { + console.error(e); + done = true; + } + if (chunk === undefined) { + if (i === 1 && !done) { + await nextTick(); + maxReadCount = 3; + continue; + } + break; + } + pending = undefined; + if (chunk.value !== undefined) values.push(chunk.value); + if (chunk.done) { + done = true; + break; + } + } + if (done && record["content-length"] === undefined) { + let length = 0; + for (const value of values) length += value.length; + record["content-length"] = String(length); + } + } + outgoing.writeHead(res.status, record); + for (const value of values) outgoing.write(value); + if (done) outgoing.end(); + else { + if (values.length === 0) outgoing.flushHeaders(); + await pump(reader, outgoing, pending); + } + } else if (record[X_ALREADY_SENT] === undefined) { + outgoing.writeHead(res.status, record); + outgoing.end(); + } +} + +async function respond( + pending: Response | Promise, + outgoing: ServerResponse, + errorHandler: CustomErrorHandler | undefined, + head: boolean, +): Promise { + let res: Response; + if (pending instanceof Promise) { + try { + res = await pending; + } catch (err) { + if (errorHandler === undefined) { + res = handleFetchError(err); + } else { + const errRes = await errorHandler(err); + if (errRes === undefined) return; + res = errRes; + } + } + } else res = pending; + await writeResponse(res, outgoing, head); +} + +/** A Response the package writes from its cache without waiting: still + * exactly its constructor arguments, with no body or a string or bytes + * body. */ +function immediateCacheable(res: Response): boolean { + const light = scriptcResponseLight(res); + return light[0] !== "0" && Number(light[2]!) >= -1; +} + +function handleFetchError(e: unknown): Response { + return emptyResponse(e instanceof Error && e.name === "TimeoutError" ? 504 : 500); +} + +async function handleRequest( + fetchCallback: FetchCallback, + options: RequestListenerOptions, + incoming: IncomingMessage, + outgoing: ServerResponse, +): Promise { + let req: Request | undefined = undefined; + let res: Response | Promise | undefined = undefined; + const state: IncomingState = { ended: false, draining: false }; + let needsBodyCleanup = false; + let closeHandlerAttached = false; + const ensureCloseHandler = (): void => { + if (req === undefined || closeHandlerAttached) return; + closeHandlerAttached = true; + if (needsBodyCleanup) + outgoing.on("close", () => { + drainAfterClose(incoming, state); + }); + }; + try { + req = newRequest(incoming, options.hostname, state); + needsBodyCleanup = + options.autoCleanupIncoming !== false && + !(incoming.method === "GET" || incoming.method === "HEAD"); + if (needsBodyCleanup) + incoming.on("end", () => { + state.ended = true; + }); + res = fetchCallback(req, { incoming, outgoing }); + if (!(res instanceof Promise) && immediateCacheable(res)) { + if (needsBodyCleanup) + outgoing.once("finish", () => { + if (!state.ended) drainIncoming(incoming, state); + }); + } else ensureCloseHandler(); + } catch (e) { + if (res !== undefined) return handleResponseError(e, outgoing); + const errorHandler = options.errorHandler; + if (errorHandler !== undefined) { + ensureCloseHandler(); + const handled = await errorHandler( + req !== undefined ? e : e instanceof RequestError ? e : toRequestError(e), + ); + if (handled === undefined) return; + res = handled; + } else if (req === undefined) res = emptyResponse(400); + else res = handleFetchError(e); + } + try { + await respond(res, outgoing, options.errorHandler, incoming.method === "HEAD"); + } catch (e) { + handleResponseError(e, outgoing); + } +} + +function toRequestError(e: unknown): RequestError { + return new RequestError(e instanceof Error ? e.message : String(e), { cause: e }); +} + +export function getRequestListener( + fetchCallback: FetchCallback, + options: RequestListenerOptions = {}, +): (incoming: IncomingMessage, outgoing: ServerResponse) => Promise { + return (incoming, outgoing) => handleRequest(fetchCallback, options, incoming, outgoing); +} + +export function createAdaptorServer(options: Options): ServerType { + const listener = getRequestListener(options.fetch, { + hostname: options.hostname, + overrideGlobalObjects: options.overrideGlobalObjects, + autoCleanupIncoming: options.autoCleanupIncoming, + }); + return createServer((incoming, outgoing) => { + void listener(incoming, outgoing); + }); +} + +export function serve( + options: Options, + listeningListener?: (info: AddressInfo) => void, +): ServerType { + const server = createAdaptorServer(options); + server.listen(options.port ?? 3000, options.hostname, () => { + const info = server.address() as AddressInfo; + if (listeningListener !== undefined) listeningListener(info); + }); + return server; +} diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 7dd4370c5..7b7d94717 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -896,6 +896,7 @@ export const LIB_FN_SYMS: Record = { // ambient-this read never throw. The fs dyn read is the sync-fs story. "fetch.start": "scr_fetch_static", "fetch.responseNew": "scr_fetch_response_new", + "fetch.responseLight": "scr_fetch_response_light", "fetch.responseJsonStatic": "scr_fetch_response_json_static", "fetch.input": "scr_fetch_input", "abort.controllerNew": "scr_fetch_abort_controller_new", @@ -1322,6 +1323,7 @@ export const LIB_FN_SYMS: Record = { "http.reqPipeClient": "scr_http_req_pipe_client", "http.reqPipeSock": "scr_http_req_pipe_sock", "http.reqResume": "scr_http_req_resume", + "http.reqPause": "scr_http_req_pause", "http.reqDestroy": "scr_http_req_destroy", "http.reqSetEncoding": "scr_http_req_set_encoding", "http.resSetHeader": "scr_http_res_set_header", diff --git a/packages/compiler/src/frontend/lowering/lower-calls.ts b/packages/compiler/src/frontend/lowering/lower-calls.ts index 82f5a27c1..436d4f1d4 100644 --- a/packages/compiler/src/frontend/lowering/lower-calls.ts +++ b/packages/compiler/src/frontend/lowering/lower-calls.ts @@ -11262,6 +11262,7 @@ const SHIM_INTRINSICS: ReadonlyMap = new Map< variants: ["hot-shots/v8-unix-dgram.ts"], }, ], + [ + "@hono/node-server", + { + name: "@hono/node-server", + mainFile: "dist/index.mjs", + typesFile: "dist/index.d.mts", + versions: new Map([[2, "hono-node-server/v2.ts"]]), + }, + ], [ "undici", { @@ -324,7 +333,7 @@ export function isShimmedImport(fromFile: string, specifier: string): boolean { /** The shimmed package whose declaration entry `path` is, with the * package directory: `/index.d.ts`, or the entry's own `typesFile`. */ function shimOfDeclarationPath(path: string): { pkgDir: string; shim: ImplementationShim } | null { - if (!path.endsWith(".d.ts")) return null; + if (!/\.d\.[cm]?ts$/.test(path)) return null; if (path.endsWith("/index.d.ts")) { const pkgDir = dirname(path); const shim = IMPLEMENTATION_SHIMS.get(pkgDir.slice(pkgDir.lastIndexOf("/") + 1)); @@ -377,7 +386,7 @@ export function isPackageShimFile(fileName: string): boolean { /** True for a name a shipped shim may declare as a bodiless runtime * intrinsic (lower-calls.ts lowers its calls to runtime calls). */ export function isShimIntrinsicName(name: string): boolean { - return name.startsWith("scriptcUnixDgram"); + return name.startsWith("scriptcUnixDgram") || name === "scriptcResponseLight"; } /** Resets per-compile caches. */ diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 6a645785f..33210f3dc 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -66,6 +66,7 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "fetch.start": { argTypes: [STRING, DYN], result: { kind: "promise", inner: DYN } }, "fetch.responseNew": { argTypes: [DYN, DYN], result: DYN }, "fetch.responseJsonStatic": { argTypes: [DYN, DYN], result: DYN }, + "fetch.responseLight": { argTypes: [DYN], result: arrayOf(STRING) }, "fetch.input": { argTypes: [DYN, DYN], result: { kind: "promise", inner: DYN } }, "abort.controllerNew": { argTypes: [], result: DYN }, /** EventTarget / Event / CustomEvent construction (scr_event_target.c): @@ -1835,7 +1836,7 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ * connection (+1, the same handle net.connect would give); * sockRemoteAddress answers `string | undefined` (NULL after the fd * closed, Node's destroyed-socket undefined; a dual-stack accept of an - * IPv4 peer reads "::ffff:a.b.c.d" like Node). reqResume/reqDestroy and + * IPv4 peer reads "::ffff:a.b.c.d" like Node). reqResume/reqPause/reqDestroy and * the req error/close listener slots complete the IncomingMessage * surface portless's client responses use; resDestroy/resOnClose and * resWriteHeadPairs ([k0,v0,k1,v1,...] — the env.pairs helper's flat @@ -1902,6 +1903,7 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "http.reqPipeClient": { argTypes: [HTTPREQ_T, HTTPCLIENTREQ_T], result: VOID }, "http.reqPipeSock": { argTypes: [HTTPREQ_T, NETSOCKET_T], result: VOID }, "http.reqResume": { argTypes: [HTTPREQ_T], result: VOID }, + "http.reqPause": { argTypes: [HTTPREQ_T], result: VOID }, "http.reqDestroy": { argTypes: [HTTPREQ_T], result: VOID }, "http.reqOnError": { argTypes: [HTTPREQ_T, null, BOOL], result: VOID }, "http.reqOnClose": { diff --git a/packages/compiler/test/package-shim-guard.test.ts b/packages/compiler/test/package-shim-guard.test.ts index a8620c1ce..9843739cd 100644 --- a/packages/compiler/test/package-shim-guard.test.ts +++ b/packages/compiler/test/package-shim-guard.test.ts @@ -51,6 +51,7 @@ describe("package shims compile without refusals", () => { expect(entries.map((e) => e.slice(shimRoot.length + 1))).toEqual([ "chalk/v5.ts", "fs-extra/index.ts", + "hono-node-server/v2.ts", "hot-shots/v8-unix-dgram.ts", "hot-shots/v8.ts", "undici/v5.ts", diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/7287-http-held-request-backpressure.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/7287-http-held-request-backpressure.ts.json new file mode 100644 index 000000000..3a4358d78 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/7287-http-held-request-backpressure.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/7287-http-held-request-backpressure.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/hono-node-server/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/hono-node-server/main.ts.json new file mode 100644 index 000000000..58f450083 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/hono-node-server/main.ts.json @@ -0,0 +1,7 @@ +{ + "order": [ + "/packages/compiler/shims/hono-node-server/v2.ts", + "/tests/fixtures/npm/cases/hono-node-server/main.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_fetch.c b/packages/runtime/src/scr_fetch.c index 7cc2beef4..94467aca9 100644 --- a/packages/runtime/src/scr_fetch.c +++ b/packages/runtime/src/scr_fetch.c @@ -184,6 +184,23 @@ struct SfResponse { int status; bool redirected; bool null_body; + /* @hono/node-server's lightweight-response state (scr_fetch_response_light): + * whether a constructed Response is still one the package would write + * from its constructor arguments, and in which header form. 0: not (it + * was materialized by a body/metadata read, or never qualified); 1: no + * init headers; 2: a Headers (or pair list) form; 3: a plain record, + * whose raw entries light_record keeps in order. */ + int8_t light; + /* Form 2 reached from form 1 by a headers read: the package's Headers + * start with its default text content type when the body is not null. */ + bool light_default_ct; + /* The constructor added the text content type (a string body without + * one), which the package's lightweight headers never hold. */ + bool auto_ct; + /* The body's byte length (string or Uint8Array), -1 for a null body, + * -2 for a stream. */ + double light_length; + ScrArr *light_record; }; #ifndef SCR_FETCH_SIGNAL_ONLY @@ -2660,6 +2677,7 @@ static void sf_response_release(SfResponse *r) { sf_headers_release(r->headers); scr_str_release(r->url); scr_str_release(r->status_text); + scr_arr_release(r->light_record); free(r); } @@ -2670,6 +2688,13 @@ static void sf_response_release_v(void *p) { sf_response_release((SfResponse *)p); } +/* A read the package's lightweight Response answers from its native + * response materializes it; status, ok and headers keep it light, and a + * headers read turns the header form into a Headers object. */ +static void sf_response_materialize(SfResponse *r) { + r->light = 0; +} + static ScrPromise *sf_response_collect(SfResponse *r, int mode) { if (!r->null_body) return sf_stream_collect(r->body, mode); @@ -2708,6 +2733,7 @@ static ScrDyn *sf_response_invoke(void *ptr, ScrDyn *self, ScrDyn *const *args, size_t argc, const char *what) { SfResponse *r = ptr; + sf_response_materialize(r); int mode = -1; if (strcmp(method, "json") == 0) mode = SF_COLLECT_JSON; else if (strcmp(method, "text") == 0) mode = SF_COLLECT_TEXT; @@ -2731,6 +2757,19 @@ static ScrDyn *sf_response_get(void *ptr, const char *key, size_t len) { if (sf_name(key, len, "ok")) { return scr_dyn_new_bool(r->status >= 200 && r->status <= 299); } + if (sf_name(key, len, "headers")) { + if (r->light == 1) { + r->light = 2; + r->light_default_ct = !r->null_body; + } else if (r->light == 3) { + r->light = 2; + } + return scr_dyn_new_handle(r->headers, SCR_DYNH_FETCH_HEADERS); + } + if (sf_name(key, len, "body") || sf_name(key, len, "bodyUsed") || + sf_name(key, len, "url") || sf_name(key, len, "redirected") || + sf_name(key, len, "statusText")) + sf_response_materialize(r); if (sf_name(key, len, "bodyUsed")) { return scr_dyn_new_bool(!r->null_body && r->body->disturbed); } @@ -2746,9 +2785,6 @@ static ScrDyn *sf_response_get(void *ptr, const char *key, size_t len) { if (sf_name(key, len, "statusText")) { return scr_dyn_new_str(r->status_text); } - if (sf_name(key, len, "headers")) { - return scr_dyn_new_handle(r->headers, SCR_DYNH_FETCH_HEADERS); - } return NULL; } @@ -2760,6 +2796,7 @@ static ScrPromise *sf_response_collect_dyn(ScrDyn *response, int mode) { &scr_dyn_release_v, NULL); } SfResponse *r = response->v.handle.ptr; + sf_response_materialize(r); return sf_response_collect(r, mode); } @@ -4752,6 +4789,65 @@ static ScrDyn *sf_response_json_serialize(const ScrDyn *value) { return serialized; } +/* The lightweight header form of a constructor's init headers (see + * SfResponse.light): absent headers are form 1 (Response.json's plain + * {"content-type": "application/json"} record is form 3), a Headers object + * or pair list form 2, and a plain record form 3, whose names and values + * *record receives in order (an array value contributes one entry per + * element, as writeHead writes it). Response.json with init headers is + * form 2 whatever their shape: the package's Response.json passes them on + * as a Headers object, adding the JSON content type when they lack one. + * 0 for any other init. */ +static int8_t sf_light_header_form(const ScrDyn *headers, bool json_body, + ScrArr **record) { + if (!headers || headers->kind == SCR_DYN_UNDEF) { + if (!json_body) return 1; + *record = scr_arr_new(SCR_ELEM_STR, 2); + sf_push_header_text(*record, "content-type", "application/json", 16); + return 3; + } + if (json_body) return 2; + if (headers->kind == SCR_DYN_ARR || + (headers->kind == SCR_DYN_HANDLE && + headers->v.handle.tag == SCR_DYNH_FETCH_HEADERS)) + return 2; + ScrDyn *materialized = NULL; + if (headers->kind == SCR_DYN_TYPED_REF) { + materialized = scr_dyn_typed_ref_materialize(headers); + if (!materialized) return 0; + headers = materialized; + } + int8_t form = 0; + if (headers->kind == SCR_DYN_OBJ) { + ScrArr *out = scr_arr_new(SCR_ELEM_STR, 2 * headers->v.obj.len); + form = 3; + for (size_t i = 0; i < headers->v.obj.len && form == 3; i++) { + const ScrDynEntry *e = &headers->v.obj.entries[i]; + const ScrDyn *value = e->value; + size_t count = value->kind == SCR_DYN_ARR ? value->v.arr.len : 1; + for (size_t j = 0; j < count; j++) { + const ScrDyn *item = value->kind == SCR_DYN_ARR ? value->v.arr.items[j] : value; + if (item->kind != SCR_DYN_STR && item->kind != SCR_DYN_NUM && + item->kind != SCR_DYN_BOOL) { + form = 0; + break; + } + ScrStr *text = scr_dyn_string_coerce_js(item); + if (!text) { + form = 0; + break; + } + scr_arr_push_ref(out, scr_str_new(e->key, e->key_len)); + scr_arr_push_ref(out, text); + } + } + if (form == 3) *record = out; + else scr_arr_release(out); + } + scr_dyn_release(materialized); + return form; +} + static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init, const ScrDyn *json_data, SfJsonSerialize serialize) { @@ -4766,6 +4862,23 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init, bool text_body = false; int status = 200; bool status_range_error = false; + /* The lightweight-response classification (see SfResponse.light): a + * Response init qualifies only while it is itself still light. */ + bool light_ok = !(init && init->kind == SCR_DYN_HANDLE && + init->v.handle.tag == SCR_DYNH_FETCH_RESPONSE && + ((SfResponse *)init->v.handle.ptr)->light == 0); + if (!json_body && !null_body) { + if (body->kind == SCR_DYN_BYTES) + light_ok = light_ok && body->v.bytes->elem == SCR_BYTES_U8 && + !body->v.bytes->is_data_view; + else if (!(body->kind == SCR_DYN_STR || + (body->kind == SCR_DYN_HANDLE && + body->v.handle.tag == SCR_DYNH_WEB_STREAM))) + light_ok = false; + } + int8_t light_form = 0; + ScrArr *light_record = NULL; + size_t light_length_bytes = 0; /* WebIDL converts constructor arguments from left to right. BodyInit's * string arm therefore runs before ResponseInit's dictionary conversion, @@ -4803,6 +4916,7 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init, * coercions precede status and statusText. Header syntax validation is a * later response-initialization step, after body extraction. */ ScrDyn *init_headers = sf_response_init_get(init, "headers", 7); + if (light_ok) light_form = sf_light_header_form(init_headers, json_body, &light_record); converted_headers = sf_headers_convert_init(init_headers); scr_dyn_release(init_headers); if (!converted_headers) goto fail; @@ -4859,6 +4973,7 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init, scr_str_release(encoding); scr_dyn_release(serialized); stream = sf_stream_new_native(); + light_length_bytes = body_bytes->len; sf_stream_enqueue_bytes(stream, body_bytes); sf_stream_close(stream); scr_bytes_release(body_bytes); @@ -4877,6 +4992,7 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init, sf_stream_retain(stream); } else { stream = sf_stream_new_native(); + light_length_bytes = body_bytes->len; sf_stream_enqueue_bytes(stream, body_bytes); sf_stream_close(stream); scr_bytes_release(body_bytes); @@ -4910,9 +5026,11 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init, sf_type_error(message); goto fail; } + bool auto_ct = false; if (text_body && !sf_pairs_have(header_pairs, "content-type")) { sf_push_header_text(header_pairs, "content-type", "text/plain;charset=UTF-8", 24); + auto_ct = true; } if (json_body && !sf_pairs_have(header_pairs, "content-type")) { static const char json_type[16] = "application/json"; @@ -4930,6 +5048,14 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init, response->status = status; response->redirected = false; response->null_body = null_body; + response->light = light_form; + response->auto_ct = auto_ct; + response->light_record = light_record; + light_record = NULL; + response->light_length = + null_body ? -1.0 + : (!json_body && body->kind == SCR_DYN_HANDLE) ? -2.0 + : (double)light_length_bytes; ScrDyn *out = scr_dyn_new_handle(response, SCR_DYNH_FETCH_RESPONSE); sf_response_release(response); @@ -4941,9 +5067,43 @@ fail: scr_str_release(status_text); scr_arr_release(header_pairs); scr_arr_release(converted_headers); + scr_arr_release(light_record); return NULL; } +/* @hono/node-server's built-in implementation reads a Response's + * lightweight state to write it as the package does: [form, flags, + * byte length, ...raw record name/value pairs], with form as in + * SfResponse.light ("0" alone when the response is not light), flags "d" + * (the package's Headers hold its default content type) and "a" (the + * constructor added the text content type), and the length -1 for a null + * body or -2 for a stream. */ +ScrArr *scr_fetch_response_light(ScrDyn *response) { + ScrArr *out = scr_arr_new(SCR_ELEM_STR, 4); + if (!response || response->kind != SCR_DYN_HANDLE || + response->v.handle.tag != SCR_DYNH_FETCH_RESPONSE) { + scr_arr_push_ref(out, scr_str_new("0", 1)); + return out; + } + SfResponse *r = response->v.handle.ptr; + char buf[32]; + int n = snprintf(buf, sizeof buf, "%d", (int)r->light); + scr_arr_push_ref(out, scr_str_new(buf, (size_t)n)); + if (r->light == 0) return out; + char flags[3]; + size_t f = 0; + if (r->light_default_ct) flags[f++] = 'd'; + if (r->auto_ct) flags[f++] = 'a'; + scr_arr_push_ref(out, scr_str_new(flags, f)); + n = snprintf(buf, sizeof buf, "%.0f", r->light_length); + scr_arr_push_ref(out, scr_str_new(buf, (size_t)n)); + if (r->light == 3 && r->light_record) { + for (size_t i = 0; i < r->light_record->len; i++) + scr_arr_push_ref(out, scr_arr_get_ref(r->light_record, (double)i)); + } + return out; +} + ScrDyn *scr_fetch_response_new(ScrDyn *body, ScrDyn *init) { return sf_response_construct(body, init, NULL, NULL); } diff --git a/packages/runtime/src/scr_generator.h b/packages/runtime/src/scr_generator.h index c3dac861d..828f339d5 100644 --- a/packages/runtime/src/scr_generator.h +++ b/packages/runtime/src/scr_generator.h @@ -181,6 +181,7 @@ ScrDyn *scr_fetch_headers_new(ScrDyn *init); ScrPromise *scr_fetch_static(ScrStr *url, ScrDyn *init); /* +1 promise */ ScrDyn *scr_fetch_response_new(ScrDyn *body, ScrDyn *init); /* borrowed args; +1 Response handle or NULL pending */ ScrDyn *scr_fetch_response_json_static(ScrDyn *data, ScrDyn *init); /* Response.json; borrowed args; +1 Response handle or NULL pending */ +ScrArr *scr_fetch_response_light(ScrDyn *response); /* @hono/node-server shim: borrowed; +1 string[] */ ScrPromise *scr_fetch_response_json(ScrDyn *response); /* +1 promise */ ScrPromise *scr_fetch_response_text(ScrDyn *response); /* +1 promise */ ScrPromise *scr_fetch_response_array_buffer(ScrDyn *response); /* +1 promise */ diff --git a/packages/runtime/src/scr_http.c b/packages/runtime/src/scr_http.c index cb88297bf..29eb52ef7 100644 --- a/packages/runtime/src/scr_http.c +++ b/packages/runtime/src/scr_http.c @@ -239,10 +239,11 @@ struct ScrHttpReq { bool close_queued; bool close_emitted; /* settled: err/close listeners dropped */ bool join_dup; /* joinDuplicateHeaders: repeated names read joined ", " */ - /* pause(): delivery holds — arrived body bytes buffer in pend (the - * parser keeps consuming; memory is the buffer, a documented bound) - * and 'end' defers (end_pending) until resume() drains through the - * emit queue, never the resuming stack. */ + /* pause(): delivery holds — arrived body bytes buffer in pend and + * 'end' defers (end_pending) until resume() drains through the emit + * queue, never the resuming stack. A held HTTP/1 server request stops + * its socket's reads once pend reaches SCR_HTTP_REQ_HOLD_BYTES + * (sock_held), as Node's readStop does. */ bool paused; bool end_pending; bool drain_queued; @@ -257,6 +258,7 @@ struct ScrHttpReq { bool received; /* the whole body arrived (req.complete) */ bool body_seen; /* body bytes arrived */ bool destroyed; /* req.destroy()/the teardown ran — req.destroyed */ + bool sock_held; /* this request stopped its socket's reads */ ScrNetLs aborted_ls; /* req.on('aborted') — the h2 compat event */ }; @@ -330,7 +332,10 @@ void scr_http_req_release(ScrHttpReq *r) { scr_http_client_release(r->pipe_client); scr_net_sock_release(r->pipe_sock); free(r->pend); - if (r->sock) scr_net_sock_release(r->sock); + if (r->sock) { + if (r->sock_held) scr_net_sock_hold(r->sock, false); + scr_net_sock_release(r->sock); + } if (r->h2_stream) scr_http_h2_ops->release(r->h2_stream); #ifdef SCR_RC_AUDIT scr_http_live--; @@ -638,6 +643,22 @@ static bool scr_http_req_holding(const ScrHttpReq *r) { return r->paused || (r->gated && !r->flowing); } +/* IncomingMessage's readableHighWaterMark (its socket's, 64 KiB): a held + * request buffers this many body bytes before its socket stops reading. */ +#define SCR_HTTP_REQ_HOLD_BYTES 65536u + +/* Stops the socket's reads while an HTTP/1 server request holds a full + * buffer, and restarts them once it flows, ends or is dumped, as Node's + * readStop when push() returns false. Client responses and h2 compat + * requests keep reading. */ +static void scr_http_req_update_hold(ScrHttpReq *r) { + bool hold = r->sock != NULL && r->status == -1 && !r->http2 && !r->ended && + scr_http_req_holding(r) && r->pend_len >= SCR_HTTP_REQ_HOLD_BYTES; + if (hold == r->sock_held) return; + r->sock_held = hold; + scr_net_sock_hold(r->sock, hold); +} + static void scr_http_req_queue_drain(ScrHttpReq *r) { if (r->drain_queued) return; r->drain_queued = true; @@ -650,6 +671,7 @@ static void scr_http_req_flow(ScrHttpReq *r) { r->paused = false; r->flowing = true; if (r->pend_len > 0 || r->end_pending) scr_http_req_queue_drain(r); + scr_http_req_update_hold(r); } /* A 'data' listener or pipe() starts the flow unless pause() stopped it, @@ -658,9 +680,7 @@ static void scr_http_req_consume(ScrHttpReq *r) { if (r->gated && !r->paused && !r->flowing) scr_http_req_flow(r); } -/* resume(): releases what pause() or the initial paused state held. This - * parser always consumes body bytes from the socket (SEMANTICS.md); held - * bytes buffer in memory. */ +/* resume(): releases what pause() or the initial paused state held. */ void scr_http_req_resume(ScrHttpReq *r) { if (!scr_http_req_holding(r)) return; scr_http_req_flow(r); @@ -676,6 +696,7 @@ static void scr_http_req_dump(ScrHttpReq *r) { r->pend_len = r->pend_cap = 0; r->paused = false; r->flowing = true; + scr_http_req_update_hold(r); } /* pause(): delivery holds until resume() (scr_http_req_deliver buffers; @@ -759,6 +780,7 @@ static void scr_http_req_finish(ScrHttpReq *r, bool fire) { return; } r->ended = true; + scr_http_req_update_hold(r); r->end_consumed = fire && (r->data_ls.n > 0 || r->pipe_res || r->pipe_client || r->pipe_sock); if (fire) scr_net_fire0_this(&r->end_ls, r, SCR_DYNH_HTTP_REQ); scr_net_ls_drop(&r->data_ls); @@ -2305,6 +2327,7 @@ static void scr_http_proto_sweep(void) { req->pend_len = req->pend_cap = 0; if (held_len > 0) scr_http_req_deliver(req, held, held_len); free(held); + scr_http_req_update_hold(req); if (!scr_exc_pending() && !req->paused && req->end_pending) { req->end_pending = false; scr_http_req_finish(req, true); @@ -2758,8 +2781,8 @@ static void scr_http_req_deliver(ScrHttpReq *r, const char *data, size_t n) { if (!r || n == 0 || r->ended) return; r->body_seen = true; if (scr_http_req_holding(r)) { - /* paused: the parser keeps consuming (memory is the buffer — a - * documented bound), delivery waits for the drain */ + /* held: delivery waits for the drain; a full buffer stops the + * socket's reads */ if (r->pend_len + n > r->pend_cap) { size_t cap = r->pend_cap ? r->pend_cap : 4096; while (cap < r->pend_len + n) cap *= 2; @@ -2769,6 +2792,7 @@ static void scr_http_req_deliver(ScrHttpReq *r, const char *data, size_t n) { } memcpy(r->pend + r->pend_len, data, n); r->pend_len += n; + scr_http_req_update_hold(r); return; } bool piped = r->pipe_res != NULL || r->pipe_client != NULL || r->pipe_sock != NULL; diff --git a/packages/runtime/src/scr_net.c b/packages/runtime/src/scr_net.c index 29a4af393..4d5a37f77 100644 --- a/packages/runtime/src/scr_net.c +++ b/packages/runtime/src/scr_net.c @@ -556,6 +556,10 @@ struct ScrNetSocket { * unconsumed stream). resume() clears user_paused and sets flowing. */ bool user_paused; bool flowing; + /* scr_net_sock_hold: a protocol consumer (an HTTP/1 server request + * holding unread body bytes) stops reads without touching the user's + * pause state */ + bool hold; /* EOF arrived while the socket buffered for a consumer it does not have * yet: 'end' waits until one drains the buffer (Node's readable state) */ bool eof_pending; @@ -978,7 +982,7 @@ static ScrNetSocket *scr_net_sock_new(void) { #define SCR_NET_READ_AHEAD 65536u static void scr_net_sock_update_read(ScrNetSocket *s) { - bool want = s->fd >= 0 && !s->connecting && !s->rd_eof && !s->user_paused && + bool want = s->fd >= 0 && !s->connecting && !s->rd_eof && !s->user_paused && !s->hold && (s->data_ls.n > 0 || s->pipe_dst != NULL || s->native_data != NULL || (s->tunnel != NULL && !s->tunnel->send_only) /* the proxy's CONNECT answer */ || s->flowing /* resume(): flow (and discard sans listeners) */ || @@ -1303,7 +1307,7 @@ static void scr_net_sock_read(ScrNetSocket *s) { if (s->tunnel != NULL) return; /* readiness feeds the tunnel pump instead */ if (s->tops && !s->t_est) return; /* readiness feeds the handshake pump instead */ for (;;) { - if (s->fd < 0 || s->user_paused) return; + if (s->fd < 0 || s->user_paused || s->hold) return; bool flowing = s->data_ls.n > 0 || s->pipe_dst || s->native_data || s->flowing; /* buffered (peeked/unshifted) bytes first — they precede the * kernel's in the stream; a TLS engine drains them through its bio */ @@ -3102,6 +3106,15 @@ ScrNetSocket *scr_net_sock_pause(ScrNetSocket *s) { return scr_net_sock_retain(s); } +/* Stops (hold) or restarts reads for a protocol consumer that has + * buffered as much as it accepts, as Node's readStop when push() returns + * false. Separate from pause()/resume(), which stay the user's. */ +void scr_net_sock_hold(ScrNetSocket *s, bool hold) { + if (s->hold == hold) return; + s->hold = hold; + scr_net_sock_update_read(s); +} + /* socket.resume(): flowing mode — a consumer even with no 'data' * listener (arrived bytes discard, so 'end' can be reached, Node's * resumed-but-unconsumed stream). Buffered bytes deliver from the next @@ -5271,7 +5284,7 @@ static bool scr_net_flags_pending(void) { * plaintext inside a transport, or unshifted bytes in the receive * buffer: the poller can't re-signal them, the sweep must deliver */ bool consumers = (s->data_ls.n > 0 || s->pipe_dst || s->native_data || s->flowing) && - !s->user_paused; + !s->user_paused && !s->hold; if (consumers && s->fd >= 0) { if (s->tops && s->t_est && s->tops->pending && s->tops->pending(s->tctx)) return true; if (!s->tops && s->rlen > s->rhead) return true; @@ -5377,7 +5390,7 @@ static void scr_net_sweep(void) { while (sock) { ScrNetSocket *next = sock->next; /* may unregister below */ scr_net_sock_retain(sock); /* callbacks may drop every other ref */ - if (sock->fd >= 0 && !sock->user_paused && + if (sock->fd >= 0 && !sock->user_paused && !sock->hold && (sock->data_ls.n > 0 || sock->pipe_dst || sock->native_data || sock->flowing) && ((sock->tops && sock->t_est && sock->tops->pending && sock->tops->pending(sock->tctx)) || (!sock->tops && sock->rlen > sock->rhead))) { diff --git a/packages/runtime/src/scr_net.h b/packages/runtime/src/scr_net.h index ffa9b2f72..6df6945ea 100644 --- a/packages/runtime/src/scr_net.h +++ b/packages/runtime/src/scr_net.h @@ -232,6 +232,7 @@ void scr_net_sock_destroy(ScrNetSocket *s); * counters/flags the compat surface reads. */ ScrNetSocket *scr_net_sock_pause(ScrNetSocket *s); /* +1: chaining */ ScrNetSocket *scr_net_sock_resume(ScrNetSocket *s); /* +1: chaining */ +void scr_net_sock_hold(ScrNetSocket *s, bool hold); /* protocol read hold (not pause) */ ScrNetSocket *scr_net_sock_set_nodelay(ScrNetSocket *s, bool enable); /* +1: chaining */ void scr_net_sock_set_keepalive(ScrNetSocket *s, bool enable, double delay_ms); void scr_net_sock_destroy_soon(ScrNetSocket *s); diff --git a/tests/corpus/7287-http-held-request-backpressure.ts b/tests/corpus/7287-http-held-request-backpressure.ts new file mode 100644 index 000000000..1953b8ec6 --- /dev/null +++ b/tests/corpus/7287-http-held-request-backpressure.ts @@ -0,0 +1,106 @@ +// A server request that holds its body (nothing reads it yet, or its +// reader paused it) stops reading its socket once its buffer is full, so +// a client that keeps uploading is held back by TCP. Each client writes +// 64 KiB chunks, one after the previous write's callback, and stops when +// the response arrives. An unread body followed by a pipelined request is +// discarded once the response finishes, and the second request is served. +import * as http from "node:http"; +import * as net from "node:net"; +import type { AddressInfo } from "node:net"; + +const MiB = 1024 * 1024; + +function upload(port: number, path: string): Promise { + return new Promise((resolve) => { + const socket = net.connect(port, "127.0.0.1"); + const frame = Buffer.concat([Buffer.from("10000\r\n"), Buffer.alloc(65536, 98), Buffer.from("\r\n")]); + let sent = 0; + let sentBeforeResponse = -1; + let response = ""; + const writeNext = (): void => { + if (sentBeforeResponse >= 0 || socket.destroyed || sent >= 256 * MiB) return; + sent += 65536; + socket.write(frame, () => { + writeNext(); + }); + }; + socket.on("connect", () => { + socket.write(`POST ${path} HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n`); + writeNext(); + }); + socket.on("data", (chunk: Buffer) => { + response += chunk.toString("latin1"); + if (sentBeforeResponse < 0 && response.includes("\r\n\r\n")) { + sentBeforeResponse = sent; + socket.destroy(); + resolve(sentBeforeResponse); + } + }); + socket.on("error", () => {}); + }); +} + +function pipelined(port: number): Promise { + return new Promise((resolve) => { + const socket = net.connect(port, "127.0.0.1"); + const body = "c".repeat(300000); + let response = ""; + socket.on("connect", () => { + socket.write( + `POST /unread HTTP/1.1\r\nHost: localhost\r\nContent-Length: ${body.length}\r\n\r\n${body}` + + "GET /second HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n", + ); + }); + socket.on("data", (chunk: Buffer) => { + response += chunk.toString("latin1"); + }); + socket.on("end", () => { + const lines = response.split("\r\n").filter((line) => line.startsWith("HTTP/") || line.startsWith("served")); + resolve(lines); + }); + }); +} + +let pausedRequest: http.IncomingMessage | undefined; + +const server = http.createServer((req, res) => { + if (req.url === "/unconsumed") { + setTimeout(() => { + res.writeHead(413, { "content-type": "text/plain" }); + res.end("unconsumed"); + }, 300); + return; + } + if (req.url === "/paused") { + let seen = 0; + req.on("data", (chunk: Buffer) => { + seen += chunk.length; + if (pausedRequest === undefined) { + pausedRequest = req; + req.pause(); + setTimeout(() => { + res.writeHead(413, { "content-type": "text/plain" }); + res.end(`paused after ${seen > 0}`); + req.resume(); + }, 300); + } + }); + return; + } + res.writeHead(200, { "content-type": "text/plain" }); + res.end(`served ${req.url}`); +}); + +server.listen(0, "127.0.0.1", () => { + const port = (server.address() as AddressInfo).port; + void (async () => { + const before = process.memoryUsage().rss; + const unconsumed = await upload(port, "/unconsumed"); + console.log("unconsumed: client stopped before 32 MiB:", unconsumed < 32 * MiB); + const paused = await upload(port, "/paused"); + console.log("paused: client stopped before 32 MiB:", paused < 32 * MiB); + console.log("memory grew under 64 MiB:", process.memoryUsage().rss - before < 64 * MiB); + for (const line of await pipelined(port)) console.log(line); + server.close(() => console.log("closed")); + })(); +}); diff --git a/tests/fixtures/npm/cases/hono-node-server/main.ts b/tests/fixtures/npm/cases/hono-node-server/main.ts new file mode 100644 index 000000000..3cca4aa44 --- /dev/null +++ b/tests/fixtures/npm/cases/hono-node-server/main.ts @@ -0,0 +1,387 @@ +// @hono/node-server 2.x (compiled from scriptc's built-in implementation) +// serving a fetch handler: a raw HTTP client prints each exchange's status +// line, wire-order headers (Date dropped) and body, covering buffered and +// streamed bodies, header forms, cookies, request bodies, URL handling, +// HEAD, empty statuses, handler errors, a client that disconnects during +// a streamed body, and request bodies the handler reads, leaves unread or +// rejects while the upload is still arriving. +import * as http from "node:http"; +import * as net from "node:net"; +import type { AddressInfo } from "node:net"; +import { getRequestListener, RequestError, serve } from "@hono/node-server"; + +function delay(ms: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} + +function slowStream(parts: string[]): ReadableStream { + const encoder = new TextEncoder(); + let index = 0; + return new ReadableStream({ + async pull(controller) { + await delay(5); + if (index < parts.length) controller.enqueue(encoder.encode(parts[index++]!)); + else controller.close(); + }, + }); +} + +function readyStream(parts: string[]): ReadableStream { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const part of parts) controller.enqueue(encoder.encode(part)); + controller.close(); + }, + }); +} + +// A stream that produces until it is cancelled (bounded, so a missed cancel +// ends the program instead of hanging it). +let reportCancel: (reason: string) => void = () => {}; +const endlessCancelled = new Promise((resolve) => { + reportCancel = resolve; +}); +function endlessStream(): ReadableStream { + const encoder = new TextEncoder(); + let pulls = 0; + return new ReadableStream({ + async pull(controller) { + await delay(5); + if (++pulls > 400) controller.close(); + else controller.enqueue(encoder.encode("tick\n")); + }, + cancel(reason) { + reportCancel(`cancelled with ${String(reason)}`); + }, + }); +} + +async function handler(request: Request): Promise { + const url = new URL(request.url); + switch (url.pathname) { + case "/echo": { + const body = await request.text(); + const headers = new Headers({ "content-type": "application/json" }); + headers.set("x-echo", "yes"); + return new Response( + JSON.stringify({ + method: request.method, + url: request.url.replace(/:\d+\//, ":PORT/"), + type: request.headers.get("content-type"), + agent: request.headers.get("x-agent"), + body, + }), + { status: 201, headers }, + ); + } + case "/headers-object": { + const headers = new Headers(); + headers.set("x-b", "2"); + headers.set("X-A", "1"); + headers.set("content-type", "text/html; charset=utf-8"); + return new Response("

hi

", { status: 202, headers }); + } + case "/cookies": { + const headers = new Headers(); + headers.append("set-cookie", "a=1; Path=/"); + headers.set("x-before", "b"); + headers.append("set-cookie", "b=2; HttpOnly"); + headers.set("content-type", "text/plain"); + return new Response("cookies", { headers }); + } + case "/no-content": + return new Response(null, { status: 204, headers: new Headers({ "x-empty": "1" }) }); + case "/declared-length": { + const headers = new Headers({ "content-length": "5", "content-type": "text/plain" }); + return new Response("12345", { headers }); + } + case "/ready-stream": { + const headers = new Headers({ "content-type": "text/plain" }); + return new Response(readyStream(["one ", "two"]), { headers }); + } + case "/slow-stream": { + const headers = new Headers({ "content-type": "application/x-ndjson" }); + return new Response(slowStream(['{"n":1}\n', '{"n":2}\n', '{"n":3}\n']), { headers }); + } + case "/chunked-stream": { + const headers = new Headers({ "content-type": "text/event-stream", "transfer-encoding": "chunked" }); + return new Response(readyStream(["data: a\n\n", "data: b\n\n"]), { headers }); + } + case "/bytes": { + const headers = new Headers({ "content-type": "application/octet-stream" }); + return new Response(new Uint8Array([104, 105, 33]), { headers }); + } + case "/null-body": { + return new Response(null, { status: 404, headers: new Headers({ "x-missing": "yes" }) }); + } + case "/reject": + throw new Error("handler failed"); + case "/light-text": + return new Response("light"); + case "/light-record": + return new Response("rec", { status: 201, headers: { "X-Custom": "a", "Content-Type": "text/html" } }); + case "/light-json": + return Response.json({ ok: true, n: [1, 2] }); + case "/light-json-headers": + return Response.json({ ok: true }, { status: 201, headers: { "X-Test": "yes" } }); + case "/light-json-typed": + return Response.json([1], { headers: { "Content-Type": "application/vnd.test+json" } }); + case "/endless": + return new Response(endlessStream(), { headers: new Headers({ "content-type": "text/plain" }) }); + case "/light-touched": { + const r = new Response("touched"); + r.headers.set("x-touched", "1"); + return r; + } + case "/light-record-touched": { + const r = new Response("rt", { headers: { "X-Rec": "1" } }); + r.headers.append("x-more", "2"); + return r; + } + case "/materialized": { + const r = new Response("m", { headers: { "X-M": "1" } }); + return new Response(r.body, r); + } + case "/light-stream": + return new Response(readyStream(["s1 ", "s2"])); + case "/light-null": + return new Response(null, { status: 202 }); + case "/light-length": + return new Response("abc", { headers: { "Content-Length": "3" } }); + case "/light-bytes": + return new Response(new Uint8Array([111, 107])); + default: { + const headers = new Headers({ "content-type": "text/plain; charset=UTF-8" }); + return new Response(`path ${url.pathname}${url.search}`, { headers }); + } + } +} + +interface Exchange { + method: string; + path: string; + headers?: Record; + body?: string; +} + +function exchange(port: number, ex: Exchange): Promise { + return new Promise((resolve, reject) => { + const headers: Record = { ...(ex.headers ?? {}) }; + if (ex.body !== undefined) headers["content-length"] = String(Buffer.byteLength(ex.body)); + const req = http.request( + { host: "127.0.0.1", port, method: ex.method, path: ex.path, headers: headers }, + (res) => { + const chunks: Buffer[] = []; + res.on("data", (chunk: Buffer) => { + chunks.push(chunk); + }); + res.on("end", () => { + const lines: string[] = [`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`]; + for (let i = 0; i < res.rawHeaders.length; i += 2) { + const name = res.rawHeaders[i]!; + if (name.toLowerCase() === "date") continue; + lines.push(`${name}: ${res.rawHeaders[i + 1]}`); + } + resolve(`### ${ex.method} ${ex.path}\n${lines.join("\n")}\n\n${Buffer.concat(chunks).toString("utf8")}\n`); + }); + }, + ); + req.on("error", reject); + if (ex.body !== undefined) req.write(ex.body); + req.end(); + }); +} + +const exchanges: Exchange[] = [ + { method: "GET", path: "/plain?q=1" }, + { method: "POST", path: "/echo", headers: { "content-type": "text/plain", "x-agent": "probe" }, body: "payload body" }, + { method: "PUT", path: "/echo", headers: { "content-type": "application/json" }, body: '{"k":[1,2]}' }, + { method: "GET", path: "/headers-object" }, + { method: "GET", path: "/cookies" }, + { method: "GET", path: "/no-content" }, + { method: "GET", path: "/declared-length" }, + { method: "GET", path: "/ready-stream" }, + { method: "GET", path: "/slow-stream" }, + { method: "GET", path: "/chunked-stream" }, + { method: "GET", path: "/bytes" }, + { method: "GET", path: "/null-body" }, + { method: "HEAD", path: "/plain" }, + { method: "GET", path: "/light-text" }, + { method: "HEAD", path: "/light-text" }, + { method: "GET", path: "/light-record" }, + { method: "HEAD", path: "/light-record" }, + { method: "GET", path: "/light-json" }, + { method: "GET", path: "/light-json-headers" }, + { method: "GET", path: "/light-json-typed" }, + { method: "GET", path: "/light-touched" }, + { method: "GET", path: "/light-record-touched" }, + { method: "GET", path: "/materialized" }, + { method: "GET", path: "/light-stream" }, + { method: "GET", path: "/light-null" }, + { method: "GET", path: "/light-length" }, + { method: "GET", path: "/light-bytes" }, + { method: "GET", path: "/reject" }, + { method: "GET", path: "/a/../b/./c?x=%20y" }, + { method: "GET", path: "/upper", headers: { host: "Example.COM:8080" } }, + { method: "GET", path: "/bad-host", headers: { host: "bad host" } }, + { method: "get", path: "/lower-method" }, +]; + +/** Reads the first chunk of /endless, then destroys the response: the + * server cancels the response's stream. */ +function disconnectMidStream(port: number): Promise { + return new Promise((resolve) => { + const req = http.request({ host: "127.0.0.1", port, method: "GET", path: "/endless" }, (res) => { + res.once("data", () => { + res.destroy(); + }); + }); + req.end(); + const timeout = new Promise((done) => { + setTimeout(() => done("not cancelled"), 3000); + }); + void Promise.race([endlessCancelled, timeout]).then((outcome) => { + resolve(`### disconnect GET /endless\n${outcome}\n`); + }); + }); +} + +const server = serve({ fetch: handler, port: 0, hostname: "127.0.0.1" }, (info: AddressInfo) => { + console.log("listening", info.family, info.address, typeof info.port); + void (async () => { + for (const ex of exchanges) console.log(await exchange(info.port, ex)); + console.log(await disconnectMidStream(info.port)); + server.close(() => { + console.log("closed"); + startSecond(); + }); + })(); +}); + +// getRequestListener with an error handler, on a plain node:http server. +const listener = getRequestListener( + async (request) => { + if (request.url.endsWith("/fail")) throw new RequestError("listener failure"); + return new Response("ok", { headers: new Headers({ "content-type": "text/plain" }) }); + }, + { + errorHandler: (err) => + new Response(`handled: ${err instanceof Error ? `${err.name} ${err.message}` : "?"}`, { + status: 503, + headers: new Headers({ "content-type": "text/plain" }), + }), + }, +); +function startSecond(): void { + const second = http.createServer((req, res) => { + void listener(req, res); + }); + second.listen(0, "127.0.0.1", () => { + const port = (second.address() as AddressInfo).port; + void (async () => { + console.log(await exchange(port, { method: "GET", path: "/ok" })); + console.log(await exchange(port, { method: "GET", path: "/fail" })); + second.close(() => { + console.log("second closed"); + startUploads(); + }); + })(); + }); +} + +// Request bodies stream to the handler. /size reads the whole body. The +// other routes leave it unread: /unread answers at once (the package's +// cached-response path) and /unread-async through a promise, and /reject +// answers 413 after 300 ms while the client keeps uploading, so the server +// must stop reading the socket. +const MiB = 1024 * 1024; + +interface UploadResult { + status: string; + body: string; + sentBeforeResponse: number; +} + +/** A chunked POST over a raw socket: 64 KiB chunks, each written after + * the previous write's callback, until `limit` bytes are out (then the + * last chunk, when `finish` is set) or the response arrives. */ +function chunkedUpload(port: number, path: string, limit: number, finish: boolean): Promise { + return new Promise((resolve) => { + const socket = net.connect(port, "127.0.0.1"); + const frame = Buffer.concat([Buffer.from("10000\r\n"), Buffer.alloc(65536, 97), Buffer.from("\r\n")]); + let sent = 0; + let sentBeforeResponse = -1; + let response = ""; + let settled = false; + const settle = (): void => { + if (settled) return; + settled = true; + const headEnd = response.indexOf("\r\n\r\n"); + const status = response.slice(0, response.indexOf("\r\n")); + resolve({ status, body: headEnd < 0 ? "" : response.slice(headEnd + 4), sentBeforeResponse }); + socket.destroy(); + }; + const writeNext = (): void => { + if (sentBeforeResponse >= 0 || socket.destroyed) return; + if (sent >= limit) { + if (finish) socket.write("0\r\n\r\n"); + return; + } + sent += 65536; + socket.write(frame, () => { + writeNext(); + }); + }; + socket.on("connect", () => { + socket.write(`POST ${path} HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n`); + writeNext(); + }); + socket.on("data", (chunk: Buffer) => { + response += chunk.toString("latin1"); + if (sentBeforeResponse < 0 && response.includes("\r\n\r\n")) sentBeforeResponse = sent; + const match = /content-length: (\d+)/i.exec(response); + const headEnd = response.indexOf("\r\n\r\n"); + if (match !== null && headEnd >= 0 && response.length - headEnd - 4 >= Number(match[1])) settle(); + }); + socket.on("error", () => settle()); + socket.on("close", () => settle()); + }); +} + +function startUploads(): void { + const server = serve( + { + fetch: (request: Request): Response | Promise => { + const url = new URL(request.url); + if (url.pathname === "/size") + return request.arrayBuffer().then((body) => new Response(`size ${body.byteLength}`)); + if (url.pathname === "/reject") + return delay(300).then(() => new Response("too large", { status: 413 })); + if (url.pathname === "/unread-async") return Promise.resolve(new Response("unread async")); + return new Response(`unread ${url.pathname}`); + }, + port: 0, + hostname: "127.0.0.1", + }, + (info: AddressInfo) => { + void (async () => { + const read = await chunkedUpload(info.port, "/size", 3 * MiB, true); + console.log(`### chunked POST /size\n${read.status}\n${read.body}\n`); + console.log(await exchange(info.port, { method: "POST", path: "/unread", body: "x".repeat(300000) })); + console.log(await exchange(info.port, { method: "GET", path: "/after-unread" })); + console.log(await exchange(info.port, { method: "POST", path: "/unread-async", body: "y".repeat(300000) })); + console.log(await exchange(info.port, { method: "GET", path: "/after-unread-async" })); + const before = process.memoryUsage().rss; + const rejected = await chunkedUpload(info.port, "/reject", 512 * MiB, false); + const grown = process.memoryUsage().rss - before; + console.log(`### chunked POST /reject\n${rejected.status}\n${rejected.body}`); + console.log("client stopped before 32 MiB:", rejected.sentBeforeResponse >= 0 && rejected.sentBeforeResponse < 32 * MiB); + console.log("server memory grew under 64 MiB:", grown < 64 * MiB); + server.close(() => console.log("uploads closed")); + })(); + }, + ); +} diff --git a/tests/fixtures/npm/cases/hono-node-server/package.json b/tests/fixtures/npm/cases/hono-node-server/package.json new file mode 100644 index 000000000..5ffd9800b --- /dev/null +++ b/tests/fixtures/npm/cases/hono-node-server/package.json @@ -0,0 +1 @@ +{ "type": "module" } diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/LICENSE b/tests/fixtures/npm/node_modules/@hono/node-server/LICENSE new file mode 100644 index 000000000..5f84afaff --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 - present, Yusuke Wada and Hono contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/README.md b/tests/fixtures/npm/node_modules/@hono/node-server/README.md new file mode 100644 index 000000000..25af664e8 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/README.md @@ -0,0 +1,450 @@ +# Node.js Adapter for Hono + +This adapter `@hono/node-server` allows you to run your Hono application on Node.js. +Initially, Hono wasn't designed for Node.js, but with this adapter, you can now use Hono on Node.js. It utilizes web standard APIs implemented in Node.js. + +## Benchmarks + +Hono is 3.1 times faster than Express. + +Express v5: + +```txt +$ bombardier -d 10s --fasthttp http://localhost:3000/ + +Statistics Avg Stdev Max + Reqs/sec 24380.41 1409.74 26604.88 + Latency 5.12ms 2.95ms 305.21ms + HTTP codes: + 1xx - 0, 2xx - 243915, 3xx - 0, 4xx - 0, 5xx - 0 + others - 0 + Throughput: 6.98MB/s +``` + +Hono + `@hono/node-server`: + +```txt +$ bombardier -d 10s --fasthttp http://localhost:3000/ + +Statistics Avg Stdev Max + Reqs/sec 74428.96 3815.27 88228.32 + Latency 1.68ms 621.25us 106.20ms + HTTP codes: + 1xx - 0, 2xx - 744378, 3xx - 0, 4xx - 0, 5xx - 0 + others - 0 + Throughput: 16.82MB/s +``` + +## Requirements + +It works on Node.js versions greater than 20.x. + +## Installation + +You can install it from the npm registry: + +```sh +npm install @hono/node-server +pnpm add @hono/node-server +``` + +## Usage + +Just import `@hono/node-server` at the top and write the code as usual. +The same code that runs on Cloudflare Workers, Deno, and Bun will work. + +```ts +import { serve } from '@hono/node-server' +import { Hono } from 'hono' + +const app = new Hono() +app.get('/', (c) => c.text('Hono meets Node.js')) + +serve(app, (info) => { + console.log(`Listening on http://localhost:${info.port}`) // Listening on http://localhost:3000 +}) +``` + +## WebSocket + +You can upgrade WebSocket connections with `upgradeWebSocket` from `@hono/node-server`. +To enable this, install `ws` (and `@types/ws`) in your project, then create and provide a `WebSocketServer` as shown in the example below. + +```ts +import { serve, upgradeWebSocket } from '@hono/node-server' +import { WebSocketServer } from 'ws' +import { Hono } from 'hono' + +const app = new Hono() + +app.get( + '/ws', + upgradeWebSocket(() => ({ + onMessage(event, ws) { + ws.send(event.data) + }, + })) +) + +const wss = new WebSocketServer({ noServer: true }) +serve({ + fetch: app.fetch, + websocket: { server: wss }, +}) +``` + +For example, run it using `ts-node`. Then an HTTP server will be launched. The default port is `3000`. + +```sh +ts-node ./index.ts +``` + +Open `http://localhost:3000` with your browser. + +## Options + +### `port` + +```ts +serve({ + fetch: app.fetch, + port: 8787, // Port number, default is 3000 +}) +``` + +### `createServer` + +```ts +import { createServer } from 'node:https' +import fs from 'node:fs' + +//... + +serve({ + fetch: app.fetch, + createServer: createServer, + serverOptions: { + key: fs.readFileSync('test/fixtures/keys/agent1-key.pem'), + cert: fs.readFileSync('test/fixtures/keys/agent1-cert.pem'), + }, +}) +``` + +### `overrideGlobalObjects` + +The default value is `true`. The Node.js Adapter rewrites the global Request/Response and uses a lightweight Request/Response to improve performance. If you don't want to do that, set `false`. + +```ts +serve({ + fetch: app.fetch, + overrideGlobalObjects: false, +}) +``` + +### `autoCleanupIncoming` + +The default value is `true`. The Node.js Adapter automatically cleans up (explicitly call `destroy()` method) if application is not finished to consume the incoming request. If you don't want to do that, set `false`. + +If the application accepts connections from arbitrary clients, this cleanup must be done otherwise incomplete requests from clients may cause the application to stop responding. If your application only accepts connections from trusted clients, such as in a reverse proxy environment and there is no process that returns a response without reading the body of the POST request all the way through, you can improve performance by setting it to `false`. + +```ts +serve({ + fetch: app.fetch, + autoCleanupIncoming: false, +}) +``` + +### `websocket` + +provide a websocket server to enable websocket support. + +```ts +import { serve, upgradeWebSocket } from '@hono/node-server' +import { WebSocketServer } from 'ws' + +// ... +const wss = new WebSocketServer({ noServer: true }) + +serve({ + fetch: app.fetch, + websocket: { server: wss }, +}) +``` + +## Middleware + +Most built-in middleware also works with Node.js. +Read [the documentation](https://hono.dev/middleware/builtin/basic-auth) and use the Middleware of your liking. + +```ts +import { serve } from '@hono/node-server' +import { Hono } from 'hono' +import { prettyJSON } from 'hono/pretty-json' + +const app = new Hono() + +app.get('*', prettyJSON()) +app.get('/', (c) => c.json({ 'Hono meets': 'Node.js' })) + +serve(app) +``` + +## Serve Static Middleware + +Use Serve Static Middleware that has been created for Node.js. + +```ts +import { serveStatic } from '@hono/node-server/serve-static' + +//... + +app.use('/static/*', serveStatic({ root: './' })) +``` + +If using a relative path, `root` will be relative to the current working directory from which the app was started. + +This can cause confusion when running your application locally. + +Imagine your project structure is: + +``` +my-hono-project/ + src/ + index.ts + static/ + index.html +``` + +Typically, you would run your app from the project's root directory (`my-hono-project`), +so you would need the following code to serve the `static` folder: + +```ts +app.use('/static/*', serveStatic({ root: './static' })) +``` + +Notice that `root` here is not relative to `src/index.ts`, rather to `my-hono-project`. + +Only `GET` and `HEAD` requests are served. Requests with other methods are passed to the next middleware. + +### Options + +#### `allowPercentInPath` + +By default, `serveStatic` skips request paths that still contain `%` after routing. This prevents a second decoding step from serving a file under a different path than the one checked by route middleware. Set `allowPercentInPath: true` to keep the previous behavior when serving files whose names contain percent signs. + +If you restrict access to only some paths under `root` (for example, `/static/admin/*`), enabling this option may let requests bypass those restrictions. Apply access control to the same route pattern as `serveStatic` (for example, `/static/*`), before the static middleware: + +```ts +app.use('/static/*', authMiddleware) +app.use('/static/*', serveStatic({ root: './static', allowPercentInPath: true })) +``` + +#### `rewriteRequestPath` + +If you want to serve files in `./.foojs` with the request path `/__foo/*`, you can write like the following. + +```ts +app.use( + '/__foo/*', + serveStatic({ + root: './.foojs/', + rewriteRequestPath: (path: string) => path.replace(/^\/__foo/, ''), + }) +) +``` + +#### `onFound` + +You can specify handling when the requested file is found with `onFound`. + +```ts +app.use( + '/static/*', + serveStatic({ + // ... + onFound: (_path, c) => { + c.header('Cache-Control', `public, immutable, max-age=31536000`) + }, + }) +) +``` + +#### `onNotFound` + +The `onNotFound` is useful for debugging. You can write a handle for when a file is not found. + +```ts +app.use( + '/static/*', + serveStatic({ + root: './non-existent-dir', + onNotFound: (path, c) => { + console.log(`${path} is not found, request to ${c.req.path}`) + }, + }) +) +``` + +#### `precompressed` + +The `precompressed` option checks if files with extensions like `.br` or `.gz` are available and serves them based on the `Accept-Encoding` header. It prioritizes Brotli, then Zstd, and Gzip. If none are available, it serves the original file. + +```ts +app.use( + '/static/*', + serveStatic({ + precompressed: true, + }) +) +``` + +## ConnInfo Helper + +You can use the [ConnInfo Helper](https://hono.dev/docs/helpers/conninfo) by importing `getConnInfo` from `@hono/node-server/conninfo`. + +```ts +import { getConnInfo } from '@hono/node-server/conninfo' + +app.get('/', (c) => { + const info = getConnInfo(c) // info is `ConnInfo` + return c.text(`Your remote address is ${info.remote.address}`) +}) +``` + +## Accessing Node.js API + +You can access the Node.js API from `c.env` in Node.js. For example, if you want to specify a type, you can write the following. + +```ts +import { serve } from '@hono/node-server' +import type { HttpBindings } from '@hono/node-server' +import { Hono } from 'hono' + +const app = new Hono<{ Bindings: HttpBindings }>() + +app.get('/', (c) => { + return c.json({ + remoteAddress: c.env.incoming.socket.remoteAddress, + }) +}) + +serve(app) +``` + +The APIs that you can get from `c.env` are as follows. + +```ts +type HttpBindings = { + incoming: IncomingMessage + outgoing: ServerResponse +} + +type Http2Bindings = { + incoming: Http2ServerRequest + outgoing: Http2ServerResponse +} +``` + +## Early Hints Middleware + +You can send HTTP 103 Early Hints to instruct browsers to preload or preconnect resources before the final response is prepared. The middleware is supported under Node.js bindings (HTTP/1.1 and HTTP/2). + +### Usage + +Import `earlyHints` from `@hono/node-server/early-hints`: + +#### Static links + +```ts +import { serve } from '@hono/node-server' +import { earlyHints } from '@hono/node-server/early-hints' +import { Hono } from 'hono' + +const app = new Hono() + +app.use( + earlyHints({ + link: '; rel=preload; as=style', + }) +) + +app.get('/', (c) => { + return c.html('

Hello Hono!

') +}) + +serve(app) +``` + +#### Dynamic links + +```ts +app.use( + earlyHints({ + link: (c) => + c.req.query('theme') === 'dark' + ? '; rel=preload; as=style' + : '; rel=preload; as=style', + }) +) +``` + +> [!NOTE] +> Early Hints are sent only for requests that look like document navigations. If `Sec-Fetch-Mode` or `Sec-Fetch-Dest` is present with a value other than `navigate` or `document`, for example a `fetch()` or XHR call from a browser, a subresource request, or an iframe navigation, the middleware skips the hints and continues to the handler. Requests without these headers, such as `curl` or `fetch()` from a JavaScript runtime, are treated as navigations and do receive Early Hints. + +## Direct response from Node.js API + +You can directly respond to the client from the Node.js API. +In that case, the response from Hono should be ignored, so return `RESPONSE_ALREADY_SENT`. + +> [!NOTE] +> This feature can be used when migrating existing Node.js applications to Hono, but we recommend using Hono's API for new applications. + +```ts +import { serve } from '@hono/node-server' +import type { HttpBindings } from '@hono/node-server' +import { RESPONSE_ALREADY_SENT } from '@hono/node-server/utils/response' +import { Hono } from 'hono' + +const app = new Hono<{ Bindings: HttpBindings }>() + +app.get('/', (c) => { + const { outgoing } = c.env + outgoing.writeHead(200, { 'Content-Type': 'text/plain' }) + outgoing.end('Hello World\n') + + return RESPONSE_ALREADY_SENT +}) + +serve(app) +``` + +## Listen to a UNIX domain socket + +You can configure the HTTP server to listen to a UNIX domain socket instead of a TCP port. + +```ts +import { createAdaptorServer } from '@hono/node-server' + +// ... + +const socketPath = '/tmp/example.sock' + +const server = createAdaptorServer(app) +server.listen(socketPath, () => { + console.log(`Listening on ${socketPath}`) +}) +``` + +## Related projects + +- Hono - +- Hono GitHub repository - + +## Authors + +- Yusuke Wada +- Taku Amano + +## License + +MIT diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.cjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.cjs new file mode 100644 index 000000000..b41116486 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.cjs @@ -0,0 +1,22 @@ +Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); + +//#region src/conninfo.ts +/** +* ConnInfo Helper for Node.js +* @param c Context +* @returns ConnInfo +*/ +const getConnInfo = (c) => { + const bindings = c.env.server ? c.env.server : c.env; + const address = bindings.incoming.socket.remoteAddress; + const port = bindings.incoming.socket.remotePort; + const family = bindings.incoming.socket.remoteFamily; + return { remote: { + address, + port, + addressType: family === "IPv4" ? "IPv4" : family === "IPv6" ? "IPv6" : void 0 + } }; +}; + +//#endregion +exports.getConnInfo = getConnInfo; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.d.cts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.d.cts new file mode 100644 index 000000000..ed43a48f0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.d.cts @@ -0,0 +1,11 @@ +import { GetConnInfo } from "hono/conninfo"; + +//#region src/conninfo.d.ts +/** + * ConnInfo Helper for Node.js + * @param c Context + * @returns ConnInfo + */ +declare const getConnInfo: GetConnInfo; +//#endregion +export { getConnInfo }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.d.mts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.d.mts new file mode 100644 index 000000000..ed43a48f0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.d.mts @@ -0,0 +1,11 @@ +import { GetConnInfo } from "hono/conninfo"; + +//#region src/conninfo.d.ts +/** + * ConnInfo Helper for Node.js + * @param c Context + * @returns ConnInfo + */ +declare const getConnInfo: GetConnInfo; +//#endregion +export { getConnInfo }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.mjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.mjs new file mode 100644 index 000000000..29ec46d0d --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/conninfo.mjs @@ -0,0 +1,20 @@ +//#region src/conninfo.ts +/** +* ConnInfo Helper for Node.js +* @param c Context +* @returns ConnInfo +*/ +const getConnInfo = (c) => { + const bindings = c.env.server ? c.env.server : c.env; + const address = bindings.incoming.socket.remoteAddress; + const port = bindings.incoming.socket.remotePort; + const family = bindings.incoming.socket.remoteFamily; + return { remote: { + address, + port, + addressType: family === "IPv4" ? "IPv4" : family === "IPv6" ? "IPv6" : void 0 + } }; +}; + +//#endregion +export { getConnInfo }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/constants-BLSFu_RU.mjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/constants-BLSFu_RU.mjs new file mode 100644 index 000000000..db7c31e48 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/constants-BLSFu_RU.mjs @@ -0,0 +1,5 @@ +//#region src/utils/response/constants.ts +const X_ALREADY_SENT = "x-hono-already-sent"; + +//#endregion +export { X_ALREADY_SENT as t }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/constants-BXAKTxRC.cjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/constants-BXAKTxRC.cjs new file mode 100644 index 000000000..8888a5804 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/constants-BXAKTxRC.cjs @@ -0,0 +1,11 @@ + +//#region src/utils/response/constants.ts +const X_ALREADY_SENT = "x-hono-already-sent"; + +//#endregion +Object.defineProperty(exports, 'X_ALREADY_SENT', { + enumerable: true, + get: function () { + return X_ALREADY_SENT; + } +}); \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.cjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.cjs new file mode 100644 index 000000000..aa3ceba6a --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.cjs @@ -0,0 +1,35 @@ +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; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.d.cts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.d.cts new file mode 100644 index 000000000..9bd27d5e3 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.d.cts @@ -0,0 +1,16 @@ +import { Context, Env, MiddlewareHandler } from "hono"; + +//#region src/early-hints.d.ts +type EarlyHintsOptions = { + link: string | string[] | ((c: Context) => string | string[] | undefined); +}; +/** + * 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 + */ +declare const earlyHints: (options: EarlyHintsOptions) => MiddlewareHandler; +//#endregion +export { EarlyHintsOptions, earlyHints }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.d.mts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.d.mts new file mode 100644 index 000000000..9bd27d5e3 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.d.mts @@ -0,0 +1,16 @@ +import { Context, Env, MiddlewareHandler } from "hono"; + +//#region src/early-hints.d.ts +type EarlyHintsOptions = { + link: string | string[] | ((c: Context) => string | string[] | undefined); +}; +/** + * 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 + */ +declare const earlyHints: (options: EarlyHintsOptions) => MiddlewareHandler; +//#endregion +export { EarlyHintsOptions, earlyHints }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.mjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.mjs new file mode 100644 index 000000000..7999a3d0c --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/early-hints.mjs @@ -0,0 +1,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 }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.cjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.cjs new file mode 100644 index 000000000..e578de054 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.cjs @@ -0,0 +1,1340 @@ +Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +const require_constants = require('./constants-BXAKTxRC.cjs'); +let node_http = require("node:http"); +let node_http2 = require("node:http2"); +let node_stream = require("node:stream"); +let hono_ws = require("hono/ws"); + +//#region src/error.ts +var RequestError = class extends Error { + constructor(message, options) { + super(message, options); + this.name = "RequestError"; + } +}; + +//#endregion +//#region src/headers.ts +const nonJoinedHeaders = new Set([ + "age", + "authorization", + "content-length", + "content-type", + "etag", + "expires", + "from", + "host", + "if-modified-since", + "if-unmodified-since", + "last-modified", + "location", + "max-forwards", + "proxy-authorization", + "referer", + "retry-after", + "server", + "user-agent" +]); +const validHeaderName = /^[!#$%&'*+\-.^_`|~\dA-Za-z]+$/; +const isHttpWhitespace = (code) => code === 9 || code === 10 || code === 13 || code === 32; +const normalizeHeaderValue = (value) => { + if (!isHttpWhitespace(value.charCodeAt(0)) && !isHttpWhitespace(value.charCodeAt(value.length - 1))) return value; + let start = 0; + let end = value.length; + while (start < end && isHttpWhitespace(value.charCodeAt(start))) start++; + while (end > start && isHttpWhitespace(value.charCodeAt(end - 1))) end--; + return value.slice(start, end); +}; +const forbiddenHeaderValue = /[\0\r\n]/; +const GlobalHeaders = globalThis.Headers; +const materializeHeaders = (rawHeaders, HeadersCtor = GlobalHeaders) => { + const headers = new HeadersCtor(); + for (let i = 0; i < rawHeaders.length; i += 2) { + const name = rawHeaders[i]; + if (!name.startsWith(":")) headers.append(name, rawHeaders[i + 1]); + } + return headers; +}; +var RequestHeaders = class { + #incoming; + #rawHeaders; + #headers; + #invalidValue; + constructor(incoming) { + this.#incoming = incoming; + if (incoming instanceof node_http2.Http2ServerRequest) this.#rawHeaders = incoming.rawHeaders.slice(); + } + get #lazyRawHeaders() { + return this.#rawHeaders ??= this.#incoming.rawHeaders.slice(); + } + get #native() { + if (!this.#headers) { + this.#headers = materializeHeaders(this.#lazyRawHeaders); + this.#rawHeaders = void 0; + } + return this.#headers; + } + #normalizedName(name) { + if (typeof name !== "string") return; + if (!validHeaderName.test(name)) throw new TypeError(`Invalid header name: ${name}`); + return name.toLowerCase(); + } + #lookupHttp1(lowerName) { + const headers = this.#incoming instanceof node_http2.Http2ServerRequest ? void 0 : this.#incoming.headers; + if (!headers || nonJoinedHeaders.has(lowerName) || lowerName === "set-cookie" || lowerName === "__proto__") return; + if (!Object.hasOwn(headers, lowerName)) return null; + const rawValue = headers[lowerName]; + if (typeof rawValue === "string") { + const value = normalizeHeaderValue(rawValue); + return forbiddenHeaderValue.test(value) ? void 0 : value; + } + } + #lookup(rawHeaders, lowerName) { + const separator = lowerName === "cookie" ? "; " : ", "; + let value = null; + for (let i = 0; i < rawHeaders.length; i += 2) { + const rawName = rawHeaders[i]; + if (rawName.length === lowerName.length && rawName.toLowerCase() === lowerName) { + const rawValue = normalizeHeaderValue(rawHeaders[i + 1]); + if (forbiddenHeaderValue.test(rawValue)) { + this.#invalidValue = true; + return; + } + value = value === null ? rawValue : value + separator + rawValue; + } + } + return value; + } + append(name, value) { + this.#native.append(name, value); + } + delete(name) { + this.#native.delete(name); + } + get(name) { + const lowerName = this.#normalizedName(name); + if (lowerName && !this.#headers && !this.#invalidValue) { + const http1Value = this.#lookupHttp1(lowerName); + if (http1Value !== void 0) return http1Value; + const value = this.#lookup(this.#lazyRawHeaders, lowerName); + if (value !== void 0) return value; + } + return this.#native.get(name); + } + has(name) { + const lowerName = this.#normalizedName(name); + if (lowerName && !this.#headers && !this.#invalidValue) { + const http1Value = this.#lookupHttp1(lowerName); + if (http1Value !== void 0) return http1Value !== null; + const value = this.#lookup(this.#lazyRawHeaders, lowerName); + if (value !== void 0) return value !== null; + } + return this.#native.has(name); + } + set(name, value) { + this.#native.set(name, value); + } + getSetCookie() { + return this.#native.getSetCookie(); + } + keys() { + return this.#native.keys(); + } + values() { + return this.#native.values(); + } + entries() { + return this.#native.entries(); + } + forEach(callback, thisArg) { + this.#native.forEach((value, key) => { + callback.call(thisArg, value, key, this); + }); + } + [Symbol.iterator]() { + return this.entries(); + } +}; +Object.defineProperty(RequestHeaders.prototype, Symbol.for("nodejs.util.inspect.custom"), { value: function(depth, options, inspectFn) { + return `Headers (lightweight) ${inspectFn(Object.fromEntries(this), { + ...options, + depth: depth == null ? null : depth - 1 + })}`; +} }); +Object.setPrototypeOf(RequestHeaders.prototype, GlobalHeaders.prototype); +const newHeadersFromIncoming = (incoming) => globalThis.Headers === GlobalHeaders ? new RequestHeaders(incoming) : materializeHeaders(incoming.rawHeaders, globalThis.Headers); + +//#endregion +//#region src/url.ts +const reValidRequestUrl = /^\/[!#$&-;=?-\[\]_a-z~]*$/; +const reDotSegment = /\/\.\.?(?:[/?#]|$)/; +const reValidHost = /^[a-z0-9._-]+(?::(?:[1-5]\d{3,4}|[6-9]\d{3}))?$/; +const buildUrl = (scheme, host, incomingUrl) => { + const url = `${scheme}://${host}${incomingUrl}`; + if (!reValidHost.test(host)) { + const urlObj = new URL(url); + if (urlObj.hostname.length !== host.length && urlObj.hostname !== (host.includes(":") ? host.replace(/:\d+$/, "") : host).toLowerCase()) throw new RequestError("Invalid host header"); + return urlObj.href; + } else if (incomingUrl.length === 0) return url + "/"; + else { + if (incomingUrl.charCodeAt(0) !== 47) throw new RequestError("Invalid URL"); + if (!reValidRequestUrl.test(incomingUrl) || reDotSegment.test(incomingUrl)) return new URL(url).href; + return url; + } +}; + +//#endregion +//#region src/request.ts +const toRequestError = (e) => { + if (e instanceof RequestError) return e; + return new RequestError(e.message, { cause: e }); +}; +const GlobalRequest = global.Request; +var Request$1 = class extends GlobalRequest { + constructor(input, options) { + if (typeof input === "object" && getRequestCache in input) { + const hasReplacementBody = options !== void 0 && "body" in options && options.body != null; + if (input[bodyConsumedDirectlyKey] && !hasReplacementBody) throw new TypeError("Cannot construct a Request with a Request object that has already been used."); + input = input[getRequestCache](); + } + if (typeof (options?.body)?.getReader !== "undefined") options.duplex ??= "half"; + super(input, options); + } +}; +const wrapBodyStream = Symbol("wrapBodyStream"); +const byteExactEncodings = new Set([ + "latin1", + "binary", + "hex", + "base64", + "base64url" +]); +const isByteExactEncoding = (encoding) => encoding === null || byteExactEncodings.has(encoding); +const bodyBufferedBeforeDisconnectKey = Symbol("bodyBufferedBeforeDisconnect"); +const bodyBufferedLengthBeforeDisconnectKey = Symbol("bodyBufferedLengthBeforeDisconnect"); +const toBufferChunk = (chunk, encoding) => Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding ?? "utf8"); +const isRecoverableDisconnectedIncoming = (incoming) => !(incoming instanceof node_http2.Http2ServerRequest) && !!incoming.complete && !!incoming.readableAborted && typeof incoming.read === "function" && isByteExactEncoding(incoming.readableEncoding); +const recordBodyBufferedBeforeDisconnect = (incoming) => { + if (incoming.readableDidRead || !isRecoverableDisconnectedIncoming(incoming)) return; + const incomingWithRecovery = incoming; + incomingWithRecovery[bodyBufferedLengthBeforeDisconnectKey] ??= incoming.readableLength; +}; +const readBodyBufferedBeforeDisconnect = (incoming, chunks) => { + if (incoming.readableDidRead && !chunks || !isRecoverableDisconnectedIncoming(incoming)) return; + const incomingWithRecovery = incoming; + if (incomingWithRecovery[bodyBufferedBeforeDisconnectKey] !== void 0) return incomingWithRecovery[bodyBufferedBeforeDisconnectKey]; + let result; + const errored = incoming.errored; + if (errored && errored.code !== "ECONNRESET") result = errored; + else if (incomingWithRecovery[bodyBufferedLengthBeforeDisconnectKey] !== void 0 && incoming.readableLength !== incomingWithRecovery[bodyBufferedLengthBeforeDisconnectKey]) result = newBodyUnusableError(); + else { + const bodyChunks = chunks ?? []; + const chunk = incoming.read(); + if (chunk !== null) bodyChunks.push(toBufferChunk(chunk, incoming.readableEncoding)); + const buffer = bodyChunks.length === 1 ? bodyChunks[0] : Buffer.concat(bodyChunks); + result = buffer; + const contentLength = incoming.headers["content-length"]; + if (typeof contentLength === "string" && /^\d+$/.test(contentLength)) { + const expectedLength = Number(contentLength); + if (Number.isSafeInteger(expectedLength) && buffer.length !== expectedLength) result = newBodyUnusableError(); + } + } + incomingWithRecovery[bodyBufferedBeforeDisconnectKey] = result; + return result; +}; +const enqueueBufferedBody = (controller, buffered) => { + if (buffered instanceof Error) { + controller.error(buffered); + return; + } + if (buffered.length > 0) controller.enqueue(buffered); + controller.close(); +}; +const newRequestFromIncoming = (method, url, headers, incoming, abortController) => { + const init = { + method, + headers, + signal: abortController.signal + }; + if (method === "TRACE") { + init.method = "GET"; + const req = new Request$1(url, init); + Object.defineProperty(req, "method", { get() { + return "TRACE"; + } }); + return req; + } + if (!(method === "GET" || method === "HEAD")) if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) init.body = new ReadableStream({ start(controller) { + controller.enqueue(incoming.rawBody); + controller.close(); + } }); + else if (incoming[wrapBodyStream]) { + let reader; + init.body = new ReadableStream({ async pull(controller) { + try { + if (!reader) { + const buffered = readBodyBufferedBeforeDisconnect(incoming); + if (buffered !== void 0) { + enqueueBufferedBody(controller, buffered); + return; + } + } + reader ||= node_stream.Readable.toWeb(incoming).getReader(); + const { done, value } = await reader.read(); + if (done) controller.close(); + else controller.enqueue(value); + } catch (error) { + controller.error(error); + } + } }); + } else { + const buffered = readBodyBufferedBeforeDisconnect(incoming); + if (buffered !== void 0) init.body = new ReadableStream({ start(controller) { + enqueueBufferedBody(controller, buffered); + } }); + else init.body = node_stream.Readable.toWeb(incoming); + } + return new Request$1(url, init); +}; +const getRequestCache = Symbol("getRequestCache"); +const requestCache = Symbol("requestCache"); +const incomingKey = Symbol("incomingKey"); +const urlKey = Symbol("urlKey"); +const methodKey = Symbol("methodKey"); +const headersKey = Symbol("headersKey"); +const abortControllerKey = Symbol("abortControllerKey"); +const getAbortController = Symbol("getAbortController"); +const abortRequest = Symbol("abortRequest"); +const bodyBufferKey = Symbol("bodyBuffer"); +const bodyReadPromiseKey = Symbol("bodyReadPromise"); +const bodyConsumedDirectlyKey = Symbol("bodyConsumedDirectly"); +const bodyLockReaderKey = Symbol("bodyLockReader"); +const abortReasonKey = Symbol("abortReason"); +const newBodyUnusableError = () => { + return /* @__PURE__ */ new TypeError("Body is unusable"); +}; +const rejectBodyUnusable = () => { + return Promise.reject(newBodyUnusableError()); +}; +const textDecoder = new TextDecoder(); +const consumeBodyDirectOnce = (request) => { + if (request[bodyConsumedDirectlyKey]) return rejectBodyUnusable(); + request[bodyConsumedDirectlyKey] = true; +}; +const toArrayBuffer = (buf) => { + return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); +}; +const contentType = (request) => { + return (request[headersKey] ||= newHeadersFromIncoming(request[incomingKey])).get("content-type") || ""; +}; +const methodTokenRegExp = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; +const normalizeIncomingMethod = (method) => { + if (typeof method !== "string" || method.length === 0) return "GET"; + switch (method) { + case "DELETE": + case "GET": + case "HEAD": + case "OPTIONS": + case "PATCH": + case "POST": + case "PUT": + case "QUERY": return method; + } + const upper = method.toUpperCase(); + switch (upper) { + case "DELETE": + case "GET": + case "HEAD": + case "OPTIONS": + case "POST": + case "PUT": return upper; + default: return method; + } +}; +const validateDirectReadMethod = (method) => { + if (!methodTokenRegExp.test(method)) return /* @__PURE__ */ new TypeError(`'${method}' is not a valid HTTP method.`); + const normalized = method.toUpperCase(); + if (normalized === "CONNECT" || normalized === "TRACK" || normalized === "TRACE" && method !== "TRACE") return /* @__PURE__ */ new TypeError(`'${method}' HTTP method is unsupported.`); +}; +const readBodyWithFastPath = (request, method, fromBuffer) => { + if (request[bodyConsumedDirectlyKey]) return rejectBodyUnusable(); + const methodName = request.method; + if (methodName === "GET" || methodName === "HEAD") return request[getRequestCache]()[method](); + const methodValidationError = validateDirectReadMethod(methodName); + if (methodValidationError) return Promise.reject(methodValidationError); + if (request[requestCache]) { + if (methodName !== "TRACE") return request[requestCache][method](); + } + const alreadyUsedError = consumeBodyDirectOnce(request); + if (alreadyUsedError) return alreadyUsedError; + const raw = readRawBodyIfAvailable(request); + if (raw) { + const result = Promise.resolve(fromBuffer(raw, request)); + request[bodyBufferKey] = void 0; + return result; + } + return readBodyDirect(request).then((buf) => { + const result = fromBuffer(buf, request); + request[bodyBufferKey] = void 0; + return result; + }); +}; +const readRawBodyIfAvailable = (request) => { + const incoming = request[incomingKey]; + if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) return incoming.rawBody; +}; +const normalizeAbortError = (request, incoming) => { + if (incoming.errored) return incoming.errored; + const reason = request[abortReasonKey]; + if (reason !== void 0) return reason instanceof Error ? reason : new Error(String(reason)); + return /* @__PURE__ */ new Error("Client connection prematurely closed."); +}; +const readBodyDirect = (request) => { + if (request[bodyBufferKey]) return Promise.resolve(request[bodyBufferKey]); + if (request[bodyReadPromiseKey]) return request[bodyReadPromiseKey]; + const incoming = request[incomingKey]; + if (incoming.readableDidRead) return rejectBodyUnusable(); + const buffered = readBodyBufferedBeforeDisconnect(incoming); + if (buffered !== void 0) { + if (buffered instanceof Error) return Promise.reject(buffered); + request[bodyBufferKey] = buffered; + return Promise.resolve(buffered); + } + const promise = new Promise((resolve, reject) => { + const chunks = []; + let settled = false; + const finish = (callback) => { + if (settled) return; + settled = true; + cleanup(); + callback(); + }; + const recoverCompleteBodyAfterDisconnect = (error) => { + const streamError = incoming.errored ?? error; + if (!isRecoverableDisconnectedIncoming(incoming) || streamError && streamError.code !== "ECONNRESET") return false; + finish(() => { + const recovered = readBodyBufferedBeforeDisconnect(incoming, chunks); + if (recovered instanceof Error) reject(recovered); + else if (recovered === void 0) reject(error ?? normalizeAbortError(request, incoming)); + else { + request[bodyBufferKey] = recovered; + resolve(recovered); + } + }); + return true; + }; + const onData = (chunk) => { + chunks.push(toBufferChunk(chunk, incoming.readableEncoding)); + }; + const onEnd = () => { + finish(() => { + const buffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks); + request[bodyBufferKey] = buffer; + resolve(buffer); + }); + }; + const onError = (error) => { + if (recoverCompleteBodyAfterDisconnect(error)) return; + finish(() => { + reject(error); + }); + }; + const onClose = () => { + if (incoming.readableEnded) { + onEnd(); + return; + } + if (recoverCompleteBodyAfterDisconnect()) return; + finish(() => { + reject(normalizeAbortError(request, incoming)); + }); + }; + const cleanup = () => { + incoming.off("data", onData); + incoming.off("end", onEnd); + incoming.off("error", onError); + incoming.off("close", onClose); + request[bodyReadPromiseKey] = void 0; + }; + incoming.on("data", onData); + incoming.on("end", onEnd); + incoming.on("error", onError); + incoming.on("close", onClose); + queueMicrotask(() => { + if (settled) return; + if (incoming.readableEnded) onEnd(); + else if (incoming.errored) onError(incoming.errored); + else if (incoming.destroyed) onClose(); + }); + }); + request[bodyReadPromiseKey] = promise; + return promise; +}; +const requestPrototype = { + get method() { + return this[methodKey]; + }, + get url() { + return this[urlKey]; + }, + get headers() { + return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]); + }, + [abortRequest](reason) { + if (this[abortReasonKey] === void 0) this[abortReasonKey] = reason; + const abortController = this[abortControllerKey]; + if (abortController && !abortController.signal.aborted) abortController.abort(reason); + }, + [getAbortController]() { + this[abortControllerKey] ||= new AbortController(); + if (this[abortReasonKey] !== void 0 && !this[abortControllerKey].signal.aborted) this[abortControllerKey].abort(this[abortReasonKey]); + return this[abortControllerKey]; + }, + [getRequestCache]() { + const abortController = this[getAbortController](); + if (this[requestCache]) return this[requestCache]; + const method = this.method; + if (this[bodyConsumedDirectlyKey] && !(method === "GET" || method === "HEAD")) { + this[bodyBufferKey] = void 0; + const init = { + method: method === "TRACE" ? "GET" : method, + headers: this.headers, + signal: abortController.signal + }; + if (method !== "TRACE") { + init.body = new ReadableStream({ start(c) { + c.close(); + } }); + init.duplex = "half"; + } + const req = new Request$1(this[urlKey], init); + if (method === "TRACE") Object.defineProperty(req, "method", { get() { + return "TRACE"; + } }); + return this[requestCache] = req; + } + return this[requestCache] = newRequestFromIncoming(this.method, this[urlKey], this.headers, this[incomingKey], abortController); + }, + get body() { + if (!this[bodyConsumedDirectlyKey]) return this[getRequestCache]().body; + const request = this[getRequestCache](); + if (!this[bodyLockReaderKey] && request.body) this[bodyLockReaderKey] = request.body.getReader(); + return request.body; + }, + get bodyUsed() { + if (this[bodyConsumedDirectlyKey]) return true; + if (this[requestCache]) return this[requestCache].bodyUsed; + return false; + } +}; +Object.defineProperty(requestPrototype, "signal", { get() { + return this[getAbortController]().signal; +} }); +[ + "cache", + "credentials", + "destination", + "integrity", + "mode", + "redirect", + "referrer", + "referrerPolicy", + "keepalive" +].forEach((k) => { + Object.defineProperty(requestPrototype, k, { get() { + return this[getRequestCache]()[k]; + } }); +}); +["clone", "formData"].forEach((k) => { + Object.defineProperty(requestPrototype, k, { value: function() { + if (this[bodyConsumedDirectlyKey]) { + if (k === "clone") throw newBodyUnusableError(); + return rejectBodyUnusable(); + } + return this[getRequestCache]()[k](); + } }); +}); +Object.defineProperty(requestPrototype, "text", { value: function() { + return readBodyWithFastPath(this, "text", (buf) => textDecoder.decode(buf)); +} }); +Object.defineProperty(requestPrototype, "arrayBuffer", { value: function() { + return readBodyWithFastPath(this, "arrayBuffer", (buf) => toArrayBuffer(buf)); +} }); +Object.defineProperty(requestPrototype, "blob", { value: function() { + return readBodyWithFastPath(this, "blob", (buf, request) => { + const type = contentType(request); + const init = type ? { headers: { "content-type": type } } : void 0; + return new Response(buf, init).blob(); + }); +} }); +Object.defineProperty(requestPrototype, "json", { value: function() { + if (this[bodyConsumedDirectlyKey]) return rejectBodyUnusable(); + return this.text().then(JSON.parse); +} }); +Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), { value: function(depth, options, inspectFn) { + return `Request (lightweight) ${inspectFn({ + method: this.method, + url: this.url, + headers: this.headers, + nativeRequest: this[requestCache] + }, { + ...options, + depth: depth == null ? null : depth - 1 + })}`; +} }); +Object.setPrototypeOf(requestPrototype, Request$1.prototype); +const newRequest = (incoming, defaultHostname) => { + const req = Object.create(requestPrototype); + req[incomingKey] = incoming; + req[methodKey] = normalizeIncomingMethod(incoming.method); + const incomingUrl = incoming.url || ""; + if (incomingUrl[0] !== "/" && (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) { + if (incoming instanceof node_http2.Http2ServerRequest) throw new RequestError("Absolute URL for :path is not allowed in HTTP/2"); + try { + req[urlKey] = new URL(incomingUrl).href; + } catch (e) { + throw new RequestError("Invalid absolute URL", { cause: e }); + } + return req; + } + const host = (incoming instanceof node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname; + if (!host) throw new RequestError("Missing host header"); + let scheme; + if (incoming instanceof node_http2.Http2ServerRequest) { + scheme = incoming.scheme; + if (!(scheme === "http" || scheme === "https")) throw new RequestError("Unsupported scheme"); + } else scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http"; + try { + req[urlKey] = buildUrl(scheme, host, incomingUrl); + } catch (e) { + if (e instanceof RequestError) throw e; + else throw new RequestError("Invalid URL", { cause: e }); + } + return req; +}; + +//#endregion +//#region src/response.ts +const defaultContentType = "text/plain; charset=UTF-8"; +const responseCache = Symbol("responseCache"); +const getResponseCache = Symbol("getResponseCache"); +const cacheKey = Symbol("cache"); +const GlobalResponse = global.Response; +var Response$1 = class Response$1 { + #body; + #init; + [getResponseCache]() { + const cache = this[cacheKey]; + const liveHeaders = cache && cache[2] instanceof Headers ? cache[2] : void 0; + delete this[cacheKey]; + return this[responseCache] ||= new GlobalResponse(this.#body, liveHeaders ? { + status: this.#init?.status, + statusText: this.#init?.statusText, + headers: liveHeaders + } : this.#init); + } + constructor(body, init) { + let headers; + this.#body = body; + if (init instanceof GlobalResponse) { + const cachedGlobalResponse = init[responseCache]; + if (cachedGlobalResponse) { + this.#init = cachedGlobalResponse; + this[getResponseCache](); + return; + } + this.#init = init instanceof Response$1 ? init.#init : init; + headers = new Headers(init.headers); + } else this.#init = init; + if (body == null || typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) this[cacheKey] = [ + init?.status || 200, + body ?? null, + headers || init?.headers + ]; + } + get headers() { + const cache = this[cacheKey]; + if (cache) { + if (!(cache[2] instanceof Headers)) cache[2] = new Headers(cache[2] || (cache[1] === null ? void 0 : { "content-type": defaultContentType })); + return cache[2]; + } + return this[getResponseCache]().headers; + } + get status() { + return this[cacheKey]?.[0] ?? this[getResponseCache]().status; + } + get ok() { + const status = this.status; + return status >= 200 && status < 300; + } +}; +[ + "body", + "bodyUsed", + "redirected", + "statusText", + "trailers", + "type", + "url" +].forEach((k) => { + Object.defineProperty(Response$1.prototype, k, { get() { + return this[getResponseCache]()[k]; + } }); +}); +[ + "arrayBuffer", + "blob", + "clone", + "formData", + "json", + "text" +].forEach((k) => { + Object.defineProperty(Response$1.prototype, k, { value: function() { + return this[getResponseCache]()[k](); + } }); +}); +Object.defineProperty(Response$1.prototype, Symbol.for("nodejs.util.inspect.custom"), { value: function(depth, options, inspectFn) { + return `Response (lightweight) ${inspectFn({ + status: this.status, + headers: this.headers, + ok: this.ok, + nativeResponse: this[responseCache] + }, { + ...options, + depth: depth == null ? null : depth - 1 + })}`; +} }); +Object.setPrototypeOf(Response$1, GlobalResponse); +Object.setPrototypeOf(Response$1.prototype, GlobalResponse.prototype); +const validRedirectUrl = /^https?:\/\/[!#-;=?-[\]_a-z~A-Z]+$/; +const parseRedirectUrl = (url) => { + if (url instanceof URL) return url.href; + if (validRedirectUrl.test(url)) return url; + return new URL(url).href; +}; +const validRedirectStatuses = new Set([ + 301, + 302, + 303, + 307, + 308 +]); +Object.defineProperty(Response$1, "redirect", { + value: function redirect(url, status = 302) { + if (!validRedirectStatuses.has(status)) throw new RangeError("Invalid status code"); + return new Response$1(null, { + status, + headers: { location: parseRedirectUrl(url) } + }); + }, + writable: true, + configurable: true +}); +Object.defineProperty(Response$1, "json", { + value: function json(data, init) { + const body = JSON.stringify(data); + if (body === void 0) throw new TypeError("The data is not JSON serializable"); + const initHeaders = init?.headers; + let headers; + if (initHeaders) { + headers = new Headers(initHeaders); + if (!headers.has("content-type")) headers.set("content-type", "application/json"); + } else headers = { "content-type": "application/json" }; + return new Response$1(body, { + status: init?.status ?? 200, + statusText: init?.statusText, + headers + }); + }, + writable: true, + configurable: true +}); + +//#endregion +//#region src/utils.ts +async function readWithoutBlocking(readPromise) { + return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]); +} +function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) { + const cancel = (error) => { + reader.cancel(error).catch(() => {}); + }; + writable.on("close", cancel); + writable.on("error", cancel); + (currentReadPromise ?? reader.read()).then(flow, handleStreamError); + return reader.closed.finally(() => { + writable.off("close", cancel); + writable.off("error", cancel); + }); + function handleStreamError(error) { + if (error) writable.destroy(error); + } + function onDrain() { + reader.read().then(flow, handleStreamError); + } + function flow({ done, value }) { + try { + if (done) writable.end(); + else if (!writable.write(value)) writable.once("drain", onDrain); + else return reader.read().then(flow, handleStreamError); + } catch (e) { + handleStreamError(e); + } + } +} +function writeFromReadableStream(stream, writable) { + if (stream.locked) throw new TypeError("ReadableStream is locked."); + else if (writable.destroyed) return; + return writeFromReadableStreamDefaultReader(stream.getReader(), writable); +} +const buildOutgoingHttpHeaders = (headers, defaultContentType) => { + const res = {}; + if (!(headers instanceof Headers)) headers = new Headers(headers ?? void 0); + if (headers.has("set-cookie")) { + const cookies = []; + for (const [k, v] of headers) if (k === "set-cookie") cookies.push(v); + else res[k] = v; + if (cookies.length > 0) res["set-cookie"] = cookies; + } else for (const [k, v] of headers) res[k] = v; + if (defaultContentType) res["content-type"] ??= defaultContentType; + return res; +}; + +//#endregion +//#region src/listener.ts +const outgoingEnded = Symbol("outgoingEnded"); +const incomingDraining = Symbol("incomingDraining"); +const DRAIN_TIMEOUT_MS = 500; +const MAX_DRAIN_BYTES = 64 * 1024 * 1024; +const drainIncoming = (incoming) => { + const incomingWithDrainState = incoming; + if (incoming.destroyed || incomingWithDrainState[incomingDraining]) return; + incomingWithDrainState[incomingDraining] = true; + if (incoming instanceof node_http2.Http2ServerRequest) { + try { + incoming.stream?.close?.(node_http2.constants.NGHTTP2_NO_ERROR); + } catch {} + return; + } + let bytesRead = 0; + const cleanup = () => { + clearTimeout(timer); + incoming.off("data", onData); + incoming.off("end", cleanup); + incoming.off("error", cleanup); + }; + const forceClose = () => { + cleanup(); + const socket = incoming.socket; + if (socket && !socket.destroyed) { + if (typeof socket.destroySoon === "function") socket.destroySoon(); + else if (typeof socket.destroy === "function") socket.destroy(); + } + }; + const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS); + timer.unref?.(); + const onData = (chunk) => { + bytesRead += chunk.length; + if (bytesRead > MAX_DRAIN_BYTES) forceClose(); + }; + incoming.on("data", onData); + incoming.on("end", cleanup); + incoming.on("error", cleanup); + incoming.resume(); +}; +const makeCloseHandler = (req, incoming, outgoing, needsBodyCleanup) => () => { + if (incoming.errored) { + recordBodyBufferedBeforeDisconnect(incoming); + req[abortRequest](incoming.errored.toString()); + } else if (!outgoing.writableFinished || incoming instanceof node_http2.Http2ServerRequest && incoming.aborted) { + recordBodyBufferedBeforeDisconnect(incoming); + req[abortRequest]("Client connection prematurely closed."); + } + if (needsBodyCleanup && !incoming.readableEnded) setTimeout(() => { + if (!incoming.readableEnded) setTimeout(() => { + drainIncoming(incoming); + }); + }); +}; +const isImmediateCacheableResponse = (res) => { + if (!(cacheKey in res)) return false; + const body = res[cacheKey][1]; + return body === null || typeof body === "string" || body instanceof Uint8Array; +}; +const handleRequestError = () => new Response(null, { status: 400 }); +const handleFetchError = (e) => new Response(null, { status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500 }); +const handleResponseError = (e, outgoing) => { + const err = e instanceof Error ? e : new Error("unknown error", { cause: e }); + if (err.code === "ERR_STREAM_PREMATURE_CLOSE") console.info("The user aborted a request."); + else { + console.error(e); + if (!outgoing.headersSent) { + if (outgoing instanceof node_http.ServerResponse) outgoing._contentLength = null; + outgoing.writeHead(500, { "Content-Type": "text/plain" }); + } + outgoing.end(`Error: ${err.message}`); + outgoing.destroy(err); + } +}; +const flushHeaders = (outgoing) => { + if ("flushHeaders" in outgoing && outgoing.writable) outgoing.flushHeaders(); +}; +const trySetContentLength = (outgoing, status, length) => { + const http1 = outgoing; + if (http1._contentLength === null && http1._hasBody && http1.useChunkedEncodingByDefault && !http1._removedContLen && status >= 200 && status !== 204 && status !== 304 && !outgoing.hasHeader("content-length") && !outgoing.hasHeader("transfer-encoding") && !outgoing.hasHeader("trailer")) { + http1._contentLength = length; + return true; + } + return false; +}; +const writeDefaultHeaders = (outgoing, status, length) => { + if (trySetContentLength(outgoing, status, length)) outgoing.writeHead(status, { "Content-Type": defaultContentType }); + else outgoing.writeHead(status, { + "Content-Type": defaultContentType, + "Content-Length": length + }); +}; +const responseViaCache = async (res, outgoing) => { + let [status, body, header] = res[cacheKey]; + if (!header) { + if (body === null) { + outgoing.writeHead(status); + outgoing.end(); + } else if (typeof body === "string") { + writeDefaultHeaders(outgoing, status, Buffer.byteLength(body)); + outgoing.end(body); + } else if (body instanceof Uint8Array) { + writeDefaultHeaders(outgoing, status, body.byteLength); + outgoing.end(body); + } else if (body instanceof Blob) { + writeDefaultHeaders(outgoing, status, body.size); + outgoing.end(new Uint8Array(await body.arrayBuffer())); + } else { + outgoing.writeHead(status, { "Content-Type": defaultContentType }); + flushHeaders(outgoing); + await writeFromReadableStream(body, outgoing)?.catch((e) => handleResponseError(e, outgoing)); + } + outgoing[outgoingEnded]?.(); + return; + } + let hasContentLength = false; + let plainHeaders = false; + let canAutoLength = true; + if (header instanceof Headers) { + hasContentLength = header.has("content-length"); + header = buildOutgoingHttpHeaders(header, body === null ? void 0 : defaultContentType); + } else if (Array.isArray(header)) { + const headerObj = new Headers(header); + hasContentLength = headerObj.has("content-length"); + header = buildOutgoingHttpHeaders(headerObj, body === null ? void 0 : defaultContentType); + } else { + plainHeaders = true; + for (const key in header) { + if (key.length === 14 && key.toLowerCase() === "content-length") { + hasContentLength = true; + break; + } + if (key.length === 17 && key.toLowerCase() === "transfer-encoding" || key.length === 7 && key.toLowerCase() === "trailer") canAutoLength = false; + } + } + if (!hasContentLength) { + let length; + if (typeof body === "string") length = Buffer.byteLength(body); + else if (body instanceof Uint8Array) length = body.byteLength; + else if (body instanceof Blob) length = body.size; + if (length !== void 0 && (!plainHeaders || !canAutoLength || !trySetContentLength(outgoing, status, length))) { + if (plainHeaders) header = { ...header }; + header["Content-Length"] = length; + } + } + outgoing.writeHead(status, header); + if (body == null) outgoing.end(); + else if (typeof body === "string" || body instanceof Uint8Array) outgoing.end(body); + else if (body instanceof Blob) outgoing.end(new Uint8Array(await body.arrayBuffer())); + else { + flushHeaders(outgoing); + await writeFromReadableStream(body, outgoing)?.catch((e) => handleResponseError(e, outgoing)); + } + outgoing[outgoingEnded]?.(); +}; +const isPromise = (res) => typeof res.then === "function"; +const responseViaResponseObject = async (res, outgoing, options = {}) => { + if (isPromise(res)) if (options.errorHandler) try { + res = await res; + } catch (err) { + const errRes = await options.errorHandler(err); + if (!errRes) return; + res = errRes; + } + else res = await res.catch(handleFetchError); + if (cacheKey in res) return responseViaCache(res, outgoing); + const resHeaderRecord = buildOutgoingHttpHeaders(res.headers, res.body === null ? void 0 : defaultContentType); + if (res.body) { + const reader = res.body.getReader(); + const values = []; + let done = false; + let currentReadPromise = void 0; + if (resHeaderRecord["transfer-encoding"] !== "chunked") { + let maxReadCount = 2; + for (let i = 0; i < maxReadCount; i++) { + currentReadPromise ||= reader.read(); + const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => { + console.error(e); + done = true; + }); + if (!chunk) { + if (i === 1) { + await new Promise((resolve) => setTimeout(resolve)); + maxReadCount = 3; + continue; + } + break; + } + currentReadPromise = void 0; + if (chunk.value) values.push(chunk.value); + if (chunk.done) { + done = true; + break; + } + } + if (done && !("content-length" in resHeaderRecord)) resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0); + } + outgoing.writeHead(res.status, resHeaderRecord); + values.forEach((value) => { + outgoing.write(value); + }); + if (done) outgoing.end(); + else { + if (values.length === 0) flushHeaders(outgoing); + await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise); + } + } else if (resHeaderRecord[require_constants.X_ALREADY_SENT]) {} else { + outgoing.writeHead(res.status, resHeaderRecord); + outgoing.end(); + } + outgoing[outgoingEnded]?.(); +}; +const getRequestListener = (fetchCallback, options = {}) => { + const autoCleanupIncoming = options.autoCleanupIncoming ?? true; + if (options.overrideGlobalObjects !== false && global.Request !== Request$1) { + Object.defineProperty(global, "Request", { value: Request$1 }); + Object.defineProperty(global, "Response", { value: Response$1 }); + } + return async (incoming, outgoing) => { + let res, req; + let needsBodyCleanup = false; + let closeHandlerAttached = false; + const ensureCloseHandler = () => { + if (!req || closeHandlerAttached) return; + closeHandlerAttached = true; + outgoing.on("close", makeCloseHandler(req, incoming, outgoing, needsBodyCleanup)); + }; + try { + req = newRequest(incoming, options.hostname); + needsBodyCleanup = autoCleanupIncoming && !(incoming.method === "GET" || incoming.method === "HEAD"); + if (needsBodyCleanup) { + incoming[wrapBodyStream] = true; + if (incoming instanceof node_http2.Http2ServerRequest) outgoing[outgoingEnded] = () => { + if (!incoming.readableEnded) setTimeout(() => { + if (!incoming.readableEnded) setTimeout(() => { + incoming.destroy(); + outgoing.destroy(); + }); + }); + }; + } + res = fetchCallback(req, { + incoming, + outgoing + }); + if (!isPromise(res) && isImmediateCacheableResponse(res)) { + if (needsBodyCleanup && !incoming.readableEnded) outgoing.once("finish", () => { + if (!incoming.readableEnded) drainIncoming(incoming); + }); + return responseViaCache(res, outgoing); + } + ensureCloseHandler(); + } catch (e) { + if (!res) if (options.errorHandler) { + ensureCloseHandler(); + res = await options.errorHandler(req ? e : toRequestError(e)); + if (!res) return; + } else if (!req) res = handleRequestError(); + else res = handleFetchError(e); + else return handleResponseError(e, outgoing); + } + try { + return await responseViaResponseObject(res, outgoing, options); + } catch (e) { + return handleResponseError(e, outgoing); + } + }; +}; + +//#endregion +//#region src/websocket.ts +/** +* @link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent +*/ +const CloseEvent = globalThis.CloseEvent ?? class extends Event { + #eventInitDict; + constructor(type, eventInitDict = {}) { + super(type, eventInitDict); + this.#eventInitDict = eventInitDict; + } + get wasClean() { + return this.#eventInitDict.wasClean ?? false; + } + get code() { + return this.#eventInitDict.code ?? 0; + } + get reason() { + return this.#eventInitDict.reason ?? ""; + } +}; +/** +* Node.js has no global `ErrorEvent`, unlike `Event`/`MessageEvent`/`CloseEvent`. +* @link https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent +*/ +const ErrorEvent = globalThis.ErrorEvent ?? class extends Event { + #eventInitDict; + constructor(type, eventInitDict = {}) { + super(type, eventInitDict); + this.#eventInitDict = eventInitDict; + } + get message() { + return this.#eventInitDict.message ?? ""; + } + get filename() { + return this.#eventInitDict.filename ?? ""; + } + get lineno() { + return this.#eventInitDict.lineno ?? 0; + } + get colno() { + return this.#eventInitDict.colno ?? 0; + } + get error() { + return this.#eventInitDict.error ?? null; + } +}; +const generateConnectionSymbol = () => Symbol("connection"); +const CONNECTION_SYMBOL_KEY = Symbol("CONNECTION_SYMBOL_KEY"); +const WAIT_FOR_WEBSOCKET_SYMBOL = Symbol("WAIT_FOR_WEBSOCKET_SYMBOL"); +const responseHeadersToSkip = new Set([ + "connection", + "content-length", + "keep-alive", + "proxy-authenticate", + "proxy-authorization", + "te", + "trailer", + "transfer-encoding", + "upgrade", + "sec-websocket-accept", + "sec-websocket-extensions", + "sec-websocket-protocol" +]); +const appendResponseHeaders = (headers, responseHeaders) => { + if (!responseHeaders) return; + responseHeaders.forEach((value, key) => { + if (responseHeadersToSkip.has(key.toLowerCase())) return; + headers.push(`${key}: ${value}`); + }); +}; +const rejectUpgradeRequest = (socket, status, responseHeaders) => { + const responseLines = ["Connection: close", "Content-Length: 0"]; + appendResponseHeaders(responseLines, responseHeaders); + socket.end(`HTTP/1.1 ${status.toString()} ${node_http.STATUS_CODES[status] ?? ""}\r\n${responseLines.join("\r\n")}\r\n\r +`); +}; +const createUpgradeRequest = (request) => { + const protocol = request.socket.encrypted ? "https" : "http"; + const url = new URL(request.url ?? "/", `${protocol}://${request.headers.host ?? "localhost"}`); + const headers = new Headers(); + for (const key in request.headers) { + const value = request.headers[key]; + if (!value) continue; + headers.append(key, Array.isArray(value) ? value[0] : value); + } + return new Request(url, { headers }); +}; +const setupWebSocket = (options) => { + const { server, fetchCallback, wss } = options; + const waiterMap = /* @__PURE__ */ new Map(); + wss.on("connection", (ws, request) => { + const waiter = waiterMap.get(request); + if (waiter) { + waiter.resolve(ws); + waiterMap.delete(request); + } + }); + const rejectWaiter = (request) => { + const waiter = waiterMap.get(request); + if (waiter) { + waiterMap.delete(request); + waiter.reject(/* @__PURE__ */ new Error("WebSocket handshake aborted")); + } + }; + const waitForWebSocket = (request, connectionSymbol) => { + return new Promise((resolve, reject) => { + waiterMap.set(request, { + resolve, + reject, + connectionSymbol + }); + }); + }; + server.on("upgrade", async (request, socket, head) => { + if (request.headers.upgrade?.toLowerCase() !== "websocket") return; + const env = { + incoming: request, + outgoing: void 0, + wss, + [WAIT_FOR_WEBSOCKET_SYMBOL]: waitForWebSocket + }; + let status = 400; + let responseHeaders; + try { + const response = await fetchCallback(createUpgradeRequest(request), env); + if (response instanceof Response) { + status = response.status; + responseHeaders = response.headers; + } + } catch { + if (server.listenerCount("upgrade") === 1) rejectUpgradeRequest(socket, 500); + return; + } + const waiter = waiterMap.get(request); + if (!waiter || waiter.connectionSymbol !== env[CONNECTION_SYMBOL_KEY]) { + rejectWaiter(request); + if (server.listenerCount("upgrade") === 1) rejectUpgradeRequest(socket, status, responseHeaders); + return; + } + const addResponseHeaders = (headers) => { + appendResponseHeaders(headers, responseHeaders); + }; + const reclaimWaiterOnClose = () => rejectWaiter(request); + socket.once("close", reclaimWaiterOnClose); + wss.on("headers", addResponseHeaders); + try { + wss.handleUpgrade(request, socket, head, (ws) => { + socket.off("close", reclaimWaiterOnClose); + wss.emit("connection", ws, request); + }); + } finally { + wss.off("headers", addResponseHeaders); + } + }); + server.on("close", () => { + wss.close(); + }); +}; +const upgradeWebSocket = (0, hono_ws.defineWebSocketHelper)(async (c, events, options) => { + if (c.req.header("upgrade")?.toLowerCase() !== "websocket") return; + const env = c.env; + const waitForWebSocket = env[WAIT_FOR_WEBSOCKET_SYMBOL]; + if (!waitForWebSocket || !env.incoming) return new Response(null, { status: 500 }); + const connectionSymbol = generateConnectionSymbol(); + env[CONNECTION_SYMBOL_KEY] = connectionSymbol; + (async () => { + let ws; + try { + ws = await waitForWebSocket(env.incoming, connectionSymbol); + } catch { + return; + } + const messagesReceivedInStarting = []; + const bufferMessage = (data, isBinary) => { + messagesReceivedInStarting.push([data, isBinary]); + }; + ws.on("message", bufferMessage); + const ctx = { + binaryType: "arraybuffer", + close(code, reason) { + ws.close(code, reason); + }, + protocol: ws.protocol, + raw: ws, + get readyState() { + return ws.readyState; + }, + send(source, opts) { + ws.send(source, { compress: opts?.compress }); + }, + url: new URL(c.req.url) + }; + try { + events?.onOpen?.(new Event("open"), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + const handleMessage = (data, isBinary) => { + const datas = Array.isArray(data) ? data : [data]; + for (const data of datas) try { + events?.onMessage?.(new MessageEvent("message", { data: isBinary ? data instanceof ArrayBuffer ? data : data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) : typeof data === "string" ? data : Buffer.from(data).toString("utf-8") }), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + }; + ws.off("message", bufferMessage); + for (const message of messagesReceivedInStarting) handleMessage(...message); + ws.on("message", (data, isBinary) => { + handleMessage(data, isBinary); + }); + ws.on("close", (code, reason) => { + try { + events?.onClose?.(new CloseEvent("close", { + code, + reason: reason.toString() + }), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + }); + ws.on("error", (error) => { + try { + events?.onError?.(new ErrorEvent("error", { error }), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + }); + })(); + return new Response(); +}); + +//#endregion +//#region src/server.ts +const createAdaptorServer = (options) => { + const fetchCallback = options.fetch; + const requestListener = getRequestListener(fetchCallback, { + hostname: options.hostname, + overrideGlobalObjects: options.overrideGlobalObjects, + autoCleanupIncoming: options.autoCleanupIncoming + }); + const server = (options.createServer || node_http.createServer)(options.serverOptions || {}, requestListener); + if (options.websocket && options.websocket.server) { + if (options.websocket.server.options.noServer !== true) throw new Error("WebSocket server must be created with { noServer: true } option"); + setupWebSocket({ + server, + fetchCallback, + wss: options.websocket.server + }); + } + return server; +}; +const serve = (options, listeningListener) => { + const server = createAdaptorServer(options); + server.listen(options?.port ?? 3e3, options.hostname, () => { + const serverInfo = server.address(); + listeningListener && listeningListener(serverInfo); + }); + return server; +}; + +//#endregion +exports.RequestError = RequestError; +exports.createAdaptorServer = createAdaptorServer; +exports.getRequestListener = getRequestListener; +exports.serve = serve; +exports.upgradeWebSocket = upgradeWebSocket; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.d.cts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.d.cts new file mode 100644 index 000000000..d3c55ce99 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.d.cts @@ -0,0 +1,101 @@ +import { AddressInfo } from "node:net"; +import { IncomingMessage, Server, ServerOptions, ServerResponse, createServer } from "node:http"; +import { Http2SecureServer, Http2Server, Http2ServerRequest, Http2ServerResponse, SecureServerOptions, ServerOptions as ServerOptions$1, createSecureServer, createServer as createServer$1 } from "node:http2"; +import { ServerOptions as ServerOptions$2, createServer as createServer$2 } from "node:https"; +import { Duplex } from "node:stream"; +import { UpgradeWebSocket } from "hono/ws"; + +//#region src/websocket-types.d.ts +type WSReadyState = 0 | 1 | 2 | 3; +type WebSocketData = string | ArrayBuffer | Uint8Array | readonly Uint8Array[]; +type WebSocketSendOptions = { + compress?: boolean; +}; +interface WebSocketLike { + protocol: string; + readyState: WSReadyState; + close(code?: number, reason?: string): void; + send(data: string | ArrayBuffer | ArrayBufferView, options?: WebSocketSendOptions): void; + on(event: 'message', listener: (data: WebSocketData, isBinary: boolean) => void): this; + on(event: 'close', listener: (code: number, reason: Uint8Array) => void): this; + on(event: 'error', listener: (error: unknown) => void): this; + off(event: 'message', listener: (data: WebSocketData, isBinary: boolean) => void): this; +} +interface WebSocketServerLike { + options: { + noServer?: boolean; + }; + on(event: 'connection', listener: (ws: WebSocketLike, request: IncomingMessage) => void): this; + on(event: 'headers', listener: (headers: string[]) => void): this; + off(event: 'headers', listener: (headers: string[]) => void): this; + emit(event: 'connection', ws: WebSocketLike, request: IncomingMessage): boolean; + handleUpgrade(request: IncomingMessage, socket: Duplex, head: Buffer, callback: (ws: WebSocketLike) => void): void; + close(): void; +} +//#endregion +//#region src/types.d.ts +type HttpBindings = { + incoming: IncomingMessage; + outgoing: ServerResponse; +}; +type Http2Bindings = { + incoming: Http2ServerRequest; + outgoing: Http2ServerResponse; +}; +type FetchCallback = (request: Request, env: HttpBindings | Http2Bindings) => Promise | unknown; +type ServerType = Server | Http2Server | Http2SecureServer; +type createHttpOptions = { + serverOptions?: ServerOptions; + createServer?: typeof createServer; +}; +type createHttpsOptions = { + serverOptions?: ServerOptions$2; + createServer?: typeof createServer$2; +}; +type createHttp2Options = { + serverOptions?: ServerOptions$1; + createServer?: typeof createServer$1; +}; +type createSecureHttp2Options = { + serverOptions?: SecureServerOptions; + createServer?: typeof createSecureServer; +}; +type ServerOptions$3 = createHttpOptions | createHttpsOptions | createHttp2Options | createSecureHttp2Options; +type Options = { + fetch: FetchCallback; + overrideGlobalObjects?: boolean; + autoCleanupIncoming?: boolean; + port?: number; + hostname?: string; + websocket?: { + server: WebSocketServerLike; + }; +} & ServerOptions$3; +type CustomErrorHandler = (err: unknown) => void | Response | Promise; +//#endregion +//#region src/server.d.ts +declare const createAdaptorServer: (options: Options) => ServerType; +declare const serve: (options: Options, listeningListener?: (info: AddressInfo) => void) => ServerType; +//#endregion +//#region src/websocket.d.ts +type UpgradeWebSocketOptions = { + onError: (err: unknown) => void; +}; +declare const upgradeWebSocket: UpgradeWebSocket; +//#endregion +//#region src/listener.d.ts +declare const getRequestListener: (fetchCallback: FetchCallback, options?: { + hostname?: string; + errorHandler?: CustomErrorHandler; + overrideGlobalObjects?: boolean; + autoCleanupIncoming?: boolean; +}) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise; +//#endregion +//#region src/error.d.ts +declare class RequestError extends Error { + constructor(message: string, options?: { + cause?: unknown; + }); +} +//#endregion +export { type Http2Bindings, type HttpBindings, RequestError, type ServerType, type WebSocketData, type WebSocketLike, type WebSocketServerLike, createAdaptorServer, getRequestListener, serve, upgradeWebSocket }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.d.mts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.d.mts new file mode 100644 index 000000000..a815b86f0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.d.mts @@ -0,0 +1,101 @@ +import { IncomingMessage, Server, ServerOptions, ServerResponse, createServer } from "node:http"; +import { Http2SecureServer, Http2Server, Http2ServerRequest, Http2ServerResponse, SecureServerOptions, ServerOptions as ServerOptions$1, createSecureServer, createServer as createServer$1 } from "node:http2"; +import { Duplex } from "node:stream"; +import { UpgradeWebSocket } from "hono/ws"; +import { AddressInfo } from "node:net"; +import { ServerOptions as ServerOptions$2, createServer as createServer$2 } from "node:https"; + +//#region src/websocket-types.d.ts +type WSReadyState = 0 | 1 | 2 | 3; +type WebSocketData = string | ArrayBuffer | Uint8Array | readonly Uint8Array[]; +type WebSocketSendOptions = { + compress?: boolean; +}; +interface WebSocketLike { + protocol: string; + readyState: WSReadyState; + close(code?: number, reason?: string): void; + send(data: string | ArrayBuffer | ArrayBufferView, options?: WebSocketSendOptions): void; + on(event: 'message', listener: (data: WebSocketData, isBinary: boolean) => void): this; + on(event: 'close', listener: (code: number, reason: Uint8Array) => void): this; + on(event: 'error', listener: (error: unknown) => void): this; + off(event: 'message', listener: (data: WebSocketData, isBinary: boolean) => void): this; +} +interface WebSocketServerLike { + options: { + noServer?: boolean; + }; + on(event: 'connection', listener: (ws: WebSocketLike, request: IncomingMessage) => void): this; + on(event: 'headers', listener: (headers: string[]) => void): this; + off(event: 'headers', listener: (headers: string[]) => void): this; + emit(event: 'connection', ws: WebSocketLike, request: IncomingMessage): boolean; + handleUpgrade(request: IncomingMessage, socket: Duplex, head: Buffer, callback: (ws: WebSocketLike) => void): void; + close(): void; +} +//#endregion +//#region src/types.d.ts +type HttpBindings = { + incoming: IncomingMessage; + outgoing: ServerResponse; +}; +type Http2Bindings = { + incoming: Http2ServerRequest; + outgoing: Http2ServerResponse; +}; +type FetchCallback = (request: Request, env: HttpBindings | Http2Bindings) => Promise | unknown; +type ServerType = Server | Http2Server | Http2SecureServer; +type createHttpOptions = { + serverOptions?: ServerOptions; + createServer?: typeof createServer; +}; +type createHttpsOptions = { + serverOptions?: ServerOptions$2; + createServer?: typeof createServer$2; +}; +type createHttp2Options = { + serverOptions?: ServerOptions$1; + createServer?: typeof createServer$1; +}; +type createSecureHttp2Options = { + serverOptions?: SecureServerOptions; + createServer?: typeof createSecureServer; +}; +type ServerOptions$3 = createHttpOptions | createHttpsOptions | createHttp2Options | createSecureHttp2Options; +type Options = { + fetch: FetchCallback; + overrideGlobalObjects?: boolean; + autoCleanupIncoming?: boolean; + port?: number; + hostname?: string; + websocket?: { + server: WebSocketServerLike; + }; +} & ServerOptions$3; +type CustomErrorHandler = (err: unknown) => void | Response | Promise; +//#endregion +//#region src/server.d.ts +declare const createAdaptorServer: (options: Options) => ServerType; +declare const serve: (options: Options, listeningListener?: (info: AddressInfo) => void) => ServerType; +//#endregion +//#region src/websocket.d.ts +type UpgradeWebSocketOptions = { + onError: (err: unknown) => void; +}; +declare const upgradeWebSocket: UpgradeWebSocket; +//#endregion +//#region src/listener.d.ts +declare const getRequestListener: (fetchCallback: FetchCallback, options?: { + hostname?: string; + errorHandler?: CustomErrorHandler; + overrideGlobalObjects?: boolean; + autoCleanupIncoming?: boolean; +}) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise; +//#endregion +//#region src/error.d.ts +declare class RequestError extends Error { + constructor(message: string, options?: { + cause?: unknown; + }); +} +//#endregion +export { type Http2Bindings, type HttpBindings, RequestError, type ServerType, type WebSocketData, type WebSocketLike, type WebSocketServerLike, createAdaptorServer, getRequestListener, serve, upgradeWebSocket }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.mjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.mjs new file mode 100644 index 000000000..4f5954d70 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/index.mjs @@ -0,0 +1,1335 @@ +import { t as X_ALREADY_SENT } from "./constants-BLSFu_RU.mjs"; +import { STATUS_CODES, ServerResponse, createServer } from "node:http"; +import { Http2ServerRequest, constants } from "node:http2"; +import { Readable } from "node:stream"; +import { defineWebSocketHelper } from "hono/ws"; + +//#region src/error.ts +var RequestError = class extends Error { + constructor(message, options) { + super(message, options); + this.name = "RequestError"; + } +}; + +//#endregion +//#region src/headers.ts +const nonJoinedHeaders = new Set([ + "age", + "authorization", + "content-length", + "content-type", + "etag", + "expires", + "from", + "host", + "if-modified-since", + "if-unmodified-since", + "last-modified", + "location", + "max-forwards", + "proxy-authorization", + "referer", + "retry-after", + "server", + "user-agent" +]); +const validHeaderName = /^[!#$%&'*+\-.^_`|~\dA-Za-z]+$/; +const isHttpWhitespace = (code) => code === 9 || code === 10 || code === 13 || code === 32; +const normalizeHeaderValue = (value) => { + if (!isHttpWhitespace(value.charCodeAt(0)) && !isHttpWhitespace(value.charCodeAt(value.length - 1))) return value; + let start = 0; + let end = value.length; + while (start < end && isHttpWhitespace(value.charCodeAt(start))) start++; + while (end > start && isHttpWhitespace(value.charCodeAt(end - 1))) end--; + return value.slice(start, end); +}; +const forbiddenHeaderValue = /[\0\r\n]/; +const GlobalHeaders = globalThis.Headers; +const materializeHeaders = (rawHeaders, HeadersCtor = GlobalHeaders) => { + const headers = new HeadersCtor(); + for (let i = 0; i < rawHeaders.length; i += 2) { + const name = rawHeaders[i]; + if (!name.startsWith(":")) headers.append(name, rawHeaders[i + 1]); + } + return headers; +}; +var RequestHeaders = class { + #incoming; + #rawHeaders; + #headers; + #invalidValue; + constructor(incoming) { + this.#incoming = incoming; + if (incoming instanceof Http2ServerRequest) this.#rawHeaders = incoming.rawHeaders.slice(); + } + get #lazyRawHeaders() { + return this.#rawHeaders ??= this.#incoming.rawHeaders.slice(); + } + get #native() { + if (!this.#headers) { + this.#headers = materializeHeaders(this.#lazyRawHeaders); + this.#rawHeaders = void 0; + } + return this.#headers; + } + #normalizedName(name) { + if (typeof name !== "string") return; + if (!validHeaderName.test(name)) throw new TypeError(`Invalid header name: ${name}`); + return name.toLowerCase(); + } + #lookupHttp1(lowerName) { + const headers = this.#incoming instanceof Http2ServerRequest ? void 0 : this.#incoming.headers; + if (!headers || nonJoinedHeaders.has(lowerName) || lowerName === "set-cookie" || lowerName === "__proto__") return; + if (!Object.hasOwn(headers, lowerName)) return null; + const rawValue = headers[lowerName]; + if (typeof rawValue === "string") { + const value = normalizeHeaderValue(rawValue); + return forbiddenHeaderValue.test(value) ? void 0 : value; + } + } + #lookup(rawHeaders, lowerName) { + const separator = lowerName === "cookie" ? "; " : ", "; + let value = null; + for (let i = 0; i < rawHeaders.length; i += 2) { + const rawName = rawHeaders[i]; + if (rawName.length === lowerName.length && rawName.toLowerCase() === lowerName) { + const rawValue = normalizeHeaderValue(rawHeaders[i + 1]); + if (forbiddenHeaderValue.test(rawValue)) { + this.#invalidValue = true; + return; + } + value = value === null ? rawValue : value + separator + rawValue; + } + } + return value; + } + append(name, value) { + this.#native.append(name, value); + } + delete(name) { + this.#native.delete(name); + } + get(name) { + const lowerName = this.#normalizedName(name); + if (lowerName && !this.#headers && !this.#invalidValue) { + const http1Value = this.#lookupHttp1(lowerName); + if (http1Value !== void 0) return http1Value; + const value = this.#lookup(this.#lazyRawHeaders, lowerName); + if (value !== void 0) return value; + } + return this.#native.get(name); + } + has(name) { + const lowerName = this.#normalizedName(name); + if (lowerName && !this.#headers && !this.#invalidValue) { + const http1Value = this.#lookupHttp1(lowerName); + if (http1Value !== void 0) return http1Value !== null; + const value = this.#lookup(this.#lazyRawHeaders, lowerName); + if (value !== void 0) return value !== null; + } + return this.#native.has(name); + } + set(name, value) { + this.#native.set(name, value); + } + getSetCookie() { + return this.#native.getSetCookie(); + } + keys() { + return this.#native.keys(); + } + values() { + return this.#native.values(); + } + entries() { + return this.#native.entries(); + } + forEach(callback, thisArg) { + this.#native.forEach((value, key) => { + callback.call(thisArg, value, key, this); + }); + } + [Symbol.iterator]() { + return this.entries(); + } +}; +Object.defineProperty(RequestHeaders.prototype, Symbol.for("nodejs.util.inspect.custom"), { value: function(depth, options, inspectFn) { + return `Headers (lightweight) ${inspectFn(Object.fromEntries(this), { + ...options, + depth: depth == null ? null : depth - 1 + })}`; +} }); +Object.setPrototypeOf(RequestHeaders.prototype, GlobalHeaders.prototype); +const newHeadersFromIncoming = (incoming) => globalThis.Headers === GlobalHeaders ? new RequestHeaders(incoming) : materializeHeaders(incoming.rawHeaders, globalThis.Headers); + +//#endregion +//#region src/url.ts +const reValidRequestUrl = /^\/[!#$&-;=?-\[\]_a-z~]*$/; +const reDotSegment = /\/\.\.?(?:[/?#]|$)/; +const reValidHost = /^[a-z0-9._-]+(?::(?:[1-5]\d{3,4}|[6-9]\d{3}))?$/; +const buildUrl = (scheme, host, incomingUrl) => { + const url = `${scheme}://${host}${incomingUrl}`; + if (!reValidHost.test(host)) { + const urlObj = new URL(url); + if (urlObj.hostname.length !== host.length && urlObj.hostname !== (host.includes(":") ? host.replace(/:\d+$/, "") : host).toLowerCase()) throw new RequestError("Invalid host header"); + return urlObj.href; + } else if (incomingUrl.length === 0) return url + "/"; + else { + if (incomingUrl.charCodeAt(0) !== 47) throw new RequestError("Invalid URL"); + if (!reValidRequestUrl.test(incomingUrl) || reDotSegment.test(incomingUrl)) return new URL(url).href; + return url; + } +}; + +//#endregion +//#region src/request.ts +const toRequestError = (e) => { + if (e instanceof RequestError) return e; + return new RequestError(e.message, { cause: e }); +}; +const GlobalRequest = global.Request; +var Request$1 = class extends GlobalRequest { + constructor(input, options) { + if (typeof input === "object" && getRequestCache in input) { + const hasReplacementBody = options !== void 0 && "body" in options && options.body != null; + if (input[bodyConsumedDirectlyKey] && !hasReplacementBody) throw new TypeError("Cannot construct a Request with a Request object that has already been used."); + input = input[getRequestCache](); + } + if (typeof (options?.body)?.getReader !== "undefined") options.duplex ??= "half"; + super(input, options); + } +}; +const wrapBodyStream = Symbol("wrapBodyStream"); +const byteExactEncodings = new Set([ + "latin1", + "binary", + "hex", + "base64", + "base64url" +]); +const isByteExactEncoding = (encoding) => encoding === null || byteExactEncodings.has(encoding); +const bodyBufferedBeforeDisconnectKey = Symbol("bodyBufferedBeforeDisconnect"); +const bodyBufferedLengthBeforeDisconnectKey = Symbol("bodyBufferedLengthBeforeDisconnect"); +const toBufferChunk = (chunk, encoding) => Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding ?? "utf8"); +const isRecoverableDisconnectedIncoming = (incoming) => !(incoming instanceof Http2ServerRequest) && !!incoming.complete && !!incoming.readableAborted && typeof incoming.read === "function" && isByteExactEncoding(incoming.readableEncoding); +const recordBodyBufferedBeforeDisconnect = (incoming) => { + if (incoming.readableDidRead || !isRecoverableDisconnectedIncoming(incoming)) return; + const incomingWithRecovery = incoming; + incomingWithRecovery[bodyBufferedLengthBeforeDisconnectKey] ??= incoming.readableLength; +}; +const readBodyBufferedBeforeDisconnect = (incoming, chunks) => { + if (incoming.readableDidRead && !chunks || !isRecoverableDisconnectedIncoming(incoming)) return; + const incomingWithRecovery = incoming; + if (incomingWithRecovery[bodyBufferedBeforeDisconnectKey] !== void 0) return incomingWithRecovery[bodyBufferedBeforeDisconnectKey]; + let result; + const errored = incoming.errored; + if (errored && errored.code !== "ECONNRESET") result = errored; + else if (incomingWithRecovery[bodyBufferedLengthBeforeDisconnectKey] !== void 0 && incoming.readableLength !== incomingWithRecovery[bodyBufferedLengthBeforeDisconnectKey]) result = newBodyUnusableError(); + else { + const bodyChunks = chunks ?? []; + const chunk = incoming.read(); + if (chunk !== null) bodyChunks.push(toBufferChunk(chunk, incoming.readableEncoding)); + const buffer = bodyChunks.length === 1 ? bodyChunks[0] : Buffer.concat(bodyChunks); + result = buffer; + const contentLength = incoming.headers["content-length"]; + if (typeof contentLength === "string" && /^\d+$/.test(contentLength)) { + const expectedLength = Number(contentLength); + if (Number.isSafeInteger(expectedLength) && buffer.length !== expectedLength) result = newBodyUnusableError(); + } + } + incomingWithRecovery[bodyBufferedBeforeDisconnectKey] = result; + return result; +}; +const enqueueBufferedBody = (controller, buffered) => { + if (buffered instanceof Error) { + controller.error(buffered); + return; + } + if (buffered.length > 0) controller.enqueue(buffered); + controller.close(); +}; +const newRequestFromIncoming = (method, url, headers, incoming, abortController) => { + const init = { + method, + headers, + signal: abortController.signal + }; + if (method === "TRACE") { + init.method = "GET"; + const req = new Request$1(url, init); + Object.defineProperty(req, "method", { get() { + return "TRACE"; + } }); + return req; + } + if (!(method === "GET" || method === "HEAD")) if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) init.body = new ReadableStream({ start(controller) { + controller.enqueue(incoming.rawBody); + controller.close(); + } }); + else if (incoming[wrapBodyStream]) { + let reader; + init.body = new ReadableStream({ async pull(controller) { + try { + if (!reader) { + const buffered = readBodyBufferedBeforeDisconnect(incoming); + if (buffered !== void 0) { + enqueueBufferedBody(controller, buffered); + return; + } + } + reader ||= Readable.toWeb(incoming).getReader(); + const { done, value } = await reader.read(); + if (done) controller.close(); + else controller.enqueue(value); + } catch (error) { + controller.error(error); + } + } }); + } else { + const buffered = readBodyBufferedBeforeDisconnect(incoming); + if (buffered !== void 0) init.body = new ReadableStream({ start(controller) { + enqueueBufferedBody(controller, buffered); + } }); + else init.body = Readable.toWeb(incoming); + } + return new Request$1(url, init); +}; +const getRequestCache = Symbol("getRequestCache"); +const requestCache = Symbol("requestCache"); +const incomingKey = Symbol("incomingKey"); +const urlKey = Symbol("urlKey"); +const methodKey = Symbol("methodKey"); +const headersKey = Symbol("headersKey"); +const abortControllerKey = Symbol("abortControllerKey"); +const getAbortController = Symbol("getAbortController"); +const abortRequest = Symbol("abortRequest"); +const bodyBufferKey = Symbol("bodyBuffer"); +const bodyReadPromiseKey = Symbol("bodyReadPromise"); +const bodyConsumedDirectlyKey = Symbol("bodyConsumedDirectly"); +const bodyLockReaderKey = Symbol("bodyLockReader"); +const abortReasonKey = Symbol("abortReason"); +const newBodyUnusableError = () => { + return /* @__PURE__ */ new TypeError("Body is unusable"); +}; +const rejectBodyUnusable = () => { + return Promise.reject(newBodyUnusableError()); +}; +const textDecoder = new TextDecoder(); +const consumeBodyDirectOnce = (request) => { + if (request[bodyConsumedDirectlyKey]) return rejectBodyUnusable(); + request[bodyConsumedDirectlyKey] = true; +}; +const toArrayBuffer = (buf) => { + return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); +}; +const contentType = (request) => { + return (request[headersKey] ||= newHeadersFromIncoming(request[incomingKey])).get("content-type") || ""; +}; +const methodTokenRegExp = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; +const normalizeIncomingMethod = (method) => { + if (typeof method !== "string" || method.length === 0) return "GET"; + switch (method) { + case "DELETE": + case "GET": + case "HEAD": + case "OPTIONS": + case "PATCH": + case "POST": + case "PUT": + case "QUERY": return method; + } + const upper = method.toUpperCase(); + switch (upper) { + case "DELETE": + case "GET": + case "HEAD": + case "OPTIONS": + case "POST": + case "PUT": return upper; + default: return method; + } +}; +const validateDirectReadMethod = (method) => { + if (!methodTokenRegExp.test(method)) return /* @__PURE__ */ new TypeError(`'${method}' is not a valid HTTP method.`); + const normalized = method.toUpperCase(); + if (normalized === "CONNECT" || normalized === "TRACK" || normalized === "TRACE" && method !== "TRACE") return /* @__PURE__ */ new TypeError(`'${method}' HTTP method is unsupported.`); +}; +const readBodyWithFastPath = (request, method, fromBuffer) => { + if (request[bodyConsumedDirectlyKey]) return rejectBodyUnusable(); + const methodName = request.method; + if (methodName === "GET" || methodName === "HEAD") return request[getRequestCache]()[method](); + const methodValidationError = validateDirectReadMethod(methodName); + if (methodValidationError) return Promise.reject(methodValidationError); + if (request[requestCache]) { + if (methodName !== "TRACE") return request[requestCache][method](); + } + const alreadyUsedError = consumeBodyDirectOnce(request); + if (alreadyUsedError) return alreadyUsedError; + const raw = readRawBodyIfAvailable(request); + if (raw) { + const result = Promise.resolve(fromBuffer(raw, request)); + request[bodyBufferKey] = void 0; + return result; + } + return readBodyDirect(request).then((buf) => { + const result = fromBuffer(buf, request); + request[bodyBufferKey] = void 0; + return result; + }); +}; +const readRawBodyIfAvailable = (request) => { + const incoming = request[incomingKey]; + if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) return incoming.rawBody; +}; +const normalizeAbortError = (request, incoming) => { + if (incoming.errored) return incoming.errored; + const reason = request[abortReasonKey]; + if (reason !== void 0) return reason instanceof Error ? reason : new Error(String(reason)); + return /* @__PURE__ */ new Error("Client connection prematurely closed."); +}; +const readBodyDirect = (request) => { + if (request[bodyBufferKey]) return Promise.resolve(request[bodyBufferKey]); + if (request[bodyReadPromiseKey]) return request[bodyReadPromiseKey]; + const incoming = request[incomingKey]; + if (incoming.readableDidRead) return rejectBodyUnusable(); + const buffered = readBodyBufferedBeforeDisconnect(incoming); + if (buffered !== void 0) { + if (buffered instanceof Error) return Promise.reject(buffered); + request[bodyBufferKey] = buffered; + return Promise.resolve(buffered); + } + const promise = new Promise((resolve, reject) => { + const chunks = []; + let settled = false; + const finish = (callback) => { + if (settled) return; + settled = true; + cleanup(); + callback(); + }; + const recoverCompleteBodyAfterDisconnect = (error) => { + const streamError = incoming.errored ?? error; + if (!isRecoverableDisconnectedIncoming(incoming) || streamError && streamError.code !== "ECONNRESET") return false; + finish(() => { + const recovered = readBodyBufferedBeforeDisconnect(incoming, chunks); + if (recovered instanceof Error) reject(recovered); + else if (recovered === void 0) reject(error ?? normalizeAbortError(request, incoming)); + else { + request[bodyBufferKey] = recovered; + resolve(recovered); + } + }); + return true; + }; + const onData = (chunk) => { + chunks.push(toBufferChunk(chunk, incoming.readableEncoding)); + }; + const onEnd = () => { + finish(() => { + const buffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks); + request[bodyBufferKey] = buffer; + resolve(buffer); + }); + }; + const onError = (error) => { + if (recoverCompleteBodyAfterDisconnect(error)) return; + finish(() => { + reject(error); + }); + }; + const onClose = () => { + if (incoming.readableEnded) { + onEnd(); + return; + } + if (recoverCompleteBodyAfterDisconnect()) return; + finish(() => { + reject(normalizeAbortError(request, incoming)); + }); + }; + const cleanup = () => { + incoming.off("data", onData); + incoming.off("end", onEnd); + incoming.off("error", onError); + incoming.off("close", onClose); + request[bodyReadPromiseKey] = void 0; + }; + incoming.on("data", onData); + incoming.on("end", onEnd); + incoming.on("error", onError); + incoming.on("close", onClose); + queueMicrotask(() => { + if (settled) return; + if (incoming.readableEnded) onEnd(); + else if (incoming.errored) onError(incoming.errored); + else if (incoming.destroyed) onClose(); + }); + }); + request[bodyReadPromiseKey] = promise; + return promise; +}; +const requestPrototype = { + get method() { + return this[methodKey]; + }, + get url() { + return this[urlKey]; + }, + get headers() { + return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]); + }, + [abortRequest](reason) { + if (this[abortReasonKey] === void 0) this[abortReasonKey] = reason; + const abortController = this[abortControllerKey]; + if (abortController && !abortController.signal.aborted) abortController.abort(reason); + }, + [getAbortController]() { + this[abortControllerKey] ||= new AbortController(); + if (this[abortReasonKey] !== void 0 && !this[abortControllerKey].signal.aborted) this[abortControllerKey].abort(this[abortReasonKey]); + return this[abortControllerKey]; + }, + [getRequestCache]() { + const abortController = this[getAbortController](); + if (this[requestCache]) return this[requestCache]; + const method = this.method; + if (this[bodyConsumedDirectlyKey] && !(method === "GET" || method === "HEAD")) { + this[bodyBufferKey] = void 0; + const init = { + method: method === "TRACE" ? "GET" : method, + headers: this.headers, + signal: abortController.signal + }; + if (method !== "TRACE") { + init.body = new ReadableStream({ start(c) { + c.close(); + } }); + init.duplex = "half"; + } + const req = new Request$1(this[urlKey], init); + if (method === "TRACE") Object.defineProperty(req, "method", { get() { + return "TRACE"; + } }); + return this[requestCache] = req; + } + return this[requestCache] = newRequestFromIncoming(this.method, this[urlKey], this.headers, this[incomingKey], abortController); + }, + get body() { + if (!this[bodyConsumedDirectlyKey]) return this[getRequestCache]().body; + const request = this[getRequestCache](); + if (!this[bodyLockReaderKey] && request.body) this[bodyLockReaderKey] = request.body.getReader(); + return request.body; + }, + get bodyUsed() { + if (this[bodyConsumedDirectlyKey]) return true; + if (this[requestCache]) return this[requestCache].bodyUsed; + return false; + } +}; +Object.defineProperty(requestPrototype, "signal", { get() { + return this[getAbortController]().signal; +} }); +[ + "cache", + "credentials", + "destination", + "integrity", + "mode", + "redirect", + "referrer", + "referrerPolicy", + "keepalive" +].forEach((k) => { + Object.defineProperty(requestPrototype, k, { get() { + return this[getRequestCache]()[k]; + } }); +}); +["clone", "formData"].forEach((k) => { + Object.defineProperty(requestPrototype, k, { value: function() { + if (this[bodyConsumedDirectlyKey]) { + if (k === "clone") throw newBodyUnusableError(); + return rejectBodyUnusable(); + } + return this[getRequestCache]()[k](); + } }); +}); +Object.defineProperty(requestPrototype, "text", { value: function() { + return readBodyWithFastPath(this, "text", (buf) => textDecoder.decode(buf)); +} }); +Object.defineProperty(requestPrototype, "arrayBuffer", { value: function() { + return readBodyWithFastPath(this, "arrayBuffer", (buf) => toArrayBuffer(buf)); +} }); +Object.defineProperty(requestPrototype, "blob", { value: function() { + return readBodyWithFastPath(this, "blob", (buf, request) => { + const type = contentType(request); + const init = type ? { headers: { "content-type": type } } : void 0; + return new Response(buf, init).blob(); + }); +} }); +Object.defineProperty(requestPrototype, "json", { value: function() { + if (this[bodyConsumedDirectlyKey]) return rejectBodyUnusable(); + return this.text().then(JSON.parse); +} }); +Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), { value: function(depth, options, inspectFn) { + return `Request (lightweight) ${inspectFn({ + method: this.method, + url: this.url, + headers: this.headers, + nativeRequest: this[requestCache] + }, { + ...options, + depth: depth == null ? null : depth - 1 + })}`; +} }); +Object.setPrototypeOf(requestPrototype, Request$1.prototype); +const newRequest = (incoming, defaultHostname) => { + const req = Object.create(requestPrototype); + req[incomingKey] = incoming; + req[methodKey] = normalizeIncomingMethod(incoming.method); + const incomingUrl = incoming.url || ""; + if (incomingUrl[0] !== "/" && (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) { + if (incoming instanceof Http2ServerRequest) throw new RequestError("Absolute URL for :path is not allowed in HTTP/2"); + try { + req[urlKey] = new URL(incomingUrl).href; + } catch (e) { + throw new RequestError("Invalid absolute URL", { cause: e }); + } + return req; + } + const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname; + if (!host) throw new RequestError("Missing host header"); + let scheme; + if (incoming instanceof Http2ServerRequest) { + scheme = incoming.scheme; + if (!(scheme === "http" || scheme === "https")) throw new RequestError("Unsupported scheme"); + } else scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http"; + try { + req[urlKey] = buildUrl(scheme, host, incomingUrl); + } catch (e) { + if (e instanceof RequestError) throw e; + else throw new RequestError("Invalid URL", { cause: e }); + } + return req; +}; + +//#endregion +//#region src/response.ts +const defaultContentType = "text/plain; charset=UTF-8"; +const responseCache = Symbol("responseCache"); +const getResponseCache = Symbol("getResponseCache"); +const cacheKey = Symbol("cache"); +const GlobalResponse = global.Response; +var Response$1 = class Response$1 { + #body; + #init; + [getResponseCache]() { + const cache = this[cacheKey]; + const liveHeaders = cache && cache[2] instanceof Headers ? cache[2] : void 0; + delete this[cacheKey]; + return this[responseCache] ||= new GlobalResponse(this.#body, liveHeaders ? { + status: this.#init?.status, + statusText: this.#init?.statusText, + headers: liveHeaders + } : this.#init); + } + constructor(body, init) { + let headers; + this.#body = body; + if (init instanceof GlobalResponse) { + const cachedGlobalResponse = init[responseCache]; + if (cachedGlobalResponse) { + this.#init = cachedGlobalResponse; + this[getResponseCache](); + return; + } + this.#init = init instanceof Response$1 ? init.#init : init; + headers = new Headers(init.headers); + } else this.#init = init; + if (body == null || typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) this[cacheKey] = [ + init?.status || 200, + body ?? null, + headers || init?.headers + ]; + } + get headers() { + const cache = this[cacheKey]; + if (cache) { + if (!(cache[2] instanceof Headers)) cache[2] = new Headers(cache[2] || (cache[1] === null ? void 0 : { "content-type": defaultContentType })); + return cache[2]; + } + return this[getResponseCache]().headers; + } + get status() { + return this[cacheKey]?.[0] ?? this[getResponseCache]().status; + } + get ok() { + const status = this.status; + return status >= 200 && status < 300; + } +}; +[ + "body", + "bodyUsed", + "redirected", + "statusText", + "trailers", + "type", + "url" +].forEach((k) => { + Object.defineProperty(Response$1.prototype, k, { get() { + return this[getResponseCache]()[k]; + } }); +}); +[ + "arrayBuffer", + "blob", + "clone", + "formData", + "json", + "text" +].forEach((k) => { + Object.defineProperty(Response$1.prototype, k, { value: function() { + return this[getResponseCache]()[k](); + } }); +}); +Object.defineProperty(Response$1.prototype, Symbol.for("nodejs.util.inspect.custom"), { value: function(depth, options, inspectFn) { + return `Response (lightweight) ${inspectFn({ + status: this.status, + headers: this.headers, + ok: this.ok, + nativeResponse: this[responseCache] + }, { + ...options, + depth: depth == null ? null : depth - 1 + })}`; +} }); +Object.setPrototypeOf(Response$1, GlobalResponse); +Object.setPrototypeOf(Response$1.prototype, GlobalResponse.prototype); +const validRedirectUrl = /^https?:\/\/[!#-;=?-[\]_a-z~A-Z]+$/; +const parseRedirectUrl = (url) => { + if (url instanceof URL) return url.href; + if (validRedirectUrl.test(url)) return url; + return new URL(url).href; +}; +const validRedirectStatuses = new Set([ + 301, + 302, + 303, + 307, + 308 +]); +Object.defineProperty(Response$1, "redirect", { + value: function redirect(url, status = 302) { + if (!validRedirectStatuses.has(status)) throw new RangeError("Invalid status code"); + return new Response$1(null, { + status, + headers: { location: parseRedirectUrl(url) } + }); + }, + writable: true, + configurable: true +}); +Object.defineProperty(Response$1, "json", { + value: function json(data, init) { + const body = JSON.stringify(data); + if (body === void 0) throw new TypeError("The data is not JSON serializable"); + const initHeaders = init?.headers; + let headers; + if (initHeaders) { + headers = new Headers(initHeaders); + if (!headers.has("content-type")) headers.set("content-type", "application/json"); + } else headers = { "content-type": "application/json" }; + return new Response$1(body, { + status: init?.status ?? 200, + statusText: init?.statusText, + headers + }); + }, + writable: true, + configurable: true +}); + +//#endregion +//#region src/utils.ts +async function readWithoutBlocking(readPromise) { + return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]); +} +function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) { + const cancel = (error) => { + reader.cancel(error).catch(() => {}); + }; + writable.on("close", cancel); + writable.on("error", cancel); + (currentReadPromise ?? reader.read()).then(flow, handleStreamError); + return reader.closed.finally(() => { + writable.off("close", cancel); + writable.off("error", cancel); + }); + function handleStreamError(error) { + if (error) writable.destroy(error); + } + function onDrain() { + reader.read().then(flow, handleStreamError); + } + function flow({ done, value }) { + try { + if (done) writable.end(); + else if (!writable.write(value)) writable.once("drain", onDrain); + else return reader.read().then(flow, handleStreamError); + } catch (e) { + handleStreamError(e); + } + } +} +function writeFromReadableStream(stream, writable) { + if (stream.locked) throw new TypeError("ReadableStream is locked."); + else if (writable.destroyed) return; + return writeFromReadableStreamDefaultReader(stream.getReader(), writable); +} +const buildOutgoingHttpHeaders = (headers, defaultContentType) => { + const res = {}; + if (!(headers instanceof Headers)) headers = new Headers(headers ?? void 0); + if (headers.has("set-cookie")) { + const cookies = []; + for (const [k, v] of headers) if (k === "set-cookie") cookies.push(v); + else res[k] = v; + if (cookies.length > 0) res["set-cookie"] = cookies; + } else for (const [k, v] of headers) res[k] = v; + if (defaultContentType) res["content-type"] ??= defaultContentType; + return res; +}; + +//#endregion +//#region src/listener.ts +const outgoingEnded = Symbol("outgoingEnded"); +const incomingDraining = Symbol("incomingDraining"); +const DRAIN_TIMEOUT_MS = 500; +const MAX_DRAIN_BYTES = 64 * 1024 * 1024; +const drainIncoming = (incoming) => { + const incomingWithDrainState = incoming; + if (incoming.destroyed || incomingWithDrainState[incomingDraining]) return; + incomingWithDrainState[incomingDraining] = true; + if (incoming instanceof Http2ServerRequest) { + try { + incoming.stream?.close?.(constants.NGHTTP2_NO_ERROR); + } catch {} + return; + } + let bytesRead = 0; + const cleanup = () => { + clearTimeout(timer); + incoming.off("data", onData); + incoming.off("end", cleanup); + incoming.off("error", cleanup); + }; + const forceClose = () => { + cleanup(); + const socket = incoming.socket; + if (socket && !socket.destroyed) { + if (typeof socket.destroySoon === "function") socket.destroySoon(); + else if (typeof socket.destroy === "function") socket.destroy(); + } + }; + const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS); + timer.unref?.(); + const onData = (chunk) => { + bytesRead += chunk.length; + if (bytesRead > MAX_DRAIN_BYTES) forceClose(); + }; + incoming.on("data", onData); + incoming.on("end", cleanup); + incoming.on("error", cleanup); + incoming.resume(); +}; +const makeCloseHandler = (req, incoming, outgoing, needsBodyCleanup) => () => { + if (incoming.errored) { + recordBodyBufferedBeforeDisconnect(incoming); + req[abortRequest](incoming.errored.toString()); + } else if (!outgoing.writableFinished || incoming instanceof Http2ServerRequest && incoming.aborted) { + recordBodyBufferedBeforeDisconnect(incoming); + req[abortRequest]("Client connection prematurely closed."); + } + if (needsBodyCleanup && !incoming.readableEnded) setTimeout(() => { + if (!incoming.readableEnded) setTimeout(() => { + drainIncoming(incoming); + }); + }); +}; +const isImmediateCacheableResponse = (res) => { + if (!(cacheKey in res)) return false; + const body = res[cacheKey][1]; + return body === null || typeof body === "string" || body instanceof Uint8Array; +}; +const handleRequestError = () => new Response(null, { status: 400 }); +const handleFetchError = (e) => new Response(null, { status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500 }); +const handleResponseError = (e, outgoing) => { + const err = e instanceof Error ? e : new Error("unknown error", { cause: e }); + if (err.code === "ERR_STREAM_PREMATURE_CLOSE") console.info("The user aborted a request."); + else { + console.error(e); + if (!outgoing.headersSent) { + if (outgoing instanceof ServerResponse) outgoing._contentLength = null; + outgoing.writeHead(500, { "Content-Type": "text/plain" }); + } + outgoing.end(`Error: ${err.message}`); + outgoing.destroy(err); + } +}; +const flushHeaders = (outgoing) => { + if ("flushHeaders" in outgoing && outgoing.writable) outgoing.flushHeaders(); +}; +const trySetContentLength = (outgoing, status, length) => { + const http1 = outgoing; + if (http1._contentLength === null && http1._hasBody && http1.useChunkedEncodingByDefault && !http1._removedContLen && status >= 200 && status !== 204 && status !== 304 && !outgoing.hasHeader("content-length") && !outgoing.hasHeader("transfer-encoding") && !outgoing.hasHeader("trailer")) { + http1._contentLength = length; + return true; + } + return false; +}; +const writeDefaultHeaders = (outgoing, status, length) => { + if (trySetContentLength(outgoing, status, length)) outgoing.writeHead(status, { "Content-Type": defaultContentType }); + else outgoing.writeHead(status, { + "Content-Type": defaultContentType, + "Content-Length": length + }); +}; +const responseViaCache = async (res, outgoing) => { + let [status, body, header] = res[cacheKey]; + if (!header) { + if (body === null) { + outgoing.writeHead(status); + outgoing.end(); + } else if (typeof body === "string") { + writeDefaultHeaders(outgoing, status, Buffer.byteLength(body)); + outgoing.end(body); + } else if (body instanceof Uint8Array) { + writeDefaultHeaders(outgoing, status, body.byteLength); + outgoing.end(body); + } else if (body instanceof Blob) { + writeDefaultHeaders(outgoing, status, body.size); + outgoing.end(new Uint8Array(await body.arrayBuffer())); + } else { + outgoing.writeHead(status, { "Content-Type": defaultContentType }); + flushHeaders(outgoing); + await writeFromReadableStream(body, outgoing)?.catch((e) => handleResponseError(e, outgoing)); + } + outgoing[outgoingEnded]?.(); + return; + } + let hasContentLength = false; + let plainHeaders = false; + let canAutoLength = true; + if (header instanceof Headers) { + hasContentLength = header.has("content-length"); + header = buildOutgoingHttpHeaders(header, body === null ? void 0 : defaultContentType); + } else if (Array.isArray(header)) { + const headerObj = new Headers(header); + hasContentLength = headerObj.has("content-length"); + header = buildOutgoingHttpHeaders(headerObj, body === null ? void 0 : defaultContentType); + } else { + plainHeaders = true; + for (const key in header) { + if (key.length === 14 && key.toLowerCase() === "content-length") { + hasContentLength = true; + break; + } + if (key.length === 17 && key.toLowerCase() === "transfer-encoding" || key.length === 7 && key.toLowerCase() === "trailer") canAutoLength = false; + } + } + if (!hasContentLength) { + let length; + if (typeof body === "string") length = Buffer.byteLength(body); + else if (body instanceof Uint8Array) length = body.byteLength; + else if (body instanceof Blob) length = body.size; + if (length !== void 0 && (!plainHeaders || !canAutoLength || !trySetContentLength(outgoing, status, length))) { + if (plainHeaders) header = { ...header }; + header["Content-Length"] = length; + } + } + outgoing.writeHead(status, header); + if (body == null) outgoing.end(); + else if (typeof body === "string" || body instanceof Uint8Array) outgoing.end(body); + else if (body instanceof Blob) outgoing.end(new Uint8Array(await body.arrayBuffer())); + else { + flushHeaders(outgoing); + await writeFromReadableStream(body, outgoing)?.catch((e) => handleResponseError(e, outgoing)); + } + outgoing[outgoingEnded]?.(); +}; +const isPromise = (res) => typeof res.then === "function"; +const responseViaResponseObject = async (res, outgoing, options = {}) => { + if (isPromise(res)) if (options.errorHandler) try { + res = await res; + } catch (err) { + const errRes = await options.errorHandler(err); + if (!errRes) return; + res = errRes; + } + else res = await res.catch(handleFetchError); + if (cacheKey in res) return responseViaCache(res, outgoing); + const resHeaderRecord = buildOutgoingHttpHeaders(res.headers, res.body === null ? void 0 : defaultContentType); + if (res.body) { + const reader = res.body.getReader(); + const values = []; + let done = false; + let currentReadPromise = void 0; + if (resHeaderRecord["transfer-encoding"] !== "chunked") { + let maxReadCount = 2; + for (let i = 0; i < maxReadCount; i++) { + currentReadPromise ||= reader.read(); + const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => { + console.error(e); + done = true; + }); + if (!chunk) { + if (i === 1) { + await new Promise((resolve) => setTimeout(resolve)); + maxReadCount = 3; + continue; + } + break; + } + currentReadPromise = void 0; + if (chunk.value) values.push(chunk.value); + if (chunk.done) { + done = true; + break; + } + } + if (done && !("content-length" in resHeaderRecord)) resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0); + } + outgoing.writeHead(res.status, resHeaderRecord); + values.forEach((value) => { + outgoing.write(value); + }); + if (done) outgoing.end(); + else { + if (values.length === 0) flushHeaders(outgoing); + await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise); + } + } else if (resHeaderRecord[X_ALREADY_SENT]) {} else { + outgoing.writeHead(res.status, resHeaderRecord); + outgoing.end(); + } + outgoing[outgoingEnded]?.(); +}; +const getRequestListener = (fetchCallback, options = {}) => { + const autoCleanupIncoming = options.autoCleanupIncoming ?? true; + if (options.overrideGlobalObjects !== false && global.Request !== Request$1) { + Object.defineProperty(global, "Request", { value: Request$1 }); + Object.defineProperty(global, "Response", { value: Response$1 }); + } + return async (incoming, outgoing) => { + let res, req; + let needsBodyCleanup = false; + let closeHandlerAttached = false; + const ensureCloseHandler = () => { + if (!req || closeHandlerAttached) return; + closeHandlerAttached = true; + outgoing.on("close", makeCloseHandler(req, incoming, outgoing, needsBodyCleanup)); + }; + try { + req = newRequest(incoming, options.hostname); + needsBodyCleanup = autoCleanupIncoming && !(incoming.method === "GET" || incoming.method === "HEAD"); + if (needsBodyCleanup) { + incoming[wrapBodyStream] = true; + if (incoming instanceof Http2ServerRequest) outgoing[outgoingEnded] = () => { + if (!incoming.readableEnded) setTimeout(() => { + if (!incoming.readableEnded) setTimeout(() => { + incoming.destroy(); + outgoing.destroy(); + }); + }); + }; + } + res = fetchCallback(req, { + incoming, + outgoing + }); + if (!isPromise(res) && isImmediateCacheableResponse(res)) { + if (needsBodyCleanup && !incoming.readableEnded) outgoing.once("finish", () => { + if (!incoming.readableEnded) drainIncoming(incoming); + }); + return responseViaCache(res, outgoing); + } + ensureCloseHandler(); + } catch (e) { + if (!res) if (options.errorHandler) { + ensureCloseHandler(); + res = await options.errorHandler(req ? e : toRequestError(e)); + if (!res) return; + } else if (!req) res = handleRequestError(); + else res = handleFetchError(e); + else return handleResponseError(e, outgoing); + } + try { + return await responseViaResponseObject(res, outgoing, options); + } catch (e) { + return handleResponseError(e, outgoing); + } + }; +}; + +//#endregion +//#region src/websocket.ts +/** +* @link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent +*/ +const CloseEvent = globalThis.CloseEvent ?? class extends Event { + #eventInitDict; + constructor(type, eventInitDict = {}) { + super(type, eventInitDict); + this.#eventInitDict = eventInitDict; + } + get wasClean() { + return this.#eventInitDict.wasClean ?? false; + } + get code() { + return this.#eventInitDict.code ?? 0; + } + get reason() { + return this.#eventInitDict.reason ?? ""; + } +}; +/** +* Node.js has no global `ErrorEvent`, unlike `Event`/`MessageEvent`/`CloseEvent`. +* @link https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent +*/ +const ErrorEvent = globalThis.ErrorEvent ?? class extends Event { + #eventInitDict; + constructor(type, eventInitDict = {}) { + super(type, eventInitDict); + this.#eventInitDict = eventInitDict; + } + get message() { + return this.#eventInitDict.message ?? ""; + } + get filename() { + return this.#eventInitDict.filename ?? ""; + } + get lineno() { + return this.#eventInitDict.lineno ?? 0; + } + get colno() { + return this.#eventInitDict.colno ?? 0; + } + get error() { + return this.#eventInitDict.error ?? null; + } +}; +const generateConnectionSymbol = () => Symbol("connection"); +const CONNECTION_SYMBOL_KEY = Symbol("CONNECTION_SYMBOL_KEY"); +const WAIT_FOR_WEBSOCKET_SYMBOL = Symbol("WAIT_FOR_WEBSOCKET_SYMBOL"); +const responseHeadersToSkip = new Set([ + "connection", + "content-length", + "keep-alive", + "proxy-authenticate", + "proxy-authorization", + "te", + "trailer", + "transfer-encoding", + "upgrade", + "sec-websocket-accept", + "sec-websocket-extensions", + "sec-websocket-protocol" +]); +const appendResponseHeaders = (headers, responseHeaders) => { + if (!responseHeaders) return; + responseHeaders.forEach((value, key) => { + if (responseHeadersToSkip.has(key.toLowerCase())) return; + headers.push(`${key}: ${value}`); + }); +}; +const rejectUpgradeRequest = (socket, status, responseHeaders) => { + const responseLines = ["Connection: close", "Content-Length: 0"]; + appendResponseHeaders(responseLines, responseHeaders); + socket.end(`HTTP/1.1 ${status.toString()} ${STATUS_CODES[status] ?? ""}\r\n${responseLines.join("\r\n")}\r\n\r +`); +}; +const createUpgradeRequest = (request) => { + const protocol = request.socket.encrypted ? "https" : "http"; + const url = new URL(request.url ?? "/", `${protocol}://${request.headers.host ?? "localhost"}`); + const headers = new Headers(); + for (const key in request.headers) { + const value = request.headers[key]; + if (!value) continue; + headers.append(key, Array.isArray(value) ? value[0] : value); + } + return new Request(url, { headers }); +}; +const setupWebSocket = (options) => { + const { server, fetchCallback, wss } = options; + const waiterMap = /* @__PURE__ */ new Map(); + wss.on("connection", (ws, request) => { + const waiter = waiterMap.get(request); + if (waiter) { + waiter.resolve(ws); + waiterMap.delete(request); + } + }); + const rejectWaiter = (request) => { + const waiter = waiterMap.get(request); + if (waiter) { + waiterMap.delete(request); + waiter.reject(/* @__PURE__ */ new Error("WebSocket handshake aborted")); + } + }; + const waitForWebSocket = (request, connectionSymbol) => { + return new Promise((resolve, reject) => { + waiterMap.set(request, { + resolve, + reject, + connectionSymbol + }); + }); + }; + server.on("upgrade", async (request, socket, head) => { + if (request.headers.upgrade?.toLowerCase() !== "websocket") return; + const env = { + incoming: request, + outgoing: void 0, + wss, + [WAIT_FOR_WEBSOCKET_SYMBOL]: waitForWebSocket + }; + let status = 400; + let responseHeaders; + try { + const response = await fetchCallback(createUpgradeRequest(request), env); + if (response instanceof Response) { + status = response.status; + responseHeaders = response.headers; + } + } catch { + if (server.listenerCount("upgrade") === 1) rejectUpgradeRequest(socket, 500); + return; + } + const waiter = waiterMap.get(request); + if (!waiter || waiter.connectionSymbol !== env[CONNECTION_SYMBOL_KEY]) { + rejectWaiter(request); + if (server.listenerCount("upgrade") === 1) rejectUpgradeRequest(socket, status, responseHeaders); + return; + } + const addResponseHeaders = (headers) => { + appendResponseHeaders(headers, responseHeaders); + }; + const reclaimWaiterOnClose = () => rejectWaiter(request); + socket.once("close", reclaimWaiterOnClose); + wss.on("headers", addResponseHeaders); + try { + wss.handleUpgrade(request, socket, head, (ws) => { + socket.off("close", reclaimWaiterOnClose); + wss.emit("connection", ws, request); + }); + } finally { + wss.off("headers", addResponseHeaders); + } + }); + server.on("close", () => { + wss.close(); + }); +}; +const upgradeWebSocket = defineWebSocketHelper(async (c, events, options) => { + if (c.req.header("upgrade")?.toLowerCase() !== "websocket") return; + const env = c.env; + const waitForWebSocket = env[WAIT_FOR_WEBSOCKET_SYMBOL]; + if (!waitForWebSocket || !env.incoming) return new Response(null, { status: 500 }); + const connectionSymbol = generateConnectionSymbol(); + env[CONNECTION_SYMBOL_KEY] = connectionSymbol; + (async () => { + let ws; + try { + ws = await waitForWebSocket(env.incoming, connectionSymbol); + } catch { + return; + } + const messagesReceivedInStarting = []; + const bufferMessage = (data, isBinary) => { + messagesReceivedInStarting.push([data, isBinary]); + }; + ws.on("message", bufferMessage); + const ctx = { + binaryType: "arraybuffer", + close(code, reason) { + ws.close(code, reason); + }, + protocol: ws.protocol, + raw: ws, + get readyState() { + return ws.readyState; + }, + send(source, opts) { + ws.send(source, { compress: opts?.compress }); + }, + url: new URL(c.req.url) + }; + try { + events?.onOpen?.(new Event("open"), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + const handleMessage = (data, isBinary) => { + const datas = Array.isArray(data) ? data : [data]; + for (const data of datas) try { + events?.onMessage?.(new MessageEvent("message", { data: isBinary ? data instanceof ArrayBuffer ? data : data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) : typeof data === "string" ? data : Buffer.from(data).toString("utf-8") }), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + }; + ws.off("message", bufferMessage); + for (const message of messagesReceivedInStarting) handleMessage(...message); + ws.on("message", (data, isBinary) => { + handleMessage(data, isBinary); + }); + ws.on("close", (code, reason) => { + try { + events?.onClose?.(new CloseEvent("close", { + code, + reason: reason.toString() + }), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + }); + ws.on("error", (error) => { + try { + events?.onError?.(new ErrorEvent("error", { error }), ctx); + } catch (e) { + (options?.onError ?? console.error)(e); + } + }); + })(); + return new Response(); +}); + +//#endregion +//#region src/server.ts +const createAdaptorServer = (options) => { + const fetchCallback = options.fetch; + const requestListener = getRequestListener(fetchCallback, { + hostname: options.hostname, + overrideGlobalObjects: options.overrideGlobalObjects, + autoCleanupIncoming: options.autoCleanupIncoming + }); + const server = (options.createServer || createServer)(options.serverOptions || {}, requestListener); + if (options.websocket && options.websocket.server) { + if (options.websocket.server.options.noServer !== true) throw new Error("WebSocket server must be created with { noServer: true } option"); + setupWebSocket({ + server, + fetchCallback, + wss: options.websocket.server + }); + } + return server; +}; +const serve = (options, listeningListener) => { + const server = createAdaptorServer(options); + server.listen(options?.port ?? 3e3, options.hostname, () => { + const serverInfo = server.address(); + listeningListener && listeningListener(serverInfo); + }); + return server; +}; + +//#endregion +export { RequestError, createAdaptorServer, getRequestListener, serve, upgradeWebSocket }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.cjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.cjs new file mode 100644 index 000000000..bcfb0e52e --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.cjs @@ -0,0 +1,153 @@ +Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +const require_utils_stream = require('./utils/stream.cjs'); +let hono_utils_mime = require("hono/utils/mime"); +let node_fs = require("node:fs"); +let node_path = require("node:path"); + +//#region src/serve-static.ts +const COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i; +const ENCODINGS = { + br: ".br", + zstd: ".zst", + gzip: ".gz" +}; +const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS); +const getStats = (path) => { + let stats; + try { + stats = (0, node_fs.statSync)(path); + } catch {} + return stats; +}; +const BYTE_RANGE_PATTERN = /^(?:bytes=)?(?!-$)(\d*)-(\d*)$/; +const parseByteRange = (range) => { + const match = range.match(BYTE_RANGE_PATTERN); + if (!match) return; + const [, start, end] = match; + if (start === "") return { + type: "suffix", + length: Number(end) + }; + if (end === "") return { + type: "open-ended", + start: Number(start) + }; + return { + type: "bounded", + start: Number(start), + end: Number(end) + }; +}; +const resolveByteRange = (spec, size) => { + if (size === 0) return; + if (spec.type === "suffix") { + if (spec.length === 0) return; + return { + start: Math.max(size - spec.length, 0), + end: size - 1 + }; + } + const end = spec.type === "bounded" ? Math.min(spec.end, size - 1) : size - 1; + if (spec.start >= size || spec.start > end) return; + return { + start: spec.start, + end + }; +}; +const tryDecode = (str, decoder) => { + try { + return decoder(str); + } catch { + return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => { + try { + return decoder(match); + } catch { + return match; + } + }); + } +}; +const tryDecodeURI = (str) => tryDecode(str, decodeURI); +const serveStatic = (options = { root: "" }) => { + const root = options.root || ""; + const optionPath = options.path; + if (root !== "" && !(0, node_fs.existsSync)(root)) console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`); + return async (c, next) => { + if (c.finalized) return next(); + if (c.req.method !== "GET" && c.req.method !== "HEAD") return next(); + let filename; + if (optionPath) filename = optionPath; + else try { + if (!options.allowPercentInPath && c.req.path.includes("%")) throw new Error(); + filename = tryDecodeURI(c.req.path); + if (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}|\\/.test(filename)) throw new Error(); + } catch { + await options.onNotFound?.(c.req.path, c); + return next(); + } + let path = (0, node_path.join)(root, !optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename); + let stats = getStats(path); + if (stats && stats.isDirectory()) { + const indexFile = options.index ?? "index.html"; + path = (0, node_path.join)(path, indexFile); + stats = getStats(path); + } + if (!stats) { + await options.onNotFound?.(path, c); + return next(); + } + const mimeType = (0, hono_utils_mime.getMimeType)(path); + c.header("Content-Type", mimeType || "application/octet-stream"); + if (options.precompressed && (!mimeType || mimeType === "application/octet-stream" || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) { + const acceptEncodingSet = new Set(c.req.header("Accept-Encoding")?.split(",").map((encoding) => encoding.trim())); + for (const encoding of ENCODINGS_ORDERED_KEYS) { + if (!acceptEncodingSet.has(encoding)) continue; + const precompressedStats = getStats(path + ENCODINGS[encoding]); + if (precompressedStats) { + c.header("Content-Encoding", encoding); + c.header("Vary", "Accept-Encoding", { append: true }); + stats = precompressedStats; + path = path + ENCODINGS[encoding]; + break; + } + } + } + let result; + const size = stats.size; + const range = c.req.header("range") || ""; + c.header("Last-Modified", stats.mtime.toUTCString()); + if (c.req.method === "HEAD") { + c.header("Content-Length", size.toString()); + c.status(200); + result = c.body(null); + } else if (!range) { + c.header("Content-Length", size.toString()); + result = c.body(require_utils_stream.createStreamBody((0, node_fs.createReadStream)(path)), 200); + } else { + c.header("Accept-Ranges", "bytes"); + const resolvedRange = resolveByteRange(parseByteRange(range) ?? { + type: "open-ended", + start: 0 + }, size); + if (!resolvedRange) { + c.header("Content-Range", `bytes */${size}`); + result = c.body(null, 416); + } else { + const { start, end } = resolvedRange; + const chunkSize = end - start + 1; + const stream = (0, node_fs.createReadStream)(path, { + start, + end + }); + c.header("Content-Length", chunkSize.toString()); + c.header("Content-Range", `bytes ${start}-${end}/${size}`); + result = c.body(require_utils_stream.createStreamBody(stream), 206); + } + } + await options.onFound?.(path, c); + return result; + }; +}; + +//#endregion +exports.serveStatic = serveStatic; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.d.cts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.d.cts new file mode 100644 index 000000000..f4dae1201 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.d.cts @@ -0,0 +1,19 @@ +import { Context, Env, MiddlewareHandler } from "hono"; + +//#region src/serve-static.d.ts +type ServeStaticOptions = { + /** + * Root path. Relative path is based on current working directory from which the app was started. + */ + root?: string; + path?: string; + index?: string; + precompressed?: boolean; /** Allow percent signs in the routed request path. Defaults to false. */ + allowPercentInPath?: boolean; + rewriteRequestPath?: (path: string, c: Context) => string; + onFound?: (path: string, c: Context) => void | Promise; + onNotFound?: (path: string, c: Context) => void | Promise; +}; +declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler; +//#endregion +export { ServeStaticOptions, serveStatic }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.d.mts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.d.mts new file mode 100644 index 000000000..f4dae1201 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.d.mts @@ -0,0 +1,19 @@ +import { Context, Env, MiddlewareHandler } from "hono"; + +//#region src/serve-static.d.ts +type ServeStaticOptions = { + /** + * Root path. Relative path is based on current working directory from which the app was started. + */ + root?: string; + path?: string; + index?: string; + precompressed?: boolean; /** Allow percent signs in the routed request path. Defaults to false. */ + allowPercentInPath?: boolean; + rewriteRequestPath?: (path: string, c: Context) => string; + onFound?: (path: string, c: Context) => void | Promise; + onNotFound?: (path: string, c: Context) => void | Promise; +}; +declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler; +//#endregion +export { ServeStaticOptions, serveStatic }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.mjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.mjs new file mode 100644 index 000000000..97505a187 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/serve-static.mjs @@ -0,0 +1,152 @@ +import { createStreamBody } from "./utils/stream.mjs"; +import { getMimeType } from "hono/utils/mime"; +import { createReadStream, existsSync, statSync } from "node:fs"; +import { join } from "node:path"; + +//#region src/serve-static.ts +const COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i; +const ENCODINGS = { + br: ".br", + zstd: ".zst", + gzip: ".gz" +}; +const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS); +const getStats = (path) => { + let stats; + try { + stats = statSync(path); + } catch {} + return stats; +}; +const BYTE_RANGE_PATTERN = /^(?:bytes=)?(?!-$)(\d*)-(\d*)$/; +const parseByteRange = (range) => { + const match = range.match(BYTE_RANGE_PATTERN); + if (!match) return; + const [, start, end] = match; + if (start === "") return { + type: "suffix", + length: Number(end) + }; + if (end === "") return { + type: "open-ended", + start: Number(start) + }; + return { + type: "bounded", + start: Number(start), + end: Number(end) + }; +}; +const resolveByteRange = (spec, size) => { + if (size === 0) return; + if (spec.type === "suffix") { + if (spec.length === 0) return; + return { + start: Math.max(size - spec.length, 0), + end: size - 1 + }; + } + const end = spec.type === "bounded" ? Math.min(spec.end, size - 1) : size - 1; + if (spec.start >= size || spec.start > end) return; + return { + start: spec.start, + end + }; +}; +const tryDecode = (str, decoder) => { + try { + return decoder(str); + } catch { + return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => { + try { + return decoder(match); + } catch { + return match; + } + }); + } +}; +const tryDecodeURI = (str) => tryDecode(str, decodeURI); +const serveStatic = (options = { root: "" }) => { + const root = options.root || ""; + const optionPath = options.path; + if (root !== "" && !existsSync(root)) console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`); + return async (c, next) => { + if (c.finalized) return next(); + if (c.req.method !== "GET" && c.req.method !== "HEAD") return next(); + let filename; + if (optionPath) filename = optionPath; + else try { + if (!options.allowPercentInPath && c.req.path.includes("%")) throw new Error(); + filename = tryDecodeURI(c.req.path); + if (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}|\\/.test(filename)) throw new Error(); + } catch { + await options.onNotFound?.(c.req.path, c); + return next(); + } + let path = join(root, !optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename); + let stats = getStats(path); + if (stats && stats.isDirectory()) { + const indexFile = options.index ?? "index.html"; + path = join(path, indexFile); + stats = getStats(path); + } + if (!stats) { + await options.onNotFound?.(path, c); + return next(); + } + const mimeType = getMimeType(path); + c.header("Content-Type", mimeType || "application/octet-stream"); + if (options.precompressed && (!mimeType || mimeType === "application/octet-stream" || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) { + const acceptEncodingSet = new Set(c.req.header("Accept-Encoding")?.split(",").map((encoding) => encoding.trim())); + for (const encoding of ENCODINGS_ORDERED_KEYS) { + if (!acceptEncodingSet.has(encoding)) continue; + const precompressedStats = getStats(path + ENCODINGS[encoding]); + if (precompressedStats) { + c.header("Content-Encoding", encoding); + c.header("Vary", "Accept-Encoding", { append: true }); + stats = precompressedStats; + path = path + ENCODINGS[encoding]; + break; + } + } + } + let result; + const size = stats.size; + const range = c.req.header("range") || ""; + c.header("Last-Modified", stats.mtime.toUTCString()); + if (c.req.method === "HEAD") { + c.header("Content-Length", size.toString()); + c.status(200); + result = c.body(null); + } else if (!range) { + c.header("Content-Length", size.toString()); + result = c.body(createStreamBody(createReadStream(path)), 200); + } else { + c.header("Accept-Ranges", "bytes"); + const resolvedRange = resolveByteRange(parseByteRange(range) ?? { + type: "open-ended", + start: 0 + }, size); + if (!resolvedRange) { + c.header("Content-Range", `bytes */${size}`); + result = c.body(null, 416); + } else { + const { start, end } = resolvedRange; + const chunkSize = end - start + 1; + const stream = createReadStream(path, { + start, + end + }); + c.header("Content-Length", chunkSize.toString()); + c.header("Content-Range", `bytes ${start}-${end}/${size}`); + result = c.body(createStreamBody(stream), 206); + } + } + await options.onFound?.(path, c); + return result; + }; +}; + +//#endregion +export { serveStatic }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.cjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.cjs new file mode 100644 index 000000000..87b8e95ab --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.cjs @@ -0,0 +1,8 @@ +Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +const require_constants = require('../constants-BXAKTxRC.cjs'); + +//#region src/utils/response.ts +const RESPONSE_ALREADY_SENT = new Response(null, { headers: { [require_constants.X_ALREADY_SENT]: "true" } }); + +//#endregion +exports.RESPONSE_ALREADY_SENT = RESPONSE_ALREADY_SENT; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.d.cts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.d.cts new file mode 100644 index 000000000..169a3b551 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.d.cts @@ -0,0 +1,4 @@ +//#region src/utils/response.d.ts +declare const RESPONSE_ALREADY_SENT: Response; +//#endregion +export { RESPONSE_ALREADY_SENT }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.d.mts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.d.mts new file mode 100644 index 000000000..169a3b551 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.d.mts @@ -0,0 +1,4 @@ +//#region src/utils/response.d.ts +declare const RESPONSE_ALREADY_SENT: Response; +//#endregion +export { RESPONSE_ALREADY_SENT }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.mjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.mjs new file mode 100644 index 000000000..92495c1f5 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/response.mjs @@ -0,0 +1,7 @@ +import { t as X_ALREADY_SENT } from "../constants-BLSFu_RU.mjs"; + +//#region src/utils/response.ts +const RESPONSE_ALREADY_SENT = new Response(null, { headers: { [X_ALREADY_SENT]: "true" } }); + +//#endregion +export { RESPONSE_ALREADY_SENT }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.cjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.cjs new file mode 100644 index 000000000..b4f4f7d42 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.cjs @@ -0,0 +1,65 @@ +Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +let node_stream = require("node:stream"); +let node_process = require("node:process"); + +//#region src/utils/stream.ts +const pr54206Applied = () => { + const [major, minor] = node_process.versions.node.split(".").map((component) => parseInt(component)); + return major >= 23 || major === 22 && minor >= 7 || major === 20 && minor >= 18; +}; +const useReadableToWeb = pr54206Applied(); +const createStreamBody = (stream, useNativeReadableToWeb = useReadableToWeb) => { + if (useNativeReadableToWeb) return node_stream.Readable.toWeb(stream); + let controller; + let settled = false; + const cleanup = () => { + stream.off("data", onData); + stream.off("error", onError); + stream.off("end", onTerminate); + stream.off("close", onTerminate); + }; + const settle = (callback) => { + if (settled) return; + settled = true; + cleanup(); + callback?.(); + }; + const onData = (chunk) => { + if (settled || !controller) return; + controller.enqueue(chunk); + if ((controller.desiredSize ?? 0) <= 0) stream.pause(); + }; + const onError = (error) => { + settle(() => { + controller?.error(error); + }); + }; + const onTerminate = () => { + settle(() => { + controller?.close(); + }); + }; + return new ReadableStream({ + start(streamController) { + controller = streamController; + stream.on("data", onData); + stream.on("error", onError); + stream.on("end", onTerminate); + stream.on("close", onTerminate); + stream.pause(); + }, + pull() { + if (!settled) stream.resume(); + }, + cancel() { + settle(); + const ignoreError = () => {}; + stream.on("error", ignoreError); + stream.once("close", () => stream.off("error", ignoreError)); + stream.destroy(); + } + }); +}; + +//#endregion +exports.createStreamBody = createStreamBody; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.d.cts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.d.cts new file mode 100644 index 000000000..5f92c4f79 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.d.cts @@ -0,0 +1,6 @@ +import { ReadStream } from "node:fs"; + +//#region src/utils/stream.d.ts +declare const createStreamBody: (stream: ReadStream, useNativeReadableToWeb?: boolean) => ReadableStream; +//#endregion +export { createStreamBody }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.d.mts b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.d.mts new file mode 100644 index 000000000..5f92c4f79 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.d.mts @@ -0,0 +1,6 @@ +import { ReadStream } from "node:fs"; + +//#region src/utils/stream.d.ts +declare const createStreamBody: (stream: ReadStream, useNativeReadableToWeb?: boolean) => ReadableStream; +//#endregion +export { createStreamBody }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.mjs b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.mjs new file mode 100644 index 000000000..8d18a9a2e --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/dist/utils/stream.mjs @@ -0,0 +1,64 @@ +import { Readable } from "node:stream"; +import { versions } from "node:process"; + +//#region src/utils/stream.ts +const pr54206Applied = () => { + const [major, minor] = versions.node.split(".").map((component) => parseInt(component)); + return major >= 23 || major === 22 && minor >= 7 || major === 20 && minor >= 18; +}; +const useReadableToWeb = pr54206Applied(); +const createStreamBody = (stream, useNativeReadableToWeb = useReadableToWeb) => { + if (useNativeReadableToWeb) return Readable.toWeb(stream); + let controller; + let settled = false; + const cleanup = () => { + stream.off("data", onData); + stream.off("error", onError); + stream.off("end", onTerminate); + stream.off("close", onTerminate); + }; + const settle = (callback) => { + if (settled) return; + settled = true; + cleanup(); + callback?.(); + }; + const onData = (chunk) => { + if (settled || !controller) return; + controller.enqueue(chunk); + if ((controller.desiredSize ?? 0) <= 0) stream.pause(); + }; + const onError = (error) => { + settle(() => { + controller?.error(error); + }); + }; + const onTerminate = () => { + settle(() => { + controller?.close(); + }); + }; + return new ReadableStream({ + start(streamController) { + controller = streamController; + stream.on("data", onData); + stream.on("error", onError); + stream.on("end", onTerminate); + stream.on("close", onTerminate); + stream.pause(); + }, + pull() { + if (!settled) stream.resume(); + }, + cancel() { + settle(); + const ignoreError = () => {}; + stream.on("error", ignoreError); + stream.once("close", () => stream.off("error", ignoreError)); + stream.destroy(); + } + }); +}; + +//#endregion +export { createStreamBody }; \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/@hono/node-server/package.json b/tests/fixtures/npm/node_modules/@hono/node-server/package.json new file mode 100644 index 000000000..b3ab47d33 --- /dev/null +++ b/tests/fixtures/npm/node_modules/@hono/node-server/package.json @@ -0,0 +1,128 @@ +{ + "name": "@hono/node-server", + "version": "2.1.5", + "description": "Node.js Adapter for Hono", + "main": "dist/index.mjs", + "type": "module", + "types": "dist/index.d.mts", + "files": [ + "dist" + ], + "exports": { + ".": { + "import": { + "types": "./dist/index.d.mts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/index.d.cts", + "default": "./dist/index.cjs" + } + }, + "./serve-static": { + "import": { + "types": "./dist/serve-static.d.mts", + "default": "./dist/serve-static.mjs" + }, + "require": { + "types": "./dist/serve-static.d.cts", + "default": "./dist/serve-static.cjs" + } + }, + "./utils/*": { + "import": { + "types": "./dist/utils/*.d.mts", + "default": "./dist/utils/*.mjs" + }, + "require": { + "types": "./dist/utils/*.d.cts", + "default": "./dist/utils/*.cjs" + } + }, + "./conninfo": { + "import": { + "types": "./dist/conninfo.d.mts", + "default": "./dist/conninfo.mjs" + }, + "require": { + "types": "./dist/conninfo.d.cts", + "default": "./dist/conninfo.cjs" + } + }, + "./early-hints": { + "import": { + "types": "./dist/early-hints.d.mts", + "default": "./dist/early-hints.mjs" + }, + "require": { + "types": "./dist/early-hints.d.cts", + "default": "./dist/early-hints.cjs" + } + } + }, + "typesVersions": { + "*": { + ".": [ + "./dist/index.d.mts" + ], + "serve-static": [ + "./dist/serve-static.d.mts" + ], + "utils/*": [ + "./dist/utils/*.d.mts" + ], + "conninfo": [ + "./dist/conninfo.d.mts" + ], + "early-hints": [ + "./dist/early-hints.d.mts" + ] + } + }, + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/honojs/node-server.git" + }, + "homepage": "https://github.com/honojs/node-server", + "author": "Yusuke Wada (https://github.com/yusukebe)", + "publishConfig": { + "registry": "https://registry.npmjs.org", + "access": "public", + "provenance": true + }, + "engines": { + "node": ">=20" + }, + "devDependencies": { + "@hono/eslint-config": "^1.0.1", + "@types/node": "^20.10.0", + "@types/ws": "^8.18.1", + "@whatwg-node/fetch": "^0.9.14", + "eslint": "^9.10.0", + "hono": "^4.13.9", + "np": "^11.2.1", + "prettier": "^3.2.4", + "publint": "^0.3.18", + "tsdown": "^0.20.3", + "typescript": "^5.3.2", + "vitest": "^4.0.18", + "ws": "^8.19.0" + }, + "peerDependencies": { + "hono": "^4 || ^5.0.0-0" + }, + "scripts": { + "test": "vitest", + "build": "tsdown --external hono", + "watch": "tsdown --watch", + "postbuild": "publint", + "prerelease": "pnpm run build && pnpm run test --run", + "release": "np --no-publish", + "typecheck": "tsc --noEmit", + "lint": "eslint src test", + "lint:fix": "eslint src test --fix", + "format": "prettier --check \"src/**/*.{js,ts}\" \"test/**/*.{js,ts}\"", + "format:fix": "prettier --write \"src/**/*.{js,ts}\" \"test/**/*.{js,ts}\"" + } +} \ No newline at end of file diff --git a/tests/fixtures/npm/node_modules/hono/LICENSE b/tests/fixtures/npm/node_modules/hono/LICENSE new file mode 100644 index 000000000..bd174facc --- /dev/null +++ b/tests/fixtures/npm/node_modules/hono/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 - present, Yusuke Wada and Hono contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/tests/fixtures/npm/node_modules/hono/dist/helper/websocket/index.js b/tests/fixtures/npm/node_modules/hono/dist/helper/websocket/index.js new file mode 100644 index 000000000..620de4105 --- /dev/null +++ b/tests/fixtures/npm/node_modules/hono/dist/helper/websocket/index.js @@ -0,0 +1,53 @@ +//#region src/helper/websocket/index.ts +/** +* A context for controlling WebSockets +*/ +var WSContext = class { + #init; + constructor(init) { + this.#init = init; + this.raw = init.raw; + this.url = init.url ? new URL(init.url) : null; + this.protocol = init.protocol ?? null; + } + send(source, options) { + this.#init.send(source, options ?? {}); + } + raw; + binaryType = "arraybuffer"; + get readyState() { + return this.#init.readyState; + } + url; + protocol; + close(code, reason) { + this.#init.close(code, reason); + } +}; +const createWSMessageEvent = (source) => { + return new MessageEvent("message", { data: source }); +}; +/** +* Create a WebSocket adapter/helper +*/ +const defineWebSocketHelper = (handler) => { + return ((...args) => { + if (typeof args[0] === "function") { + const [createEvents, options] = args; + return async function upgradeWebSocket(c, next) { + const result = await handler(c, await createEvents(c), options); + if (result) return result; + await next(); + }; + } else { + const [c, events, options] = args; + return (async () => { + const upgraded = await handler(c, events, options); + if (!upgraded) throw new Error("Failed to upgrade WebSocket"); + return upgraded; + })(); + } + }); +}; +//#endregion +export { WSContext, createWSMessageEvent, defineWebSocketHelper }; diff --git a/tests/fixtures/npm/node_modules/hono/package.json b/tests/fixtures/npm/node_modules/hono/package.json new file mode 100644 index 000000000..6264c824a --- /dev/null +++ b/tests/fixtures/npm/node_modules/hono/package.json @@ -0,0 +1,12 @@ +{ + "name": "hono", + "version": "4.13.13", + "description": "Test fixture: only the hono/ws helper that @hono/node-server's index imports.", + "type": "module", + "license": "MIT", + "exports": { + "./ws": { + "import": "./dist/helper/websocket/index.js" + } + } +} diff --git a/tests/harness/corpus-costs/7287-http-held-request-backpressure.ts.json b/tests/harness/corpus-costs/7287-http-held-request-backpressure.ts.json new file mode 100644 index 000000000..9622e52d1 --- /dev/null +++ b/tests/harness/corpus-costs/7287-http-held-request-backpressure.ts.json @@ -0,0 +1 @@ +{ "plain": 5600, "san": 5600 }