globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { Tt as sql } from "./d1_DTqC4o_t.mjs";
import { d as mapContentError, u as jsonOk } from "./crypto_CY2zBtG3.mjs";
import { n as getFullDb } from "./db_DCN9iNkm.mjs";
import { i as requireAdmin } from "./auth_B0DWM0G6.mjs";
import { env } from "cloudflare:workers";
//#region ../../packages/util/dist/health.js
/**
* Run all checks (settled — a thrown/rejected check becomes `status:'down'`, never propagates),
* roll up to the worst status: any 'down' => 'down'; else any 'degraded' => 'degraded'; else 'ok'.
* An empty check set rolls up to 'ok' (nothing unhealthy).
*/
async function aggregateHealth(checks) {
	const results = (await Promise.allSettled(checks.map(async (c) => c.check()))).map((s, i) => {
		if (s.status === "fulfilled") return s.value;
		return {
			name: checks[i].name,
			status: "down",
			detail: "check failed"
		};
	});
	return {
		status: rollup(results),
		checks: results
	};
}
function rollup(results) {
	if (results.some((r) => r.status === "down")) return "down";
	if (results.some((r) => r.status === "degraded")) return "degraded";
	return "ok";
}
//#endregion
//#region ../../packages/db/dist/health.js
/**
* A `SELECT 1` round-trip probe over any query-capable handle.
*
* Returns a `HealthCheck` whose `check()` NEVER throws — a failed query becomes
* `status:'down'` with a GENERIC `detail`. The raw driver error is deliberately
* swallowed: it can carry a DSN, credentials, or host topology and must never
* reach an admin screen or log (info-disclosure hard floor).
*/
function healthCheck(db, opts) {
	const name = opts?.name ?? "db";
	return {
		name,
		async check() {
			const start = Date.now();
			try {
				await db.execute(sql`SELECT 1`);
				return {
					name,
					status: "ok",
					latencyMs: Date.now() - start
				};
			} catch {
				return {
					name,
					status: "down",
					detail: "query failed",
					latencyMs: Date.now() - start
				};
			}
		}
	};
}
//#endregion
//#region src/lib/health.ts
async function runHealthChecks(db) {
	return aggregateHealth([healthCheck(db, { name: "db" })]);
}
//#endregion
//#region src/pages/api/admin/health.ts
var health_exports = /* @__PURE__ */ __exportAll({
	GET: () => GET,
	prerender: () => false
});
var GET = async ({ request, cookies }) => {
	try {
		await requireAdmin(request, env, cookies);
		const health = await runHealthChecks(getFullDb(env).db);
		return jsonOk(health);
	} catch (error) {
		return mapContentError(error);
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/admin/health@_@ts
var page = () => health_exports;
//#endregion
export { page };
