Path: blob/master/src/packages/sync-client/lib/api.ts
1503 views
/*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}9899async query(opts: any): Promise<any> {100if (opts.timeout == null) {101opts.timeout = 30000;102}103const timeout_ms = opts.timeout * 1000 + 2000;104return await this.call({ cmd: "query", opts }, timeout_ms);105}106107async compute_filesystem_cache(opts, timeout_ms = 30000) {108return await this.call(109{ cmd: "compute_filesystem_cache", opts },110timeout_ms,111);112}113114async syncFS(opts, timeout_ms = 1000 * 15 * 60) {115return await this.call({ cmd: "sync_fs", opts }, timeout_ms);116}117118async computeServerSyncRegister(compute_server_id) {119return await this.call(120{ cmd: "compute_server_sync_register", opts: { compute_server_id } },12115000,122);123}124async computeServerComputeRegister(compute_server_id) {125return await this.call(126{ cmd: "compute_server_compute_register", opts: { compute_server_id } },12715000,128);129}130}131132133