2026-08-15 18:37:30 +02:00
|
|
|
import "./src/bootstrap/async-local-storage-polyfill";
|
|
|
|
|
import { createServer } from "node:http";
|
2026-08-16 11:03:25 +02:00
|
|
|
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();
|
|
|
|
|
|
2026-08-16 11:03:25 +02:00
|
|
|
// `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));
|
|
|
|
|
const app = next({ dev, dir, hostname, port });
|
2026-08-15 18:37:30 +02:00
|
|
|
const handle = app.getRequestHandler();
|
|
|
|
|
|
|
|
|
|
app.prepare().then(() => {
|
2026-08-15 19:46:41 +02:00
|
|
|
const nextUpgradeHandler = app.getUpgradeHandler();
|
|
|
|
|
|
2026-08-15 18:37:30 +02:00
|
|
|
const httpServer = createServer((req, res) => {
|
2026-08-15 19:41:37 +02:00
|
|
|
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) => {
|
2026-08-15 19:41:37 +02:00
|
|
|
const { pathname } = new URL(req.url ?? "/", "http://internal");
|
2026-08-15 18:37:30 +02:00
|
|
|
if (pathname !== "/ws/runs") {
|
2026-08-15 19:46:41 +02:00
|
|
|
// 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"));
|
|
|
|
|
});
|