UNPKG

@actually-colab/editor-client

Version:

API Client for Actually Colab's editor

2 lines 7.82 kB
!function(e,t){for(var o in t)e[o]=t[o];t.__esModule&&Object.defineProperty(e,"__esModule",{value:!0})}(exports,(()=>{"use strict";var e={455:function(e,t,o){var s=this&&this.__awaiter||function(e,t,o,s){return new(o||(o=Promise))((function(i,n){function r(e){try{d(s.next(e))}catch(e){n(e)}}function a(e){try{d(s.throw(e))}catch(e){n(e)}}function d(e){var t;e.done?i(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(r,a)}d((s=s.apply(e,t||[])).next())}))},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.ActuallyColabRESTClient=void 0;const n=i(o(376)),r=i(o(236));t.ActuallyColabRESTClient=class{constructor(e){this.setSessionToken=e=>{this.axiosInstance.defaults.headers.Authorization=`Bearer ${e}`},this.login=e=>s(this,void 0,void 0,(function*(){var t,o;const s=null===(o=null===(t=yield this.axiosInstance.post("/login",e))||void 0===t?void 0:t.data)||void 0===o?void 0:o.data;if(null==(null==s?void 0:s.sessionToken))throw new Error("Login failed");return this.setSessionToken(s.sessionToken),s})),this.devLogin=(e,t)=>s(this,void 0,void 0,(function*(){return this.login({tokenType:"dev",email:e,name:t})})),this.loginWithGoogleIdToken=e=>s(this,void 0,void 0,(function*(){return this.login({tokenType:"google",idToken:e})})),this.refreshSessionToken=e=>s(this,void 0,void 0,(function*(){return this.login({tokenType:"session",sessionToken:e})})),this.getNotebooksForUser=()=>s(this,void 0,void 0,(function*(){return(yield this.axiosInstance.get("/notebooks")).data.data})),this.getNotebookContents=e=>s(this,void 0,void 0,(function*(){return(yield this.axiosInstance.get(`/notebook/${e}`)).data.data})),this.createNotebook=(e,t="python",o)=>s(this,void 0,void 0,(function*(){return(yield this.axiosInstance.post("/notebook",{name:e,language:t,cells:o})).data.data})),this.shareNotebook=(e,t,o)=>s(this,void 0,void 0,(function*(){return(yield this.axiosInstance.post(`/notebook/${t}/share`,{email:e,access_level:o})).data.data})),this.createWorkshop=(e,t,o)=>s(this,void 0,void 0,(function*(){return(yield this.axiosInstance.post("/workshop",{name:e,description:t,cells:o})).data.data})),this.getWorkshopsForUser=()=>s(this,void 0,void 0,(function*(){return(yield this.axiosInstance.get("/workshops")).data.data})),this.axiosInstance=n.default.create({baseURL:e,timeout:1e4,headers:{}}),r.default(this.axiosInstance,{retries:3,shouldResetTimeout:!0})}}},607:function(e,t,o){var s=this&&this.__createBinding||(Object.create?function(e,t,o,s){void 0===s&&(s=o),Object.defineProperty(e,s,{enumerable:!0,get:function(){return t[o]}})}:function(e,t,o,s){void 0===s&&(s=o),e[s]=t[o]}),i=this&&this.__exportStar||function(e,t){for(var o in e)"default"===o||Object.prototype.hasOwnProperty.call(t,o)||s(t,e,o)};Object.defineProperty(t,"__esModule",{value:!0}),i(o(455),t),i(o(528),t),i(o(699),t)},528:function(e,t,o){var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.ActuallyColabSocketClient=void 0;const i=s(o(913)),n=s(o(553)),r=s(o(994)),a=o(294);class d extends n.default{constructor(e,t){super(),this.initSocketEventListeners=()=>{this.socketClient.onopen=()=>{this.emit("connect")},this.socketClient.onclose=e=>{this.emit("close",e)},this.socketClient.onerror=e=>{this.emit("error",e)},this.socketClient.onmessage=e=>{if("string"!=typeof e.data||""===e.data)throw new Error("Malformed message received");{const t=JSON.parse(e.data);switch(t.action){case"notebook_opened":{const e=t.data;this.emit("notebook_opened",e.nb_id,e.uid,t.triggered_by);break}case"notebook_contents":{const e=t.data;this.emit("notebook_contents",e,t.triggered_by);break}case"notebook_closed":{const e=t.data;this.emit("notebook_closed",e.nb_id,e.uid,t.triggered_by);break}case"notebook_shared":{const e=t.data;this.emit("notebook_shared",e.nb_id,e.users,t.triggered_by);break}case"notebook_unshared":{const e=t.data;this.emit("notebook_unshared",e.nb_id,e.uids,t.triggered_by);break}case"workshop_shared":{const e=t.data;this.emit("workshop_shared",e.ws_id,e.attendees,e.instructors,t.triggered_by);break}case"workshop_started":{const e=t.data;this.emit("workshop_started",e.ws_id,t.triggered_by);break}case"cell_created":{const e=t.data;this.emit("cell_created",e,t.triggered_by);break}case"cell_deleted":{const e=t.data;this.emit("cell_deleted",e.nb_id,e.cell_id,t.triggered_by);break}case"cell_edited":{const e=t.data;this.emit("cell_edited",e,t.triggered_by);break}case"cell_locked":{const e=t.data;this.emit("cell_locked",e,t.triggered_by);break}case"cell_unlocked":{const e=t.data;this.emit("cell_unlocked",e,t.triggered_by);break}case"output_updated":{const e=t.data;r.default.decompressAsync(e.output,{inputEncoding:"Base64",outputEncoding:"String"},((o,s)=>{if(null!=s)throw console.error(s),new Error("Could not decompress output");this.emit("output_updated",Object.assign(Object.assign({},e),{output:o}),t.triggered_by)}));break}case"chat_message_sent":{const e=t.data;this.emit("chat_message_sent",e,t.triggered_by);break}default:throw new Error("Message of unknown action type received "+e.data)}}}},this.close=()=>{this.socketClient.close()},this.disconnectAndRemoveAllListeners=()=>{this.socketClient.close(),this.removeAllListeners()},this.sendEvent=(e,t)=>{const o=JSON.stringify({action:e,data:t});this.socketClient.send(o)},this.openNotebook=e=>{this.sendEvent("open_notebook",{nb_id:e})},this.closeNotebook=e=>{this.sendEvent("close_notebook",{nb_id:e})},this.shareNotebook=(e,t,o)=>{this.sendEvent("share_notebook",{emails:e,nb_id:t,access_level:o})},this.unshareNotebook=(e,t)=>{this.sendEvent("share_notebook",{emails:e,nb_id:t,access_level:null})},this.shareWorkshop=(e,t,o)=>{this.sendEvent("share_workshop",{emails:e,ws_id:t,access_level:o})},this.startWorkshop=e=>{this.sendEvent("start_workshop",{ws_id:e})},this.createCell=(e,t)=>{this.sendEvent("create_cell",{nb_id:e,language:t})},this.lockCell=(e,t)=>{this.sendEvent("lock_cell",{nb_id:e,cell_id:t})},this.unlockCell=(e,t,o)=>{this.editCell.flush(e,t,o),this.sendEvent("unlock_cell",{nb_id:e,cell_id:t})},this.editCell=a.memoizeDebounce(((e,t,o)=>{this.sendEvent("edit_cell",{nb_id:e,cell_id:t,cellData:o})}),500,{maxWait:1e3},((e,t,o)=>e+t)),this.deleteCell=(e,t)=>{this.sendEvent("edit_cell",{nb_id:e,cell_id:t,cellData:null})},this.updateOutput=a.memoizeDebounce(((e,t,o)=>{r.default.compressAsync(o,{outputEncoding:"Base64"},((o,s)=>{if(null!=s)throw console.error(s),new Error(`Could not compress output data for cell_id ${t}`);this.sendEvent("update_output",{nb_id:e,cell_id:t,output:o})}))}),1e3,{maxWait:2e3},((e,t,o)=>e+t)),this.sendChatMessage=(e,t)=>{this.sendEvent("send_chat_message",{nb_id:e,message:t})};const o=`${e}/?sessionToken=Bearer ${t}`;this.socketClient=new i.default.w3cwebsocket(o),this.initSocketEventListeners()}}t.ActuallyColabSocketClient=d},294:function(e,t,o){var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.memoizeDebounce=void 0;const i=s(o(804));t.memoizeDebounce=function(e,t=0,o={},s){const n=i.default.memoize(((...s)=>i.default.debounce(e,t,o)),s);function r(...e){return n(...e)(...e)}return r.flush=(...e)=>n(...e).flush(),r.cancel=(...e)=>n(...e).cancel(),r}},699:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},376:e=>{e.exports=require("axios")},236:e=>{e.exports=require("axios-retry")},553:e=>{e.exports=require("eventemitter3")},804:e=>{e.exports=require("lodash")},994:e=>{e.exports=require("lzutf8")},913:e=>{e.exports=require("websocket")}},t={};return function o(s){if(t[s])return t[s].exports;var i=t[s]={exports:{}};return e[s].call(i.exports,i,i.exports,o),i.exports}(607)})()); //# sourceMappingURL=index.js.map