import { Readable } from "node:stream"; import { type IncomingHttpHeaders, type IncomingMessage, type RequestListener, ServerResponse, Server } from "node:http"; import { FetchRouter } from "../http/fetch-router.ts"; import { type RouteDefinition } from "../http/define-route.ts"; import { type MaybePromise } from "../core/types.ts"; export interface NodeRouterOptions { routes?: RouteDefinition[]; } /** * @hidden * * A HTTP router for pure Node.js, you should probably use {@link serveHTTP} * * ```js * import http from "node:http"; * * const router = new NodeRouter(…) * const server = http.createServer(router.forHttpServer()) * server.listen(3000) * ``` */ export declare class NodeRouter { router: FetchRouter; constructor(options?: NodeRouterOptions); getResponse(request: Request): Promise; forHttpServer(): RequestListener; onRouteError(error: unknown, request: Request): void; respond(res: ServerResponse, response: Response): void; } /** * @group HTTP * * Send a web-standards Response to a Node.js [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse) * * ```js * import http from "node:http" * * http.createServer((req, res) => { * applyResponse( * Response.json({ msg: "ok" }), * res * ) * }) * ``` */ export declare function applyResponse(response: Response, res: ServerResponse): void; /** * @group HTTP * * Convert a Node.js [IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage) into a * web-standards [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) * * ```js * import http from "node:http" * * http.createServer((req, res) => { * let request = getFetchRequest(req) * // ... * }) * ``` */ export declare function getFetchRequest(req: IncomingMessage): Request; /** * @group HTTP * * Parse Node.js IncomingHttpHeaders into a web-standards [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object * * ```js * const headers = getFetchHeaders({ accept: "text/plain" }) // Headers * ``` */ export declare function getFetchHeaders(input: IncomingHttpHeaders): Headers; /** * @group HTTP * * Convert the body of a Node.js [IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage) into a Streams API ReadableStream * * ```js * import http from "node:http" * * http.createServer((req, res) => { * let stream = getIncomingMessageBody(req) * // ... * }) * ``` */ export declare function getIncomingMessageBody(req: IncomingMessage): BodyInit | undefined; /** * @group HTTP * * Convert a Streams API ReadableStream into a Readable to later be piped to a Node.js [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse) * * ```js * import http from "node:http" * * http.createServer((req, res) => { * const webResponse = Response.json({ msg: "OK" }) * getResponseReadable(webResponse, res).pipe(res) * }) * ``` * * Pass the second, `res` parameter if you'd like to terminate the web Response if Node.js is terminated. */ export declare function getResponseReadable(response: Response, res?: ServerResponse): Readable; /** @unstable */ export interface ServeHTTPOptions { port: number; hostname?: string; grace?: number; } /** @unstable */ export interface ServeHTTPHandler { (request: Request): MaybePromise; } /** * @unstable * @group HTTP * * A simple abstraction for creating a HTTP server, converting Node.js primatives into Fetch API objects and handling requests through a FetchRouter. * * ```js * const server = await serveHTTP({ port: 3000 }, async (request) => { * return new Response('Hello, There!') * }) * ``` * * This method returns a `node:http` Server after waiting for it to start listening. * The server has an extra `stop` method and also implements `[Symbol.asyncDispose]`. * * When stop is called, or it is dispoed with the `using` keyword, it will attempt to gracefully shutdown the HTTP server, * attempting to terminate each connection. If you created the server with a `grace` option, * it will wait for that maximum time before forcing every connection to close. * To quote the author of stoppable, this is "the way you probably expected it to work by default". * * ```js * async function main() { * await using server = await serveHTTP( * { port: 3000, grace: 5000 }, * () => new Response('ok') * ) * } * * await main() * ``` * * When main function exits, it will automatically close the server with a 5 second grace period. * * The stop method is also useful when used with a [Terminator](/core/#terminator). * */ export declare function serveHTTP(options: ServeHTTPOptions, handler: ServeHTTPHandler): Promise; export interface StopServerOptions { grace?: number; } export interface Stoppable { stop(): Promise; } /** * @hidden * * A port of [stoppable.js](https://github.com/hunterloftis/stoppable/blob/master/lib/stoppable.js), * ported to Gruber to reduce external dependencies and simplify. * * ```js * import http from 'node:http' * * const server = http.createServer(…) * const stop = createStoppable(server, { grace: 10_000 }) * ``` */ export declare function createStoppable(server: Server, options?: StopServerOptions): Stoppable["stop"]; //# sourceMappingURL=node-router.d.ts.map