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/sync-fs/lib/send-files.ts
Views: 687
1
/*
2
Send files over a websocket to a remote server
3
*/
4
5
import getLogger from "@cocalc/backend/logger";
6
const logger = getLogger("sync-fs:send-files");
7
8
import { spawn } from "child_process";
9
10
export default function sendFiles({
11
ws,
12
HOME = process.env.HOME,
13
args,
14
}: {
15
ws;
16
HOME?;
17
args: string[];
18
}) {
19
if (args.length == 0) {
20
ws.emit("error", "no arguments given");
21
ws.close();
22
return;
23
}
24
let start = Date.now();
25
const tar = spawn("tar", args, {
26
cwd: HOME,
27
});
28
logger.debug("got connection", start);
29
let fileSize = 0;
30
31
// TODO: here we would wait for a message listing files we should send
32
// or maybe that is just input to this function
33
logger.debug("Sending files...");
34
35
tar.stdout.on("data", (data) => {
36
fileSize += data.length;
37
// logger.debug("sending ", data.length, "bytes");
38
ws.send(data);
39
});
40
41
tar.stdout.on("end", () => {
42
let end = Date.now();
43
let timeTaken = (end - start) / 1000; // convert ms to s
44
let speed = fileSize / timeTaken / 1000000;
45
logger.debug(
46
`sendFiles: ${fileSize / 1000000}MB sent in ${
47
end - start
48
} ms, speed: ${speed} MB/s`,
49
);
50
ws.close();
51
});
52
}
53
54