Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/sync-client/lib/api.ts
Views: 687
/*1This is the small lightweight subset of the project's websocket api that we2need for this compute package. It's a subset of34packages/frontend/project/websocket/api.ts5*/67import type {8API as API_Interface,9Channel,10ProjectWebsocket,11} from "@cocalc/sync/client/types";12import call from "@cocalc/sync/client/call";1314export default class API implements API_Interface {15private conn: ProjectWebsocket;16private cachedVersion?: number;1718constructor(conn) {19this.conn = conn;20}2122async call(mesg: object, timeout_ms: number): Promise<any> {23return await call(this.conn, mesg, timeout_ms);24}2526async version(): Promise<number> {27// version can never change, so its safe to cache28if (this.cachedVersion != null) {29return this.cachedVersion;30}31try {32this.cachedVersion = await this.call({ cmd: "version" }, 15000);33} catch (err) {34if (err.message.includes('command "version" not implemented')) {35this.cachedVersion = 0;36} else {37throw err;38}39}40if (this.cachedVersion == null) {41this.cachedVersion = 0;42}43return this.cachedVersion;44}4546async listing(47path: string,48hidden: boolean = false,49timeout: number = 15000,50) {51return await this.call({ cmd: "listing", path, hidden }, timeout);52}5354async configuration(aspect, no_cache = false) {55return await this.call({ cmd: "configuration", aspect, no_cache }, 15000);56}5758async jupyter(59path: string,60endpoint: string,61query: any = undefined,62timeout_ms: number = 20000,63) {64return await this.call(65{ cmd: "jupyter", path, endpoint, query },66timeout_ms,67);68}6970async exec(opts: any): Promise<any> {71let timeout_ms = 10000;72if (opts.timeout) {73timeout_ms = opts.timeout * 1000 + 2000;74}75return await this.call({ cmd: "exec", opts }, timeout_ms);76}7778async eval_code(code: string, timeout_ms: number = 20000): Promise<any> {79return await this.call({ cmd: "eval_code", code }, timeout_ms);80}8182async terminal(path: string, options: object = {}): Promise<Channel> {83const channel_name = await this.call(84{85cmd: "terminal",86path: path,87options,88},8960000,90);91return this.conn.channel(channel_name);92}9394async project_info(): Promise<Channel> {95const channel_name = await this.call({ cmd: "project_info" }, 60000);96return this.conn.channel(channel_name);97}9899// Get the sync *channel* for the given SyncTable project query.100async synctable_channel(101query: { [field: string]: any },102options: { [field: string]: any }[],103): Promise<Channel> {104const channel_name = await this.call(105{106cmd: "synctable_channel",107query,108options,109},11010000,111);112// console.log("synctable_channel", query, options, channel_name);113return this.conn.channel(channel_name);114}115116// Command-response API for synctables.117// - mesg = {cmd:'close'} -- closes the synctable, even if persistent.118async syncdoc_call(119path: string,120mesg: { [field: string]: any },121timeout_ms: number = 30000, // ms timeout for call122): Promise<any> {123return await this.call({ cmd: "syncdoc_call", path, mesg }, timeout_ms);124}125126async symmetric_channel(name: string) {127const channel_name = await this.call(128{129cmd: "symmetric_channel",130name,131},13230000,133);134return this.conn.channel(channel_name);135}136137async query(opts: any): Promise<any> {138if (opts.timeout == null) {139opts.timeout = 30000;140}141const timeout_ms = opts.timeout * 1000 + 2000;142return await this.call({ cmd: "query", opts }, timeout_ms);143}144145async compute_filesystem_cache(opts, timeout_ms = 30000) {146return await this.call(147{ cmd: "compute_filesystem_cache", opts },148timeout_ms,149);150}151152async syncFS(opts, timeout_ms = 1000 * 15 * 60) {153return await this.call({ cmd: "sync_fs", opts }, timeout_ms);154}155156async computeServerSyncRegister(compute_server_id) {157return await this.call(158{ cmd: "compute_server_sync_register", opts: { compute_server_id } },15915000,160);161}162async computeServerComputeRegister(compute_server_id) {163return await this.call(164{ cmd: "compute_server_compute_register", opts: { compute_server_id } },16515000,166);167}168}169170171