mirror of
https://gitlab.com/jiehong/jq_offline.git
synced 2024-12-14 18:30:48 +01:00
2 lines
7.3 KiB
JavaScript
2 lines
7.3 KiB
JavaScript
|
const e=Symbol("Comlink.proxy"),t=Symbol("Comlink.endpoint"),o=Symbol("Comlink.releaseProxy"),n=Symbol("Comlink.thrown"),r=e=>"object"==typeof e&&null!==e||"function"==typeof e,s=new Map([["proxy",{canHandle:t=>r(t)&&t[e],serialize(e){const{port1:t,port2:o}=new MessageChannel;return a(e,t),[o,[o]]},deserialize(e){return e.start(),c(e,[],t);var t}}],["throw",{canHandle:e=>r(e)&&n in e,serialize({value:e}){let t;return t=e instanceof Error?{isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:{isError:!1,value:e},[t,[]]},deserialize(e){if(e.isError)throw Object.assign(new Error(e.value.message),e.value);throw e.value}}]]);function a(t,o=self){o.addEventListener("message",(function r(s){if(!s||!s.data)return;const{id:l,type:c,path:u}=Object.assign({path:[]},s.data),p=(s.data.argumentList||[]).map(m);let g;try{const o=u.slice(0,-1).reduce(((e,t)=>e[t]),t),n=u.reduce(((e,t)=>e[t]),t);switch(c){case"GET":g=n;break;case"SET":o[u.slice(-1)[0]]=m(s.data.value),g=!0;break;case"APPLY":g=n.apply(o,p);break;case"CONSTRUCT":g=function(t){return Object.assign(t,{[e]:!0})}(new n(...p));break;case"ENDPOINT":{const{port1:e,port2:o}=new MessageChannel;a(t,o),g=function(e,t){return d.set(e,t),e}(e,[e])}break;case"RELEASE":g=void 0;break;default:return}}catch(e){g={value:e,[n]:0}}Promise.resolve(g).catch((e=>({value:e,[n]:0}))).then((e=>{const[t,n]=f(e);o.postMessage(Object.assign(Object.assign({},t),{id:l}),n),"RELEASE"===c&&(o.removeEventListener("message",r),i(o))}))})),o.start&&o.start()}function i(e){(function(e){return"MessagePort"===e.constructor.name})(e)&&e.close()}function l(e){if(e)throw new Error("Proxy has been released and is not useable")}function c(e,n=[],r=function(){}){let s=!1;const a=new Proxy(r,{get(t,r){if(l(s),r===o)return()=>p(e,{type:"RELEASE",path:n.map((e=>e.toString()))}).then((()=>{i(e),s=!0}));if("then"===r){if(0===n.length)return{then:()=>a};const t=p(e,{type:"GET",path:n.map((e=>e.toString()))}).then(m);return t.then.bind(t)}return c(e,[...n,r])},set(t,o,r){l(s);const[a,i]=f(r);return p(e,{type:"SET",path:[...n,o].map((e=>e.toString())),value:a},i).then(m)},apply(o,r,a){l(s);const i=n[n.length-1];if(i===t)return p(e,{type:"ENDPOINT"}).then(m);if("bind"===i)return c(e,n.slice(0,-1));const[d,f]=u(a);return p(e,{type:"APPLY",path:n.map((e=>e.toString())),argumentList:d},f).then(m)},construct(t,o){l(s);const[r,a]=u(o);return p(e,{type:"CONSTRUCT",path:n.map((e=>e.toString())),argumentList:r},a).then(m)}});return a}function u(e){const t=e.map(f);return[t.map((e=>e[0])),(o=t.map((e=>e[1])),Array.prototype.concat.apply([],o))];var o}const d=new WeakMap;function f(e){for(const[t,o]of s)if(o.canHandle(e)){const[n,r]=o.serialize(e);return[{type:"HANDLER",name:t,value:n},r]}return[{type:"RAW",value:e},d.get(e)||[]]}function m(e){switch(e.type){case"HANDLER":return s.get(e.name).deserialize(e.value);case"RAW":return e.value}}function p(e,t,o){return new Promise((n=>{const r=new Array(4).fill(0).map((()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16))).join("-");e.addEventListener("message",(function t(o){o.data&&o.data.id&&o.data.id===r&&(e.removeEventListener("message",t),n(o.data))})),e.start&&e.start(),e.postMessage(Object.assign({id:r},t),o)}))}const g={tools:[],config:{},files:[],fs:{},async init(){if(g.tools.length<2)throw"Expecting at least 1 tool.";const e=g.tools[0];return await this._setup(e,!0),e.module.FS.mkdir(g.config.dirData,511),e.module.FS.mkdir(g.config.dirMounted,511),e.module.FS.chdir(g.config.dirData),g.fs=e.module.FS,await Promise.all(g.tools.map((e=>this._setup(e)))),await this._setupFS(),g._log("Ready"),!0},mount(e){const t=g.config.dirData,o=g.config.dirShared,n=g.config.dirMounted;let r=[],s=[];e?.length&&"string"!=typeof e||(e=[e]),g._log(`Mounting ${e.length} files`);for(let o of e)if(o instanceof File||o?.data instanceof Blob&&o.name)r.push(o),s.push(o.name);else{if("string"!=typeof o||!o.startsWith("http"))throw"Cannot mount file(s) specified. Must be a File, Blob, or a URL string.";{const e=o.split("//").pop().replace(/\//g,"-");g.fs.createLazyFile(t,e,o,!0,!0),s.
|