crosis
Version:
Crosis done just right
3 lines (2 loc) • 5.35 kB
JavaScript
var w=Object.create;var h=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var y=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var U=(i,r)=>{for(var t in r)h(i,t,{get:r[t],enumerable:!0})},p=(i,r,t,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of R(r))!S.call(i,n)&&n!==t&&h(i,n,{get:()=>r[n],enumerable:!(e=v(r,n))||e.enumerable});return i};var x=(i,r,t)=>(t=i!=null?w(y(i)):{},p(r||!i||!i.__esModule?h(t,"default",{value:i,enumerable:!0}):t,i)),b=i=>p(h({},"__esModule",{value:!0}),i);var F={};U(F,{Crosis:()=>g});module.exports=b(F);var f=x(require("@replit/protocol"),1),l=require("ws"),m=require("./channel.cjs"),C=require("events"),u=require("path");const c=f.default.api,O={};class g extends C.EventEmitter{constructor(t){if(!t.url&&!t.adapter)throw new Error("Either url or adapter must be specified");super();t={...O,...t},this.adapter=t.adapter||null,this.url=t.url||void 0,this.ws=null,this.debug=t.debug||!1,this.refHandlers={},this.channels={},this.channelsByName={},this.utilFuncsChannels={},this.otStatuses={},this.bootStatus=null,this.containerState=null,this.execUtilResolve=null,this.execUtilReject=null,this.execUtilOutput=null}setAdapter(t){if(this.wsReadyState!=l.WebSocket.CLOSED)throw new Error("Cannot change adapter while connected");this.adapter=t}setUrl(t){if(this.wsReadyState!=l.WebSocket.CLOSED)throw new Error("Cannot change URL while connected");this.url=t}get wsReadyState(){var t,e;return(e=(t=this.ws)==null?void 0:t.readyState)!=null?e:l.WebSocket.CLOSED}async connect(){const t=this.adapter?await this.adapter():null;t!=null&&t.url&&(this.url=t.url),this.ws=new l.WebSocket(this.url),await new Promise(e=>{this.ws.onopen=e}),this.ws.onmessage=e=>{var o,d;if(typeof e.data=="string")return;let n;if(e.data instanceof Uint8Array)n=e.data;else if(e.data instanceof ArrayBuffer||e.data instanceof Buffer)n=new Uint8Array(e.data);else return;const s=c.Command.decode(n),a=this.channels[s.channel];this.debug&&console.log(s),typeof((o=s.bootStatus)==null?void 0:o.stage)=="number"&&(this.bootStatus=s.bootStatus.stage),typeof((d=s.containerState)==null?void 0:d.state)=="number"&&(this.containerState=s.containerState.state),s.channel==this.utilFuncsChannels.exec&&(s.output?this.execUtilOutput+=s.output:s.ok&&(this.execUtilResolve(this.execUtilOutput),this.execUtilResolve=null,this.execUtilReject=null,this.execUtilOutput=null)),(a==null?void 0:a.service)=="ot"&&s.otstatus&&(this.otStatuses[s.otstatus.linkedFile.path]=s.otstatus),s.ref&&this.refHandlers[s.ref]&&this.refHandlers[s.ref](s),this.emit("message",s),s.toast&&this.emit("toast",s.toast.text)},this.emit("connect")}generateRef(){return Math.random().toString(36).substring(2)}send(t,e=!0,n=!0){return e&&!t.ref&&(t.ref=this.generateRef()),this.ws.send(c.Command.encode(c.Command.create(t)).finish()),this.emit("messageSent",t),new Promise((s,a)=>{this.refHandlers[t.ref]=o=>{n&&o.error&&a(o.error),s(o)}})}async openChannel(t,e,n){const s=await this.send({channel:0,openChan:{service:t,name:e||"",action:n||c.OpenChannel.Action.ATTACH_OR_CREATE}}),a=new m.Channel(this,s.openChanRes,t,e);if(this.channels[s.openChanRes.id]=a,e){if(e in this.channelsByName)throw new Error(`Channel already exists with name "${e}"`);this.channelsByName[e]=s.openChanRes.id}return this.emit("openChannel",a),a}async closeChannel(t,e){const n=await this.send({channel:0,closeChan:{id:t,action:e||c.CloseChannel.Action.TRY_CLOSE}});return delete this.channels[t],this.emit("closeChannel",n.closeChanRes),n.closeChanRes}async disconnect(t=!0){var e;if(this.wsReadyState==l.WebSocket.CLOSED)return!1;if(t)for(const n of Object.values(this.channels))await n.close();return this.ws.close(),(e=this.execUtilReject)==null||e.call(this,"Disconnected"),this.execUtilResolve=null,this.execUtilReject=null,this.execUtilOutput=null,this.emit("disconnect"),!0}getChannelIdByName(t){return this.channelsByName[t]}async startUtil(...t){const e=t[0];if(e in this.utilFuncsChannels)return this.channels[this.utilFuncsChannels[e]];{const n=await this.openChannel(...t);return this.utilFuncsChannels[e]=n.id,n}}async readFile(t){var s;return(s=(await(await this.startUtil("gcsfiles")).send({read:{path:t}})).file)==null?void 0:s.content}async writeFile(t,e){return await(await this.startUtil("gcsfiles")).send({write:{path:t,content:e}})}async statFile(t){return(await(await this.startUtil("gcsfiles")).send({stat:{path:t}})).statRes}async readDir(t){return(await(await this.startUtil("gcsfiles")).send({readdir:{path:t}})).files.files}async createDir(t){return await(await this.startUtil("gcsfiles")).send({mkdir:{path:t}})}async exec(t,e){if(this.execUtilResolve)throw new Error("Cannot execute multiple commands at once");const n=await this.startUtil("exec");return this.execUtilOutput="",(await Promise.all([new Promise((a,o)=>{this.execUtilResolve=a,this.execUtilReject=o}),n.send({exec:{args:t,env:e}})]))[0]}async getFileHistory(t,e=1,n){var o;return t=(0,u.normalize)(t),n=n!=null?n:this.getLatestFileVersion(t),(o=(await(await this.startUtil("ot",`ot:${t}`)).send({otFetchRequest:{versionFrom:e,versionTo:n}})).otFetchResponse)==null?void 0:o.packets}getLatestFileVersion(t){var e,n;return t=(0,u.normalize)(t),(n=(e=this.otStatuses[t])==null?void 0:e.version)!=null?n:null}}
//# sourceMappingURL=crosis.cjs.map