New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@devbookhq/sdk

Package Overview
Dependencies
Maintainers
2
Versions
152
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@devbookhq/sdk - npm Package Compare versions

Comparing version 2.3.0 to 2.3.1

2

dist/cjs/constants.d.ts
export declare const SESSION_REFRESH_PERIOD = 4000;
export declare const WS_RECONNECT_INTERVAL = 4000;
export declare const WS_RECONNECT_INTERVAL = 100;
export declare const SESSION_DOMAIN = "ondevbook.com";
export declare const WS_PORT = 8010;
export declare const WS_ROUTE = "/ws";

@@ -15,3 +15,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var s=require("rpc-websocket-client");require("cross-fetch/polyfill");var e=require("openapi-typescript-fetch");

PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function i(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 d(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,d)}l((t=t.apply(s,e||[])).next())}))}var t,o;exports.CodeSnippetExecState=void 0,(t=exports.CodeSnippetExecState||(exports.CodeSnippetExecState={})).Running="Running",t.Stopped="Stopped",t.Loading="Loading",exports.OutType=void 0,(o=exports.OutType||(exports.OutType={})).Stdout="Stdout",o.Stderr="Stderr";const n=e.Fetcher.for();function r(s){return new Promise((e=>setTimeout(e,s)))}n.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(`[${this.id()}]`,...s)}error(...s){console.error(`[${this.id()} ERROR]`,...s)}}const l=n.path("/sessions").method("post").create({api_key:!0}),c=n.path("/sessions/{sessionID}/refresh").method("post").create({api_key:!0});exports.Session=class extends class{constructor(e){this.opts=e,this.isOpen=!1,this.rpc=new s.RpcWebSocketClient,this.subscribers=[],this.logger=new d("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((i=>i.handler===e&&i.method===s));i&&(yield this.call(`${s}_unsubscribe`,[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((s=>s!==i)),this.logger.log(`Unsubscribed from "${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 ${s} with params ${t}. Expected response to be a subscription ID, instead got ${JSON.stringify(i)}`);this.subscribers.push({id:i,handler:e,method:s}),this.logger.log(`Subscribed to "${s}_${t}" 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,t;return i(this,void 0,void 0,(function*(){if(this.isOpen){this.isOpen=!1,this.logger.log("Unsubscribing...");(yield Promise.allSettled(this.subscribers.map((s=>this.unsubscribe(s.method,s.handler))))).forEach((s=>{"rejected"===s.status&&this.logger.log(`Failed to unsubscribe: "${s.reason}"`)})),null===(s=this.rpc.ws)||void 0===s||s.close(),null===(t=null===(e=this.opts)||void 0===e?void 0:e.onClose)||void 0===t||t.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 l({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled,api_key:this.opts.apiKey});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof l.Error){const e=s.getActualType();if(400===e.status)throw new Error(`Error creating session - (${e.status}) bad request: ${e.data.message}`);if(401===e.status)throw new Error(`Error creating session - (${e.status}) unauthenticated (you need to be authenticated to start an session with persistent edits): ${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 r(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 r(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s,api_key:this.opts.apiKey})}catch(e){if(e instanceof c.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,t,o,n,r,d;return i(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>i(this,void 0,void 0,(function*(){var e,i;if(!this.isOpen||!this.session)throw new Error("Session is not active");const t=yield this.call("codeSnippet_run",[s]);return null===(i=null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStateChange)||void 0===i||i.call(e,t),this.logger.log("Started running code",s),t})),stop:()=>i(this,void 0,void 0,(function*(){var s,e;if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=yield this.call("codeSnippet_stop");return null===(e=null===(s=this.codeSnippetOpts)||void 0===s?void 0:s.onStateChange)||void 0===e||e.call(s,i),this.logger.log("Stopped running code"),i})),listDeps:()=>i(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started listing deps");const s=yield this.call("codeSnippet_deps");return this.logger.log("Stopped listing deps",s),s})),installDep:s=>i(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started installing dependency",s);const e=yield this.call("codeSnippet_installDep",[s]);return this.logger.log("Stopped installing dependency",e),e})),uninstallDep:s=>i(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started uninstalling dependency",s);const e=yield this.call("codeSnippet_uninstallDep",[s]);return this.logger.log("Stopped uninstalling dependency",e),e}))},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(),(null===(n=this.codeSnippetOpts)||void 0===n?void 0:n.onDepsStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStdout,"depsStdout"):Promise.resolve(),(null===(r=this.codeSnippetOpts)||void 0===r?void 0:r.onDepsStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStderr,"depsStderr"):Promise.resolve(),(null===(d=this.codeSnippetOpts)||void 0===d?void 0:d.onDepsChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsChange,"depsChange"):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])}))}}))}}))}},exports.api=n;
***************************************************************************** */function i(s,e,i,t){return new(i||(i=Promise))((function(o,n){function r(s){try{d(t.next(s))}catch(s){n(s)}}function l(s){try{d(t.throw(s))}catch(s){n(s)}}function d(s){var e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,l)}d((t=t.apply(s,e||[])).next())}))}var t,o;exports.CodeSnippetExecState=void 0,(t=exports.CodeSnippetExecState||(exports.CodeSnippetExecState={})).Running="Running",t.Stopped="Stopped",t.Loading="Loading",exports.OutType=void 0,(o=exports.OutType||(exports.OutType={})).Stdout="Stdout",o.Stderr="Stderr";const n=e.Fetcher.for();function r(s){return new Promise((e=>setTimeout(e,s)))}n.configure({baseUrl:"https://ondevbook.com"});class l{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(`[${this.id()}]`,...s)}error(...s){console.error(`[${this.id()} ERROR]`,...s)}}const d=n.path("/sessions").method("post").create({api_key:!0}),c=n.path("/sessions/{sessionID}/refresh").method("post").create({api_key:!0});exports.Session=class extends class{constructor(e){this.opts=e,this.isOpen=!1,this.rpc=new s.RpcWebSocketClient,this.subscribers=[],this.logger=new l("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((i=>i.handler===e&&i.method===s));i&&(yield this.call(`${s}_unsubscribe`,[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((s=>s!==i)),this.logger.log(`Unsubscribed from "${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 ${s} with params ${t}. Expected response to be a subscription ID, instead got ${JSON.stringify(i)}`);this.subscribers.push({id:i,handler:e,method:s}),this.logger.log(`Subscribed to "${s}_${t}" 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,t;return i(this,void 0,void 0,(function*(){if(this.isOpen){this.isOpen=!1,this.logger.log("Unsubscribing...");(yield Promise.allSettled(this.subscribers.map((s=>this.unsubscribe(s.method,s.handler))))).forEach((s=>{"rejected"===s.status&&this.logger.log(`Failed to unsubscribe: "${s.reason}"`)})),null===(s=this.rpc.ws)||void 0===s||s.close(),null===(t=null===(e=this.opts)||void 0===e?void 0:e.onClose)||void 0===t||t.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 d({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled,api_key:this.opts.apiKey});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof d.Error){const e=s.getActualType();if(400===e.status)throw new Error(`Error creating session - (${e.status}) bad request: ${e.data.message}`);if(401===e.status)throw new Error(`Error creating session - (${e.status}) unauthenticated (you need to be authenticated to start an session with persistent edits): ${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.rpc.onError((s=>{this.logger.log("Error in WS session:",this.session,s)}));let e,t,o=!1;const n=new Promise(((s,i)=>{e=()=>{o||(o=!0,s())},t=()=>{o||(o=!0,i())}}));this.rpc.onOpen((()=>{null==e||e()})),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 r(100),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.log("Failed reconnecting to session:",this.session)}}else null==t||t()})))),this.rpc.onNotification.push(this.handleNotification.bind(this));try{this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s)}catch(s){this.logger.log("Error connecting to session",s)}yield n,this.logger.log("Connected to 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.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield r(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s,api_key:this.opts.apiKey})}catch(e){if(e instanceof c.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,t,o,n,r,l,d;return i(this,void 0,void 0,(function*(){yield s.open.call(this),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(),(null===(n=this.codeSnippetOpts)||void 0===n?void 0:n.onDepsStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStdout,"depsStdout"):Promise.resolve(),(null===(r=this.codeSnippetOpts)||void 0===r?void 0:r.onDepsStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStderr,"depsStderr"):Promise.resolve(),(null===(l=this.codeSnippetOpts)||void 0===l?void 0:l.onDepsChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsChange,"depsChange"):Promise.resolve(),(null===(d=this.codeSnippetOpts)||void 0===d?void 0:d.onScanPorts)?this.subscribe("codeSnippet",this.codeSnippetOpts.onScanPorts,"scanOpenedPorts"):Promise.resolve()]),this.codeSnippet={run:s=>i(this,void 0,void 0,(function*(){var e,i;if(!this.isOpen||!this.session)throw new Error("Session is not active");const t=yield this.call("codeSnippet_run",[s]);return null===(i=null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStateChange)||void 0===i||i.call(e,t),this.logger.log("Started running code",s),t})),stop:()=>i(this,void 0,void 0,(function*(){var s,e;if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=yield this.call("codeSnippet_stop");return null===(e=null===(s=this.codeSnippetOpts)||void 0===s?void 0:s.onStateChange)||void 0===e||e.call(s,i),this.logger.log("Stopped running code"),i})),listDeps:()=>i(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started listing deps");const s=yield this.call("codeSnippet_deps");return this.logger.log("Stopped listing deps",s),s})),installDep:s=>i(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started installing dependency",s);const e=yield this.call("codeSnippet_installDep",[s]);return this.logger.log("Stopped installing dependency",e),e})),uninstallDep:s=>i(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started uninstalling dependency",s);const e=yield this.call("codeSnippet_uninstallDep",[s]);return this.logger.log("Stopped uninstalling dependency",e),e}))},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])}))}}))}}))}},exports.api=n;
//# sourceMappingURL=index.js.map

@@ -34,2 +34,7 @@ export declare const codeSnippetMethod = "codeSnippet";

}
export interface OpenedPort {
State: string;
Ip: string;
Port: number;
}
export declare type CodeSnippetStateHandler = (state: CodeSnippetExecState) => void;

@@ -41,3 +46,4 @@ export declare type CodeSnippetStderrHandler = (o: OutStderrResponse) => void;

export declare type DepsChangeHandler = (deps: string[]) => void;
export declare type CodeSnippetSubscriptionHandler = CodeSnippetStateHandler | CodeSnippetStderrHandler | CodeSnippetStdoutHandler | DepsStderrHandler | DepsStderrHandler | DepsChangeHandler;
export declare type ScanOpenedPortsHandler = (ports: OpenedPort[]) => void;
export declare type CodeSnippetSubscriptionHandler = CodeSnippetStateHandler | CodeSnippetStderrHandler | CodeSnippetStdoutHandler | DepsStderrHandler | DepsStderrHandler | DepsChangeHandler | ScanOpenedPortsHandler;
export declare type CodeSnippetSubscriptionHandlerType = {

@@ -50,2 +56,3 @@ 'state': CodeSnippetStateHandler;

'depsChange': DepsChangeHandler;
'scanOpenedPorts': ScanOpenedPortsHandler;
};

@@ -52,0 +59,0 @@ export interface CodeSnippetManager {

@@ -1,2 +0,2 @@

import { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, DepsStdoutHandler, DepsStderrHandler, DepsChangeHandler } from './codeSnippet';
import { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, DepsStdoutHandler, DepsStderrHandler, DepsChangeHandler, ScanOpenedPortsHandler } from './codeSnippet';
import { TerminalManager } from './terminal';

@@ -11,2 +11,3 @@ import SessionConnection, { SessionConnectionOpts } from './sessionConnection';

onDepsChange?: DepsChangeHandler;
onScanPorts?: ScanOpenedPortsHandler;
}

@@ -13,0 +14,0 @@ export interface SessionOpts extends SessionConnectionOpts {

export declare const SESSION_REFRESH_PERIOD = 4000;
export declare const WS_RECONNECT_INTERVAL = 4000;
export declare const WS_RECONNECT_INTERVAL = 100;
export declare const SESSION_DOMAIN = "ondevbook.com";
export declare const WS_PORT = 8010;
export declare const WS_ROUTE = "/ws";

@@ -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{d(e.next(s))}catch(s){n(s)}}function l(s){try{d(e.throw(s))}catch(s){n(s)}}function d(s){var i;s.done?o(s.value):(i=s.value,i instanceof t?i:new t((function(s){s(i)}))).then(r,l)}d((e=e.apply(s,i||[])).next())}))}var e,o;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(e||(e={})),function(s){s.Stdout="Stdout",s.Stderr="Stderr"}(o||(o={}));const n=i.for();function r(s){return new Promise((i=>setTimeout(i,s)))}n.configure({baseUrl:"https://ondevbook.com"});class l{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(`[${this.id()}]`,...s)}error(...s){console.error(`[${this.id()} ERROR]`,...s)}}const d=n.path("/sessions").method("post").create({api_key:!0}),c=n.path("/sessions/{sessionID}/refresh").method("post").create({api_key:!0});class h extends class{constructor(i){this.opts=i,this.isOpen=!1,this.rpc=new s,this.subscribers=[],this.logger=new l("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((t=>t.handler===i&&t.method===s));t&&(yield this.call(`${s}_unsubscribe`,[null==t?void 0:t.id]),this.subscribers=this.subscribers.filter((s=>s!==t)),this.logger.log(`Unsubscribed from "${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 ${s} with params ${e}. Expected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:i,method:s}),this.logger.log(`Subscribed to "${s}_${e}" 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,e;return t(this,void 0,void 0,(function*(){if(this.isOpen){this.isOpen=!1,this.logger.log("Unsubscribing...");(yield Promise.allSettled(this.subscribers.map((s=>this.unsubscribe(s.method,s.handler))))).forEach((s=>{"rejected"===s.status&&this.logger.log(`Failed to unsubscribe: "${s.reason}"`)})),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 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 d({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled,api_key:this.opts.apiKey});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof d.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - (${i.status}) bad request: ${i.data.message}`);if(401===i.status)throw new Error(`Error creating session - (${i.status}) unauthenticated (you need to be authenticated to start an session with persistent edits): ${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 r(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 r(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s,api_key:this.opts.apiKey})}catch(i){if(i instanceof c.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,n,r,l;return t(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>t(this,void 0,void 0,(function*(){var i,t;if(!this.isOpen||!this.session)throw new Error("Session is not active");const e=yield this.call("codeSnippet_run",[s]);return null===(t=null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStateChange)||void 0===t||t.call(i,e),this.logger.log("Started running code",s),e})),stop:()=>t(this,void 0,void 0,(function*(){var s,i;if(!this.isOpen||!this.session)throw new Error("Session is not active");const t=yield this.call("codeSnippet_stop");return null===(i=null===(s=this.codeSnippetOpts)||void 0===s?void 0:s.onStateChange)||void 0===i||i.call(s,t),this.logger.log("Stopped running code"),t})),listDeps:()=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started listing deps");const s=yield this.call("codeSnippet_deps");return this.logger.log("Stopped listing deps",s),s})),installDep:s=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started installing dependency",s);const i=yield this.call("codeSnippet_installDep",[s]);return this.logger.log("Stopped installing dependency",i),i})),uninstallDep:s=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started uninstalling dependency",s);const i=yield this.call("codeSnippet_uninstallDep",[s]);return this.logger.log("Stopped uninstalling dependency",i),i}))},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(),(null===(n=this.codeSnippetOpts)||void 0===n?void 0:n.onDepsStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStdout,"depsStdout"):Promise.resolve(),(null===(r=this.codeSnippetOpts)||void 0===r?void 0:r.onDepsStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStderr,"depsStderr"):Promise.resolve(),(null===(l=this.codeSnippetOpts)||void 0===l?void 0:l.onDepsChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsChange,"depsChange"):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,o as OutType,h as Session,n as api};
***************************************************************************** */function t(s,i,t,e){return new(t||(t=Promise))((function(o,n){function r(s){try{d(e.next(s))}catch(s){n(s)}}function l(s){try{d(e.throw(s))}catch(s){n(s)}}function d(s){var i;s.done?o(s.value):(i=s.value,i instanceof t?i:new t((function(s){s(i)}))).then(r,l)}d((e=e.apply(s,i||[])).next())}))}var e,o;!function(s){s.Running="Running",s.Stopped="Stopped",s.Loading="Loading"}(e||(e={})),function(s){s.Stdout="Stdout",s.Stderr="Stderr"}(o||(o={}));const n=i.for();function r(s){return new Promise((i=>setTimeout(i,s)))}n.configure({baseUrl:"https://ondevbook.com"});class l{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(`[${this.id()}]`,...s)}error(...s){console.error(`[${this.id()} ERROR]`,...s)}}const d=n.path("/sessions").method("post").create({api_key:!0}),c=n.path("/sessions/{sessionID}/refresh").method("post").create({api_key:!0});class h extends class{constructor(i){this.opts=i,this.isOpen=!1,this.rpc=new s,this.subscribers=[],this.logger=new l("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((t=>t.handler===i&&t.method===s));t&&(yield this.call(`${s}_unsubscribe`,[null==t?void 0:t.id]),this.subscribers=this.subscribers.filter((s=>s!==t)),this.logger.log(`Unsubscribed from "${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 ${s} with params ${e}. Expected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:i,method:s}),this.logger.log(`Subscribed to "${s}_${e}" 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,e;return t(this,void 0,void 0,(function*(){if(this.isOpen){this.isOpen=!1,this.logger.log("Unsubscribing...");(yield Promise.allSettled(this.subscribers.map((s=>this.unsubscribe(s.method,s.handler))))).forEach((s=>{"rejected"===s.status&&this.logger.log(`Failed to unsubscribe: "${s.reason}"`)})),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 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 d({codeSnippetID:this.opts.id,editEnabled:this.opts.editEnabled,api_key:this.opts.apiKey});this.session=s.data,this.logger.log("Aquired session:",this.session),this.refresh(this.session.sessionID)}catch(s){if(s instanceof d.Error){const i=s.getActualType();if(400===i.status)throw new Error(`Error creating session - (${i.status}) bad request: ${i.data.message}`);if(401===i.status)throw new Error(`Error creating session - (${i.status}) unauthenticated (you need to be authenticated to start an session with persistent edits): ${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.rpc.onError((s=>{this.logger.log("Error in WS session:",this.session,s)}));let i,e,o=!1;const n=new Promise(((s,t)=>{i=()=>{o||(o=!0,s())},e=()=>{o||(o=!0,t())}}));this.rpc.onOpen((()=>{null==i||i()})),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 r(100),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.log("Failed reconnecting to session:",this.session)}}else null==e||e()})))),this.rpc.onNotification.push(this.handleNotification.bind(this));try{this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s)}catch(s){this.logger.log("Error connecting to session",s)}yield n,this.logger.log("Connected to session:",this.session)}))}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 r(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield c({sessionID:s,api_key:this.opts.apiKey})}catch(i){if(i instanceof c.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,n,r,l,d;return t(this,void 0,void 0,(function*(){yield s.open.call(this),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(),(null===(n=this.codeSnippetOpts)||void 0===n?void 0:n.onDepsStdout)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStdout,"depsStdout"):Promise.resolve(),(null===(r=this.codeSnippetOpts)||void 0===r?void 0:r.onDepsStderr)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsStderr,"depsStderr"):Promise.resolve(),(null===(l=this.codeSnippetOpts)||void 0===l?void 0:l.onDepsChange)?this.subscribe("codeSnippet",this.codeSnippetOpts.onDepsChange,"depsChange"):Promise.resolve(),(null===(d=this.codeSnippetOpts)||void 0===d?void 0:d.onScanPorts)?this.subscribe("codeSnippet",this.codeSnippetOpts.onScanPorts,"scanOpenedPorts"):Promise.resolve()]),this.codeSnippet={run:s=>t(this,void 0,void 0,(function*(){var i,t;if(!this.isOpen||!this.session)throw new Error("Session is not active");const e=yield this.call("codeSnippet_run",[s]);return null===(t=null===(i=this.codeSnippetOpts)||void 0===i?void 0:i.onStateChange)||void 0===t||t.call(i,e),this.logger.log("Started running code",s),e})),stop:()=>t(this,void 0,void 0,(function*(){var s,i;if(!this.isOpen||!this.session)throw new Error("Session is not active");const t=yield this.call("codeSnippet_stop");return null===(i=null===(s=this.codeSnippetOpts)||void 0===s?void 0:s.onStateChange)||void 0===i||i.call(s,t),this.logger.log("Stopped running code"),t})),listDeps:()=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started listing deps");const s=yield this.call("codeSnippet_deps");return this.logger.log("Stopped listing deps",s),s})),installDep:s=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started installing dependency",s);const i=yield this.call("codeSnippet_installDep",[s]);return this.logger.log("Stopped installing dependency",i),i})),uninstallDep:s=>t(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started uninstalling dependency",s);const i=yield this.call("codeSnippet_uninstallDep",[s]);return this.logger.log("Stopped uninstalling dependency",i),i}))},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,o as OutType,h as Session,n as api};
//# sourceMappingURL=index.js.map

@@ -34,2 +34,7 @@ export declare const codeSnippetMethod = "codeSnippet";

}
export interface OpenedPort {
State: string;
Ip: string;
Port: number;
}
export declare type CodeSnippetStateHandler = (state: CodeSnippetExecState) => void;

@@ -41,3 +46,4 @@ export declare type CodeSnippetStderrHandler = (o: OutStderrResponse) => void;

export declare type DepsChangeHandler = (deps: string[]) => void;
export declare type CodeSnippetSubscriptionHandler = CodeSnippetStateHandler | CodeSnippetStderrHandler | CodeSnippetStdoutHandler | DepsStderrHandler | DepsStderrHandler | DepsChangeHandler;
export declare type ScanOpenedPortsHandler = (ports: OpenedPort[]) => void;
export declare type CodeSnippetSubscriptionHandler = CodeSnippetStateHandler | CodeSnippetStderrHandler | CodeSnippetStdoutHandler | DepsStderrHandler | DepsStderrHandler | DepsChangeHandler | ScanOpenedPortsHandler;
export declare type CodeSnippetSubscriptionHandlerType = {

@@ -50,2 +56,3 @@ 'state': CodeSnippetStateHandler;

'depsChange': DepsChangeHandler;
'scanOpenedPorts': ScanOpenedPortsHandler;
};

@@ -52,0 +59,0 @@ export interface CodeSnippetManager {

@@ -1,2 +0,2 @@

import { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, DepsStdoutHandler, DepsStderrHandler, DepsChangeHandler } from './codeSnippet';
import { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, DepsStdoutHandler, DepsStderrHandler, DepsChangeHandler, ScanOpenedPortsHandler } from './codeSnippet';
import { TerminalManager } from './terminal';

@@ -11,2 +11,3 @@ import SessionConnection, { SessionConnectionOpts } from './sessionConnection';

onDepsChange?: DepsChangeHandler;
onScanPorts?: ScanOpenedPortsHandler;
}

@@ -13,0 +14,0 @@ export interface SessionOpts extends SessionConnectionOpts {

export declare const SESSION_REFRESH_PERIOD = 4000;
export declare const WS_RECONNECT_INTERVAL = 4000;
export declare const WS_RECONNECT_INTERVAL = 100;
export declare const SESSION_DOMAIN = "ondevbook.com";
export declare const WS_PORT = 8010;
export declare const WS_ROUTE = "/ws";

@@ -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{d(t.next(s))}catch(s){n(s)}}function l(s){try{d(t.throw(s))}catch(s){n(s)}}function d(s){var e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,l)}d((t=t.apply(s,e||[])).next())}))}const n="codeSnippet";var r,l;s.CodeSnippetExecState=void 0,(r=s.CodeSnippetExecState||(s.CodeSnippetExecState={})).Running="Running",r.Stopped="Stopped",r.Loading="Loading",s.OutType=void 0,(l=s.OutType||(s.OutType={})).Stdout="Stdout",l.Stderr="Stderr";const d="terminal",c=t.Fetcher.for();function h(s){return new Promise((e=>setTimeout(e,s)))}c.configure({baseUrl:"https://ondevbook.com"});class p{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(`[${this.id()}]`,...s)}error(...s){console.error(`[${this.id()} ERROR]`,...s)}}const a=c.path("/sessions").method("post").create({api_key:!0}),u=c.path("/sessions/{sessionID}/refresh").method("post").create({api_key:!0});s.Session=class extends class{constructor(s){this.opts=s,this.isOpen=!1,this.rpc=new e.RpcWebSocketClient,this.subscribers=[],this.logger=new p("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((i=>i.handler===e&&i.method===s));i&&(yield this.call(`${s}_unsubscribe`,[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((s=>s!==i)),this.logger.log(`Unsubscribed from "${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 ${s} with params ${i}. Expected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:e,method:s}),this.logger.log(`Subscribed to "${s}_${i}" 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;return o(this,void 0,void 0,(function*(){if(this.isOpen){this.isOpen=!1,this.logger.log("Unsubscribing...");(yield Promise.allSettled(this.subscribers.map((s=>this.unsubscribe(s.method,s.handler))))).forEach((s=>{"rejected"===s.status&&this.logger.log(`Failed to unsubscribe: "${s.reason}"`)})),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,api_key:this.opts.apiKey});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(401===e.status)throw new Error(`Error creating session - (${e.status}) unauthenticated (you need to be authenticated to start an session with persistent edits): ${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 u({sessionID:s,api_key:this.opts.apiKey})}catch(e){if(e instanceof u.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,r,l,c;return o(this,void 0,void 0,(function*(){yield s.open.call(this),this.codeSnippet={run:s=>o(this,void 0,void 0,(function*(){var e,i;if(!this.isOpen||!this.session)throw new Error("Session is not active");const t=yield this.call("codeSnippet_run",[s]);return null===(i=null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStateChange)||void 0===i||i.call(e,t),this.logger.log("Started running code",s),t})),stop:()=>o(this,void 0,void 0,(function*(){var s,e;if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=yield this.call("codeSnippet_stop");return null===(e=null===(s=this.codeSnippetOpts)||void 0===s?void 0:s.onStateChange)||void 0===e||e.call(s,i),this.logger.log("Stopped running code"),i})),listDeps:()=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started listing deps");const s=yield this.call("codeSnippet_deps");return this.logger.log("Stopped listing deps",s),s})),installDep:s=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started installing dependency",s);const e=yield this.call("codeSnippet_installDep",[s]);return this.logger.log("Stopped installing dependency",e),e})),uninstallDep:s=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started uninstalling dependency",s);const e=yield this.call("codeSnippet_uninstallDep",[s]);return this.logger.log("Stopped uninstalling dependency",e),e}))},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(),(null===(r=this.codeSnippetOpts)||void 0===r?void 0:r.onDepsStdout)?this.subscribe(n,this.codeSnippetOpts.onDepsStdout,"depsStdout"):Promise.resolve(),(null===(l=this.codeSnippetOpts)||void 0===l?void 0:l.onDepsStderr)?this.subscribe(n,this.codeSnippetOpts.onDepsStderr,"depsStderr"):Promise.resolve(),(null===(c=this.codeSnippetOpts)||void 0===c?void 0:c.onDepsChange)?this.subscribe(n,this.codeSnippetOpts.onDepsChange,"depsChange"):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(d,s,[i]),{destroy:()=>o(this,void 0,void 0,(function*(){yield this.unsubscribe(d,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=c,Object.defineProperty(s,"__esModule",{value:!0})}));
***************************************************************************** */function o(s,e,i,t){return new(i||(i=Promise))((function(o,n){function r(s){try{d(t.next(s))}catch(s){n(s)}}function l(s){try{d(t.throw(s))}catch(s){n(s)}}function d(s){var e;s.done?o(s.value):(e=s.value,e instanceof i?e:new i((function(s){s(e)}))).then(r,l)}d((t=t.apply(s,e||[])).next())}))}const n="codeSnippet";var r,l;s.CodeSnippetExecState=void 0,(r=s.CodeSnippetExecState||(s.CodeSnippetExecState={})).Running="Running",r.Stopped="Stopped",r.Loading="Loading",s.OutType=void 0,(l=s.OutType||(s.OutType={})).Stdout="Stdout",l.Stderr="Stderr";const d="terminal",c=t.Fetcher.for();function h(s){return new Promise((e=>setTimeout(e,s)))}c.configure({baseUrl:"https://ondevbook.com"});class p{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(`[${this.id()}]`,...s)}error(...s){console.error(`[${this.id()} ERROR]`,...s)}}const a=c.path("/sessions").method("post").create({api_key:!0}),u=c.path("/sessions/{sessionID}/refresh").method("post").create({api_key:!0});s.Session=class extends class{constructor(s){this.opts=s,this.isOpen=!1,this.rpc=new e.RpcWebSocketClient,this.subscribers=[],this.logger=new p("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((i=>i.handler===e&&i.method===s));i&&(yield this.call(`${s}_unsubscribe`,[null==i?void 0:i.id]),this.subscribers=this.subscribers.filter((s=>s!==i)),this.logger.log(`Unsubscribed from "${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 ${s} with params ${i}. Expected response to be a subscription ID, instead got ${JSON.stringify(t)}`);this.subscribers.push({id:t,handler:e,method:s}),this.logger.log(`Subscribed to "${s}_${i}" 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;return o(this,void 0,void 0,(function*(){if(this.isOpen){this.isOpen=!1,this.logger.log("Unsubscribing...");(yield Promise.allSettled(this.subscribers.map((s=>this.unsubscribe(s.method,s.handler))))).forEach((s=>{"rejected"===s.status&&this.logger.log(`Failed to unsubscribe: "${s.reason}"`)})),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,api_key:this.opts.apiKey});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(401===e.status)throw new Error(`Error creating session - (${e.status}) unauthenticated (you need to be authenticated to start an session with persistent edits): ${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.rpc.onError((s=>{this.logger.log("Error in WS session:",this.session,s)}));let e,i,t=!1;const n=new Promise(((s,o)=>{e=()=>{t||(t=!0,s())},i=()=>{t||(t=!0,o())}}));this.rpc.onOpen((()=>{null==e||e()})),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(100),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.log("Failed reconnecting to session:",this.session)}}else null==i||i()})))),this.rpc.onNotification.push(this.handleNotification.bind(this));try{this.logger.log("Connection to session:",this.session),yield this.rpc.connect(s)}catch(s){this.logger.log("Error connecting to session",s)}yield n,this.logger.log("Connected to 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.isOpen)return void this.logger.log("Cannot refresh session - it was closed");yield h(4e3);try{this.logger.log(`Refreshed session "${s}"`),yield u({sessionID:s,api_key:this.opts.apiKey})}catch(e){if(e instanceof u.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,r,l,c,h;return o(this,void 0,void 0,(function*(){yield s.open.call(this),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(),(null===(r=this.codeSnippetOpts)||void 0===r?void 0:r.onDepsStdout)?this.subscribe(n,this.codeSnippetOpts.onDepsStdout,"depsStdout"):Promise.resolve(),(null===(l=this.codeSnippetOpts)||void 0===l?void 0:l.onDepsStderr)?this.subscribe(n,this.codeSnippetOpts.onDepsStderr,"depsStderr"):Promise.resolve(),(null===(c=this.codeSnippetOpts)||void 0===c?void 0:c.onDepsChange)?this.subscribe(n,this.codeSnippetOpts.onDepsChange,"depsChange"):Promise.resolve(),(null===(h=this.codeSnippetOpts)||void 0===h?void 0:h.onScanPorts)?this.subscribe(n,this.codeSnippetOpts.onScanPorts,"scanOpenedPorts"):Promise.resolve()]),this.codeSnippet={run:s=>o(this,void 0,void 0,(function*(){var e,i;if(!this.isOpen||!this.session)throw new Error("Session is not active");const t=yield this.call("codeSnippet_run",[s]);return null===(i=null===(e=this.codeSnippetOpts)||void 0===e?void 0:e.onStateChange)||void 0===i||i.call(e,t),this.logger.log("Started running code",s),t})),stop:()=>o(this,void 0,void 0,(function*(){var s,e;if(!this.isOpen||!this.session)throw new Error("Session is not active");const i=yield this.call("codeSnippet_stop");return null===(e=null===(s=this.codeSnippetOpts)||void 0===s?void 0:s.onStateChange)||void 0===e||e.call(s,i),this.logger.log("Stopped running code"),i})),listDeps:()=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started listing deps");const s=yield this.call("codeSnippet_deps");return this.logger.log("Stopped listing deps",s),s})),installDep:s=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started installing dependency",s);const e=yield this.call("codeSnippet_installDep",[s]);return this.logger.log("Stopped installing dependency",e),e})),uninstallDep:s=>o(this,void 0,void 0,(function*(){if(!this.isOpen||!this.session)throw new Error("Session is not active");this.logger.log("Started uninstalling dependency",s);const e=yield this.call("codeSnippet_uninstallDep",[s]);return this.logger.log("Stopped uninstalling dependency",e),e}))},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(d,s,[i]),{destroy:()=>o(this,void 0,void 0,(function*(){yield this.unsubscribe(d,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=c,Object.defineProperty(s,"__esModule",{value:!0})}));
//# sourceMappingURL=index.js.map

@@ -34,2 +34,7 @@ export declare const codeSnippetMethod = "codeSnippet";

}
export interface OpenedPort {
State: string;
Ip: string;
Port: number;
}
export declare type CodeSnippetStateHandler = (state: CodeSnippetExecState) => void;

@@ -41,3 +46,4 @@ export declare type CodeSnippetStderrHandler = (o: OutStderrResponse) => void;

export declare type DepsChangeHandler = (deps: string[]) => void;
export declare type CodeSnippetSubscriptionHandler = CodeSnippetStateHandler | CodeSnippetStderrHandler | CodeSnippetStdoutHandler | DepsStderrHandler | DepsStderrHandler | DepsChangeHandler;
export declare type ScanOpenedPortsHandler = (ports: OpenedPort[]) => void;
export declare type CodeSnippetSubscriptionHandler = CodeSnippetStateHandler | CodeSnippetStderrHandler | CodeSnippetStdoutHandler | DepsStderrHandler | DepsStderrHandler | DepsChangeHandler | ScanOpenedPortsHandler;
export declare type CodeSnippetSubscriptionHandlerType = {

@@ -50,2 +56,3 @@ 'state': CodeSnippetStateHandler;

'depsChange': DepsChangeHandler;
'scanOpenedPorts': ScanOpenedPortsHandler;
};

@@ -52,0 +59,0 @@ export interface CodeSnippetManager {

@@ -1,2 +0,2 @@

import { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, DepsStdoutHandler, DepsStderrHandler, DepsChangeHandler } from './codeSnippet';
import { CodeSnippetManager, CodeSnippetStateHandler, CodeSnippetStderrHandler, CodeSnippetStdoutHandler, DepsStdoutHandler, DepsStderrHandler, DepsChangeHandler, ScanOpenedPortsHandler } from './codeSnippet';
import { TerminalManager } from './terminal';

@@ -11,2 +11,3 @@ import SessionConnection, { SessionConnectionOpts } from './sessionConnection';

onDepsChange?: DepsChangeHandler;
onScanPorts?: ScanOpenedPortsHandler;
}

@@ -13,0 +14,0 @@ export interface SessionOpts extends SessionConnectionOpts {

{
"name": "@devbookhq/sdk",
"version": "2.3.0",
"version": "2.3.1",
"description": "Devbook allows visitors of your docs to interact with and execute any code snippet or shell command in a private VM",

@@ -24,3 +24,4 @@ "homepage": "https://usedevbook.com",

"watch": "rollup -c -w",
"run": "node examples/index.mjs",
"measure": "node test/performance.mjs",
"run": "node test/run.mjs",
"fix": "npx eslint **/src/**/*.{ts,tsx,js} --fix",

@@ -27,0 +28,0 @@ "generate": "openapi-typescript shared/api/openapi.yml -x api_key --immutable-types --output src/api/schema.gen.ts"

@@ -60,1 +60,7 @@ # Devbook SDK

```
## Performance
Every 8 hours runs an automated CRON on GitHub that measures time to acquire a session. You can inspect the performance summaries in the GH Actions' details - in the workflow "Measure performance".
You can also trigger this workflow manually.
> The GitHub Actions CRON is not very reliable so don't rely it on running every 8 hours.

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc