You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
4.6 KiB

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.webfuse=t():e.webfuse=t()}(window,(function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var s=t[n]={i:n,l:!1,exports:{}};return e[n].call(s.exports,s,s.exports,r),s.l=!0,s.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var s in e)r.d(n,s,function(t){return e[t]}.bind(null,s));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";r.r(t),r.d(t,"BadState",(function(){return n})),r.d(t,"Provider",(function(){return s})),r.d(t,"FileMode",(function(){return i})),r.d(t,"Client",(function(){return o}));class n extends Error{static get BAD(){return 1}static get NOT_IMPLEMENTED(){return 2}static get TIMEOUT(){return 3}static get FORMAT(){return 4}static get NO_ENTRY(){return 101}static get NO_ACCESS(){return 102}constructor(e){super("Bad State"),this.code=e}}class s{async lookup(e,t){throw new n(n.NOT_IMPLEMENTED)}async getattr(e){throw new n(n.NOT_IMPLEMENTED)}async readdir(e){throw new n(n.NOT_IMPLEMENTED)}async open(e,t){throw new n(n.NOT_IMPLEMENTED)}close(e,t,r){}async read(e,t,r,s){throw new n(n.NOT_IMPLEMENTED)}}class i{static get ACCESS_MODE(){return 3}static get READONLY(){return 0}static get WRITEONLY(){return 1}static get READWRITE(){return 2}static get CREATE(){return 64}static get EXCLUSIVE(){return 128}static get TRUNKATE(){return 512}static get APPEND(){return 1024}}class o{static get _PROTOCOL(){return"webfuse-adapter-server"}constructor(e){this._provider={},this._pendingRequests={},this._id=0,this._ws=null,this.onopen=()=>{},this.onclose=()=>{},this.onerror=()=>{}}connectTo(e){this.disconnect(),this._ws=new WebSocket(e,o._PROTOCOL),this._ws.onopen=this.onopen,this._ws.onclose=this.onclose,this._ws.onerror=this.onerror,this._ws.onmessage=e=>{this._onmessage(e)}}_invokeRequest(e,t){this._id+=1;const r=this._id,n={method:e,params:t,id:r};return new Promise((e,t)=>{this._pendingRequests[r]={resolve:e,reject:t},this._ws.send(JSON.stringify(n))})}authenticate(e,t){return this._invokeRequest("authenticate",[e,t])}addProvider(e,t){this._provider[e]=t;const r={method:"add_filesystem",params:[e],id:23};this._ws.send(JSON.stringify(r))}disconnect(){this._ws&&(this._ws.close(),this._ws=null)}isConnected(){return this._ws&&this._ws.readyState===WebSocket.OPEN}_isRequest(e){return"string"==typeof e.method&&"params"in e}_isResponse(e){return"number"==typeof e.id&&("result"in e||"error"in e)}_removePendingRequest(e){let t=null;return e in this._pendingRequests&&(t=this._pendingRequests[e],Reflect.deleteProperty(this._pendingRequests,e)),t}_onmessage(e){try{const t=JSON.parse(e.data);if(this._isRequest(t)){const e=t.method,r=t.id,n=t.params;"number"==typeof r?this._invoke(e,n,r):this._notify(e,n)}else if(this._isResponse(t)){const e=t.id,r=t.result,n=t.error,s=this._removePendingRequest(e);s&&(r?s.resolve(r):s.reject(n))}}catch(e){}}_invoke(e,t,r){this._invokeAsync(e,t).then(e=>{const t={result:e,id:r};this._ws.send(JSON.stringify(t))}).catch(e=>{const t={error:{code:e.code||n.BAD},id:r};this._ws.send(JSON.stringify(t))})}async _invokeAsync(e,t){switch(e){case"lookup":return this._lookup(t);case"getattr":return this._getattr(t);case"readdir":return this._readdir(t);case"open":return this._open(t);case"read":return this._read(t);default:throw new n(n.NOT_IMPLEMENTED)}}_notify(e,t){switch(e){case"close":this._close(t);break;default:throw new Error(`Invalid method: "${e}"`)}}_getProvider(e){if(e in this._provider)return this._provider[e];throw new Error("Unknown provider")}async _lookup([e,t,r]){return this._getProvider(e).lookup(t,r)}async _getattr([e,t]){return this._getProvider(e).getattr(t)}async _readdir([e,t]){return this._getProvider(e).readdir(t)}async _open([e,t,r]){return this._getProvider(e).open(t,r)}_close([e,t,r,n]){this._getProvider(e).close(t,r,n)}async _read([e,t,r,s,i]){const o=this._getProvider(e),u=await o.read(t,r,s,i);if("string"==typeof u)return{data:btoa(u),format:"base64",count:u.length};throw new n(n.BAD)}}}])}));