Files
triggershell/server.ts
T

83 lines
3.0 KiB
TypeScript
Raw Normal View History

2026-08-15 18:37:30 +02:00
import "./src/bootstrap/async-local-storage-polyfill";
import { createServer } from "node:http";
import path from "node:path";
import { fileURLToPath } from "node:url";
2026-08-15 18:37:30 +02:00
import next from "next";
import { WebSocketServer } from "ws";
import { getConfig } from "./src/lib/config/load";
import { migrateOnBoot } from "./src/lib/db/client";
import { syncAuthFromConfig } from "./src/lib/auth/sync";
import { reconcileOrphanedRuns } from "./src/lib/runner/engine";
import { killAllRuns } from "./src/lib/runner/registry";
import { attachWsServer, authenticateUpgrade } from "./src/lib/ws/server";
const dev = process.env.NODE_ENV !== "production";
const { config } = getConfig();
const port = Number(process.env.PORT ?? config.server.port);
const hostname = process.env.HOST ?? config.server.host;
migrateOnBoot();
syncAuthFromConfig();
reconcileOrphanedRuns();
// `dir` must be this file's own directory, not `process.cwd()` - when launched by the installed
// `triggershell` CLI, the working directory is wherever the user's config lives, not the package.
const dir = path.dirname(fileURLToPath(import.meta.url));
// Exposed via `process.env` (not just the local `dir` const) so Route Handlers/Server Components -
// compiled through Next's own module graph, separate from this file's - can find it too. See
// `docs.ts`'s use of this and the `globalThis` comment in `runner/events.ts` for the same reasoning.
process.env.TRIGGERSHELL_APP_ROOT = dir;
const app = next({ dev, dir, hostname, port });
2026-08-15 18:37:30 +02:00
const handle = app.getRequestHandler();
app.prepare().then(() => {
const nextUpgradeHandler = app.getUpgradeHandler();
2026-08-15 18:37:30 +02:00
const httpServer = createServer((req, res) => {
handle(req, res);
2026-08-15 18:37:30 +02:00
});
const wss = new WebSocketServer({ noServer: true });
attachWsServer(wss);
httpServer.on("upgrade", (req, socket, head) => {
const { pathname } = new URL(req.url ?? "/", "http://internal");
2026-08-15 18:37:30 +02:00
if (pathname !== "/ws/runs") {
// Anything else (e.g. Next's own dev-mode HMR websocket at /_next/hmr) is Next's to handle.
nextUpgradeHandler(req, socket, head).catch(() => socket.destroy());
2026-08-15 18:37:30 +02:00
return;
}
authenticateUpgrade(req)
.then((ok) => {
if (!ok) {
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
socket.destroy();
return;
}
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit("connection", ws, req);
});
})
.catch(() => socket.destroy());
});
httpServer.listen(port, hostname, () => {
console.log(
`> triggershell ready on http://${hostname}:${port} (${dev ? "development" : "production"})`,
);
});
const shutdown = (signal: string) => {
console.log(`> received ${signal}, shutting down...`);
killAllRuns();
httpServer.close(() => process.exit(0));
// Force-exit if graceful shutdown hangs (e.g. a stuck WS connection).
setTimeout(() => process.exit(1), 5000).unref();
};
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
});