import generateVouchers from "@cocalc/util/vouchers";
import type { Location } from "./types";
import { encodeBase64 } from "@cocalc/conat/util";
export function randomId() {
return generateVouchers({ count: 1, length: 10 })[0];
}
export function jsName({
project_id,
account_id,
hub_id,
}: {
project_id?: string;
account_id?: string;
hub_id?: string;
}) {
if (project_id) {
return `project-${project_id}`;
}
if (account_id) {
return `account-${account_id}`;
}
if (hub_id) {
return `hub-${hub_id}`;
}
if (process.env.COCALC_TEST_MODE) {
return "test";
} else {
return "public";
}
}
export function localLocationName({
compute_server_id,
browser_id,
path,
}: Location): string {
const v: string[] = [];
if (compute_server_id) {
v.push(`id=${compute_server_id}`);
} else if (browser_id) {
v.push(`id=${browser_id}`);
}
if (path) {
v.push(`path=${path}`);
}
return v.join(",");
}
export function inboxPrefix({
account_id,
project_id,
hub_id,
}: {
account_id?: string;
project_id?: string;
hub_id?: string;
}) {
return `_INBOX.${jsName({ account_id, project_id, hub_id })}`;
}
export function streamSubject({
project_id,
account_id,
ephemeral,
}: {
project_id?: string;
account_id?: string;
ephemeral?: boolean;
}) {
const e = ephemeral ? "e" : "";
if (project_id) {
if (account_id) {
throw Error("both account_id and project_id can't be set");
}
return `project.${project_id}.${e}stream.>`;
}
if (!account_id) {
if (process.env.COCALC_TEST_MODE) {
return `test.${e}stream.>`;
}
return `public.${e}stream.>`;
}
return `account.${account_id}.${e}stream.>`;
}
export function projectSubject({
service,
project_id,
compute_server_id,
path,
}: {
project_id: string;
service: string;
compute_server_id?: number;
path?: string;
}): string {
if (!project_id) {
throw Error("project_id must be set");
}
const segments = [
"project",
project_id,
compute_server_id ?? "-",
service ?? "-",
path ? encodeBase64(path) : "-",
];
return segments.join(".");
}
export function projectStreamName({
project_id,
service,
path,
}: {
project_id: string;
service?: string;
path?: string;
}): string {
if (!project_id) {
throw Error("project_id must be set");
}
let streamName = `project-${project_id}`;
if (service) {
streamName += "-" + service;
if (path) {
streamName += "-" + encodeBase64(path);
}
}
return streamName;
}
export function browserSubject({ account_id, sessionId, service }) {
if (!sessionId) {
throw Error("sessionId must be set");
}
if (!account_id) {
throw Error("account_id must be set");
}
if (!service) {
throw Error("service must be set");
}
return `${sessionId}.account-${account_id}.${service}`;
}