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/named-servers/index.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
import { getLogger } from "@cocalc/project/logger";
7
import * as message from "@cocalc/util/message";
8
import { NamedServerName } from "@cocalc/util/types/servers";
9
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
10
import { start } from "./control";
11
12
const winston = getLogger("named-servers");
13
14
async function getPort(name: NamedServerName): Promise<number> {
15
winston.debug(`getPort("${name}")`);
16
return await start(name);
17
}
18
19
async function handleMessage(socket, mesg): Promise<void> {
20
try {
21
mesg.port = await getPort(mesg.name);
22
} catch (err) {
23
socket.write_mesg("json", message.error({ id: mesg.id, error: `${err}` }));
24
return;
25
}
26
socket.write_mesg("json", mesg);
27
}
28
29
const handle = reuseInFlight(handleMessage, {
30
createKey: (args) => `${args[1]?.name}`,
31
});
32
export default handle;
33
34