85 lines
No EOL
3.7 KiB
TypeScript
85 lines
No EOL
3.7 KiB
TypeScript
import { FetchHandler, NodeHttpHandler, NodeServerRequest, NodeServerResponse, Server, ServerOptions, ServerRequest, TrustProxyOption } from "../_chunks/types.mjs";
|
|
import { FastURL } from "../_chunks/_url.mjs";
|
|
import { Readable } from "node:stream";
|
|
type NodeRequestContext = {
|
|
req: NodeServerRequest;
|
|
res?: NodeServerResponse;
|
|
/**
|
|
* Maximum allowed size (in bytes) for the request body, enforced for both the
|
|
* buffered reads and the streamed body. See `ServerOptions.maxRequestBodySize`.
|
|
*/
|
|
maxRequestBodySize?: number;
|
|
/**
|
|
* Whether to trust `X-Forwarded-*` / `:scheme` headers when deriving the
|
|
* request protocol, host and client IP. See `ServerOptions.trustProxy`.
|
|
*/
|
|
trustProxy?: TrustProxyOption;
|
|
};
|
|
declare const NodeRequest: {
|
|
new (nodeCtx: NodeRequestContext): ServerRequest;
|
|
};
|
|
/**
|
|
* Undici uses an incompatible Request constructor depending on private property accessors.
|
|
*
|
|
* This utility, patches global Request to support `new Request(req)` in Node.js.
|
|
*
|
|
* Alternatively you can use `new Request(req._request || req)` instead of patching global Request.
|
|
*/
|
|
declare function patchGlobalRequest(): typeof Request;
|
|
type PreparedNodeResponseBody = string | Buffer | Uint8Array | DataView | ReadableStream | Readable | undefined | null;
|
|
interface PreparedNodeResponse {
|
|
status: number;
|
|
statusText: string;
|
|
/** Flat rawHeaders-style list: `[name1, value1, name2, value2, …]` */
|
|
headers: string[];
|
|
body: PreparedNodeResponseBody;
|
|
}
|
|
/**
|
|
* Fast Response for Node.js runtime
|
|
*
|
|
* It is faster because in most cases it doesn't create a full Response instance.
|
|
*/
|
|
declare const NodeResponse: {
|
|
new (body?: BodyInit | null, init?: ResponseInit): globalThis.Response & {
|
|
_toNodeResponse: () => PreparedNodeResponse;
|
|
};
|
|
};
|
|
type NodeResponse = InstanceType<typeof NodeResponse>;
|
|
/**
|
|
* Sends a web `Response` to a Node.js `ServerResponse`.
|
|
*
|
|
* The returned promise resolves once the response has been fully sent
|
|
* (kept for `toNodeHandler` consumers that await completion).
|
|
*/
|
|
declare function sendNodeResponse(nodeRes: NodeServerResponse, webRes: Response | NodeResponse): Promise<void>;
|
|
/**
|
|
* Calls a Node.js HTTP Request handler with a Fetch API Request object and returns a Response object.
|
|
*
|
|
* If the web Request contains an existing Node.js req/res pair (indicating it originated from a Node.js server from srvx/node), it will be called directly.
|
|
*
|
|
* Otherwise, new Node.js IncomingMessage and ServerResponse objects are created and linked to a custom Duplex stream that bridges the Fetch API streams with Node.js streams.
|
|
*
|
|
* The handler is invoked with these objects, and the response is constructed from the ServerResponse once it is finished.
|
|
*
|
|
* @experimental Behavior might be unstable.
|
|
*/
|
|
declare function fetchNodeHandler(handler: NodeHttpHandler, req: ServerRequest): Promise<Response>;
|
|
type AdapterMeta = {
|
|
__nodeHandler?: NodeHttpHandler;
|
|
__fetchHandler?: FetchHandler;
|
|
};
|
|
/**
|
|
* Converts a Fetch API handler to a Node.js HTTP handler.
|
|
*/
|
|
declare function toNodeHandler(handler: FetchHandler & AdapterMeta, options?: {
|
|
maxRequestBodySize?: number;
|
|
trustProxy?: TrustProxyOption;
|
|
}): NodeHttpHandler & AdapterMeta;
|
|
/**
|
|
* Converts a Node.js HTTP handler into a Fetch API handler.
|
|
*
|
|
* @experimental Behavior might be unstable and won't work in Bun and Deno currently (tracker: https://github.com/h3js/srvx/issues/132)
|
|
*/
|
|
declare function toFetchHandler(handler: NodeHttpHandler & AdapterMeta): FetchHandler & AdapterMeta;
|
|
declare function serve(options: ServerOptions): Server;
|
|
export { type AdapterMeta, NodeResponse as FastResponse, FastURL, NodeRequest, NodeResponse, fetchNodeHandler, patchGlobalRequest, sendNodeResponse, serve, toFetchHandler, toNodeHandler }; |