Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/port_manager.ts
1447 views
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { readFile } from "node:fs/promises";
7
import { sageServerPaths } from "@cocalc/project/data";
8
9
type Type = "sage";
10
11
/*
12
The port_manager manages the ports for the sage worksheet server.
13
*/
14
15
// a local cache
16
const ports: { [type in Type]?: number } = {};
17
18
export async function get_port(type: Type = "sage"): Promise<number> {
19
const val = ports[type];
20
if (val != null) {
21
return val;
22
} else {
23
const content = await readFile(sageServerPaths.port);
24
try {
25
const val = parseInt(content.toString());
26
ports[type] = val;
27
return val;
28
} catch (err) {
29
throw new Error(`${type}_server port file corrupted -- ${err}`);
30
}
31
}
32
}
33
34
export function forget_port(type: Type = "sage") {
35
if (ports[type] != null) {
36
delete ports[type];
37
}
38
}
39
40