import type { RequestListener } from 'node:http'; import type { MaybePromise, MaybePromiseLike } from '@whatwg-node/promise-helpers'; import type { NodeRequest, NodeResponse } from './utils.cjs'; import { UWSHandler, UWSRequest, UWSResponse } from './uwebsockets.cjs'; export interface FetchEvent extends Event { waitUntil(f: MaybePromise): void; request: Request; respondWith(r: MaybePromiseLike): void; } export interface ServerAdapterBaseObject = ServerAdapterRequestHandler> { /** * An async function that takes `Request` and the server context and returns a `Response`. * If you use `requestListener`, the server context is `{ req: IncomingMessage, res: ServerResponse }`. */ handle: THandleRequest; } export interface ServerAdapterObject extends EventListenerObject, AsyncDisposable { /** * A basic request listener that takes a `Request` with the server context and returns a `Response`. */ handleRequest: (request: Request, ctx: TServerContext & Partial) => MaybePromise; /** * WHATWG Fetch spec compliant `fetch` function that can be used for testing purposes. */ fetch(request: Request, ctx: TServerContext): MaybePromise; fetch(request: Request, ...ctx: Partial[]): MaybePromise; fetch(urlStr: string, ctx: TServerContext): MaybePromise; fetch(urlStr: string, ...ctx: Partial[]): MaybePromise; fetch(urlStr: string, init: RequestInit, ctx: TServerContext): MaybePromise; fetch(urlStr: string, init: RequestInit, ...ctx: Partial[]): MaybePromise; fetch(url: URL, ctx: TServerContext): MaybePromise; fetch(url: URL, ...ctx: Partial[]): MaybePromise; fetch(url: URL, init: RequestInit, ctx: TServerContext): MaybePromise; fetch(url: URL, init: RequestInit, ...ctx: Partial[]): MaybePromise; /** * This function takes Node's request and response objects and returns a WHATWG Fetch spec compliant `Response` object. */ handleNodeRequestAndResponse(nodeRequest: NodeRequest, nodeResponseOrContainer: { raw: NodeResponse; } | NodeResponse, ...ctx: Partial[]): MaybePromise; /** * A request listener function that can be used with any Node server variation. */ requestListener: RequestListener; handleUWS: UWSHandler; handle(req: NodeRequest, res: NodeResponse, ...ctx: Partial[]): Promise; handle(requestLike: RequestLike, ...ctx: Partial[]): MaybePromise; handle(request: Request, ...ctx: Partial[]): MaybePromise; handle(request: Request, ...ctx: Partial[]): MaybePromise; handle(fetchEvent: FetchEvent & Partial, ...ctx: Partial[]): void; handle(res: UWSResponse, req: UWSRequest, ...ctx: Partial[]): Promise; handle(container: { request: Request; } & Partial, ...ctx: Partial[]): MaybePromise; disposableStack: AsyncDisposableStack; dispose(): Promise | void; /** * Register a promise that should be waited in the background for before the server process is exited. * * This also avoids unhandled promise rejections, which would otherwise cause the process to exit on some environment like Node. * @param promise The promise to wait for * @returns */ waitUntil: WaitUntilFn; } export interface RequestLike { url: string; method: string; headers: HeadersLike; body?: AsyncIterable | null; } export interface HeadersLike extends Iterable<[string, string]> { get(name: string): string | null | undefined; set(name: string, value: string): void; has(name: string): boolean; } export type ServerAdapter> = TBaseObject & ServerAdapterObject['handle'] & ServerAdapterObject; export type ServerAdapterRequestHandler = (request: Request, ctx: TServerContext & ServerAdapterInitialContext, fetchAPI: FetchAPI) => MaybePromise; export type ServerAdapterNodeContext = { req: NodeRequest; res: NodeResponse; }; export type WaitUntilFn = (promise: Promise | void) => void; export type FetchAPI = ReturnType; export type ServerAdapterInitialContext = { /** * Register a promise that should be waited in the background for before the server process is exited. * * This also avoids unhandled promise rejections, which would otherwise cause the process to exit on some environment like Node. * @param promise The promise to wait for * @returns */ waitUntil: WaitUntilFn; };