CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/data.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Paths to temporary files used by the project.
8
*/
9
10
import { join } from "path";
11
import { data } from "@cocalc/backend/data";
12
import { is_valid_uuid_string } from "@cocalc/util/misc";
13
import { pidFilename } from "@cocalc/util/project-info";
14
15
export const infoJson = join(data, "info.json");
16
export const hubPortFile = join(data, "hub-server.port");
17
export const apiServerPortFile = join(data, "api-server.port");
18
export const browserPortFile = join(data, "browser-server.port");
19
export const projectPidFile = join(data, pidFilename);
20
export const startTimestampFile = join(data, "start-timestamp.txt");
21
export const sessionIDFile = join(data, "session-id.txt");
22
export const rootSymlink = join(data, "root");
23
export const SSH_LOG = join(data, "sshd.log");
24
export const SSH_ERR = join(data, "sshd.err");
25
export const secretToken =
26
process.env.COCALC_SECRET_TOKEN ?? join(data, "secret_token");
27
28
// note that the "username" need not be the output of `whoami`, e.g.,
29
// when using a cc-in-cc dev project where users are "virtual".
30
function getIDs() {
31
let project_id, username;
32
if (process.env.COCALC_PROJECT_ID && process.env.COCALC_USERNAME) {
33
project_id = process.env.COCALC_PROJECT_ID;
34
username = process.env.COCALC_USERNAME;
35
} else {
36
if (!process.env.HOME) {
37
throw Error("HOME not defined, so no way to determine project_id");
38
}
39
const v = process.env.HOME.split("/");
40
project_id = v[v.length - 1];
41
if (!is_valid_uuid_string(project_id)) {
42
throw Error("unable to determine project_id from HOME directory path");
43
}
44
username = project_id.replace(/-/g, "");
45
}
46
// Throw in some consistency checks:
47
if (!is_valid_uuid_string(project_id)) {
48
throw Error(`project_id=${project_id} is not a valid UUID`);
49
}
50
if (!username) {
51
throw Error("unable to determine username");
52
}
53
return { project_id, username };
54
}
55
56
export const { project_id, username } = getIDs();
57
58