"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const zod_1 = require("zod");
const index_core_1 = require("./index-core");
const prototypeHost_1 = require("./routes/prototypeHost");
const BundleStore_1 = require("./services/BundleStore");
const SupabaseAuthService_1 = require("./services/SupabaseAuthService");
const log = (0, index_core_1.createLogger)('PrototypeHostBootstrap');
/**
 * Standalone deployable: the Rivet Prototype Preview Host. Ingests built static
 * bundles (uploaded by the CLI's PrototypeDeployService) and serves each at a
 * public share URL `/p/<deployId>/`. Modeled on `src/hosted-demo.ts`.
 *
 * All hosting credentials live here, server-side. Clients only ever send their
 * Rivet JWT, which we verify against Supabase (same SUPABASE_* env the proxy
 * already holds). The host does NOT call any Render API — it simply runs on
 * Render like the hosted demo does.
 */
const envSchema = zod_1.z.object({
    PORT: zod_1.z.coerce.number().int().positive().default(4100),
    /** Public origin used to build share URLs (e.g. https://prototypes.rivet.design).
     *  Falls back to the onrender.com URL when a custom domain isn't configured. */
    PROTOTYPE_HOST_PUBLIC_URL: zod_1.z
        .string()
        .min(1)
        .default('https://rivet-prototype-host.onrender.com'),
    /** Mount path of the persistent disk that holds extracted bundles. */
    PROTOTYPE_BUNDLE_ROOT: zod_1.z.string().min(1).default('/var/rivet-prototype-bundles'),
    PROTOTYPE_MAX_BUNDLE_MB: zod_1.z.coerce.number().int().positive().default(25),
    SUPABASE_URL: zod_1.z.string().min(1),
    SUPABASE_ANON_KEY: zod_1.z.string().min(1),
});
const main = async () => {
    const env = envSchema.parse(process.env);
    await (0, BundleStore_1.ensureBundleRoot)(env.PROTOTYPE_BUNDLE_ROOT);
    const app = (0, express_1.default)();
    // body-parser auto-inflates gzip/deflate request bodies, so the CLI can POST
    // a gzipped JSON file-list and we parse it transparently here.
    app.use(express_1.default.json({ limit: `${env.PROTOTYPE_MAX_BUNDLE_MB + 5}mb` }));
    app.use((0, prototypeHost_1.createPrototypeHostRouter)({
        store: new BundleStore_1.DiskBundleStore(env.PROTOTYPE_BUNDLE_ROOT),
        auth: new SupabaseAuthService_1.SupabaseAuthService(),
        publicBaseUrl: env.PROTOTYPE_HOST_PUBLIC_URL,
        maxBundleBytes: env.PROTOTYPE_MAX_BUNDLE_MB * 1024 * 1024,
    }));
    app.listen(env.PORT, () => {
        log.info(`Prototype Preview Host listening on ${env.PORT} (bundles: ${env.PROTOTYPE_BUNDLE_ROOT})`);
    });
    await new Promise(() => { });
};
void main();
//# sourceMappingURL=prototype-host.js.map