@devbookhq/sdk
Advanced tools
Comparing version 2.1.0 to 2.1.1
export { default as Session } from './session'; | ||
export type { SessionOpts, } from './session'; | ||
export type { CodeSnippetManager, CodeSnippetExecState, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, CodeSnippetSubscriptionEvent, CodeSnippetSubscriptionHandler, CodeSnippetSubscriptionHandlerType, } from './session/codeSnippet'; | ||
export { CodeSnippetExecState, } from './session/codeSnippet'; | ||
export type { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, CodeSnippetSubscriptionEvent, CodeSnippetSubscriptionHandler, CodeSnippetSubscriptionHandlerType, } from './session/codeSnippet'; | ||
export type { TerminalManager, TerminalSession, } from './session/terminal'; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var s=require("rpc-websocket-client");require("cross-fetch/polyfill");var i=require("openapi-typescript-fetch"); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var s=require("rpc-websocket-client");require("cross-fetch/polyfill");var e=require("openapi-typescript-fetch"); | ||
/*! ***************************************************************************** | ||
@@ -15,3 +15,3 @@ Copyright (c) Microsoft Corporation. | ||
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;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(t||(t={}));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,saveFSChanges:this.opts.saveFSChanges});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}codeSnippet(){return this._codeSnippet}terminal(){return this._terminal}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 i(s,e,i,t){return new(i||(i=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 e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,c)}h((t=t.apply(s,e||[])).next())}))}var t;exports.CodeSnippetExecState=void 0,(t=exports.CodeSnippetExecState||(exports.CodeSnippetExecState={})).Running="Running",t.Stopped="Stopped",t.Loading="Loading";const o=e.Fetcher.for();function n(s){return new Promise((e=>setTimeout(e,s)))}o.configure({baseUrl:"https://ondevbook.com"});class r{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 c=o.path("/sessions").method("post").create(),h=o.path("/sessions/{sessionID}/refresh").method("put").create();exports.Session=class extends class{constructor(e){this.opts=e,this.isOpen=!1,this.rpc=new s.RpcWebSocketClient,this.subscribers=[],this.logger=new r("Session",e.debug),this.logger.log(`Session for code snippet "${e.id}" initialized`)}call(s,e){return i(this,void 0,void 0,(function*(){return this.rpc.call(s,e)}))}unsubscribe(s,e){return i(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,t){return i(this,void 0,void 0,(function*(){const i=yield this.call(`${s}_subscribe`,[t]);if("string"!=typeof i)throw new Error(`Cannot subscribe to method ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(i)}`);this.subscribers.push({id:i,handler:e}),this.logger.log(`Subscribed to method "${s}" with id "${i}"`)}))}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 i(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,saveFSChanges:this.opts.saveFSChanges});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof c.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=>i(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,e),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((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 i(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(e){if(e instanceof h.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}codeSnippet(){return this._codeSnippet}terminal(){return this._terminal}open(){const s=Object.create(null,{open:{get:()=>super.open}});var e,t,o;return i(this,void 0,void 0,(function*(){yield s.open.call(this),this._codeSnippet={run:s=>i(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:()=>i(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("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,e)=>i(this,void 0,void 0,(function*(){const t=yield this.call("terminal_start",e?[e]:[]);if("string"!=typeof t)throw new Error("Cannot initialize terminal");return yield this.subscribe("terminal",s,[t]),{destroy:()=>i(this,void 0,void 0,(function*(){yield this.unsubscribe("terminal",s)})),sendData:s=>i(this,void 0,void 0,(function*(){yield this.call("terminal_data",[t,s])})),resize:({cols:s,rows:e})=>i(this,void 0,void 0,(function*(){yield this.call("terminal_resize",[t,s,e])}))}}))}}))}}; | ||
//# sourceMappingURL=index.js.map |
export { default as Session } from './session'; | ||
export type { SessionOpts, } from './session'; | ||
export type { CodeSnippetManager, CodeSnippetExecState, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, CodeSnippetSubscriptionEvent, CodeSnippetSubscriptionHandler, CodeSnippetSubscriptionHandlerType, } from './session/codeSnippet'; | ||
export { CodeSnippetExecState, } from './session/codeSnippet'; | ||
export type { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, CodeSnippetSubscriptionEvent, CodeSnippetSubscriptionHandler, CodeSnippetSubscriptionHandlerType, } from './session/codeSnippet'; | ||
export type { TerminalManager, TerminalSession, } from './session/terminal'; |
@@ -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 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;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(t||(t={}));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 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,saveFSChanges:this.opts.saveFSChanges});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}codeSnippet(){return this._codeSnippet}terminal(){return this._terminal}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])}))}}))}}))}}export{l as Session}; | ||
***************************************************************************** */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;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(t||(t={}));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 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,saveFSChanges:this.opts.saveFSChanges});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}codeSnippet(){return this._codeSnippet}terminal(){return this._terminal}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])}))}}))}}))}}export{t as CodeSnippetExecState,l as Session}; | ||
//# sourceMappingURL=index.js.map |
export { default as Session } from './session'; | ||
export type { SessionOpts, } from './session'; | ||
export type { CodeSnippetManager, CodeSnippetExecState, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, CodeSnippetSubscriptionEvent, CodeSnippetSubscriptionHandler, CodeSnippetSubscriptionHandlerType, } from './session/codeSnippet'; | ||
export { CodeSnippetExecState, } from './session/codeSnippet'; | ||
export type { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, CodeSnippetSubscriptionEvent, CodeSnippetSubscriptionHandler, CodeSnippetSubscriptionHandlerType, } from './session/codeSnippet'; | ||
export type { TerminalManager, TerminalSession, } from './session/terminal'; |
@@ -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{h(t.next(s))}catch(s){n(s)}}function c(s){try{h(t.throw(s))}catch(s){n(s)}}function h(s){var e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,c)}h((t=t.apply(s,e||[])).next())}))}const n="codeSnippet";var r;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(r||(r={}));const c="terminal",h=t.Fetcher.for();function l(s){return new Promise((e=>setTimeout(e,s)))}h.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=h.path("/sessions").method("post").create(),p=h.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,saveFSChanges:this.opts.saveFSChanges});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 l(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 l(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}codeSnippet(){return this._codeSnippet}terminal(){return this._terminal}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{h(t.next(s))}catch(s){n(s)}}function c(s){try{h(t.throw(s))}catch(s){n(s)}}function h(s){var e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,c)}h((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",h=t.Fetcher.for();function l(s){return new Promise((e=>setTimeout(e,s)))}h.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=h.path("/sessions").method("post").create(),p=h.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,saveFSChanges:this.opts.saveFSChanges});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 l(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 l(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}codeSnippet(){return this._codeSnippet}terminal(){return this._terminal}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})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@devbookhq/sdk", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Devbook allows visitors of your docs to interact with and execute any code snippet or shell command in a private VM", | ||
@@ -5,0 +5,0 @@ "homepage": "https://usedevbook.com", |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
99711
496
0