/** * Run a nifra app (or any Web-`fetch` handler) on Deno via `Deno.serve`. * * import { serve } from "@nifrajs/deno" * import { server } from "@nifrajs/core/server" * const app = server().get("/", () => ({ ok: true })) * await serve(app, { port: 3000 }) * * `Deno.serve`'s handler already receives a Web `Request` and returns a `Response`, so - * unlike `@nifrajs/node` - there's no stream bridge: the app's `fetch` *is* the handler. * This adapter adds a Bun-`listen()`-style graceful `stop()` (Deno's `shutdown()` drains * in-flight requests) and opt-in signal handling. The app-level request timeout and body * cap ride along inside `app.fetch`, so they apply here with no extra wiring. */ /** A received frame, normalized: text → `string`, binary → `Uint8Array`. */ type NifraWsData = string | Uint8Array; /** The portable socket a nifra WS handler sees (mirror of core's `NifraWebSocket`). */ interface NifraWs { send(data: string | ArrayBufferView | ArrayBuffer): void; close(code?: number, reason?: string): void; readonly readyState: number; subscribe(topic: string): void; unsubscribe(topic: string): void; data: unknown; readonly raw: unknown; } /** A nifra WS route's post-upgrade lifecycle (mirror of core's `WebSocketHandler`). */ interface NifraWsHandler { open?(ws: NifraWs): void | Promise; message?(ws: NifraWs, data: NifraWsData): void | Promise; close?(ws: NifraWs, code: number, reason: string): void | Promise; error?(ws: NifraWs, error: unknown): void | Promise; } /** Mirror of core's `TopicRegistry` surface - the app's pub/sub the adapter wires `ws.subscribe` to. */ interface WsPubSub { subscribe(topic: string, ws: NifraWs): void; unsubscribe(topic: string, ws: NifraWs): void; unsubscribeAll(ws: NifraWs): void; } /** Mirror of core's `WebSocketUpgradeOutcome` - what `resolveWebSocketUpgrade` returns. */ type WsUpgradeOutcome = { readonly kind: "pass"; } | { readonly kind: "reject"; readonly response: Response; } | { readonly kind: "upgrade"; readonly handler: NifraWsHandler; readonly data: unknown; readonly pubsub: WsPubSub; readonly maxPayloadBytes?: number; }; /** Anything exposing a Web `fetch` handler - a nifra `app`, for instance. */ export interface FetchHandler { fetch(request: Request, platform?: { readonly clientIp?: string; }): Response | Promise; /** A nifra app also exposes this WS-upgrade seam; present → this adapter serves `app.ws()` routes * via `Deno.upgradeWebSocket`. Absent (a plain `{ fetch }` handler) → HTTP only. */ resolveWebSocketUpgrade?(request: Request, platform?: { readonly clientIp?: string; }): WsUpgradeOutcome | Promise; } export interface ServeOptions { readonly port: number; readonly hostname?: string; /** * Install SIGTERM/SIGINT handlers that call `stop()` for a graceful drain on * `docker stop` / Ctrl-C. Off by default - taking over process signals is opt-in, * mirroring nifra's Bun `listen({ gracefulSignals })`. */ readonly signals?: boolean; } export interface DenoServer { /** The bound port (resolved when `port: 0` is requested). */ readonly port: number; /** * Stop accepting connections, let in-flight requests drain (up to `drainMs`), then * force-close stragglers. Mirrors nifra's Bun `stop()`. */ stop(options?: { drainMs?: number; }): Promise; } /** * Serve a Web-`fetch` app on Deno. Returns once bound, so `port` is the real one * (matters for `port: 0`). */ export declare function serve(app: FetchHandler, options: ServeOptions): Promise; export {}; //# sourceMappingURL=index.d.ts.map