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/hub/proxy/parse.ts
Views: 687
1
type ProxyType = "port" | "raw" | "server";
2
3
export function parseReq(
4
url: string, // with base_path removed (url does start with /)
5
remember_me?: string, // only impacts the key that is returned
6
api_key?: string // only impacts key
7
): {
8
key: string; // used for caching
9
type: ProxyType;
10
project_id: string; // the uuid of the target project containing the service being proxied
11
port_desc: string; // description of port; "" for raw, or a number or "jupyter"
12
internal_url: string | undefined; // url at target of thing we are proxying to; this is ONLY set in case type == 'server'.
13
} {
14
if (url[0] != "/") {
15
throw Error(`invalid url -- it should start with / but is "${url}"`);
16
}
17
const v = url.split("/").slice(1);
18
const project_id = v[0];
19
if (v[1] != "port" && v[1] != "raw" && v[1] != "server") {
20
throw Error(
21
`invalid type -- "${v[1]}" must be "port", "raw" or "server" in url="${url}"`
22
);
23
}
24
const type: ProxyType = v[1];
25
let internal_url: string | undefined = undefined;
26
let port_desc: string;
27
if (type == "raw") {
28
port_desc = "";
29
} else if (type === "port") {
30
port_desc = v[2];
31
} else if (type === "server") {
32
port_desc = v[2];
33
internal_url = v.slice(3).join("/");
34
} else {
35
throw Error(`unknown type "${type}"`);
36
}
37
const key = `${remember_me}-${api_key}-${project_id}-${type}-${port_desc}-${internal_url}`;
38
return { key, type, project_id, port_desc, internal_url };
39
}
40
41