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/common.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
import { to_json, trunc } from "@cocalc/util/misc";
7
8
import { getLogger } from "./logger";
9
const winston = getLogger("common");
10
11
function envVarToInt(name: string, fallback: number): number {
12
const x = process.env[name];
13
if (x == null) {
14
return fallback;
15
}
16
const y = parseInt(x);
17
if (isNaN(y)) {
18
return fallback;
19
}
20
return y;
21
}
22
23
// We make it an error for a client to try to edit a file larger than MAX_FILE_SIZE.
24
// I decided on this, because attempts to open a much larger file leads
25
// to disaster. Opening a 10MB file works but is a just a little slow.
26
const MAX_FILE_SIZE =
27
1000 * 1000 * envVarToInt("COCALC_PROJECT_MAX_FILE_SIZE_MB", 10); // in bytes
28
winston.debug(`MAX_FILE_SIZE = ${MAX_FILE_SIZE} bytes`);
29
30
export function check_file_size(size): string | undefined {
31
if (size != null && size > MAX_FILE_SIZE) {
32
const e = `Attempt to open large file of size ${Math.round(
33
size / 1000000
34
)}MB; the maximum allowed size is ${Math.round(
35
MAX_FILE_SIZE / 1000000
36
)}MB. Use vim, emacs, or pico from a terminal instead.`;
37
winston.debug(e);
38
return e;
39
}
40
}
41
42
export function json(out): string {
43
return trunc(to_json(out), 500);
44
}
45
46