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

2 lines
14 KiB
JavaScript

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