1 line
7.6 KiB
JavaScript
1 line
7.6 KiB
JavaScript
var e=Symbol(`Comlink.proxy`),t=Symbol(`Comlink.endpoint`),n=Symbol(`Comlink.releaseProxy`),r=Symbol(`Comlink.finalizer`),i=Symbol(`Comlink.thrown`),a=e=>typeof e==`object`&&!!e||typeof e==`function`,o=new Map([[`proxy`,{canHandle:t=>a(t)&&t[e],serialize(e){let{port1:t,port2:n}=new MessageChannel;return s(e,t),[n,[n]]},deserialize:e=>(e.start(),l(e))}],[`throw`,{canHandle:e=>a(e)&&i 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){throw e.isError?Object.assign(Error(e.value.message),e.value):e.value}}]]);function s(e,t=globalThis,n=[`*`]){t.addEventListener(`message`,(function a(o){if(!o||!o.data)return;if(!function(e,t){for(let n of e)if(t===n||n===`*`||n instanceof RegExp&&n.test(t))return!0;return!1}(n,o.origin))return void console.warn(`Invalid origin '${o.origin}' for comlink proxy`);let{id:l,type:u,path:d}=Object.assign({path:[]},o.data),f=(o.data.argumentList||[]).map(y),p;try{let t=d.slice(0,-1).reduce(((e,t)=>e[t]),e),n=d.reduce(((e,t)=>e[t]),e);switch(u){case`GET`:p=n;break;case`SET`:t[d.slice(-1)[0]]=y(o.data.value),p=!0;break;case`APPLY`:p=n.apply(t,f);break;case`CONSTRUCT`:p=_(new n(...f));break;case`ENDPOINT`:{let{port1:t,port2:n}=new MessageChannel;s(e,n),p=function(e,t){return g.set(e,t),e}(t,[t])}break;case`RELEASE`:p=void 0;break;default:return}}catch(e){p={value:e,[i]:0}}Promise.resolve(p).catch((e=>({value:e,[i]:0}))).then((n=>{let[i,o]=v(n);t.postMessage(Object.assign(Object.assign({},i),{id:l}),o),u===`RELEASE`&&(t.removeEventListener(`message`,a),c(t),r in e&&typeof e[r]==`function`&&e[r]())})).catch((e=>{let[n,r]=v({value:TypeError(`Unserializable return value`),[i]:0});t.postMessage(Object.assign(Object.assign({},n),{id:l}),r)}))})),t.start&&t.start()}function c(e){(function(e){return e.constructor.name===`MessagePort`})(e)&&e.close()}function l(e,t){return m(e,[],t)}function u(e){if(e)throw Error(`Proxy has been released and is not useable`)}function d(e){return b(e,{type:`RELEASE`}).then((()=>{c(e)}))}var f=new WeakMap,p=`FinalizationRegistry`in globalThis&&new FinalizationRegistry((e=>{let t=(f.get(e)||0)-1;f.set(e,t),t===0&&d(e)}));function m(e,r=[],i=function(){}){let a=!1,o=new Proxy(i,{get(t,i){if(u(a),i===n)return()=>{(function(e){p&&p.unregister(e)})(o),d(e),a=!0};if(i===`then`){if(r.length===0)return{then:()=>o};let t=b(e,{type:`GET`,path:r.map((e=>e.toString()))}).then(y);return t.then.bind(t)}return m(e,[...r,i])},set(t,n,i){u(a);let[o,s]=v(i);return b(e,{type:`SET`,path:[...r,n].map((e=>e.toString())),value:o},s).then(y)},apply(n,i,o){u(a);let s=r[r.length-1];if(s===t)return b(e,{type:`ENDPOINT`}).then(y);if(s===`bind`)return m(e,r.slice(0,-1));let[c,l]=h(o);return b(e,{type:`APPLY`,path:r.map((e=>e.toString())),argumentList:c},l).then(y)},construct(t,n){u(a);let[i,o]=h(n);return b(e,{type:`CONSTRUCT`,path:r.map((e=>e.toString())),argumentList:i},o).then(y)}});return function(e,t){let n=(f.get(t)||0)+1;f.set(t,n),p&&p.register(e,t,e)}(o,e),o}function h(e){let t=e.map(v);return[t.map((e=>e[0])),(n=t.map((e=>e[1])),Array.prototype.concat.apply([],n))];var n}var g=new WeakMap;function _(t){return Object.assign(t,{[e]:!0})}function v(e){for(let[t,n]of o)if(n.canHandle(e)){let[r,i]=n.serialize(e);return[{type:`HANDLER`,name:t,value:r},i]}return[{type:`RAW`,value:e},g.get(e)||[]]}function y(e){switch(e.type){case`HANDLER`:return o.get(e.name).deserialize(e.value);case`RAW`:return e.value}}function b(e,t,n){return new Promise((r=>{let i=[,,,,].fill(0).map((()=>Math.floor(Math.random()*(2**53-1)).toString(16))).join(`-`);e.addEventListener(`message`,(function t(n){n.data&&n.data.id&&n.data.id===i&&(e.removeEventListener(`message`,t),r(n.data))})),e.start&&e.start(),e.postMessage(Object.assign({id:i},t),n)}))}var x=class{constructor(e,t,n,r,i){this._name=e,this._size=t,this._path=n,this._lastModified=r,this._archiveRef=i}get name(){return this._name}get size(){return this._size}get lastModified(){return this._lastModified}extract(){return this._archiveRef.extractSingleFile(this._path)}};function S(e){if(e instanceof File||e instanceof x||e===null)return e;let t={};for(let n of Object.keys(e))t[n]=S(e[n]);return t}function C(e,t=``){let n=[];for(let r of Object.keys(e))e[r]instanceof File||e[r]instanceof x||e[r]===null?n.push({file:e[r]||r,path:t}):n.push(...C(e[r],`${t}${r}/`));return n}function w(e,t){let n=t.split(`/`);n[n.length-1]===``&&n.pop();let r=e,i=null;for(let e of n)r[e]=r[e]||{},i=r,r=r[e];return[i,n[n.length-1]]}var T=class{constructor(e,t,n){this._content={},this._processed=0,this.file=e,this.client=t,this.worker=n}open(){return this._content={},this._processed=0,new Promise(((e,t)=>{this.client.open(this.file,_((()=>{e(this)})))}))}async close(){var e;(e=this.worker)==null||e.terminate(),this.worker=null,this.client=null,this.file=null}async hasEncryptedData(){return await this.client.hasEncryptedData()}async usePassword(e){await this.client.usePassword(e)}async setLocale(e){await this.client.setLocale(e)}async getFilesObject(){return this._processed>0?Promise.resolve().then((()=>this._content)):((await this.client.listFiles()).forEach((e=>{let[t,n]=w(this._content,e.path);e.type===`FILE`&&(t[n]=new x(e.fileName,e.size,e.path,e.lastModified,this))})),this._processed=1,S(this._content))}getFilesArray(){return this.getFilesObject().then((e=>C(e)))}async extractSingleFile(e){if(this.worker===null)throw Error(`Archive already closed`);let t=await this.client.extractSingleFile(e);return new File([t.fileData],t.fileName,{type:`application/octet-stream`,lastModified:t.lastModified/1e6})}async extractFiles(e=void 0){var t;return this._processed>1?Promise.resolve().then((()=>this._content)):((await this.client.extractFiles()).forEach((t=>{let[n,r]=w(this._content,t.path);t.type===`FILE`&&(n[r]=new File([t.fileData],t.fileName,{type:`application/octet-stream`}),e!==void 0&&setTimeout(e.bind(null,{file:n[r],path:t.path})))})),this._processed=2,(t=this.worker)==null||t.terminate(),S(this._content))}},E,D;(function(e){e.SEVEN_ZIP=`7zip`,e.AR=`ar`,e.ARBSD=`arbsd`,e.ARGNU=`argnu`,e.ARSVR4=`arsvr4`,e.BIN=`bin`,e.BSDTAR=`bsdtar`,e.CD9660=`cd9660`,e.CPIO=`cpio`,e.GNUTAR=`gnutar`,e.ISO=`iso`,e.ISO9660=`iso9660`,e.MTREE=`mtree`,e.MTREE_CLASSIC=`mtree-classic`,e.NEWC=`newc`,e.ODC=`odc`,e.OLDTAR=`oldtar`,e.PAX=`pax`,e.PAXR=`paxr`,e.POSIX=`posix`,e.PWB=`pwb`,e.RAW=`raw`,e.RPAX=`rpax`,e.SHAR=`shar`,e.SHARDUMP=`shardump`,e.USTAR=`ustar`,e.V7TAR=`v7tar`,e.V7=`v7`,e.WARC=`warc`,e.XAR=`xar`,e.ZIP=`zip`})(E||={}),function(e){e.B64ENCODE=`b64encode`,e.BZIP2=`bzip2`,e.COMPRESS=`compress`,e.GRZIP=`grzip`,e.GZIP=`gzip`,e.LRZIP=`lrzip`,e.LZ4=`lz4`,e.LZIP=`lzip`,e.LZMA=`lzma`,e.LZOP=`lzop`,e.UUENCODE=`uuencode`,e.XZ=`xz`,e.ZSTD=`zstd`,e.NONE=`none`}(D||={});var O=class e{static init(t=null){return e._options=t||{},e._options}static async open(t){let n=e.getWorker(e._options);return await new T(t,await e.getClient(n,e._options),n).open()}static async write({files:t,outputFileName:n,compression:r,format:i,passphrase:a=null}){let o=e.getWorker(e._options),s=await(await e.getClient(o,e._options)).writeArchive(t,r,i,a);return o.terminate(),new File([s],n,{type:`application/octet-stream`})}static getWorker(e){return e.getWorker?e.getWorker():new Worker(e.workerUrl||new URL(``+new URL(`worker-bundle-Dx5mKZOL.js`,import.meta.url).href,``+import.meta.url),{type:`module`})}static async getClient(e,t){let n=t.createClient?.call(t,e)||l(e),{promise:r,resolve:i}=Promise.withResolvers(),a=await new n(_((()=>{i()})));return await r,a}};O._options={},Promise.withResolvers||(Promise.withResolvers=function(){var e,t,n=new this((function(n,r){e=n,t=r}));return{resolve:e,reject:t,promise:n}});export{O as Archive,D as ArchiveCompression,E as ArchiveFormat}; |