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.16 to 1.0.17

4

lib/cjs/core/constants/runner.d.ts
export declare const STORAGE_SESSION_ID_KEY = "dbk_sdk_session_id";
export declare const SESSION_PING_PERIOD = 4000;
export declare const SESSION_RESTART_PERIOD = 4000;
export declare const SESSION_REQUEST_CAP = 3;
export declare const SESSION_IDLE_TIME = 120000;
export declare const SESSION_REQUEST_CAP = 1;
export declare const SESSION_IDLE_TIME = 60000;
export declare const WSCONN_RESTART_PERIOD = 5000;

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

***************************************************************************** */
function n(t,e,n,i){return new(n||(n=Promise))((function(s,o){function r(t){try{h(i.next(t))}catch(t){o(t)}}function a(t){try{h(i.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(r,a)}h((i=i.apply(t,e||[])).next())}))}function i(t){return e="1234567890abcdefghijklmnopqrstuvwxyz",n=t,()=>{let t="",i=n;for(;i--;)t+=e[Math.random()*e.length|0];return t};var e,n}class s{constructor(t,e=!1){this.logID=t,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...t){this.isEnabled&&console.log(`[${this.id()}]`,...t)}warn(...t){this.isEnabled&&console.warn(`[${this.id()}]`,...t)}error(...t){console.error(`[${this.id()} ERROR]`,...t)}}function o(t){return new Promise((e=>setTimeout(e,t)))}var r,a,h;!function(t){t.Error="Runner.Error"}(r||(r={})),function(t){t.Start="RunningEnvironment.Start",t.StartAck="RunningEnvironment.StartAck",t.Eval="RunningEnvironment.Eval",t.FSEventCreate="RunningEnvironment.FSEventCreate",t.FSEventRemove="RunningEnvironment.FSEventRemove",t.FSEventWrite="RunningEnvironment.FSEventWrite",t.CreateDir="RunningEnvironment.CreateDir",t.ListDir="RunningEnvironment.ListDir",t.WriteFile="RunningEnvironment.WriteFile",t.GetFile="RunningEnvironment.GetFile",t.RemoveFile="RunningEnvironment.RemoveFile",t.DirContent="RunningEnvironment.DirContent",t.FileContent="RunningEnvironment.FileContent",t.Stdout="RunningEnvironment.Stdout",t.Stderr="RunningEnvironment.Stderr",t.ExecCmd="RunningEnvironment.ExecCmd",t.KillCmd="RunningEnvironment.KillCmd",t.ListRunningCmds="RunningEnvironment.ListRunningCmds",t.CmdOut="RunningEnvironment.CmdOut",t.CmdExit="RunningEnvironment.CmdExit",t.RunningCmds="RunningEnvironment.RunningCmds",t.TermData="RunningEnvironment.TermData",t.TermResize="RunningEnvironment.TermResize",t.TermStart="RunningEnvironment.TermStart",t.TermStartAck="RunningEnvironment.TermStartAck",t.RunCode="RunningEnvironment.Run"}(a||(a={})),function(t){t.Error="CodeCell.Error"}(h||(h={}));const l={Runner:r,RunningEnvironment:a,CodeCell:h};class c{constructor({domain:t,logging:e}){this.handlers=[],this.url=`wss://${t}`,this.logger=new s("WebSocketConnection",e)}get state(){var t;return null===(t=this.client)||void 0===t?void 0:t.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(t){return this.handlers.push(t),()=>{this.handlers=this.handlers.filter((e=>e!==t))}}connect(t){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(t||this.sessionID?(t?(this.logger.log(`Will try to connect to session "${t}"`),this.sessionID=t):!t&&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.client.onmessage=t=>{this.logger.log("Received (raw)",{msg:t}),this.handleMessage(t)},this.client.onerror=t=>this.handleError(t),this.client.onclose=t=>this.handleClose(t)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(t){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",t),this._send(t)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",t)}close(){var t;this.logger.log("Closing connection"),null===(t=this.client)||void 0===t||t.close(1e3)}handleOpen(){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()))}_send(t){var e,n;null===(e=this.client)||void 0===e||e.send((n=t,JSON.stringify(n,(()=>{const t=new WeakSet;return(e,n)=>{if("object"==typeof n&&null!==n){if(t.has(n))return;t.add(n)}return n}})(),2)))}handleClose(t){return n(this,void 0,void 0,(function*(){this.logger.log("Connection closed",t),this.handlers.forEach((t=>t.onClose())),this.logger.log("Will try to reconnect in 5s"),yield o(5e3),this.connect()}))}handleError(t){var e;this.logger.error("Connection error",t),null===(e=this.client)||void 0===e||e.close()}handleMessage(t){if(!t.data)return void this.logger.error("Message has empty data field",t);const e=JSON.parse(t.data);if(!e.type)return void this.logger.error("Message has no type",e);const n=e;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((t=>t.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var d,u;!function(t){t.Ok="Ok",t.Terminated="Terminated"}(d||(d={}));class m{constructor({id:t,url:e}){this.logger=new s("RunnerSession"),this.lastPing=new Date,this.id=t,this.url=e}ping(){return n(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const t=JSON.stringify({sessionID:this.id});try{const e=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:t}),n=yield e.json();if(!e.ok)throw this.logger.error(e.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(t){throw this.logger.error(t),new Error("Failed to ping active Runner session")}}))}}!function(t){t.Connected="Connected",t.Connecting="Connecting",t.Disconnected="Disconnected"}(u||(u={}));class g{constructor({conn:t,domain:e}){this.logger=new s("SessionManager"),this.userActivity=Promise.resolve(),this.isGettingSessionActive=!1,this.status=u.Disconnected,this.conn=t,this.url=`https://${e}`,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(t){null===t?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${t}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",t))}stop(){var t;null===(t=this.resolveActivity)||void 0===t||t.call(this),this.userActivity=new Promise((t=>{this.resolveActivity=t,this.rejectActivity=t}))}start(){var t;null===(t=this.rejectActivity)||void 0===t||t.call(this),this.userActivity=Promise.resolve()}reset(){this.logger.log("Reset"),this.status=u.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var t;return n(this,void 0,void 0,(function*(){if(this.isGettingSessionActive)return;this.isGettingSessionActive=!0;let e=3;for(;e>0;){this.status=u.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 i=yield fetch(n),s=yield i.json();if(!i.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 4s",i.headers,s),e-=1,yield o(4e3);continue}for(e=3,this.session=new m({id:s.sessionID,url:this.url}),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.userActivity,yield this.session.ping(),yield o(4e3)}catch(t){this.logger.error(`Failed to ping session "${this.session.id}"`,t);break}this.logger.log(`Stopped pinging session "${null===(t=this.session)||void 0===t?void 0:t.id}"`),this.session=void 0,this.status=u.Disconnected,this.conn.close()}catch(t){this.logger.error("Failed to acquire Runner session. Will try again in 4s",t),e-=1,yield o(4e3)}}}))}}function v(t,e){for(var n=0,i=t.length-1;i>=0;i--){var s=t[i];"."===s?t.splice(i,1):".."===s?(t.splice(i,1),n++):n&&(t.splice(i,1),n--)}if(e)for(;n--;n)t.unshift("..");return t}var p=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,f=function(t){return p.exec(t).slice(1)};function D(){for(var t="",e=!1,n=arguments.length-1;n>=-1&&!e;n--){var i=n>=0?arguments[n]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(t=i+"/"+t,e="/"===i.charAt(0))}return(e?"/":"")+(t=v(C(t.split("/"),(function(t){return!!t})),!e).join("/"))||"."}function y(t){var e=b(t),n="/"===w(t,-1);return(t=v(C(t.split("/"),(function(t){return!!t})),!e).join("/"))||e||(t="."),t&&n&&(t+="/"),(e?"/":"")+t}function b(t){return"/"===t.charAt(0)}var S={extname:function(t){return f(t)[3]},basename:function(t,e){var n=f(t)[2];return e&&n.substr(-1*e.length)===e&&(n=n.substr(0,n.length-e.length)),n},dirname:function(t){var e=f(t),n=e[0],i=e[1];return n||i?(i&&(i=i.substr(0,i.length-1)),n+i):"."},sep:"/",delimiter:":",relative:function(t,e){function n(t){for(var e=0;e<t.length&&""===t[e];e++);for(var n=t.length-1;n>=0&&""===t[n];n--);return e>n?[]:t.slice(e,n-e+1)}t=D(t).substr(1),e=D(e).substr(1);for(var i=n(t.split("/")),s=n(e.split("/")),o=Math.min(i.length,s.length),r=o,a=0;a<o;a++)if(i[a]!==s[a]){r=a;break}var h=[];for(a=r;a<i.length;a++)h.push("..");return(h=h.concat(s.slice(r))).join("/")},join:function(){var t=Array.prototype.slice.call(arguments,0);return y(C(t,(function(t,e){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t})).join("/"))},isAbsolute:b,normalize:y,resolve:D};function C(t,e){if(t.filter)return t.filter(e);for(var n=[],i=0;i<t.length;i++)e(t[i],i,t)&&n.push(t[i]);return n}var E,w="b"==="ab".substr(-1)?function(t,e,n){return t.substr(e,n)}:function(t,e,n){return e<0&&(e=t.length+e),t.substr(e,n)};class I{get level(){return S.normalize(this.path).split(S.sep).length-1}}function R(t){return t.sort(((t,e)=>"Prompt"===t.type?-2:t.type!==e.type?"Dir"===t.type?-1:"Dir"===e.type?1:0:t.key.localeCompare(e.key))),t}class k extends I{constructor({name:t,parent:e,onAddItem:n}){super(),this.children=[],this.name=t,this.parent=e,this.addItemHandler=n,this.path=S.join(this.parent.path,this.name)}removeChildNode(t){this.children=this.children.filter((e=>e!==t))}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Dir",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:R(i)}}}class x extends I{constructor({name:t,parent:e}){super(),this.name=t,this.parent=e,this.path=S.join(this.parent.path,this.name)}serialize(){return{type:"File",key:this.path,title:this.name,isLeaf:!0}}}class _ extends I{constructor({parent:t,forNode:e,onConfirm:n,onBlur:i}){super(),this.name="name-prompt",this.parent=t,this.forNode=e,this.path=S.join(t.path,this.name),this.onConfirm=n,this.onBlur=i}serialize(t,e,n){return{type:"Prompt",key:this.path,title:e({onConfirm:t=>{var e;return null===(e=this.onConfirm)||void 0===e?void 0:e.call(this,this,t)},onBlur:()=>{var t;return null===(t=this.onBlur)||void 0===t?void 0:t.call(this,this)}}),isLeaf:!0,selectable:!1,icon:n({type:this.forNode})}}}class F extends I{constructor({onAddItem:t}){super(),this.name="/",this.path="/",this.parent=void 0,this.children=[],this.addItemHandler=t}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Root",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:R(i)}}}class P extends I{constructor({parent:t}){super(),this.name="empty-node",this.parent=t,this.path=S.join(this.parent.path,this.name)}serialize(){return{type:"Empty",key:this.path,title:"Empty Directory",isLeaf:!0,disabled:!0}}}class O{constructor(){this.logger=new s("Filesystem"),this.root=new F({onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}),this.startingPrintIndent=0,this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}removeAllListeners(){this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}addListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs.push(e);break;case"onDirsChange":this.onDirsChangeLs.push(e);break;case"onFileContent":this.onFileContentLs.push(e);break;case"onShowPrompt":this.onShowPromptLs.push(e);break;default:throw new Error(`Unknown event type: ${t}`)}}removeListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs=this.onNewItemConfirmLs.filter((t=>t!==e));break;case"onDirsChange":this.onDirsChangeLs=this.onDirsChangeLs.filter((t=>t!==e));break;case"onFileContent":this.onFileContentLs=this.onFileContentLs.filter((t=>t!==e));break;case"onShowPrompt":this.onShowPromptLs=this.onShowPromptLs.filter((t=>t!==e));break;default:throw new Error(`Unknown event type: ${t}`)}}addNodeToDir(t,e){this.logger.log("Adding node to dir",{dirPath:t,node:e});let n=this.find(t);if(n||(this.logger.log("Dir not found, will make all",{dirPath:t}),n=this.mkAll(t)),!n)throw new Error("dirNode is undefined");if(!(n instanceof F||n instanceof k))throw new Error(`Node at '${t}' is not a directory or a root node`);if(n.children.some((t=>t.name===e.name)))return;let i;i="Dir"===e.type?new k({name:e.name,parent:n,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:e.name,parent:n}),1===n.children.length&&n.children[0]instanceof P?n.children=[i]:n.children.push(i),this.notifyOnDirsChangeLs({dirPaths:[t]})}removeNodeFromDir(t,e){this.logger.log("Remove node from dir",{dirPath:t,nodeName:e.name});const n=this.find(t);if(!n)throw new Error(`No node with path '${t}'`);if(!(n instanceof F||n instanceof k))throw new Error(`Node at '${t}' is not a directory or root node`);const i=n.children.length;n.children=n.children.filter((t=>t.name!==e.name));i!==n.children.length?(0===n.children.length&&(n.children=[new P({parent:n})]),this.notifyOnDirsChangeLs({dirPaths:[t]})):this.logger.warn("Node not found in dir",{dirPath:t,nodeName:e.name})}setFileContent(t){this.logger.log("Update file content",t),this.onFileContentLs.forEach((e=>e(t)))}setDirsContent(t){this.logger.log("Set dirs content",t);for(const e of t){let t=this.find(e.dirPath);if(t||(this.logger.log("Dir not found, will make all",{dirPath:e.dirPath}),t=this.mkAll(e.dirPath)),!t)throw new Error("dirNode is undefined");if(!(t instanceof F||t instanceof k))throw new Error(`Node at '${e.dirPath}' is not a directory or a root node`);const n=t.children.find((t=>t instanceof _));if(e.content.length){const n=[];for(const i of e.content){let e;e="Dir"===i.type?new k({name:i.name,parent:t,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:i.name,parent:t}),n.push(e)}t.children=n}else t.children=[new P({parent:t})];n&&t.children.push(n)}this.notifyOnDirsChangeLs({dirPaths:t.map((t=>t.dirPath))})}showPrompt(t){const{event:e,parent:n,type:i}=t;if(this.logger.log("Show prompt",{parent:n,type:i}),n.children.some((t=>t instanceof _)))return;e.preventDefault();const s=new _({parent:n,forNode:i,onConfirm:((t,e)=>{if(this.logger.log("Prompt confirm",{prompt:t,name:e}),!e)return;const n=t.parent.path,s=S.join(t.parent.path,e);this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[n]}),this.notifyOnNewItemConfirmLs({fullPath:s,name:e,type:i})}).bind(this),onBlur:(t=>{this.logger.log("Prompt blur",{prompt:t});const e=t.parent.path;this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[e]})}).bind(this)});n.children.push(s),this.notifyOnDirsChangeLs({dirPaths:[s.parent.path]}),this.notifyOnShowPromptLs({dirPath:s.parent.path})}serialize(t,e,n){return this.logger.log("Serialize"),[this.root.serialize(t,e,n)]}print(t=this.root,e=this.startingPrintIndent){const n=" ".repeat(e);console.log(n+t.path,`(${t.name})`);for(const i of t.children)i instanceof k?this.print(i,e+1):console.log(n+" "+i.path,`(${i.name})`)}find(t,e=this.root){if(this.logger.log("Find (recursion)",{currentPath:e.path,targetPath:t}),t=S.normalize(t),e.path===t)return e;const n=t.split(S.sep).length-1;if(!(e.level>n)&&(e instanceof F||e instanceof k))for(const n of e.children){const e=this.find(t,n);if(e)return e}}mkDir(t){this.logger.log("Make dir",t);const e=this.find(t.parentPath);if(!e)throw new Error(`Parent dir '${t.parentPath}' not found`);if(!(e instanceof F||e instanceof k))throw new Error("Parent is not a dir or root");const n=new k({parent:e,name:t.name,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})});return e.children.push(n),n}mkAll(t){this.logger.log("Make all",t);const e=t.split(S.sep);let n,i="/";for(const t of e)if(i=S.join(i,t),!this.find(i)){const e=S.dirname(i);n=this.mkDir({parentPath:e,name:t})}return n}removeFromParent(t){const{parent:e}=t;(e instanceof F||e instanceof k)&&(e.children=e.children.filter((e=>e!==t))),t.parent=void 0}notifyOnShowPromptLs(t){this.logger.log("Notify on show prompt",t),this.onShowPromptLs.forEach((e=>e(t)))}notifyOnNewItemConfirmLs(t){this.logger.log("Notify on new item confirm",t),this.onNewItemConfirmLs.forEach((e=>e(t)))}notifyOnDirsChangeLs(t){this.logger.log("Notify on dirs change",t),this.onDirsChangeLs.forEach((e=>e(t)))}}function A(t){return function(t){const e=Array.from(t).reduce(((t,e)=>0|31*t+e.charCodeAt(0)),0);return("0000000"+(e>>>0).toString(16)).slice(-8)}(t)}!function(t){t[t.Stdout=0]="Stdout",t[t.Stderr=1]="Stderr"}(E||(E={}));class T{constructor(t,e){this.contextID=t,this.templateID=e,this.output=[],this.filesystem=new O,this.isReady=!1,this.id=`${t}_${A(e)}`}restart(){this.output=[],this.isReady=!1,this.filesystem.setDirsContent([{dirPath:"/",content:[]}])}logOutput(t,e){this.output=[...this.output,{message:t,source:e}]}}function N(t,{environmentID:e,templateID:n}){const i={type:l.RunningEnvironment.Start,payload:{environmentID:e,templateID:n}};t.send(i)}const L=i(6);class ${constructor(t){var e,n;this.opts=t,this.cmdExitSubscribers=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.termDataSubscribers=[],this.termStartAckSubscribers=[],this.logger=new s(`EvaluationContext [${t.templateID}]`,t.debug),this.env=new T(this.contextID,t.templateID);this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:()=>this.handleConnectionOpen(),onMessage:t=>this.handleConnectionMessage(t),onClose:()=>this.handleConnectionClose()}),this.opts.conn.isOpen&&this.handleConnectionOpen(),this.opts.conn.isClosed&&this.handleConnectionClose(),N(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID}),null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env)}get contextID(){return this.opts.contextID}restart(){this.logger.log("Restart",this.opts.conn.sessionID),this.env.restart(),N(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID})}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({path:t}){return n(this,void 0,void 0,(function*(){let e;this.logger.log("Get file",{filepath:t});const n=new Promise(((t,n)=>{e=t,setTimeout((()=>{n("Timeout")}),1e4)})),i=n=>{n.path.endsWith(t)&&e(n.content)};this.subscribeFileContent(i),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.GetFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t});try{return yield n}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(i)}}))}deleteFile({path:t}){this.logger.log("Delete file",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.RemoveFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}updateFile({path:t,content:e}){return n(this,void 0,void 0,(function*(){let n;this.logger.log("Update file",{filepath:t});const i=new Promise(((t,e)=>{n=t,setTimeout((()=>{e("Timeout")}),1e4)})),s=e=>{e.path.endsWith(t)&&n()};this.subscribeFSWrite(s),function(t,{environmentID:e,path:n,content:i}){const s={type:l.RunningEnvironment.WriteFile,payload:{environmentID:e,path:n,content:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,path:t,content:e});try{yield i}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(s)}}))}createDir({path:t}){this.logger.log("Create dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.CreateDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}listDir({path:t}){this.logger.log("List dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.ListDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}executeCommand({executionID:t,command:e}){return n(this,void 0,void 0,(function*(){let n;this.logger.log("Execute shell command",{executionID:t,command:e});let i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=e=>{e.executionID===t&&(n(t),i=!0)};this.subscribeCmdExit(o),function(t,{environmentID:e,executionID:n,command:i}){const s={type:l.RunningEnvironment.ExecCmd,payload:{environmentID:e,executionID:n,command:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,executionID:t,command:e});try{yield s}catch(t){throw new Error(`Can't exit cmd: ${t}`)}finally{this.unsubscribeCmdExit(o)}}))}startTerminal({terminalID:t}){return n(this,void 0,void 0,(function*(){this.logger.log("Start terminal",{terminalID:t});const e=L();let n,i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=t=>{t.messageID===e&&(n(t.terminalID),i=!0)};this.subscribeTermStartAck(o),function(t,{environmentID:e,terminalID:n,messageID:i}){const s={type:l.RunningEnvironment.TermStart,payload:{environmentID:e,terminalID:n,messageID:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,messageID:e});try{return yield s}catch(t){throw new Error(`Can't start terminal session: ${t}`)}finally{this.unsubscribeTermStartAck(o)}}))}resizeTerminal({terminalID:t,cols:e,rows:n}){this.logger.log("Resize terminal",{terminalID:t,cols:e,rows:n}),function(t,{environmentID:e,terminalID:n,cols:i,rows:s}){const o={type:l.RunningEnvironment.TermResize,payload:{environmentID:e,terminalID:n,cols:i,rows:s}};t.send(o)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,cols:e,rows:n})}sendTerminalData({terminalID:t,data:e}){this.logger.log("Send terminal data",{terminalID:t}),function(t,{environmentID:e,terminalID:n,data:i}){const s={type:l.RunningEnvironment.TermData,payload:{environmentID:e,terminalID:n,data:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,data:e})}onTerminalData({terminalID:t,onData:e}){const n=n=>{n.terminalID===t&&e(n.data)};return this.subscribeTermData(n),()=>this.unsubscribeTermData(n)}subscribeCmdExit(t){this.cmdExitSubscribers.push(t)}unsubscribeCmdExit(t){const e=this.cmdExitSubscribers.indexOf(t);e>-1&&this.cmdExitSubscribers.splice(e,1)}subscribeTermStartAck(t){this.termStartAckSubscribers.push(t)}unsubscribeTermStartAck(t){const e=this.termStartAckSubscribers.indexOf(t);e>-1&&this.termStartAckSubscribers.splice(e,1)}subscribeTermData(t){this.termDataSubscribers.push(t)}unsubscribeTermData(t){const e=this.termDataSubscribers.indexOf(t);e>-1&&this.termDataSubscribers.splice(e,1)}subscribeFileContent(t){this.fileContentSubscribers.push(t)}unsubscribeFileContent(t){const e=this.fileContentSubscribers.indexOf(t);e>-1&&this.fileContentSubscribers.splice(e,1)}subscribeFSWrite(t){this.fsWriteSubscribers.push(t)}unsubscribeFSWrite(t){const e=this.fsWriteSubscribers.indexOf(t);e>-1&&this.fsWriteSubscribers.splice(e,1)}handleConnectionOpen(){var t,e;this.restart(),null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:u.Connecting})}handleConnectionClose(){var t,e;null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:u.Connecting})}handleConnectionMessage(t){switch(this.logger.log("Handling message from remote Runner",{message:t}),t.type){case l.RunningEnvironment.StartAck:{const e=t;this.vmenv_handleStartAck(e.payload);break}case l.RunningEnvironment.CmdOut:{const e=t;this.vmenv_handleCmdOut(e.payload);break}case l.RunningEnvironment.CmdExit:{const e=t;this.vmenv_handleCmdExit(e.payload);break}case l.RunningEnvironment.FSEventWrite:{const e=t;this.vmenv_handleFSEventWrite(e.payload);break}case l.RunningEnvironment.FileContent:{const e=t;this.vmenv_handleFileContent(e.payload);break}case l.RunningEnvironment.FSEventCreate:{const e=t;this.vmenv_handleFSEventCreate(e.payload);break}case l.RunningEnvironment.FSEventRemove:{const e=t;this.vmenv_handleFSEventRemove(e.payload);break}case l.RunningEnvironment.DirContent:{const e=t;this.vmenv_handleDirContent(e.payload);break}case l.RunningEnvironment.Stderr:{const e=t;this.vmenv_handleStderr(e.payload);break}case l.RunningEnvironment.Stdout:{const e=t;this.vmenv_handleStdout(e.payload);break}case l.RunningEnvironment.TermData:{const e=t;this.vmenv_handleTermData(e.payload);break}case l.RunningEnvironment.TermStartAck:{const e=t;this.vmenv_handleTermStartAck(e.payload);break}default:this.logger.warn("Unknown message type",{message:t})}}vmenv_handleTermStartAck(t){this.logger.log('[vmenv] Handling "TermStartAck"',t),this.termStartAckSubscribers.forEach((e=>e(t)))}vmenv_handleTermData(t){this.logger.log('[vmenv] Handling "TermData"',t),this.termDataSubscribers.forEach((e=>e(t)))}vmenv_handleFSEventCreate(t){this.logger.log('[vmenv] Handling "FSEventCreate"',t);const e=S.basename(t.path),n=S.dirname(t.path),i="Directory"===t.type?"Dir":"File";this.env.filesystem.addNodeToDir(n,{name:e,type:i})}vmenv_handleFSEventRemove(t){this.logger.log('[vmenv] Handling "FSEventRemove"',{payload:t});const e=S.basename(t.path),n=S.dirname(t.path);this.env.filesystem.removeNodeFromDir(n,{name:e})}vmenv_handleDirContent(t){this.logger.log('[vmenv] Handling "DirContent"',t);const e=[];for(const n of t.content){const t=S.basename(n.path),i="Directory"===n.type?"Dir":"File";e.push({name:t,type:i})}this.env.filesystem.setDirsContent([{dirPath:t.dirPath,content:e}])}vmenv_handleCmdExit(t){var e,n;this.logger.log('[vmenv] Handling "CmdExit"',t),this.cmdExitSubscribers.forEach((e=>e(t))),void 0!==t.error&&(null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,{environmentID:t.environmentID,executionID:t.executionID,stderr:t.error}))}vmenv_handleFSEventWrite(t){this.logger.log('[vmenv] Handling "FSEventWrite"',t),this.fsWriteSubscribers.forEach((e=>e(t)))}vmenv_handleFileContent(t){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:t.environmentID,path:t.path}),this.fileContentSubscribers.forEach((e=>e(t)))}vmenv_handleStartAck(t){var e,n,i,s;this.logger.log('[vmenv] Handling "StartAck"',{payload:t}),this.env.isReady=!0,null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env),null===(s=(i=this.opts).onSessionChange)||void 0===s||s.call(i,{status:u.Connected})}vmenv_handleCmdOut(t){var e,n;this.logger.log('[vmenv] Handling "CmdOut"',t),null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,t)}vmenv_handleStderr(t){this.logger.log('[vmenv] Handling "Stderr"',t),this.env.logOutput(t.message,E.Stderr)}vmenv_handleStdout(t){this.logger.log('[vmenv] Handling "Stdout"',t),this.env.logOutput(t.message,E.Stdout)}}class M{constructor(){this.logger=new s("Runner"),this.conn=new c({domain:M.config.domain,logging:M.config.logging}),this.sessManager=new g({conn:this.conn,domain:M.config.domain})}static get obj(){if(!M.config)throw new Error("Config not set");return M._obj||(M._obj=new M)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(t){return new $(Object.assign(Object.assign({},t),{conn:this.conn}))}__internal__start(){this.sessManager.start()}__internal__stop(){this.sessManager.stop()}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const j=i(6);var W;exports.DevbookStatus=void 0,(W=exports.DevbookStatus||(exports.DevbookStatus={})).Disconnected="Disconnected",W.Connecting="Connecting",W.Connected="Connected";class z{constructor(t){if(this.opts=t,this.contextID="default",this.executionID=j(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=u.Disconnected,this._status=exports.DevbookStatus.Disconnected,!t.config)throw new Error("Missing Devbook config");const e=()=>this.opts.env,i=()=>this.executionID,s=t=>this.isEnvReady=t,o=t=>this.sessionStatus=t;M.config=Object.assign(Object.assign({},this.opts.config),{logging:t.debug}),this.context=M.obj.createContext({debug:t.debug,templateID:t.env,contextID:this.contextID,onEnvChange(t){t.templateID===e()&&s(t.isReady)},onSessionChange({status:t}){o(t)},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.fs={delete:this.deleteFile.bind(this),listDir:this.listDir.bind(this),createDir:this.createDir.bind(this),get:this.getFile.bind(this),write:this.writeFile.bind(this),addListener:this.env.filesystem.addListener.bind(this.env.filesystem),removeListener:this.env.filesystem.removeListener.bind(this.env.filesystem),serialize:this.env.filesystem.serialize.bind(this.env.filesystem)},this.terminal={createSession:(t,e)=>n(this,void 0,void 0,(function*(){const n=yield this.context.startTerminal({terminalID:e}),i=this.context.onTerminalData({onData:t,terminalID:n});return{destroy:()=>{i()},sendData:t=>{this.context.sendTerminalData({terminalID:n,data:t})},resize:({cols:t,rows:e})=>{this.context.resizeTerminal({terminalID:n,cols:t,rows:e})}}}))}}get isDestroyed(){return this._isDestroyed}set isDestroyed(t){this._isDestroyed=t,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(t){this._isEnvReady=t,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(t){this._sessionStatus=t,this.updateStatus()}get status(){return this._status}set status(t){var e,n;this._status=t,null===(n=(e=this.opts).onStatusChange)||void 0===n||n.call(e,t)}get env(){return this.context.env}runCmd(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.executionID=j(),this.context.executeCommand({executionID:this.executionID,command:t})}destroy(){this.context.destroy(),this.isDestroyed=!0}__internal__start(){M.obj.__internal__start()}__internal__stop(){M.obj.__internal__stop()}listDir(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.listDir({path:t})}createDir(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.createDir({path:t})}deleteFile(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.deleteFile({path:t})}getFile(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({path:t})}writeFile(t,e){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({path:t,content:e})}updateStatus(){if(this.isDestroyed)return void(this.status!==exports.DevbookStatus.Disconnected&&(this.status=exports.DevbookStatus.Disconnected));let t;switch(this.sessionStatus){case u.Disconnected:t=exports.DevbookStatus.Disconnected;break;case u.Connecting:t=exports.DevbookStatus.Connecting;break;case u.Connected:if(!this.isEnvReady){t=exports.DevbookStatus.Connecting;break}t=exports.DevbookStatus.Connected}this.status=t}}exports.Devbook=z,exports.useDevbook=function({env:i,debug:s,config:o,__debug__idleTime:r}){const[a,h]=t.useState(),[l,c]=t.useState(exports.DevbookStatus.Disconnected),[d,u]=t.useState([]),[m,g]=t.useState([]),v=t.useCallback((t=>n(this,void 0,void 0,(function*(){a&&(g([]),u([]),a.runCmd(t))}))),[a]);t.useEffect((function(){const t=new z({debug:s,env:i,config:{domain:o.domain},onStatusChange(t){c(t)},onStderr(t){u((e=>[...e,t]))},onStdout(t){g((e=>[...e,t]))}});return g([]),u([]),h(t),()=>{t.destroy()}}),[i,s,o.domain]);const p=e.useIdleTimer({timeout:r||12e4,throttle:500,onIdle(){null==a||a.__internal__stop()},onAction(){null==a||a.__internal__start()},startOnMount:!1,startManually:!0});return t.useEffect((function(){(null==a?void 0:a.status)===exports.DevbookStatus.Connected&&p.start()}),[a,p]),{stderr:d,stdout:m,runCmd:v,status:l,fs:null==a?void 0:a.fs,terminal:null==a?void 0:a.terminal}};
function n(t,e,n,i){return new(n||(n=Promise))((function(s,o){function r(t){try{h(i.next(t))}catch(t){o(t)}}function a(t){try{h(i.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(r,a)}h((i=i.apply(t,e||[])).next())}))}function i(t){return e="1234567890abcdefghijklmnopqrstuvwxyz",n=t,()=>{let t="",i=n;for(;i--;)t+=e[Math.random()*e.length|0];return t};var e,n}class s{constructor(t,e=!1){this.logID=t,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...t){this.isEnabled&&console.log(`[${this.id()}]`,...t)}warn(...t){this.isEnabled&&console.warn(`[${this.id()}]`,...t)}error(...t){console.error(`[${this.id()} ERROR]`,...t)}}function o(t){return new Promise((e=>setTimeout(e,t)))}var r,a,h;!function(t){t.Error="Runner.Error"}(r||(r={})),function(t){t.Start="RunningEnvironment.Start",t.StartAck="RunningEnvironment.StartAck",t.Eval="RunningEnvironment.Eval",t.FSEventCreate="RunningEnvironment.FSEventCreate",t.FSEventRemove="RunningEnvironment.FSEventRemove",t.FSEventWrite="RunningEnvironment.FSEventWrite",t.CreateDir="RunningEnvironment.CreateDir",t.ListDir="RunningEnvironment.ListDir",t.WriteFile="RunningEnvironment.WriteFile",t.GetFile="RunningEnvironment.GetFile",t.RemoveFile="RunningEnvironment.RemoveFile",t.DirContent="RunningEnvironment.DirContent",t.FileContent="RunningEnvironment.FileContent",t.Stdout="RunningEnvironment.Stdout",t.Stderr="RunningEnvironment.Stderr",t.ExecCmd="RunningEnvironment.ExecCmd",t.KillCmd="RunningEnvironment.KillCmd",t.ListRunningCmds="RunningEnvironment.ListRunningCmds",t.CmdOut="RunningEnvironment.CmdOut",t.CmdExit="RunningEnvironment.CmdExit",t.RunningCmds="RunningEnvironment.RunningCmds",t.TermData="RunningEnvironment.TermData",t.TermResize="RunningEnvironment.TermResize",t.TermStart="RunningEnvironment.TermStart",t.TermStartAck="RunningEnvironment.TermStartAck",t.RunCode="RunningEnvironment.Run"}(a||(a={})),function(t){t.Error="CodeCell.Error"}(h||(h={}));const l={Runner:r,RunningEnvironment:a,CodeCell:h};class c{constructor({domain:t,logging:e}){this.handlers=[],this.url=`wss://${t}`,this.logger=new s("WebSocketConnection",e)}get state(){var t;return null===(t=this.client)||void 0===t?void 0:t.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(t){return this.handlers.push(t),()=>{this.handlers=this.handlers.filter((e=>e!==t))}}connect(t){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(t||this.sessionID?(t?(this.logger.log(`Will try to connect to session "${t}"`),this.sessionID=t):!t&&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.client.onmessage=t=>{this.logger.log("Received (raw)",{msg:t}),this.handleMessage(t)},this.client.onerror=t=>this.handleError(t),this.client.onclose=t=>this.handleClose(t)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(t){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",t),this._send(t)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",t)}close(){var t;this.logger.log("Closing connection"),null===(t=this.client)||void 0===t||t.close(1e3)}handleOpen(){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()))}_send(t){var e,n;null===(e=this.client)||void 0===e||e.send((n=t,JSON.stringify(n,(()=>{const t=new WeakSet;return(e,n)=>{if("object"==typeof n&&null!==n){if(t.has(n))return;t.add(n)}return n}})(),2)))}handleClose(t){return n(this,void 0,void 0,(function*(){this.logger.log("Connection closed",t),this.handlers.forEach((t=>t.onClose())),this.logger.log("Will try to reconnect in 5s"),yield o(5e3),this.connect()}))}handleError(t){var e;this.logger.error("Connection error",t),null===(e=this.client)||void 0===e||e.close()}handleMessage(t){if(!t.data)return void this.logger.error("Message has empty data field",t);const e=JSON.parse(t.data);if(!e.type)return void this.logger.error("Message has no type",e);const n=e;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((t=>t.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var d,u;!function(t){t.Ok="Ok",t.Terminated="Terminated"}(d||(d={}));class m{constructor({id:t,url:e}){this.logger=new s("RunnerSession"),this.lastPing=new Date,this.id=t,this.url=e}ping(){return n(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const t=JSON.stringify({sessionID:this.id});try{const e=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:t}),n=yield e.json();if(!e.ok)throw this.logger.error(e.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(t){throw this.logger.error(t),new Error("Failed to ping active Runner session")}}))}}!function(t){t.Connected="Connected",t.Connecting="Connecting",t.Disconnected="Disconnected"}(u||(u={}));class g{constructor({conn:t,domain:e}){this.logger=new s("SessionManager"),this.userActivity=Promise.resolve(),this.isGettingSessionActive=!1,this.status=u.Disconnected,this.conn=t,this.url=`https://${e}`,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(t){null===t?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${t}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",t))}stop(){var t;null===(t=this.resolveActivity)||void 0===t||t.call(this),this.userActivity=new Promise((t=>{this.resolveActivity=t,this.rejectActivity=t}))}start(){var t;null===(t=this.rejectActivity)||void 0===t||t.call(this),this.userActivity=Promise.resolve()}reset(){this.logger.log("Reset"),this.status=u.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var t;return n(this,void 0,void 0,(function*(){if(this.isGettingSessionActive)return;this.isGettingSessionActive=!0;let e=1;for(;e>0;){this.status=u.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 i=yield fetch(n),s=yield i.json();if(!i.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 4s",i.headers,s),e-=1,yield o(4e3);continue}for(e=1,this.session=new m({id:s.sessionID,url:this.url}),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.userActivity,yield this.session.ping(),yield o(4e3)}catch(t){this.logger.error(`Failed to ping session "${this.session.id}"`,t);break}this.logger.log(`Stopped pinging session "${null===(t=this.session)||void 0===t?void 0:t.id}"`),this.session=void 0,this.status=u.Disconnected,this.conn.close()}catch(t){this.logger.error("Failed to acquire Runner session. Will try again in 4s",t),e-=1,yield o(4e3)}}}))}}function v(t,e){for(var n=0,i=t.length-1;i>=0;i--){var s=t[i];"."===s?t.splice(i,1):".."===s?(t.splice(i,1),n++):n&&(t.splice(i,1),n--)}if(e)for(;n--;n)t.unshift("..");return t}var p=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,f=function(t){return p.exec(t).slice(1)};function D(){for(var t="",e=!1,n=arguments.length-1;n>=-1&&!e;n--){var i=n>=0?arguments[n]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(t=i+"/"+t,e="/"===i.charAt(0))}return(e?"/":"")+(t=v(C(t.split("/"),(function(t){return!!t})),!e).join("/"))||"."}function y(t){var e=b(t),n="/"===w(t,-1);return(t=v(C(t.split("/"),(function(t){return!!t})),!e).join("/"))||e||(t="."),t&&n&&(t+="/"),(e?"/":"")+t}function b(t){return"/"===t.charAt(0)}var S={extname:function(t){return f(t)[3]},basename:function(t,e){var n=f(t)[2];return e&&n.substr(-1*e.length)===e&&(n=n.substr(0,n.length-e.length)),n},dirname:function(t){var e=f(t),n=e[0],i=e[1];return n||i?(i&&(i=i.substr(0,i.length-1)),n+i):"."},sep:"/",delimiter:":",relative:function(t,e){function n(t){for(var e=0;e<t.length&&""===t[e];e++);for(var n=t.length-1;n>=0&&""===t[n];n--);return e>n?[]:t.slice(e,n-e+1)}t=D(t).substr(1),e=D(e).substr(1);for(var i=n(t.split("/")),s=n(e.split("/")),o=Math.min(i.length,s.length),r=o,a=0;a<o;a++)if(i[a]!==s[a]){r=a;break}var h=[];for(a=r;a<i.length;a++)h.push("..");return(h=h.concat(s.slice(r))).join("/")},join:function(){var t=Array.prototype.slice.call(arguments,0);return y(C(t,(function(t,e){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t})).join("/"))},isAbsolute:b,normalize:y,resolve:D};function C(t,e){if(t.filter)return t.filter(e);for(var n=[],i=0;i<t.length;i++)e(t[i],i,t)&&n.push(t[i]);return n}var E,w="b"==="ab".substr(-1)?function(t,e,n){return t.substr(e,n)}:function(t,e,n){return e<0&&(e=t.length+e),t.substr(e,n)};class I{get level(){return S.normalize(this.path).split(S.sep).length-1}}function R(t){return t.sort(((t,e)=>"Prompt"===t.type?-2:t.type!==e.type?"Dir"===t.type?-1:"Dir"===e.type?1:0:t.key.localeCompare(e.key))),t}class k extends I{constructor({name:t,parent:e,onAddItem:n}){super(),this.children=[],this.name=t,this.parent=e,this.addItemHandler=n,this.path=S.join(this.parent.path,this.name)}removeChildNode(t){this.children=this.children.filter((e=>e!==t))}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Dir",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:R(i)}}}class x extends I{constructor({name:t,parent:e}){super(),this.name=t,this.parent=e,this.path=S.join(this.parent.path,this.name)}serialize(){return{type:"File",key:this.path,title:this.name,isLeaf:!0}}}class _ extends I{constructor({parent:t,forNode:e,onConfirm:n,onBlur:i}){super(),this.name="name-prompt",this.parent=t,this.forNode=e,this.path=S.join(t.path,this.name),this.onConfirm=n,this.onBlur=i}serialize(t,e,n){return{type:"Prompt",key:this.path,title:e({onConfirm:t=>{var e;return null===(e=this.onConfirm)||void 0===e?void 0:e.call(this,this,t)},onBlur:()=>{var t;return null===(t=this.onBlur)||void 0===t?void 0:t.call(this,this)}}),isLeaf:!0,selectable:!1,icon:n({type:this.forNode})}}}class F extends I{constructor({onAddItem:t}){super(),this.name="/",this.path="/",this.parent=void 0,this.children=[],this.addItemHandler=t}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Root",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:R(i)}}}class P extends I{constructor({parent:t}){super(),this.name="empty-node",this.parent=t,this.path=S.join(this.parent.path,this.name)}serialize(){return{type:"Empty",key:this.path,title:"Empty Directory",isLeaf:!0,disabled:!0}}}class O{constructor(){this.logger=new s("Filesystem"),this.root=new F({onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}),this.startingPrintIndent=0,this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}removeAllListeners(){this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}addListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs.push(e);break;case"onDirsChange":this.onDirsChangeLs.push(e);break;case"onFileContent":this.onFileContentLs.push(e);break;case"onShowPrompt":this.onShowPromptLs.push(e);break;default:throw new Error(`Unknown event type: ${t}`)}}removeListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs=this.onNewItemConfirmLs.filter((t=>t!==e));break;case"onDirsChange":this.onDirsChangeLs=this.onDirsChangeLs.filter((t=>t!==e));break;case"onFileContent":this.onFileContentLs=this.onFileContentLs.filter((t=>t!==e));break;case"onShowPrompt":this.onShowPromptLs=this.onShowPromptLs.filter((t=>t!==e));break;default:throw new Error(`Unknown event type: ${t}`)}}addNodeToDir(t,e){this.logger.log("Adding node to dir",{dirPath:t,node:e});let n=this.find(t);if(n||(this.logger.log("Dir not found, will make all",{dirPath:t}),n=this.mkAll(t)),!n)throw new Error("dirNode is undefined");if(!(n instanceof F||n instanceof k))throw new Error(`Node at '${t}' is not a directory or a root node`);if(n.children.some((t=>t.name===e.name)))return;let i;i="Dir"===e.type?new k({name:e.name,parent:n,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:e.name,parent:n}),1===n.children.length&&n.children[0]instanceof P?n.children=[i]:n.children.push(i),this.notifyOnDirsChangeLs({dirPaths:[t]})}removeNodeFromDir(t,e){this.logger.log("Remove node from dir",{dirPath:t,nodeName:e.name});const n=this.find(t);if(!n)throw new Error(`No node with path '${t}'`);if(!(n instanceof F||n instanceof k))throw new Error(`Node at '${t}' is not a directory or root node`);const i=n.children.length;n.children=n.children.filter((t=>t.name!==e.name));i!==n.children.length?(0===n.children.length&&(n.children=[new P({parent:n})]),this.notifyOnDirsChangeLs({dirPaths:[t]})):this.logger.warn("Node not found in dir",{dirPath:t,nodeName:e.name})}setFileContent(t){this.logger.log("Update file content",t),this.onFileContentLs.forEach((e=>e(t)))}setDirsContent(t){this.logger.log("Set dirs content",t);for(const e of t){let t=this.find(e.dirPath);if(t||(this.logger.log("Dir not found, will make all",{dirPath:e.dirPath}),t=this.mkAll(e.dirPath)),!t)throw new Error("dirNode is undefined");if(!(t instanceof F||t instanceof k))throw new Error(`Node at '${e.dirPath}' is not a directory or a root node`);const n=t.children.find((t=>t instanceof _));if(e.content.length){const n=[];for(const i of e.content){let e;e="Dir"===i.type?new k({name:i.name,parent:t,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:i.name,parent:t}),n.push(e)}t.children=n}else t.children=[new P({parent:t})];n&&t.children.push(n)}this.notifyOnDirsChangeLs({dirPaths:t.map((t=>t.dirPath))})}showPrompt(t){const{event:e,parent:n,type:i}=t;if(this.logger.log("Show prompt",{parent:n,type:i}),n.children.some((t=>t instanceof _)))return;e.preventDefault();const s=new _({parent:n,forNode:i,onConfirm:((t,e)=>{if(this.logger.log("Prompt confirm",{prompt:t,name:e}),!e)return;const n=t.parent.path,s=S.join(t.parent.path,e);this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[n]}),this.notifyOnNewItemConfirmLs({fullPath:s,name:e,type:i})}).bind(this),onBlur:(t=>{this.logger.log("Prompt blur",{prompt:t});const e=t.parent.path;this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[e]})}).bind(this)});n.children.push(s),this.notifyOnDirsChangeLs({dirPaths:[s.parent.path]}),this.notifyOnShowPromptLs({dirPath:s.parent.path})}serialize(t,e,n){return this.logger.log("Serialize"),[this.root.serialize(t,e,n)]}print(t=this.root,e=this.startingPrintIndent){const n=" ".repeat(e);console.log(n+t.path,`(${t.name})`);for(const i of t.children)i instanceof k?this.print(i,e+1):console.log(n+" "+i.path,`(${i.name})`)}find(t,e=this.root){if(this.logger.log("Find (recursion)",{currentPath:e.path,targetPath:t}),t=S.normalize(t),e.path===t)return e;const n=t.split(S.sep).length-1;if(!(e.level>n)&&(e instanceof F||e instanceof k))for(const n of e.children){const e=this.find(t,n);if(e)return e}}mkDir(t){this.logger.log("Make dir",t);const e=this.find(t.parentPath);if(!e)throw new Error(`Parent dir '${t.parentPath}' not found`);if(!(e instanceof F||e instanceof k))throw new Error("Parent is not a dir or root");const n=new k({parent:e,name:t.name,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})});return e.children.push(n),n}mkAll(t){this.logger.log("Make all",t);const e=t.split(S.sep);let n,i="/";for(const t of e)if(i=S.join(i,t),!this.find(i)){const e=S.dirname(i);n=this.mkDir({parentPath:e,name:t})}return n}removeFromParent(t){const{parent:e}=t;(e instanceof F||e instanceof k)&&(e.children=e.children.filter((e=>e!==t))),t.parent=void 0}notifyOnShowPromptLs(t){this.logger.log("Notify on show prompt",t),this.onShowPromptLs.forEach((e=>e(t)))}notifyOnNewItemConfirmLs(t){this.logger.log("Notify on new item confirm",t),this.onNewItemConfirmLs.forEach((e=>e(t)))}notifyOnDirsChangeLs(t){this.logger.log("Notify on dirs change",t),this.onDirsChangeLs.forEach((e=>e(t)))}}function A(t){return function(t){const e=Array.from(t).reduce(((t,e)=>0|31*t+e.charCodeAt(0)),0);return("0000000"+(e>>>0).toString(16)).slice(-8)}(t)}!function(t){t[t.Stdout=0]="Stdout",t[t.Stderr=1]="Stderr"}(E||(E={}));class T{constructor(t,e){this.contextID=t,this.templateID=e,this.output=[],this.filesystem=new O,this.isReady=!1,this.id=`${t}_${A(e)}`}restart(){this.output=[],this.isReady=!1,this.filesystem.setDirsContent([{dirPath:"/",content:[]}])}logOutput(t,e){this.output=[...this.output,{message:t,source:e}]}}function N(t,{environmentID:e,templateID:n}){const i={type:l.RunningEnvironment.Start,payload:{environmentID:e,templateID:n}};t.send(i)}const L=i(6);class ${constructor(t){var e,n;this.opts=t,this.cmdExitSubscribers=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.termDataSubscribers=[],this.termStartAckSubscribers=[],this.logger=new s(`EvaluationContext [${t.templateID}]`,t.debug),this.env=new T(this.contextID,t.templateID);this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:()=>this.handleConnectionOpen(),onMessage:t=>this.handleConnectionMessage(t),onClose:()=>this.handleConnectionClose()}),this.opts.conn.isOpen&&this.handleConnectionOpen(),this.opts.conn.isClosed&&this.handleConnectionClose(),N(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID}),null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env)}get contextID(){return this.opts.contextID}restart(){this.logger.log("Restart",this.opts.conn.sessionID),this.env.restart(),N(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID})}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({path:t}){return n(this,void 0,void 0,(function*(){let e;this.logger.log("Get file",{filepath:t});const n=new Promise(((t,n)=>{e=t,setTimeout((()=>{n("Timeout")}),1e4)})),i=n=>{n.path.endsWith(t)&&e(n.content)};this.subscribeFileContent(i),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.GetFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t});try{return yield n}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(i)}}))}deleteFile({path:t}){this.logger.log("Delete file",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.RemoveFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}updateFile({path:t,content:e}){return n(this,void 0,void 0,(function*(){let n;this.logger.log("Update file",{filepath:t});const i=new Promise(((t,e)=>{n=t,setTimeout((()=>{e("Timeout")}),1e4)})),s=e=>{e.path.endsWith(t)&&n()};this.subscribeFSWrite(s),function(t,{environmentID:e,path:n,content:i}){const s={type:l.RunningEnvironment.WriteFile,payload:{environmentID:e,path:n,content:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,path:t,content:e});try{yield i}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(s)}}))}createDir({path:t}){this.logger.log("Create dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.CreateDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}listDir({path:t}){this.logger.log("List dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:l.RunningEnvironment.ListDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}executeCommand({executionID:t,command:e}){return n(this,void 0,void 0,(function*(){let n;this.logger.log("Execute shell command",{executionID:t,command:e});const i=new Promise((t=>{n=t})),s=e=>{e.executionID===t&&n(t)};this.subscribeCmdExit(s),function(t,{environmentID:e,executionID:n,command:i}){const s={type:l.RunningEnvironment.ExecCmd,payload:{environmentID:e,executionID:n,command:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,executionID:t,command:e});try{yield i}catch(t){throw new Error(`Can't exit cmd: ${t}`)}finally{this.unsubscribeCmdExit(s)}}))}startTerminal({terminalID:t}){return n(this,void 0,void 0,(function*(){this.logger.log("Start terminal",{terminalID:t});const e=L();let n,i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=t=>{t.messageID===e&&(n(t.terminalID),i=!0)};this.subscribeTermStartAck(o),function(t,{environmentID:e,terminalID:n,messageID:i}){const s={type:l.RunningEnvironment.TermStart,payload:{environmentID:e,terminalID:n,messageID:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,messageID:e});try{return yield s}catch(t){throw new Error(`Can't start terminal session: ${t}`)}finally{this.unsubscribeTermStartAck(o)}}))}resizeTerminal({terminalID:t,cols:e,rows:n}){this.logger.log("Resize terminal",{terminalID:t,cols:e,rows:n}),function(t,{environmentID:e,terminalID:n,cols:i,rows:s}){const o={type:l.RunningEnvironment.TermResize,payload:{environmentID:e,terminalID:n,cols:i,rows:s}};t.send(o)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,cols:e,rows:n})}sendTerminalData({terminalID:t,data:e}){this.logger.log("Send terminal data",{terminalID:t}),function(t,{environmentID:e,terminalID:n,data:i}){const s={type:l.RunningEnvironment.TermData,payload:{environmentID:e,terminalID:n,data:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,data:e})}onTerminalData({terminalID:t,onData:e}){const n=n=>{n.terminalID===t&&e(n.data)};return this.subscribeTermData(n),()=>this.unsubscribeTermData(n)}subscribeCmdExit(t){this.cmdExitSubscribers.push(t)}unsubscribeCmdExit(t){const e=this.cmdExitSubscribers.indexOf(t);e>-1&&this.cmdExitSubscribers.splice(e,1)}subscribeTermStartAck(t){this.termStartAckSubscribers.push(t)}unsubscribeTermStartAck(t){const e=this.termStartAckSubscribers.indexOf(t);e>-1&&this.termStartAckSubscribers.splice(e,1)}subscribeTermData(t){this.termDataSubscribers.push(t)}unsubscribeTermData(t){const e=this.termDataSubscribers.indexOf(t);e>-1&&this.termDataSubscribers.splice(e,1)}subscribeFileContent(t){this.fileContentSubscribers.push(t)}unsubscribeFileContent(t){const e=this.fileContentSubscribers.indexOf(t);e>-1&&this.fileContentSubscribers.splice(e,1)}subscribeFSWrite(t){this.fsWriteSubscribers.push(t)}unsubscribeFSWrite(t){const e=this.fsWriteSubscribers.indexOf(t);e>-1&&this.fsWriteSubscribers.splice(e,1)}handleConnectionOpen(){var t,e;this.restart(),null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:u.Connecting})}handleConnectionClose(){var t,e;null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:u.Connecting})}handleConnectionMessage(t){switch(this.logger.log("Handling message from remote Runner",{message:t}),t.type){case l.RunningEnvironment.StartAck:{const e=t;this.vmenv_handleStartAck(e.payload);break}case l.RunningEnvironment.CmdOut:{const e=t;this.vmenv_handleCmdOut(e.payload);break}case l.RunningEnvironment.CmdExit:{const e=t;this.vmenv_handleCmdExit(e.payload);break}case l.RunningEnvironment.FSEventWrite:{const e=t;this.vmenv_handleFSEventWrite(e.payload);break}case l.RunningEnvironment.FileContent:{const e=t;this.vmenv_handleFileContent(e.payload);break}case l.RunningEnvironment.FSEventCreate:{const e=t;this.vmenv_handleFSEventCreate(e.payload);break}case l.RunningEnvironment.FSEventRemove:{const e=t;this.vmenv_handleFSEventRemove(e.payload);break}case l.RunningEnvironment.DirContent:{const e=t;this.vmenv_handleDirContent(e.payload);break}case l.RunningEnvironment.Stderr:{const e=t;this.vmenv_handleStderr(e.payload);break}case l.RunningEnvironment.Stdout:{const e=t;this.vmenv_handleStdout(e.payload);break}case l.RunningEnvironment.TermData:{const e=t;this.vmenv_handleTermData(e.payload);break}case l.RunningEnvironment.TermStartAck:{const e=t;this.vmenv_handleTermStartAck(e.payload);break}default:this.logger.warn("Unknown message type",{message:t})}}vmenv_handleTermStartAck(t){this.logger.log('[vmenv] Handling "TermStartAck"',t),this.termStartAckSubscribers.forEach((e=>e(t)))}vmenv_handleTermData(t){this.logger.log('[vmenv] Handling "TermData"',t),this.termDataSubscribers.forEach((e=>e(t)))}vmenv_handleFSEventCreate(t){this.logger.log('[vmenv] Handling "FSEventCreate"',t);const e=S.basename(t.path),n=S.dirname(t.path),i="Directory"===t.type?"Dir":"File";this.env.filesystem.addNodeToDir(n,{name:e,type:i})}vmenv_handleFSEventRemove(t){this.logger.log('[vmenv] Handling "FSEventRemove"',{payload:t});const e=S.basename(t.path),n=S.dirname(t.path);this.env.filesystem.removeNodeFromDir(n,{name:e})}vmenv_handleDirContent(t){this.logger.log('[vmenv] Handling "DirContent"',t);const e=[];for(const n of t.content){const t=S.basename(n.path),i="Directory"===n.type?"Dir":"File";e.push({name:t,type:i})}this.env.filesystem.setDirsContent([{dirPath:t.dirPath,content:e}])}vmenv_handleCmdExit(t){var e,n;this.logger.log('[vmenv] Handling "CmdExit"',t),this.cmdExitSubscribers.forEach((e=>e(t))),void 0!==t.error&&(null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,{environmentID:t.environmentID,executionID:t.executionID,stderr:t.error}))}vmenv_handleFSEventWrite(t){this.logger.log('[vmenv] Handling "FSEventWrite"',t),this.fsWriteSubscribers.forEach((e=>e(t)))}vmenv_handleFileContent(t){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:t.environmentID,path:t.path}),this.fileContentSubscribers.forEach((e=>e(t)))}vmenv_handleStartAck(t){var e,n,i,s;this.logger.log('[vmenv] Handling "StartAck"',{payload:t}),this.env.isReady=!0,null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env),null===(s=(i=this.opts).onSessionChange)||void 0===s||s.call(i,{status:u.Connected})}vmenv_handleCmdOut(t){var e,n;this.logger.log('[vmenv] Handling "CmdOut"',t),null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,t)}vmenv_handleStderr(t){this.logger.log('[vmenv] Handling "Stderr"',t),this.env.logOutput(t.message,E.Stderr)}vmenv_handleStdout(t){this.logger.log('[vmenv] Handling "Stdout"',t),this.env.logOutput(t.message,E.Stdout)}}class M{constructor(){this.logger=new s("Runner"),this.conn=new c({domain:M.config.domain,logging:M.config.logging}),this.sessManager=new g({conn:this.conn,domain:M.config.domain})}static get obj(){if(!M.config)throw new Error("Config not set");return M._obj||(M._obj=new M)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(t){return new $(Object.assign(Object.assign({},t),{conn:this.conn}))}__internal__start(){this.sessManager.start()}__internal__stop(){this.sessManager.stop()}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const j=i(6);var W;exports.DevbookStatus=void 0,(W=exports.DevbookStatus||(exports.DevbookStatus={})).Disconnected="Disconnected",W.Connecting="Connecting",W.Connected="Connected";class z{constructor(t){if(this.opts=t,this.contextID="default",this.executionID=j(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=u.Disconnected,this._status=exports.DevbookStatus.Disconnected,!t.config)throw new Error("Missing Devbook config");const e=()=>this.opts.env,i=()=>this.executionID,s=t=>this.isEnvReady=t,o=t=>this.sessionStatus=t;M.config=Object.assign(Object.assign({},this.opts.config),{logging:t.debug}),this.context=M.obj.createContext({debug:t.debug,templateID:t.env,contextID:this.contextID,onEnvChange(t){t.templateID===e()&&s(t.isReady)},onSessionChange({status:t}){o(t)},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.fs={delete:this.deleteFile.bind(this),listDir:this.listDir.bind(this),createDir:this.createDir.bind(this),get:this.getFile.bind(this),write:this.writeFile.bind(this),addListener:this.env.filesystem.addListener.bind(this.env.filesystem),removeListener:this.env.filesystem.removeListener.bind(this.env.filesystem),serialize:this.env.filesystem.serialize.bind(this.env.filesystem)},this.terminal={createSession:(t,e)=>n(this,void 0,void 0,(function*(){const n=yield this.context.startTerminal({terminalID:e}),i=this.context.onTerminalData({onData:t,terminalID:n});return{destroy:()=>{i()},sendData:t=>{this.context.sendTerminalData({terminalID:n,data:t})},resize:({cols:t,rows:e})=>{this.context.resizeTerminal({terminalID:n,cols:t,rows:e})}}}))}}get isDestroyed(){return this._isDestroyed}set isDestroyed(t){this._isDestroyed=t,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(t){this._isEnvReady=t,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(t){this._sessionStatus=t,this.updateStatus()}get status(){return this._status}set status(t){var e,n;this._status=t,null===(n=(e=this.opts).onStatusChange)||void 0===n||n.call(e,t)}get env(){return this.context.env}runCmd(t){return n(this,void 0,void 0,(function*(){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=j(),yield this.context.executeCommand({executionID:this.executionID,command:t})}))}destroy(){this.context.destroy(),this.isDestroyed=!0}__internal__start(){M.obj.__internal__start()}__internal__stop(){M.obj.__internal__stop()}listDir(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.listDir({path:t})}createDir(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.createDir({path:t})}deleteFile(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.deleteFile({path:t})}getFile(t){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({path:t})}writeFile(t,e){if(this.status!==exports.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({path:t,content:e})}updateStatus(){if(this.isDestroyed)return void(this.status!==exports.DevbookStatus.Disconnected&&(this.status=exports.DevbookStatus.Disconnected));let t;switch(this.sessionStatus){case u.Disconnected:t=exports.DevbookStatus.Disconnected;break;case u.Connecting:t=exports.DevbookStatus.Connecting;break;case u.Connected:if(!this.isEnvReady){t=exports.DevbookStatus.Connecting;break}t=exports.DevbookStatus.Connected}this.status=t}}exports.Devbook=z,exports.useDevbook=function({env:i,debug:s,config:o,__debug__idleTime:r}){const[a,h]=t.useState(),[l,c]=t.useState(exports.DevbookStatus.Disconnected),[d,u]=t.useState([]),[m,g]=t.useState([]),v=t.useCallback((t=>n(this,void 0,void 0,(function*(){a&&(g([]),u([]),a.runCmd(t))}))),[a]);t.useEffect((function(){const t=new z({debug:s,env:i,config:{domain:o.domain},onStatusChange(t){c(t)},onStderr(t){u((e=>[...e,t]))},onStdout(t){g((e=>[...e,t]))}});return g([]),u([]),h(t),()=>{t.destroy()}}),[i,s,o.domain]);const p=e.useIdleTimer({timeout:r||6e4,throttle:500,onIdle(){null==a||a.__internal__stop()},onAction(){null==a||a.__internal__start()},startOnMount:!1,startManually:!0});return t.useEffect((function(){(null==a?void 0:a.status)===exports.DevbookStatus.Connected&&p.start()}),[a,p]),{stderr:d,stdout:m,runCmd:v,status:l,fs:null==a?void 0:a.fs,terminal:null==a?void 0:a.terminal}};
//# sourceMappingURL=index.js.map
export declare const STORAGE_SESSION_ID_KEY = "dbk_sdk_session_id";
export declare const SESSION_PING_PERIOD = 4000;
export declare const SESSION_RESTART_PERIOD = 4000;
export declare const SESSION_REQUEST_CAP = 3;
export declare const SESSION_IDLE_TIME = 120000;
export declare const SESSION_REQUEST_CAP = 1;
export declare const SESSION_IDLE_TIME = 60000;
export declare const WSCONN_RESTART_PERIOD = 5000;

@@ -15,3 +15,3 @@ import{useState as t,useCallback as e,useEffect as n}from"react";import{useIdleTimer as i}from"react-idle-timer";

PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function s(t,e,n,i){return new(n||(n=Promise))((function(s,o){function r(t){try{h(i.next(t))}catch(t){o(t)}}function a(t){try{h(i.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(r,a)}h((i=i.apply(t,e||[])).next())}))}function o(t){return e="1234567890abcdefghijklmnopqrstuvwxyz",n=t,()=>{let t="",i=n;for(;i--;)t+=e[Math.random()*e.length|0];return t};var e,n}class r{constructor(t,e=!1){this.logID=t,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...t){this.isEnabled&&console.log(`[${this.id()}]`,...t)}warn(...t){this.isEnabled&&console.warn(`[${this.id()}]`,...t)}error(...t){console.error(`[${this.id()} ERROR]`,...t)}}function a(t){return new Promise((e=>setTimeout(e,t)))}var h,l,c;!function(t){t.Error="Runner.Error"}(h||(h={})),function(t){t.Start="RunningEnvironment.Start",t.StartAck="RunningEnvironment.StartAck",t.Eval="RunningEnvironment.Eval",t.FSEventCreate="RunningEnvironment.FSEventCreate",t.FSEventRemove="RunningEnvironment.FSEventRemove",t.FSEventWrite="RunningEnvironment.FSEventWrite",t.CreateDir="RunningEnvironment.CreateDir",t.ListDir="RunningEnvironment.ListDir",t.WriteFile="RunningEnvironment.WriteFile",t.GetFile="RunningEnvironment.GetFile",t.RemoveFile="RunningEnvironment.RemoveFile",t.DirContent="RunningEnvironment.DirContent",t.FileContent="RunningEnvironment.FileContent",t.Stdout="RunningEnvironment.Stdout",t.Stderr="RunningEnvironment.Stderr",t.ExecCmd="RunningEnvironment.ExecCmd",t.KillCmd="RunningEnvironment.KillCmd",t.ListRunningCmds="RunningEnvironment.ListRunningCmds",t.CmdOut="RunningEnvironment.CmdOut",t.CmdExit="RunningEnvironment.CmdExit",t.RunningCmds="RunningEnvironment.RunningCmds",t.TermData="RunningEnvironment.TermData",t.TermResize="RunningEnvironment.TermResize",t.TermStart="RunningEnvironment.TermStart",t.TermStartAck="RunningEnvironment.TermStartAck",t.RunCode="RunningEnvironment.Run"}(l||(l={})),function(t){t.Error="CodeCell.Error"}(c||(c={}));const d={Runner:h,RunningEnvironment:l,CodeCell:c};class u{constructor({domain:t,logging:e}){this.handlers=[],this.url=`wss://${t}`,this.logger=new r("WebSocketConnection",e)}get state(){var t;return null===(t=this.client)||void 0===t?void 0:t.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(t){return this.handlers.push(t),()=>{this.handlers=this.handlers.filter((e=>e!==t))}}connect(t){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(t||this.sessionID?(t?(this.logger.log(`Will try to connect to session "${t}"`),this.sessionID=t):!t&&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.client.onmessage=t=>{this.logger.log("Received (raw)",{msg:t}),this.handleMessage(t)},this.client.onerror=t=>this.handleError(t),this.client.onclose=t=>this.handleClose(t)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(t){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",t),this._send(t)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",t)}close(){var t;this.logger.log("Closing connection"),null===(t=this.client)||void 0===t||t.close(1e3)}handleOpen(){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()))}_send(t){var e,n;null===(e=this.client)||void 0===e||e.send((n=t,JSON.stringify(n,(()=>{const t=new WeakSet;return(e,n)=>{if("object"==typeof n&&null!==n){if(t.has(n))return;t.add(n)}return n}})(),2)))}handleClose(t){return s(this,void 0,void 0,(function*(){this.logger.log("Connection closed",t),this.handlers.forEach((t=>t.onClose())),this.logger.log("Will try to reconnect in 5s"),yield a(5e3),this.connect()}))}handleError(t){var e;this.logger.error("Connection error",t),null===(e=this.client)||void 0===e||e.close()}handleMessage(t){if(!t.data)return void this.logger.error("Message has empty data field",t);const e=JSON.parse(t.data);if(!e.type)return void this.logger.error("Message has no type",e);const n=e;Object.values(d.RunningEnvironment).includes(n.type)||Object.values(d.Runner).includes(n.type)||Object.values(d.CodeCell).includes(n.type)?n.type!==d.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((t=>t.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var m,g;!function(t){t.Ok="Ok",t.Terminated="Terminated"}(m||(m={}));class v{constructor({id:t,url:e}){this.logger=new r("RunnerSession"),this.lastPing=new Date,this.id=t,this.url=e}ping(){return s(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const t=JSON.stringify({sessionID:this.id});try{const e=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:t}),n=yield e.json();if(!e.ok)throw this.logger.error(e.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(t){throw this.logger.error(t),new Error("Failed to ping active Runner session")}}))}}!function(t){t.Connected="Connected",t.Connecting="Connecting",t.Disconnected="Disconnected"}(g||(g={}));class p{constructor({conn:t,domain:e}){this.logger=new r("SessionManager"),this.userActivity=Promise.resolve(),this.isGettingSessionActive=!1,this.status=g.Disconnected,this.conn=t,this.url=`https://${e}`,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(t){null===t?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${t}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",t))}stop(){var t;null===(t=this.resolveActivity)||void 0===t||t.call(this),this.userActivity=new Promise((t=>{this.resolveActivity=t,this.rejectActivity=t}))}start(){var t;null===(t=this.rejectActivity)||void 0===t||t.call(this),this.userActivity=Promise.resolve()}reset(){this.logger.log("Reset"),this.status=g.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var t;return s(this,void 0,void 0,(function*(){if(this.isGettingSessionActive)return;this.isGettingSessionActive=!0;let e=3;for(;e>0;){this.status=g.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 i=yield fetch(n),s=yield i.json();if(!i.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 4s",i.headers,s),e-=1,yield a(4e3);continue}for(e=3,this.session=new v({id:s.sessionID,url:this.url}),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=g.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.userActivity,yield this.session.ping(),yield a(4e3)}catch(t){this.logger.error(`Failed to ping session "${this.session.id}"`,t);break}this.logger.log(`Stopped pinging session "${null===(t=this.session)||void 0===t?void 0:t.id}"`),this.session=void 0,this.status=g.Disconnected,this.conn.close()}catch(t){this.logger.error("Failed to acquire Runner session. Will try again in 4s",t),e-=1,yield a(4e3)}}}))}}function f(t,e){for(var n=0,i=t.length-1;i>=0;i--){var s=t[i];"."===s?t.splice(i,1):".."===s?(t.splice(i,1),n++):n&&(t.splice(i,1),n--)}if(e)for(;n--;n)t.unshift("..");return t}var D=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,y=function(t){return D.exec(t).slice(1)};function b(){for(var t="",e=!1,n=arguments.length-1;n>=-1&&!e;n--){var i=n>=0?arguments[n]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(t=i+"/"+t,e="/"===i.charAt(0))}return(e?"/":"")+(t=f(E(t.split("/"),(function(t){return!!t})),!e).join("/"))||"."}function C(t){var e=S(t),n="/"===R(t,-1);return(t=f(E(t.split("/"),(function(t){return!!t})),!e).join("/"))||e||(t="."),t&&n&&(t+="/"),(e?"/":"")+t}function S(t){return"/"===t.charAt(0)}var w={extname:function(t){return y(t)[3]},basename:function(t,e){var n=y(t)[2];return e&&n.substr(-1*e.length)===e&&(n=n.substr(0,n.length-e.length)),n},dirname:function(t){var e=y(t),n=e[0],i=e[1];return n||i?(i&&(i=i.substr(0,i.length-1)),n+i):"."},sep:"/",delimiter:":",relative:function(t,e){function n(t){for(var e=0;e<t.length&&""===t[e];e++);for(var n=t.length-1;n>=0&&""===t[n];n--);return e>n?[]:t.slice(e,n-e+1)}t=b(t).substr(1),e=b(e).substr(1);for(var i=n(t.split("/")),s=n(e.split("/")),o=Math.min(i.length,s.length),r=o,a=0;a<o;a++)if(i[a]!==s[a]){r=a;break}var h=[];for(a=r;a<i.length;a++)h.push("..");return(h=h.concat(s.slice(r))).join("/")},join:function(){var t=Array.prototype.slice.call(arguments,0);return C(E(t,(function(t,e){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t})).join("/"))},isAbsolute:S,normalize:C,resolve:b};function E(t,e){if(t.filter)return t.filter(e);for(var n=[],i=0;i<t.length;i++)e(t[i],i,t)&&n.push(t[i]);return n}var I,R="b"==="ab".substr(-1)?function(t,e,n){return t.substr(e,n)}:function(t,e,n){return e<0&&(e=t.length+e),t.substr(e,n)};class _{get level(){return w.normalize(this.path).split(w.sep).length-1}}function k(t){return t.sort(((t,e)=>"Prompt"===t.type?-2:t.type!==e.type?"Dir"===t.type?-1:"Dir"===e.type?1:0:t.key.localeCompare(e.key))),t}class F extends _{constructor({name:t,parent:e,onAddItem:n}){super(),this.children=[],this.name=t,this.parent=e,this.addItemHandler=n,this.path=w.join(this.parent.path,this.name)}removeChildNode(t){this.children=this.children.filter((e=>e!==t))}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Dir",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:k(i)}}}class x extends _{constructor({name:t,parent:e}){super(),this.name=t,this.parent=e,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"File",key:this.path,title:this.name,isLeaf:!0}}}class P extends _{constructor({parent:t,forNode:e,onConfirm:n,onBlur:i}){super(),this.name="name-prompt",this.parent=t,this.forNode=e,this.path=w.join(t.path,this.name),this.onConfirm=n,this.onBlur=i}serialize(t,e,n){return{type:"Prompt",key:this.path,title:e({onConfirm:t=>{var e;return null===(e=this.onConfirm)||void 0===e?void 0:e.call(this,this,t)},onBlur:()=>{var t;return null===(t=this.onBlur)||void 0===t?void 0:t.call(this,this)}}),isLeaf:!0,selectable:!1,icon:n({type:this.forNode})}}}class O extends _{constructor({onAddItem:t}){super(),this.name="/",this.path="/",this.parent=void 0,this.children=[],this.addItemHandler=t}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Root",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:k(i)}}}class A extends _{constructor({parent:t}){super(),this.name="empty-node",this.parent=t,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"Empty",key:this.path,title:"Empty Directory",isLeaf:!0,disabled:!0}}}class T{constructor(){this.logger=new r("Filesystem"),this.root=new O({onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}),this.startingPrintIndent=0,this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}removeAllListeners(){this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}addListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs.push(e);break;case"onDirsChange":this.onDirsChangeLs.push(e);break;case"onFileContent":this.onFileContentLs.push(e);break;case"onShowPrompt":this.onShowPromptLs.push(e);break;default:throw new Error(`Unknown event type: ${t}`)}}removeListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs=this.onNewItemConfirmLs.filter((t=>t!==e));break;case"onDirsChange":this.onDirsChangeLs=this.onDirsChangeLs.filter((t=>t!==e));break;case"onFileContent":this.onFileContentLs=this.onFileContentLs.filter((t=>t!==e));break;case"onShowPrompt":this.onShowPromptLs=this.onShowPromptLs.filter((t=>t!==e));break;default:throw new Error(`Unknown event type: ${t}`)}}addNodeToDir(t,e){this.logger.log("Adding node to dir",{dirPath:t,node:e});let n=this.find(t);if(n||(this.logger.log("Dir not found, will make all",{dirPath:t}),n=this.mkAll(t)),!n)throw new Error("dirNode is undefined");if(!(n instanceof O||n instanceof F))throw new Error(`Node at '${t}' is not a directory or a root node`);if(n.children.some((t=>t.name===e.name)))return;let i;i="Dir"===e.type?new F({name:e.name,parent:n,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:e.name,parent:n}),1===n.children.length&&n.children[0]instanceof A?n.children=[i]:n.children.push(i),this.notifyOnDirsChangeLs({dirPaths:[t]})}removeNodeFromDir(t,e){this.logger.log("Remove node from dir",{dirPath:t,nodeName:e.name});const n=this.find(t);if(!n)throw new Error(`No node with path '${t}'`);if(!(n instanceof O||n instanceof F))throw new Error(`Node at '${t}' is not a directory or root node`);const i=n.children.length;n.children=n.children.filter((t=>t.name!==e.name));i!==n.children.length?(0===n.children.length&&(n.children=[new A({parent:n})]),this.notifyOnDirsChangeLs({dirPaths:[t]})):this.logger.warn("Node not found in dir",{dirPath:t,nodeName:e.name})}setFileContent(t){this.logger.log("Update file content",t),this.onFileContentLs.forEach((e=>e(t)))}setDirsContent(t){this.logger.log("Set dirs content",t);for(const e of t){let t=this.find(e.dirPath);if(t||(this.logger.log("Dir not found, will make all",{dirPath:e.dirPath}),t=this.mkAll(e.dirPath)),!t)throw new Error("dirNode is undefined");if(!(t instanceof O||t instanceof F))throw new Error(`Node at '${e.dirPath}' is not a directory or a root node`);const n=t.children.find((t=>t instanceof P));if(e.content.length){const n=[];for(const i of e.content){let e;e="Dir"===i.type?new F({name:i.name,parent:t,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:i.name,parent:t}),n.push(e)}t.children=n}else t.children=[new A({parent:t})];n&&t.children.push(n)}this.notifyOnDirsChangeLs({dirPaths:t.map((t=>t.dirPath))})}showPrompt(t){const{event:e,parent:n,type:i}=t;if(this.logger.log("Show prompt",{parent:n,type:i}),n.children.some((t=>t instanceof P)))return;e.preventDefault();const s=new P({parent:n,forNode:i,onConfirm:((t,e)=>{if(this.logger.log("Prompt confirm",{prompt:t,name:e}),!e)return;const n=t.parent.path,s=w.join(t.parent.path,e);this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[n]}),this.notifyOnNewItemConfirmLs({fullPath:s,name:e,type:i})}).bind(this),onBlur:(t=>{this.logger.log("Prompt blur",{prompt:t});const e=t.parent.path;this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[e]})}).bind(this)});n.children.push(s),this.notifyOnDirsChangeLs({dirPaths:[s.parent.path]}),this.notifyOnShowPromptLs({dirPath:s.parent.path})}serialize(t,e,n){return this.logger.log("Serialize"),[this.root.serialize(t,e,n)]}print(t=this.root,e=this.startingPrintIndent){const n=" ".repeat(e);console.log(n+t.path,`(${t.name})`);for(const i of t.children)i instanceof F?this.print(i,e+1):console.log(n+" "+i.path,`(${i.name})`)}find(t,e=this.root){if(this.logger.log("Find (recursion)",{currentPath:e.path,targetPath:t}),t=w.normalize(t),e.path===t)return e;const n=t.split(w.sep).length-1;if(!(e.level>n)&&(e instanceof O||e instanceof F))for(const n of e.children){const e=this.find(t,n);if(e)return e}}mkDir(t){this.logger.log("Make dir",t);const e=this.find(t.parentPath);if(!e)throw new Error(`Parent dir '${t.parentPath}' not found`);if(!(e instanceof O||e instanceof F))throw new Error("Parent is not a dir or root");const n=new F({parent:e,name:t.name,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})});return e.children.push(n),n}mkAll(t){this.logger.log("Make all",t);const e=t.split(w.sep);let n,i="/";for(const t of e)if(i=w.join(i,t),!this.find(i)){const e=w.dirname(i);n=this.mkDir({parentPath:e,name:t})}return n}removeFromParent(t){const{parent:e}=t;(e instanceof O||e instanceof F)&&(e.children=e.children.filter((e=>e!==t))),t.parent=void 0}notifyOnShowPromptLs(t){this.logger.log("Notify on show prompt",t),this.onShowPromptLs.forEach((e=>e(t)))}notifyOnNewItemConfirmLs(t){this.logger.log("Notify on new item confirm",t),this.onNewItemConfirmLs.forEach((e=>e(t)))}notifyOnDirsChangeLs(t){this.logger.log("Notify on dirs change",t),this.onDirsChangeLs.forEach((e=>e(t)))}}function N(t){return function(t){const e=Array.from(t).reduce(((t,e)=>0|31*t+e.charCodeAt(0)),0);return("0000000"+(e>>>0).toString(16)).slice(-8)}(t)}!function(t){t[t.Stdout=0]="Stdout",t[t.Stderr=1]="Stderr"}(I||(I={}));class L{constructor(t,e){this.contextID=t,this.templateID=e,this.output=[],this.filesystem=new T,this.isReady=!1,this.id=`${t}_${N(e)}`}restart(){this.output=[],this.isReady=!1,this.filesystem.setDirsContent([{dirPath:"/",content:[]}])}logOutput(t,e){this.output=[...this.output,{message:t,source:e}]}}function $(t,{environmentID:e,templateID:n}){const i={type:d.RunningEnvironment.Start,payload:{environmentID:e,templateID:n}};t.send(i)}const M=o(6);class j{constructor(t){var e,n;this.opts=t,this.cmdExitSubscribers=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.termDataSubscribers=[],this.termStartAckSubscribers=[],this.logger=new r(`EvaluationContext [${t.templateID}]`,t.debug),this.env=new L(this.contextID,t.templateID);this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:()=>this.handleConnectionOpen(),onMessage:t=>this.handleConnectionMessage(t),onClose:()=>this.handleConnectionClose()}),this.opts.conn.isOpen&&this.handleConnectionOpen(),this.opts.conn.isClosed&&this.handleConnectionClose(),$(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID}),null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env)}get contextID(){return this.opts.contextID}restart(){this.logger.log("Restart",this.opts.conn.sessionID),this.env.restart(),$(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID})}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({path:t}){return s(this,void 0,void 0,(function*(){let e;this.logger.log("Get file",{filepath:t});const n=new Promise(((t,n)=>{e=t,setTimeout((()=>{n("Timeout")}),1e4)})),i=n=>{n.path.endsWith(t)&&e(n.content)};this.subscribeFileContent(i),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.GetFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t});try{return yield n}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(i)}}))}deleteFile({path:t}){this.logger.log("Delete file",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.RemoveFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}updateFile({path:t,content:e}){return s(this,void 0,void 0,(function*(){let n;this.logger.log("Update file",{filepath:t});const i=new Promise(((t,e)=>{n=t,setTimeout((()=>{e("Timeout")}),1e4)})),s=e=>{e.path.endsWith(t)&&n()};this.subscribeFSWrite(s),function(t,{environmentID:e,path:n,content:i}){const s={type:d.RunningEnvironment.WriteFile,payload:{environmentID:e,path:n,content:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,path:t,content:e});try{yield i}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(s)}}))}createDir({path:t}){this.logger.log("Create dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.CreateDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}listDir({path:t}){this.logger.log("List dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.ListDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}executeCommand({executionID:t,command:e}){return s(this,void 0,void 0,(function*(){let n;this.logger.log("Execute shell command",{executionID:t,command:e});let i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=e=>{e.executionID===t&&(n(t),i=!0)};this.subscribeCmdExit(o),function(t,{environmentID:e,executionID:n,command:i}){const s={type:d.RunningEnvironment.ExecCmd,payload:{environmentID:e,executionID:n,command:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,executionID:t,command:e});try{yield s}catch(t){throw new Error(`Can't exit cmd: ${t}`)}finally{this.unsubscribeCmdExit(o)}}))}startTerminal({terminalID:t}){return s(this,void 0,void 0,(function*(){this.logger.log("Start terminal",{terminalID:t});const e=M();let n,i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=t=>{t.messageID===e&&(n(t.terminalID),i=!0)};this.subscribeTermStartAck(o),function(t,{environmentID:e,terminalID:n,messageID:i}){const s={type:d.RunningEnvironment.TermStart,payload:{environmentID:e,terminalID:n,messageID:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,messageID:e});try{return yield s}catch(t){throw new Error(`Can't start terminal session: ${t}`)}finally{this.unsubscribeTermStartAck(o)}}))}resizeTerminal({terminalID:t,cols:e,rows:n}){this.logger.log("Resize terminal",{terminalID:t,cols:e,rows:n}),function(t,{environmentID:e,terminalID:n,cols:i,rows:s}){const o={type:d.RunningEnvironment.TermResize,payload:{environmentID:e,terminalID:n,cols:i,rows:s}};t.send(o)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,cols:e,rows:n})}sendTerminalData({terminalID:t,data:e}){this.logger.log("Send terminal data",{terminalID:t}),function(t,{environmentID:e,terminalID:n,data:i}){const s={type:d.RunningEnvironment.TermData,payload:{environmentID:e,terminalID:n,data:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,data:e})}onTerminalData({terminalID:t,onData:e}){const n=n=>{n.terminalID===t&&e(n.data)};return this.subscribeTermData(n),()=>this.unsubscribeTermData(n)}subscribeCmdExit(t){this.cmdExitSubscribers.push(t)}unsubscribeCmdExit(t){const e=this.cmdExitSubscribers.indexOf(t);e>-1&&this.cmdExitSubscribers.splice(e,1)}subscribeTermStartAck(t){this.termStartAckSubscribers.push(t)}unsubscribeTermStartAck(t){const e=this.termStartAckSubscribers.indexOf(t);e>-1&&this.termStartAckSubscribers.splice(e,1)}subscribeTermData(t){this.termDataSubscribers.push(t)}unsubscribeTermData(t){const e=this.termDataSubscribers.indexOf(t);e>-1&&this.termDataSubscribers.splice(e,1)}subscribeFileContent(t){this.fileContentSubscribers.push(t)}unsubscribeFileContent(t){const e=this.fileContentSubscribers.indexOf(t);e>-1&&this.fileContentSubscribers.splice(e,1)}subscribeFSWrite(t){this.fsWriteSubscribers.push(t)}unsubscribeFSWrite(t){const e=this.fsWriteSubscribers.indexOf(t);e>-1&&this.fsWriteSubscribers.splice(e,1)}handleConnectionOpen(){var t,e;this.restart(),null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:g.Connecting})}handleConnectionClose(){var t,e;null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:g.Connecting})}handleConnectionMessage(t){switch(this.logger.log("Handling message from remote Runner",{message:t}),t.type){case d.RunningEnvironment.StartAck:{const e=t;this.vmenv_handleStartAck(e.payload);break}case d.RunningEnvironment.CmdOut:{const e=t;this.vmenv_handleCmdOut(e.payload);break}case d.RunningEnvironment.CmdExit:{const e=t;this.vmenv_handleCmdExit(e.payload);break}case d.RunningEnvironment.FSEventWrite:{const e=t;this.vmenv_handleFSEventWrite(e.payload);break}case d.RunningEnvironment.FileContent:{const e=t;this.vmenv_handleFileContent(e.payload);break}case d.RunningEnvironment.FSEventCreate:{const e=t;this.vmenv_handleFSEventCreate(e.payload);break}case d.RunningEnvironment.FSEventRemove:{const e=t;this.vmenv_handleFSEventRemove(e.payload);break}case d.RunningEnvironment.DirContent:{const e=t;this.vmenv_handleDirContent(e.payload);break}case d.RunningEnvironment.Stderr:{const e=t;this.vmenv_handleStderr(e.payload);break}case d.RunningEnvironment.Stdout:{const e=t;this.vmenv_handleStdout(e.payload);break}case d.RunningEnvironment.TermData:{const e=t;this.vmenv_handleTermData(e.payload);break}case d.RunningEnvironment.TermStartAck:{const e=t;this.vmenv_handleTermStartAck(e.payload);break}default:this.logger.warn("Unknown message type",{message:t})}}vmenv_handleTermStartAck(t){this.logger.log('[vmenv] Handling "TermStartAck"',t),this.termStartAckSubscribers.forEach((e=>e(t)))}vmenv_handleTermData(t){this.logger.log('[vmenv] Handling "TermData"',t),this.termDataSubscribers.forEach((e=>e(t)))}vmenv_handleFSEventCreate(t){this.logger.log('[vmenv] Handling "FSEventCreate"',t);const e=w.basename(t.path),n=w.dirname(t.path),i="Directory"===t.type?"Dir":"File";this.env.filesystem.addNodeToDir(n,{name:e,type:i})}vmenv_handleFSEventRemove(t){this.logger.log('[vmenv] Handling "FSEventRemove"',{payload:t});const e=w.basename(t.path),n=w.dirname(t.path);this.env.filesystem.removeNodeFromDir(n,{name:e})}vmenv_handleDirContent(t){this.logger.log('[vmenv] Handling "DirContent"',t);const e=[];for(const n of t.content){const t=w.basename(n.path),i="Directory"===n.type?"Dir":"File";e.push({name:t,type:i})}this.env.filesystem.setDirsContent([{dirPath:t.dirPath,content:e}])}vmenv_handleCmdExit(t){var e,n;this.logger.log('[vmenv] Handling "CmdExit"',t),this.cmdExitSubscribers.forEach((e=>e(t))),void 0!==t.error&&(null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,{environmentID:t.environmentID,executionID:t.executionID,stderr:t.error}))}vmenv_handleFSEventWrite(t){this.logger.log('[vmenv] Handling "FSEventWrite"',t),this.fsWriteSubscribers.forEach((e=>e(t)))}vmenv_handleFileContent(t){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:t.environmentID,path:t.path}),this.fileContentSubscribers.forEach((e=>e(t)))}vmenv_handleStartAck(t){var e,n,i,s;this.logger.log('[vmenv] Handling "StartAck"',{payload:t}),this.env.isReady=!0,null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env),null===(s=(i=this.opts).onSessionChange)||void 0===s||s.call(i,{status:g.Connected})}vmenv_handleCmdOut(t){var e,n;this.logger.log('[vmenv] Handling "CmdOut"',t),null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,t)}vmenv_handleStderr(t){this.logger.log('[vmenv] Handling "Stderr"',t),this.env.logOutput(t.message,I.Stderr)}vmenv_handleStdout(t){this.logger.log('[vmenv] Handling "Stdout"',t),this.env.logOutput(t.message,I.Stdout)}}class W{constructor(){this.logger=new r("Runner"),this.conn=new u({domain:W.config.domain,logging:W.config.logging}),this.sessManager=new p({conn:this.conn,domain:W.config.domain})}static get obj(){if(!W.config)throw new Error("Config not set");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(t){return new j(Object.assign(Object.assign({},t),{conn:this.conn}))}__internal__start(){this.sessManager.start()}__internal__stop(){this.sessManager.stop()}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const z=o(6);var H;!function(t){t.Disconnected="Disconnected",t.Connecting="Connecting",t.Connected="Connected"}(H||(H={}));class G{constructor(t){if(this.opts=t,this.contextID="default",this.executionID=z(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=g.Disconnected,this._status=H.Disconnected,!t.config)throw new Error("Missing Devbook config");const e=()=>this.opts.env,n=()=>this.executionID,i=t=>this.isEnvReady=t,o=t=>this.sessionStatus=t;W.config=Object.assign(Object.assign({},this.opts.config),{logging:t.debug}),this.context=W.obj.createContext({debug:t.debug,templateID:t.env,contextID:this.contextID,onEnvChange(t){t.templateID===e()&&i(t.isReady)},onSessionChange({status:t}){o(t)},onCmdOut(e){var i,s;e.executionID===n()&&(void 0!==e.stdout&&(null===(i=t.onStdout)||void 0===i||i.call(t,e.stdout)),void 0!==e.stderr&&(null===(s=t.onStderr)||void 0===s||s.call(t,e.stderr)))}}),this.fs={delete:this.deleteFile.bind(this),listDir:this.listDir.bind(this),createDir:this.createDir.bind(this),get:this.getFile.bind(this),write:this.writeFile.bind(this),addListener:this.env.filesystem.addListener.bind(this.env.filesystem),removeListener:this.env.filesystem.removeListener.bind(this.env.filesystem),serialize:this.env.filesystem.serialize.bind(this.env.filesystem)},this.terminal={createSession:(t,e)=>s(this,void 0,void 0,(function*(){const n=yield this.context.startTerminal({terminalID:e}),i=this.context.onTerminalData({onData:t,terminalID:n});return{destroy:()=>{i()},sendData:t=>{this.context.sendTerminalData({terminalID:n,data:t})},resize:({cols:t,rows:e})=>{this.context.resizeTerminal({terminalID:n,cols:t,rows:e})}}}))}}get isDestroyed(){return this._isDestroyed}set isDestroyed(t){this._isDestroyed=t,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(t){this._isEnvReady=t,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(t){this._sessionStatus=t,this.updateStatus()}get status(){return this._status}set status(t){var e,n;this._status=t,null===(n=(e=this.opts).onStatusChange)||void 0===n||n.call(e,t)}get env(){return this.context.env}runCmd(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.executionID=z(),this.context.executeCommand({executionID:this.executionID,command:t})}destroy(){this.context.destroy(),this.isDestroyed=!0}__internal__start(){W.obj.__internal__start()}__internal__stop(){W.obj.__internal__stop()}listDir(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.listDir({path:t})}createDir(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.createDir({path:t})}deleteFile(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.deleteFile({path:t})}getFile(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({path:t})}writeFile(t,e){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({path:t,content:e})}updateStatus(){if(this.isDestroyed)return void(this.status!==H.Disconnected&&(this.status=H.Disconnected));let t;switch(this.sessionStatus){case g.Disconnected:t=H.Disconnected;break;case g.Connecting:t=H.Connecting;break;case g.Connected:if(!this.isEnvReady){t=H.Connecting;break}t=H.Connected}this.status=t}}function V({env:o,debug:r,config:a,__debug__idleTime:h}){const[l,c]=t(),[d,u]=t(H.Disconnected),[m,g]=t([]),[v,p]=t([]),f=e((t=>s(this,void 0,void 0,(function*(){l&&(p([]),g([]),l.runCmd(t))}))),[l]);n((function(){const t=new G({debug:r,env:o,config:{domain:a.domain},onStatusChange(t){u(t)},onStderr(t){g((e=>[...e,t]))},onStdout(t){p((e=>[...e,t]))}});return p([]),g([]),c(t),()=>{t.destroy()}}),[o,r,a.domain]);const D=i({timeout:h||12e4,throttle:500,onIdle(){null==l||l.__internal__stop()},onAction(){null==l||l.__internal__start()},startOnMount:!1,startManually:!0});return n((function(){(null==l?void 0:l.status)===H.Connected&&D.start()}),[l,D]),{stderr:m,stdout:v,runCmd:f,status:d,fs:null==l?void 0:l.fs,terminal:null==l?void 0:l.terminal}}export{G as Devbook,H as DevbookStatus,V as useDevbook};
***************************************************************************** */function s(t,e,n,i){return new(n||(n=Promise))((function(s,o){function r(t){try{h(i.next(t))}catch(t){o(t)}}function a(t){try{h(i.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(r,a)}h((i=i.apply(t,e||[])).next())}))}function o(t){return e="1234567890abcdefghijklmnopqrstuvwxyz",n=t,()=>{let t="",i=n;for(;i--;)t+=e[Math.random()*e.length|0];return t};var e,n}class r{constructor(t,e=!1){this.logID=t,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...t){this.isEnabled&&console.log(`[${this.id()}]`,...t)}warn(...t){this.isEnabled&&console.warn(`[${this.id()}]`,...t)}error(...t){console.error(`[${this.id()} ERROR]`,...t)}}function a(t){return new Promise((e=>setTimeout(e,t)))}var h,l,c;!function(t){t.Error="Runner.Error"}(h||(h={})),function(t){t.Start="RunningEnvironment.Start",t.StartAck="RunningEnvironment.StartAck",t.Eval="RunningEnvironment.Eval",t.FSEventCreate="RunningEnvironment.FSEventCreate",t.FSEventRemove="RunningEnvironment.FSEventRemove",t.FSEventWrite="RunningEnvironment.FSEventWrite",t.CreateDir="RunningEnvironment.CreateDir",t.ListDir="RunningEnvironment.ListDir",t.WriteFile="RunningEnvironment.WriteFile",t.GetFile="RunningEnvironment.GetFile",t.RemoveFile="RunningEnvironment.RemoveFile",t.DirContent="RunningEnvironment.DirContent",t.FileContent="RunningEnvironment.FileContent",t.Stdout="RunningEnvironment.Stdout",t.Stderr="RunningEnvironment.Stderr",t.ExecCmd="RunningEnvironment.ExecCmd",t.KillCmd="RunningEnvironment.KillCmd",t.ListRunningCmds="RunningEnvironment.ListRunningCmds",t.CmdOut="RunningEnvironment.CmdOut",t.CmdExit="RunningEnvironment.CmdExit",t.RunningCmds="RunningEnvironment.RunningCmds",t.TermData="RunningEnvironment.TermData",t.TermResize="RunningEnvironment.TermResize",t.TermStart="RunningEnvironment.TermStart",t.TermStartAck="RunningEnvironment.TermStartAck",t.RunCode="RunningEnvironment.Run"}(l||(l={})),function(t){t.Error="CodeCell.Error"}(c||(c={}));const d={Runner:h,RunningEnvironment:l,CodeCell:c};class u{constructor({domain:t,logging:e}){this.handlers=[],this.url=`wss://${t}`,this.logger=new r("WebSocketConnection",e)}get state(){var t;return null===(t=this.client)||void 0===t?void 0:t.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(t){return this.handlers.push(t),()=>{this.handlers=this.handlers.filter((e=>e!==t))}}connect(t){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(t||this.sessionID?(t?(this.logger.log(`Will try to connect to session "${t}"`),this.sessionID=t):!t&&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.client.onmessage=t=>{this.logger.log("Received (raw)",{msg:t}),this.handleMessage(t)},this.client.onerror=t=>this.handleError(t),this.client.onclose=t=>this.handleClose(t)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(t){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",t),this._send(t)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",t)}close(){var t;this.logger.log("Closing connection"),null===(t=this.client)||void 0===t||t.close(1e3)}handleOpen(){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()))}_send(t){var e,n;null===(e=this.client)||void 0===e||e.send((n=t,JSON.stringify(n,(()=>{const t=new WeakSet;return(e,n)=>{if("object"==typeof n&&null!==n){if(t.has(n))return;t.add(n)}return n}})(),2)))}handleClose(t){return s(this,void 0,void 0,(function*(){this.logger.log("Connection closed",t),this.handlers.forEach((t=>t.onClose())),this.logger.log("Will try to reconnect in 5s"),yield a(5e3),this.connect()}))}handleError(t){var e;this.logger.error("Connection error",t),null===(e=this.client)||void 0===e||e.close()}handleMessage(t){if(!t.data)return void this.logger.error("Message has empty data field",t);const e=JSON.parse(t.data);if(!e.type)return void this.logger.error("Message has no type",e);const n=e;Object.values(d.RunningEnvironment).includes(n.type)||Object.values(d.Runner).includes(n.type)||Object.values(d.CodeCell).includes(n.type)?n.type!==d.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((t=>t.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var m,g;!function(t){t.Ok="Ok",t.Terminated="Terminated"}(m||(m={}));class v{constructor({id:t,url:e}){this.logger=new r("RunnerSession"),this.lastPing=new Date,this.id=t,this.url=e}ping(){return s(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const t=JSON.stringify({sessionID:this.id});try{const e=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:t}),n=yield e.json();if(!e.ok)throw this.logger.error(e.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(t){throw this.logger.error(t),new Error("Failed to ping active Runner session")}}))}}!function(t){t.Connected="Connected",t.Connecting="Connecting",t.Disconnected="Disconnected"}(g||(g={}));class p{constructor({conn:t,domain:e}){this.logger=new r("SessionManager"),this.userActivity=Promise.resolve(),this.isGettingSessionActive=!1,this.status=g.Disconnected,this.conn=t,this.url=`https://${e}`,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem("dbk_sdk_session_id")}set cachedSessionID(t){null===t?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem("dbk_sdk_session_id")):(this.logger.log(`Saved sessionID "${t}" as last sessionID`),sessionStorage.setItem("dbk_sdk_session_id",t))}stop(){var t;null===(t=this.resolveActivity)||void 0===t||t.call(this),this.userActivity=new Promise((t=>{this.resolveActivity=t,this.rejectActivity=t}))}start(){var t;null===(t=this.rejectActivity)||void 0===t||t.call(this),this.userActivity=Promise.resolve()}reset(){this.logger.log("Reset"),this.status=g.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var t;return s(this,void 0,void 0,(function*(){if(this.isGettingSessionActive)return;this.isGettingSessionActive=!0;let e=1;for(;e>0;){this.status=g.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 i=yield fetch(n),s=yield i.json();if(!i.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 4s",i.headers,s),e-=1,yield a(4e3);continue}for(e=1,this.session=new v({id:s.sessionID,url:this.url}),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=g.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.userActivity,yield this.session.ping(),yield a(4e3)}catch(t){this.logger.error(`Failed to ping session "${this.session.id}"`,t);break}this.logger.log(`Stopped pinging session "${null===(t=this.session)||void 0===t?void 0:t.id}"`),this.session=void 0,this.status=g.Disconnected,this.conn.close()}catch(t){this.logger.error("Failed to acquire Runner session. Will try again in 4s",t),e-=1,yield a(4e3)}}}))}}function f(t,e){for(var n=0,i=t.length-1;i>=0;i--){var s=t[i];"."===s?t.splice(i,1):".."===s?(t.splice(i,1),n++):n&&(t.splice(i,1),n--)}if(e)for(;n--;n)t.unshift("..");return t}var D=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,y=function(t){return D.exec(t).slice(1)};function b(){for(var t="",e=!1,n=arguments.length-1;n>=-1&&!e;n--){var i=n>=0?arguments[n]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(t=i+"/"+t,e="/"===i.charAt(0))}return(e?"/":"")+(t=f(E(t.split("/"),(function(t){return!!t})),!e).join("/"))||"."}function C(t){var e=S(t),n="/"===R(t,-1);return(t=f(E(t.split("/"),(function(t){return!!t})),!e).join("/"))||e||(t="."),t&&n&&(t+="/"),(e?"/":"")+t}function S(t){return"/"===t.charAt(0)}var w={extname:function(t){return y(t)[3]},basename:function(t,e){var n=y(t)[2];return e&&n.substr(-1*e.length)===e&&(n=n.substr(0,n.length-e.length)),n},dirname:function(t){var e=y(t),n=e[0],i=e[1];return n||i?(i&&(i=i.substr(0,i.length-1)),n+i):"."},sep:"/",delimiter:":",relative:function(t,e){function n(t){for(var e=0;e<t.length&&""===t[e];e++);for(var n=t.length-1;n>=0&&""===t[n];n--);return e>n?[]:t.slice(e,n-e+1)}t=b(t).substr(1),e=b(e).substr(1);for(var i=n(t.split("/")),s=n(e.split("/")),o=Math.min(i.length,s.length),r=o,a=0;a<o;a++)if(i[a]!==s[a]){r=a;break}var h=[];for(a=r;a<i.length;a++)h.push("..");return(h=h.concat(s.slice(r))).join("/")},join:function(){var t=Array.prototype.slice.call(arguments,0);return C(E(t,(function(t,e){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t})).join("/"))},isAbsolute:S,normalize:C,resolve:b};function E(t,e){if(t.filter)return t.filter(e);for(var n=[],i=0;i<t.length;i++)e(t[i],i,t)&&n.push(t[i]);return n}var I,R="b"==="ab".substr(-1)?function(t,e,n){return t.substr(e,n)}:function(t,e,n){return e<0&&(e=t.length+e),t.substr(e,n)};class _{get level(){return w.normalize(this.path).split(w.sep).length-1}}function k(t){return t.sort(((t,e)=>"Prompt"===t.type?-2:t.type!==e.type?"Dir"===t.type?-1:"Dir"===e.type?1:0:t.key.localeCompare(e.key))),t}class F extends _{constructor({name:t,parent:e,onAddItem:n}){super(),this.children=[],this.name=t,this.parent=e,this.addItemHandler=n,this.path=w.join(this.parent.path,this.name)}removeChildNode(t){this.children=this.children.filter((e=>e!==t))}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Dir",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:k(i)}}}class x extends _{constructor({name:t,parent:e}){super(),this.name=t,this.parent=e,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"File",key:this.path,title:this.name,isLeaf:!0}}}class P extends _{constructor({parent:t,forNode:e,onConfirm:n,onBlur:i}){super(),this.name="name-prompt",this.parent=t,this.forNode=e,this.path=w.join(t.path,this.name),this.onConfirm=n,this.onBlur=i}serialize(t,e,n){return{type:"Prompt",key:this.path,title:e({onConfirm:t=>{var e;return null===(e=this.onConfirm)||void 0===e?void 0:e.call(this,this,t)},onBlur:()=>{var t;return null===(t=this.onBlur)||void 0===t?void 0:t.call(this,this)}}),isLeaf:!0,selectable:!1,icon:n({type:this.forNode})}}}class O extends _{constructor({onAddItem:t}){super(),this.name="/",this.path="/",this.parent=void 0,this.children=[],this.addItemHandler=t}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Root",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:k(i)}}}class A extends _{constructor({parent:t}){super(),this.name="empty-node",this.parent=t,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"Empty",key:this.path,title:"Empty Directory",isLeaf:!0,disabled:!0}}}class N{constructor(){this.logger=new r("Filesystem"),this.root=new O({onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}),this.startingPrintIndent=0,this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}removeAllListeners(){this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}addListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs.push(e);break;case"onDirsChange":this.onDirsChangeLs.push(e);break;case"onFileContent":this.onFileContentLs.push(e);break;case"onShowPrompt":this.onShowPromptLs.push(e);break;default:throw new Error(`Unknown event type: ${t}`)}}removeListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs=this.onNewItemConfirmLs.filter((t=>t!==e));break;case"onDirsChange":this.onDirsChangeLs=this.onDirsChangeLs.filter((t=>t!==e));break;case"onFileContent":this.onFileContentLs=this.onFileContentLs.filter((t=>t!==e));break;case"onShowPrompt":this.onShowPromptLs=this.onShowPromptLs.filter((t=>t!==e));break;default:throw new Error(`Unknown event type: ${t}`)}}addNodeToDir(t,e){this.logger.log("Adding node to dir",{dirPath:t,node:e});let n=this.find(t);if(n||(this.logger.log("Dir not found, will make all",{dirPath:t}),n=this.mkAll(t)),!n)throw new Error("dirNode is undefined");if(!(n instanceof O||n instanceof F))throw new Error(`Node at '${t}' is not a directory or a root node`);if(n.children.some((t=>t.name===e.name)))return;let i;i="Dir"===e.type?new F({name:e.name,parent:n,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:e.name,parent:n}),1===n.children.length&&n.children[0]instanceof A?n.children=[i]:n.children.push(i),this.notifyOnDirsChangeLs({dirPaths:[t]})}removeNodeFromDir(t,e){this.logger.log("Remove node from dir",{dirPath:t,nodeName:e.name});const n=this.find(t);if(!n)throw new Error(`No node with path '${t}'`);if(!(n instanceof O||n instanceof F))throw new Error(`Node at '${t}' is not a directory or root node`);const i=n.children.length;n.children=n.children.filter((t=>t.name!==e.name));i!==n.children.length?(0===n.children.length&&(n.children=[new A({parent:n})]),this.notifyOnDirsChangeLs({dirPaths:[t]})):this.logger.warn("Node not found in dir",{dirPath:t,nodeName:e.name})}setFileContent(t){this.logger.log("Update file content",t),this.onFileContentLs.forEach((e=>e(t)))}setDirsContent(t){this.logger.log("Set dirs content",t);for(const e of t){let t=this.find(e.dirPath);if(t||(this.logger.log("Dir not found, will make all",{dirPath:e.dirPath}),t=this.mkAll(e.dirPath)),!t)throw new Error("dirNode is undefined");if(!(t instanceof O||t instanceof F))throw new Error(`Node at '${e.dirPath}' is not a directory or a root node`);const n=t.children.find((t=>t instanceof P));if(e.content.length){const n=[];for(const i of e.content){let e;e="Dir"===i.type?new F({name:i.name,parent:t,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new x({name:i.name,parent:t}),n.push(e)}t.children=n}else t.children=[new A({parent:t})];n&&t.children.push(n)}this.notifyOnDirsChangeLs({dirPaths:t.map((t=>t.dirPath))})}showPrompt(t){const{event:e,parent:n,type:i}=t;if(this.logger.log("Show prompt",{parent:n,type:i}),n.children.some((t=>t instanceof P)))return;e.preventDefault();const s=new P({parent:n,forNode:i,onConfirm:((t,e)=>{if(this.logger.log("Prompt confirm",{prompt:t,name:e}),!e)return;const n=t.parent.path,s=w.join(t.parent.path,e);this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[n]}),this.notifyOnNewItemConfirmLs({fullPath:s,name:e,type:i})}).bind(this),onBlur:(t=>{this.logger.log("Prompt blur",{prompt:t});const e=t.parent.path;this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[e]})}).bind(this)});n.children.push(s),this.notifyOnDirsChangeLs({dirPaths:[s.parent.path]}),this.notifyOnShowPromptLs({dirPath:s.parent.path})}serialize(t,e,n){return this.logger.log("Serialize"),[this.root.serialize(t,e,n)]}print(t=this.root,e=this.startingPrintIndent){const n=" ".repeat(e);console.log(n+t.path,`(${t.name})`);for(const i of t.children)i instanceof F?this.print(i,e+1):console.log(n+" "+i.path,`(${i.name})`)}find(t,e=this.root){if(this.logger.log("Find (recursion)",{currentPath:e.path,targetPath:t}),t=w.normalize(t),e.path===t)return e;const n=t.split(w.sep).length-1;if(!(e.level>n)&&(e instanceof O||e instanceof F))for(const n of e.children){const e=this.find(t,n);if(e)return e}}mkDir(t){this.logger.log("Make dir",t);const e=this.find(t.parentPath);if(!e)throw new Error(`Parent dir '${t.parentPath}' not found`);if(!(e instanceof O||e instanceof F))throw new Error("Parent is not a dir or root");const n=new F({parent:e,name:t.name,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})});return e.children.push(n),n}mkAll(t){this.logger.log("Make all",t);const e=t.split(w.sep);let n,i="/";for(const t of e)if(i=w.join(i,t),!this.find(i)){const e=w.dirname(i);n=this.mkDir({parentPath:e,name:t})}return n}removeFromParent(t){const{parent:e}=t;(e instanceof O||e instanceof F)&&(e.children=e.children.filter((e=>e!==t))),t.parent=void 0}notifyOnShowPromptLs(t){this.logger.log("Notify on show prompt",t),this.onShowPromptLs.forEach((e=>e(t)))}notifyOnNewItemConfirmLs(t){this.logger.log("Notify on new item confirm",t),this.onNewItemConfirmLs.forEach((e=>e(t)))}notifyOnDirsChangeLs(t){this.logger.log("Notify on dirs change",t),this.onDirsChangeLs.forEach((e=>e(t)))}}function T(t){return function(t){const e=Array.from(t).reduce(((t,e)=>0|31*t+e.charCodeAt(0)),0);return("0000000"+(e>>>0).toString(16)).slice(-8)}(t)}!function(t){t[t.Stdout=0]="Stdout",t[t.Stderr=1]="Stderr"}(I||(I={}));class L{constructor(t,e){this.contextID=t,this.templateID=e,this.output=[],this.filesystem=new N,this.isReady=!1,this.id=`${t}_${T(e)}`}restart(){this.output=[],this.isReady=!1,this.filesystem.setDirsContent([{dirPath:"/",content:[]}])}logOutput(t,e){this.output=[...this.output,{message:t,source:e}]}}function $(t,{environmentID:e,templateID:n}){const i={type:d.RunningEnvironment.Start,payload:{environmentID:e,templateID:n}};t.send(i)}const M=o(6);class j{constructor(t){var e,n;this.opts=t,this.cmdExitSubscribers=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.termDataSubscribers=[],this.termStartAckSubscribers=[],this.logger=new r(`EvaluationContext [${t.templateID}]`,t.debug),this.env=new L(this.contextID,t.templateID);this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:()=>this.handleConnectionOpen(),onMessage:t=>this.handleConnectionMessage(t),onClose:()=>this.handleConnectionClose()}),this.opts.conn.isOpen&&this.handleConnectionOpen(),this.opts.conn.isClosed&&this.handleConnectionClose(),$(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID}),null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env)}get contextID(){return this.opts.contextID}restart(){this.logger.log("Restart",this.opts.conn.sessionID),this.env.restart(),$(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID})}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({path:t}){return s(this,void 0,void 0,(function*(){let e;this.logger.log("Get file",{filepath:t});const n=new Promise(((t,n)=>{e=t,setTimeout((()=>{n("Timeout")}),1e4)})),i=n=>{n.path.endsWith(t)&&e(n.content)};this.subscribeFileContent(i),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.GetFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t});try{return yield n}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(i)}}))}deleteFile({path:t}){this.logger.log("Delete file",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.RemoveFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}updateFile({path:t,content:e}){return s(this,void 0,void 0,(function*(){let n;this.logger.log("Update file",{filepath:t});const i=new Promise(((t,e)=>{n=t,setTimeout((()=>{e("Timeout")}),1e4)})),s=e=>{e.path.endsWith(t)&&n()};this.subscribeFSWrite(s),function(t,{environmentID:e,path:n,content:i}){const s={type:d.RunningEnvironment.WriteFile,payload:{environmentID:e,path:n,content:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,path:t,content:e});try{yield i}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(s)}}))}createDir({path:t}){this.logger.log("Create dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.CreateDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}listDir({path:t}){this.logger.log("List dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:d.RunningEnvironment.ListDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}executeCommand({executionID:t,command:e}){return s(this,void 0,void 0,(function*(){let n;this.logger.log("Execute shell command",{executionID:t,command:e});const i=new Promise((t=>{n=t})),s=e=>{e.executionID===t&&n(t)};this.subscribeCmdExit(s),function(t,{environmentID:e,executionID:n,command:i}){const s={type:d.RunningEnvironment.ExecCmd,payload:{environmentID:e,executionID:n,command:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,executionID:t,command:e});try{yield i}catch(t){throw new Error(`Can't exit cmd: ${t}`)}finally{this.unsubscribeCmdExit(s)}}))}startTerminal({terminalID:t}){return s(this,void 0,void 0,(function*(){this.logger.log("Start terminal",{terminalID:t});const e=M();let n,i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=t=>{t.messageID===e&&(n(t.terminalID),i=!0)};this.subscribeTermStartAck(o),function(t,{environmentID:e,terminalID:n,messageID:i}){const s={type:d.RunningEnvironment.TermStart,payload:{environmentID:e,terminalID:n,messageID:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,messageID:e});try{return yield s}catch(t){throw new Error(`Can't start terminal session: ${t}`)}finally{this.unsubscribeTermStartAck(o)}}))}resizeTerminal({terminalID:t,cols:e,rows:n}){this.logger.log("Resize terminal",{terminalID:t,cols:e,rows:n}),function(t,{environmentID:e,terminalID:n,cols:i,rows:s}){const o={type:d.RunningEnvironment.TermResize,payload:{environmentID:e,terminalID:n,cols:i,rows:s}};t.send(o)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,cols:e,rows:n})}sendTerminalData({terminalID:t,data:e}){this.logger.log("Send terminal data",{terminalID:t}),function(t,{environmentID:e,terminalID:n,data:i}){const s={type:d.RunningEnvironment.TermData,payload:{environmentID:e,terminalID:n,data:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,data:e})}onTerminalData({terminalID:t,onData:e}){const n=n=>{n.terminalID===t&&e(n.data)};return this.subscribeTermData(n),()=>this.unsubscribeTermData(n)}subscribeCmdExit(t){this.cmdExitSubscribers.push(t)}unsubscribeCmdExit(t){const e=this.cmdExitSubscribers.indexOf(t);e>-1&&this.cmdExitSubscribers.splice(e,1)}subscribeTermStartAck(t){this.termStartAckSubscribers.push(t)}unsubscribeTermStartAck(t){const e=this.termStartAckSubscribers.indexOf(t);e>-1&&this.termStartAckSubscribers.splice(e,1)}subscribeTermData(t){this.termDataSubscribers.push(t)}unsubscribeTermData(t){const e=this.termDataSubscribers.indexOf(t);e>-1&&this.termDataSubscribers.splice(e,1)}subscribeFileContent(t){this.fileContentSubscribers.push(t)}unsubscribeFileContent(t){const e=this.fileContentSubscribers.indexOf(t);e>-1&&this.fileContentSubscribers.splice(e,1)}subscribeFSWrite(t){this.fsWriteSubscribers.push(t)}unsubscribeFSWrite(t){const e=this.fsWriteSubscribers.indexOf(t);e>-1&&this.fsWriteSubscribers.splice(e,1)}handleConnectionOpen(){var t,e;this.restart(),null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:g.Connecting})}handleConnectionClose(){var t,e;null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:g.Connecting})}handleConnectionMessage(t){switch(this.logger.log("Handling message from remote Runner",{message:t}),t.type){case d.RunningEnvironment.StartAck:{const e=t;this.vmenv_handleStartAck(e.payload);break}case d.RunningEnvironment.CmdOut:{const e=t;this.vmenv_handleCmdOut(e.payload);break}case d.RunningEnvironment.CmdExit:{const e=t;this.vmenv_handleCmdExit(e.payload);break}case d.RunningEnvironment.FSEventWrite:{const e=t;this.vmenv_handleFSEventWrite(e.payload);break}case d.RunningEnvironment.FileContent:{const e=t;this.vmenv_handleFileContent(e.payload);break}case d.RunningEnvironment.FSEventCreate:{const e=t;this.vmenv_handleFSEventCreate(e.payload);break}case d.RunningEnvironment.FSEventRemove:{const e=t;this.vmenv_handleFSEventRemove(e.payload);break}case d.RunningEnvironment.DirContent:{const e=t;this.vmenv_handleDirContent(e.payload);break}case d.RunningEnvironment.Stderr:{const e=t;this.vmenv_handleStderr(e.payload);break}case d.RunningEnvironment.Stdout:{const e=t;this.vmenv_handleStdout(e.payload);break}case d.RunningEnvironment.TermData:{const e=t;this.vmenv_handleTermData(e.payload);break}case d.RunningEnvironment.TermStartAck:{const e=t;this.vmenv_handleTermStartAck(e.payload);break}default:this.logger.warn("Unknown message type",{message:t})}}vmenv_handleTermStartAck(t){this.logger.log('[vmenv] Handling "TermStartAck"',t),this.termStartAckSubscribers.forEach((e=>e(t)))}vmenv_handleTermData(t){this.logger.log('[vmenv] Handling "TermData"',t),this.termDataSubscribers.forEach((e=>e(t)))}vmenv_handleFSEventCreate(t){this.logger.log('[vmenv] Handling "FSEventCreate"',t);const e=w.basename(t.path),n=w.dirname(t.path),i="Directory"===t.type?"Dir":"File";this.env.filesystem.addNodeToDir(n,{name:e,type:i})}vmenv_handleFSEventRemove(t){this.logger.log('[vmenv] Handling "FSEventRemove"',{payload:t});const e=w.basename(t.path),n=w.dirname(t.path);this.env.filesystem.removeNodeFromDir(n,{name:e})}vmenv_handleDirContent(t){this.logger.log('[vmenv] Handling "DirContent"',t);const e=[];for(const n of t.content){const t=w.basename(n.path),i="Directory"===n.type?"Dir":"File";e.push({name:t,type:i})}this.env.filesystem.setDirsContent([{dirPath:t.dirPath,content:e}])}vmenv_handleCmdExit(t){var e,n;this.logger.log('[vmenv] Handling "CmdExit"',t),this.cmdExitSubscribers.forEach((e=>e(t))),void 0!==t.error&&(null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,{environmentID:t.environmentID,executionID:t.executionID,stderr:t.error}))}vmenv_handleFSEventWrite(t){this.logger.log('[vmenv] Handling "FSEventWrite"',t),this.fsWriteSubscribers.forEach((e=>e(t)))}vmenv_handleFileContent(t){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:t.environmentID,path:t.path}),this.fileContentSubscribers.forEach((e=>e(t)))}vmenv_handleStartAck(t){var e,n,i,s;this.logger.log('[vmenv] Handling "StartAck"',{payload:t}),this.env.isReady=!0,null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env),null===(s=(i=this.opts).onSessionChange)||void 0===s||s.call(i,{status:g.Connected})}vmenv_handleCmdOut(t){var e,n;this.logger.log('[vmenv] Handling "CmdOut"',t),null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,t)}vmenv_handleStderr(t){this.logger.log('[vmenv] Handling "Stderr"',t),this.env.logOutput(t.message,I.Stderr)}vmenv_handleStdout(t){this.logger.log('[vmenv] Handling "Stdout"',t),this.env.logOutput(t.message,I.Stdout)}}class W{constructor(){this.logger=new r("Runner"),this.conn=new u({domain:W.config.domain,logging:W.config.logging}),this.sessManager=new p({conn:this.conn,domain:W.config.domain})}static get obj(){if(!W.config)throw new Error("Config not set");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(t){return new j(Object.assign(Object.assign({},t),{conn:this.conn}))}__internal__start(){this.sessManager.start()}__internal__stop(){this.sessManager.stop()}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const z=o(6);var H;!function(t){t.Disconnected="Disconnected",t.Connecting="Connecting",t.Connected="Connected"}(H||(H={}));class G{constructor(t){if(this.opts=t,this.contextID="default",this.executionID=z(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=g.Disconnected,this._status=H.Disconnected,!t.config)throw new Error("Missing Devbook config");const e=()=>this.opts.env,n=()=>this.executionID,i=t=>this.isEnvReady=t,o=t=>this.sessionStatus=t;W.config=Object.assign(Object.assign({},this.opts.config),{logging:t.debug}),this.context=W.obj.createContext({debug:t.debug,templateID:t.env,contextID:this.contextID,onEnvChange(t){t.templateID===e()&&i(t.isReady)},onSessionChange({status:t}){o(t)},onCmdOut(e){var i,s;e.executionID===n()&&(void 0!==e.stdout&&(null===(i=t.onStdout)||void 0===i||i.call(t,e.stdout)),void 0!==e.stderr&&(null===(s=t.onStderr)||void 0===s||s.call(t,e.stderr)))}}),this.fs={delete:this.deleteFile.bind(this),listDir:this.listDir.bind(this),createDir:this.createDir.bind(this),get:this.getFile.bind(this),write:this.writeFile.bind(this),addListener:this.env.filesystem.addListener.bind(this.env.filesystem),removeListener:this.env.filesystem.removeListener.bind(this.env.filesystem),serialize:this.env.filesystem.serialize.bind(this.env.filesystem)},this.terminal={createSession:(t,e)=>s(this,void 0,void 0,(function*(){const n=yield this.context.startTerminal({terminalID:e}),i=this.context.onTerminalData({onData:t,terminalID:n});return{destroy:()=>{i()},sendData:t=>{this.context.sendTerminalData({terminalID:n,data:t})},resize:({cols:t,rows:e})=>{this.context.resizeTerminal({terminalID:n,cols:t,rows:e})}}}))}}get isDestroyed(){return this._isDestroyed}set isDestroyed(t){this._isDestroyed=t,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(t){this._isEnvReady=t,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(t){this._sessionStatus=t,this.updateStatus()}get status(){return this._status}set status(t){var e,n;this._status=t,null===(n=(e=this.opts).onStatusChange)||void 0===n||n.call(e,t)}get env(){return this.context.env}runCmd(t){return s(this,void 0,void 0,(function*(){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");this.executionID=z(),yield this.context.executeCommand({executionID:this.executionID,command:t})}))}destroy(){this.context.destroy(),this.isDestroyed=!0}__internal__start(){W.obj.__internal__start()}__internal__stop(){W.obj.__internal__stop()}listDir(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.listDir({path:t})}createDir(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.createDir({path:t})}deleteFile(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.deleteFile({path:t})}getFile(t){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({path:t})}writeFile(t,e){if(this.status!==H.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({path:t,content:e})}updateStatus(){if(this.isDestroyed)return void(this.status!==H.Disconnected&&(this.status=H.Disconnected));let t;switch(this.sessionStatus){case g.Disconnected:t=H.Disconnected;break;case g.Connecting:t=H.Connecting;break;case g.Connected:if(!this.isEnvReady){t=H.Connecting;break}t=H.Connected}this.status=t}}function V({env:o,debug:r,config:a,__debug__idleTime:h}){const[l,c]=t(),[d,u]=t(H.Disconnected),[m,g]=t([]),[v,p]=t([]),f=e((t=>s(this,void 0,void 0,(function*(){l&&(p([]),g([]),l.runCmd(t))}))),[l]);n((function(){const t=new G({debug:r,env:o,config:{domain:a.domain},onStatusChange(t){u(t)},onStderr(t){g((e=>[...e,t]))},onStdout(t){p((e=>[...e,t]))}});return p([]),g([]),c(t),()=>{t.destroy()}}),[o,r,a.domain]);const D=i({timeout:h||6e4,throttle:500,onIdle(){null==l||l.__internal__stop()},onAction(){null==l||l.__internal__start()},startOnMount:!1,startManually:!0});return n((function(){(null==l?void 0:l.status)===H.Connected&&D.start()}),[l,D]),{stderr:m,stdout:v,runCmd:f,status:d,fs:null==l?void 0:l.fs,terminal:null==l?void 0:l.terminal}}export{G as Devbook,H as DevbookStatus,V as useDevbook};
//# sourceMappingURL=index.js.map
export declare const STORAGE_SESSION_ID_KEY = "dbk_sdk_session_id";
export declare const SESSION_PING_PERIOD = 4000;
export declare const SESSION_RESTART_PERIOD = 4000;
export declare const SESSION_REQUEST_CAP = 3;
export declare const SESSION_IDLE_TIME = 120000;
export declare const SESSION_REQUEST_CAP = 1;
export declare const SESSION_IDLE_TIME = 60000;
export declare const WSCONN_RESTART_PERIOD = 5000;

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

PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function i(t,e,n,i){return new(n||(n=Promise))((function(s,o){function r(t){try{h(i.next(t))}catch(t){o(t)}}function a(t){try{h(i.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(r,a)}h((i=i.apply(t,e||[])).next())}))}const s="dbk_sdk_session_id",o=4e3;function r(t){return e="1234567890abcdefghijklmnopqrstuvwxyz",n=t,()=>{let t="",i=n;for(;i--;)t+=e[Math.random()*e.length|0];return t};var e,n}class a{constructor(t,e=!1){this.logID=t,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...t){this.isEnabled&&console.log(`[${this.id()}]`,...t)}warn(...t){this.isEnabled&&console.warn(`[${this.id()}]`,...t)}error(...t){console.error(`[${this.id()} ERROR]`,...t)}}function h(t){return new Promise((e=>setTimeout(e,t)))}var l,c,d;!function(t){t.Error="Runner.Error"}(l||(l={})),function(t){t.Start="RunningEnvironment.Start",t.StartAck="RunningEnvironment.StartAck",t.Eval="RunningEnvironment.Eval",t.FSEventCreate="RunningEnvironment.FSEventCreate",t.FSEventRemove="RunningEnvironment.FSEventRemove",t.FSEventWrite="RunningEnvironment.FSEventWrite",t.CreateDir="RunningEnvironment.CreateDir",t.ListDir="RunningEnvironment.ListDir",t.WriteFile="RunningEnvironment.WriteFile",t.GetFile="RunningEnvironment.GetFile",t.RemoveFile="RunningEnvironment.RemoveFile",t.DirContent="RunningEnvironment.DirContent",t.FileContent="RunningEnvironment.FileContent",t.Stdout="RunningEnvironment.Stdout",t.Stderr="RunningEnvironment.Stderr",t.ExecCmd="RunningEnvironment.ExecCmd",t.KillCmd="RunningEnvironment.KillCmd",t.ListRunningCmds="RunningEnvironment.ListRunningCmds",t.CmdOut="RunningEnvironment.CmdOut",t.CmdExit="RunningEnvironment.CmdExit",t.RunningCmds="RunningEnvironment.RunningCmds",t.TermData="RunningEnvironment.TermData",t.TermResize="RunningEnvironment.TermResize",t.TermStart="RunningEnvironment.TermStart",t.TermStartAck="RunningEnvironment.TermStartAck",t.RunCode="RunningEnvironment.Run"}(c||(c={})),function(t){t.Error="CodeCell.Error"}(d||(d={}));const u={Runner:l,RunningEnvironment:c,CodeCell:d};class m{constructor({domain:t,logging:e}){this.handlers=[],this.url=`wss://${t}`,this.logger=new a("WebSocketConnection",e)}get state(){var t;return null===(t=this.client)||void 0===t?void 0:t.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(t){return this.handlers.push(t),()=>{this.handlers=this.handlers.filter((e=>e!==t))}}connect(t){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(t||this.sessionID?(t?(this.logger.log(`Will try to connect to session "${t}"`),this.sessionID=t):!t&&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.client.onmessage=t=>{this.logger.log("Received (raw)",{msg:t}),this.handleMessage(t)},this.client.onerror=t=>this.handleError(t),this.client.onclose=t=>this.handleClose(t)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(t){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",t),this._send(t)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",t)}close(){var t;this.logger.log("Closing connection"),null===(t=this.client)||void 0===t||t.close(1e3)}handleOpen(){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()))}_send(t){var e,n;null===(e=this.client)||void 0===e||e.send((n=t,JSON.stringify(n,(()=>{const t=new WeakSet;return(e,n)=>{if("object"==typeof n&&null!==n){if(t.has(n))return;t.add(n)}return n}})(),2)))}handleClose(t){return i(this,void 0,void 0,(function*(){this.logger.log("Connection closed",t),this.handlers.forEach((t=>t.onClose())),this.logger.log("Will try to reconnect in 5s"),yield h(5e3),this.connect()}))}handleError(t){var e;this.logger.error("Connection error",t),null===(e=this.client)||void 0===e||e.close()}handleMessage(t){if(!t.data)return void this.logger.error("Message has empty data field",t);const e=JSON.parse(t.data);if(!e.type)return void this.logger.error("Message has no type",e);const n=e;Object.values(u.RunningEnvironment).includes(n.type)||Object.values(u.Runner).includes(n.type)||Object.values(u.CodeCell).includes(n.type)?n.type!==u.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((t=>t.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var g,v;!function(t){t.Ok="Ok",t.Terminated="Terminated"}(g||(g={}));class p{constructor({id:t,url:e}){this.logger=new a("RunnerSession"),this.lastPing=new Date,this.id=t,this.url=e}ping(){return i(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const t=JSON.stringify({sessionID:this.id});try{const e=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:t}),n=yield e.json();if(!e.ok)throw this.logger.error(e.headers,n),new Error("Non-OK response when trying to ping active Runner session");if(n.status===g.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(t){throw this.logger.error(t),new Error("Failed to ping active Runner session")}}))}}!function(t){t.Connected="Connected",t.Connecting="Connecting",t.Disconnected="Disconnected"}(v||(v={}));class f{constructor({conn:t,domain:e}){this.logger=new a("SessionManager"),this.userActivity=Promise.resolve(),this.isGettingSessionActive=!1,this.status=v.Disconnected,this.conn=t,this.url=`https://${e}`,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem(s)}set cachedSessionID(t){null===t?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem(s)):(this.logger.log(`Saved sessionID "${t}" as last sessionID`),sessionStorage.setItem(s,t))}stop(){var t;null===(t=this.resolveActivity)||void 0===t||t.call(this),this.userActivity=new Promise((t=>{this.resolveActivity=t,this.rejectActivity=t}))}start(){var t;null===(t=this.rejectActivity)||void 0===t||t.call(this),this.userActivity=Promise.resolve()}reset(){this.logger.log("Reset"),this.status=v.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var t;return i(this,void 0,void 0,(function*(){if(this.isGettingSessionActive)return;this.isGettingSessionActive=!0;let e=3;for(;e>0;){this.status=v.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 i=yield fetch(n),s=yield i.json();if(!i.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 4s",i.headers,s),e-=1,yield h(o);continue}for(e=3,this.session=new p({id:s.sessionID,url:this.url}),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=v.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.userActivity,yield this.session.ping(),yield h(4e3)}catch(t){this.logger.error(`Failed to ping session "${this.session.id}"`,t);break}this.logger.log(`Stopped pinging session "${null===(t=this.session)||void 0===t?void 0:t.id}"`),this.session=void 0,this.status=v.Disconnected,this.conn.close()}catch(t){this.logger.error("Failed to acquire Runner session. Will try again in 4s",t),e-=1,yield h(o)}}}))}}function D(t,e){for(var n=0,i=t.length-1;i>=0;i--){var s=t[i];"."===s?t.splice(i,1):".."===s?(t.splice(i,1),n++):n&&(t.splice(i,1),n--)}if(e)for(;n--;n)t.unshift("..");return t}var y=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,b=function(t){return y.exec(t).slice(1)};function S(){for(var t="",e=!1,n=arguments.length-1;n>=-1&&!e;n--){var i=n>=0?arguments[n]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(t=i+"/"+t,e="/"===i.charAt(0))}return(e?"/":"")+(t=D(I(t.split("/"),(function(t){return!!t})),!e).join("/"))||"."}function C(t){var e=E(t),n="/"===k(t,-1);return(t=D(I(t.split("/"),(function(t){return!!t})),!e).join("/"))||e||(t="."),t&&n&&(t+="/"),(e?"/":"")+t}function E(t){return"/"===t.charAt(0)}var w={extname:function(t){return b(t)[3]},basename:function(t,e){var n=b(t)[2];return e&&n.substr(-1*e.length)===e&&(n=n.substr(0,n.length-e.length)),n},dirname:function(t){var e=b(t),n=e[0],i=e[1];return n||i?(i&&(i=i.substr(0,i.length-1)),n+i):"."},sep:"/",delimiter:":",relative:function(t,e){function n(t){for(var e=0;e<t.length&&""===t[e];e++);for(var n=t.length-1;n>=0&&""===t[n];n--);return e>n?[]:t.slice(e,n-e+1)}t=S(t).substr(1),e=S(e).substr(1);for(var i=n(t.split("/")),s=n(e.split("/")),o=Math.min(i.length,s.length),r=o,a=0;a<o;a++)if(i[a]!==s[a]){r=a;break}var h=[];for(a=r;a<i.length;a++)h.push("..");return(h=h.concat(s.slice(r))).join("/")},join:function(){var t=Array.prototype.slice.call(arguments,0);return C(I(t,(function(t,e){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t})).join("/"))},isAbsolute:E,normalize:C,resolve:S};function I(t,e){if(t.filter)return t.filter(e);for(var n=[],i=0;i<t.length;i++)e(t[i],i,t)&&n.push(t[i]);return n}var R,k="b"==="ab".substr(-1)?function(t,e,n){return t.substr(e,n)}:function(t,e,n){return e<0&&(e=t.length+e),t.substr(e,n)};class _{get level(){return w.normalize(this.path).split(w.sep).length-1}}function F(t){return t.sort(((t,e)=>"Prompt"===t.type?-2:t.type!==e.type?"Dir"===t.type?-1:"Dir"===e.type?1:0:t.key.localeCompare(e.key))),t}class x extends _{constructor({name:t,parent:e,onAddItem:n}){super(),this.children=[],this.name=t,this.parent=e,this.addItemHandler=n,this.path=w.join(this.parent.path,this.name)}removeChildNode(t){this.children=this.children.filter((e=>e!==t))}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Dir",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:F(i)}}}class P extends _{constructor({name:t,parent:e}){super(),this.name=t,this.parent=e,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"File",key:this.path,title:this.name,isLeaf:!0}}}class O extends _{constructor({parent:t,forNode:e,onConfirm:n,onBlur:i}){super(),this.name="name-prompt",this.parent=t,this.forNode=e,this.path=w.join(t.path,this.name),this.onConfirm=n,this.onBlur=i}serialize(t,e,n){return{type:"Prompt",key:this.path,title:e({onConfirm:t=>{var e;return null===(e=this.onConfirm)||void 0===e?void 0:e.call(this,this,t)},onBlur:()=>{var t;return null===(t=this.onBlur)||void 0===t?void 0:t.call(this,this)}}),isLeaf:!0,selectable:!1,icon:n({type:this.forNode})}}}class T extends _{constructor({onAddItem:t}){super(),this.name="/",this.path="/",this.parent=void 0,this.children=[],this.addItemHandler=t}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Root",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:F(i)}}}class A extends _{constructor({parent:t}){super(),this.name="empty-node",this.parent=t,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"Empty",key:this.path,title:"Empty Directory",isLeaf:!0,disabled:!0}}}class N{constructor(){this.logger=new a("Filesystem"),this.root=new T({onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}),this.startingPrintIndent=0,this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}removeAllListeners(){this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}addListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs.push(e);break;case"onDirsChange":this.onDirsChangeLs.push(e);break;case"onFileContent":this.onFileContentLs.push(e);break;case"onShowPrompt":this.onShowPromptLs.push(e);break;default:throw new Error(`Unknown event type: ${t}`)}}removeListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs=this.onNewItemConfirmLs.filter((t=>t!==e));break;case"onDirsChange":this.onDirsChangeLs=this.onDirsChangeLs.filter((t=>t!==e));break;case"onFileContent":this.onFileContentLs=this.onFileContentLs.filter((t=>t!==e));break;case"onShowPrompt":this.onShowPromptLs=this.onShowPromptLs.filter((t=>t!==e));break;default:throw new Error(`Unknown event type: ${t}`)}}addNodeToDir(t,e){this.logger.log("Adding node to dir",{dirPath:t,node:e});let n=this.find(t);if(n||(this.logger.log("Dir not found, will make all",{dirPath:t}),n=this.mkAll(t)),!n)throw new Error("dirNode is undefined");if(!(n instanceof T||n instanceof x))throw new Error(`Node at '${t}' is not a directory or a root node`);if(n.children.some((t=>t.name===e.name)))return;let i;i="Dir"===e.type?new x({name:e.name,parent:n,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new P({name:e.name,parent:n}),1===n.children.length&&n.children[0]instanceof A?n.children=[i]:n.children.push(i),this.notifyOnDirsChangeLs({dirPaths:[t]})}removeNodeFromDir(t,e){this.logger.log("Remove node from dir",{dirPath:t,nodeName:e.name});const n=this.find(t);if(!n)throw new Error(`No node with path '${t}'`);if(!(n instanceof T||n instanceof x))throw new Error(`Node at '${t}' is not a directory or root node`);const i=n.children.length;n.children=n.children.filter((t=>t.name!==e.name));i!==n.children.length?(0===n.children.length&&(n.children=[new A({parent:n})]),this.notifyOnDirsChangeLs({dirPaths:[t]})):this.logger.warn("Node not found in dir",{dirPath:t,nodeName:e.name})}setFileContent(t){this.logger.log("Update file content",t),this.onFileContentLs.forEach((e=>e(t)))}setDirsContent(t){this.logger.log("Set dirs content",t);for(const e of t){let t=this.find(e.dirPath);if(t||(this.logger.log("Dir not found, will make all",{dirPath:e.dirPath}),t=this.mkAll(e.dirPath)),!t)throw new Error("dirNode is undefined");if(!(t instanceof T||t instanceof x))throw new Error(`Node at '${e.dirPath}' is not a directory or a root node`);const n=t.children.find((t=>t instanceof O));if(e.content.length){const n=[];for(const i of e.content){let e;e="Dir"===i.type?new x({name:i.name,parent:t,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new P({name:i.name,parent:t}),n.push(e)}t.children=n}else t.children=[new A({parent:t})];n&&t.children.push(n)}this.notifyOnDirsChangeLs({dirPaths:t.map((t=>t.dirPath))})}showPrompt(t){const{event:e,parent:n,type:i}=t;if(this.logger.log("Show prompt",{parent:n,type:i}),n.children.some((t=>t instanceof O)))return;e.preventDefault();const s=new O({parent:n,forNode:i,onConfirm:((t,e)=>{if(this.logger.log("Prompt confirm",{prompt:t,name:e}),!e)return;const n=t.parent.path,s=w.join(t.parent.path,e);this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[n]}),this.notifyOnNewItemConfirmLs({fullPath:s,name:e,type:i})}).bind(this),onBlur:(t=>{this.logger.log("Prompt blur",{prompt:t});const e=t.parent.path;this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[e]})}).bind(this)});n.children.push(s),this.notifyOnDirsChangeLs({dirPaths:[s.parent.path]}),this.notifyOnShowPromptLs({dirPath:s.parent.path})}serialize(t,e,n){return this.logger.log("Serialize"),[this.root.serialize(t,e,n)]}print(t=this.root,e=this.startingPrintIndent){const n=" ".repeat(e);console.log(n+t.path,`(${t.name})`);for(const i of t.children)i instanceof x?this.print(i,e+1):console.log(n+" "+i.path,`(${i.name})`)}find(t,e=this.root){if(this.logger.log("Find (recursion)",{currentPath:e.path,targetPath:t}),t=w.normalize(t),e.path===t)return e;const n=t.split(w.sep).length-1;if(!(e.level>n)&&(e instanceof T||e instanceof x))for(const n of e.children){const e=this.find(t,n);if(e)return e}}mkDir(t){this.logger.log("Make dir",t);const e=this.find(t.parentPath);if(!e)throw new Error(`Parent dir '${t.parentPath}' not found`);if(!(e instanceof T||e instanceof x))throw new Error("Parent is not a dir or root");const n=new x({parent:e,name:t.name,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})});return e.children.push(n),n}mkAll(t){this.logger.log("Make all",t);const e=t.split(w.sep);let n,i="/";for(const t of e)if(i=w.join(i,t),!this.find(i)){const e=w.dirname(i);n=this.mkDir({parentPath:e,name:t})}return n}removeFromParent(t){const{parent:e}=t;(e instanceof T||e instanceof x)&&(e.children=e.children.filter((e=>e!==t))),t.parent=void 0}notifyOnShowPromptLs(t){this.logger.log("Notify on show prompt",t),this.onShowPromptLs.forEach((e=>e(t)))}notifyOnNewItemConfirmLs(t){this.logger.log("Notify on new item confirm",t),this.onNewItemConfirmLs.forEach((e=>e(t)))}notifyOnDirsChangeLs(t){this.logger.log("Notify on dirs change",t),this.onDirsChangeLs.forEach((e=>e(t)))}}function L(t){return function(t){const e=Array.from(t).reduce(((t,e)=>0|31*t+e.charCodeAt(0)),0);return("0000000"+(e>>>0).toString(16)).slice(-8)}(t)}!function(t){t[t.Stdout=0]="Stdout",t[t.Stderr=1]="Stderr"}(R||(R={}));class ${constructor(t,e){this.contextID=t,this.templateID=e,this.output=[],this.filesystem=new N,this.isReady=!1,this.id=`${t}_${L(e)}`}restart(){this.output=[],this.isReady=!1,this.filesystem.setDirsContent([{dirPath:"/",content:[]}])}logOutput(t,e){this.output=[...this.output,{message:t,source:e}]}}function M(t,{environmentID:e,templateID:n}){const i={type:u.RunningEnvironment.Start,payload:{environmentID:e,templateID:n}};t.send(i)}const j=r(6);class W{constructor(t){var e,n;this.opts=t,this.cmdExitSubscribers=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.termDataSubscribers=[],this.termStartAckSubscribers=[],this.logger=new a(`EvaluationContext [${t.templateID}]`,t.debug),this.env=new $(this.contextID,t.templateID);this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:()=>this.handleConnectionOpen(),onMessage:t=>this.handleConnectionMessage(t),onClose:()=>this.handleConnectionClose()}),this.opts.conn.isOpen&&this.handleConnectionOpen(),this.opts.conn.isClosed&&this.handleConnectionClose(),M(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID}),null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env)}get contextID(){return this.opts.contextID}restart(){this.logger.log("Restart",this.opts.conn.sessionID),this.env.restart(),M(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID})}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({path:t}){return i(this,void 0,void 0,(function*(){let e;this.logger.log("Get file",{filepath:t});const n=new Promise(((t,n)=>{e=t,setTimeout((()=>{n("Timeout")}),1e4)})),i=n=>{n.path.endsWith(t)&&e(n.content)};this.subscribeFileContent(i),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.GetFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t});try{return yield n}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(i)}}))}deleteFile({path:t}){this.logger.log("Delete file",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.RemoveFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}updateFile({path:t,content:e}){return i(this,void 0,void 0,(function*(){let n;this.logger.log("Update file",{filepath:t});const i=new Promise(((t,e)=>{n=t,setTimeout((()=>{e("Timeout")}),1e4)})),s=e=>{e.path.endsWith(t)&&n()};this.subscribeFSWrite(s),function(t,{environmentID:e,path:n,content:i}){const s={type:u.RunningEnvironment.WriteFile,payload:{environmentID:e,path:n,content:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,path:t,content:e});try{yield i}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(s)}}))}createDir({path:t}){this.logger.log("Create dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.CreateDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}listDir({path:t}){this.logger.log("List dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.ListDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}executeCommand({executionID:t,command:e}){return i(this,void 0,void 0,(function*(){let n;this.logger.log("Execute shell command",{executionID:t,command:e});let i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=e=>{e.executionID===t&&(n(t),i=!0)};this.subscribeCmdExit(o),function(t,{environmentID:e,executionID:n,command:i}){const s={type:u.RunningEnvironment.ExecCmd,payload:{environmentID:e,executionID:n,command:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,executionID:t,command:e});try{yield s}catch(t){throw new Error(`Can't exit cmd: ${t}`)}finally{this.unsubscribeCmdExit(o)}}))}startTerminal({terminalID:t}){return i(this,void 0,void 0,(function*(){this.logger.log("Start terminal",{terminalID:t});const e=j();let n,i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=t=>{t.messageID===e&&(n(t.terminalID),i=!0)};this.subscribeTermStartAck(o),function(t,{environmentID:e,terminalID:n,messageID:i}){const s={type:u.RunningEnvironment.TermStart,payload:{environmentID:e,terminalID:n,messageID:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,messageID:e});try{return yield s}catch(t){throw new Error(`Can't start terminal session: ${t}`)}finally{this.unsubscribeTermStartAck(o)}}))}resizeTerminal({terminalID:t,cols:e,rows:n}){this.logger.log("Resize terminal",{terminalID:t,cols:e,rows:n}),function(t,{environmentID:e,terminalID:n,cols:i,rows:s}){const o={type:u.RunningEnvironment.TermResize,payload:{environmentID:e,terminalID:n,cols:i,rows:s}};t.send(o)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,cols:e,rows:n})}sendTerminalData({terminalID:t,data:e}){this.logger.log("Send terminal data",{terminalID:t}),function(t,{environmentID:e,terminalID:n,data:i}){const s={type:u.RunningEnvironment.TermData,payload:{environmentID:e,terminalID:n,data:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,data:e})}onTerminalData({terminalID:t,onData:e}){const n=n=>{n.terminalID===t&&e(n.data)};return this.subscribeTermData(n),()=>this.unsubscribeTermData(n)}subscribeCmdExit(t){this.cmdExitSubscribers.push(t)}unsubscribeCmdExit(t){const e=this.cmdExitSubscribers.indexOf(t);e>-1&&this.cmdExitSubscribers.splice(e,1)}subscribeTermStartAck(t){this.termStartAckSubscribers.push(t)}unsubscribeTermStartAck(t){const e=this.termStartAckSubscribers.indexOf(t);e>-1&&this.termStartAckSubscribers.splice(e,1)}subscribeTermData(t){this.termDataSubscribers.push(t)}unsubscribeTermData(t){const e=this.termDataSubscribers.indexOf(t);e>-1&&this.termDataSubscribers.splice(e,1)}subscribeFileContent(t){this.fileContentSubscribers.push(t)}unsubscribeFileContent(t){const e=this.fileContentSubscribers.indexOf(t);e>-1&&this.fileContentSubscribers.splice(e,1)}subscribeFSWrite(t){this.fsWriteSubscribers.push(t)}unsubscribeFSWrite(t){const e=this.fsWriteSubscribers.indexOf(t);e>-1&&this.fsWriteSubscribers.splice(e,1)}handleConnectionOpen(){var t,e;this.restart(),null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:v.Connecting})}handleConnectionClose(){var t,e;null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:v.Connecting})}handleConnectionMessage(t){switch(this.logger.log("Handling message from remote Runner",{message:t}),t.type){case u.RunningEnvironment.StartAck:{const e=t;this.vmenv_handleStartAck(e.payload);break}case u.RunningEnvironment.CmdOut:{const e=t;this.vmenv_handleCmdOut(e.payload);break}case u.RunningEnvironment.CmdExit:{const e=t;this.vmenv_handleCmdExit(e.payload);break}case u.RunningEnvironment.FSEventWrite:{const e=t;this.vmenv_handleFSEventWrite(e.payload);break}case u.RunningEnvironment.FileContent:{const e=t;this.vmenv_handleFileContent(e.payload);break}case u.RunningEnvironment.FSEventCreate:{const e=t;this.vmenv_handleFSEventCreate(e.payload);break}case u.RunningEnvironment.FSEventRemove:{const e=t;this.vmenv_handleFSEventRemove(e.payload);break}case u.RunningEnvironment.DirContent:{const e=t;this.vmenv_handleDirContent(e.payload);break}case u.RunningEnvironment.Stderr:{const e=t;this.vmenv_handleStderr(e.payload);break}case u.RunningEnvironment.Stdout:{const e=t;this.vmenv_handleStdout(e.payload);break}case u.RunningEnvironment.TermData:{const e=t;this.vmenv_handleTermData(e.payload);break}case u.RunningEnvironment.TermStartAck:{const e=t;this.vmenv_handleTermStartAck(e.payload);break}default:this.logger.warn("Unknown message type",{message:t})}}vmenv_handleTermStartAck(t){this.logger.log('[vmenv] Handling "TermStartAck"',t),this.termStartAckSubscribers.forEach((e=>e(t)))}vmenv_handleTermData(t){this.logger.log('[vmenv] Handling "TermData"',t),this.termDataSubscribers.forEach((e=>e(t)))}vmenv_handleFSEventCreate(t){this.logger.log('[vmenv] Handling "FSEventCreate"',t);const e=w.basename(t.path),n=w.dirname(t.path),i="Directory"===t.type?"Dir":"File";this.env.filesystem.addNodeToDir(n,{name:e,type:i})}vmenv_handleFSEventRemove(t){this.logger.log('[vmenv] Handling "FSEventRemove"',{payload:t});const e=w.basename(t.path),n=w.dirname(t.path);this.env.filesystem.removeNodeFromDir(n,{name:e})}vmenv_handleDirContent(t){this.logger.log('[vmenv] Handling "DirContent"',t);const e=[];for(const n of t.content){const t=w.basename(n.path),i="Directory"===n.type?"Dir":"File";e.push({name:t,type:i})}this.env.filesystem.setDirsContent([{dirPath:t.dirPath,content:e}])}vmenv_handleCmdExit(t){var e,n;this.logger.log('[vmenv] Handling "CmdExit"',t),this.cmdExitSubscribers.forEach((e=>e(t))),void 0!==t.error&&(null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,{environmentID:t.environmentID,executionID:t.executionID,stderr:t.error}))}vmenv_handleFSEventWrite(t){this.logger.log('[vmenv] Handling "FSEventWrite"',t),this.fsWriteSubscribers.forEach((e=>e(t)))}vmenv_handleFileContent(t){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:t.environmentID,path:t.path}),this.fileContentSubscribers.forEach((e=>e(t)))}vmenv_handleStartAck(t){var e,n,i,s;this.logger.log('[vmenv] Handling "StartAck"',{payload:t}),this.env.isReady=!0,null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env),null===(s=(i=this.opts).onSessionChange)||void 0===s||s.call(i,{status:v.Connected})}vmenv_handleCmdOut(t){var e,n;this.logger.log('[vmenv] Handling "CmdOut"',t),null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,t)}vmenv_handleStderr(t){this.logger.log('[vmenv] Handling "Stderr"',t),this.env.logOutput(t.message,R.Stderr)}vmenv_handleStdout(t){this.logger.log('[vmenv] Handling "Stdout"',t),this.env.logOutput(t.message,R.Stdout)}}class z{constructor(){this.logger=new a("Runner"),this.conn=new m({domain:z.config.domain,logging:z.config.logging}),this.sessManager=new f({conn:this.conn,domain:z.config.domain})}static get obj(){if(!z.config)throw new Error("Config not set");return z._obj||(z._obj=new z)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(t){return new W(Object.assign(Object.assign({},t),{conn:this.conn}))}__internal__start(){this.sessManager.start()}__internal__stop(){this.sessManager.stop()}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const H=r(6);var G;t.DevbookStatus=void 0,(G=t.DevbookStatus||(t.DevbookStatus={})).Disconnected="Disconnected",G.Connecting="Connecting",G.Connected="Connected";class q{constructor(e){if(this.opts=e,this.contextID="default",this.executionID=H(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=v.Disconnected,this._status=t.DevbookStatus.Disconnected,!e.config)throw new Error("Missing Devbook config");const n=()=>this.opts.env,s=()=>this.executionID,o=t=>this.isEnvReady=t,r=t=>this.sessionStatus=t;z.config=Object.assign(Object.assign({},this.opts.config),{logging:e.debug}),this.context=z.obj.createContext({debug:e.debug,templateID:e.env,contextID:this.contextID,onEnvChange(t){t.templateID===n()&&o(t.isReady)},onSessionChange({status:t}){r(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.fs={delete:this.deleteFile.bind(this),listDir:this.listDir.bind(this),createDir:this.createDir.bind(this),get:this.getFile.bind(this),write:this.writeFile.bind(this),addListener:this.env.filesystem.addListener.bind(this.env.filesystem),removeListener:this.env.filesystem.removeListener.bind(this.env.filesystem),serialize:this.env.filesystem.serialize.bind(this.env.filesystem)},this.terminal={createSession:(t,e)=>i(this,void 0,void 0,(function*(){const n=yield this.context.startTerminal({terminalID:e}),i=this.context.onTerminalData({onData:t,terminalID:n});return{destroy:()=>{i()},sendData:t=>{this.context.sendTerminalData({terminalID:n,data:t})},resize:({cols:t,rows:e})=>{this.context.resizeTerminal({terminalID:n,cols:t,rows:e})}}}))}}get isDestroyed(){return this._isDestroyed}set isDestroyed(t){this._isDestroyed=t,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(t){this._isEnvReady=t,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(t){this._sessionStatus=t,this.updateStatus()}get status(){return this._status}set status(t){var e,n;this._status=t,null===(n=(e=this.opts).onStatusChange)||void 0===n||n.call(e,t)}get env(){return this.context.env}runCmd(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.executionID=H(),this.context.executeCommand({executionID:this.executionID,command:e})}destroy(){this.context.destroy(),this.isDestroyed=!0}__internal__start(){z.obj.__internal__start()}__internal__stop(){z.obj.__internal__stop()}listDir(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.listDir({path:e})}createDir(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.createDir({path:e})}deleteFile(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.deleteFile({path:e})}getFile(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({path:e})}writeFile(e,n){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({path:e,content:n})}updateStatus(){if(this.isDestroyed)return void(this.status!==t.DevbookStatus.Disconnected&&(this.status=t.DevbookStatus.Disconnected));let e;switch(this.sessionStatus){case v.Disconnected:e=t.DevbookStatus.Disconnected;break;case v.Connecting:e=t.DevbookStatus.Connecting;break;case v.Connected:if(!this.isEnvReady){e=t.DevbookStatus.Connecting;break}e=t.DevbookStatus.Connected}this.status=e}}t.Devbook=q,t.useDevbook=function({env:s,debug:o,config:r,__debug__idleTime:a}){const[h,l]=e.useState(),[c,d]=e.useState(t.DevbookStatus.Disconnected),[u,m]=e.useState([]),[g,v]=e.useState([]),p=e.useCallback((t=>i(this,void 0,void 0,(function*(){h&&(v([]),m([]),h.runCmd(t))}))),[h]);e.useEffect((function(){const t=new q({debug:o,env:s,config:{domain:r.domain},onStatusChange(t){d(t)},onStderr(t){m((e=>[...e,t]))},onStdout(t){v((e=>[...e,t]))}});return v([]),m([]),l(t),()=>{t.destroy()}}),[s,o,r.domain]);const f=n.useIdleTimer({timeout:a||12e4,throttle:500,onIdle(){null==h||h.__internal__stop()},onAction(){null==h||h.__internal__start()},startOnMount:!1,startManually:!0});return e.useEffect((function(){(null==h?void 0:h.status)===t.DevbookStatus.Connected&&f.start()}),[h,f]),{stderr:u,stdout:g,runCmd:p,status:c,fs:null==h?void 0:h.fs,terminal:null==h?void 0:h.terminal}},Object.defineProperty(t,"__esModule",{value:!0})}));
***************************************************************************** */function i(t,e,n,i){return new(n||(n=Promise))((function(s,o){function r(t){try{h(i.next(t))}catch(t){o(t)}}function a(t){try{h(i.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(r,a)}h((i=i.apply(t,e||[])).next())}))}const s="dbk_sdk_session_id",o=4e3;function r(t){return e="1234567890abcdefghijklmnopqrstuvwxyz",n=t,()=>{let t="",i=n;for(;i--;)t+=e[Math.random()*e.length|0];return t};var e,n}class a{constructor(t,e=!1){this.logID=t,this.isEnabled=e}id(){return"function"==typeof this.logID?this.logID():this.logID}log(...t){this.isEnabled&&console.log(`[${this.id()}]`,...t)}warn(...t){this.isEnabled&&console.warn(`[${this.id()}]`,...t)}error(...t){console.error(`[${this.id()} ERROR]`,...t)}}function h(t){return new Promise((e=>setTimeout(e,t)))}var l,c,d;!function(t){t.Error="Runner.Error"}(l||(l={})),function(t){t.Start="RunningEnvironment.Start",t.StartAck="RunningEnvironment.StartAck",t.Eval="RunningEnvironment.Eval",t.FSEventCreate="RunningEnvironment.FSEventCreate",t.FSEventRemove="RunningEnvironment.FSEventRemove",t.FSEventWrite="RunningEnvironment.FSEventWrite",t.CreateDir="RunningEnvironment.CreateDir",t.ListDir="RunningEnvironment.ListDir",t.WriteFile="RunningEnvironment.WriteFile",t.GetFile="RunningEnvironment.GetFile",t.RemoveFile="RunningEnvironment.RemoveFile",t.DirContent="RunningEnvironment.DirContent",t.FileContent="RunningEnvironment.FileContent",t.Stdout="RunningEnvironment.Stdout",t.Stderr="RunningEnvironment.Stderr",t.ExecCmd="RunningEnvironment.ExecCmd",t.KillCmd="RunningEnvironment.KillCmd",t.ListRunningCmds="RunningEnvironment.ListRunningCmds",t.CmdOut="RunningEnvironment.CmdOut",t.CmdExit="RunningEnvironment.CmdExit",t.RunningCmds="RunningEnvironment.RunningCmds",t.TermData="RunningEnvironment.TermData",t.TermResize="RunningEnvironment.TermResize",t.TermStart="RunningEnvironment.TermStart",t.TermStartAck="RunningEnvironment.TermStartAck",t.RunCode="RunningEnvironment.Run"}(c||(c={})),function(t){t.Error="CodeCell.Error"}(d||(d={}));const u={Runner:l,RunningEnvironment:c,CodeCell:d};class m{constructor({domain:t,logging:e}){this.handlers=[],this.url=`wss://${t}`,this.logger=new a("WebSocketConnection",e)}get state(){var t;return null===(t=this.client)||void 0===t?void 0:t.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(t){return this.handlers.push(t),()=>{this.handlers=this.handlers.filter((e=>e!==t))}}connect(t){(!this.client||this.client.readyState!==this.client.CONNECTING&&this.client.readyState!==this.client.OPEN)&&(t||this.sessionID?(t?(this.logger.log(`Will try to connect to session "${t}"`),this.sessionID=t):!t&&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.client.onmessage=t=>{this.logger.log("Received (raw)",{msg:t}),this.handleMessage(t)},this.client.onerror=t=>this.handleError(t),this.client.onclose=t=>this.handleClose(t)):this.logger.error("Cannot connect, no session ID passed to the function and no session ID saved from the previous session"))}send(t){this.client&&this.client.readyState===this.client.OPEN?(this.logger.log("Send",t),this._send(t)):this.logger.warn("Trying to send a message while not being in the `OPEN` state or without established connection, message will be discarded",t)}close(){var t;this.logger.log("Closing connection"),null===(t=this.client)||void 0===t||t.close(1e3)}handleOpen(){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()))}_send(t){var e,n;null===(e=this.client)||void 0===e||e.send((n=t,JSON.stringify(n,(()=>{const t=new WeakSet;return(e,n)=>{if("object"==typeof n&&null!==n){if(t.has(n))return;t.add(n)}return n}})(),2)))}handleClose(t){return i(this,void 0,void 0,(function*(){this.logger.log("Connection closed",t),this.handlers.forEach((t=>t.onClose())),this.logger.log("Will try to reconnect in 5s"),yield h(5e3),this.connect()}))}handleError(t){var e;this.logger.error("Connection error",t),null===(e=this.client)||void 0===e||e.close()}handleMessage(t){if(!t.data)return void this.logger.error("Message has empty data field",t);const e=JSON.parse(t.data);if(!e.type)return void this.logger.error("Message has no type",e);const n=e;Object.values(u.RunningEnvironment).includes(n.type)||Object.values(u.Runner).includes(n.type)||Object.values(u.CodeCell).includes(n.type)?n.type!==u.Runner.Error?(this.logger.log("Received (parsed)",n),this.handlers.forEach((t=>t.onMessage(n)))):this.logger.error("Runner error",n):this.logger.error('Message "type" field has unexpected value',n)}}var g,v;!function(t){t.Ok="Ok",t.Terminated="Terminated"}(g||(g={}));class p{constructor({id:t,url:e}){this.logger=new a("RunnerSession"),this.lastPing=new Date,this.id=t,this.url=e}ping(){return i(this,void 0,void 0,(function*(){this.logger.log(`Pinging session "${this.id}"`);const t=JSON.stringify({sessionID:this.id});try{const e=yield fetch(`${this.url}/session/ping`,{method:"POST",headers:{"Content-Type":"application/json"},body:t}),n=yield e.json();if(!e.ok)throw this.logger.error(e.headers,n),new Error("Non-OK response when trying to ping active Runner session");if(n.status===g.Terminated)throw new Error(`[keepAlive]: Session '${this.id}' is terminated`);this.lastPing=new Date}catch(t){throw this.logger.error(t),new Error("Failed to ping active Runner session")}}))}}!function(t){t.Connected="Connected",t.Connecting="Connecting",t.Disconnected="Disconnected"}(v||(v={}));class f{constructor({conn:t,domain:e}){this.logger=new a("SessionManager"),this.userActivity=Promise.resolve(),this.isGettingSessionActive=!1,this.status=v.Disconnected,this.conn=t,this.url=`https://${e}`,this.logger.log("Initialize"),this.getSession()}get cachedSessionID(){return sessionStorage.getItem(s)}set cachedSessionID(t){null===t?(this.logger.log("Cleared last sessionID"),sessionStorage.removeItem(s)):(this.logger.log(`Saved sessionID "${t}" as last sessionID`),sessionStorage.setItem(s,t))}stop(){var t;null===(t=this.resolveActivity)||void 0===t||t.call(this),this.userActivity=new Promise((t=>{this.resolveActivity=t,this.rejectActivity=t}))}start(){var t;null===(t=this.rejectActivity)||void 0===t||t.call(this),this.userActivity=Promise.resolve()}reset(){this.logger.log("Reset"),this.status=v.Disconnected,this.cachedSessionID=null,this.conn.close(),this.session=void 0}getSession(){var t;return i(this,void 0,void 0,(function*(){if(this.isGettingSessionActive)return;this.isGettingSessionActive=!0;let e=1;for(;e>0;){this.status=v.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 i=yield fetch(n),s=yield i.json();if(!i.ok){this.logger.error("Non-OK response when trying to ping active Runner session. Will try again in 4s",i.headers,s),e-=1,yield h(o);continue}for(e=1,this.session=new p({id:s.sessionID,url:this.url}),this.logger.log(`Acquired session "${this.session.id}"`),this.cachedSessionID=this.session.id,this.status=v.Connected,this.conn.connect(this.session.id),this.logger.log(`Started pinging session "${this.session.id}"`);this.session;)try{yield this.userActivity,yield this.session.ping(),yield h(4e3)}catch(t){this.logger.error(`Failed to ping session "${this.session.id}"`,t);break}this.logger.log(`Stopped pinging session "${null===(t=this.session)||void 0===t?void 0:t.id}"`),this.session=void 0,this.status=v.Disconnected,this.conn.close()}catch(t){this.logger.error("Failed to acquire Runner session. Will try again in 4s",t),e-=1,yield h(o)}}}))}}function D(t,e){for(var n=0,i=t.length-1;i>=0;i--){var s=t[i];"."===s?t.splice(i,1):".."===s?(t.splice(i,1),n++):n&&(t.splice(i,1),n--)}if(e)for(;n--;n)t.unshift("..");return t}var y=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,b=function(t){return y.exec(t).slice(1)};function S(){for(var t="",e=!1,n=arguments.length-1;n>=-1&&!e;n--){var i=n>=0?arguments[n]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(t=i+"/"+t,e="/"===i.charAt(0))}return(e?"/":"")+(t=D(I(t.split("/"),(function(t){return!!t})),!e).join("/"))||"."}function C(t){var e=E(t),n="/"===k(t,-1);return(t=D(I(t.split("/"),(function(t){return!!t})),!e).join("/"))||e||(t="."),t&&n&&(t+="/"),(e?"/":"")+t}function E(t){return"/"===t.charAt(0)}var w={extname:function(t){return b(t)[3]},basename:function(t,e){var n=b(t)[2];return e&&n.substr(-1*e.length)===e&&(n=n.substr(0,n.length-e.length)),n},dirname:function(t){var e=b(t),n=e[0],i=e[1];return n||i?(i&&(i=i.substr(0,i.length-1)),n+i):"."},sep:"/",delimiter:":",relative:function(t,e){function n(t){for(var e=0;e<t.length&&""===t[e];e++);for(var n=t.length-1;n>=0&&""===t[n];n--);return e>n?[]:t.slice(e,n-e+1)}t=S(t).substr(1),e=S(e).substr(1);for(var i=n(t.split("/")),s=n(e.split("/")),o=Math.min(i.length,s.length),r=o,a=0;a<o;a++)if(i[a]!==s[a]){r=a;break}var h=[];for(a=r;a<i.length;a++)h.push("..");return(h=h.concat(s.slice(r))).join("/")},join:function(){var t=Array.prototype.slice.call(arguments,0);return C(I(t,(function(t,e){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t})).join("/"))},isAbsolute:E,normalize:C,resolve:S};function I(t,e){if(t.filter)return t.filter(e);for(var n=[],i=0;i<t.length;i++)e(t[i],i,t)&&n.push(t[i]);return n}var R,k="b"==="ab".substr(-1)?function(t,e,n){return t.substr(e,n)}:function(t,e,n){return e<0&&(e=t.length+e),t.substr(e,n)};class _{get level(){return w.normalize(this.path).split(w.sep).length-1}}function F(t){return t.sort(((t,e)=>"Prompt"===t.type?-2:t.type!==e.type?"Dir"===t.type?-1:"Dir"===e.type?1:0:t.key.localeCompare(e.key))),t}class x extends _{constructor({name:t,parent:e,onAddItem:n}){super(),this.children=[],this.name=t,this.parent=e,this.addItemHandler=n,this.path=w.join(this.parent.path,this.name)}removeChildNode(t){this.children=this.children.filter((e=>e!==t))}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Dir",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:F(i)}}}class P extends _{constructor({name:t,parent:e}){super(),this.name=t,this.parent=e,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"File",key:this.path,title:this.name,isLeaf:!0}}}class O extends _{constructor({parent:t,forNode:e,onConfirm:n,onBlur:i}){super(),this.name="name-prompt",this.parent=t,this.forNode=e,this.path=w.join(t.path,this.name),this.onConfirm=n,this.onBlur=i}serialize(t,e,n){return{type:"Prompt",key:this.path,title:e({onConfirm:t=>{var e;return null===(e=this.onConfirm)||void 0===e?void 0:e.call(this,this,t)},onBlur:()=>{var t;return null===(t=this.onBlur)||void 0===t?void 0:t.call(this,this)}}),isLeaf:!0,selectable:!1,icon:n({type:this.forNode})}}}class A extends _{constructor({onAddItem:t}){super(),this.name="/",this.path="/",this.parent=void 0,this.children=[],this.addItemHandler=t}serialize(t,e,n){const i=this.children.map((i=>i.serialize(t,e,n)));return{type:"Root",key:this.path,title:t({name:this.name,onAddFileMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"File"})},onAddDirMouseDown:t=>{this.addItemHandler({event:t,dir:this,type:"Dir"})}}),children:F(i)}}}class T extends _{constructor({parent:t}){super(),this.name="empty-node",this.parent=t,this.path=w.join(this.parent.path,this.name)}serialize(){return{type:"Empty",key:this.path,title:"Empty Directory",isLeaf:!0,disabled:!0}}}class N{constructor(){this.logger=new a("Filesystem"),this.root=new A({onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}),this.startingPrintIndent=0,this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}removeAllListeners(){this.onNewItemConfirmLs=[],this.onDirsChangeLs=[],this.onFileContentLs=[],this.onShowPromptLs=[]}addListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs.push(e);break;case"onDirsChange":this.onDirsChangeLs.push(e);break;case"onFileContent":this.onFileContentLs.push(e);break;case"onShowPrompt":this.onShowPromptLs.push(e);break;default:throw new Error(`Unknown event type: ${t}`)}}removeListener(t,e){switch(t){case"onNewItemConfirm":this.onNewItemConfirmLs=this.onNewItemConfirmLs.filter((t=>t!==e));break;case"onDirsChange":this.onDirsChangeLs=this.onDirsChangeLs.filter((t=>t!==e));break;case"onFileContent":this.onFileContentLs=this.onFileContentLs.filter((t=>t!==e));break;case"onShowPrompt":this.onShowPromptLs=this.onShowPromptLs.filter((t=>t!==e));break;default:throw new Error(`Unknown event type: ${t}`)}}addNodeToDir(t,e){this.logger.log("Adding node to dir",{dirPath:t,node:e});let n=this.find(t);if(n||(this.logger.log("Dir not found, will make all",{dirPath:t}),n=this.mkAll(t)),!n)throw new Error("dirNode is undefined");if(!(n instanceof A||n instanceof x))throw new Error(`Node at '${t}' is not a directory or a root node`);if(n.children.some((t=>t.name===e.name)))return;let i;i="Dir"===e.type?new x({name:e.name,parent:n,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new P({name:e.name,parent:n}),1===n.children.length&&n.children[0]instanceof T?n.children=[i]:n.children.push(i),this.notifyOnDirsChangeLs({dirPaths:[t]})}removeNodeFromDir(t,e){this.logger.log("Remove node from dir",{dirPath:t,nodeName:e.name});const n=this.find(t);if(!n)throw new Error(`No node with path '${t}'`);if(!(n instanceof A||n instanceof x))throw new Error(`Node at '${t}' is not a directory or root node`);const i=n.children.length;n.children=n.children.filter((t=>t.name!==e.name));i!==n.children.length?(0===n.children.length&&(n.children=[new T({parent:n})]),this.notifyOnDirsChangeLs({dirPaths:[t]})):this.logger.warn("Node not found in dir",{dirPath:t,nodeName:e.name})}setFileContent(t){this.logger.log("Update file content",t),this.onFileContentLs.forEach((e=>e(t)))}setDirsContent(t){this.logger.log("Set dirs content",t);for(const e of t){let t=this.find(e.dirPath);if(t||(this.logger.log("Dir not found, will make all",{dirPath:e.dirPath}),t=this.mkAll(e.dirPath)),!t)throw new Error("dirNode is undefined");if(!(t instanceof A||t instanceof x))throw new Error(`Node at '${e.dirPath}' is not a directory or a root node`);const n=t.children.find((t=>t instanceof O));if(e.content.length){const n=[];for(const i of e.content){let e;e="Dir"===i.type?new x({name:i.name,parent:t,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})}):new P({name:i.name,parent:t}),n.push(e)}t.children=n}else t.children=[new T({parent:t})];n&&t.children.push(n)}this.notifyOnDirsChangeLs({dirPaths:t.map((t=>t.dirPath))})}showPrompt(t){const{event:e,parent:n,type:i}=t;if(this.logger.log("Show prompt",{parent:n,type:i}),n.children.some((t=>t instanceof O)))return;e.preventDefault();const s=new O({parent:n,forNode:i,onConfirm:((t,e)=>{if(this.logger.log("Prompt confirm",{prompt:t,name:e}),!e)return;const n=t.parent.path,s=w.join(t.parent.path,e);this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[n]}),this.notifyOnNewItemConfirmLs({fullPath:s,name:e,type:i})}).bind(this),onBlur:(t=>{this.logger.log("Prompt blur",{prompt:t});const e=t.parent.path;this.removeFromParent(t),this.notifyOnDirsChangeLs({dirPaths:[e]})}).bind(this)});n.children.push(s),this.notifyOnDirsChangeLs({dirPaths:[s.parent.path]}),this.notifyOnShowPromptLs({dirPath:s.parent.path})}serialize(t,e,n){return this.logger.log("Serialize"),[this.root.serialize(t,e,n)]}print(t=this.root,e=this.startingPrintIndent){const n=" ".repeat(e);console.log(n+t.path,`(${t.name})`);for(const i of t.children)i instanceof x?this.print(i,e+1):console.log(n+" "+i.path,`(${i.name})`)}find(t,e=this.root){if(this.logger.log("Find (recursion)",{currentPath:e.path,targetPath:t}),t=w.normalize(t),e.path===t)return e;const n=t.split(w.sep).length-1;if(!(e.level>n)&&(e instanceof A||e instanceof x))for(const n of e.children){const e=this.find(t,n);if(e)return e}}mkDir(t){this.logger.log("Make dir",t);const e=this.find(t.parentPath);if(!e)throw new Error(`Parent dir '${t.parentPath}' not found`);if(!(e instanceof A||e instanceof x))throw new Error("Parent is not a dir or root");const n=new x({parent:e,name:t.name,onAddItem:({event:t,dir:e,type:n})=>this.showPrompt({event:t,parent:e,type:n})});return e.children.push(n),n}mkAll(t){this.logger.log("Make all",t);const e=t.split(w.sep);let n,i="/";for(const t of e)if(i=w.join(i,t),!this.find(i)){const e=w.dirname(i);n=this.mkDir({parentPath:e,name:t})}return n}removeFromParent(t){const{parent:e}=t;(e instanceof A||e instanceof x)&&(e.children=e.children.filter((e=>e!==t))),t.parent=void 0}notifyOnShowPromptLs(t){this.logger.log("Notify on show prompt",t),this.onShowPromptLs.forEach((e=>e(t)))}notifyOnNewItemConfirmLs(t){this.logger.log("Notify on new item confirm",t),this.onNewItemConfirmLs.forEach((e=>e(t)))}notifyOnDirsChangeLs(t){this.logger.log("Notify on dirs change",t),this.onDirsChangeLs.forEach((e=>e(t)))}}function L(t){return function(t){const e=Array.from(t).reduce(((t,e)=>0|31*t+e.charCodeAt(0)),0);return("0000000"+(e>>>0).toString(16)).slice(-8)}(t)}!function(t){t[t.Stdout=0]="Stdout",t[t.Stderr=1]="Stderr"}(R||(R={}));class ${constructor(t,e){this.contextID=t,this.templateID=e,this.output=[],this.filesystem=new N,this.isReady=!1,this.id=`${t}_${L(e)}`}restart(){this.output=[],this.isReady=!1,this.filesystem.setDirsContent([{dirPath:"/",content:[]}])}logOutput(t,e){this.output=[...this.output,{message:t,source:e}]}}function M(t,{environmentID:e,templateID:n}){const i={type:u.RunningEnvironment.Start,payload:{environmentID:e,templateID:n}};t.send(i)}const j=r(6);class W{constructor(t){var e,n;this.opts=t,this.cmdExitSubscribers=[],this.fsWriteSubscribers=[],this.fileContentSubscribers=[],this.termDataSubscribers=[],this.termStartAckSubscribers=[],this.logger=new a(`EvaluationContext [${t.templateID}]`,t.debug),this.env=new $(this.contextID,t.templateID);this.unsubscribeConnHandler=this.opts.conn.subscribeHandler({onOpen:()=>this.handleConnectionOpen(),onMessage:t=>this.handleConnectionMessage(t),onClose:()=>this.handleConnectionClose()}),this.opts.conn.isOpen&&this.handleConnectionOpen(),this.opts.conn.isClosed&&this.handleConnectionClose(),M(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID}),null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env)}get contextID(){return this.opts.contextID}restart(){this.logger.log("Restart",this.opts.conn.sessionID),this.env.restart(),M(this.opts.conn,{environmentID:this.env.id,templateID:this.env.templateID})}destroy(){this.logger.log("Destroy"),this.unsubscribeConnHandler(),this.fsWriteSubscribers=[],this.fileContentSubscribers=[]}getFile({path:t}){return i(this,void 0,void 0,(function*(){let e;this.logger.log("Get file",{filepath:t});const n=new Promise(((t,n)=>{e=t,setTimeout((()=>{n("Timeout")}),1e4)})),i=n=>{n.path.endsWith(t)&&e(n.content)};this.subscribeFileContent(i),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.GetFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t});try{return yield n}catch(e){throw new Error(`Error retrieving file ${t}: ${e}`)}finally{this.unsubscribeFileContent(i)}}))}deleteFile({path:t}){this.logger.log("Delete file",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.RemoveFile,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}updateFile({path:t,content:e}){return i(this,void 0,void 0,(function*(){let n;this.logger.log("Update file",{filepath:t});const i=new Promise(((t,e)=>{n=t,setTimeout((()=>{e("Timeout")}),1e4)})),s=e=>{e.path.endsWith(t)&&n()};this.subscribeFSWrite(s),function(t,{environmentID:e,path:n,content:i}){const s={type:u.RunningEnvironment.WriteFile,payload:{environmentID:e,path:n,content:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,path:t,content:e});try{yield i}catch(e){throw new Error(`File ${t} not written to VM: ${e}`)}finally{this.unsubscribeFSWrite(s)}}))}createDir({path:t}){this.logger.log("Create dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.CreateDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}listDir({path:t}){this.logger.log("List dir",{filepath:t}),function(t,{environmentID:e,path:n}){const i={type:u.RunningEnvironment.ListDir,payload:{environmentID:e,path:n}};t.send(i)}(this.opts.conn,{environmentID:this.env.id,path:t})}executeCommand({executionID:t,command:e}){return i(this,void 0,void 0,(function*(){let n;this.logger.log("Execute shell command",{executionID:t,command:e});const i=new Promise((t=>{n=t})),s=e=>{e.executionID===t&&n(t)};this.subscribeCmdExit(s),function(t,{environmentID:e,executionID:n,command:i}){const s={type:u.RunningEnvironment.ExecCmd,payload:{environmentID:e,executionID:n,command:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,executionID:t,command:e});try{yield i}catch(t){throw new Error(`Can't exit cmd: ${t}`)}finally{this.unsubscribeCmdExit(s)}}))}startTerminal({terminalID:t}){return i(this,void 0,void 0,(function*(){this.logger.log("Start terminal",{terminalID:t});const e=j();let n,i=!1;const s=new Promise(((t,e)=>{n=t,setTimeout((()=>{i||e("Timeout")}),1e4)})),o=t=>{t.messageID===e&&(n(t.terminalID),i=!0)};this.subscribeTermStartAck(o),function(t,{environmentID:e,terminalID:n,messageID:i}){const s={type:u.RunningEnvironment.TermStart,payload:{environmentID:e,terminalID:n,messageID:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,messageID:e});try{return yield s}catch(t){throw new Error(`Can't start terminal session: ${t}`)}finally{this.unsubscribeTermStartAck(o)}}))}resizeTerminal({terminalID:t,cols:e,rows:n}){this.logger.log("Resize terminal",{terminalID:t,cols:e,rows:n}),function(t,{environmentID:e,terminalID:n,cols:i,rows:s}){const o={type:u.RunningEnvironment.TermResize,payload:{environmentID:e,terminalID:n,cols:i,rows:s}};t.send(o)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,cols:e,rows:n})}sendTerminalData({terminalID:t,data:e}){this.logger.log("Send terminal data",{terminalID:t}),function(t,{environmentID:e,terminalID:n,data:i}){const s={type:u.RunningEnvironment.TermData,payload:{environmentID:e,terminalID:n,data:i}};t.send(s)}(this.opts.conn,{environmentID:this.env.id,terminalID:t,data:e})}onTerminalData({terminalID:t,onData:e}){const n=n=>{n.terminalID===t&&e(n.data)};return this.subscribeTermData(n),()=>this.unsubscribeTermData(n)}subscribeCmdExit(t){this.cmdExitSubscribers.push(t)}unsubscribeCmdExit(t){const e=this.cmdExitSubscribers.indexOf(t);e>-1&&this.cmdExitSubscribers.splice(e,1)}subscribeTermStartAck(t){this.termStartAckSubscribers.push(t)}unsubscribeTermStartAck(t){const e=this.termStartAckSubscribers.indexOf(t);e>-1&&this.termStartAckSubscribers.splice(e,1)}subscribeTermData(t){this.termDataSubscribers.push(t)}unsubscribeTermData(t){const e=this.termDataSubscribers.indexOf(t);e>-1&&this.termDataSubscribers.splice(e,1)}subscribeFileContent(t){this.fileContentSubscribers.push(t)}unsubscribeFileContent(t){const e=this.fileContentSubscribers.indexOf(t);e>-1&&this.fileContentSubscribers.splice(e,1)}subscribeFSWrite(t){this.fsWriteSubscribers.push(t)}unsubscribeFSWrite(t){const e=this.fsWriteSubscribers.indexOf(t);e>-1&&this.fsWriteSubscribers.splice(e,1)}handleConnectionOpen(){var t,e;this.restart(),null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:v.Connecting})}handleConnectionClose(){var t,e;null===(e=(t=this.opts).onSessionChange)||void 0===e||e.call(t,{status:v.Connecting})}handleConnectionMessage(t){switch(this.logger.log("Handling message from remote Runner",{message:t}),t.type){case u.RunningEnvironment.StartAck:{const e=t;this.vmenv_handleStartAck(e.payload);break}case u.RunningEnvironment.CmdOut:{const e=t;this.vmenv_handleCmdOut(e.payload);break}case u.RunningEnvironment.CmdExit:{const e=t;this.vmenv_handleCmdExit(e.payload);break}case u.RunningEnvironment.FSEventWrite:{const e=t;this.vmenv_handleFSEventWrite(e.payload);break}case u.RunningEnvironment.FileContent:{const e=t;this.vmenv_handleFileContent(e.payload);break}case u.RunningEnvironment.FSEventCreate:{const e=t;this.vmenv_handleFSEventCreate(e.payload);break}case u.RunningEnvironment.FSEventRemove:{const e=t;this.vmenv_handleFSEventRemove(e.payload);break}case u.RunningEnvironment.DirContent:{const e=t;this.vmenv_handleDirContent(e.payload);break}case u.RunningEnvironment.Stderr:{const e=t;this.vmenv_handleStderr(e.payload);break}case u.RunningEnvironment.Stdout:{const e=t;this.vmenv_handleStdout(e.payload);break}case u.RunningEnvironment.TermData:{const e=t;this.vmenv_handleTermData(e.payload);break}case u.RunningEnvironment.TermStartAck:{const e=t;this.vmenv_handleTermStartAck(e.payload);break}default:this.logger.warn("Unknown message type",{message:t})}}vmenv_handleTermStartAck(t){this.logger.log('[vmenv] Handling "TermStartAck"',t),this.termStartAckSubscribers.forEach((e=>e(t)))}vmenv_handleTermData(t){this.logger.log('[vmenv] Handling "TermData"',t),this.termDataSubscribers.forEach((e=>e(t)))}vmenv_handleFSEventCreate(t){this.logger.log('[vmenv] Handling "FSEventCreate"',t);const e=w.basename(t.path),n=w.dirname(t.path),i="Directory"===t.type?"Dir":"File";this.env.filesystem.addNodeToDir(n,{name:e,type:i})}vmenv_handleFSEventRemove(t){this.logger.log('[vmenv] Handling "FSEventRemove"',{payload:t});const e=w.basename(t.path),n=w.dirname(t.path);this.env.filesystem.removeNodeFromDir(n,{name:e})}vmenv_handleDirContent(t){this.logger.log('[vmenv] Handling "DirContent"',t);const e=[];for(const n of t.content){const t=w.basename(n.path),i="Directory"===n.type?"Dir":"File";e.push({name:t,type:i})}this.env.filesystem.setDirsContent([{dirPath:t.dirPath,content:e}])}vmenv_handleCmdExit(t){var e,n;this.logger.log('[vmenv] Handling "CmdExit"',t),this.cmdExitSubscribers.forEach((e=>e(t))),void 0!==t.error&&(null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,{environmentID:t.environmentID,executionID:t.executionID,stderr:t.error}))}vmenv_handleFSEventWrite(t){this.logger.log('[vmenv] Handling "FSEventWrite"',t),this.fsWriteSubscribers.forEach((e=>e(t)))}vmenv_handleFileContent(t){this.logger.log('[vmenv] Handling "FileContent"',{environmentID:t.environmentID,path:t.path}),this.fileContentSubscribers.forEach((e=>e(t)))}vmenv_handleStartAck(t){var e,n,i,s;this.logger.log('[vmenv] Handling "StartAck"',{payload:t}),this.env.isReady=!0,null===(n=(e=this.opts).onEnvChange)||void 0===n||n.call(e,this.env),null===(s=(i=this.opts).onSessionChange)||void 0===s||s.call(i,{status:v.Connected})}vmenv_handleCmdOut(t){var e,n;this.logger.log('[vmenv] Handling "CmdOut"',t),null===(n=(e=this.opts).onCmdOut)||void 0===n||n.call(e,t)}vmenv_handleStderr(t){this.logger.log('[vmenv] Handling "Stderr"',t),this.env.logOutput(t.message,R.Stderr)}vmenv_handleStdout(t){this.logger.log('[vmenv] Handling "Stdout"',t),this.env.logOutput(t.message,R.Stdout)}}class z{constructor(){this.logger=new a("Runner"),this.conn=new m({domain:z.config.domain,logging:z.config.logging}),this.sessManager=new f({conn:this.conn,domain:z.config.domain})}static get obj(){if(!z.config)throw new Error("Config not set");return z._obj||(z._obj=new z)}get session(){return this.sessManager.session}get status(){return this.sessManager.status}reset(){this.logger.log("Reset"),this.sessManager.reset()}createContext(t){return new W(Object.assign(Object.assign({},t),{conn:this.conn}))}__internal__start(){this.sessManager.start()}__internal__stop(){this.sessManager.stop()}__debug__loadNewSession(){this.logger.log("__debug__loadNewSession"),this.sessManager.reset()}}const H=r(6);var G;t.DevbookStatus=void 0,(G=t.DevbookStatus||(t.DevbookStatus={})).Disconnected="Disconnected",G.Connecting="Connecting",G.Connected="Connected";class q{constructor(e){if(this.opts=e,this.contextID="default",this.executionID=H(),this._isDestroyed=!1,this._isEnvReady=!1,this._sessionStatus=v.Disconnected,this._status=t.DevbookStatus.Disconnected,!e.config)throw new Error("Missing Devbook config");const n=()=>this.opts.env,s=()=>this.executionID,o=t=>this.isEnvReady=t,r=t=>this.sessionStatus=t;z.config=Object.assign(Object.assign({},this.opts.config),{logging:e.debug}),this.context=z.obj.createContext({debug:e.debug,templateID:e.env,contextID:this.contextID,onEnvChange(t){t.templateID===n()&&o(t.isReady)},onSessionChange({status:t}){r(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.fs={delete:this.deleteFile.bind(this),listDir:this.listDir.bind(this),createDir:this.createDir.bind(this),get:this.getFile.bind(this),write:this.writeFile.bind(this),addListener:this.env.filesystem.addListener.bind(this.env.filesystem),removeListener:this.env.filesystem.removeListener.bind(this.env.filesystem),serialize:this.env.filesystem.serialize.bind(this.env.filesystem)},this.terminal={createSession:(t,e)=>i(this,void 0,void 0,(function*(){const n=yield this.context.startTerminal({terminalID:e}),i=this.context.onTerminalData({onData:t,terminalID:n});return{destroy:()=>{i()},sendData:t=>{this.context.sendTerminalData({terminalID:n,data:t})},resize:({cols:t,rows:e})=>{this.context.resizeTerminal({terminalID:n,cols:t,rows:e})}}}))}}get isDestroyed(){return this._isDestroyed}set isDestroyed(t){this._isDestroyed=t,this.updateStatus()}get isEnvReady(){return this._isEnvReady}set isEnvReady(t){this._isEnvReady=t,this.updateStatus()}get sessionStatus(){return this._sessionStatus}set sessionStatus(t){this._sessionStatus=t,this.updateStatus()}get status(){return this._status}set status(t){var e,n;this._status=t,null===(n=(e=this.opts).onStatusChange)||void 0===n||n.call(e,t)}get env(){return this.context.env}runCmd(e){return i(this,void 0,void 0,(function*(){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");this.executionID=H(),yield this.context.executeCommand({executionID:this.executionID,command:e})}))}destroy(){this.context.destroy(),this.isDestroyed=!0}__internal__start(){z.obj.__internal__start()}__internal__stop(){z.obj.__internal__stop()}listDir(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.listDir({path:e})}createDir(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.createDir({path:e})}deleteFile(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.deleteFile({path:e})}getFile(e){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.getFile({path:e})}writeFile(e,n){if(this.status!==t.DevbookStatus.Connected)throw new Error("Not connected to the VM yet.");return this.context.updateFile({path:e,content:n})}updateStatus(){if(this.isDestroyed)return void(this.status!==t.DevbookStatus.Disconnected&&(this.status=t.DevbookStatus.Disconnected));let e;switch(this.sessionStatus){case v.Disconnected:e=t.DevbookStatus.Disconnected;break;case v.Connecting:e=t.DevbookStatus.Connecting;break;case v.Connected:if(!this.isEnvReady){e=t.DevbookStatus.Connecting;break}e=t.DevbookStatus.Connected}this.status=e}}t.Devbook=q,t.useDevbook=function({env:s,debug:o,config:r,__debug__idleTime:a}){const[h,l]=e.useState(),[c,d]=e.useState(t.DevbookStatus.Disconnected),[u,m]=e.useState([]),[g,v]=e.useState([]),p=e.useCallback((t=>i(this,void 0,void 0,(function*(){h&&(v([]),m([]),h.runCmd(t))}))),[h]);e.useEffect((function(){const t=new q({debug:o,env:s,config:{domain:r.domain},onStatusChange(t){d(t)},onStderr(t){m((e=>[...e,t]))},onStdout(t){v((e=>[...e,t]))}});return v([]),m([]),l(t),()=>{t.destroy()}}),[s,o,r.domain]);const f=n.useIdleTimer({timeout:a||6e4,throttle:500,onIdle(){null==h||h.__internal__stop()},onAction(){null==h||h.__internal__start()},startOnMount:!1,startManually:!0});return e.useEffect((function(){(null==h?void 0:h.status)===t.DevbookStatus.Connected&&f.start()}),[h,f]),{stderr:u,stdout:g,runCmd:p,status:c,fs:null==h?void 0:h.fs,terminal:null==h?void 0:h.terminal}},Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=index.js.map
{
"name": "@devbookhq/sdk",
"version": "1.0.16",
"version": "1.0.17",
"description": "Devbook allows visitors of your docs to interact with and execute any code snippet or shell command in a private VM",

@@ -5,0 +5,0 @@ "homepage": "https://usedevbook.com",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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