Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync-client/lib/api.ts
1503 views
1
/*
2
This is the small lightweight subset of the project's websocket api that we
3
need for this compute package. It's a subset of
4
5
packages/frontend/project/websocket/api.ts
6
*/
7
8
import type {
9
API as API_Interface,
10
Channel,
11
ProjectWebsocket,
12
} from "@cocalc/sync/client/types";
13
import call from "@cocalc/sync/client/call";
14
15
export default class API implements API_Interface {
16
private conn: ProjectWebsocket;
17
private cachedVersion?: number;
18
19
constructor(conn) {
20
this.conn = conn;
21
}
22
23
async call(mesg: object, timeout_ms: number): Promise<any> {
24
return await call(this.conn, mesg, timeout_ms);
25
}
26
27
async version(): Promise<number> {
28
// version can never change, so its safe to cache
29
if (this.cachedVersion != null) {
30
return this.cachedVersion;
31
}
32
try {
33
this.cachedVersion = await this.call({ cmd: "version" }, 15000);
34
} catch (err) {
35
if (err.message.includes('command "version" not implemented')) {
36
this.cachedVersion = 0;
37
} else {
38
throw err;
39
}
40
}
41
if (this.cachedVersion == null) {
42
this.cachedVersion = 0;
43
}
44
return this.cachedVersion;
45
}
46
47
async listing(
48
path: string,
49
hidden: boolean = false,
50
timeout: number = 15000,
51
) {
52
return await this.call({ cmd: "listing", path, hidden }, timeout);
53
}
54
55
async configuration(aspect, no_cache = false) {
56
return await this.call({ cmd: "configuration", aspect, no_cache }, 15000);
57
}
58
59
async jupyter(
60
path: string,
61
endpoint: string,
62
query: any = undefined,
63
timeout_ms: number = 20000,
64
) {
65
return await this.call(
66
{ cmd: "jupyter", path, endpoint, query },
67
timeout_ms,
68
);
69
}
70
71
async exec(opts: any): Promise<any> {
72
let timeout_ms = 10000;
73
if (opts.timeout) {
74
timeout_ms = opts.timeout * 1000 + 2000;
75
}
76
return await this.call({ cmd: "exec", opts }, timeout_ms);
77
}
78
79
async eval_code(code: string, timeout_ms: number = 20000): Promise<any> {
80
return await this.call({ cmd: "eval_code", code }, timeout_ms);
81
}
82
83
async terminal(path: string, options: object = {}): Promise<Channel> {
84
const channel_name = await this.call(
85
{
86
cmd: "terminal",
87
path: path,
88
options,
89
},
90
60000,
91
);
92
return this.conn.channel(channel_name);
93
}
94
95
async project_info(): Promise<Channel> {
96
const channel_name = await this.call({ cmd: "project_info" }, 60000);
97
return this.conn.channel(channel_name);
98
}
99
100
async query(opts: any): Promise<any> {
101
if (opts.timeout == null) {
102
opts.timeout = 30000;
103
}
104
const timeout_ms = opts.timeout * 1000 + 2000;
105
return await this.call({ cmd: "query", opts }, timeout_ms);
106
}
107
108
async compute_filesystem_cache(opts, timeout_ms = 30000) {
109
return await this.call(
110
{ cmd: "compute_filesystem_cache", opts },
111
timeout_ms,
112
);
113
}
114
115
async syncFS(opts, timeout_ms = 1000 * 15 * 60) {
116
return await this.call({ cmd: "sync_fs", opts }, timeout_ms);
117
}
118
119
async computeServerSyncRegister(compute_server_id) {
120
return await this.call(
121
{ cmd: "compute_server_sync_register", opts: { compute_server_id } },
122
15000,
123
);
124
}
125
async computeServerComputeRegister(compute_server_id) {
126
return await this.call(
127
{ cmd: "compute_server_compute_register", opts: { compute_server_id } },
128
15000,
129
);
130
}
131
}
132
133