SPB Git

Repository Public

serve.ts 2.6 KB 37 lines typescript

1// Starts the REAL built server (dist/server.mjs serving dist/web, with the production CSP) on a throwaway database.2import { spawn } from "node:child_process";3import crypto from "node:crypto";4import fs from "node:fs";5import os from "node:os";6import path from "node:path";7import postgres from "postgres";89export const PORT = 8761;10export const MANAGED_PORT = 8762;11export const ORIGIN = `http://localhost:${PORT}`;12export const DB = "postgres://127.0.0.1/sp_vault_e2e_test";1314if (process.argv[1] && /serve\.ts$/.test(process.argv[1])) {15  if (!fs.existsSync("dist/server.mjs") || !fs.existsSync("dist/web/index.html")) throw new Error("build first: node scripts/build.mjs && vite build");16  const sql = postgres(DB, { onnotice: () => {} });17  await sql.unsafe("drop schema public cascade; create schema public;");18  await sql.end();19  const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "spv-e2e-"));20  // The isolated managed-secrets process (SP Vault Connect), with a throwaway key and token, so that the Connect UI is exercised.21  const keyFile = path.join(dataDir, "managed.key"), tokenFile = path.join(dataDir, "managed.token");22  fs.writeFileSync(keyFile, crypto.randomBytes(32).toString("base64"), { mode: 0o600 });23  fs.writeFileSync(tokenFile, crypto.randomBytes(32).toString("base64url"), { mode: 0o600 });24  const managed = spawn("node", ["dist/managed.mjs"], { stdio: ["ignore", "inherit", "inherit"],25    env: { ...process.env, SPV_MANAGED_PORT: String(MANAGED_PORT), SPV_MANAGED_KEY_FILE: keyFile, SPV_MANAGED_TOKEN_FILE: tokenFile, SPV_MANAGED_DATABASE_URL: DB, SPV_LOG_LEVEL: "warn" } });26  // --import: see static-headers-shim.mjs (works around a server bug in the static-file cache header; remove once fixed).27  const server = spawn("node", ["dist/server.mjs"], {28    stdio: ["ignore", "inherit", "inherit"],29    env: { ...process.env, PORT: String(PORT), HOST: "127.0.0.1", DATABASE_URL: DB, SPV_ORIGIN: ORIGIN, SPV_RP_ID: "localhost", SPV_DATA_DIR: dataDir, SPV_WEB_DIR: "dist/web",30      SPV_RATE_SCALE: "50", SPV_LOG_LEVEL: "warn", SPV_BREACH_UPSTREAM: `http://127.0.0.1:${PORT}/api/health/ready?`,31      SPV_MANAGED_URL: `http://127.0.0.1:${MANAGED_PORT}`, SPV_MANAGED_TOKEN_FILE: tokenFile },32  });33  const killAll = () => { for (const p of [server, managed]) if (p.pid) { try { process.kill(p.pid, "SIGTERM"); } catch { /* already gone */ } } fs.rmSync(dataDir, { recursive: true, force: true }); };34  process.on("SIGTERM", () => { killAll(); process.exit(0); }); process.on("SIGINT", () => { killAll(); process.exit(0); });35  server.on("exit", (code) => { killAll(); process.exit(code ?? 0); });36}37
Files main
Menu

Theme