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/project-info/project-info.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
/*
7
Project information
8
*/
9
10
import { ProjectInfoCmds } from "@cocalc/util/types/project-info/types";
11
import { ProjectInfoServer } from "./server";
12
import { exec } from "./utils";
13
14
// singleton, we instantiate it when we need it
15
let _info: ProjectInfoServer | undefined = undefined;
16
17
export function get_ProjectInfoServer(): ProjectInfoServer {
18
if (_info != null) return _info;
19
_info = new ProjectInfoServer();
20
return _info;
21
}
22
23
export async function project_info_ws(
24
primus: any,
25
logger: { debug: Function }
26
): Promise<string> {
27
const L = (...msg) => logger.debug("project_info:", ...msg);
28
const name = `project_info`;
29
const channel = primus.channel(name);
30
31
function deregister(spark) {
32
L(`deregistering ${spark.id}`);
33
}
34
35
channel.on("connection", function (spark: any): void {
36
// Now handle the connection
37
L(`channel: new connection from ${spark.address.ip} -- ${spark.id}`);
38
39
function close(type) {
40
L(`event ${type}: deregistering`);
41
deregister(spark);
42
}
43
44
spark.on("close", () => close("close"));
45
spark.on("end", () => close("end"));
46
spark.on("data", function (data: ProjectInfoCmds) {
47
// we assume only ProjectInfoCmds should come in, but better check what this is
48
if (typeof data === "object") {
49
switch (data.cmd) {
50
case "signal":
51
L(`Signal ${data.signal} from ${spark.id} for pids: ${data.pids}`);
52
exec(`kill -s ${data.signal ?? 15} ${data.pids.join(" ")}`);
53
break;
54
default:
55
throw Error("WARNING: unknown command -- " + data.cmd);
56
}
57
}
58
});
59
});
60
61
channel.on("disconnection", function (spark: any): void {
62
L(`channel: disconnection from ${spark.address.ip} -- ${spark.id}`);
63
deregister(spark);
64
});
65
66
return name;
67
}
68
69