Huge News!Announcing our $40M Series B led by Abstract Ventures.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 1.0.6 to 1.0.7

14

lib/cjs/core/constants/template.d.ts

@@ -7,7 +7,7 @@ import { TemplateConfig } from '../../common-ts/TemplateConfig';

/**
* Runtime environment that uses NextJS server with hot-reloading.
* Runtime environment that uses NextJS server with hot-reloading.
*/
NextJS = "nextjs-v11-components",
/**
* Runtime environment that supports executing JS code with NodeJS 16 runtime.
* Runtime environment that supports executing JS code with NodeJS 16 runtime.
*/

@@ -18,3 +18,11 @@ NodeJS = "nodejs-v16",

*/
Supabase = "supabase"
Supabase = "supabase",
/**
* Runtime environment for the Banana Node API.
*/
BananaNode = "banana-node",
/**
* Runtime environment for the Banana Python API.
*/
BananaPython = "banana-python"
}

@@ -21,0 +29,0 @@ export declare const templates: {

@@ -15,3 +15,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");

PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function t(e,t,n,s){return new(n||(n=Promise))((function(i,o){function r(e){try{l(s.next(e))}catch(e){o(e)}}function a(e){try{l(s.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((s=s.apply(e,t||[])).next())}))}class n{constructor(e,t=!1){this.logID=e,this.isEnabled=t}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...e){this.isEnabled&&console.log(`[${this.id()}]`,...e)}warn(...e){this.isEnabled&&console.warn(`[${this.id()}]`,...e)}error(...e){console.error(`[${this.id()} ERROR]`,...e)}}function s(e){return new Promise((t=>setTimeout(t,e)))}var i,o,r;!function(e){e.Error="Runner.Error"}(i||(i={})),function(e){e.Start="RunningEnvironment.Start",e.StartAck="RunningEnvironment.StartAck",e.Eval="RunningEnvironment.Eval",e.FSEventCreate="RunningEnvironment.FSEventCreate",e.FSEventRemove="RunningEnvironment.FSEventRemove",e.FSEventWrite="RunningEnvironment.FSEventWrite",e.CreateDir="RunningEnvironment.CreateDir",e.ListDir="RunningEnvironment.ListDir",e.WriteFile="RunningEnvironment.WriteFile",e.GetFile="RunningEnvironment.GetFile",e.RemoveFile="RunningEnvironment.RemoveFile",e.DirContent="RunningEnvironment.DirContent",e.FileContent="RunningEnvironment.FileContent",e.Stdout="RunningEnvironment.Stdout",e.Stderr="RunningEnvironment.Stderr",e.ExecCmd="RunningEnvironment.ExecCmd",e.KillCmd="RunningEnvironment.KillCmd",e.ListRunningCmds="RunningEnvironment.ListRunningCmds",e.CmdOut="RunningEnvironment.CmdOut",e.CmdExit="RunningEnvironment.CmdExit",e.RunningCmds="RunningEnvironment.RunningCmds",e.RunCode="RunningEnvironment.Run"}(o||(o={})),function(e){e.Error="CodeCell.Error"}(r||(r={}));const a={Runner:i,RunningEnvironment:o,CodeCell:r};var l;exports.Env=void 0,(l=exports.Env||(exports.Env={})).NextJS="nextjs-v11-components",l.NodeJS="nodejs-v16",l.Supabase="supabase";const c={[exports.Env.NodeJS]:{id:"nodejs-v16",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nodejs-v16:latest",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[exports.Env.Supabase]:{id:"supabase",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/supabase",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[exports.Env.NextJS]:{id:"nextjs-v11-components",fileExtension:".tsx",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nextjs-v11-components",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"}};class h{constructor(){this.url="wss://orchestrator.usedevbook.com",this.logger=new n("WebSocketConnection"),this.handlers=[]}get state(){var e;return null===(e=this.client)||void 0===e?void 0:e.readyState}get isClosed(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSED}get isClosing(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSING}get isOpen(){if(void 0!==this.client)return this.client.readyState===this.client.OPEN}get isConnecting(){if(void 0!==this.client)return this.client.readyState===this.client.CONNECTING}subscribeHandler(e){return this.handlers.push(e),()=>{this.handlers=this.handlers.filter((t=>t!==e))}}connect(e){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(e||this.sessionID?(e?(this.logger.log(`Will try to connect to session "${e}"`),this.sessionID=e):!e&&this.sessionID&&this.logger.log(`Will try to connect to previous session "${this.sessionID}"`),this.client=new WebSocket(`${this.url}/session/ws/${this.sessionID}`),this.client.onopen=()=>this.handleOpen(this.sessionID),this.client.onmessage=e=>{this.logger.log("Received (raw)",{msg:e}),this.handleMessage(e)},this.client.onerror=e=>this.handleError(e),this.client.onclose=e=>this.handleClose(e)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(e){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",e),this._send(e)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",e)}close(){var e;this.logger.log("Closing connection"),null===(e=this.client)||void 0===e||e.close(1e3)}handleOpen(e){var t;this.logger.log("Connection opened",{readyState:null===(t=this.client)||void 0===t?void 0:t.readyState}),this.handlers.forEach((t=>t.onOpen(e)))}_send(e){var t,n;null===(t=this.client)||void 0===t||t.send((n=e,JSON.stringify(n,(()=>{const e=new WeakSet;return(t,n)=>{if("object"==typeof n&&null!==n){if(e.has(n))return;e.add(n)}return n}})(),2)))}handleClose(e){return t(this,void 0,void 0,(function*(){this.logger.log("Connection closed",e),this.handlers.forEach((e=>e.onClose())),this.logger.log("Will try to reconnect in 3s"),yield s(3e3),this.connect()}))}handleError(e){var t;this.logger.error("Connection error",e),null===(t=this.client)||void 0===t||t.close()}handleMessage(e){if(!e.data)return void this.logger.error("Message has empty data field",e);const t=JSON.parse(e.data);if(!t.type)return void this.logger.error("Message has no type",t);const n=t;Object.values(a.RunningEnvironment).includes(n.type)||Object.values(a.Runner).includes(n.type)||Object.values(a.CodeCell).includes(n.type)?n.type!==a.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((e=>e.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var u,d;!function(e){e.Ok="Ok",e.Terminated="Terminated"}(u||(u={}));class g{constructor(e,t=new Date){this.id=e,this.lastPing=t,this.logger=new n("RunnerSession"),this.url="https://orchestrator.usedevbook.com"}ping(){return t(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const e=JSON.stringify({sessionID:this.id});try{const t=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:e}),n=yield t.json();if(!t.ok)throw this.logger.error(t.headers,n),new Error("Non-OK response when trying to ping active Runner session");if(n.status===u.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(e){throw this.logger.error(e),new Error("Failed to ping active Runner session")}}))}}!function(e){e.Connected="Connected",e.Connecting="Connecting",e.Disconnected="Disconnected"}(d||(d={}));class v{constructor(e){this.conn=e,this.logger=new n("SessionManager"),this.url="https://orchestrator.usedevbook.com",this.isGettingSessionActive=!1,this.status=d.Disconnected,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(e){null===e?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${e}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",e))}reset(){this.logger.log("Reset"),this.status=d.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var e;return t(this,void 0,void 0,(function*(){if(!this.isGettingSessionActive)for(this.isGettingSessionActive=!0;;){this.status=d.Connecting;try{const t=this.cachedSessionID?`${this.url}/session/${this.cachedSessionID}`:`${this.url}/session`;this.cachedSessionID?this.logger.log(`Restoring old Runner session "${this.cachedSessionID}"`):this.logger.log("Acquiring new Runner session");const n=yield fetch(t),i=yield n.json();if(!n.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 3s",n.headers,i),yield s(3e3);continue}for(this.session=new g(i.sessionID),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=d.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.session.ping(),yield s(5e3)}catch(e){this.logger.error(`Failed to ping session "${this.session.id}"`,e);break}this.logger.log(`Stopped pinging session "${null===(e=this.session)||void 0===e?void 0:e.id}"`),this.session=void 0,this.status=d.Disconnected,this.conn.close()}catch(e){this.logger.error("Failed to acquire Runner session. Will try again in 3s",e),yield s(3e3)}}}))}}function m(e,t){for(var n=0,s=e.length-1;s>=0;s--){var i=e[s];"."===i?e.splice(s,1):".."===i?(e.splice(s,1),n++):n&&(e.splice(s,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}var p=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,D=function(e){return p.exec(e).slice(1)};function f(){for(var e="",t=!1,n=arguments.length-1;n>=-1&&!t;n--){var s=n>=0?arguments[n]:"/";if("string"!=typeof s)throw new TypeError("Arguments to path.resolve must be strings");s&&(e=s+"/"+e,t="/"===s.charAt(0))}return(t?"/":"")+(e=m(S(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function b(e){var t=C(e),n="/"===I(e,-1);return(e=m(S(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function C(e){return"/"===e.charAt(0)}var E={extname:function(e){return D(e)[3]},basename:function(e,t){var n=D(e)[2];return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n},dirname:function(e){var t=D(e),n=t[0],s=t[1];return n||s?(s&&(s=s.substr(0,s.length-1)),n+s):"."},sep:"/",delimiter:":",relative:function(e,t){function n(e){for(var t=0;t<e.length&&""===e[t];t++);for(var n=e.length-1;n>=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=f(e).substr(1),t=f(t).substr(1);for(var s=n(e.split("/")),i=n(t.split("/")),o=Math.min(s.length,i.length),r=o,a=0;a<o;a++)if(s[a]!==i[a]){r=a;break}var l=[];for(a=r;a<s.length;a++)l.push("..");return(l=l.concat(i.slice(r))).join("/")},join:function(){var e=Array.prototype.slice.call(arguments,0);return b(S(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))},isAbsolute:C,normalize:b,resolve:f};function S(e,t){if(e.filter)return e.filter(t);for(var n=[],s=0;s<e.length;s++)t(e[s],s,e)&&n.push(e[s]);return n}var I="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)};function y(e){return function(e){const t=Array.from(e).reduce(((e,t)=>0|31*e+t.charCodeAt(0)),0);return("0000000"+(t>>>0).toString(16)).slice(-8)}(e)}class x{constructor(e,t){this.contextID=e,this.templateID=t,this.isReady=!1,this.id=`${e}_${y(t)}`,this.template=c[this.templateID]}}function R(e,{environmentID:t,template:n}){const s={type:a.RunningEnvironment.Start,payload:{environmentID:t,template:n}};e.send(s)}function w(e,{environmentID:t,executionID:n,command:s}){const i={type:a.RunningEnvironment.ExecCmd,payload:{environmentID:t,executionID:n,command:s}};e.send(i)}function k(e,{environmentID:t,path:n,content:s}){const i={type:a.RunningEnvironment.WriteFile,payload:{environmentID:t,path:n,content:s}};e.send(i)}class _{constructor(e){this.opts=e,this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.envs=[],this.logger=new n("EvaluationContext",e.debug),this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:this.handleConnectionOpen.bind(this),onMessage:this.handleConnectionMessage.bind(this),onClose:this.handleConnectionClose.bind(this)}),this.opts.conn.isOpen&&this.opts.conn.sessionID&&this.handleConnectionOpen(this.opts.conn.sessionID),this.opts.conn.isClosed&&this.handleConnectionClose()}get contextID(){return this.opts.contextID}restart(){return this.logger.log("Restart",this.opts.conn.sessionID),this.envs.forEach((e=>{e.isReady=!1,R(this.opts.conn,{environmentID:e.id,template:e.template})}))}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.envs=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({templateID:e,path:n}){return t(this,void 0,void 0,(function*(){this.logger.log("Get file",{templateID:e,filepath:n});const t=this.getRunningEnvironment({templateID:e});if(!t)return void this.logger.error("Environment not found",{templateID:e,filepath:n});if(!t.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:n});let s;const i=new Promise(((e,t)=>{s=e,setTimeout((()=>{t("Timeout")}),1e4)})),o=e=>{e.path.endsWith(n)&&s(e.content)};this.subscribeFileContent(o),function(e,{environmentID:t,path:n}){const s={type:a.RunningEnvironment.GetFile,payload:{environmentID:t,path:n}};e.send(s)}(this.opts.conn,{path:n,environmentID:t.id});try{return yield i}catch(e){throw new Error(`Error retrieving file ${n}: ${e}`)}finally{this.unsubscribeFileContent(o)}}))}updateFile({templateID:e,path:n,content:s}){return t(this,void 0,void 0,(function*(){this.logger.log("Update file",{templateID:e,filepath:n});const t=this.getRunningEnvironment({templateID:e});if(!t)return void this.logger.error("Environment not found",{templateID:e,filepath:n});if(!t.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:n});let i;const o=new Promise(((e,t)=>{i=e,setTimeout((()=>{t("Timeout")}),1e4)})),r=e=>{e.path.endsWith(n)&&i()};this.subscribeFSWrite(r),k(this.opts.conn,{environmentID:t.id,path:n,content:s});try{yield o}catch(e){throw new Error(`File ${n} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(r)}}))}executeCode({templateID:e,executionID:n,code:s}){return t(this,void 0,void 0,(function*(){this.logger.log("Execute code",{templateID:e,executionID:n});const t=c[e].toCommand;if(void 0===t)return;const i=this.getRunningEnvironment({templateID:e});if(!i)return void this.logger.error("Environment not found",{templateID:e,executionID:n});if(!i.isReady)return void this.logger.error("Environment is not ready",{templateID:e,executionID:n});const o=c[e].fileExtension,r=`${n}${o}`,a=E.join("/src",r);k(this.opts.conn,{environmentID:i.id,path:a,content:s});const l=t(E.join(c[e].root_dir,a));w(this.opts.conn,{environmentID:i.id,executionID:n,command:l})}))}executeCommand({templateID:e,executionID:t,command:n}){this.logger.log("Execute shell command",{templateID:e,executionID:t,command:n});const s=this.getRunningEnvironment({templateID:e});s?s.isReady?w(this.opts.conn,{environmentID:s.id,executionID:t,command:n}):this.logger.error("Environment is not ready",{templateID:e,executionID:t,command:n}):this.logger.error("Environment not found",{templateID:e,executionID:t,command:n})}getRunningEnvironment({templateID:e}){return this.envs.find((t=>t.templateID===e))}createRunningEnvironment({templateID:e}){var t,n;this.logger.log("Creating running environment",{templateID:e});if(this.getRunningEnvironment({templateID:e}))return;const s=new x(this.contextID,e);this.envs.push(s),R(this.opts.conn,{environmentID:s.id,template:s.template}),null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)}subscribeFileContent(e){this.fileContentSubscribers.push(e)}unsubscribeFileContent(e){const t=this.fileContentSubscribers.indexOf(e);t>-1&&this.fileContentSubscribers.splice(t,1)}subscribeFSWrite(e){this.fsWriteSubscribers.push(e)}unsubscribeFSWrite(e){const t=this.fsWriteSubscribers.indexOf(e);t>-1&&this.fsWriteSubscribers.splice(t,1)}handleConnectionOpen(e){var t,n;this.restart(),null===(n=(t=this.opts).onSessionChange)||void 0===n||n.call(t,{status:d.Connected,sessionID:e})}handleConnectionClose(){var e,t;null===(t=(e=this.opts).onSessionChange)||void 0===t||t.call(e,{status:d.Connecting})}handleConnectionMessage(e){switch(this.logger.log("Handling message from remote Runner",{message:e}),e.type){case a.RunningEnvironment.StartAck:{const t=e;this.vmenv_handleStartAck(t.payload);break}case a.RunningEnvironment.CmdOut:{const t=e;this.vmenv_handleCmdOut(t.payload);break}case a.RunningEnvironment.CmdExit:{const t=e;this.vmenv_handleCmdExit(t.payload);break}case a.RunningEnvironment.FSEventWrite:{const t=e;this.vmenv_handleFSEventWrite(t.payload);break}case a.RunningEnvironment.FileContent:{const t=e;this.vmenv_handleFileContent(t.payload);break}default:this.logger.warn("Unknown message type",{message:e})}}vmenv_handleCmdExit(e){var t,n;void 0!==e.error&&(null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,{environmentID:e.environmentID,executionID:e.executionID,stderr:e.error}))}vmenv_handleFSEventWrite(e){this.logger.log('[vmenv] Handling "FSEventWrite"',e);this.envs.find((t=>t.id===e.environmentID))?this.fsWriteSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleFileContent(e){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:e.environmentID,path:e.path});this.envs.find((t=>t.id===e.environmentID))?this.fileContentSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleStartAck(e){var t,n;this.logger.log('[vmenv] Handling "StartAck"',{payload:e});const s=this.envs.find((t=>t.id===e.environmentID));s?(s.isReady=!0,null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)):this.logger.warn("Environment not found",{payload:e})}vmenv_handleCmdOut(e){var t,n;this.logger.log('[vmenv] Handling "CmdOut"',e),null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,e)}}class F{constructor(){this.logger=new n("Runner"),this.conn=new h,this.sessManager=new v(this.conn)}static get obj(){return F._obj||(F._obj=new F)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(e){return new _(Object.assign(Object.assign({},e),{conn:this.conn}))}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const O=($="1234567890abcdefghijklmnopqrstuvwxyz",j=6,()=>{let e="",t=j;for(;t--;)e+=$[Math.random()*$.length|0];return e});var $,j,W;exports.DevbookStatus=void 0,(W=exports.DevbookStatus||(exports.DevbookStatus={}))[W.Disconnected=0]="Disconnected",W[W.Connecting=1]="Connecting",W[W.Connected=2]="Connected";class N{constructor(e){this.opts=e,this.contextID="default",this.executionID=O(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=d.Disconnected,this._status=exports.DevbookStatus.Disconnected;const t=this.getURL.bind(this),n=()=>this.opts.env,s=()=>this.executionID,i=e=>this.isEnvReady=e,o=e=>this.sessionStatus=e,r=e=>this.sessionID=e;this.context=F.obj.createContext({debug:e.debug,contextID:this.contextID,onEnvChange(s){var o;s.templateID===n()&&(i(s.isReady),null===(o=e.onURLChange)||void 0===o||o.call(e,t))},onSessionChange({status:n,sessionID:s}){var i;r(s),o(n),null===(i=e.onURLChange)||void 0===i||i.call(e,t)},onCmdOut(t){var n,i;t.executionID===s()&&(void 0!==t.stdout&&(null===(n=e.onStdout)||void 0===n||n.call(e,t.stdout)),void 0!==t.stderr&&(null===(i=e.onStderr)||void 0===i||i.call(e,t.stderr)))}}),this.context.createRunningEnvironment({templateID:e.env})}get sessionID(){return this._sessionID}set sessionID(e){this._sessionID=e}get isDestroyed(){return this._isDestroyed}set isDestroyed(e){this._isDestroyed=e,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(e){this._isEnvReady=e,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(e){this._sessionStatus=e,this.updateStatus()}get status(){return this._status}set status(e){var t,n;this._status=e,null===(n=(t=this.opts).onStatusChange)||void 0===n||n.call(t,e)}get fs(){return{get:this.getFile.bind(this),write:this.writeFile.bind(this)}}getURL(e){if(this.status!==exports.DevbookStatus.Connected)return;const t=this.sessionID;if(!this.sessionID)return;const n=this.context.getRunningEnvironment({templateID:this.opts.env});return(null==n?void 0:n.isReady)?`https://${e}-${n.id}-${t}.o.usedevbook.com`:void 0}runCmd(e){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=O(),this.context.executeCommand({templateID:this.opts.env,executionID:this.executionID,command:e})}runCode(e){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=O(),this.context.executeCode({templateID:this.opts.env,executionID:this.executionID,code:e})}destroy(){this.context.destroy(),this.isDestroyed=!0}getFile(e){return t(this,void 0,void 0,(function*(){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({templateID:this.opts.env,path:e})}))}writeFile(e,n){return t(this,void 0,void 0,(function*(){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({templateID:this.opts.env,path:e,content:n})}))}updateStatus(){if(this.isDestroyed)return void(this.status!==exports.DevbookStatus.Disconnected&&(this.status=exports.DevbookStatus.Disconnected));let e;switch(this.sessionStatus){case d.Disconnected:e=exports.DevbookStatus.Disconnected;break;case d.Connecting:e=exports.DevbookStatus.Connecting;break;case d.Connected:if(!this.isEnvReady){e=exports.DevbookStatus.Connecting;break}e=exports.DevbookStatus.Connected}this.status=e}}exports.Devbook=N,exports.useDevbook=function({env:n,debug:s,port:i}){const[o,r]=e.useState(),[a,l]=e.useState(exports.DevbookStatus.Disconnected),[c,h]=e.useState([]),[u,d]=e.useState([]),[g,v]=e.useState(),m=e.useMemo((()=>o?o.fs:{get(){return t(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))},write(){return t(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))}}),[o]),p=e.useCallback((e=>{o&&(d([]),h([]),o.runCmd(e))}),[o]),D=e.useCallback((e=>{o&&(d([]),h([]),o.runCode(e))}),[o]);return e.useEffect((function(){const e=new N({debug:s,env:n,onStatusChange(e){l(e)},onStderr(e){h((t=>[...t,e]))},onStdout(e){d((t=>[...t,e]))},onURLChange(e){i&&v(e(i))}});return d([]),h([]),v(void 0),r(e),()=>{e.destroy()}}),[n,s,i]),{stderr:c,stdout:u,runCmd:p,runCode:D,status:a,fs:m,url:g}};
***************************************************************************** */function t(e,t,n,s){return new(n||(n=Promise))((function(i,o){function r(e){try{l(s.next(e))}catch(e){o(e)}}function a(e){try{l(s.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((s=s.apply(e,t||[])).next())}))}class n{constructor(e,t=!1){this.logID=e,this.isEnabled=t}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...e){this.isEnabled&&console.log(`[${this.id()}]`,...e)}warn(...e){this.isEnabled&&console.warn(`[${this.id()}]`,...e)}error(...e){console.error(`[${this.id()} ERROR]`,...e)}}function s(e){return new Promise((t=>setTimeout(t,e)))}var i,o,r;!function(e){e.Error="Runner.Error"}(i||(i={})),function(e){e.Start="RunningEnvironment.Start",e.StartAck="RunningEnvironment.StartAck",e.Eval="RunningEnvironment.Eval",e.FSEventCreate="RunningEnvironment.FSEventCreate",e.FSEventRemove="RunningEnvironment.FSEventRemove",e.FSEventWrite="RunningEnvironment.FSEventWrite",e.CreateDir="RunningEnvironment.CreateDir",e.ListDir="RunningEnvironment.ListDir",e.WriteFile="RunningEnvironment.WriteFile",e.GetFile="RunningEnvironment.GetFile",e.RemoveFile="RunningEnvironment.RemoveFile",e.DirContent="RunningEnvironment.DirContent",e.FileContent="RunningEnvironment.FileContent",e.Stdout="RunningEnvironment.Stdout",e.Stderr="RunningEnvironment.Stderr",e.ExecCmd="RunningEnvironment.ExecCmd",e.KillCmd="RunningEnvironment.KillCmd",e.ListRunningCmds="RunningEnvironment.ListRunningCmds",e.CmdOut="RunningEnvironment.CmdOut",e.CmdExit="RunningEnvironment.CmdExit",e.RunningCmds="RunningEnvironment.RunningCmds",e.RunCode="RunningEnvironment.Run"}(o||(o={})),function(e){e.Error="CodeCell.Error"}(r||(r={}));const a={Runner:i,RunningEnvironment:o,CodeCell:r};var l;exports.Env=void 0,(l=exports.Env||(exports.Env={})).NextJS="nextjs-v11-components",l.NodeJS="nodejs-v16",l.Supabase="supabase",l.BananaNode="banana-node",l.BananaPython="banana-python";const c={[exports.Env.NodeJS]:{id:"nodejs-v16",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nodejs-v16:latest",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[exports.Env.Supabase]:{id:"supabase",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/supabase",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[exports.Env.NextJS]:{id:"nextjs-v11-components",fileExtension:".tsx",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nextjs-v11-components",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"},[exports.Env.BananaNode]:{id:"banana-node",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/banana-node",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"},[exports.Env.BananaPython]:{id:"banana-python",fileExtension:".py",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/banana-python",root_dir:"/home/runner",code_cells_dir:"/home/runner"}};class h{constructor(){this.url="wss://orchestrator.usedevbook.com",this.logger=new n("WebSocketConnection"),this.handlers=[]}get state(){var e;return null===(e=this.client)||void 0===e?void 0:e.readyState}get isClosed(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSED}get isClosing(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSING}get isOpen(){if(void 0!==this.client)return this.client.readyState===this.client.OPEN}get isConnecting(){if(void 0!==this.client)return this.client.readyState===this.client.CONNECTING}subscribeHandler(e){return this.handlers.push(e),()=>{this.handlers=this.handlers.filter((t=>t!==e))}}connect(e){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(e||this.sessionID?(e?(this.logger.log(`Will try to connect to session "${e}"`),this.sessionID=e):!e&&this.sessionID&&this.logger.log(`Will try to connect to previous session "${this.sessionID}"`),this.client=new WebSocket(`${this.url}/session/ws/${this.sessionID}`),this.client.onopen=()=>this.handleOpen(this.sessionID),this.client.onmessage=e=>{this.logger.log("Received (raw)",{msg:e}),this.handleMessage(e)},this.client.onerror=e=>this.handleError(e),this.client.onclose=e=>this.handleClose(e)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(e){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",e),this._send(e)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",e)}close(){var e;this.logger.log("Closing connection"),null===(e=this.client)||void 0===e||e.close(1e3)}handleOpen(e){var t;this.logger.log("Connection opened",{readyState:null===(t=this.client)||void 0===t?void 0:t.readyState}),this.handlers.forEach((t=>t.onOpen(e)))}_send(e){var t,n;null===(t=this.client)||void 0===t||t.send((n=e,JSON.stringify(n,(()=>{const e=new WeakSet;return(t,n)=>{if("object"==typeof n&&null!==n){if(e.has(n))return;e.add(n)}return n}})(),2)))}handleClose(e){return t(this,void 0,void 0,(function*(){this.logger.log("Connection closed",e),this.handlers.forEach((e=>e.onClose())),this.logger.log("Will try to reconnect in 3s"),yield s(3e3),this.connect()}))}handleError(e){var t;this.logger.error("Connection error",e),null===(t=this.client)||void 0===t||t.close()}handleMessage(e){if(!e.data)return void this.logger.error("Message has empty data field",e);const t=JSON.parse(e.data);if(!t.type)return void this.logger.error("Message has no type",t);const n=t;Object.values(a.RunningEnvironment).includes(n.type)||Object.values(a.Runner).includes(n.type)||Object.values(a.CodeCell).includes(n.type)?n.type!==a.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((e=>e.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var d,u;!function(e){e.Ok="Ok",e.Terminated="Terminated"}(d||(d={}));class g{constructor(e,t=new Date){this.id=e,this.lastPing=t,this.logger=new n("RunnerSession"),this.url="https://orchestrator.usedevbook.com"}ping(){return t(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const e=JSON.stringify({sessionID:this.id});try{const t=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:e}),n=yield t.json();if(!t.ok)throw this.logger.error(t.headers,n),new Error("Non-OK response when trying to ping active Runner session");if(n.status===d.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(e){throw this.logger.error(e),new Error("Failed to ping active Runner session")}}))}}!function(e){e.Connected="Connected",e.Connecting="Connecting",e.Disconnected="Disconnected"}(u||(u={}));class v{constructor(e){this.conn=e,this.logger=new n("SessionManager"),this.url="https://orchestrator.usedevbook.com",this.isGettingSessionActive=!1,this.status=u.Disconnected,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(e){null===e?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${e}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",e))}reset(){this.logger.log("Reset"),this.status=u.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var e;return t(this,void 0,void 0,(function*(){if(!this.isGettingSessionActive)for(this.isGettingSessionActive=!0;;){this.status=u.Connecting;try{const t=this.cachedSessionID?`${this.url}/session/${this.cachedSessionID}`:`${this.url}/session`;this.cachedSessionID?this.logger.log(`Restoring old Runner session "${this.cachedSessionID}"`):this.logger.log("Acquiring new Runner session");const n=yield fetch(t),i=yield n.json();if(!n.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 3s",n.headers,i),yield s(3e3);continue}for(this.session=new g(i.sessionID),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=u.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.session.ping(),yield s(5e3)}catch(e){this.logger.error(`Failed to ping session "${this.session.id}"`,e);break}this.logger.log(`Stopped pinging session "${null===(e=this.session)||void 0===e?void 0:e.id}"`),this.session=void 0,this.status=u.Disconnected,this.conn.close()}catch(e){this.logger.error("Failed to acquire Runner session. Will try again in 3s",e),yield s(3e3)}}}))}}function m(e,t){for(var n=0,s=e.length-1;s>=0;s--){var i=e[s];"."===i?e.splice(s,1):".."===i?(e.splice(s,1),n++):n&&(e.splice(s,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}var p=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,D=function(e){return p.exec(e).slice(1)};function f(){for(var e="",t=!1,n=arguments.length-1;n>=-1&&!t;n--){var s=n>=0?arguments[n]:"/";if("string"!=typeof s)throw new TypeError("Arguments to path.resolve must be strings");s&&(e=s+"/"+e,t="/"===s.charAt(0))}return(t?"/":"")+(e=m(S(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function b(e){var t=C(e),n="/"===I(e,-1);return(e=m(S(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function C(e){return"/"===e.charAt(0)}var E={extname:function(e){return D(e)[3]},basename:function(e,t){var n=D(e)[2];return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n},dirname:function(e){var t=D(e),n=t[0],s=t[1];return n||s?(s&&(s=s.substr(0,s.length-1)),n+s):"."},sep:"/",delimiter:":",relative:function(e,t){function n(e){for(var t=0;t<e.length&&""===e[t];t++);for(var n=e.length-1;n>=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=f(e).substr(1),t=f(t).substr(1);for(var s=n(e.split("/")),i=n(t.split("/")),o=Math.min(s.length,i.length),r=o,a=0;a<o;a++)if(s[a]!==i[a]){r=a;break}var l=[];for(a=r;a<s.length;a++)l.push("..");return(l=l.concat(i.slice(r))).join("/")},join:function(){var e=Array.prototype.slice.call(arguments,0);return b(S(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))},isAbsolute:C,normalize:b,resolve:f};function S(e,t){if(e.filter)return e.filter(t);for(var n=[],s=0;s<e.length;s++)t(e[s],s,e)&&n.push(e[s]);return n}var I="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)};function y(e){return function(e){const t=Array.from(e).reduce(((e,t)=>0|31*e+t.charCodeAt(0)),0);return("0000000"+(t>>>0).toString(16)).slice(-8)}(e)}class x{constructor(e,t){this.contextID=e,this.templateID=t,this.isReady=!1,this.id=`${e}_${y(t)}`,this.template=c[this.templateID]}}function R(e,{environmentID:t,template:n}){const s={type:a.RunningEnvironment.Start,payload:{environmentID:t,template:n}};e.send(s)}function k(e,{environmentID:t,executionID:n,command:s}){const i={type:a.RunningEnvironment.ExecCmd,payload:{environmentID:t,executionID:n,command:s}};e.send(i)}function w(e,{environmentID:t,path:n,content:s}){const i={type:a.RunningEnvironment.WriteFile,payload:{environmentID:t,path:n,content:s}};e.send(i)}class _{constructor(e){this.opts=e,this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.envs=[],this.logger=new n("EvaluationContext",e.debug),this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:this.handleConnectionOpen.bind(this),onMessage:this.handleConnectionMessage.bind(this),onClose:this.handleConnectionClose.bind(this)}),this.opts.conn.isOpen&&this.opts.conn.sessionID&&this.handleConnectionOpen(this.opts.conn.sessionID),this.opts.conn.isClosed&&this.handleConnectionClose()}get contextID(){return this.opts.contextID}restart(){return this.logger.log("Restart",this.opts.conn.sessionID),this.envs.forEach((e=>{e.isReady=!1,R(this.opts.conn,{environmentID:e.id,template:e.template})}))}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.envs=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({templateID:e,path:n}){return t(this,void 0,void 0,(function*(){this.logger.log("Get file",{templateID:e,filepath:n});const t=this.getRunningEnvironment({templateID:e});if(!t)return void this.logger.error("Environment not found",{templateID:e,filepath:n});if(!t.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:n});let s;const i=new Promise(((e,t)=>{s=e,setTimeout((()=>{t("Timeout")}),1e4)})),o=e=>{e.path.endsWith(n)&&s(e.content)};this.subscribeFileContent(o),function(e,{environmentID:t,path:n}){const s={type:a.RunningEnvironment.GetFile,payload:{environmentID:t,path:n}};e.send(s)}(this.opts.conn,{path:n,environmentID:t.id});try{return yield i}catch(e){throw new Error(`Error retrieving file ${n}: ${e}`)}finally{this.unsubscribeFileContent(o)}}))}updateFile({templateID:e,path:n,content:s}){return t(this,void 0,void 0,(function*(){this.logger.log("Update file",{templateID:e,filepath:n});const t=this.getRunningEnvironment({templateID:e});if(!t)return void this.logger.error("Environment not found",{templateID:e,filepath:n});if(!t.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:n});let i;const o=new Promise(((e,t)=>{i=e,setTimeout((()=>{t("Timeout")}),1e4)})),r=e=>{e.path.endsWith(n)&&i()};this.subscribeFSWrite(r),w(this.opts.conn,{environmentID:t.id,path:n,content:s});try{yield o}catch(e){throw new Error(`File ${n} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(r)}}))}executeCode({templateID:e,executionID:n,code:s}){return t(this,void 0,void 0,(function*(){this.logger.log("Execute code",{templateID:e,executionID:n});const t=c[e].toCommand;if(void 0===t)return;const i=this.getRunningEnvironment({templateID:e});if(!i)return void this.logger.error("Environment not found",{templateID:e,executionID:n});if(!i.isReady)return void this.logger.error("Environment is not ready",{templateID:e,executionID:n});const o=c[e].fileExtension,r=`${n}${o}`,a=E.join("/src",r);w(this.opts.conn,{environmentID:i.id,path:a,content:s});const l=t(E.join(c[e].root_dir,a));k(this.opts.conn,{environmentID:i.id,executionID:n,command:l})}))}executeCommand({templateID:e,executionID:t,command:n}){this.logger.log("Execute shell command",{templateID:e,executionID:t,command:n});const s=this.getRunningEnvironment({templateID:e});s?s.isReady?k(this.opts.conn,{environmentID:s.id,executionID:t,command:n}):this.logger.error("Environment is not ready",{templateID:e,executionID:t,command:n}):this.logger.error("Environment not found",{templateID:e,executionID:t,command:n})}getRunningEnvironment({templateID:e}){return this.envs.find((t=>t.templateID===e))}createRunningEnvironment({templateID:e}){var t,n;this.logger.log("Creating running environment",{templateID:e});if(this.getRunningEnvironment({templateID:e}))return;const s=new x(this.contextID,e);this.envs.push(s),R(this.opts.conn,{environmentID:s.id,template:s.template}),null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)}subscribeFileContent(e){this.fileContentSubscribers.push(e)}unsubscribeFileContent(e){const t=this.fileContentSubscribers.indexOf(e);t>-1&&this.fileContentSubscribers.splice(t,1)}subscribeFSWrite(e){this.fsWriteSubscribers.push(e)}unsubscribeFSWrite(e){const t=this.fsWriteSubscribers.indexOf(e);t>-1&&this.fsWriteSubscribers.splice(t,1)}handleConnectionOpen(e){var t,n;this.restart(),null===(n=(t=this.opts).onSessionChange)||void 0===n||n.call(t,{status:u.Connected,sessionID:e})}handleConnectionClose(){var e,t;null===(t=(e=this.opts).onSessionChange)||void 0===t||t.call(e,{status:u.Connecting})}handleConnectionMessage(e){switch(this.logger.log("Handling message from remote Runner",{message:e}),e.type){case a.RunningEnvironment.StartAck:{const t=e;this.vmenv_handleStartAck(t.payload);break}case a.RunningEnvironment.CmdOut:{const t=e;this.vmenv_handleCmdOut(t.payload);break}case a.RunningEnvironment.CmdExit:{const t=e;this.vmenv_handleCmdExit(t.payload);break}case a.RunningEnvironment.FSEventWrite:{const t=e;this.vmenv_handleFSEventWrite(t.payload);break}case a.RunningEnvironment.FileContent:{const t=e;this.vmenv_handleFileContent(t.payload);break}default:this.logger.warn("Unknown message type",{message:e})}}vmenv_handleCmdExit(e){var t,n;void 0!==e.error&&(null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,{environmentID:e.environmentID,executionID:e.executionID,stderr:e.error}))}vmenv_handleFSEventWrite(e){this.logger.log('[vmenv] Handling "FSEventWrite"',e);this.envs.find((t=>t.id===e.environmentID))?this.fsWriteSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleFileContent(e){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:e.environmentID,path:e.path});this.envs.find((t=>t.id===e.environmentID))?this.fileContentSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleStartAck(e){var t,n;this.logger.log('[vmenv] Handling "StartAck"',{payload:e});const s=this.envs.find((t=>t.id===e.environmentID));s?(s.isReady=!0,null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)):this.logger.warn("Environment not found",{payload:e})}vmenv_handleCmdOut(e){var t,n;this.logger.log('[vmenv] Handling "CmdOut"',e),null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,e)}}class F{constructor(){this.logger=new n("Runner"),this.conn=new h,this.sessManager=new v(this.conn)}static get obj(){return F._obj||(F._obj=new F)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(e){return new _(Object.assign(Object.assign({},e),{conn:this.conn}))}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const O=($="1234567890abcdefghijklmnopqrstuvwxyz",j=6,()=>{let e="",t=j;for(;t--;)e+=$[Math.random()*$.length|0];return e});var $,j,W;exports.DevbookStatus=void 0,(W=exports.DevbookStatus||(exports.DevbookStatus={}))[W.Disconnected=0]="Disconnected",W[W.Connecting=1]="Connecting",W[W.Connected=2]="Connected";class N{constructor(e){this.opts=e,this.contextID="default",this.executionID=O(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=u.Disconnected,this._status=exports.DevbookStatus.Disconnected;const t=this.getURL.bind(this),n=()=>this.opts.env,s=()=>this.executionID,i=e=>this.isEnvReady=e,o=e=>this.sessionStatus=e,r=e=>this.sessionID=e;this.context=F.obj.createContext({debug:e.debug,contextID:this.contextID,onEnvChange(s){var o;s.templateID===n()&&(i(s.isReady),null===(o=e.onURLChange)||void 0===o||o.call(e,t))},onSessionChange({status:n,sessionID:s}){var i;r(s),o(n),null===(i=e.onURLChange)||void 0===i||i.call(e,t)},onCmdOut(t){var n,i;t.executionID===s()&&(void 0!==t.stdout&&(null===(n=e.onStdout)||void 0===n||n.call(e,t.stdout)),void 0!==t.stderr&&(null===(i=e.onStderr)||void 0===i||i.call(e,t.stderr)))}}),this.context.createRunningEnvironment({templateID:e.env})}get sessionID(){return this._sessionID}set sessionID(e){this._sessionID=e}get isDestroyed(){return this._isDestroyed}set isDestroyed(e){this._isDestroyed=e,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(e){this._isEnvReady=e,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(e){this._sessionStatus=e,this.updateStatus()}get status(){return this._status}set status(e){var t,n;this._status=e,null===(n=(t=this.opts).onStatusChange)||void 0===n||n.call(t,e)}get fs(){return{get:this.getFile.bind(this),write:this.writeFile.bind(this)}}getURL(e){if(this.status!==exports.DevbookStatus.Connected)return;const t=this.sessionID;if(!this.sessionID)return;const n=this.context.getRunningEnvironment({templateID:this.opts.env});return(null==n?void 0:n.isReady)?`https://${e}-${n.id}-${t}.o.usedevbook.com`:void 0}runCmd(e){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=O(),this.context.executeCommand({templateID:this.opts.env,executionID:this.executionID,command:e})}runCode(e){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=O(),this.context.executeCode({templateID:this.opts.env,executionID:this.executionID,code:e})}destroy(){this.context.destroy(),this.isDestroyed=!0}getFile(e){return t(this,void 0,void 0,(function*(){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({templateID:this.opts.env,path:e})}))}writeFile(e,n){return t(this,void 0,void 0,(function*(){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({templateID:this.opts.env,path:e,content:n})}))}updateStatus(){if(this.isDestroyed)return void(this.status!==exports.DevbookStatus.Disconnected&&(this.status=exports.DevbookStatus.Disconnected));let e;switch(this.sessionStatus){case u.Disconnected:e=exports.DevbookStatus.Disconnected;break;case u.Connecting:e=exports.DevbookStatus.Connecting;break;case u.Connected:if(!this.isEnvReady){e=exports.DevbookStatus.Connecting;break}e=exports.DevbookStatus.Connected}this.status=e}}exports.Devbook=N,exports.useDevbook=function({env:n,debug:s,port:i}){const[o,r]=e.useState(),[a,l]=e.useState(exports.DevbookStatus.Disconnected),[c,h]=e.useState([]),[d,u]=e.useState([]),[g,v]=e.useState(),m=e.useMemo((()=>o?o.fs:{get(){return t(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))},write(){return t(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))}}),[o]),p=e.useCallback((e=>{o&&(u([]),h([]),o.runCmd(e))}),[o]),D=e.useCallback((e=>{o&&(u([]),h([]),o.runCode(e))}),[o]);return e.useEffect((function(){const e=new N({debug:s,env:n,onStatusChange(e){l(e)},onStderr(e){h((t=>[...t,e]))},onStdout(e){u((t=>[...t,e]))},onURLChange(e){i&&v(e(i))}});return u([]),h([]),v(void 0),r(e),()=>{e.destroy()}}),[n,s,i]),{stderr:c,stdout:d,runCmd:p,runCode:D,status:a,fs:m,url:g}};
//# sourceMappingURL=index.js.map

@@ -7,7 +7,7 @@ import { TemplateConfig } from '../../common-ts/TemplateConfig';

/**
* Runtime environment that uses NextJS server with hot-reloading.
* Runtime environment that uses NextJS server with hot-reloading.
*/
NextJS = "nextjs-v11-components",
/**
* Runtime environment that supports executing JS code with NodeJS 16 runtime.
* Runtime environment that supports executing JS code with NodeJS 16 runtime.
*/

@@ -18,3 +18,11 @@ NodeJS = "nodejs-v16",

*/
Supabase = "supabase"
Supabase = "supabase",
/**
* Runtime environment for the Banana Node API.
*/
BananaNode = "banana-node",
/**
* Runtime environment for the Banana Python API.
*/
BananaPython = "banana-python"
}

@@ -21,0 +29,0 @@ export declare const templates: {

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

import{useState as e,useMemo as t,useCallback as n,useEffect as s}from"react";
import{useState as e,useMemo as n,useCallback as t,useEffect as s}from"react";
/*! *****************************************************************************

@@ -15,3 +15,3 @@ Copyright (c) Microsoft Corporation.

PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function i(e,t,n,s){return new(n||(n=Promise))((function(i,o){function r(e){try{l(s.next(e))}catch(e){o(e)}}function a(e){try{l(s.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((s=s.apply(e,t||[])).next())}))}class o{constructor(e,t=!1){this.logID=e,this.isEnabled=t}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...e){this.isEnabled&&console.log(`[${this.id()}]`,...e)}warn(...e){this.isEnabled&&console.warn(`[${this.id()}]`,...e)}error(...e){console.error(`[${this.id()} ERROR]`,...e)}}function r(e){return new Promise((t=>setTimeout(t,e)))}var a,l,c;!function(e){e.Error="Runner.Error"}(a||(a={})),function(e){e.Start="RunningEnvironment.Start",e.StartAck="RunningEnvironment.StartAck",e.Eval="RunningEnvironment.Eval",e.FSEventCreate="RunningEnvironment.FSEventCreate",e.FSEventRemove="RunningEnvironment.FSEventRemove",e.FSEventWrite="RunningEnvironment.FSEventWrite",e.CreateDir="RunningEnvironment.CreateDir",e.ListDir="RunningEnvironment.ListDir",e.WriteFile="RunningEnvironment.WriteFile",e.GetFile="RunningEnvironment.GetFile",e.RemoveFile="RunningEnvironment.RemoveFile",e.DirContent="RunningEnvironment.DirContent",e.FileContent="RunningEnvironment.FileContent",e.Stdout="RunningEnvironment.Stdout",e.Stderr="RunningEnvironment.Stderr",e.ExecCmd="RunningEnvironment.ExecCmd",e.KillCmd="RunningEnvironment.KillCmd",e.ListRunningCmds="RunningEnvironment.ListRunningCmds",e.CmdOut="RunningEnvironment.CmdOut",e.CmdExit="RunningEnvironment.CmdExit",e.RunningCmds="RunningEnvironment.RunningCmds",e.RunCode="RunningEnvironment.Run"}(l||(l={})),function(e){e.Error="CodeCell.Error"}(c||(c={}));const h={Runner:a,RunningEnvironment:l,CodeCell:c};var d;!function(e){e.NextJS="nextjs-v11-components",e.NodeJS="nodejs-v16",e.Supabase="supabase"}(d||(d={}));const u={[d.NodeJS]:{id:"nodejs-v16",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nodejs-v16:latest",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[d.Supabase]:{id:"supabase",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/supabase",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[d.NextJS]:{id:"nextjs-v11-components",fileExtension:".tsx",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nextjs-v11-components",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"}};class g{constructor(){this.url="wss://orchestrator.usedevbook.com",this.logger=new o("WebSocketConnection"),this.handlers=[]}get state(){var e;return null===(e=this.client)||void 0===e?void 0:e.readyState}get isClosed(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSED}get isClosing(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSING}get isOpen(){if(void 0!==this.client)return this.client.readyState===this.client.OPEN}get isConnecting(){if(void 0!==this.client)return this.client.readyState===this.client.CONNECTING}subscribeHandler(e){return this.handlers.push(e),()=>{this.handlers=this.handlers.filter((t=>t!==e))}}connect(e){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(e||this.sessionID?(e?(this.logger.log(`Will try to connect to session "${e}"`),this.sessionID=e):!e&&this.sessionID&&this.logger.log(`Will try to connect to previous session "${this.sessionID}"`),this.client=new WebSocket(`${this.url}/session/ws/${this.sessionID}`),this.client.onopen=()=>this.handleOpen(this.sessionID),this.client.onmessage=e=>{this.logger.log("Received (raw)",{msg:e}),this.handleMessage(e)},this.client.onerror=e=>this.handleError(e),this.client.onclose=e=>this.handleClose(e)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(e){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",e),this._send(e)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",e)}close(){var e;this.logger.log("Closing connection"),null===(e=this.client)||void 0===e||e.close(1e3)}handleOpen(e){var t;this.logger.log("Connection opened",{readyState:null===(t=this.client)||void 0===t?void 0:t.readyState}),this.handlers.forEach((t=>t.onOpen(e)))}_send(e){var t,n;null===(t=this.client)||void 0===t||t.send((n=e,JSON.stringify(n,(()=>{const e=new WeakSet;return(t,n)=>{if("object"==typeof n&&null!==n){if(e.has(n))return;e.add(n)}return n}})(),2)))}handleClose(e){return i(this,void 0,void 0,(function*(){this.logger.log("Connection closed",e),this.handlers.forEach((e=>e.onClose())),this.logger.log("Will try to reconnect in 3s"),yield r(3e3),this.connect()}))}handleError(e){var t;this.logger.error("Connection error",e),null===(t=this.client)||void 0===t||t.close()}handleMessage(e){if(!e.data)return void this.logger.error("Message has empty data field",e);const t=JSON.parse(e.data);if(!t.type)return void this.logger.error("Message has no type",t);const n=t;Object.values(h.RunningEnvironment).includes(n.type)||Object.values(h.Runner).includes(n.type)||Object.values(h.CodeCell).includes(n.type)?n.type!==h.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((e=>e.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var v,m;!function(e){e.Ok="Ok",e.Terminated="Terminated"}(v||(v={}));class p{constructor(e,t=new Date){this.id=e,this.lastPing=t,this.logger=new o("RunnerSession"),this.url="https://orchestrator.usedevbook.com"}ping(){return i(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const e=JSON.stringify({sessionID:this.id});try{const t=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:e}),n=yield t.json();if(!t.ok)throw this.logger.error(t.headers,n),new Error("Non-OK response when trying to ping active Runner session");if(n.status===v.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(e){throw this.logger.error(e),new Error("Failed to ping active Runner session")}}))}}!function(e){e.Connected="Connected",e.Connecting="Connecting",e.Disconnected="Disconnected"}(m||(m={}));class f{constructor(e){this.conn=e,this.logger=new o("SessionManager"),this.url="https://orchestrator.usedevbook.com",this.isGettingSessionActive=!1,this.status=m.Disconnected,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(e){null===e?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${e}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",e))}reset(){this.logger.log("Reset"),this.status=m.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var e;return i(this,void 0,void 0,(function*(){if(!this.isGettingSessionActive)for(this.isGettingSessionActive=!0;;){this.status=m.Connecting;try{const t=this.cachedSessionID?`${this.url}/session/${this.cachedSessionID}`:`${this.url}/session`;this.cachedSessionID?this.logger.log(`Restoring old Runner session "${this.cachedSessionID}"`):this.logger.log("Acquiring new Runner session");const n=yield fetch(t),s=yield n.json();if(!n.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 3s",n.headers,s),yield r(3e3);continue}for(this.session=new p(s.sessionID),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=m.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.session.ping(),yield r(5e3)}catch(e){this.logger.error(`Failed to ping session "${this.session.id}"`,e);break}this.logger.log(`Stopped pinging session "${null===(e=this.session)||void 0===e?void 0:e.id}"`),this.session=void 0,this.status=m.Disconnected,this.conn.close()}catch(e){this.logger.error("Failed to acquire Runner session. Will try again in 3s",e),yield r(3e3)}}}))}}function D(e,t){for(var n=0,s=e.length-1;s>=0;s--){var i=e[s];"."===i?e.splice(s,1):".."===i?(e.splice(s,1),n++):n&&(e.splice(s,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}var C=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,I=function(e){return C.exec(e).slice(1)};function E(){for(var e="",t=!1,n=arguments.length-1;n>=-1&&!t;n--){var s=n>=0?arguments[n]:"/";if("string"!=typeof s)throw new TypeError("Arguments to path.resolve must be strings");s&&(e=s+"/"+e,t="/"===s.charAt(0))}return(t?"/":"")+(e=D(R(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function b(e){var t=y(e),n="/"===w(e,-1);return(e=D(R(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function y(e){return"/"===e.charAt(0)}var S={extname:function(e){return I(e)[3]},basename:function(e,t){var n=I(e)[2];return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n},dirname:function(e){var t=I(e),n=t[0],s=t[1];return n||s?(s&&(s=s.substr(0,s.length-1)),n+s):"."},sep:"/",delimiter:":",relative:function(e,t){function n(e){for(var t=0;t<e.length&&""===e[t];t++);for(var n=e.length-1;n>=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=E(e).substr(1),t=E(t).substr(1);for(var s=n(e.split("/")),i=n(t.split("/")),o=Math.min(s.length,i.length),r=o,a=0;a<o;a++)if(s[a]!==i[a]){r=a;break}var l=[];for(a=r;a<s.length;a++)l.push("..");return(l=l.concat(i.slice(r))).join("/")},join:function(){var e=Array.prototype.slice.call(arguments,0);return b(R(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))},isAbsolute:y,normalize:b,resolve:E};function R(e,t){if(e.filter)return e.filter(t);for(var n=[],s=0;s<e.length;s++)t(e[s],s,e)&&n.push(e[s]);return n}var w="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)};function x(e){return function(e){const t=Array.from(e).reduce(((e,t)=>0|31*e+t.charCodeAt(0)),0);return("0000000"+(t>>>0).toString(16)).slice(-8)}(e)}class _{constructor(e,t){this.contextID=e,this.templateID=t,this.isReady=!1,this.id=`${e}_${x(t)}`,this.template=u[this.templateID]}}function k(e,{environmentID:t,template:n}){const s={type:h.RunningEnvironment.Start,payload:{environmentID:t,template:n}};e.send(s)}function F(e,{environmentID:t,executionID:n,command:s}){const i={type:h.RunningEnvironment.ExecCmd,payload:{environmentID:t,executionID:n,command:s}};e.send(i)}function O(e,{environmentID:t,path:n,content:s}){const i={type:h.RunningEnvironment.WriteFile,payload:{environmentID:t,path:n,content:s}};e.send(i)}class ${constructor(e){this.opts=e,this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.envs=[],this.logger=new o("EvaluationContext",e.debug),this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:this.handleConnectionOpen.bind(this),onMessage:this.handleConnectionMessage.bind(this),onClose:this.handleConnectionClose.bind(this)}),this.opts.conn.isOpen&&this.opts.conn.sessionID&&this.handleConnectionOpen(this.opts.conn.sessionID),this.opts.conn.isClosed&&this.handleConnectionClose()}get contextID(){return this.opts.contextID}restart(){return this.logger.log("Restart",this.opts.conn.sessionID),this.envs.forEach((e=>{e.isReady=!1,k(this.opts.conn,{environmentID:e.id,template:e.template})}))}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.envs=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({templateID:e,path:t}){return i(this,void 0,void 0,(function*(){this.logger.log("Get file",{templateID:e,filepath:t});const n=this.getRunningEnvironment({templateID:e});if(!n)return void this.logger.error("Environment not found",{templateID:e,filepath:t});if(!n.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:t});let s;const i=new Promise(((e,t)=>{s=e,setTimeout((()=>{t("Timeout")}),1e4)})),o=e=>{e.path.endsWith(t)&&s(e.content)};this.subscribeFileContent(o),function(e,{environmentID:t,path:n}){const s={type:h.RunningEnvironment.GetFile,payload:{environmentID:t,path:n}};e.send(s)}(this.opts.conn,{path:t,environmentID:n.id});try{return yield i}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(o)}}))}updateFile({templateID:e,path:t,content:n}){return i(this,void 0,void 0,(function*(){this.logger.log("Update file",{templateID:e,filepath:t});const s=this.getRunningEnvironment({templateID:e});if(!s)return void this.logger.error("Environment not found",{templateID:e,filepath:t});if(!s.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:t});let i;const o=new Promise(((e,t)=>{i=e,setTimeout((()=>{t("Timeout")}),1e4)})),r=e=>{e.path.endsWith(t)&&i()};this.subscribeFSWrite(r),O(this.opts.conn,{environmentID:s.id,path:t,content:n});try{yield o}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(r)}}))}executeCode({templateID:e,executionID:t,code:n}){return i(this,void 0,void 0,(function*(){this.logger.log("Execute code",{templateID:e,executionID:t});const s=u[e].toCommand;if(void 0===s)return;const i=this.getRunningEnvironment({templateID:e});if(!i)return void this.logger.error("Environment not found",{templateID:e,executionID:t});if(!i.isReady)return void this.logger.error("Environment is not ready",{templateID:e,executionID:t});const o=u[e].fileExtension,r=`${t}${o}`,a=S.join("/src",r);O(this.opts.conn,{environmentID:i.id,path:a,content:n});const l=s(S.join(u[e].root_dir,a));F(this.opts.conn,{environmentID:i.id,executionID:t,command:l})}))}executeCommand({templateID:e,executionID:t,command:n}){this.logger.log("Execute shell command",{templateID:e,executionID:t,command:n});const s=this.getRunningEnvironment({templateID:e});s?s.isReady?F(this.opts.conn,{environmentID:s.id,executionID:t,command:n}):this.logger.error("Environment is not ready",{templateID:e,executionID:t,command:n}):this.logger.error("Environment not found",{templateID:e,executionID:t,command:n})}getRunningEnvironment({templateID:e}){return this.envs.find((t=>t.templateID===e))}createRunningEnvironment({templateID:e}){var t,n;this.logger.log("Creating running environment",{templateID:e});if(this.getRunningEnvironment({templateID:e}))return;const s=new _(this.contextID,e);this.envs.push(s),k(this.opts.conn,{environmentID:s.id,template:s.template}),null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)}subscribeFileContent(e){this.fileContentSubscribers.push(e)}unsubscribeFileContent(e){const t=this.fileContentSubscribers.indexOf(e);t>-1&&this.fileContentSubscribers.splice(t,1)}subscribeFSWrite(e){this.fsWriteSubscribers.push(e)}unsubscribeFSWrite(e){const t=this.fsWriteSubscribers.indexOf(e);t>-1&&this.fsWriteSubscribers.splice(t,1)}handleConnectionOpen(e){var t,n;this.restart(),null===(n=(t=this.opts).onSessionChange)||void 0===n||n.call(t,{status:m.Connected,sessionID:e})}handleConnectionClose(){var e,t;null===(t=(e=this.opts).onSessionChange)||void 0===t||t.call(e,{status:m.Connecting})}handleConnectionMessage(e){switch(this.logger.log("Handling message from remote Runner",{message:e}),e.type){case h.RunningEnvironment.StartAck:{const t=e;this.vmenv_handleStartAck(t.payload);break}case h.RunningEnvironment.CmdOut:{const t=e;this.vmenv_handleCmdOut(t.payload);break}case h.RunningEnvironment.CmdExit:{const t=e;this.vmenv_handleCmdExit(t.payload);break}case h.RunningEnvironment.FSEventWrite:{const t=e;this.vmenv_handleFSEventWrite(t.payload);break}case h.RunningEnvironment.FileContent:{const t=e;this.vmenv_handleFileContent(t.payload);break}default:this.logger.warn("Unknown message type",{message:e})}}vmenv_handleCmdExit(e){var t,n;void 0!==e.error&&(null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,{environmentID:e.environmentID,executionID:e.executionID,stderr:e.error}))}vmenv_handleFSEventWrite(e){this.logger.log('[vmenv] Handling "FSEventWrite"',e);this.envs.find((t=>t.id===e.environmentID))?this.fsWriteSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleFileContent(e){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:e.environmentID,path:e.path});this.envs.find((t=>t.id===e.environmentID))?this.fileContentSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleStartAck(e){var t,n;this.logger.log('[vmenv] Handling "StartAck"',{payload:e});const s=this.envs.find((t=>t.id===e.environmentID));s?(s.isReady=!0,null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)):this.logger.warn("Environment not found",{payload:e})}vmenv_handleCmdOut(e){var t,n;this.logger.log('[vmenv] Handling "CmdOut"',e),null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,e)}}class j{constructor(){this.logger=new o("Runner"),this.conn=new g,this.sessManager=new f(this.conn)}static get obj(){return j._obj||(j._obj=new j)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(e){return new $(Object.assign(Object.assign({},e),{conn:this.conn}))}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const W=(N="1234567890abcdefghijklmnopqrstuvwxyz",M=6,()=>{let e="",t=M;for(;t--;)e+=N[Math.random()*N.length|0];return e});var N,M,A;!function(e){e[e.Disconnected=0]="Disconnected",e[e.Connecting=1]="Connecting",e[e.Connected=2]="Connected"}(A||(A={}));class T{constructor(e){this.opts=e,this.contextID="default",this.executionID=W(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=m.Disconnected,this._status=A.Disconnected;const t=this.getURL.bind(this),n=()=>this.opts.env,s=()=>this.executionID,i=e=>this.isEnvReady=e,o=e=>this.sessionStatus=e,r=e=>this.sessionID=e;this.context=j.obj.createContext({debug:e.debug,contextID:this.contextID,onEnvChange(s){var o;s.templateID===n()&&(i(s.isReady),null===(o=e.onURLChange)||void 0===o||o.call(e,t))},onSessionChange({status:n,sessionID:s}){var i;r(s),o(n),null===(i=e.onURLChange)||void 0===i||i.call(e,t)},onCmdOut(t){var n,i;t.executionID===s()&&(void 0!==t.stdout&&(null===(n=e.onStdout)||void 0===n||n.call(e,t.stdout)),void 0!==t.stderr&&(null===(i=e.onStderr)||void 0===i||i.call(e,t.stderr)))}}),this.context.createRunningEnvironment({templateID:e.env})}get sessionID(){return this._sessionID}set sessionID(e){this._sessionID=e}get isDestroyed(){return this._isDestroyed}set isDestroyed(e){this._isDestroyed=e,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(e){this._isEnvReady=e,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(e){this._sessionStatus=e,this.updateStatus()}get status(){return this._status}set status(e){var t,n;this._status=e,null===(n=(t=this.opts).onStatusChange)||void 0===n||n.call(t,e)}get fs(){return{get:this.getFile.bind(this),write:this.writeFile.bind(this)}}getURL(e){if(this.status!==A.Connected)return;const t=this.sessionID;if(!this.sessionID)return;const n=this.context.getRunningEnvironment({templateID:this.opts.env});return(null==n?void 0:n.isReady)?`https://${e}-${n.id}-${t}.o.usedevbook.com`:void 0}runCmd(e){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");this.executionID=W(),this.context.executeCommand({templateID:this.opts.env,executionID:this.executionID,command:e})}runCode(e){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");this.executionID=W(),this.context.executeCode({templateID:this.opts.env,executionID:this.executionID,code:e})}destroy(){this.context.destroy(),this.isDestroyed=!0}getFile(e){return i(this,void 0,void 0,(function*(){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({templateID:this.opts.env,path:e})}))}writeFile(e,t){return i(this,void 0,void 0,(function*(){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({templateID:this.opts.env,path:e,content:t})}))}updateStatus(){if(this.isDestroyed)return void(this.status!==A.Disconnected&&(this.status=A.Disconnected));let e;switch(this.sessionStatus){case m.Disconnected:e=A.Disconnected;break;case m.Connecting:e=A.Connecting;break;case m.Connected:if(!this.isEnvReady){e=A.Connecting;break}e=A.Connected}this.status=e}}function P({env:o,debug:r,port:a}){const[l,c]=e(),[h,d]=e(A.Disconnected),[u,g]=e([]),[v,m]=e([]),[p,f]=e(),D=t((()=>l?l.fs:{get(){return i(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))},write(){return i(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))}}),[l]),C=n((e=>{l&&(m([]),g([]),l.runCmd(e))}),[l]),I=n((e=>{l&&(m([]),g([]),l.runCode(e))}),[l]);return s((function(){const e=new T({debug:r,env:o,onStatusChange(e){d(e)},onStderr(e){g((t=>[...t,e]))},onStdout(e){m((t=>[...t,e]))},onURLChange(e){a&&f(e(a))}});return m([]),g([]),f(void 0),c(e),()=>{e.destroy()}}),[o,r,a]),{stderr:u,stdout:v,runCmd:C,runCode:I,status:h,fs:D,url:p}}export{T as Devbook,A as DevbookStatus,d as Env,P as useDevbook};
***************************************************************************** */function i(e,n,t,s){return new(t||(t=Promise))((function(i,o){function r(e){try{l(s.next(e))}catch(e){o(e)}}function a(e){try{l(s.throw(e))}catch(e){o(e)}}function l(e){var n;e.done?i(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(r,a)}l((s=s.apply(e,n||[])).next())}))}class o{constructor(e,n=!1){this.logID=e,this.isEnabled=n}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...e){this.isEnabled&&console.log(`[${this.id()}]`,...e)}warn(...e){this.isEnabled&&console.warn(`[${this.id()}]`,...e)}error(...e){console.error(`[${this.id()} ERROR]`,...e)}}function r(e){return new Promise((n=>setTimeout(n,e)))}var a,l,c;!function(e){e.Error="Runner.Error"}(a||(a={})),function(e){e.Start="RunningEnvironment.Start",e.StartAck="RunningEnvironment.StartAck",e.Eval="RunningEnvironment.Eval",e.FSEventCreate="RunningEnvironment.FSEventCreate",e.FSEventRemove="RunningEnvironment.FSEventRemove",e.FSEventWrite="RunningEnvironment.FSEventWrite",e.CreateDir="RunningEnvironment.CreateDir",e.ListDir="RunningEnvironment.ListDir",e.WriteFile="RunningEnvironment.WriteFile",e.GetFile="RunningEnvironment.GetFile",e.RemoveFile="RunningEnvironment.RemoveFile",e.DirContent="RunningEnvironment.DirContent",e.FileContent="RunningEnvironment.FileContent",e.Stdout="RunningEnvironment.Stdout",e.Stderr="RunningEnvironment.Stderr",e.ExecCmd="RunningEnvironment.ExecCmd",e.KillCmd="RunningEnvironment.KillCmd",e.ListRunningCmds="RunningEnvironment.ListRunningCmds",e.CmdOut="RunningEnvironment.CmdOut",e.CmdExit="RunningEnvironment.CmdExit",e.RunningCmds="RunningEnvironment.RunningCmds",e.RunCode="RunningEnvironment.Run"}(l||(l={})),function(e){e.Error="CodeCell.Error"}(c||(c={}));const h={Runner:a,RunningEnvironment:l,CodeCell:c};var d;!function(e){e.NextJS="nextjs-v11-components",e.NodeJS="nodejs-v16",e.Supabase="supabase",e.BananaNode="banana-node",e.BananaPython="banana-python"}(d||(d={}));const u={[d.NodeJS]:{id:"nodejs-v16",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nodejs-v16:latest",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[d.Supabase]:{id:"supabase",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/supabase",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[d.NextJS]:{id:"nextjs-v11-components",fileExtension:".tsx",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nextjs-v11-components",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"},[d.BananaNode]:{id:"banana-node",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/banana-node",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"},[d.BananaPython]:{id:"banana-python",fileExtension:".py",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/banana-python",root_dir:"/home/runner",code_cells_dir:"/home/runner"}};class g{constructor(){this.url="wss://orchestrator.usedevbook.com",this.logger=new o("WebSocketConnection"),this.handlers=[]}get state(){var e;return null===(e=this.client)||void 0===e?void 0:e.readyState}get isClosed(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSED}get isClosing(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSING}get isOpen(){if(void 0!==this.client)return this.client.readyState===this.client.OPEN}get isConnecting(){if(void 0!==this.client)return this.client.readyState===this.client.CONNECTING}subscribeHandler(e){return this.handlers.push(e),()=>{this.handlers=this.handlers.filter((n=>n!==e))}}connect(e){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(e||this.sessionID?(e?(this.logger.log(`Will try to connect to session "${e}"`),this.sessionID=e):!e&&this.sessionID&&this.logger.log(`Will try to connect to previous session "${this.sessionID}"`),this.client=new WebSocket(`${this.url}/session/ws/${this.sessionID}`),this.client.onopen=()=>this.handleOpen(this.sessionID),this.client.onmessage=e=>{this.logger.log("Received (raw)",{msg:e}),this.handleMessage(e)},this.client.onerror=e=>this.handleError(e),this.client.onclose=e=>this.handleClose(e)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(e){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",e),this._send(e)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",e)}close(){var e;this.logger.log("Closing connection"),null===(e=this.client)||void 0===e||e.close(1e3)}handleOpen(e){var n;this.logger.log("Connection opened",{readyState:null===(n=this.client)||void 0===n?void 0:n.readyState}),this.handlers.forEach((n=>n.onOpen(e)))}_send(e){var n,t;null===(n=this.client)||void 0===n||n.send((t=e,JSON.stringify(t,(()=>{const e=new WeakSet;return(n,t)=>{if("object"==typeof t&&null!==t){if(e.has(t))return;e.add(t)}return t}})(),2)))}handleClose(e){return i(this,void 0,void 0,(function*(){this.logger.log("Connection closed",e),this.handlers.forEach((e=>e.onClose())),this.logger.log("Will try to reconnect in 3s"),yield r(3e3),this.connect()}))}handleError(e){var n;this.logger.error("Connection error",e),null===(n=this.client)||void 0===n||n.close()}handleMessage(e){if(!e.data)return void this.logger.error("Message has empty data field",e);const n=JSON.parse(e.data);if(!n.type)return void this.logger.error("Message has no type",n);const t=n;Object.values(h.RunningEnvironment).includes(t.type)||Object.values(h.Runner).includes(t.type)||Object.values(h.CodeCell).includes(t.type)?t.type!==h.Runner.Error?(this.logger.log("Received (parsed)",t),this.handlers.forEach((e=>e.onMessage(t)))):this.logger.error("Runner error",t):this.logger.error('Message "type" field has unexpected value',t)}}var v,m;!function(e){e.Ok="Ok",e.Terminated="Terminated"}(v||(v={}));class p{constructor(e,n=new Date){this.id=e,this.lastPing=n,this.logger=new o("RunnerSession"),this.url="https://orchestrator.usedevbook.com"}ping(){return i(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const e=JSON.stringify({sessionID:this.id});try{const n=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:e}),t=yield n.json();if(!n.ok)throw this.logger.error(n.headers,t),new Error("Non-OK response when trying to ping active Runner session");if(t.status===v.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(e){throw this.logger.error(e),new Error("Failed to ping active Runner session")}}))}}!function(e){e.Connected="Connected",e.Connecting="Connecting",e.Disconnected="Disconnected"}(m||(m={}));class f{constructor(e){this.conn=e,this.logger=new o("SessionManager"),this.url="https://orchestrator.usedevbook.com",this.isGettingSessionActive=!1,this.status=m.Disconnected,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(e){null===e?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${e}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",e))}reset(){this.logger.log("Reset"),this.status=m.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var e;return i(this,void 0,void 0,(function*(){if(!this.isGettingSessionActive)for(this.isGettingSessionActive=!0;;){this.status=m.Connecting;try{const n=this.cachedSessionID?`${this.url}/session/${this.cachedSessionID}`:`${this.url}/session`;this.cachedSessionID?this.logger.log(`Restoring old Runner session "${this.cachedSessionID}"`):this.logger.log("Acquiring new Runner session");const t=yield fetch(n),s=yield t.json();if(!t.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 3s",t.headers,s),yield r(3e3);continue}for(this.session=new p(s.sessionID),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=m.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.session.ping(),yield r(5e3)}catch(e){this.logger.error(`Failed to ping session "${this.session.id}"`,e);break}this.logger.log(`Stopped pinging session "${null===(e=this.session)||void 0===e?void 0:e.id}"`),this.session=void 0,this.status=m.Disconnected,this.conn.close()}catch(e){this.logger.error("Failed to acquire Runner session. Will try again in 3s",e),yield r(3e3)}}}))}}function D(e,n){for(var t=0,s=e.length-1;s>=0;s--){var i=e[s];"."===i?e.splice(s,1):".."===i?(e.splice(s,1),t++):t&&(e.splice(s,1),t--)}if(n)for(;t--;t)e.unshift("..");return e}var C=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,b=function(e){return C.exec(e).slice(1)};function E(){for(var e="",n=!1,t=arguments.length-1;t>=-1&&!n;t--){var s=t>=0?arguments[t]:"/";if("string"!=typeof s)throw new TypeError("Arguments to path.resolve must be strings");s&&(e=s+"/"+e,n="/"===s.charAt(0))}return(n?"/":"")+(e=D(R(e.split("/"),(function(e){return!!e})),!n).join("/"))||"."}function I(e){var n=y(e),t="/"===x(e,-1);return(e=D(R(e.split("/"),(function(e){return!!e})),!n).join("/"))||n||(e="."),e&&t&&(e+="/"),(n?"/":"")+e}function y(e){return"/"===e.charAt(0)}var S={extname:function(e){return b(e)[3]},basename:function(e,n){var t=b(e)[2];return n&&t.substr(-1*n.length)===n&&(t=t.substr(0,t.length-n.length)),t},dirname:function(e){var n=b(e),t=n[0],s=n[1];return t||s?(s&&(s=s.substr(0,s.length-1)),t+s):"."},sep:"/",delimiter:":",relative:function(e,n){function t(e){for(var n=0;n<e.length&&""===e[n];n++);for(var t=e.length-1;t>=0&&""===e[t];t--);return n>t?[]:e.slice(n,t-n+1)}e=E(e).substr(1),n=E(n).substr(1);for(var s=t(e.split("/")),i=t(n.split("/")),o=Math.min(s.length,i.length),r=o,a=0;a<o;a++)if(s[a]!==i[a]){r=a;break}var l=[];for(a=r;a<s.length;a++)l.push("..");return(l=l.concat(i.slice(r))).join("/")},join:function(){var e=Array.prototype.slice.call(arguments,0);return I(R(e,(function(e,n){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))},isAbsolute:y,normalize:I,resolve:E};function R(e,n){if(e.filter)return e.filter(n);for(var t=[],s=0;s<e.length;s++)n(e[s],s,e)&&t.push(e[s]);return t}var x="b"==="ab".substr(-1)?function(e,n,t){return e.substr(n,t)}:function(e,n,t){return n<0&&(n=e.length+n),e.substr(n,t)};function w(e){return function(e){const n=Array.from(e).reduce(((e,n)=>0|31*e+n.charCodeAt(0)),0);return("0000000"+(n>>>0).toString(16)).slice(-8)}(e)}class _{constructor(e,n){this.contextID=e,this.templateID=n,this.isReady=!1,this.id=`${e}_${w(n)}`,this.template=u[this.templateID]}}function k(e,{environmentID:n,template:t}){const s={type:h.RunningEnvironment.Start,payload:{environmentID:n,template:t}};e.send(s)}function F(e,{environmentID:n,executionID:t,command:s}){const i={type:h.RunningEnvironment.ExecCmd,payload:{environmentID:n,executionID:t,command:s}};e.send(i)}function O(e,{environmentID:n,path:t,content:s}){const i={type:h.RunningEnvironment.WriteFile,payload:{environmentID:n,path:t,content:s}};e.send(i)}class ${constructor(e){this.opts=e,this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.envs=[],this.logger=new o("EvaluationContext",e.debug),this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:this.handleConnectionOpen.bind(this),onMessage:this.handleConnectionMessage.bind(this),onClose:this.handleConnectionClose.bind(this)}),this.opts.conn.isOpen&&this.opts.conn.sessionID&&this.handleConnectionOpen(this.opts.conn.sessionID),this.opts.conn.isClosed&&this.handleConnectionClose()}get contextID(){return this.opts.contextID}restart(){return this.logger.log("Restart",this.opts.conn.sessionID),this.envs.forEach((e=>{e.isReady=!1,k(this.opts.conn,{environmentID:e.id,template:e.template})}))}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.envs=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({templateID:e,path:n}){return i(this,void 0,void 0,(function*(){this.logger.log("Get file",{templateID:e,filepath:n});const t=this.getRunningEnvironment({templateID:e});if(!t)return void this.logger.error("Environment not found",{templateID:e,filepath:n});if(!t.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:n});let s;const i=new Promise(((e,n)=>{s=e,setTimeout((()=>{n("Timeout")}),1e4)})),o=e=>{e.path.endsWith(n)&&s(e.content)};this.subscribeFileContent(o),function(e,{environmentID:n,path:t}){const s={type:h.RunningEnvironment.GetFile,payload:{environmentID:n,path:t}};e.send(s)}(this.opts.conn,{path:n,environmentID:t.id});try{return yield i}catch(e){throw new Error(`Error retrieving file ${n}: ${e}`)}finally{this.unsubscribeFileContent(o)}}))}updateFile({templateID:e,path:n,content:t}){return i(this,void 0,void 0,(function*(){this.logger.log("Update file",{templateID:e,filepath:n});const s=this.getRunningEnvironment({templateID:e});if(!s)return void this.logger.error("Environment not found",{templateID:e,filepath:n});if(!s.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:n});let i;const o=new Promise(((e,n)=>{i=e,setTimeout((()=>{n("Timeout")}),1e4)})),r=e=>{e.path.endsWith(n)&&i()};this.subscribeFSWrite(r),O(this.opts.conn,{environmentID:s.id,path:n,content:t});try{yield o}catch(e){throw new Error(`File ${n} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(r)}}))}executeCode({templateID:e,executionID:n,code:t}){return i(this,void 0,void 0,(function*(){this.logger.log("Execute code",{templateID:e,executionID:n});const s=u[e].toCommand;if(void 0===s)return;const i=this.getRunningEnvironment({templateID:e});if(!i)return void this.logger.error("Environment not found",{templateID:e,executionID:n});if(!i.isReady)return void this.logger.error("Environment is not ready",{templateID:e,executionID:n});const o=u[e].fileExtension,r=`${n}${o}`,a=S.join("/src",r);O(this.opts.conn,{environmentID:i.id,path:a,content:t});const l=s(S.join(u[e].root_dir,a));F(this.opts.conn,{environmentID:i.id,executionID:n,command:l})}))}executeCommand({templateID:e,executionID:n,command:t}){this.logger.log("Execute shell command",{templateID:e,executionID:n,command:t});const s=this.getRunningEnvironment({templateID:e});s?s.isReady?F(this.opts.conn,{environmentID:s.id,executionID:n,command:t}):this.logger.error("Environment is not ready",{templateID:e,executionID:n,command:t}):this.logger.error("Environment not found",{templateID:e,executionID:n,command:t})}getRunningEnvironment({templateID:e}){return this.envs.find((n=>n.templateID===e))}createRunningEnvironment({templateID:e}){var n,t;this.logger.log("Creating running environment",{templateID:e});if(this.getRunningEnvironment({templateID:e}))return;const s=new _(this.contextID,e);this.envs.push(s),k(this.opts.conn,{environmentID:s.id,template:s.template}),null===(t=(n=this.opts).onEnvChange)||void 0===t||t.call(n,s)}subscribeFileContent(e){this.fileContentSubscribers.push(e)}unsubscribeFileContent(e){const n=this.fileContentSubscribers.indexOf(e);n>-1&&this.fileContentSubscribers.splice(n,1)}subscribeFSWrite(e){this.fsWriteSubscribers.push(e)}unsubscribeFSWrite(e){const n=this.fsWriteSubscribers.indexOf(e);n>-1&&this.fsWriteSubscribers.splice(n,1)}handleConnectionOpen(e){var n,t;this.restart(),null===(t=(n=this.opts).onSessionChange)||void 0===t||t.call(n,{status:m.Connected,sessionID:e})}handleConnectionClose(){var e,n;null===(n=(e=this.opts).onSessionChange)||void 0===n||n.call(e,{status:m.Connecting})}handleConnectionMessage(e){switch(this.logger.log("Handling message from remote Runner",{message:e}),e.type){case h.RunningEnvironment.StartAck:{const n=e;this.vmenv_handleStartAck(n.payload);break}case h.RunningEnvironment.CmdOut:{const n=e;this.vmenv_handleCmdOut(n.payload);break}case h.RunningEnvironment.CmdExit:{const n=e;this.vmenv_handleCmdExit(n.payload);break}case h.RunningEnvironment.FSEventWrite:{const n=e;this.vmenv_handleFSEventWrite(n.payload);break}case h.RunningEnvironment.FileContent:{const n=e;this.vmenv_handleFileContent(n.payload);break}default:this.logger.warn("Unknown message type",{message:e})}}vmenv_handleCmdExit(e){var n,t;void 0!==e.error&&(null===(t=(n=this.opts).onCmdOut)||void 0===t||t.call(n,{environmentID:e.environmentID,executionID:e.executionID,stderr:e.error}))}vmenv_handleFSEventWrite(e){this.logger.log('[vmenv] Handling "FSEventWrite"',e);this.envs.find((n=>n.id===e.environmentID))?this.fsWriteSubscribers.forEach((n=>n(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleFileContent(e){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:e.environmentID,path:e.path});this.envs.find((n=>n.id===e.environmentID))?this.fileContentSubscribers.forEach((n=>n(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleStartAck(e){var n,t;this.logger.log('[vmenv] Handling "StartAck"',{payload:e});const s=this.envs.find((n=>n.id===e.environmentID));s?(s.isReady=!0,null===(t=(n=this.opts).onEnvChange)||void 0===t||t.call(n,s)):this.logger.warn("Environment not found",{payload:e})}vmenv_handleCmdOut(e){var n,t;this.logger.log('[vmenv] Handling "CmdOut"',e),null===(t=(n=this.opts).onCmdOut)||void 0===t||t.call(n,e)}}class j{constructor(){this.logger=new o("Runner"),this.conn=new g,this.sessManager=new f(this.conn)}static get obj(){return j._obj||(j._obj=new j)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(e){return new $(Object.assign(Object.assign({},e),{conn:this.conn}))}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const W=(N="1234567890abcdefghijklmnopqrstuvwxyz",M=6,()=>{let e="",n=M;for(;n--;)e+=N[Math.random()*N.length|0];return e});var N,M,A;!function(e){e[e.Disconnected=0]="Disconnected",e[e.Connecting=1]="Connecting",e[e.Connected=2]="Connected"}(A||(A={}));class T{constructor(e){this.opts=e,this.contextID="default",this.executionID=W(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=m.Disconnected,this._status=A.Disconnected;const n=this.getURL.bind(this),t=()=>this.opts.env,s=()=>this.executionID,i=e=>this.isEnvReady=e,o=e=>this.sessionStatus=e,r=e=>this.sessionID=e;this.context=j.obj.createContext({debug:e.debug,contextID:this.contextID,onEnvChange(s){var o;s.templateID===t()&&(i(s.isReady),null===(o=e.onURLChange)||void 0===o||o.call(e,n))},onSessionChange({status:t,sessionID:s}){var i;r(s),o(t),null===(i=e.onURLChange)||void 0===i||i.call(e,n)},onCmdOut(n){var t,i;n.executionID===s()&&(void 0!==n.stdout&&(null===(t=e.onStdout)||void 0===t||t.call(e,n.stdout)),void 0!==n.stderr&&(null===(i=e.onStderr)||void 0===i||i.call(e,n.stderr)))}}),this.context.createRunningEnvironment({templateID:e.env})}get sessionID(){return this._sessionID}set sessionID(e){this._sessionID=e}get isDestroyed(){return this._isDestroyed}set isDestroyed(e){this._isDestroyed=e,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(e){this._isEnvReady=e,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(e){this._sessionStatus=e,this.updateStatus()}get status(){return this._status}set status(e){var n,t;this._status=e,null===(t=(n=this.opts).onStatusChange)||void 0===t||t.call(n,e)}get fs(){return{get:this.getFile.bind(this),write:this.writeFile.bind(this)}}getURL(e){if(this.status!==A.Connected)return;const n=this.sessionID;if(!this.sessionID)return;const t=this.context.getRunningEnvironment({templateID:this.opts.env});return(null==t?void 0:t.isReady)?`https://${e}-${t.id}-${n}.o.usedevbook.com`:void 0}runCmd(e){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");this.executionID=W(),this.context.executeCommand({templateID:this.opts.env,executionID:this.executionID,command:e})}runCode(e){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");this.executionID=W(),this.context.executeCode({templateID:this.opts.env,executionID:this.executionID,code:e})}destroy(){this.context.destroy(),this.isDestroyed=!0}getFile(e){return i(this,void 0,void 0,(function*(){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({templateID:this.opts.env,path:e})}))}writeFile(e,n){return i(this,void 0,void 0,(function*(){if(this.status!==A.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({templateID:this.opts.env,path:e,content:n})}))}updateStatus(){if(this.isDestroyed)return void(this.status!==A.Disconnected&&(this.status=A.Disconnected));let e;switch(this.sessionStatus){case m.Disconnected:e=A.Disconnected;break;case m.Connecting:e=A.Connecting;break;case m.Connected:if(!this.isEnvReady){e=A.Connecting;break}e=A.Connected}this.status=e}}function P({env:o,debug:r,port:a}){const[l,c]=e(),[h,d]=e(A.Disconnected),[u,g]=e([]),[v,m]=e([]),[p,f]=e(),D=n((()=>l?l.fs:{get(){return i(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))},write(){return i(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))}}),[l]),C=t((e=>{l&&(m([]),g([]),l.runCmd(e))}),[l]),b=t((e=>{l&&(m([]),g([]),l.runCode(e))}),[l]);return s((function(){const e=new T({debug:r,env:o,onStatusChange(e){d(e)},onStderr(e){g((n=>[...n,e]))},onStdout(e){m((n=>[...n,e]))},onURLChange(e){a&&f(e(a))}});return m([]),g([]),f(void 0),c(e),()=>{e.destroy()}}),[o,r,a]),{stderr:u,stdout:v,runCmd:C,runCode:b,status:h,fs:D,url:p}}export{T as Devbook,A as DevbookStatus,d as Env,P as useDevbook};
//# sourceMappingURL=index.js.map

@@ -7,7 +7,7 @@ import { TemplateConfig } from '../../common-ts/TemplateConfig';

/**
* Runtime environment that uses NextJS server with hot-reloading.
* Runtime environment that uses NextJS server with hot-reloading.
*/
NextJS = "nextjs-v11-components",
/**
* Runtime environment that supports executing JS code with NodeJS 16 runtime.
* Runtime environment that supports executing JS code with NodeJS 16 runtime.
*/

@@ -18,3 +18,11 @@ NodeJS = "nodejs-v16",

*/
Supabase = "supabase"
Supabase = "supabase",
/**
* Runtime environment for the Banana Node API.
*/
BananaNode = "banana-node",
/**
* Runtime environment for the Banana Python API.
*/
BananaPython = "banana-python"
}

@@ -21,0 +29,0 @@ export declare const templates: {

@@ -15,3 +15,3 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@devbookhq/sdk"]={},e.react)}(this,(function(e,t){"use strict";

PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function n(e,t,n,s){return new(n||(n=Promise))((function(i,o){function r(e){try{l(s.next(e))}catch(e){o(e)}}function a(e){try{l(s.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((s=s.apply(e,t||[])).next())}))}class s{constructor(e,t=!1){this.logID=e,this.isEnabled=t}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...e){this.isEnabled&&console.log(`[${this.id()}]`,...e)}warn(...e){this.isEnabled&&console.warn(`[${this.id()}]`,...e)}error(...e){console.error(`[${this.id()} ERROR]`,...e)}}function i(e){return new Promise((t=>setTimeout(t,e)))}var o,r,a;!function(e){e.Error="Runner.Error"}(o||(o={})),function(e){e.Start="RunningEnvironment.Start",e.StartAck="RunningEnvironment.StartAck",e.Eval="RunningEnvironment.Eval",e.FSEventCreate="RunningEnvironment.FSEventCreate",e.FSEventRemove="RunningEnvironment.FSEventRemove",e.FSEventWrite="RunningEnvironment.FSEventWrite",e.CreateDir="RunningEnvironment.CreateDir",e.ListDir="RunningEnvironment.ListDir",e.WriteFile="RunningEnvironment.WriteFile",e.GetFile="RunningEnvironment.GetFile",e.RemoveFile="RunningEnvironment.RemoveFile",e.DirContent="RunningEnvironment.DirContent",e.FileContent="RunningEnvironment.FileContent",e.Stdout="RunningEnvironment.Stdout",e.Stderr="RunningEnvironment.Stderr",e.ExecCmd="RunningEnvironment.ExecCmd",e.KillCmd="RunningEnvironment.KillCmd",e.ListRunningCmds="RunningEnvironment.ListRunningCmds",e.CmdOut="RunningEnvironment.CmdOut",e.CmdExit="RunningEnvironment.CmdExit",e.RunningCmds="RunningEnvironment.RunningCmds",e.RunCode="RunningEnvironment.Run"}(r||(r={})),function(e){e.Error="CodeCell.Error"}(a||(a={}));const l={Runner:o,RunningEnvironment:r,CodeCell:a},c="orchestrator.usedevbook.com",h="dbk_sdk_session_id",u=3e3;var d;e.Env=void 0,(d=e.Env||(e.Env={})).NextJS="nextjs-v11-components",d.NodeJS="nodejs-v16",d.Supabase="supabase";const g={[e.Env.NodeJS]:{id:"nodejs-v16",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nodejs-v16:latest",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[e.Env.Supabase]:{id:"supabase",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/supabase",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[e.Env.NextJS]:{id:"nextjs-v11-components",fileExtension:".tsx",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nextjs-v11-components",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"}};class v{constructor(){this.url=`wss://${c}`,this.logger=new s("WebSocketConnection"),this.handlers=[]}get state(){var e;return null===(e=this.client)||void 0===e?void 0:e.readyState}get isClosed(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSED}get isClosing(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSING}get isOpen(){if(void 0!==this.client)return this.client.readyState===this.client.OPEN}get isConnecting(){if(void 0!==this.client)return this.client.readyState===this.client.CONNECTING}subscribeHandler(e){return this.handlers.push(e),()=>{this.handlers=this.handlers.filter((t=>t!==e))}}connect(e){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(e||this.sessionID?(e?(this.logger.log(`Will try to connect to session "${e}"`),this.sessionID=e):!e&&this.sessionID&&this.logger.log(`Will try to connect to previous session "${this.sessionID}"`),this.client=new WebSocket(`${this.url}/session/ws/${this.sessionID}`),this.client.onopen=()=>this.handleOpen(this.sessionID),this.client.onmessage=e=>{this.logger.log("Received (raw)",{msg:e}),this.handleMessage(e)},this.client.onerror=e=>this.handleError(e),this.client.onclose=e=>this.handleClose(e)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(e){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",e),this._send(e)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",e)}close(){var e;this.logger.log("Closing connection"),null===(e=this.client)||void 0===e||e.close(1e3)}handleOpen(e){var t;this.logger.log("Connection opened",{readyState:null===(t=this.client)||void 0===t?void 0:t.readyState}),this.handlers.forEach((t=>t.onOpen(e)))}_send(e){var t,n;null===(t=this.client)||void 0===t||t.send((n=e,JSON.stringify(n,(()=>{const e=new WeakSet;return(t,n)=>{if("object"==typeof n&&null!==n){if(e.has(n))return;e.add(n)}return n}})(),2)))}handleClose(e){return n(this,void 0,void 0,(function*(){this.logger.log("Connection closed",e),this.handlers.forEach((e=>e.onClose())),this.logger.log("Will try to reconnect in 3s"),yield i(3e3),this.connect()}))}handleError(e){var t;this.logger.error("Connection error",e),null===(t=this.client)||void 0===t||t.close()}handleMessage(e){if(!e.data)return void this.logger.error("Message has empty data field",e);const t=JSON.parse(e.data);if(!t.type)return void this.logger.error("Message has no type",t);const n=t;Object.values(l.RunningEnvironment).includes(n.type)||Object.values(l.Runner).includes(n.type)||Object.values(l.CodeCell).includes(n.type)?n.type!==l.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((e=>e.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var m,p;!function(e){e.Ok="Ok",e.Terminated="Terminated"}(m||(m={}));class f{constructor(e,t=new Date){this.id=e,this.lastPing=t,this.logger=new s("RunnerSession"),this.url=`https://${c}`}ping(){return n(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const e=JSON.stringify({sessionID:this.id});try{const t=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:e}),n=yield t.json();if(!t.ok)throw this.logger.error(t.headers,n),new Error("Non-OK response when trying to ping active Runner session");if(n.status===m.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(e){throw this.logger.error(e),new Error("Failed to ping active Runner session")}}))}}!function(e){e.Connected="Connected",e.Connecting="Connecting",e.Disconnected="Disconnected"}(p||(p={}));class D{constructor(e){this.conn=e,this.logger=new s("SessionManager"),this.url=`https://${c}`,this.isGettingSessionActive=!1,this.status=p.Disconnected,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem(h)}set cachedSessionID(e){null===e?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem(h)):(this.logger.log(`Saved sessionID "${e}" as last sessionID`),sessionStorage.setItem(h,e))}reset(){this.logger.log("Reset"),this.status=p.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var e;return n(this,void 0,void 0,(function*(){if(!this.isGettingSessionActive)for(this.isGettingSessionActive=!0;;){this.status=p.Connecting;try{const t=this.cachedSessionID?`${this.url}/session/${this.cachedSessionID}`:`${this.url}/session`;this.cachedSessionID?this.logger.log(`Restoring old Runner session "${this.cachedSessionID}"`):this.logger.log("Acquiring new Runner session");const n=yield fetch(t),s=yield n.json();if(!n.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 3s",n.headers,s),yield i(u);continue}for(this.session=new f(s.sessionID),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=p.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.session.ping(),yield i(5e3)}catch(e){this.logger.error(`Failed to ping session "${this.session.id}"`,e);break}this.logger.log(`Stopped pinging session "${null===(e=this.session)||void 0===e?void 0:e.id}"`),this.session=void 0,this.status=p.Disconnected,this.conn.close()}catch(e){this.logger.error("Failed to acquire Runner session. Will try again in 3s",e),yield i(u)}}}))}}function b(e,t){for(var n=0,s=e.length-1;s>=0;s--){var i=e[s];"."===i?e.splice(s,1):".."===i?(e.splice(s,1),n++):n&&(e.splice(s,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}var C=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,E=function(e){return C.exec(e).slice(1)};function S(){for(var e="",t=!1,n=arguments.length-1;n>=-1&&!t;n--){var s=n>=0?arguments[n]:"/";if("string"!=typeof s)throw new TypeError("Arguments to path.resolve must be strings");s&&(e=s+"/"+e,t="/"===s.charAt(0))}return(t?"/":"")+(e=b(x(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function I(e){var t=y(e),n="/"===w(e,-1);return(e=b(x(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function y(e){return"/"===e.charAt(0)}var R={extname:function(e){return E(e)[3]},basename:function(e,t){var n=E(e)[2];return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n},dirname:function(e){var t=E(e),n=t[0],s=t[1];return n||s?(s&&(s=s.substr(0,s.length-1)),n+s):"."},sep:"/",delimiter:":",relative:function(e,t){function n(e){for(var t=0;t<e.length&&""===e[t];t++);for(var n=e.length-1;n>=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=S(e).substr(1),t=S(t).substr(1);for(var s=n(e.split("/")),i=n(t.split("/")),o=Math.min(s.length,i.length),r=o,a=0;a<o;a++)if(s[a]!==i[a]){r=a;break}var l=[];for(a=r;a<s.length;a++)l.push("..");return(l=l.concat(i.slice(r))).join("/")},join:function(){var e=Array.prototype.slice.call(arguments,0);return I(x(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))},isAbsolute:y,normalize:I,resolve:S};function x(e,t){if(e.filter)return e.filter(t);for(var n=[],s=0;s<e.length;s++)t(e[s],s,e)&&n.push(e[s]);return n}var w="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)};function k(e){return function(e){const t=Array.from(e).reduce(((e,t)=>0|31*e+t.charCodeAt(0)),0);return("0000000"+(t>>>0).toString(16)).slice(-8)}(e)}class _{constructor(e,t){this.contextID=e,this.templateID=t,this.isReady=!1,this.id=`${e}_${k(t)}`,this.template=g[this.templateID]}}function F(e,{environmentID:t,template:n}){const s={type:l.RunningEnvironment.Start,payload:{environmentID:t,template:n}};e.send(s)}function O(e,{environmentID:t,executionID:n,command:s}){const i={type:l.RunningEnvironment.ExecCmd,payload:{environmentID:t,executionID:n,command:s}};e.send(i)}function $(e,{environmentID:t,path:n,content:s}){const i={type:l.RunningEnvironment.WriteFile,payload:{environmentID:t,path:n,content:s}};e.send(i)}class j{constructor(e){this.opts=e,this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.envs=[],this.logger=new s("EvaluationContext",e.debug),this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:this.handleConnectionOpen.bind(this),onMessage:this.handleConnectionMessage.bind(this),onClose:this.handleConnectionClose.bind(this)}),this.opts.conn.isOpen&&this.opts.conn.sessionID&&this.handleConnectionOpen(this.opts.conn.sessionID),this.opts.conn.isClosed&&this.handleConnectionClose()}get contextID(){return this.opts.contextID}restart(){return this.logger.log("Restart",this.opts.conn.sessionID),this.envs.forEach((e=>{e.isReady=!1,F(this.opts.conn,{environmentID:e.id,template:e.template})}))}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.envs=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({templateID:e,path:t}){return n(this,void 0,void 0,(function*(){this.logger.log("Get file",{templateID:e,filepath:t});const n=this.getRunningEnvironment({templateID:e});if(!n)return void this.logger.error("Environment not found",{templateID:e,filepath:t});if(!n.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:t});let s;const i=new Promise(((e,t)=>{s=e,setTimeout((()=>{t("Timeout")}),1e4)})),o=e=>{e.path.endsWith(t)&&s(e.content)};this.subscribeFileContent(o),function(e,{environmentID:t,path:n}){const s={type:l.RunningEnvironment.GetFile,payload:{environmentID:t,path:n}};e.send(s)}(this.opts.conn,{path:t,environmentID:n.id});try{return yield i}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(o)}}))}updateFile({templateID:e,path:t,content:s}){return n(this,void 0,void 0,(function*(){this.logger.log("Update file",{templateID:e,filepath:t});const n=this.getRunningEnvironment({templateID:e});if(!n)return void this.logger.error("Environment not found",{templateID:e,filepath:t});if(!n.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:t});let i;const o=new Promise(((e,t)=>{i=e,setTimeout((()=>{t("Timeout")}),1e4)})),r=e=>{e.path.endsWith(t)&&i()};this.subscribeFSWrite(r),$(this.opts.conn,{environmentID:n.id,path:t,content:s});try{yield o}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(r)}}))}executeCode({templateID:e,executionID:t,code:s}){return n(this,void 0,void 0,(function*(){this.logger.log("Execute code",{templateID:e,executionID:t});const n=g[e].toCommand;if(void 0===n)return;const i=this.getRunningEnvironment({templateID:e});if(!i)return void this.logger.error("Environment not found",{templateID:e,executionID:t});if(!i.isReady)return void this.logger.error("Environment is not ready",{templateID:e,executionID:t});const o=g[e].fileExtension,r=`${t}${o}`,a=R.join("/src",r);$(this.opts.conn,{environmentID:i.id,path:a,content:s});const l=n(R.join(g[e].root_dir,a));O(this.opts.conn,{environmentID:i.id,executionID:t,command:l})}))}executeCommand({templateID:e,executionID:t,command:n}){this.logger.log("Execute shell command",{templateID:e,executionID:t,command:n});const s=this.getRunningEnvironment({templateID:e});s?s.isReady?O(this.opts.conn,{environmentID:s.id,executionID:t,command:n}):this.logger.error("Environment is not ready",{templateID:e,executionID:t,command:n}):this.logger.error("Environment not found",{templateID:e,executionID:t,command:n})}getRunningEnvironment({templateID:e}){return this.envs.find((t=>t.templateID===e))}createRunningEnvironment({templateID:e}){var t,n;this.logger.log("Creating running environment",{templateID:e});if(this.getRunningEnvironment({templateID:e}))return;const s=new _(this.contextID,e);this.envs.push(s),F(this.opts.conn,{environmentID:s.id,template:s.template}),null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)}subscribeFileContent(e){this.fileContentSubscribers.push(e)}unsubscribeFileContent(e){const t=this.fileContentSubscribers.indexOf(e);t>-1&&this.fileContentSubscribers.splice(t,1)}subscribeFSWrite(e){this.fsWriteSubscribers.push(e)}unsubscribeFSWrite(e){const t=this.fsWriteSubscribers.indexOf(e);t>-1&&this.fsWriteSubscribers.splice(t,1)}handleConnectionOpen(e){var t,n;this.restart(),null===(n=(t=this.opts).onSessionChange)||void 0===n||n.call(t,{status:p.Connected,sessionID:e})}handleConnectionClose(){var e,t;null===(t=(e=this.opts).onSessionChange)||void 0===t||t.call(e,{status:p.Connecting})}handleConnectionMessage(e){switch(this.logger.log("Handling message from remote Runner",{message:e}),e.type){case l.RunningEnvironment.StartAck:{const t=e;this.vmenv_handleStartAck(t.payload);break}case l.RunningEnvironment.CmdOut:{const t=e;this.vmenv_handleCmdOut(t.payload);break}case l.RunningEnvironment.CmdExit:{const t=e;this.vmenv_handleCmdExit(t.payload);break}case l.RunningEnvironment.FSEventWrite:{const t=e;this.vmenv_handleFSEventWrite(t.payload);break}case l.RunningEnvironment.FileContent:{const t=e;this.vmenv_handleFileContent(t.payload);break}default:this.logger.warn("Unknown message type",{message:e})}}vmenv_handleCmdExit(e){var t,n;void 0!==e.error&&(null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,{environmentID:e.environmentID,executionID:e.executionID,stderr:e.error}))}vmenv_handleFSEventWrite(e){this.logger.log('[vmenv] Handling "FSEventWrite"',e);this.envs.find((t=>t.id===e.environmentID))?this.fsWriteSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleFileContent(e){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:e.environmentID,path:e.path});this.envs.find((t=>t.id===e.environmentID))?this.fileContentSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleStartAck(e){var t,n;this.logger.log('[vmenv] Handling "StartAck"',{payload:e});const s=this.envs.find((t=>t.id===e.environmentID));s?(s.isReady=!0,null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)):this.logger.warn("Environment not found",{payload:e})}vmenv_handleCmdOut(e){var t,n;this.logger.log('[vmenv] Handling "CmdOut"',e),null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,e)}}class W{constructor(){this.logger=new s("Runner"),this.conn=new v,this.sessManager=new D(this.conn)}static get obj(){return W._obj||(W._obj=new W)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(e){return new j(Object.assign(Object.assign({},e),{conn:this.conn}))}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const N=(M="1234567890abcdefghijklmnopqrstuvwxyz",A=6,()=>{let e="",t=A;for(;t--;)e+=M[Math.random()*M.length|0];return e});var M,A,T;e.DevbookStatus=void 0,(T=e.DevbookStatus||(e.DevbookStatus={}))[T.Disconnected=0]="Disconnected",T[T.Connecting=1]="Connecting",T[T.Connected=2]="Connected";class P{constructor(t){this.opts=t,this.contextID="default",this.executionID=N(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=p.Disconnected,this._status=e.DevbookStatus.Disconnected;const n=this.getURL.bind(this),s=()=>this.opts.env,i=()=>this.executionID,o=e=>this.isEnvReady=e,r=e=>this.sessionStatus=e,a=e=>this.sessionID=e;this.context=W.obj.createContext({debug:t.debug,contextID:this.contextID,onEnvChange(e){var i;e.templateID===s()&&(o(e.isReady),null===(i=t.onURLChange)||void 0===i||i.call(t,n))},onSessionChange({status:e,sessionID:s}){var i;a(s),r(e),null===(i=t.onURLChange)||void 0===i||i.call(t,n)},onCmdOut(e){var n,s;e.executionID===i()&&(void 0!==e.stdout&&(null===(n=t.onStdout)||void 0===n||n.call(t,e.stdout)),void 0!==e.stderr&&(null===(s=t.onStderr)||void 0===s||s.call(t,e.stderr)))}}),this.context.createRunningEnvironment({templateID:t.env})}get sessionID(){return this._sessionID}set sessionID(e){this._sessionID=e}get isDestroyed(){return this._isDestroyed}set isDestroyed(e){this._isDestroyed=e,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(e){this._isEnvReady=e,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(e){this._sessionStatus=e,this.updateStatus()}get status(){return this._status}set status(e){var t,n;this._status=e,null===(n=(t=this.opts).onStatusChange)||void 0===n||n.call(t,e)}get fs(){return{get:this.getFile.bind(this),write:this.writeFile.bind(this)}}getURL(t){if(this.status!==e.DevbookStatus.Connected)return;const n=this.sessionID;if(!this.sessionID)return;const s=this.context.getRunningEnvironment({templateID:this.opts.env});return(null==s?void 0:s.isReady)?`https://${t}-${s.id}-${n}.o.usedevbook.com`:void 0}runCmd(t){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=N(),this.context.executeCommand({templateID:this.opts.env,executionID:this.executionID,command:t})}runCode(t){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=N(),this.context.executeCode({templateID:this.opts.env,executionID:this.executionID,code:t})}destroy(){this.context.destroy(),this.isDestroyed=!0}getFile(t){return n(this,void 0,void 0,(function*(){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({templateID:this.opts.env,path:t})}))}writeFile(t,s){return n(this,void 0,void 0,(function*(){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({templateID:this.opts.env,path:t,content:s})}))}updateStatus(){if(this.isDestroyed)return void(this.status!==e.DevbookStatus.Disconnected&&(this.status=e.DevbookStatus.Disconnected));let t;switch(this.sessionStatus){case p.Disconnected:t=e.DevbookStatus.Disconnected;break;case p.Connecting:t=e.DevbookStatus.Connecting;break;case p.Connected:if(!this.isEnvReady){t=e.DevbookStatus.Connecting;break}t=e.DevbookStatus.Connected}this.status=t}}e.Devbook=P,e.useDevbook=function({env:s,debug:i,port:o}){const[r,a]=t.useState(),[l,c]=t.useState(e.DevbookStatus.Disconnected),[h,u]=t.useState([]),[d,g]=t.useState([]),[v,m]=t.useState(),p=t.useMemo((()=>r?r.fs:{get(){return n(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))},write(){return n(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))}}),[r]),f=t.useCallback((e=>{r&&(g([]),u([]),r.runCmd(e))}),[r]),D=t.useCallback((e=>{r&&(g([]),u([]),r.runCode(e))}),[r]);return t.useEffect((function(){const e=new P({debug:i,env:s,onStatusChange(e){c(e)},onStderr(e){u((t=>[...t,e]))},onStdout(e){g((t=>[...t,e]))},onURLChange(e){o&&m(e(o))}});return g([]),u([]),m(void 0),a(e),()=>{e.destroy()}}),[s,i,o]),{stderr:h,stdout:d,runCmd:f,runCode:D,status:l,fs:p,url:v}},Object.defineProperty(e,"__esModule",{value:!0})}));
***************************************************************************** */function n(e,t,n,s){return new(n||(n=Promise))((function(i,o){function r(e){try{l(s.next(e))}catch(e){o(e)}}function a(e){try{l(s.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((s=s.apply(e,t||[])).next())}))}class s{constructor(e,t=!1){this.logID=e,this.isEnabled=t}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...e){this.isEnabled&&console.log(`[${this.id()}]`,...e)}warn(...e){this.isEnabled&&console.warn(`[${this.id()}]`,...e)}error(...e){console.error(`[${this.id()} ERROR]`,...e)}}function i(e){return new Promise((t=>setTimeout(t,e)))}var o,r,a;!function(e){e.Error="Runner.Error"}(o||(o={})),function(e){e.Start="RunningEnvironment.Start",e.StartAck="RunningEnvironment.StartAck",e.Eval="RunningEnvironment.Eval",e.FSEventCreate="RunningEnvironment.FSEventCreate",e.FSEventRemove="RunningEnvironment.FSEventRemove",e.FSEventWrite="RunningEnvironment.FSEventWrite",e.CreateDir="RunningEnvironment.CreateDir",e.ListDir="RunningEnvironment.ListDir",e.WriteFile="RunningEnvironment.WriteFile",e.GetFile="RunningEnvironment.GetFile",e.RemoveFile="RunningEnvironment.RemoveFile",e.DirContent="RunningEnvironment.DirContent",e.FileContent="RunningEnvironment.FileContent",e.Stdout="RunningEnvironment.Stdout",e.Stderr="RunningEnvironment.Stderr",e.ExecCmd="RunningEnvironment.ExecCmd",e.KillCmd="RunningEnvironment.KillCmd",e.ListRunningCmds="RunningEnvironment.ListRunningCmds",e.CmdOut="RunningEnvironment.CmdOut",e.CmdExit="RunningEnvironment.CmdExit",e.RunningCmds="RunningEnvironment.RunningCmds",e.RunCode="RunningEnvironment.Run"}(r||(r={})),function(e){e.Error="CodeCell.Error"}(a||(a={}));const l={Runner:o,RunningEnvironment:r,CodeCell:a},c="orchestrator.usedevbook.com",h="dbk_sdk_session_id",d=3e3;var u;e.Env=void 0,(u=e.Env||(e.Env={})).NextJS="nextjs-v11-components",u.NodeJS="nodejs-v16",u.Supabase="supabase",u.BananaNode="banana-node",u.BananaPython="banana-python";const g={[e.Env.NodeJS]:{id:"nodejs-v16",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nodejs-v16:latest",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[e.Env.Supabase]:{id:"supabase",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/supabase",root_dir:"/home/runner",code_cells_dir:"/home/runner/src",toCommand:e=>`node "${e}"`},[e.Env.NextJS]:{id:"nextjs-v11-components",fileExtension:".tsx",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/nextjs-v11-components",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"},[e.Env.BananaNode]:{id:"banana-node",fileExtension:".js",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/banana-node",root_dir:"/home/runner",code_cells_dir:"/home/runner/src"},[e.Env.BananaPython]:{id:"banana-python",fileExtension:".py",image:"us-central1-docker.pkg.dev/devbookhq/devbook-runner-templates/banana-python",root_dir:"/home/runner",code_cells_dir:"/home/runner"}};class v{constructor(){this.url=`wss://${c}`,this.logger=new s("WebSocketConnection"),this.handlers=[]}get state(){var e;return null===(e=this.client)||void 0===e?void 0:e.readyState}get isClosed(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSED}get isClosing(){if(void 0!==this.client)return this.client.readyState===this.client.CLOSING}get isOpen(){if(void 0!==this.client)return this.client.readyState===this.client.OPEN}get isConnecting(){if(void 0!==this.client)return this.client.readyState===this.client.CONNECTING}subscribeHandler(e){return this.handlers.push(e),()=>{this.handlers=this.handlers.filter((t=>t!==e))}}connect(e){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(e||this.sessionID?(e?(this.logger.log(`Will try to connect to session "${e}"`),this.sessionID=e):!e&&this.sessionID&&this.logger.log(`Will try to connect to previous session "${this.sessionID}"`),this.client=new WebSocket(`${this.url}/session/ws/${this.sessionID}`),this.client.onopen=()=>this.handleOpen(this.sessionID),this.client.onmessage=e=>{this.logger.log("Received (raw)",{msg:e}),this.handleMessage(e)},this.client.onerror=e=>this.handleError(e),this.client.onclose=e=>this.handleClose(e)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(e){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",e),this._send(e)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",e)}close(){var e;this.logger.log("Closing connection"),null===(e=this.client)||void 0===e||e.close(1e3)}handleOpen(e){var t;this.logger.log("Connection opened",{readyState:null===(t=this.client)||void 0===t?void 0:t.readyState}),this.handlers.forEach((t=>t.onOpen(e)))}_send(e){var t,n;null===(t=this.client)||void 0===t||t.send((n=e,JSON.stringify(n,(()=>{const e=new WeakSet;return(t,n)=>{if("object"==typeof n&&null!==n){if(e.has(n))return;e.add(n)}return n}})(),2)))}handleClose(e){return n(this,void 0,void 0,(function*(){this.logger.log("Connection closed",e),this.handlers.forEach((e=>e.onClose())),this.logger.log("Will try to reconnect in 3s"),yield i(3e3),this.connect()}))}handleError(e){var t;this.logger.error("Connection error",e),null===(t=this.client)||void 0===t||t.close()}handleMessage(e){if(!e.data)return void this.logger.error("Message has empty data field",e);const t=JSON.parse(e.data);if(!t.type)return void this.logger.error("Message has no type",t);const n=t;Object.values(l.RunningEnvironment).includes(n.type)||Object.values(l.Runner).includes(n.type)||Object.values(l.CodeCell).includes(n.type)?n.type!==l.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((e=>e.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var m,p;!function(e){e.Ok="Ok",e.Terminated="Terminated"}(m||(m={}));class f{constructor(e,t=new Date){this.id=e,this.lastPing=t,this.logger=new s("RunnerSession"),this.url=`https://${c}`}ping(){return n(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const e=JSON.stringify({sessionID:this.id});try{const t=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:e}),n=yield t.json();if(!t.ok)throw this.logger.error(t.headers,n),new Error("Non-OK response when trying to ping active Runner session");if(n.status===m.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(e){throw this.logger.error(e),new Error("Failed to ping active Runner session")}}))}}!function(e){e.Connected="Connected",e.Connecting="Connecting",e.Disconnected="Disconnected"}(p||(p={}));class D{constructor(e){this.conn=e,this.logger=new s("SessionManager"),this.url=`https://${c}`,this.isGettingSessionActive=!1,this.status=p.Disconnected,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem(h)}set cachedSessionID(e){null===e?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem(h)):(this.logger.log(`Saved sessionID "${e}" as last sessionID`),sessionStorage.setItem(h,e))}reset(){this.logger.log("Reset"),this.status=p.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var e;return n(this,void 0,void 0,(function*(){if(!this.isGettingSessionActive)for(this.isGettingSessionActive=!0;;){this.status=p.Connecting;try{const t=this.cachedSessionID?`${this.url}/session/${this.cachedSessionID}`:`${this.url}/session`;this.cachedSessionID?this.logger.log(`Restoring old Runner session "${this.cachedSessionID}"`):this.logger.log("Acquiring new Runner session");const n=yield fetch(t),s=yield n.json();if(!n.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 3s",n.headers,s),yield i(d);continue}for(this.session=new f(s.sessionID),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=p.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.session.ping(),yield i(5e3)}catch(e){this.logger.error(`Failed to ping session "${this.session.id}"`,e);break}this.logger.log(`Stopped pinging session "${null===(e=this.session)||void 0===e?void 0:e.id}"`),this.session=void 0,this.status=p.Disconnected,this.conn.close()}catch(e){this.logger.error("Failed to acquire Runner session. Will try again in 3s",e),yield i(d)}}}))}}function b(e,t){for(var n=0,s=e.length-1;s>=0;s--){var i=e[s];"."===i?e.splice(s,1):".."===i?(e.splice(s,1),n++):n&&(e.splice(s,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}var C=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,E=function(e){return C.exec(e).slice(1)};function S(){for(var e="",t=!1,n=arguments.length-1;n>=-1&&!t;n--){var s=n>=0?arguments[n]:"/";if("string"!=typeof s)throw new TypeError("Arguments to path.resolve must be strings");s&&(e=s+"/"+e,t="/"===s.charAt(0))}return(t?"/":"")+(e=b(x(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function y(e){var t=I(e),n="/"===w(e,-1);return(e=b(x(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function I(e){return"/"===e.charAt(0)}var R={extname:function(e){return E(e)[3]},basename:function(e,t){var n=E(e)[2];return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n},dirname:function(e){var t=E(e),n=t[0],s=t[1];return n||s?(s&&(s=s.substr(0,s.length-1)),n+s):"."},sep:"/",delimiter:":",relative:function(e,t){function n(e){for(var t=0;t<e.length&&""===e[t];t++);for(var n=e.length-1;n>=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=S(e).substr(1),t=S(t).substr(1);for(var s=n(e.split("/")),i=n(t.split("/")),o=Math.min(s.length,i.length),r=o,a=0;a<o;a++)if(s[a]!==i[a]){r=a;break}var l=[];for(a=r;a<s.length;a++)l.push("..");return(l=l.concat(i.slice(r))).join("/")},join:function(){var e=Array.prototype.slice.call(arguments,0);return y(x(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))},isAbsolute:I,normalize:y,resolve:S};function x(e,t){if(e.filter)return e.filter(t);for(var n=[],s=0;s<e.length;s++)t(e[s],s,e)&&n.push(e[s]);return n}var w="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)};function k(e){return function(e){const t=Array.from(e).reduce(((e,t)=>0|31*e+t.charCodeAt(0)),0);return("0000000"+(t>>>0).toString(16)).slice(-8)}(e)}class _{constructor(e,t){this.contextID=e,this.templateID=t,this.isReady=!1,this.id=`${e}_${k(t)}`,this.template=g[this.templateID]}}function F(e,{environmentID:t,template:n}){const s={type:l.RunningEnvironment.Start,payload:{environmentID:t,template:n}};e.send(s)}function O(e,{environmentID:t,executionID:n,command:s}){const i={type:l.RunningEnvironment.ExecCmd,payload:{environmentID:t,executionID:n,command:s}};e.send(i)}function $(e,{environmentID:t,path:n,content:s}){const i={type:l.RunningEnvironment.WriteFile,payload:{environmentID:t,path:n,content:s}};e.send(i)}class j{constructor(e){this.opts=e,this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.envs=[],this.logger=new s("EvaluationContext",e.debug),this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:this.handleConnectionOpen.bind(this),onMessage:this.handleConnectionMessage.bind(this),onClose:this.handleConnectionClose.bind(this)}),this.opts.conn.isOpen&&this.opts.conn.sessionID&&this.handleConnectionOpen(this.opts.conn.sessionID),this.opts.conn.isClosed&&this.handleConnectionClose()}get contextID(){return this.opts.contextID}restart(){return this.logger.log("Restart",this.opts.conn.sessionID),this.envs.forEach((e=>{e.isReady=!1,F(this.opts.conn,{environmentID:e.id,template:e.template})}))}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.envs=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({templateID:e,path:t}){return n(this,void 0,void 0,(function*(){this.logger.log("Get file",{templateID:e,filepath:t});const n=this.getRunningEnvironment({templateID:e});if(!n)return void this.logger.error("Environment not found",{templateID:e,filepath:t});if(!n.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:t});let s;const i=new Promise(((e,t)=>{s=e,setTimeout((()=>{t("Timeout")}),1e4)})),o=e=>{e.path.endsWith(t)&&s(e.content)};this.subscribeFileContent(o),function(e,{environmentID:t,path:n}){const s={type:l.RunningEnvironment.GetFile,payload:{environmentID:t,path:n}};e.send(s)}(this.opts.conn,{path:t,environmentID:n.id});try{return yield i}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(o)}}))}updateFile({templateID:e,path:t,content:s}){return n(this,void 0,void 0,(function*(){this.logger.log("Update file",{templateID:e,filepath:t});const n=this.getRunningEnvironment({templateID:e});if(!n)return void this.logger.error("Environment not found",{templateID:e,filepath:t});if(!n.isReady)return void this.logger.error("Environment is not ready",{templateID:e,filepath:t});let i;const o=new Promise(((e,t)=>{i=e,setTimeout((()=>{t("Timeout")}),1e4)})),r=e=>{e.path.endsWith(t)&&i()};this.subscribeFSWrite(r),$(this.opts.conn,{environmentID:n.id,path:t,content:s});try{yield o}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(r)}}))}executeCode({templateID:e,executionID:t,code:s}){return n(this,void 0,void 0,(function*(){this.logger.log("Execute code",{templateID:e,executionID:t});const n=g[e].toCommand;if(void 0===n)return;const i=this.getRunningEnvironment({templateID:e});if(!i)return void this.logger.error("Environment not found",{templateID:e,executionID:t});if(!i.isReady)return void this.logger.error("Environment is not ready",{templateID:e,executionID:t});const o=g[e].fileExtension,r=`${t}${o}`,a=R.join("/src",r);$(this.opts.conn,{environmentID:i.id,path:a,content:s});const l=n(R.join(g[e].root_dir,a));O(this.opts.conn,{environmentID:i.id,executionID:t,command:l})}))}executeCommand({templateID:e,executionID:t,command:n}){this.logger.log("Execute shell command",{templateID:e,executionID:t,command:n});const s=this.getRunningEnvironment({templateID:e});s?s.isReady?O(this.opts.conn,{environmentID:s.id,executionID:t,command:n}):this.logger.error("Environment is not ready",{templateID:e,executionID:t,command:n}):this.logger.error("Environment not found",{templateID:e,executionID:t,command:n})}getRunningEnvironment({templateID:e}){return this.envs.find((t=>t.templateID===e))}createRunningEnvironment({templateID:e}){var t,n;this.logger.log("Creating running environment",{templateID:e});if(this.getRunningEnvironment({templateID:e}))return;const s=new _(this.contextID,e);this.envs.push(s),F(this.opts.conn,{environmentID:s.id,template:s.template}),null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)}subscribeFileContent(e){this.fileContentSubscribers.push(e)}unsubscribeFileContent(e){const t=this.fileContentSubscribers.indexOf(e);t>-1&&this.fileContentSubscribers.splice(t,1)}subscribeFSWrite(e){this.fsWriteSubscribers.push(e)}unsubscribeFSWrite(e){const t=this.fsWriteSubscribers.indexOf(e);t>-1&&this.fsWriteSubscribers.splice(t,1)}handleConnectionOpen(e){var t,n;this.restart(),null===(n=(t=this.opts).onSessionChange)||void 0===n||n.call(t,{status:p.Connected,sessionID:e})}handleConnectionClose(){var e,t;null===(t=(e=this.opts).onSessionChange)||void 0===t||t.call(e,{status:p.Connecting})}handleConnectionMessage(e){switch(this.logger.log("Handling message from remote Runner",{message:e}),e.type){case l.RunningEnvironment.StartAck:{const t=e;this.vmenv_handleStartAck(t.payload);break}case l.RunningEnvironment.CmdOut:{const t=e;this.vmenv_handleCmdOut(t.payload);break}case l.RunningEnvironment.CmdExit:{const t=e;this.vmenv_handleCmdExit(t.payload);break}case l.RunningEnvironment.FSEventWrite:{const t=e;this.vmenv_handleFSEventWrite(t.payload);break}case l.RunningEnvironment.FileContent:{const t=e;this.vmenv_handleFileContent(t.payload);break}default:this.logger.warn("Unknown message type",{message:e})}}vmenv_handleCmdExit(e){var t,n;void 0!==e.error&&(null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,{environmentID:e.environmentID,executionID:e.executionID,stderr:e.error}))}vmenv_handleFSEventWrite(e){this.logger.log('[vmenv] Handling "FSEventWrite"',e);this.envs.find((t=>t.id===e.environmentID))?this.fsWriteSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleFileContent(e){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:e.environmentID,path:e.path});this.envs.find((t=>t.id===e.environmentID))?this.fileContentSubscribers.forEach((t=>t(e))):this.logger.warn("Environment not found",{payload:e})}vmenv_handleStartAck(e){var t,n;this.logger.log('[vmenv] Handling "StartAck"',{payload:e});const s=this.envs.find((t=>t.id===e.environmentID));s?(s.isReady=!0,null===(n=(t=this.opts).onEnvChange)||void 0===n||n.call(t,s)):this.logger.warn("Environment not found",{payload:e})}vmenv_handleCmdOut(e){var t,n;this.logger.log('[vmenv] Handling "CmdOut"',e),null===(n=(t=this.opts).onCmdOut)||void 0===n||n.call(t,e)}}class W{constructor(){this.logger=new s("Runner"),this.conn=new v,this.sessManager=new D(this.conn)}static get obj(){return W._obj||(W._obj=new W)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(e){return new j(Object.assign(Object.assign({},e),{conn:this.conn}))}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const N=(M="1234567890abcdefghijklmnopqrstuvwxyz",A=6,()=>{let e="",t=A;for(;t--;)e+=M[Math.random()*M.length|0];return e});var M,A,T;e.DevbookStatus=void 0,(T=e.DevbookStatus||(e.DevbookStatus={}))[T.Disconnected=0]="Disconnected",T[T.Connecting=1]="Connecting",T[T.Connected=2]="Connected";class P{constructor(t){this.opts=t,this.contextID="default",this.executionID=N(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=p.Disconnected,this._status=e.DevbookStatus.Disconnected;const n=this.getURL.bind(this),s=()=>this.opts.env,i=()=>this.executionID,o=e=>this.isEnvReady=e,r=e=>this.sessionStatus=e,a=e=>this.sessionID=e;this.context=W.obj.createContext({debug:t.debug,contextID:this.contextID,onEnvChange(e){var i;e.templateID===s()&&(o(e.isReady),null===(i=t.onURLChange)||void 0===i||i.call(t,n))},onSessionChange({status:e,sessionID:s}){var i;a(s),r(e),null===(i=t.onURLChange)||void 0===i||i.call(t,n)},onCmdOut(e){var n,s;e.executionID===i()&&(void 0!==e.stdout&&(null===(n=t.onStdout)||void 0===n||n.call(t,e.stdout)),void 0!==e.stderr&&(null===(s=t.onStderr)||void 0===s||s.call(t,e.stderr)))}}),this.context.createRunningEnvironment({templateID:t.env})}get sessionID(){return this._sessionID}set sessionID(e){this._sessionID=e}get isDestroyed(){return this._isDestroyed}set isDestroyed(e){this._isDestroyed=e,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(e){this._isEnvReady=e,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(e){this._sessionStatus=e,this.updateStatus()}get status(){return this._status}set status(e){var t,n;this._status=e,null===(n=(t=this.opts).onStatusChange)||void 0===n||n.call(t,e)}get fs(){return{get:this.getFile.bind(this),write:this.writeFile.bind(this)}}getURL(t){if(this.status!==e.DevbookStatus.Connected)return;const n=this.sessionID;if(!this.sessionID)return;const s=this.context.getRunningEnvironment({templateID:this.opts.env});return(null==s?void 0:s.isReady)?`https://${t}-${s.id}-${n}.o.usedevbook.com`:void 0}runCmd(t){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=N(),this.context.executeCommand({templateID:this.opts.env,executionID:this.executionID,command:t})}runCode(t){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=N(),this.context.executeCode({templateID:this.opts.env,executionID:this.executionID,code:t})}destroy(){this.context.destroy(),this.isDestroyed=!0}getFile(t){return n(this,void 0,void 0,(function*(){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({templateID:this.opts.env,path:t})}))}writeFile(t,s){return n(this,void 0,void 0,(function*(){if(this.status!==e.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({templateID:this.opts.env,path:t,content:s})}))}updateStatus(){if(this.isDestroyed)return void(this.status!==e.DevbookStatus.Disconnected&&(this.status=e.DevbookStatus.Disconnected));let t;switch(this.sessionStatus){case p.Disconnected:t=e.DevbookStatus.Disconnected;break;case p.Connecting:t=e.DevbookStatus.Connecting;break;case p.Connected:if(!this.isEnvReady){t=e.DevbookStatus.Connecting;break}t=e.DevbookStatus.Connected}this.status=t}}e.Devbook=P,e.useDevbook=function({env:s,debug:i,port:o}){const[r,a]=t.useState(),[l,c]=t.useState(e.DevbookStatus.Disconnected),[h,d]=t.useState([]),[u,g]=t.useState([]),[v,m]=t.useState(),p=t.useMemo((()=>r?r.fs:{get(){return n(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))},write(){return n(this,void 0,void 0,(function*(){throw new Error("FS is not ready yet")}))}}),[r]),f=t.useCallback((e=>{r&&(g([]),d([]),r.runCmd(e))}),[r]),D=t.useCallback((e=>{r&&(g([]),d([]),r.runCode(e))}),[r]);return t.useEffect((function(){const e=new P({debug:i,env:s,onStatusChange(e){c(e)},onStderr(e){d((t=>[...t,e]))},onStdout(e){g((t=>[...t,e]))},onURLChange(e){o&&m(e(o))}});return g([]),d([]),m(void 0),a(e),()=>{e.destroy()}}),[s,i,o]),{stderr:h,stdout:u,runCmd:f,runCode:D,status:l,fs:p,url:v}},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.js.map
{
"name": "@devbookhq/sdk",
"version": "1.0.6",
"version": "1.0.7",
"description": "Devbook allows visitors of your docs to interact with and execute any code snippet or shell command in a private VM",

@@ -54,2 +54,2 @@ "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

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