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-client/lib/api.ts
Views: 687
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
// Get the sync *channel* for the given SyncTable project query.
101
async synctable_channel(
102
query: { [field: string]: any },
103
options: { [field: string]: any }[],
104
): Promise<Channel> {
105
const channel_name = await this.call(
106
{
107
cmd: "synctable_channel",
108
query,
109
options,
110
},
111
10000,
112
);
113
// console.log("synctable_channel", query, options, channel_name);
114
return this.conn.channel(channel_name);
115
}
116
117
// Command-response API for synctables.
118
// - mesg = {cmd:'close'} -- closes the synctable, even if persistent.
119
async syncdoc_call(
120
path: string,
121
mesg: { [field: string]: any },
122
timeout_ms: number = 30000, // ms timeout for call
123
): Promise<any> {
124
return await this.call({ cmd: "syncdoc_call", path, mesg }, timeout_ms);
125
}
126
127
async symmetric_channel(name: string) {
128
const channel_name = await this.call(
129
{
130
cmd: "symmetric_channel",
131
name,
132
},
133
30000,
134
);
135
return this.conn.channel(channel_name);
136
}
137
138
async query(opts: any): Promise<any> {
139
if (opts.timeout == null) {
140
opts.timeout = 30000;
141
}
142
const timeout_ms = opts.timeout * 1000 + 2000;
143
return await this.call({ cmd: "query", opts }, timeout_ms);
144
}
145
146
async compute_filesystem_cache(opts, timeout_ms = 30000) {
147
return await this.call(
148
{ cmd: "compute_filesystem_cache", opts },
149
timeout_ms,
150
);
151
}
152
153
async syncFS(opts, timeout_ms = 1000 * 15 * 60) {
154
return await this.call({ cmd: "sync_fs", opts }, timeout_ms);
155
}
156
157
async computeServerSyncRegister(compute_server_id) {
158
return await this.call(
159
{ cmd: "compute_server_sync_register", opts: { compute_server_id } },
160
15000,
161
);
162
}
163
async computeServerComputeRegister(compute_server_id) {
164
return await this.call(
165
{ cmd: "compute_server_compute_register", opts: { compute_server_id } },
166
15000,
167
);
168
}
169
}
170
171