@devbookhq/sdk
Advanced tools
Comparing version 2.1.3 to 2.1.4
@@ -13,2 +13,2 @@ import 'cross-fetch/polyfill'; | ||
export default client; | ||
export type { components }; | ||
export type { components, paths }; |
@@ -6,1 +6,3 @@ export { default as Session } from './session'; | ||
export type { TerminalManager, TerminalSession, } from './session/terminal'; | ||
export { default as api } from './api'; | ||
export type { components, paths, } from './api'; |
@@ -15,3 +15,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var s=require("rpc-websocket-client");require("cross-fetch/polyfill");var i=require("openapi-typescript-fetch"); | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function e(s,i,e,t){return new(e||(e=Promise))((function(o,n){function r(s){try{h(t.next(s))}catch(s){n(s)}}function c(s){try{h(t.throw(s))}catch(s){n(s)}}function h(s){var i;s.done?o(s.value):(i=s.value,i instanceof e?i:new e((function(s){s(i)}))).then(r,c)}h((t=t.apply(s,i||[])).next())}))}var t;exports.CodeSnippetExecState=void 0,(t=exports.CodeSnippetExecState||(exports.CodeSnippetExecState={})).Running="Running",t.Stopped="Stopped",t.Loading="Loading";const o=i.Fetcher.for();function n(s){return new Promise((i=>setTimeout(i,s)))}o.configure({baseUrl:"https://ondevbook.com"});class r{constructor(s,i=!1){this.logID=s,this.isEnabled=i}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...s){this.isEnabled&&console.log(`[36m[${this.id()}][0m`,...s)}error(...s){console.error(`[31m[${this.id()} ERROR][0m`,...s)}}const c=o.path("/sessions").method("post").create(),h=o.path("/sessions/{sessionID}/refresh").method("put").create();exports.Session=class extends class{constructor(i){this.opts=i,this.isOpen=!1,this.rpc=new s.RpcWebSocketClient,this.subscribers=[],this.logger=new r("Session",i.debug),this.logger.log(`Session for code snippet "${i.id}" initialized`)}call(s,i){return e(this,void 0,void 0,(function*(){return this.rpc.call(s,i)}))}unsubscribe(s,i){return e(this,void 0,void 0,(function*(){const e=this.subscribers.find((s=>s.handler===i));e&&(yield this.call(`${s}_unsubscribe`,[null==e?void 0:e.id]),this.subscribers=this.subscribers.filter((s=>s.handler!==i)),this.logger.log(`Unsubscribed from method "${s}"`))}))}subscribe(s,i,t){return e(this,void 0,void 0,(function*(){const e=yield this.call(`${s}_subscribe`,[t]);if("string"!=typeof e)throw new Error(`Cannot subscribe to method ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(e)}`);this.subscribers.push({id:e,handler:i}),this.logger.log(`Subscribed to method "${s}" with id "${e}"`)}))}getHostname(s){if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=`${this.session.sessionID}-${this.session.clientID}.ondevbook.com`;return s?`${s}-${i}`:i}close(){var s,i,e;this.isOpen&&(this.isOpen=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(e=null===(i=this.opts)||void 0===i?void 0:i.onClose)||void 0===e||e.call(i),this.logger.log("Disconected from the session"))}open(){return e(this,void 0,void 0,(function*(){if(this.isOpen||this.session)throw new Error("Session connect was already called");this.isOpen=!0;try{const s=yield c({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof c.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - (${i.status}) bad request: ${i.data.message}`);if(500===i.status)throw new Error(`Error creating session - (${i.status}) server error: ${i.data.message}`);throw s}}if(!this.session)throw new Error("Session is not defined");const s=`wss://${this.getHostname(8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s),this.logger.log("Connected to session:",this.session),this.rpc.onClose((i=>e(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,i),this.isOpen){yield n(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(s),this.logger.log("Reconnected to session:",this.session)}catch(s){this.logger.error("Failed reconnecting to session:",this.session)}}})))),this.rpc.onError((s=>{this.logger.error("Error in WS session:",this.session,s)})),this.rpc.onNotification.push(this.handleNotification.bind(this))}))}handleNotification(s){this.subscribers.filter((i=>{var e;return i.id===(null===(e=s.params)||void 0===e?void 0:e.subscription)})).forEach((i=>{var e;return i.handler(null===(e=s.params)||void 0===e?void 0:e.result)}))}refresh(s){return e(this,void 0,void 0,(function*(){this.logger.log(`Started refreshing session "${s}"`);try{for(;;){if(!this.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield n(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield h({sessionID:s})}catch(i){if(i instanceof h.Error){const e=i.getActualType();if(404===e.status)return void this.logger.error(`Error refreshing session - (${e.status}): ${e.data.message}`);this.logger.error(`Refreshing session "${s}" failed - (${e.status})`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.close()}}))}}{constructor(s){super(s),this.codeSnippetOpts=s.codeSnippet}open(){const s=Object.create(null,{open:{get:()=>super.open}});var i,t,o;return e(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>e(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)})),stop:()=>e(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))},yield Promise.all([(null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStateChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStateChange,"state"):Promise.resolve(),(null===(t=this.codeSnippetOpts)||void 0===t?void 0:t.onStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStderr,"stderr"):Promise.resolve(),(null===(o=this.codeSnippetOpts)||void 0===o?void 0:o.onStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStdout,"stdout"):Promise.resolve()]),this.terminal={createSession:(s,i)=>e(this,void 0,void 0,(function*(){const t=yield this.call("terminal_start",i?[i]:[]);if("string"!=typeof t)throw new Error("Cannot initialize terminal");return yield this.subscribe("terminal",s,[t]),{destroy:()=>e(this,void 0,void 0,(function*(){yield this.unsubscribe("terminal",s)})),sendData:s=>e(this,void 0,void 0,(function*(){yield this.call("terminal_data",[t,s])})),resize:({cols:s,rows:i})=>e(this,void 0,void 0,(function*(){yield this.call("terminal_resize",[t,s,i])}))}}))}}))}}; | ||
***************************************************************************** */function e(s,i,e,t){return new(e||(e=Promise))((function(o,n){function r(s){try{h(t.next(s))}catch(s){n(s)}}function c(s){try{h(t.throw(s))}catch(s){n(s)}}function h(s){var i;s.done?o(s.value):(i=s.value,i instanceof e?i:new e((function(s){s(i)}))).then(r,c)}h((t=t.apply(s,i||[])).next())}))}var t;exports.CodeSnippetExecState=void 0,(t=exports.CodeSnippetExecState||(exports.CodeSnippetExecState={})).Running="Running",t.Stopped="Stopped",t.Loading="Loading";const o=i.Fetcher.for();function n(s){return new Promise((i=>setTimeout(i,s)))}o.configure({baseUrl:"https://ondevbook.com"});class r{constructor(s,i=!1){this.logID=s,this.isEnabled=i}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...s){this.isEnabled&&console.log(`[36m[${this.id()}][0m`,...s)}error(...s){console.error(`[31m[${this.id()} ERROR][0m`,...s)}}const c=o.path("/sessions").method("post").create(),h=o.path("/sessions/{sessionID}/refresh").method("put").create();exports.Session=class extends class{constructor(i){this.opts=i,this.isOpen=!1,this.rpc=new s.RpcWebSocketClient,this.subscribers=[],this.logger=new r("Session",i.debug),this.logger.log(`Session for code snippet "${i.id}" initialized`)}call(s,i){return e(this,void 0,void 0,(function*(){return this.rpc.call(s,i)}))}unsubscribe(s,i){return e(this,void 0,void 0,(function*(){const e=this.subscribers.find((s=>s.handler===i));e&&(yield this.call(`${s}_unsubscribe`,[null==e?void 0:e.id]),this.subscribers=this.subscribers.filter((s=>s.handler!==i)),this.logger.log(`Unsubscribed from method "${s}"`))}))}subscribe(s,i,t){return e(this,void 0,void 0,(function*(){const e=yield this.call(`${s}_subscribe`,[t]);if("string"!=typeof e)throw new Error(`Cannot subscribe to method ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(e)}`);this.subscribers.push({id:e,handler:i}),this.logger.log(`Subscribed to method "${s}" with id "${e}"`)}))}getHostname(s){if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=`${this.session.sessionID}-${this.session.clientID}.ondevbook.com`;return s?`${s}-${i}`:i}close(){var s,i,e;this.isOpen&&(this.isOpen=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(e=null===(i=this.opts)||void 0===i?void 0:i.onClose)||void 0===e||e.call(i),this.logger.log("Disconected from the session"))}open(){return e(this,void 0,void 0,(function*(){if(this.isOpen||this.session)throw new Error("Session connect was already called");this.isOpen=!0;try{const s=yield c({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof c.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - (${i.status}) bad request: ${i.data.message}`);if(500===i.status)throw new Error(`Error creating session - (${i.status}) server error: ${i.data.message}`);throw s}}if(!this.session)throw new Error("Session is not defined");const s=`wss://${this.getHostname(8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s),this.logger.log("Connected to session:",this.session),this.rpc.onClose((i=>e(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,i),this.isOpen){yield n(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(s),this.logger.log("Reconnected to session:",this.session)}catch(s){this.logger.error("Failed reconnecting to session:",this.session)}}})))),this.rpc.onError((s=>{this.logger.error("Error in WS session:",this.session,s)})),this.rpc.onNotification.push(this.handleNotification.bind(this))}))}handleNotification(s){this.subscribers.filter((i=>{var e;return i.id===(null===(e=s.params)||void 0===e?void 0:e.subscription)})).forEach((i=>{var e;return i.handler(null===(e=s.params)||void 0===e?void 0:e.result)}))}refresh(s){return e(this,void 0,void 0,(function*(){this.logger.log(`Started refreshing session "${s}"`);try{for(;;){if(!this.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield n(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield h({sessionID:s})}catch(i){if(i instanceof h.Error){const e=i.getActualType();if(404===e.status)return void this.logger.error(`Error refreshing session - (${e.status}): ${e.data.message}`);this.logger.error(`Refreshing session "${s}" failed - (${e.status})`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.close()}}))}}{constructor(s){super(s),this.codeSnippetOpts=s.codeSnippet}open(){const s=Object.create(null,{open:{get:()=>super.open}});var i,t,o;return e(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>e(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)})),stop:()=>e(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))},yield Promise.all([(null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStateChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStateChange,"state"):Promise.resolve(),(null===(t=this.codeSnippetOpts)||void 0===t?void 0:t.onStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStderr,"stderr"):Promise.resolve(),(null===(o=this.codeSnippetOpts)||void 0===o?void 0:o.onStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStdout,"stdout"):Promise.resolve()]),this.terminal={createSession:(s,i)=>e(this,void 0,void 0,(function*(){const t=yield this.call("terminal_start",i?[i]:[]);if("string"!=typeof t)throw new Error("Cannot initialize terminal");return yield this.subscribe("terminal",s,[t]),{destroy:()=>e(this,void 0,void 0,(function*(){yield this.unsubscribe("terminal",s)})),sendData:s=>e(this,void 0,void 0,(function*(){yield this.call("terminal_data",[t,s])})),resize:({cols:s,rows:i})=>e(this,void 0,void 0,(function*(){yield this.call("terminal_resize",[t,s,i])}))}}))}}))}},exports.api=o; | ||
//# sourceMappingURL=index.js.map |
@@ -13,2 +13,2 @@ import 'cross-fetch/polyfill'; | ||
export default client; | ||
export type { components }; | ||
export type { components, paths }; |
@@ -6,1 +6,3 @@ export { default as Session } from './session'; | ||
export type { TerminalManager, TerminalSession, } from './session/terminal'; | ||
export { default as api } from './api'; | ||
export type { components, paths, } from './api'; |
@@ -15,3 +15,3 @@ import{RpcWebSocketClient as s}from"rpc-websocket-client";import"cross-fetch/polyfill";import{Fetcher as i}from"openapi-typescript-fetch"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function t(s,i,t,e){return new(t||(t=Promise))((function(o,n){function r(s){try{h(e.next(s))}catch(s){n(s)}}function c(s){try{h(e.throw(s))}catch(s){n(s)}}function h(s){var i;s.done?o(s.value):(i=s.value,i instanceof t?i:new t((function(s){s(i)}))).then(r,c)}h((e=e.apply(s,i||[])).next())}))}var e;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(e||(e={}));const o=i.for();function n(s){return new Promise((i=>setTimeout(i,s)))}o.configure({baseUrl:"https://ondevbook.com"});class r{constructor(s,i=!1){this.logID=s,this.isEnabled=i}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...s){this.isEnabled&&console.log(`[36m[${this.id()}][0m`,...s)}error(...s){console.error(`[31m[${this.id()} ERROR][0m`,...s)}}const c=o.path("/sessions").method("post").create(),h=o.path("/sessions/{sessionID}/refresh").method("put").create();class l extends class{constructor(i){this.opts=i,this.isOpen=!1,this.rpc=new s,this.subscribers=[],this.logger=new r("Session",i.debug),this.logger.log(`Session for code snippet "${i.id}" initialized`)}call(s,i){return t(this,void 0,void 0,(function*(){return this.rpc.call(s,i)}))}unsubscribe(s,i){return t(this,void 0,void 0,(function*(){const t=this.subscribers.find((s=>s.handler===i));t&&(yield this.call(`${s}_unsubscribe`,[null==t?void 0:t.id]),this.subscribers=this.subscribers.filter((s=>s.handler!==i)),this.logger.log(`Unsubscribed from method "${s}"`))}))}subscribe(s,i,e){return t(this,void 0,void 0,(function*(){const t=yield this.call(`${s}_subscribe`,[e]);if("string"!=typeof t)throw new Error(`Cannot subscribe to method ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:i}),this.logger.log(`Subscribed to method "${s}" with id "${t}"`)}))}getHostname(s){if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=`${this.session.sessionID}-${this.session.clientID}.ondevbook.com`;return s?`${s}-${i}`:i}close(){var s,i,t;this.isOpen&&(this.isOpen=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(t=null===(i=this.opts)||void 0===i?void 0:i.onClose)||void 0===t||t.call(i),this.logger.log("Disconected from the session"))}open(){return t(this,void 0,void 0,(function*(){if(this.isOpen||this.session)throw new Error("Session connect was already called");this.isOpen=!0;try{const s=yield c({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof c.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - (${i.status}) bad request: ${i.data.message}`);if(500===i.status)throw new Error(`Error creating session - (${i.status}) server error: ${i.data.message}`);throw s}}if(!this.session)throw new Error("Session is not defined");const s=`wss://${this.getHostname(8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s),this.logger.log("Connected to session:",this.session),this.rpc.onClose((i=>t(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,i),this.isOpen){yield n(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(s),this.logger.log("Reconnected to session:",this.session)}catch(s){this.logger.error("Failed reconnecting to session:",this.session)}}})))),this.rpc.onError((s=>{this.logger.error("Error in WS session:",this.session,s)})),this.rpc.onNotification.push(this.handleNotification.bind(this))}))}handleNotification(s){this.subscribers.filter((i=>{var t;return i.id===(null===(t=s.params)||void 0===t?void 0:t.subscription)})).forEach((i=>{var t;return i.handler(null===(t=s.params)||void 0===t?void 0:t.result)}))}refresh(s){return t(this,void 0,void 0,(function*(){this.logger.log(`Started refreshing session "${s}"`);try{for(;;){if(!this.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield n(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield h({sessionID:s})}catch(i){if(i instanceof h.Error){const t=i.getActualType();if(404===t.status)return void this.logger.error(`Error refreshing session - (${t.status}): ${t.data.message}`);this.logger.error(`Refreshing session "${s}" failed - (${t.status})`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.close()}}))}}{constructor(s){super(s),this.codeSnippetOpts=s.codeSnippet}open(){const s=Object.create(null,{open:{get:()=>super.open}});var i,e,o;return t(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)})),stop:()=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))},yield Promise.all([(null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStateChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStateChange,"state"):Promise.resolve(),(null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStderr,"stderr"):Promise.resolve(),(null===(o=this.codeSnippetOpts)||void 0===o?void 0:o.onStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStdout,"stdout"):Promise.resolve()]),this.terminal={createSession:(s,i)=>t(this,void 0,void 0,(function*(){const e=yield this.call("terminal_start",i?[i]:[]);if("string"!=typeof e)throw new Error("Cannot initialize terminal");return yield this.subscribe("terminal",s,[e]),{destroy:()=>t(this,void 0,void 0,(function*(){yield this.unsubscribe("terminal",s)})),sendData:s=>t(this,void 0,void 0,(function*(){yield this.call("terminal_data",[e,s])})),resize:({cols:s,rows:i})=>t(this,void 0,void 0,(function*(){yield this.call("terminal_resize",[e,s,i])}))}}))}}))}}export{e as CodeSnippetExecState,l as Session}; | ||
***************************************************************************** */function t(s,i,t,e){return new(t||(t=Promise))((function(o,n){function r(s){try{h(e.next(s))}catch(s){n(s)}}function c(s){try{h(e.throw(s))}catch(s){n(s)}}function h(s){var i;s.done?o(s.value):(i=s.value,i instanceof t?i:new t((function(s){s(i)}))).then(r,c)}h((e=e.apply(s,i||[])).next())}))}var e;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(e||(e={}));const o=i.for();function n(s){return new Promise((i=>setTimeout(i,s)))}o.configure({baseUrl:"https://ondevbook.com"});class r{constructor(s,i=!1){this.logID=s,this.isEnabled=i}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...s){this.isEnabled&&console.log(`[36m[${this.id()}][0m`,...s)}error(...s){console.error(`[31m[${this.id()} ERROR][0m`,...s)}}const c=o.path("/sessions").method("post").create(),h=o.path("/sessions/{sessionID}/refresh").method("put").create();class l extends class{constructor(i){this.opts=i,this.isOpen=!1,this.rpc=new s,this.subscribers=[],this.logger=new r("Session",i.debug),this.logger.log(`Session for code snippet "${i.id}" initialized`)}call(s,i){return t(this,void 0,void 0,(function*(){return this.rpc.call(s,i)}))}unsubscribe(s,i){return t(this,void 0,void 0,(function*(){const t=this.subscribers.find((s=>s.handler===i));t&&(yield this.call(`${s}_unsubscribe`,[null==t?void 0:t.id]),this.subscribers=this.subscribers.filter((s=>s.handler!==i)),this.logger.log(`Unsubscribed from method "${s}"`))}))}subscribe(s,i,e){return t(this,void 0,void 0,(function*(){const t=yield this.call(`${s}_subscribe`,[e]);if("string"!=typeof t)throw new Error(`Cannot subscribe to method ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:i}),this.logger.log(`Subscribed to method "${s}" with id "${t}"`)}))}getHostname(s){if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=`${this.session.sessionID}-${this.session.clientID}.ondevbook.com`;return s?`${s}-${i}`:i}close(){var s,i,t;this.isOpen&&(this.isOpen=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(t=null===(i=this.opts)||void 0===i?void 0:i.onClose)||void 0===t||t.call(i),this.logger.log("Disconected from the session"))}open(){return t(this,void 0,void 0,(function*(){if(this.isOpen||this.session)throw new Error("Session connect was already called");this.isOpen=!0;try{const s=yield c({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof c.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - (${i.status}) bad request: ${i.data.message}`);if(500===i.status)throw new Error(`Error creating session - (${i.status}) server error: ${i.data.message}`);throw s}}if(!this.session)throw new Error("Session is not defined");const s=`wss://${this.getHostname(8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s),this.logger.log("Connected to session:",this.session),this.rpc.onClose((i=>t(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,i),this.isOpen){yield n(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(s),this.logger.log("Reconnected to session:",this.session)}catch(s){this.logger.error("Failed reconnecting to session:",this.session)}}})))),this.rpc.onError((s=>{this.logger.error("Error in WS session:",this.session,s)})),this.rpc.onNotification.push(this.handleNotification.bind(this))}))}handleNotification(s){this.subscribers.filter((i=>{var t;return i.id===(null===(t=s.params)||void 0===t?void 0:t.subscription)})).forEach((i=>{var t;return i.handler(null===(t=s.params)||void 0===t?void 0:t.result)}))}refresh(s){return t(this,void 0,void 0,(function*(){this.logger.log(`Started refreshing session "${s}"`);try{for(;;){if(!this.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield n(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield h({sessionID:s})}catch(i){if(i instanceof h.Error){const t=i.getActualType();if(404===t.status)return void this.logger.error(`Error refreshing session - (${t.status}): ${t.data.message}`);this.logger.error(`Refreshing session "${s}" failed - (${t.status})`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.close()}}))}}{constructor(s){super(s),this.codeSnippetOpts=s.codeSnippet}open(){const s=Object.create(null,{open:{get:()=>super.open}});var i,e,o;return t(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)})),stop:()=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))},yield Promise.all([(null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStateChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStateChange,"state"):Promise.resolve(),(null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStderr,"stderr"):Promise.resolve(),(null===(o=this.codeSnippetOpts)||void 0===o?void 0:o.onStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onStdout,"stdout"):Promise.resolve()]),this.terminal={createSession:(s,i)=>t(this,void 0,void 0,(function*(){const e=yield this.call("terminal_start",i?[i]:[]);if("string"!=typeof e)throw new Error("Cannot initialize terminal");return yield this.subscribe("terminal",s,[e]),{destroy:()=>t(this,void 0,void 0,(function*(){yield this.unsubscribe("terminal",s)})),sendData:s=>t(this,void 0,void 0,(function*(){yield this.call("terminal_data",[e,s])})),resize:({cols:s,rows:i})=>t(this,void 0,void 0,(function*(){yield this.call("terminal_resize",[e,s,i])}))}}))}}))}}export{e as CodeSnippetExecState,l as Session,o as api}; | ||
//# sourceMappingURL=index.js.map |
@@ -13,2 +13,2 @@ import 'cross-fetch/polyfill'; | ||
export default client; | ||
export type { components }; | ||
export type { components, paths }; |
@@ -6,1 +6,3 @@ export { default as Session } from './session'; | ||
export type { TerminalManager, TerminalSession, } from './session/terminal'; | ||
export { default as api } from './api'; | ||
export type { components, paths, } from './api'; |
@@ -15,3 +15,3 @@ !function(s,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("rpc-websocket-client"),require("cross-fetch/polyfill"),require("openapi-typescript-fetch")):"function"==typeof define&&define.amd?define(["exports","rpc-websocket-client","cross-fetch/polyfill","openapi-typescript-fetch"],e):e((s="undefined"!=typeof globalThis?globalThis:s||self)["@devbookhq/sdk"]={},s.rpcWebsocketClient,null,s.openapiTypescriptFetch)}(this,(function(s,e,i,t){"use strict"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function o(s,e,i,t){return new(i||(i=Promise))((function(o,n){function r(s){try{l(t.next(s))}catch(s){n(s)}}function c(s){try{l(t.throw(s))}catch(s){n(s)}}function l(s){var e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,c)}l((t=t.apply(s,e||[])).next())}))}const n="codeSnippet";var r;s.CodeSnippetExecState=void 0,(r=s.CodeSnippetExecState||(s.CodeSnippetExecState={})).Running="Running",r.Stopped="Stopped",r.Loading="Loading";const c="terminal",l=t.Fetcher.for();function h(s){return new Promise((e=>setTimeout(e,s)))}l.configure({baseUrl:"https://ondevbook.com"});class d{constructor(s,e=!1){this.logID=s,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...s){this.isEnabled&&console.log(`[36m[${this.id()}][0m`,...s)}error(...s){console.error(`[31m[${this.id()} ERROR][0m`,...s)}}const a=l.path("/sessions").method("post").create(),p=l.path("/sessions/{sessionID}/refresh").method("put").create();s.Session=class extends class{constructor(s){this.opts=s,this.isOpen=!1,this.rpc=new e.RpcWebSocketClient,this.subscribers=[],this.logger=new d("Session",s.debug),this.logger.log(`Session for code snippet "${s.id}" initialized`)}call(s,e){return o(this,void 0,void 0,(function*(){return this.rpc.call(s,e)}))}unsubscribe(s,e){return o(this,void 0,void 0,(function*(){const i=this.subscribers.find((s=>s.handler===e));i&&(yield this.call(`${s}_unsubscribe`,[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((s=>s.handler!==e)),this.logger.log(`Unsubscribed from method "${s}"`))}))}subscribe(s,e,i){return o(this,void 0,void 0,(function*(){const t=yield this.call(`${s}_subscribe`,[i]);if("string"!=typeof t)throw new Error(`Cannot subscribe to method ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:e}),this.logger.log(`Subscribed to method "${s}" with id "${t}"`)}))}getHostname(s){if(!this.isOpen||!this.session)throw new Error("Session is not active");const e=`${this.session.sessionID}-${this.session.clientID}.ondevbook.com`;return s?`${s}-${e}`:e}close(){var s,e,i;this.isOpen&&(this.isOpen=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(i=null===(e=this.opts)||void 0===e?void 0:e.onClose)||void 0===i||i.call(e),this.logger.log("Disconected from the session"))}open(){return o(this,void 0,void 0,(function*(){if(this.isOpen||this.session)throw new Error("Session connect was already called");this.isOpen=!0;try{const s=yield a({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof a.Error){const e=s.getActualType();if(400===e.status)throw new Error(`Error creating session - (${e.status}) bad request: ${e.data.message}`);if(500===e.status)throw new Error(`Error creating session - (${e.status}) server error: ${e.data.message}`);throw s}}if(!this.session)throw new Error("Session is not defined");const s=`wss://${this.getHostname(8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s),this.logger.log("Connected to session:",this.session),this.rpc.onClose((e=>o(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,e),this.isOpen){yield h(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(s),this.logger.log("Reconnected to session:",this.session)}catch(s){this.logger.error("Failed reconnecting to session:",this.session)}}})))),this.rpc.onError((s=>{this.logger.error("Error in WS session:",this.session,s)})),this.rpc.onNotification.push(this.handleNotification.bind(this))}))}handleNotification(s){this.subscribers.filter((e=>{var i;return e.id===(null===(i=s.params)||void 0===i?void 0:i.subscription)})).forEach((e=>{var i;return e.handler(null===(i=s.params)||void 0===i?void 0:i.result)}))}refresh(s){return o(this,void 0,void 0,(function*(){this.logger.log(`Started refreshing session "${s}"`);try{for(;;){if(!this.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield h(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield p({sessionID:s})}catch(e){if(e instanceof p.Error){const i=e.getActualType();if(404===i.status)return void this.logger.error(`Error refreshing session - (${i.status}): ${i.data.message}`);this.logger.error(`Refreshing session "${s}" failed - (${i.status})`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.close()}}))}}{constructor(s){super(s),this.codeSnippetOpts=s.codeSnippet}open(){const s=Object.create(null,{open:{get:()=>super.open}});var e,i,t;return o(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)})),stop:()=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))},yield Promise.all([(null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStateChange)?this.subscribe(n,this.codeSnippetOpts.onStateChange,"state"):Promise.resolve(),(null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStderr)?this.subscribe(n,this.codeSnippetOpts.onStderr,"stderr"):Promise.resolve(),(null===(t=this.codeSnippetOpts)||void 0===t?void 0:t.onStdout)?this.subscribe(n,this.codeSnippetOpts.onStdout,"stdout"):Promise.resolve()]),this.terminal={createSession:(s,e)=>o(this,void 0,void 0,(function*(){const i=yield this.call("terminal_start",e?[e]:[]);if("string"!=typeof i)throw new Error("Cannot initialize terminal");return yield this.subscribe(c,s,[i]),{destroy:()=>o(this,void 0,void 0,(function*(){yield this.unsubscribe(c,s)})),sendData:s=>o(this,void 0,void 0,(function*(){yield this.call("terminal_data",[i,s])})),resize:({cols:s,rows:e})=>o(this,void 0,void 0,(function*(){yield this.call("terminal_resize",[i,s,e])}))}}))}}))}},Object.defineProperty(s,"__esModule",{value:!0})})); | ||
***************************************************************************** */function o(s,e,i,t){return new(i||(i=Promise))((function(o,n){function r(s){try{l(t.next(s))}catch(s){n(s)}}function c(s){try{l(t.throw(s))}catch(s){n(s)}}function l(s){var e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,c)}l((t=t.apply(s,e||[])).next())}))}const n="codeSnippet";var r;s.CodeSnippetExecState=void 0,(r=s.CodeSnippetExecState||(s.CodeSnippetExecState={})).Running="Running",r.Stopped="Stopped",r.Loading="Loading";const c="terminal",l=t.Fetcher.for();function h(s){return new Promise((e=>setTimeout(e,s)))}l.configure({baseUrl:"https://ondevbook.com"});class d{constructor(s,e=!1){this.logID=s,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...s){this.isEnabled&&console.log(`[36m[${this.id()}][0m`,...s)}error(...s){console.error(`[31m[${this.id()} ERROR][0m`,...s)}}const a=l.path("/sessions").method("post").create(),p=l.path("/sessions/{sessionID}/refresh").method("put").create();s.Session=class extends class{constructor(s){this.opts=s,this.isOpen=!1,this.rpc=new e.RpcWebSocketClient,this.subscribers=[],this.logger=new d("Session",s.debug),this.logger.log(`Session for code snippet "${s.id}" initialized`)}call(s,e){return o(this,void 0,void 0,(function*(){return this.rpc.call(s,e)}))}unsubscribe(s,e){return o(this,void 0,void 0,(function*(){const i=this.subscribers.find((s=>s.handler===e));i&&(yield this.call(`${s}_unsubscribe`,[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((s=>s.handler!==e)),this.logger.log(`Unsubscribed from method "${s}"`))}))}subscribe(s,e,i){return o(this,void 0,void 0,(function*(){const t=yield this.call(`${s}_subscribe`,[i]);if("string"!=typeof t)throw new Error(`Cannot subscribe to method ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:e}),this.logger.log(`Subscribed to method "${s}" with id "${t}"`)}))}getHostname(s){if(!this.isOpen||!this.session)throw new Error("Session is not active");const e=`${this.session.sessionID}-${this.session.clientID}.ondevbook.com`;return s?`${s}-${e}`:e}close(){var s,e,i;this.isOpen&&(this.isOpen=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(i=null===(e=this.opts)||void 0===e?void 0:e.onClose)||void 0===i||i.call(e),this.logger.log("Disconected from the session"))}open(){return o(this,void 0,void 0,(function*(){if(this.isOpen||this.session)throw new Error("Session connect was already called");this.isOpen=!0;try{const s=yield a({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof a.Error){const e=s.getActualType();if(400===e.status)throw new Error(`Error creating session - (${e.status}) bad request: ${e.data.message}`);if(500===e.status)throw new Error(`Error creating session - (${e.status}) server error: ${e.data.message}`);throw s}}if(!this.session)throw new Error("Session is not defined");const s=`wss://${this.getHostname(8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s),this.logger.log("Connected to session:",this.session),this.rpc.onClose((e=>o(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,e),this.isOpen){yield h(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(s),this.logger.log("Reconnected to session:",this.session)}catch(s){this.logger.error("Failed reconnecting to session:",this.session)}}})))),this.rpc.onError((s=>{this.logger.error("Error in WS session:",this.session,s)})),this.rpc.onNotification.push(this.handleNotification.bind(this))}))}handleNotification(s){this.subscribers.filter((e=>{var i;return e.id===(null===(i=s.params)||void 0===i?void 0:i.subscription)})).forEach((e=>{var i;return e.handler(null===(i=s.params)||void 0===i?void 0:i.result)}))}refresh(s){return o(this,void 0,void 0,(function*(){this.logger.log(`Started refreshing session "${s}"`);try{for(;;){if(!this.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield h(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield p({sessionID:s})}catch(e){if(e instanceof p.Error){const i=e.getActualType();if(404===i.status)return void this.logger.error(`Error refreshing session - (${i.status}): ${i.data.message}`);this.logger.error(`Refreshing session "${s}" failed - (${i.status})`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.close()}}))}}{constructor(s){super(s),this.codeSnippetOpts=s.codeSnippet}open(){const s=Object.create(null,{open:{get:()=>super.open}});var e,i,t;return o(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)})),stop:()=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");yield this.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))},yield Promise.all([(null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStateChange)?this.subscribe(n,this.codeSnippetOpts.onStateChange,"state"):Promise.resolve(),(null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStderr)?this.subscribe(n,this.codeSnippetOpts.onStderr,"stderr"):Promise.resolve(),(null===(t=this.codeSnippetOpts)||void 0===t?void 0:t.onStdout)?this.subscribe(n,this.codeSnippetOpts.onStdout,"stdout"):Promise.resolve()]),this.terminal={createSession:(s,e)=>o(this,void 0,void 0,(function*(){const i=yield this.call("terminal_start",e?[e]:[]);if("string"!=typeof i)throw new Error("Cannot initialize terminal");return yield this.subscribe(c,s,[i]),{destroy:()=>o(this,void 0,void 0,(function*(){yield this.unsubscribe(c,s)})),sendData:s=>o(this,void 0,void 0,(function*(){yield this.call("terminal_data",[i,s])})),resize:({cols:s,rows:e})=>o(this,void 0,void 0,(function*(){yield this.call("terminal_resize",[i,s,e])}))}}))}}))}},s.api=l,Object.defineProperty(s,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@devbookhq/sdk", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"description": "Devbook allows visitors of your docs to interact with and execute any code snippet or shell command in a private VM", | ||
@@ -26,3 +26,3 @@ "homepage": "https://usedevbook.com", | ||
"fix": "npx eslint **/src/**/*.{ts,tsx,js} --fix", | ||
"generate": "openapi-typescript shared/api/openapi.yml --immutable-types --output src/api/schema.gen.d.ts" | ||
"generate": "openapi-typescript shared/api/openapi.yml --immutable-types --output src/api/schema.gen.ts" | ||
}, | ||
@@ -29,0 +29,0 @@ "devDependencies": { |
@@ -12,18 +12,23 @@ # Devbook SDK | ||
```js | ||
import { Session } from '@devbookhq/sdk' | ||
async function main() { | ||
const session = new Session('<codeSnippetID>', { | ||
onStateChange(state) { | ||
console.log(state) | ||
const session = new Session({ | ||
id: '<code-snippet-id>', | ||
codeSnippet: { | ||
onStateChange(state) { | ||
console.log(state) | ||
}, | ||
onStderr(stderr) { | ||
console.log(stderr) | ||
}, | ||
onStdout(stdout) { | ||
console.log(stdout) | ||
}, | ||
}, | ||
onStderr(stderr) { | ||
console.log(stderr) | ||
}, | ||
onStdout(stdout) { | ||
console.log(stdout) | ||
}, | ||
}) | ||
try { | ||
await session.connect() | ||
await session.run('console.log("4")') | ||
await session.open() | ||
await session.codeSnippet?.run('console.error("test")') | ||
} catch (e) { | ||
@@ -30,0 +35,0 @@ console.error(e) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125347
39
1198
60
1