Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/servers/sync-fs.ts
Views: 687
/*1Start sync-fs websocket endpoint.23This listens for connections then streams and extracts a tarball.45This is how all the real data gets sent back and forth for6@cocalc/sync-fs.7*/89import { join } from "node:path";10import type { Server } from "http";11import { getLogger } from "@cocalc/project/logger";12import { WebSocketServer } from "ws";13import { parse } from "url";14import recvFiles from "@cocalc/sync-fs/lib/recv-files";15import sendFiles from "@cocalc/sync-fs/lib/send-files";1617const logger = getLogger("cocalc:project:server:sync-fs");1819export default function initSyncFs(server: Server, basePath: string): void {20initReceive(server, basePath);21initSend(server, basePath);22}2324function initReceive(server, basePath) {25const path = join(basePath, ".smc", "sync-fs", "recv");26logger.info(`Initializing syncfs-recv server`, path);2728const wss = new WebSocketServer({ noServer: true });29wss.on("connection", (ws) => {30logger.debug("syncfs-recv server: got new connection");31ws.once("message", (mesg) => {32// logger.debug("syncfs-recv server: received args", mesg);33const args = JSON.parse(mesg.toString());34logger.debug("parse", args);35recvFiles({ ws, args, HOME: process.env.HOME });36});37});3839server.on("upgrade", (request, socket, head) => {40const { pathname } = parse(request.url ?? "");41if (pathname === path) {42logger.info("creating new syncfs-recv handler");43wss.handleUpgrade(request, socket, head, (ws) => {44wss.emit("connection", ws, request);45});46}47});48}4950function initSend(server, basePath) {51const path = join(basePath, ".smc", "sync-fs", "send");52logger.info(`Initializing syncfs-send server`, path);53const wss = new WebSocketServer({ noServer: true });5455wss.on("connection", (ws) => {56logger.debug("syncfs-send server: got new connection");57ws.once("message", (mesg) => {58// logger.debug("syncfs-send server: received", mesg);59const args = JSON.parse(mesg.toString());60logger.debug("parse", args);61sendFiles({ ws, args, HOME: process.env.HOME });62});63});6465server.on("upgrade", (request, socket, head) => {66const { pathname } = parse(request.url ?? "");67if (pathname === path) {68logger.info("creating new syncfs-recv handler");69wss.handleUpgrade(request, socket, head, (ws) => {70wss.emit("connection", ws, request);71});72}73});74}757677