everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
179 lines (177 loc) • 6.31 kB
JavaScript
let node_path = require("node:path");
let effect = require("effect");
let node_net = require("node:net");
//#region src/infra/preflight.ts
var PreflightConnectionError = class extends effect.Data.TaggedError("PreflightConnectionError") {
get message() {
return `Postgres connection failed: ${this.cause instanceof Error ? this.cause.message : String(this.cause)}`;
}
};
const scheduleTimeout = (ms, fn) => setTimeout(fn, ms);
function parseLocalUrl(url) {
try {
const parsed = new URL(url);
const host = parsed.hostname;
if (host !== "localhost" && host !== "127.0.0.1" && host !== "::1") return null;
const port = Number.parseInt(parsed.port, 10);
if (Number.isNaN(port)) return {
host,
port: parsed.protocol === "redis:" ? 6379 : 5432
};
return {
host,
port
};
} catch {
return null;
}
}
function checkTcpReachable(host, port, timeoutMs = 2e3) {
return effect.Effect.callback((resume) => {
const socket = (0, node_net.createConnection)({
host,
port
});
const timer = scheduleTimeout(timeoutMs, () => {
socket.destroy();
resume(effect.Effect.succeed(false));
});
socket.once("connect", () => {
clearTimeout(timer);
socket.destroy();
resume(effect.Effect.succeed(true));
});
socket.once("error", () => {
clearTimeout(timer);
socket.destroy();
resume(effect.Effect.succeed(false));
});
});
}
function checkPgConnection(url) {
return effect.Effect.gen(function* () {
if (!(yield* checkTcpReachable(new URL(url).hostname, Number(new URL(url).port) || 5432, 4e3))) return false;
return yield* effect.Effect.tryPromise({
try: async () => {
const { Pool } = await import("pg");
const pool = new Pool({
connectionString: url,
connectionTimeoutMillis: 3e3
});
try {
const client = await pool.connect();
try {
await client.query("SELECT 1");
return true;
} finally {
client.release();
}
} finally {
await pool.end().catch(() => {});
}
},
catch: (cause) => new PreflightConnectionError({ cause })
}).pipe(effect.Effect.orElseSucceed(() => false));
});
}
function preflightTargetsFromEnv(env) {
const targets = [];
for (const [secret, value] of Object.entries(env)) {
if (!value) continue;
if (secret.endsWith("_DATABASE_URL")) {
const parsed = parseLocalUrl(value);
if (parsed) targets.push({
secret,
host: parsed.host,
port: parsed.port,
kind: "postgres",
url: value
});
} else if (secret.endsWith("_REDIS_URL")) {
const parsed = parseLocalUrl(value);
if (parsed) targets.push({
secret,
host: parsed.host,
port: parsed.port,
kind: "redis",
url: value
});
}
}
return targets;
}
/**
* The one corruption preflight can catch without I/O: two or more
* `*_DATABASE_URL` secrets resolving to the same file-backed PGlite data dir.
* Multiple live PGlite instances share no coordination — they checkpoint
* stale pages over each other's committed data (ADR 0044). In-memory dirs are
* exempt (instances never share state), and a single occupant is fine. This
* fires after the one-time repair (`env/legacy-databases`) has rewritten the
* known-broken shape, so what remains is operator-authored — fail loudly
* rather than boot a corrupting composition.
*/
function pgliteDirCollisions(env) {
const byDir = /* @__PURE__ */ new Map();
for (const [secret, value] of Object.entries(env)) {
if (!secret.endsWith("_DATABASE_URL") || !value?.startsWith("pglite:")) continue;
const rawDir = value.slice(7);
if (rawDir === ":memory:" || rawDir.endsWith("/:memory:")) continue;
const dir = (0, node_path.resolve)(rawDir);
const secrets = byDir.get(dir);
if (secrets) secrets.push(secret);
else byDir.set(dir, [secret]);
}
const failures = [];
for (const [dir, secrets] of byDir) {
if (secrets.length < 2) continue;
failures.push({
secret: secrets.join(", "),
host: dir,
port: 0,
error: `${secrets.join(" and ")} resolve to the same PGlite data dir "${dir}". Multiple live PGlite instances on one data dir silently corrupt committed data (ADR 0044) — give each workspace its own data dir (pglite:.bos/db/<slug>) or point the extras at postgres:// URLs.`,
tcpReachable: false
});
}
return failures;
}
function preflightLocalInfra(env, overrides) {
const merged = overrides ? {
...env,
...overrides
} : env;
const targets = preflightTargetsFromEnv(merged);
const collisions = pgliteDirCollisions(merged);
if (targets.length === 0) return effect.Effect.succeed(collisions);
return effect.Effect.forEach(targets, (target) => effect.Effect.gen(function* () {
if (target.kind === "postgres") {
if (yield* checkPgConnection(target.url)) return null;
const tcpOk = yield* checkTcpReachable(target.host, target.port);
const pluginContext = target.secret === "API_DATABASE_URL" || target.secret === "AUTH_DATABASE_URL" ? " Point it at a postgres:// URL you control (local persistence needs no server — clear the URL to fall back to the per-workspace .bos/db/<slug> PGlite)." : ` The plugin for ${target.secret} runs inside the local host process, so this DB must be reachable. Point it at a postgres:// URL you control or clear the URL to fall back to the per-workspace .bos/db/<slug> PGlite.`;
if (tcpOk) return {
secret: target.secret,
host: target.host,
port: target.port,
error: `${target.secret} at ${target.host}:${target.port} is reachable but Postgres connection failed. Check credentials and database name.${pluginContext}`,
tcpReachable: true
};
return {
secret: target.secret,
host: target.host,
port: target.port,
error: `${target.secret} points to ${target.host}:${target.port} but nothing is listening.${pluginContext}`,
tcpReachable: false
};
}
if (yield* checkTcpReachable(target.host, target.port)) return null;
return {
secret: target.secret,
host: target.host,
port: target.port,
error: `${target.secret} points to ${target.host}:${target.port} but nothing is listening`,
tcpReachable: false
};
}), { concurrency: "unbounded" }).pipe(effect.Effect.map((results) => [...collisions, ...results.filter((r) => r !== null)]));
}
//#endregion
exports.preflightLocalInfra = preflightLocalInfra;
//# sourceMappingURL=preflight.cjs.map