import type http from "node:http"; import { AsyncLocalStorage } from "node:async_hooks"; import { Container } from "@webiny/di"; import { HandlerApp } from "~/index.js"; import { EventDispatcher } from "~/index.js"; import type { HandlerSetup } from "~/index.js"; import { createNodeHttpServer } from "~/nodeHttp/server/createNodeHttpServer.js"; import { HandlerAppEventDispatcher } from "~/nodeHttp/server/HandlerAppEventDispatcher.js"; import { handleShutdownSignals } from "~/nodeHttp/server/handleShutdownSignals.js"; export interface CreateServerHandlerOptions { root: HandlerSetup; child?: HandlerSetup; /** * Called once, at startup, after the root container is built and the HTTP server is created — * before it starts listening. Lets a transport attach to the raw server (e.g. a WebSockets * upgrade handler) using the already-initialized root container. */ onServer?: (server: http.Server, rootContainer: Container) => void | Promise; /** * Called once, when the process is asked to stop (SIGTERM or SIGINT), after the server stops * taking connections and before the process exits. Gets at most 8 seconds. Without it, the * process keeps Node's default and exits at once. */ onShutdown?: (rootContainer: Container) => Promise; /** * Exit cleanly on SIGTERM or SIGINT (close the server, let busy connections finish, exit within * 8 seconds) without an `onShutdown` hook, so the root container isn't built just to shut down. */ exitOnShutdownSignal?: boolean; } export async function createServerHandler( options: CreateServerHandlerOptions ): Promise { /* * Taken here, at boot and outside any request, so dispatched events never inherit a request's * async context. */ const runInBootContext = AsyncLocalStorage.snapshot(); const app = HandlerApp.init({ root: async container => { // Registered first, so everything `root` registers can depend on it. const dispatcher = new HandlerAppEventDispatcher(app, runInBootContext); container.registerInstance(EventDispatcher, dispatcher); await options.root(container); }, child: options.child }); const server = createNodeHttpServer(app); if (options.onServer) { // Build the root container eagerly (rather than lazily on the first request) so `onServer` can // hand it — and the running HTTP server — to transport add-ons like WebSockets at startup. const rootContainer = await app.getRootContainer(); await options.onServer(server, rootContainer); } const onShutdown = options.onShutdown; if (onShutdown || options.exitOnShutdownSignal) { handleShutdownSignals({ server, shutdown: async () => { if (!onShutdown) { return; } const rootContainer = await app.getRootContainer(); await onShutdown(rootContainer); } }); } return server; }