← Files RivetARCHIVED FILE
node_modules/rivet-design/dist/services/PrototypeDeployService.js
4.98 KB · Nov 7, 2026 · 13:16 UTC
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrototypeDeployService = exports.normalizePrototypeShareTitle = exports.PROTOTYPE_SHARE_TITLE_MAX_LENGTH = void 0;
const authUpdate_1 = require("./authUpdate");
const zlib_1 = require("zlib");
const zod_1 = require("zod");
const logger_1 = require("../utils/logger");
const ConfigManager_1 = require("./ConfigManager");
const proxy_1 = require("../config/proxy");
const PrototypeBuildService_1 = require("./PrototypeBuildService");
const log = (0, logger_1.createLogger)('PrototypeDeployService');
const HTML_ENTRY_PATH = 'index.html';
exports.PROTOTYPE_SHARE_TITLE_MAX_LENGTH = 200;
const DeployResultSchema = zod_1.z.object({
deployId: zod_1.z.string().min(1),
shareUrl: zod_1.z.string().url(),
});
const normalizePrototypeShareTitle = (title) => {
const trimmedTitle = title?.trim();
if (!trimmedTitle) {
return undefined;
}
return trimmedTitle.slice(0, exports.PROTOTYPE_SHARE_TITLE_MAX_LENGTH);
};
exports.normalizePrototypeShareTitle = normalizePrototypeShareTitle;
class PrototypeDeployService {
buildService;
hostUrl;
proxyUrl;
configManager = (0, ConfigManager_1.getConfigManager)();
constructor(buildService = new PrototypeBuildService_1.PrototypeBuildService(), hostUrl = proxy_1.PROTOTYPE_HOST_URL, proxyUrl = proxy_1.PROXY_URL) {
this.buildService = buildService;
this.hostUrl = hostUrl;
this.proxyUrl = proxyUrl;
}
async deploy(filesDir, options = {}) {
const { files, totalBytes } = await this.buildService.build(filesDir);
return this.publishFiles(files, totalBytes, 'Built', options);
}
async deployStatic(filesDir, options) {
const { files, totalBytes } = await this.buildService.packStatic(filesDir, options.entryPath);
return this.publishFiles(files, totalBytes, 'Packed', options);
}
async deployHtml(html, options = {}) {
const content = Buffer.from(html, 'utf8').toString('base64');
return this.publishFiles([{ path: HTML_ENTRY_PATH, content }], Buffer.byteLength(html, 'utf8'), 'Packed', options);
}
async publishFiles(files, totalBytes, verb, options) {
log.info(`${verb} prototype bundle: ${files.length} files, ${Math.round(totalBytes / 1024)}KB`);
const title = (0, exports.normalizePrototypeShareTitle)(options.title);
const payload = (0, zlib_1.gzipSync)(Buffer.from(JSON.stringify({ files, ...(title ? { title } : {}) })));
return this.publish(payload, 0);
}
async publish(payload, retryCount) {
const authToken = this.configManager.getAuthToken();
if (!authToken) {
throw new Error('Not authenticated. Please run `rivet login` to deploy a prototype.');
}
const response = await fetch(`${this.hostUrl}/publish`, {
method: 'POST',
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
},
body: new Uint8Array(payload),
});
if (response.status === 401 && retryCount === 0) {
log.warn('Token expired, attempting to refresh...');
const refreshed = await this.refreshAccessToken();
if (refreshed) {
return this.publish(payload, retryCount + 1);
}
throw new Error('Authentication expired. Please run `rivet logout` and `rivet login` to re-authenticate.');
}
if (!response.ok) {
const body = await response.json().catch(() => ({}));
const message = body.message ??
`Deploy failed (${response.status}).`;
throw new Error(message);
}
const result = DeployResultSchema.safeParse(await response.json());
if (!result.success) {
throw new Error('Deploy host returned an unexpected response.');
}
return result.data;
}
async refreshAccessToken() {
const updateAuth = (0, authUpdate_1.captureAuthUpdate)(this.configManager);
const refreshToken = this.configManager.getRefreshToken();
if (!refreshToken) {
log.warn('No refresh token available');
return false;
}
try {
const response = await fetch(`${this.proxyUrl}/api/auth/refresh`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refreshToken }),
});
if (!response.ok)
return false;
const result = await response.json();
if (result.success && result.token) {
updateAuth(result);
return true;
}
return false;
}
catch (err) {
log.warn('Error refreshing token:', err);
return false;
}
}
}
exports.PrototypeDeployService = PrototypeDeployService;
//# sourceMappingURL=PrototypeDeployService.js.mapSHA-256: 948da22d168e802013bc8395c5aa0b2d7f362f11408e99f1f83692edbee84aca