← Files RivetARCHIVED FILE

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

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

↓ Download file

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.callerSessionOf = exports.editorUrlFor = exports.requestServer = exports.attachToServer = exports.inspectServer = exports.invalidateAttachCache = exports.enableAttachCache = exports.isServerHealthy = void 0;
const telemetryPolicy_1 = require("../config/telemetryPolicy");
const editorSession_1 = require("./editorSession");
const editorAccess_1 = require("../utils/editorAccess");
const telemetry_1 = require("../config/telemetry");
const serverHandshake_1 = require("./serverHandshake");
const queueAccess_1 = require("../utils/queueAccess");
const zod_1 = require("zod");
const pluginContext_1 = require("../hosts/pluginContext");
const HEALTH_PING_TIMEOUT_MS = 2_000;
const REQUEST_TIMEOUT_MS = 30_000;
const isServerHealthy = async (rivetPort, env = process.env) => {
    try {
        const response = await fetch(`http://127.0.0.1:${rivetPort}/api/health`, {
            signal: AbortSignal.timeout(HEALTH_PING_TIMEOUT_MS),
        });
        if (!response.ok)
            return false;
        const health = zod_1.z
            .object({
            pluginName: zod_1.z.string().optional(),
            telemetryMode: zod_1.z.unknown().optional(),
        })
            .safeParse(await response.json().catch(() => null));
        if (health.success) {
            (0, pluginContext_1.assertPluginContext)(health.data.pluginName, env);
            (0, telemetryPolicy_1.assertTelemetryMode)(health.data.telemetryMode, env);
        }
        else if ((0, pluginContext_1.getPluginName)(env)) {
            return false;
        }
        return true;
    }
    catch (error) {
        if (error instanceof pluginContext_1.PluginContextMismatchError ||
            error instanceof telemetryPolicy_1.TelemetryPolicyMismatchError)
            throw error;
        return false;
    }
};
exports.isServerHealthy = isServerHealthy;
let attachCache = null;
const enableAttachCache = () => {
    attachCache ??= new Map();
};
exports.enableAttachCache = enableAttachCache;
const invalidateAttachCache = () => {
    attachCache?.clear();
};
exports.invalidateAttachCache = invalidateAttachCache;
const inspectServer = async (projectPath, env = process.env) => {
    const cached = attachCache?.get(projectPath);
    const handshakeResult = (0, serverHandshake_1.readHandshakeResult)(projectPath);
    if (handshakeResult.status !== 'found') {
        return {
            status: 'unavailable',
            reason: handshakeResult.status === 'missing'
                ? 'handshake_missing'
                : 'handshake_invalid',
        };
    }
    const { handshake } = handshakeResult;
    if (!(0, serverHandshake_1.isPidAlive)(handshake.pid)) {
        (0, serverHandshake_1.removeHandshake)(projectPath, handshake.pid);
        return { status: 'unavailable', reason: 'process_not_running' };
    }
    (0, pluginContext_1.assertPluginContext)(handshake.pluginName, env);
    (0, telemetryPolicy_1.assertTelemetryMode)(handshake.telemetryMode, env);
    if (cached &&
        cached.handshake.pid === handshake.pid &&
        cached.handshake.startedAt === handshake.startedAt &&
        cached.handshake.rivetPort === handshake.rivetPort &&
        cached.handshake.rivetVersion === handshake.rivetVersion &&
        cached.handshake.pluginName === handshake.pluginName &&
        cached.handshake.telemetryMode === handshake.telemetryMode &&
        cached.handshake.queueAccessToken === handshake.queueAccessToken) {
        return { status: 'attached', server: cached };
    }
    attachCache?.delete(projectPath);
    if (!(await (0, exports.isServerHealthy)(handshake.rivetPort, env))) {
        return { status: 'unavailable', reason: 'health_check_failed' };
    }
    const attached = {
        baseUrl: `http://127.0.0.1:${handshake.rivetPort}`,
        handshake,
    };
    attachCache?.set(projectPath, attached);
    return { status: 'attached', server: attached };
};
exports.inspectServer = inspectServer;
const attachToServer = async (projectPath, env = process.env) => {
    const result = await (0, exports.inspectServer)(projectPath, env);
    return result.status === 'attached' ? result.server : null;
};
exports.attachToServer = attachToServer;
const requestServer = async (attached, path, options = {}) => {
    const headers = {
        'Content-Type': 'application/json',
        'X-Rivet-Client-Version': telemetry_1.RIVET_VERSION,
    };
    if (attached.handshake.queueAccessToken) {
        headers[queueAccess_1.QUEUE_ACCESS_TOKEN_HEADER] = attached.handshake.queueAccessToken;
    }
    if (options.callerSession) {
        headers[queueAccess_1.CALLER_SESSION_HEADER] = options.callerSession;
    }
    if (options.expectedAccountId) {
        headers['X-Rivet-Account-Id'] = options.expectedAccountId;
    }
    if ((0, editorAccess_1.isEditorApiPath)(path, options.method)) {
        const token = (0, editorSession_1.readEditorAccessToken)({
            pid: attached.handshake.pid,
            port: attached.handshake.rivetPort,
            projectPath: attached.handshake.projectPath,
            startedAt: attached.handshake.startedAt,
        });
        if (token)
            headers[editorAccess_1.EDITOR_ACCESS_TOKEN_HEADER] = token;
    }
    const response = await fetch(`${attached.baseUrl}${path}`, {
        redirect: 'error',
        method: options.method ?? 'GET',
        headers,
        ...(options.body === undefined
            ? {}
            : { body: JSON.stringify(options.body) }),
        signal: AbortSignal.timeout(options.timeoutMs ?? REQUEST_TIMEOUT_MS),
    });
    const body = (await response.json().catch(() => ({})));
    return { status: response.status, body };
};
exports.requestServer = requestServer;
const editorUrlFor = (attached) => {
    const token = (0, editorSession_1.readEditorAccessToken)({
        pid: attached.handshake.pid,
        port: attached.handshake.rivetPort,
        projectPath: attached.handshake.projectPath,
        startedAt: attached.handshake.startedAt,
    });
    if (!token)
        throw new Error('Restart this project editor to authorize browser access.');
    return (0, editorAccess_1.createEditorUrl)({ port: attached.handshake.rivetPort, token });
};
exports.editorUrlFor = editorUrlFor;
const callerSessionOf = (handoff) => handoff?.session ?? undefined;
exports.callerSessionOf = callerSessionOf;
//# sourceMappingURL=client.js.map

SHA-256: b1537a3bede711b1a10d0855b36f6a804557ccb089c91e5ca8e001d7b11a457e