From d3c6ba9f4b442b5a42cd5e83a51125e89c159cf6 Mon Sep 17 00:00:00 2001 From: Patrick Lenihan <83040764+Game4Move78@users.noreply.github.com> Date: Mon, 11 Aug 2025 15:58:47 +0100 Subject: [PATCH] [PM-20220] feat: Add support for fd and unix socket bindings (#14262) * Add support for unix sockets with (unix://) scheme * Add support for listening socket FD (fd+listening:// scheme) * Add support for connected socket FD (fd+connected:// scheme) --------- Co-authored-by: Addison Beck --- apps/cli/src/commands/serve.command.ts | 33 ++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/apps/cli/src/commands/serve.command.ts b/apps/cli/src/commands/serve.command.ts index 801f505f1ae..c0ec37d3c9c 100644 --- a/apps/cli/src/commands/serve.command.ts +++ b/apps/cli/src/commands/serve.command.ts @@ -1,3 +1,6 @@ +import http from "node:http"; +import net from "node:net"; + import * as koaRouter from "@koa/router"; import { OptionValues } from "commander"; import * as koa from "koa"; @@ -50,11 +53,33 @@ export class ServeCommand { this.serveConfigurator.configureRouter(router); - server - .use(router.routes()) - .use(router.allowedMethods()) - .listen(port, hostname === "all" ? null : hostname, () => { + server.use(router.routes()).use(router.allowedMethods()); + + if (hostname.startsWith("fd+connected://")) { + const fd = parseInt(hostname.slice("fd+connected://".length)); + const httpServer = http.createServer(server.callback()); + const socket = new net.Socket({ fd: fd, readable: true, writable: true }); + // allow idle sockets, incomplete handshakes and slow requests + httpServer.keepAliveTimeout = 0; + httpServer.headersTimeout = 0; + httpServer.timeout = 0; + socket.pause(); + httpServer.emit("connection", socket); + socket.resume(); // Let the HTTP parser start reading + } else if (hostname.startsWith("fd+listening://")) { + const fd = parseInt(hostname.slice("fd+listening://".length)); + server.listen({ fd }, () => { + this.serviceContainer.logService.info("Listening on " + hostname); + }); + } else if (hostname.startsWith("unix://")) { + const socketPath = hostname.slice("unix://".length); + server.listen(socketPath, () => { + this.serviceContainer.logService.info("Listening on " + hostname); + }); + } else { + server.listen(port, hostname === "all" ? null : hostname, () => { this.serviceContainer.logService.info("Listening on " + hostname + ":" + port); }); + } } }