← Files RivetARCHIVED FILE

node_modules/rivet-design/dist/services/ProjectRuntimeService.js

6.15 KB · Nov 7, 2026 · 13:16 UTC

↓ Download file

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectRuntimeService = void 0;
const fs_1 = __importDefault(require("fs"));
const zod_1 = require("zod");
const installationChannel_1 = require("../install/installationChannel");
const libraryErrors_1 = require("./libraryErrors");
const serverHandshake_1 = require("../cli/serverHandshake");
const shared_1 = require("../cli/commands/shared");
const HEALTH_TIMEOUT_MS = 2_000;
class ProjectRuntimeService {
    registry;
    activations = new Map();
    constructor(registry) {
        this.registry = registry;
    }
    async originalPreviewUrl(projectId) {
        const project = this.registry.get(projectId);
        if (!project.isAvailable)
            return null;
        const handshake = (0, serverHandshake_1.readHandshake)(project.projectPath);
        if (!handshake || !(0, serverHandshake_1.isPidAlive)(handshake.pid))
            return null;
        const origin = `http://127.0.0.1:${handshake.rivetPort}`;
        try {
            const response = await fetch(`${origin}/api/health`, {
                signal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
            });
            if (!response.ok)
                return null;
            const health = zod_1.z
                .object({
                projectPath: zod_1.z.string(),
                pid: zod_1.z.number().int(),
                channel: zod_1.z.string(),
                originalPreviewPath: zod_1.z
                    .enum(['/static/', '/__rivet/preview/base/'])
                    .nullish(),
            })
                .parse(await response.json());
            if (health.pid !== handshake.pid ||
                health.channel !== (0, installationChannel_1.installationChannel)() ||
                fs_1.default.realpathSync(health.projectPath) !== project.projectPath ||
                !health.originalPreviewPath)
                return null;
            return new URL(health.originalPreviewPath, origin).toString();
        }
        catch {
            return null;
        }
    }
    activate(projectId) {
        const project = this.registry.get(projectId);
        if (!project.isAvailable)
            throw new libraryErrors_1.LibraryError('workspace_unavailable', 'Relink this project to its working folder before editing.');
        const pending = this.activations.get(project.id);
        if (pending)
            return pending;
        const activation = this.open(project);
        this.activations.set(project.id, activation);
        void activation
            .finally(() => this.activations.delete(project.id))
            .catch(() => { });
        return activation;
    }
    async open(project) {
        const { ensureProjectRuntime } = await Promise.resolve().then(() => __importStar(require('../cli/commands/open')));
        const context = (0, shared_1.parseCommandContext)('open', [
            '--project',
            project.projectPath,
            ...(process.env.RIVET_LIBRARY_TELEMETRY === '0'
                ? ['--no-telemetry']
                : []),
        ]);
        if ((0, shared_1.isCommandResult)(context)) {
            if (!context.envelope.ok)
                throw new Error(context.envelope.error.message);
            throw new Error('Could not resolve project runtime context');
        }
        const result = await ensureProjectRuntime(context, {
            writeProgress: () => { },
            writeEnvelope: () => { },
        });
        if (!result.envelope.ok)
            throw new Error(result.envelope.error.message);
        const data = zod_1.z
            .object({ editorUrl: zod_1.z.string().url() })
            .parse(result.envelope.data);
        const response = await fetch(new URL('/api/health', data.editorUrl), {
            signal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
        });
        const health = zod_1.z
            .object({
            projectPath: zod_1.z.string(),
            pid: zod_1.z.number().int(),
            channel: zod_1.z.string(),
        })
            .parse(await response.json());
        const { readHandshake } = await Promise.resolve().then(() => __importStar(require('../cli/serverHandshake')));
        const handshake = readHandshake(project.projectPath);
        if (!response.ok ||
            !handshake ||
            health.pid !== handshake.pid ||
            fs_1.default.realpathSync(health.projectPath) !== project.projectPath ||
            health.channel !== (0, installationChannel_1.installationChannel)())
            throw new Error('The project runtime could not verify its identity. Keep current work running and reopen after it finishes.');
        return data;
    }
}
exports.ProjectRuntimeService = ProjectRuntimeService;
//# sourceMappingURL=ProjectRuntimeService.js.map

SHA-256: 0f0cc478715d5f82e9cede255532d249024e1604d9a525ee966e525b0908e0cb