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/backend/base-path.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/* Determine the base path for this CoCalc service.
7
8
The default export of this module is base path, which should
9
be used by the CoCalc backend hub server.
10
*/
11
12
const DEFN: string = `
13
The basePath is the URL prefix for all paths, relative to the
14
host root. It must start with a leading slash /, but does not
15
end with one unless it is '/'. It also should not include ://.
16
Some examples of valid basePaths:
17
18
- /
19
- /10f0e544-313c-4efe-8718-1111ac97ad11/port/5000
20
21
These are not valid:
22
23
- //
24
- "" (empty string)
25
- /foo/
26
- https://cocalc.com/
27
28
If the environment variable BASE_PATH is set then use that (e.g., used
29
when running a project), or throw an error if our assumptions are not satisfied.
30
31
Otherwise, if this code is running in a CoCalc project (i.e., if the env variable
32
COCALC_PROJECT_ID is set), then the base path is a combination of
33
COCALC_PROJECT_ID and the port that the hub will serve on.
34
35
If neither of the above conditions are met, then the base path is /.
36
37
NOTES:
38
39
- We use this code in a project started by the hub to determine the base path;
40
in that case, the env variable BASE_PATH is set, since otherwise the project
41
itself would view the base path as being relative to its own id.
42
43
`;
44
45
import PORT from "./port";
46
47
function isValidBasePath(s: string): boolean {
48
if (s[0] != "/") return false;
49
if (s.length == 1) return true;
50
if (s[s.length - 1] == "/") return false;
51
if (s.includes("://")) return false;
52
return true;
53
}
54
55
function basePath(): string {
56
if (process.env.BASE_PATH) {
57
if (!isValidBasePath(process.env.BASE_PATH)) {
58
throw Error(
59
`BASE_PATH (="${process.env.BASE_PATH}") is invalid - ${DEFN}.`,
60
);
61
}
62
return process.env.BASE_PATH;
63
}
64
if (process.env.API_SERVER) {
65
// if an api server is set but BASE_PATH isn't (e.g., we're using a remote api server), then we
66
// deduce the base path from that URL. Examples:
67
// API_SERVER=http://localhost:5000/6659c2e3-ff5e-4bb4-9a43-8830aa951282/port/5000 --> "/6659c2e3-ff5e-4bb4-9a43-8830aa951282/port/5000"
68
// API_SERVER=https://compute.cocalc.io --> "/"
69
// API_SERVER=https://cocalc.com --> "/"
70
const url = process.env.API_SERVER;
71
const i = url.indexOf("://");
72
if (i == -1) {
73
throw Error(`invalid API_SERVER url ${url}`);
74
}
75
const j = url.indexOf("/", i + 3);
76
if (j == -1) {
77
return "/";
78
}
79
return url.slice(j) ? url.slice(j) : "/";
80
}
81
if (!process.env.COCALC_PROJECT_ID) {
82
return "/";
83
}
84
// This is used by the project server, i.e., inside the project.
85
return `/${process.env.COCALC_PROJECT_ID}/port/${PORT}`;
86
}
87
88
export default basePath();
89
90