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/frontend/client/console.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
/*
7
Some extra functionality that is made available in the command line console
8
for debugging and generally working with CoCalc.
9
10
For security reasons, only available in DEBUG mode (e.g,. when doing cc-in-cc dev),
11
not in production.
12
13
Security note: not easily exposing this to the global scope would make it harder
14
for an attacker who is eval'ing dangerous code in a Sage worksheet (say).
15
However, even if it were not exposed, the attacker could just do
16
"conn = new Primus(url, opts)"
17
and make a Primus connection, and start sending/receiving messages. This would work,
18
because the primus connection authenticates based on secure https cookies,
19
which are there. So we could make everything painful and hard to program and
20
actually get zero security gain.
21
22
**CRITICAL:** If the smc object isn't defined in your Google Chrome console session,
23
you have to change the context to *top*! See
24
http://stackoverflow.com/questions/3275816/debugging-iframes-with-chrome-developer-tools/8581276#8581276
25
*/
26
27
declare const DEBUG; // this comes from rspack.
28
console.log("DEBUG = ", DEBUG);
29
30
import { IS_TOUCH } from "../feature";
31
import { redux } from "../app-framework";
32
33
declare global {
34
interface Window {
35
cocalc: any; // special support for debugging
36
cc: any; // alias for "cocalc"
37
eruda: any; // provides a debugger for mobile devices (iOS).
38
}
39
}
40
41
export function setup_global_cocalc(client): void {
42
if (!DEBUG) {
43
return;
44
}
45
46
const cocalc: any = window.cc ?? {};
47
cocalc.client = client;
48
cocalc.misc = require("@cocalc/util/misc");
49
cocalc.immutable = require("immutable");
50
cocalc.done = cocalc.misc.done;
51
cocalc.sha1 = require("sha1");
52
cocalc.prom_client = require("../prom-client");
53
cocalc.schema = require("@cocalc/util/schema");
54
cocalc.redux = redux;
55
cocalc.load_eruda = load_eruda;
56
cocalc.compute = require("@cocalc/frontend/compute/api");
57
console.log(
58
"DEBUG: Enabling extra CoCalc library functionality. Type cocalc or cc.[tab].",
59
);
60
window.cocalc = window.cc = cocalc;
61
62
if (IS_TOUCH) {
63
// Debug mode and on a touch device: always load eruda so we
64
// get a nice dev console! This is very handy for iPad development.
65
load_eruda();
66
}
67
}
68
69
function load_eruda(): void {
70
// -- e.g., iPad -- so make it possible to get a
71
// devel console via https://github.com/liriliri/eruda
72
// This pulls eruda from a CDN.
73
const script = document.createElement("script");
74
script.src = "//cdn.jsdelivr.net/npm/eruda";
75
document.body.appendChild(script);
76
script.onload = function () {
77
window.eruda?.init();
78
};
79
}
80
81