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/servers/websocketfs.ts
Views: 687
1
/*
2
Start a websocketfs server.
3
*/
4
5
import { join } from "node:path";
6
import type { Server } from "http";
7
import { Server as SftpServer } from "websocket-sftp";
8
import { getLogger } from "@cocalc/project/logger";
9
import { WebSocketServer } from "ws";
10
import { parse } from "url";
11
12
const log = getLogger("cocalc:websocketfs");
13
14
export default function initWebsocketFs(
15
server: Server,
16
basePath: string,
17
): void {
18
const path = join(basePath, ".smc", "websocketfs");
19
log.info(`Initializing websocketfs filesystem server`);
20
21
const wss = new WebSocketServer({ noServer: true });
22
23
const sftpServer = new SftpServer({
24
virtualRoot: process.env.HOME,
25
wss,
26
});
27
28
process.on("exit", () => {
29
sftpServer.end();
30
});
31
32
server.on("upgrade", (request, socket, head) => {
33
const { pathname } = parse(request.url ?? "");
34
// log.info("Got upgrade request for ", pathname);
35
if (pathname === path) {
36
log.info("creating new websocketfs handler");
37
wss.handleUpgrade(request, socket, head, (ws) => {
38
wss.emit("connection", ws, request);
39
});
40
}
41
});
42
}
43
44