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/recv-files.ts
Views: 687
1
/*
2
Receive files over a websocket.
3
*/
4
5
import getLogger from "@cocalc/backend/logger";
6
const logger = getLogger("sync-fs:recv-files");
7
8
import { spawn } from "child_process";
9
10
export default function recvFiles({
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
const tar = spawn("tar", args, {
25
cwd: HOME,
26
});
27
let start = Date.now();
28
logger.debug("got connection", start);
29
let fileSize = 0;
30
ws.on("message", (data) => {
31
if (data instanceof Buffer) {
32
// logger.debug("received ", data.length, "bytes");
33
fileSize += data.length;
34
tar.stdin.write(data);
35
}
36
});
37
ws.on("close", () => {
38
tar.stdin.end();
39
let end = Date.now();
40
let timeTaken = (end - start) / 1000; // convert ms to s
41
let speed = fileSize / timeTaken / 1000000;
42
logger.debug(
43
`recvFiles: ${fileSize / 1000000}MB received in ${
44
end - start
45
} ms, speed: ${speed} MB/s`,
46
);
47
});
48
}
49
50