← Files RivetARCHIVED FILE

node_modules/rivet-design/dist/cli/projectRuntimeUpgrade.js

5.56 KB · Nov 5, 2026 · 12:09 UTC

↓ Download file

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareRuntimeUpgrade = void 0;
const zod_1 = require("zod");
const fs_1 = __importDefault(require("fs"));
const client_1 = require("./client");
const serverVersion_1 = require("./serverVersion");
const serverShutdown_1 = require("./serverShutdown");
const stopRuntimeProcess_1 = require("../services/stopRuntimeProcess");
const runtimeActivity_1 = require("../services/runtimeActivity");
const runtimeInterruption_1 = require("./runtimeInterruption");
const errorMessage_1 = require("../utils/errorMessage");
const healthSchema = zod_1.z.object({
    pid: zod_1.z.number().int().positive(),
    rivetVersion: zod_1.z.string().optional(),
    projectPath: zod_1.z.string(),
    framework: zod_1.z.string().optional(),
});
const upgradeSchema = zod_1.z.object({
    stopping: zod_1.z.boolean().optional(),
    reason: zod_1.z.string().optional(),
    error: zod_1.z.string().optional(),
    restartArgs: zod_1.z.array(zod_1.z.string()).optional(),
});
const devServerSchema = zod_1.z.object({
    devServerHealth: zod_1.z
        .object({ ownership: zod_1.z.string(), port: zod_1.z.number().nullable() })
        .optional(),
});
const INTERRUPTIBLE_REASONS = new Set([
    'active_work',
    'unknown_execution_state',
]);
const prepareRuntimeUpgrade = async ({ attached, io, shutdownTimeoutMs, }) => {
    const info = (0, serverVersion_1.serverVersionInfo)(attached.handshake);
    if (!info.staleServer)
        return { outcome: 'attach' };
    const fail = (reason, message) => ({
        outcome: 'failed',
        reason,
        message,
    });
    try {
        const processIdentity = (0, stopRuntimeProcess_1.runtimeProcessIdentity)(attached.handshake.pid);
        const response = await (0, client_1.requestServer)(attached, '/api/health');
        const health = healthSchema.safeParse(response.body);
        if (response.status !== 200 ||
            !health.success ||
            health.data.pid !== attached.handshake.pid ||
            fs_1.default.realpathSync(health.data.projectPath) !==
                fs_1.default.realpathSync(attached.handshake.projectPath) ||
            health.data.rivetVersion !== attached.handshake.rivetVersion)
            return fail('server_identity_mismatch', 'The outdated project server could not verify its identity. It was not stopped.');
        const status = await (0, client_1.requestServer)(attached, '/api/cli/status').catch(() => null);
        if (status?.status === 401 || status?.status === 403)
            return fail('server_access_denied', 'The outdated project server denied access. It was not stopped.');
        const snapshot = status?.status === 200 ? status.body : null;
        let upgrade = await (0, client_1.requestServer)(attached, '/api/cli/upgrade', {
            method: 'POST',
            body: { clientVersion: info.cliVersion },
        });
        let parsed = upgradeSchema.parse(upgrade.body);
        const fallbackArgs = [
            '--project',
            attached.handshake.projectPath,
            '--rivet-port',
            String(attached.handshake.rivetPort),
        ];
        if (health.data.framework)
            fallbackArgs.push('--framework', health.data.framework);
        const devServer = devServerSchema.safeParse(snapshot);
        if (devServer.success &&
            devServer.data.devServerHealth?.ownership === 'external' &&
            devServer.data.devServerHealth.port)
            fallbackArgs.push('--user-port', String(devServer.data.devServerHealth.port));
        if (upgrade.status === 404 ||
            (upgrade.status === 409 && INTERRUPTIBLE_REASONS.has(parsed.reason ?? ''))) {
            upgrade = await (0, client_1.requestServer)(attached, '/api/cli/shutdown', {
                method: 'POST',
                body: {},
            });
            parsed = upgradeSchema.parse(upgrade.body);
        }
        if (upgrade.status !== 200 || parsed.stopping !== true)
            return fail(parsed.reason ?? 'server_upgrade_required', parsed.error ??
                'The outdated project server could not be restarted. Inspect its log and retry `rivet open`.');
        const interruptedWork = (0, runtimeActivity_1.classifyRuntimeActivity)(snapshot) !== 'idle'
            ? (0, runtimeInterruption_1.recordRuntimeInterruption)({ attached, snapshot })
            : undefined;
        if (interruptedWork)
            io.writeProgress(`${interruptedWork.message} Recovery details: ${interruptedWork.receiptPath}`);
        if (!(await (0, serverShutdown_1.waitForServerShutdown)(attached, shutdownTimeoutMs))) {
            await (0, stopRuntimeProcess_1.stopRuntimeProcess)({
                pid: attached.handshake.pid,
                timeoutMs: shutdownTimeoutMs,
                isOwned: async () => {
                    return (processIdentity !== null &&
                        (0, stopRuntimeProcess_1.runtimeProcessIdentity)(attached.handshake.pid) === processIdentity);
                },
            });
        }
        io.writeProgress(`Upgrading project server to rivet v${info.cliVersion}…`);
        return {
            outcome: 'spawn',
            restartArgs: parsed.restartArgs ?? fallbackArgs,
            ...(interruptedWork ? { interruptedWork } : {}),
        };
    }
    catch (error) {
        return fail('server_upgrade_required', (0, errorMessage_1.errorMessage)(error));
    }
};
exports.prepareRuntimeUpgrade = prepareRuntimeUpgrade;
//# sourceMappingURL=projectRuntimeUpgrade.js.map

SHA-256: 4f8a17320024a2b833a78aa2b2a2214b1eff63346100b852df9d1950833d9d5f