lufi/themes/default/public/js/worker/main.js

2 lines
14 KiB
JavaScript

var te=Object.create;var U=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var oe=Object.getOwnPropertyNames;var ie=Object.getPrototypeOf,se=Object.prototype.hasOwnProperty;var ue=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);var ae=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of oe(e))!se.call(r,o)&&o!==t&&U(r,o,{get:()=>e[o],enumerable:!(n=ne(e,o))||n.enumerable});return r};var fe=(r,e,t)=>(t=r!=null?te(ie(r)):{},ae(e||!r||!r.__esModule?U(t,"default",{value:r,enumerable:!0}):t,r));var G=ue((Ae,M)=>{"use strict";var g=typeof Reflect=="object"?Reflect:null,R=g&&typeof g.apply=="function"?g.apply:function(e,t,n){return Function.prototype.apply.call(e,t,n)},P;g&&typeof g.ownKeys=="function"?P=g.ownKeys:Object.getOwnPropertySymbols?P=function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:P=function(e){return Object.getOwnPropertyNames(e)};function ce(r){console&&console.warn&&console.warn(r)}var T=Number.isNaN||function(e){return e!==e};function a(){a.init.call(this)}M.exports=a;M.exports.once=de;a.EventEmitter=a;a.prototype._events=void 0;a.prototype._eventsCount=0;a.prototype._maxListeners=void 0;var F=10;function x(r){if(typeof r!="function")throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof r)}Object.defineProperty(a,"defaultMaxListeners",{enumerable:!0,get:function(){return F},set:function(r){if(typeof r!="number"||r<0||T(r))throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+r+".");F=r}});a.init=function(){(this._events===void 0||this._events===Object.getPrototypeOf(this)._events)&&(this._events=Object.create(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0};a.prototype.setMaxListeners=function(e){if(typeof e!="number"||e<0||T(e))throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received '+e+".");return this._maxListeners=e,this};function j(r){return r._maxListeners===void 0?a.defaultMaxListeners:r._maxListeners}a.prototype.getMaxListeners=function(){return j(this)};a.prototype.emit=function(e){for(var t=[],n=1;n<arguments.length;n++)t.push(arguments[n]);var o=e==="error",s=this._events;if(s!==void 0)o=o&&s.error===void 0;else if(!o)return!1;if(o){var i;if(t.length>0&&(i=t[0]),i instanceof Error)throw i;var f=new Error("Unhandled error."+(i?" ("+i.message+")":""));throw f.context=i,f}var h=s[e];if(h===void 0)return!1;if(typeof h=="function")R(h,this,t);else for(var m=h.length,u=B(h,m),n=0;n<m;++n)R(u[n],this,t);return!0};function N(r,e,t,n){var o,s,i;if(x(t),s=r._events,s===void 0?(s=r._events=Object.create(null),r._eventsCount=0):(s.newListener!==void 0&&(r.emit("newListener",e,t.listener?t.listener:t),s=r._events),i=s[e]),i===void 0)i=s[e]=t,++r._eventsCount;else if(typeof i=="function"?i=s[e]=n?[t,i]:[i,t]:n?i.unshift(t):i.push(t),o=j(r),o>0&&i.length>o&&!i.warned){i.warned=!0;var f=new Error("Possible EventEmitter memory leak detected. "+i.length+" "+String(e)+" listeners added. Use emitter.setMaxListeners() to increase limit");f.name="MaxListenersExceededWarning",f.emitter=r,f.type=e,f.count=i.length,ce(f)}return r}a.prototype.addListener=function(e,t){return N(this,e,t,!1)};a.prototype.on=a.prototype.addListener;a.prototype.prependListener=function(e,t){return N(this,e,t,!0)};function le(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,arguments.length===0?this.listener.call(this.target):this.listener.apply(this.target,arguments)}function W(r,e,t){var n={fired:!1,wrapFn:void 0,target:r,type:e,listener:t},o=le.bind(n);return o.listener=t,n.wrapFn=o,o}a.prototype.once=function(e,t){return x(t),this.on(e,W(this,e,t)),this};a.prototype.prependOnceListener=function(e,t){return x(t),this.prependListener(e,W(this,e,t)),this};a.prototype.removeListener=function(e,t){var n,o,s,i,f;if(x(t),o=this._events,o===void 0)return this;if(n=o[e],n===void 0)return this;if(n===t||n.listener===t)--this._eventsCount===0?this._events=Object.create(null):(delete o[e],o.removeListener&&this.emit("removeListener",e,n.listener||t));else if(typeof n!="function"){for(s=-1,i=n.length-1;i>=0;i--)if(n[i]===t||n[i].listener===t){f=n[i].listener,s=i;break}if(s<0)return this;s===0?n.shift():he(n,s),n.length===1&&(o[e]=n[0]),o.removeListener!==void 0&&this.emit("removeListener",e,f||t)}return this};a.prototype.off=a.prototype.removeListener;a.prototype.removeAllListeners=function(e){var t,n,o;if(n=this._events,n===void 0)return this;if(n.removeListener===void 0)return arguments.length===0?(this._events=Object.create(null),this._eventsCount=0):n[e]!==void 0&&(--this._eventsCount===0?this._events=Object.create(null):delete n[e]),this;if(arguments.length===0){var s=Object.keys(n),i;for(o=0;o<s.length;++o)i=s[o],i!=="removeListener"&&this.removeAllListeners(i);return this.removeAllListeners("removeListener"),this._events=Object.create(null),this._eventsCount=0,this}if(t=n[e],typeof t=="function")this.removeListener(e,t);else if(t!==void 0)for(o=t.length-1;o>=0;o--)this.removeListener(e,t[o]);return this};function J(r,e,t){var n=r._events;if(n===void 0)return[];var o=n[e];return o===void 0?[]:typeof o=="function"?t?[o.listener||o]:[o]:t?pe(o):B(o,o.length)}a.prototype.listeners=function(e){return J(this,e,!0)};a.prototype.rawListeners=function(e){return J(this,e,!1)};a.listenerCount=function(r,e){return typeof r.listenerCount=="function"?r.listenerCount(e):H.call(r,e)};a.prototype.listenerCount=H;function H(r){var e=this._events;if(e!==void 0){var t=e[r];if(typeof t=="function")return 1;if(t!==void 0)return t.length}return 0}a.prototype.eventNames=function(){return this._eventsCount>0?P(this._events):[]};function B(r,e){for(var t=new Array(e),n=0;n<e;++n)t[n]=r[n];return t}function he(r,e){for(;e+1<r.length;e++)r[e]=r[e+1];r.pop()}function pe(r){for(var e=new Array(r.length),t=0;t<e.length;++t)e[t]=r[t].listener||r[t];return e}function de(r,e){return new Promise(function(t,n){function o(i){r.removeListener(e,s),n(i)}function s(){typeof r.removeListener=="function"&&r.removeListener("error",o),t([].slice.call(arguments))}$(r,e,s,{once:!0}),e!=="error"&&me(r,o,{once:!0})})}function me(r,e,t){typeof r.on=="function"&&$(r,"error",e,t)}function $(r,e,t,n){if(typeof r.on=="function")n.once?r.once(e,t):r.on(e,t);else if(typeof r.addEventListener=="function")r.addEventListener(e,function o(s){n.once&&r.removeEventListener(e,o),t(s)});else throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof r)}});var V=fe(G());var k=(u=>(u[u.ARCHIVE_ADD=0]="ARCHIVE_ADD",u[u.ARCHHIVE_COMPRESS=1]="ARCHHIVE_COMPRESS",u[u.CANCEL=2]="CANCEL",u[u.DOWNLOAD=3]="DOWNLOAD",u[u.INFOS=4]="INFOS",u[u.PAUSE=5]="PAUSE",u[u.PROVIDE_FILE=6]="PROVIDE_FILE",u[u.REMOVE=7]="REMOVE",u[u.RESUME=8]="RESUME",u[u.UPLOAD=9]="UPLOAD",u))(k||{});var ve={withStackTrace:!1},Q=(r,e,t=ve)=>{let n=e.isOk()?{type:"Ok",value:e.value}:{type:"Err",value:e.error},o=t.withStackTrace?new Error().stack:void 0;return{data:n,message:r,stack:o}};function b(r,e,t,n){function o(s){return s instanceof t?s:new t(function(i){i(s)})}return new(t||(t=Promise))(function(s,i){function f(u){try{m(n.next(u))}catch(_){i(_)}}function h(u){try{m(n.throw(u))}catch(_){i(_)}}function m(u){u.done?s(u.value):o(u.value).then(f,h)}m((n=n.apply(r,e||[])).next())})}function q(r){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&r[e],n=0;if(t)return t.call(r);if(r&&typeof r.length=="number")return{next:function(){return r&&n>=r.length&&(r=void 0),{value:r&&r[n++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function v(r){return this instanceof v?(this.v=r,this):new v(r)}function z(r,e,t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n=t.apply(r,e||[]),o,s=[];return o=Object.create((typeof AsyncIterator=="function"?AsyncIterator:Object).prototype),f("next"),f("throw"),f("return",i),o[Symbol.asyncIterator]=function(){return this},o;function i(c){return function(E){return Promise.resolve(E).then(c,_)}}function f(c,E){n[c]&&(o[c]=function(A){return new Promise(function(ee,re){s.push([c,A,ee,re])>1||h(c,A)})},E&&(o[c]=E(o[c])))}function h(c,E){try{m(n[c](E))}catch(A){C(s[0][3],A)}}function m(c){c.value instanceof v?Promise.resolve(c.value.v).then(u,_):C(s[0][2],c)}function u(c){h("next",c)}function _(c){h("throw",c)}function C(c,E){c(E),s.shift(),s.length&&h(s[0][0],s[0][1])}}function ye(r){var e,t;return e={},n("next"),n("throw",function(o){throw o}),n("return"),e[Symbol.iterator]=function(){return this},e;function n(o,s){e[o]=r[o]?function(i){return(t=!t)?{value:v(r[o](i)),done:!1}:s?s(i):i}:s}}function we(r){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var e=r[Symbol.asyncIterator],t;return e?e.call(r):(r=typeof q=="function"?q(r):r[Symbol.iterator](),t={},n("next"),n("throw"),n("return"),t[Symbol.asyncIterator]=function(){return this},t);function n(s){t[s]=r[s]&&function(i){return new Promise(function(f,h){i=r[s](i),o(f,h,i.done,i.value)})}}function o(s,i,f,h){Promise.resolve(h).then(function(m){s({value:m,done:f})},i)}}var y=class r{constructor(e){this._promise=e}static fromSafePromise(e){let t=e.then(n=>new d(n));return new r(t)}static fromPromise(e,t){let n=e.then(o=>new d(o)).catch(o=>new p(t(o)));return new r(n)}static fromThrowable(e,t){return(...n)=>new r(b(this,void 0,void 0,function*(){try{return new d(yield e(...n))}catch(o){return new p(t?t(o):o)}}))}static combine(e){return Ee(e)}static combineWithAllErrors(e){return Le(e)}map(e){return new r(this._promise.then(t=>b(this,void 0,void 0,function*(){return t.isErr()?new p(t.error):new d(yield e(t.value))})))}andThrough(e){return new r(this._promise.then(t=>b(this,void 0,void 0,function*(){if(t.isErr())return new p(t.error);let n=yield e(t.value);return n.isErr()?new p(n.error):new d(t.value)})))}andTee(e){return new r(this._promise.then(t=>b(this,void 0,void 0,function*(){if(t.isErr())return new p(t.error);try{yield e(t.value)}catch{}return new d(t.value)})))}mapErr(e){return new r(this._promise.then(t=>b(this,void 0,void 0,function*(){return t.isOk()?new d(t.value):new p(yield e(t.error))})))}andThen(e){return new r(this._promise.then(t=>{if(t.isErr())return new p(t.error);let n=e(t.value);return n instanceof r?n._promise:n}))}orElse(e){return new r(this._promise.then(t=>b(this,void 0,void 0,function*(){return t.isErr()?e(t.error):new d(t.value)})))}match(e,t){return this._promise.then(n=>n.match(e,t))}unwrapOr(e){return this._promise.then(t=>t.unwrapOr(e))}safeUnwrap(){return z(this,arguments,function*(){return yield v(yield v(yield*ye(we(yield v(this._promise.then(t=>t.safeUnwrap()))))))})}then(e,t){return this._promise.then(e,t)}[Symbol.asyncIterator](){return z(this,arguments,function*(){let t=yield v(this._promise);return t.isErr()&&(yield yield v(O(t.error))),yield v(t.value)})}};var O=r=>new y(Promise.resolve(new p(r))),ke=y.fromPromise,Se=y.fromSafePromise,Ie=y.fromThrowable,Z=r=>{let e=L([]);for(let t of r)if(t.isErr()){e=w(t.error);break}else e.map(n=>n.push(t.value));return e},Ee=r=>y.fromSafePromise(Promise.all(r)).andThen(Z),X=r=>{let e=L([]);for(let t of r)t.isErr()&&e.isErr()?e.error.push(t.error):t.isErr()&&e.isOk()?e=w([t.error]):t.isOk()&&e.isOk()&&e.value.push(t.value);return e},Le=r=>y.fromSafePromise(Promise.all(r)).andThen(X),S;(function(r){function e(o,s){return(...i)=>{try{let f=o(...i);return L(f)}catch(f){return w(s?s(f):f)}}}r.fromThrowable=e;function t(o){return Z(o)}r.combine=t;function n(o){return X(o)}r.combineWithAllErrors=n})(S||(S={}));var L=r=>new d(r);function w(r){return new p(r)}var d=class{constructor(e){this.value=e}isOk(){return!0}isErr(){return!this.isOk()}map(e){return L(e(this.value))}mapErr(e){return L(this.value)}andThen(e){return e(this.value)}andThrough(e){return e(this.value).map(t=>this.value)}andTee(e){try{e(this.value)}catch{}return L(this.value)}orElse(e){return L(this.value)}asyncAndThen(e){return e(this.value)}asyncAndThrough(e){return e(this.value).map(()=>this.value)}asyncMap(e){return y.fromSafePromise(e(this.value))}unwrapOr(e){return this.value}match(e,t){return e(this.value)}safeUnwrap(){let e=this.value;return function*(){return e}()}_unsafeUnwrap(e){return this.value}_unsafeUnwrapErr(e){throw Q("Called `_unsafeUnwrapErr` on an Ok",this,e)}*[Symbol.iterator](){return this.value}},p=class{constructor(e){this.error=e}isOk(){return!1}isErr(){return!this.isOk()}map(e){return w(this.error)}mapErr(e){return w(e(this.error))}andThrough(e){return w(this.error)}andTee(e){return w(this.error)}andThen(e){return w(this.error)}orElse(e){return e(this.error)}asyncAndThen(e){return O(this.error)}asyncAndThrough(e){return O(this.error)}asyncMap(e){return O(this.error)}unwrapOr(e){return e}match(e,t){return t(this.error)}safeUnwrap(){let e=this.error;return function*(){throw yield w(e),new Error("Do not use this generator out of `safeTry`")}()}_unsafeUnwrap(e){throw Q("Called `_unsafeUnwrap` on an Err",this,e)}_unsafeUnwrapErr(e){return this.error}*[Symbol.iterator](){let e=this;return yield e,e}},Ce=S.fromThrowable;var ge=()=>typeof Deno<"u";var D=r=>ge()?new URL(`./worker/${r}.ts`,new URL(".",import.meta.url).href):new URL(import.meta.resolve(`./${r==="main"?`worker/${r}`:r}.js`));var I=new V.default,l,K=!1;self.onmessage=r=>{De(r),K||(K=!0,I.once("OPERATION_FAILED",()=>{l&&l.terminate()}))};var be=(r,e)=>(Object.assign(r,e),typeof WorkerGlobalScope<"u"&&self.postMessage({event:"FILE_UPDATED",lufiFile:r}),r),rr=(r,e)=>{be(r,{uploadStatus:2}),self.postMessage({event:"OPERATION_FAILED",error:e})},De=(r,e)=>{let{action:t}=r.data;if(t===5||t===8)l.postMessage(r.data);else{switch(t){case 2:l=new Worker(D("cancel"),{type:"module"}),l.postMessage(r.data);break;case 3:l=new Worker(D("download"),{type:"module"}),l.postMessage(r.data);break;case 4:l=new Worker(D("infos"),{type:"module"}),l.postMessage(r.data);break;case 7:l=l||new Worker(D("remove"),{type:"module"}),l.postMessage(r.data);break;case 9:l=l||new Worker(D("upload"),{type:"module"}),l.postMessage(r.data);break;default:console.error("Action not found")}l.onmessage=n=>{e&&e(n),self.postMessage(n.data)},l.onerror=n=>{console.error(`Error: ${n.message}`)}}},tr=()=>{I.once("SOCKET_OPENED",()=>{self.postMessage({event:"SOCKET_OPENED"})}),I.once("OPERATION_FAILED",r=>{self.postMessage({event:"OPERATION_FAILED",error:r})})},nr=r=>typeof r=="object"&&r!==null&&"action"in r&&Object.values(k).includes(r.action);export{I as events,tr as init,nr as isWorkerActionMessage,rr as sendFileError,be as updateFile};