@devbookhq/sdk
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var s=require("rpc-websocket-client");require("cross-fetch/polyfill");var e=require("openapi-typescript-fetch"); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var s=require("rpc-websocket-client");require("cross-fetch/polyfill");var i=require("openapi-typescript-fetch"); | ||
/*! ***************************************************************************** | ||
@@ -15,3 +15,3 @@ Copyright (c) Microsoft Corporation. | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function i(s,e,i,t){return new(i||(i=Promise))((function(o,r){function n(s){try{h(t.next(s))}catch(s){r(s)}}function c(s){try{h(t.throw(s))}catch(s){r(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(n,c)}h((t=t.apply(s,e||[])).next())}))}const t=e.Fetcher.for();function o(s){return new Promise((e=>setTimeout(e,s)))}t.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 n=t.path("/sessions").method("post").create(),c=t.path("/sessions/{sessionID}/refresh").method("put").create();exports.Session=class{constructor(e,i,t){this.codeSnippetID=e,this.handlers=i,this.rpc=new s.RpcWebSocketClient,this.subscribers=[],this._isActive=!1,this.logger=new r("Session",t),this.logger.log(`Session for code snippet "${e}" initialized`)}get isActive(){return this._isActive}set isActive(s){this._isActive=s}unsubscribe(s,e){return i(this,void 0,void 0,(function*(){const i=this.subscribers.find((i=>i.handler===e&&i.event===s));i&&(yield this.rpc.call("codeSnippet_unsubscribe",[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((i=>i.handler!==e||i.event!==s)),this.logger.log(`Unsubscribed from event "${s}"`))}))}subscribe(s,e){return i(this,void 0,void 0,(function*(){const i=yield this.rpc.call("codeSnippet_subscribe",[s]);if("string"!=typeof i)throw new Error(`Cannot subscribe to event ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(i)}`);this.subscribers.push({id:i,event:s,handler:e}),this.logger.log(`Subscribed to event "${s}" with id "${i}"`)}))}run(s){return i(this,void 0,void 0,(function*(){yield this.rpc.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)}))}stop(){return i(this,void 0,void 0,(function*(){yield this.rpc.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))}disconnect(){var s,e,i;this.isActive&&(this.isActive=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(i=null===(e=this.handlers)||void 0===e?void 0:e.onClose)||void 0===i||i.call(e),this.logger.log("Disconected from the session"))}connect(){var s,e,t;return i(this,void 0,void 0,(function*(){if(this.isActive||this.session)throw new Error("Session.connect was already called");this.isActive=!0;try{const s=yield n({codeSnippetID:this.codeSnippetID});this.session=s.data,this.logger.log("Aquired session:",this.session)}catch(s){if(s instanceof n.Error){const e=s.getActualType();if(400===e.status)throw new Error(`Error creating session - bad request: ${e}`);if(500===e.status)throw new Error(`Error creating session - server error: ${e}`);throw s}}if(!this.session)return;this.refresh(this.session.sessionID);const r=`wss://${function(s,e){const i=`${s.sessionID}-${s.clientID}.ondevbook.com`;return e?`${e}-${i}`:i}(this.session,8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(r),this.logger.log("Connected to session:",this.session),this.rpc.onClose((s=>i(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,s),this.isActive){yield o(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(r),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)})),yield Promise.all([(null===(s=this.handlers)||void 0===s?void 0:s.onStateChange)?this.subscribe("state",this.handlers.onStateChange):Promise.resolve(),(null===(e=this.handlers)||void 0===e?void 0:e.onStderr)?this.subscribe("stderr",this.handlers.onStderr):Promise.resolve(),(null===(t=this.handlers)||void 0===t?void 0:t.onStdout)?this.subscribe("stdout",this.handlers.onStdout):Promise.resolve()]),this.rpc.onNotification.push(this.handleNotification.bind(this)),this.logger.log("Connected handlers for session:",this.session)}))}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.isActive)return void this.logger.log("Cannot refresh session - it was closed");yield o(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s})}catch(e){if(e instanceof c.Error){const i=e.getActualType();if(404===i.status)return void this.logger.error(`Error refreshing session ${i}`);this.logger.error(`Refreshing session "${s}" failed: ${i.status}`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.disconnect()}}))}}; | ||
***************************************************************************** */function e(s,i,e,t){return new(e||(e=Promise))((function(o,r){function n(s){try{h(t.next(s))}catch(s){r(s)}}function c(s){try{h(t.throw(s))}catch(s){r(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(n,c)}h((t=t.apply(s,i||[])).next())}))}const t=i.Fetcher.for();function o(s){return new Promise((i=>setTimeout(i,s)))}t.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 n=t.path("/sessions").method("post").create(),c=t.path("/sessions/{sessionID}/refresh").method("put").create();exports.Session=class{constructor(i,e,t){this.codeSnippetID=i,this.handlers=e,this.rpc=new s.RpcWebSocketClient,this.subscribers=[],this._isActive=!1,this.logger=new r("Session",t),this.logger.log(`Session for code snippet "${i}" initialized`)}get isActive(){return this._isActive}set isActive(s){this._isActive=s}unsubscribe(s,i){return e(this,void 0,void 0,(function*(){const e=this.subscribers.find((e=>e.handler===i&&e.event===s));e&&(yield this.rpc.call("codeSnippet_unsubscribe",[null==e?void 0:e.id]),this.subscribers=this.subscribers.filter((e=>e.handler!==i||e.event!==s)),this.logger.log(`Unsubscribed from event "${s}"`))}))}subscribe(s,i){return e(this,void 0,void 0,(function*(){const e=yield this.rpc.call("codeSnippet_subscribe",[s]);if("string"!=typeof e)throw new Error(`Cannot subscribe to event ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(e)}`);this.subscribers.push({id:e,event:s,handler:i}),this.logger.log(`Subscribed to event "${s}" with id "${e}"`)}))}run(s){return e(this,void 0,void 0,(function*(){if(!this.isActive||!this.session)throw new Error("Session is not active");yield this.rpc.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)}))}stop(){return e(this,void 0,void 0,(function*(){if(!this.isActive||!this.session)throw new Error("Session is not active");yield this.rpc.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))}disconnect(){var s,i,e;this.isActive&&(this.isActive=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(e=null===(i=this.handlers)||void 0===i?void 0:i.onClose)||void 0===e||e.call(i),this.logger.log("Disconected from the session"))}connect(){var s,i,t;return e(this,void 0,void 0,(function*(){if(this.isActive||this.session)throw new Error("Session connect was already called");this.isActive=!0;try{const s=yield n({codeSnippetID:this.codeSnippetID});this.session=s.data,this.logger.log("Aquired session:",this.session)}catch(s){if(s instanceof n.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - bad request: ${i}`);if(500===i.status)throw new Error(`Error creating session - server error: ${i}`);throw s}}if(!this.session)return;this.refresh(this.session.sessionID);const r=`wss://${function(s,i){const e=`${s.sessionID}-${s.clientID}.ondevbook.com`;return i?`${i}-${e}`:e}(this.session,8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(r),this.logger.log("Connected to session:",this.session),this.rpc.onClose((s=>e(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,s),this.isActive){yield o(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(r),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)})),yield Promise.all([(null===(s=this.handlers)||void 0===s?void 0:s.onStateChange)?this.subscribe("state",this.handlers.onStateChange):Promise.resolve(),(null===(i=this.handlers)||void 0===i?void 0:i.onStderr)?this.subscribe("stderr",this.handlers.onStderr):Promise.resolve(),(null===(t=this.handlers)||void 0===t?void 0:t.onStdout)?this.subscribe("stdout",this.handlers.onStdout):Promise.resolve()]),this.rpc.onNotification.push(this.handleNotification.bind(this)),this.logger.log("Connected handlers for session:",this.session)}))}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.isActive)return void this.logger.log("Cannot refresh session - it was closed");yield o(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s})}catch(i){if(i instanceof c.Error){const e=i.getActualType();if(404===e.status)return void this.logger.error(`Error refreshing session ${e}`);this.logger.error(`Refreshing session "${s}" failed: ${e.status}`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.disconnect()}}))}}; | ||
//# sourceMappingURL=index.js.map |
@@ -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,r){function n(s){try{h(t.next(s))}catch(s){r(s)}}function c(s){try{h(t.throw(s))}catch(s){r(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(n,c)}h((t=t.apply(s,i||[])).next())}))}const t=i.for();function o(s){return new Promise((i=>setTimeout(i,s)))}t.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 n=t.path("/sessions").method("post").create(),c=t.path("/sessions/{sessionID}/refresh").method("put").create();class h{constructor(i,e,t){this.codeSnippetID=i,this.handlers=e,this.rpc=new s,this.subscribers=[],this._isActive=!1,this.logger=new r("Session",t),this.logger.log(`Session for code snippet "${i}" initialized`)}get isActive(){return this._isActive}set isActive(s){this._isActive=s}unsubscribe(s,i){return e(this,void 0,void 0,(function*(){const e=this.subscribers.find((e=>e.handler===i&&e.event===s));e&&(yield this.rpc.call("codeSnippet_unsubscribe",[null==e?void 0:e.id]),this.subscribers=this.subscribers.filter((e=>e.handler!==i||e.event!==s)),this.logger.log(`Unsubscribed from event "${s}"`))}))}subscribe(s,i){return e(this,void 0,void 0,(function*(){const e=yield this.rpc.call("codeSnippet_subscribe",[s]);if("string"!=typeof e)throw new Error(`Cannot subscribe to event ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(e)}`);this.subscribers.push({id:e,event:s,handler:i}),this.logger.log(`Subscribed to event "${s}" with id "${e}"`)}))}run(s){return e(this,void 0,void 0,(function*(){yield this.rpc.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)}))}stop(){return e(this,void 0,void 0,(function*(){yield this.rpc.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))}disconnect(){var s,i,e;this.isActive&&(this.isActive=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(e=null===(i=this.handlers)||void 0===i?void 0:i.onClose)||void 0===e||e.call(i),this.logger.log("Disconected from the session"))}connect(){var s,i,t;return e(this,void 0,void 0,(function*(){if(this.isActive||this.session)throw new Error("Session.connect was already called");this.isActive=!0;try{const s=yield n({codeSnippetID:this.codeSnippetID});this.session=s.data,this.logger.log("Aquired session:",this.session)}catch(s){if(s instanceof n.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - bad request: ${i}`);if(500===i.status)throw new Error(`Error creating session - server error: ${i}`);throw s}}if(!this.session)return;this.refresh(this.session.sessionID);const r=`wss://${function(s,i){const e=`${s.sessionID}-${s.clientID}.ondevbook.com`;return i?`${i}-${e}`:e}(this.session,8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(r),this.logger.log("Connected to session:",this.session),this.rpc.onClose((s=>e(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,s),this.isActive){yield o(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(r),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)})),yield Promise.all([(null===(s=this.handlers)||void 0===s?void 0:s.onStateChange)?this.subscribe("state",this.handlers.onStateChange):Promise.resolve(),(null===(i=this.handlers)||void 0===i?void 0:i.onStderr)?this.subscribe("stderr",this.handlers.onStderr):Promise.resolve(),(null===(t=this.handlers)||void 0===t?void 0:t.onStdout)?this.subscribe("stdout",this.handlers.onStdout):Promise.resolve()]),this.rpc.onNotification.push(this.handleNotification.bind(this)),this.logger.log("Connected handlers for session:",this.session)}))}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.isActive)return void this.logger.log("Cannot refresh session - it was closed");yield o(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s})}catch(i){if(i instanceof c.Error){const e=i.getActualType();if(404===e.status)return void this.logger.error(`Error refreshing session ${e}`);this.logger.error(`Refreshing session "${s}" failed: ${e.status}`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.disconnect()}}))}}export{h as Session}; | ||
***************************************************************************** */function e(s,i,e,t){return new(e||(e=Promise))((function(o,r){function n(s){try{h(t.next(s))}catch(s){r(s)}}function c(s){try{h(t.throw(s))}catch(s){r(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(n,c)}h((t=t.apply(s,i||[])).next())}))}const t=i.for();function o(s){return new Promise((i=>setTimeout(i,s)))}t.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 n=t.path("/sessions").method("post").create(),c=t.path("/sessions/{sessionID}/refresh").method("put").create();class h{constructor(i,e,t){this.codeSnippetID=i,this.handlers=e,this.rpc=new s,this.subscribers=[],this._isActive=!1,this.logger=new r("Session",t),this.logger.log(`Session for code snippet "${i}" initialized`)}get isActive(){return this._isActive}set isActive(s){this._isActive=s}unsubscribe(s,i){return e(this,void 0,void 0,(function*(){const e=this.subscribers.find((e=>e.handler===i&&e.event===s));e&&(yield this.rpc.call("codeSnippet_unsubscribe",[null==e?void 0:e.id]),this.subscribers=this.subscribers.filter((e=>e.handler!==i||e.event!==s)),this.logger.log(`Unsubscribed from event "${s}"`))}))}subscribe(s,i){return e(this,void 0,void 0,(function*(){const e=yield this.rpc.call("codeSnippet_subscribe",[s]);if("string"!=typeof e)throw new Error(`Cannot subscribe to event ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(e)}`);this.subscribers.push({id:e,event:s,handler:i}),this.logger.log(`Subscribed to event "${s}" with id "${e}"`)}))}run(s){return e(this,void 0,void 0,(function*(){if(!this.isActive||!this.session)throw new Error("Session is not active");yield this.rpc.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)}))}stop(){return e(this,void 0,void 0,(function*(){if(!this.isActive||!this.session)throw new Error("Session is not active");yield this.rpc.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))}disconnect(){var s,i,e;this.isActive&&(this.isActive=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(e=null===(i=this.handlers)||void 0===i?void 0:i.onClose)||void 0===e||e.call(i),this.logger.log("Disconected from the session"))}connect(){var s,i,t;return e(this,void 0,void 0,(function*(){if(this.isActive||this.session)throw new Error("Session connect was already called");this.isActive=!0;try{const s=yield n({codeSnippetID:this.codeSnippetID});this.session=s.data,this.logger.log("Aquired session:",this.session)}catch(s){if(s instanceof n.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - bad request: ${i}`);if(500===i.status)throw new Error(`Error creating session - server error: ${i}`);throw s}}if(!this.session)return;this.refresh(this.session.sessionID);const r=`wss://${function(s,i){const e=`${s.sessionID}-${s.clientID}.ondevbook.com`;return i?`${i}-${e}`:e}(this.session,8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(r),this.logger.log("Connected to session:",this.session),this.rpc.onClose((s=>e(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,s),this.isActive){yield o(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(r),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)})),yield Promise.all([(null===(s=this.handlers)||void 0===s?void 0:s.onStateChange)?this.subscribe("state",this.handlers.onStateChange):Promise.resolve(),(null===(i=this.handlers)||void 0===i?void 0:i.onStderr)?this.subscribe("stderr",this.handlers.onStderr):Promise.resolve(),(null===(t=this.handlers)||void 0===t?void 0:t.onStdout)?this.subscribe("stdout",this.handlers.onStdout):Promise.resolve()]),this.rpc.onNotification.push(this.handleNotification.bind(this)),this.logger.log("Connected handlers for session:",this.session)}))}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.isActive)return void this.logger.log("Cannot refresh session - it was closed");yield o(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s})}catch(i){if(i instanceof c.Error){const e=i.getActualType();if(404===e.status)return void this.logger.error(`Error refreshing session ${e}`);this.logger.error(`Refreshing session "${s}" failed: ${e.status}`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.disconnect()}}))}}export{h as Session}; | ||
//# sourceMappingURL=index.js.map |
@@ -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=t.Fetcher.for();function r(s){return new Promise((e=>setTimeout(e,s)))}n.configure({baseUrl:"https://ondevbook.com"});class c{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 l=n.path("/sessions").method("post").create(),h=n.path("/sessions/{sessionID}/refresh").method("put").create();s.Session=class{constructor(s,i,t){this.codeSnippetID=s,this.handlers=i,this.rpc=new e.RpcWebSocketClient,this.subscribers=[],this._isActive=!1,this.logger=new c("Session",t),this.logger.log(`Session for code snippet "${s}" initialized`)}get isActive(){return this._isActive}set isActive(s){this._isActive=s}unsubscribe(s,e){return o(this,void 0,void 0,(function*(){const i=this.subscribers.find((i=>i.handler===e&&i.event===s));i&&(yield this.rpc.call("codeSnippet_unsubscribe",[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((i=>i.handler!==e||i.event!==s)),this.logger.log(`Unsubscribed from event "${s}"`))}))}subscribe(s,e){return o(this,void 0,void 0,(function*(){const i=yield this.rpc.call("codeSnippet_subscribe",[s]);if("string"!=typeof i)throw new Error(`Cannot subscribe to event ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(i)}`);this.subscribers.push({id:i,event:s,handler:e}),this.logger.log(`Subscribed to event "${s}" with id "${i}"`)}))}run(s){return o(this,void 0,void 0,(function*(){yield this.rpc.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)}))}stop(){return o(this,void 0,void 0,(function*(){yield this.rpc.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))}disconnect(){var s,e,i;this.isActive&&(this.isActive=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(i=null===(e=this.handlers)||void 0===e?void 0:e.onClose)||void 0===i||i.call(e),this.logger.log("Disconected from the session"))}connect(){var s,e,i;return o(this,void 0,void 0,(function*(){if(this.isActive||this.session)throw new Error("Session.connect was already called");this.isActive=!0;try{const s=yield l({codeSnippetID:this.codeSnippetID});this.session=s.data,this.logger.log("Aquired session:",this.session)}catch(s){if(s instanceof l.Error){const e=s.getActualType();if(400===e.status)throw new Error(`Error creating session - bad request: ${e}`);if(500===e.status)throw new Error(`Error creating session - server error: ${e}`);throw s}}if(!this.session)return;this.refresh(this.session.sessionID);const t=`wss://${function(s,e){const i=`${s.sessionID}-${s.clientID}.ondevbook.com`;return e?`${e}-${i}`:i}(this.session,8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(t),this.logger.log("Connected to session:",this.session),this.rpc.onClose((s=>o(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,s),this.isActive){yield r(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(t),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)})),yield Promise.all([(null===(s=this.handlers)||void 0===s?void 0:s.onStateChange)?this.subscribe("state",this.handlers.onStateChange):Promise.resolve(),(null===(e=this.handlers)||void 0===e?void 0:e.onStderr)?this.subscribe("stderr",this.handlers.onStderr):Promise.resolve(),(null===(i=this.handlers)||void 0===i?void 0:i.onStdout)?this.subscribe("stdout",this.handlers.onStdout):Promise.resolve()]),this.rpc.onNotification.push(this.handleNotification.bind(this)),this.logger.log("Connected handlers for session:",this.session)}))}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.isActive)return void this.logger.log("Cannot refresh session - it was closed");yield r(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}`);this.logger.error(`Refreshing session "${s}" failed: ${i.status}`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.disconnect()}}))}},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=t.Fetcher.for();function r(s){return new Promise((e=>setTimeout(e,s)))}n.configure({baseUrl:"https://ondevbook.com"});class c{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 h=n.path("/sessions").method("post").create(),l=n.path("/sessions/{sessionID}/refresh").method("put").create();s.Session=class{constructor(s,i,t){this.codeSnippetID=s,this.handlers=i,this.rpc=new e.RpcWebSocketClient,this.subscribers=[],this._isActive=!1,this.logger=new c("Session",t),this.logger.log(`Session for code snippet "${s}" initialized`)}get isActive(){return this._isActive}set isActive(s){this._isActive=s}unsubscribe(s,e){return o(this,void 0,void 0,(function*(){const i=this.subscribers.find((i=>i.handler===e&&i.event===s));i&&(yield this.rpc.call("codeSnippet_unsubscribe",[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((i=>i.handler!==e||i.event!==s)),this.logger.log(`Unsubscribed from event "${s}"`))}))}subscribe(s,e){return o(this,void 0,void 0,(function*(){const i=yield this.rpc.call("codeSnippet_subscribe",[s]);if("string"!=typeof i)throw new Error(`Cannot subscribe to event ${s}. Ecpected response to be a subscription ID, instead got ${JSON.stringify(i)}`);this.subscribers.push({id:i,event:s,handler:e}),this.logger.log(`Subscribed to event "${s}" with id "${i}"`)}))}run(s){return o(this,void 0,void 0,(function*(){if(!this.isActive||!this.session)throw new Error("Session is not active");yield this.rpc.call("codeSnippet_run",[s]),this.logger.log("Started running code",s)}))}stop(){return o(this,void 0,void 0,(function*(){if(!this.isActive||!this.session)throw new Error("Session is not active");yield this.rpc.call("codeSnippet_stop"),this.logger.log("Stopped running code")}))}disconnect(){var s,e,i;this.isActive&&(this.isActive=!1,null===(s=this.rpc.ws)||void 0===s||s.close(),null===(i=null===(e=this.handlers)||void 0===e?void 0:e.onClose)||void 0===i||i.call(e),this.logger.log("Disconected from the session"))}connect(){var s,e,i;return o(this,void 0,void 0,(function*(){if(this.isActive||this.session)throw new Error("Session connect was already called");this.isActive=!0;try{const s=yield h({codeSnippetID:this.codeSnippetID});this.session=s.data,this.logger.log("Aquired session:",this.session)}catch(s){if(s instanceof h.Error){const e=s.getActualType();if(400===e.status)throw new Error(`Error creating session - bad request: ${e}`);if(500===e.status)throw new Error(`Error creating session - server error: ${e}`);throw s}}if(!this.session)return;this.refresh(this.session.sessionID);const t=`wss://${function(s,e){const i=`${s.sessionID}-${s.clientID}.ondevbook.com`;return e?`${e}-${i}`:i}(this.session,8010)}/ws`;this.logger.log("Connection to session:",this.session),yield this.rpc.connect(t),this.logger.log("Connected to session:",this.session),this.rpc.onClose((s=>o(this,void 0,void 0,(function*(){if(this.logger.log("Closing WS connection to session:",this.session,s),this.isActive){yield r(4e3),this.logger.log("Reconnecting to session:",this.session);try{yield this.rpc.connect(t),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)})),yield Promise.all([(null===(s=this.handlers)||void 0===s?void 0:s.onStateChange)?this.subscribe("state",this.handlers.onStateChange):Promise.resolve(),(null===(e=this.handlers)||void 0===e?void 0:e.onStderr)?this.subscribe("stderr",this.handlers.onStderr):Promise.resolve(),(null===(i=this.handlers)||void 0===i?void 0:i.onStdout)?this.subscribe("stdout",this.handlers.onStdout):Promise.resolve()]),this.rpc.onNotification.push(this.handleNotification.bind(this)),this.logger.log("Connected handlers for session:",this.session)}))}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.isActive)return void this.logger.log("Cannot refresh session - it was closed");yield r(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield l({sessionID:s})}catch(e){if(e instanceof l.Error){const i=e.getActualType();if(404===i.status)return void this.logger.error(`Error refreshing session ${i}`);this.logger.error(`Refreshing session "${s}" failed: ${i.status}`)}}}}finally{this.logger.log(`Stopped refreshing session "${s}"`),this.disconnect()}}))}},Object.defineProperty(s,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@devbookhq/sdk", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"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
75580
331