import "dotenv/config"; import { ensureTenant } from "./src/modules/2-identity/db/repo.js"; import { createApp } from "./src/app.js"; async function configureUndiciDispatcher() { // Node 18+ usa undici debajo de fetch. Esto suele arreglar timeouts “fantasma” por keep-alive/pooling. // Nota: si el módulo `undici` no está disponible, no rompemos el arranque (solo logueamos warning). try { const { setGlobalDispatcher, Agent } = await import("undici"); setGlobalDispatcher( new Agent({ connections: 10, pipelining: 0, keepAliveTimeout: 10_000, keepAliveMaxTimeout: 10_000, }) ); console.log("[http] undici global dispatcher configured"); } catch (e) { console.warn("[http] undici dispatcher not configured:", e?.message || e); } } /** * --- Tenant --- */ const TENANT_KEY = process.env.TENANT_KEY || "piaf"; let TENANT_ID = null; /** * --- Boot --- */ const port = process.env.PORT || 3000; (async function boot() { await configureUndiciDispatcher(); TENANT_ID = await ensureTenant({ key: TENANT_KEY, name: TENANT_KEY.toUpperCase() }); const app = createApp({ tenantId: TENANT_ID }); app.listen(port, () => console.log(`UI: http://localhost:${port} (tenant=${TENANT_KEY})`)); })().catch((err) => { console.error("Boot failed:", err); process.exit(1); });