@php-wasm/progress 0.7.0 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.cjs +3 -1
  2. package/index.js +308 -144
  3. package/package.json +4 -3
package/index.cjs CHANGED
@@ -1 +1,3 @@
1
- "use strict";var v=(r,t,e)=>{if(!t.has(r))throw TypeError("Cannot "+e)};var a=(r,t,e)=>(v(r,t,"read from private field"),e?e.call(r):t.get(r)),u=(r,t,e)=>{if(t.has(r))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(r):t.set(r,e)},b=(r,t,e,s)=>(v(r,t,"write to private field"),s?s.call(r,e):t.set(r,e),e);var E=(r,t,e,s)=>({set _(n){b(r,t,n,e)},get _(){return a(r,t,s)}}),m=(r,t,e)=>(v(r,t,"access private method"),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=function(){var r;return typeof process<"u"&&((r=process.release)==null?void 0:r.name)==="node"?"NODE":typeof window<"u"?"WEB":typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope?"WORKER":"NODE"}();if(R==="NODE"){let r=function(e){return new Promise(function(s,n){e.onload=e.onerror=function(i){e.onload=e.onerror=null,i.type==="load"?s(e.result):n(new Error("Failed to read the blob/file"))}})},t=function(){const e=new Uint8Array([1,2,3,4]),n=new File([e],"test").stream();try{return n.getReader({mode:"byob"}),!0}catch{return!1}};if(typeof File>"u"){class e extends Blob{constructor(n,i,l){super(n);let o;l!=null&&l.lastModified&&(o=new Date),(!o||isNaN(o.getFullYear()))&&(o=new Date),this.lastModifiedDate=o,this.lastModified=o.getMilliseconds(),this.name=i||""}}global.File=e}typeof Blob.prototype.arrayBuffer>"u"&&(Blob.prototype.arrayBuffer=function(){const s=new FileReader;return s.readAsArrayBuffer(this),r(s)}),typeof Blob.prototype.text>"u"&&(Blob.prototype.text=function(){const s=new FileReader;return s.readAsText(this),r(s)}),(typeof Blob.prototype.stream>"u"||!t())&&(Blob.prototype.stream=function(){let e=0;const s=this;return new ReadableStream({type:"bytes",autoAllocateChunkSize:512*1024,async pull(n){const i=n.byobRequest.view,o=await s.slice(e,e+i.byteLength).arrayBuffer(),f=new Uint8Array(o);new Uint8Array(i.buffer).set(f);const g=f.byteLength;n.byobRequest.respond(g),e+=g,e>=s.size&&n.close()}})})}if(R==="NODE"&&typeof CustomEvent>"u"){class r extends Event{constructor(e,s={}){super(e,s),this.detail=s.detail}initCustomEvent(){}}globalThis.CustomEvent=r}const C=5*1024*1024;var c,h,_,O;class I extends EventTarget{constructor(){super(...arguments);u(this,_);u(this,c,{});u(this,h,{})}expectAssets(e){for(const[s,n]of Object.entries(e)){const i="http://example.com/",o=new URL(s,i).pathname.split("/").pop();o in a(this,c)||(a(this,c)[o]=n),o in a(this,h)||(a(this,h)[o]=0)}}async monitorFetch(e){const s=await e;return B(s,i=>{m(this,_,O).call(this,s.url,i.detail.loaded,i.detail.total)})}}c=new WeakMap,h=new WeakMap,_=new WeakSet,O=function(e,s,n){const i=new URL(e,"http://example.com").pathname.split("/").pop();n||(n=a(this,c)[i]),i in a(this,h)||console.warn(`Registered a download #progress of an unregistered file "${i}". This may cause a sudden **decrease** in the #progress percentage as the total number of bytes increases during the download.`),a(this,h)[i]=s,this.dispatchEvent(new CustomEvent("progress",{detail:{loaded:T(a(this,h)),total:T(a(this,c))}}))};function T(r){return Object.values(r).reduce((t,e)=>t+e,0)}function B(r,t){const e=r.headers.get("content-length")||"",s=parseInt(e,10)||C;function n(i,l){t(new CustomEvent("progress",{detail:{loaded:i,total:l}}))}return new Response(new ReadableStream({async start(i){if(!r.body){i.close();return}const l=r.body.getReader();let o=0;for(;;)try{const{done:f,value:g}=await l.read();if(g&&(o+=g.byteLength),f){n(o,o),i.close();break}else n(o,s),i.enqueue(g)}catch(f){console.error({e:f}),i.error(f);break}}}),{status:r.status,statusText:r.statusText,headers:r.headers})}var d,p,y,P;class k extends EventTarget{constructor(){super(...arguments);u(this,y);u(this,d,void 0);u(this,p,void 0);b(this,d,{}),b(this,p,0),this.progress=0,this.mode="REAL_TIME",this.caption=""}partialObserver(e,s=""){const n=++E(this,p)._;return a(this,d)[n]=0,i=>{const{loaded:l,total:o}=(i==null?void 0:i.detail)||{};a(this,d)[n]=l/o*e,m(this,y,P).call(this,this.totalProgress,"REAL_TIME",s)}}slowlyIncrementBy(e){const s=++E(this,p)._;a(this,d)[s]=e,m(this,y,P).call(this,this.totalProgress,"SLOWLY_INCREMENT")}get totalProgress(){return Object.values(a(this,d)).reduce((e,s)=>e+s,0)}}d=new WeakMap,p=new WeakMap,y=new WeakSet,P=function(e,s,n){this.dispatchEvent(new CustomEvent("progress",{detail:{progress:e,mode:s,caption:n}}))};const w=1e-5;class L extends EventTarget{constructor({weight:t=1,caption:e="",fillTime:s=4}={}){super(),this._selfWeight=1,this._selfDone=!1,this._selfProgress=0,this._selfCaption="",this._isFilling=!1,this._subTrackers=[],this._weight=t,this._selfCaption=e,this._fillTime=s}stage(t,e=""){if(t||(t=this._selfWeight),this._selfWeight-t<-w)throw new Error(`Cannot add a stage with weight ${t} as the total weight of registered stages would exceed 1.`);this._selfWeight-=t;const s=new L({caption:e,weight:t,fillTime:this._fillTime});return this._subTrackers.push(s),s.addEventListener("progress",()=>this.notifyProgress()),s.addEventListener("done",()=>{this.done&&this.notifyDone()}),s}fillSlowly({stopBeforeFinishing:t=!0}={}){if(this._isFilling)return;this._isFilling=!0;const e=100,s=this._fillTime/e;this._fillInterval=setInterval(()=>{this.set(this._selfProgress+1),t&&this._selfProgress>=99&&clearInterval(this._fillInterval)},s)}set(t){this._selfProgress=Math.min(t,100),this.notifyProgress(),this._selfProgress+w>=100&&this.finish()}finish(){this._fillInterval&&clearInterval(this._fillInterval),this._selfDone=!0,this._selfProgress=100,this._isFilling=!1,this._fillInterval=void 0,this.notifyProgress(),this.notifyDone()}get caption(){for(let t=this._subTrackers.length-1;t>=0;t--)if(!this._subTrackers[t].done){const e=this._subTrackers[t].caption;if(e)return e}return this._selfCaption}setCaption(t){this._selfCaption=t,this.notifyProgress()}get done(){return this.progress+w>=100}get progress(){if(this._selfDone)return 100;const t=this._subTrackers.reduce((e,s)=>e+s.progress*s.weight,this._selfProgress*this._selfWeight);return Math.round(t*1e4)/1e4}get weight(){return this._weight}get observer(){return this._progressObserver||(this._progressObserver=t=>{this.set(t)}),this._progressObserver}get loadingListener(){return this._loadingListener||(this._loadingListener=t=>{this.set(t.detail.loaded/t.detail.total*100)}),this._loadingListener}pipe(t){t.setProgress({progress:this.progress,caption:this.caption}),this.addEventListener("progress",e=>{t.setProgress({progress:e.detail.progress,caption:e.detail.caption})}),this.addEventListener("done",()=>{t.setLoaded()})}addEventListener(t,e){super.addEventListener(t,e)}removeEventListener(t,e){super.removeEventListener(t,e)}notifyProgress(){const t=this;this.dispatchEvent(new CustomEvent("progress",{detail:{get progress(){return t.progress},get caption(){return t.caption}}}))}notifyDone(){this.dispatchEvent(new CustomEvent("done"))}}exports.EmscriptenDownloadMonitor=I;exports.ProgressObserver=k;exports.ProgressTracker=L;exports.cloneResponseMonitorProgress=B;
1
+ "use strict";var w=(s,e,t)=>{if(!e.has(s))throw TypeError("Cannot "+t)};var a=(s,e,t)=>(w(s,e,"read from private field"),t?t.call(s):e.get(s)),u=(s,e,t)=>{if(e.has(s))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(s):e.set(s,t)},y=(s,e,t,r)=>(w(s,e,"write to private field"),r?r.call(s,t):e.set(s,t),t);var E=(s,e,t,r)=>({set _(n){y(s,e,n,t)},get _(){return a(s,e,r)}}),b=(s,e,t)=>(w(s,e,"access private method"),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=function(){var s;return typeof process<"u"&&((s=process.release)==null?void 0:s.name)==="node"?"NODE":typeof window<"u"?"WEB":typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope?"WORKER":"NODE"}();if(I==="NODE"){let s=function(t){return new Promise(function(r,n){t.onload=t.onerror=function(i){t.onload=t.onerror=null,i.type==="load"?r(t.result):n(new Error("Failed to read the blob/file"))}})},e=function(){const t=new Uint8Array([1,2,3,4]),n=new File([t],"test").stream();try{return n.getReader({mode:"byob"}),!0}catch{return!1}};if(typeof File>"u"){class t extends Blob{constructor(n,i,l){super(n);let o;l!=null&&l.lastModified&&(o=new Date),(!o||isNaN(o.getFullYear()))&&(o=new Date),this.lastModifiedDate=o,this.lastModified=o.getMilliseconds(),this.name=i||""}}global.File=t}typeof Blob.prototype.arrayBuffer>"u"&&(Blob.prototype.arrayBuffer=function(){const r=new FileReader;return r.readAsArrayBuffer(this),s(r)}),typeof Blob.prototype.text>"u"&&(Blob.prototype.text=function(){const r=new FileReader;return r.readAsText(this),s(r)}),(typeof Blob.prototype.stream>"u"||!e())&&(Blob.prototype.stream=function(){let t=0;const r=this;return new ReadableStream({type:"bytes",autoAllocateChunkSize:512*1024,async pull(n){const i=n.byobRequest.view,o=await r.slice(t,t+i.byteLength).arrayBuffer(),c=new Uint8Array(o);new Uint8Array(i.buffer).set(c);const g=c.byteLength;n.byobRequest.respond(g),t+=g,t>=r.size&&n.close()}})})}if(I==="NODE"&&typeof CustomEvent>"u"){class s extends Event{constructor(t,r={}){super(t,r),this.detail=r.detail}initCustomEvent(){}}globalThis.CustomEvent=s}const O=(s,...e)=>{switch(s.severity){case"Debug":console.debug(s.message,...e);break;case"Info":console.info(s.message,...e);break;case"Warn":console.warn(s.message,...e);break;case"Error":console.error(s.message,...e);break;case"Fatal":console.error(s.message,...e);break;default:console.log(s.message,...e)}},S=(s,...e)=>[typeof s=="object"?JSON.stringify(s):s,...e.map(t=>JSON.stringify(t))].join(" "),R=[],P=s=>{R.push(s)},x=s=>{if(s.raw===!0)P(s.message);else{const e=F(typeof s.message=="object"?S(s.message):s.message,s.severity??"Info",s.prefix??"JavaScript");P(e)}};class B extends EventTarget{constructor(e=[]){super(),this.handlers=e,this.fatalErrorEvent="playground-fatal-error"}getLogs(){return this.handlers.includes(x)?[...R]:(this.error(`Logs aren't stored because the logToMemory handler isn't registered.
2
+ If you're using a custom logger instance, make sure to register logToMemory handler.
3
+ `),[])}logMessage(e,...t){for(const r of this.handlers)r(e,...t)}log(e,...t){this.logMessage({message:e,severity:void 0,prefix:"JavaScript",raw:!1},...t)}debug(e,...t){this.logMessage({message:e,severity:"Debug",prefix:"JavaScript",raw:!1},...t)}info(e,...t){this.logMessage({message:e,severity:"Info",prefix:"JavaScript",raw:!1},...t)}warn(e,...t){this.logMessage({message:e,severity:"Warn",prefix:"JavaScript",raw:!1},...t)}error(e,...t){this.logMessage({message:e,severity:"Error",prefix:"JavaScript",raw:!1},...t)}}const k=new B([x,O]),F=(s,e,t)=>{const r=new Date,n=new Intl.DateTimeFormat("en-GB",{year:"numeric",month:"short",day:"2-digit",timeZone:"UTC"}).format(r).replace(/ /g,"-"),i=new Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1,timeZone:"UTC",timeZoneName:"short"}).format(r);return`[${n+" "+i}] ${t} ${e}: ${s}`},A=5*1024*1024;var f,h,v,C;class N extends EventTarget{constructor(){super(...arguments);u(this,v);u(this,f,{});u(this,h,{})}expectAssets(t){for(const[r,n]of Object.entries(t)){const i="http://example.com/",o=new URL(r,i).pathname.split("/").pop();o in a(this,f)||(a(this,f)[o]=n),o in a(this,h)||(a(this,h)[o]=0)}}async monitorFetch(t){const r=await t;return D(r,i=>{b(this,v,C).call(this,r.url,i.detail.loaded,i.detail.total)})}}f=new WeakMap,h=new WeakMap,v=new WeakSet,C=function(t,r,n){const i=new URL(t,"http://example.com").pathname.split("/").pop();n?i in a(this,f)||(a(this,f)[i]=n,a(this,h)[i]=r):n=a(this,f)[i],i in a(this,h)||k.warn(`Registered a download #progress of an unregistered file "${i}". This may cause a sudden **decrease** in the #progress percentage as the total number of bytes increases during the download.`),a(this,h)[i]=r,this.dispatchEvent(new CustomEvent("progress",{detail:{loaded:M(a(this,h)),total:M(a(this,f))}}))};function M(s){return Object.values(s).reduce((e,t)=>e+t,0)}function D(s,e){const t=s.headers.get("content-length")||"",r=parseInt(t,10)||A;function n(i,l){e(new CustomEvent("progress",{detail:{loaded:i,total:l}}))}return new Response(new ReadableStream({async start(i){if(!s.body){i.close();return}const l=s.body.getReader();let o=0;for(;;)try{const{done:c,value:g}=await l.read();if(g&&(o+=g.byteLength),c){n(o,o),i.close();break}else n(o,r),i.enqueue(g)}catch(c){k.error({e:c}),i.error(c);break}}}),{status:s.status,statusText:s.statusText,headers:s.headers})}var d,p,m,T;class W extends EventTarget{constructor(){super(...arguments);u(this,m);u(this,d,void 0);u(this,p,void 0);y(this,d,{}),y(this,p,0),this.progress=0,this.mode="REAL_TIME",this.caption=""}partialObserver(t,r=""){const n=++E(this,p)._;return a(this,d)[n]=0,i=>{const{loaded:l,total:o}=(i==null?void 0:i.detail)||{};a(this,d)[n]=l/o*t,b(this,m,T).call(this,this.totalProgress,"REAL_TIME",r)}}slowlyIncrementBy(t){const r=++E(this,p)._;a(this,d)[r]=t,b(this,m,T).call(this,this.totalProgress,"SLOWLY_INCREMENT")}get totalProgress(){return Object.values(a(this,d)).reduce((t,r)=>t+r,0)}}d=new WeakMap,p=new WeakMap,m=new WeakSet,T=function(t,r,n){this.dispatchEvent(new CustomEvent("progress",{detail:{progress:t,mode:r,caption:n}}))};const _=1e-5;class L extends EventTarget{constructor({weight:e=1,caption:t="",fillTime:r=4}={}){super(),this._selfWeight=1,this._selfDone=!1,this._selfProgress=0,this._selfCaption="",this._isFilling=!1,this._subTrackers=[],this._weight=e,this._selfCaption=t,this._fillTime=r}stage(e,t=""){if(e||(e=this._selfWeight),this._selfWeight-e<-_)throw new Error(`Cannot add a stage with weight ${e} as the total weight of registered stages would exceed 1.`);this._selfWeight-=e;const r=new L({caption:t,weight:e,fillTime:this._fillTime});return this._subTrackers.push(r),r.addEventListener("progress",()=>this.notifyProgress()),r.addEventListener("done",()=>{this.done&&this.notifyDone()}),r}fillSlowly({stopBeforeFinishing:e=!0}={}){if(this._isFilling)return;this._isFilling=!0;const t=100,r=this._fillTime/t;this._fillInterval=setInterval(()=>{this.set(this._selfProgress+1),e&&this._selfProgress>=99&&clearInterval(this._fillInterval)},r)}set(e){this._selfProgress=Math.min(e,100),this.notifyProgress(),this._selfProgress+_>=100&&this.finish()}finish(){this._fillInterval&&clearInterval(this._fillInterval),this._selfDone=!0,this._selfProgress=100,this._isFilling=!1,this._fillInterval=void 0,this.notifyProgress(),this.notifyDone()}get caption(){for(let e=this._subTrackers.length-1;e>=0;e--)if(!this._subTrackers[e].done){const t=this._subTrackers[e].caption;if(t)return t}return this._selfCaption}setCaption(e){this._selfCaption=e,this.notifyProgress()}get done(){return this.progress+_>=100}get progress(){if(this._selfDone)return 100;const e=this._subTrackers.reduce((t,r)=>t+r.progress*r.weight,this._selfProgress*this._selfWeight);return Math.round(e*1e4)/1e4}get weight(){return this._weight}get observer(){return this._progressObserver||(this._progressObserver=e=>{this.set(e)}),this._progressObserver}get loadingListener(){return this._loadingListener||(this._loadingListener=e=>{this.set(e.detail.loaded/e.detail.total*100)}),this._loadingListener}pipe(e){e.setProgress({progress:this.progress,caption:this.caption}),this.addEventListener("progress",t=>{e.setProgress({progress:t.detail.progress,caption:t.detail.caption})}),this.addEventListener("done",()=>{e.setLoaded()})}addEventListener(e,t){super.addEventListener(e,t)}removeEventListener(e,t){super.removeEventListener(e,t)}notifyProgress(){const e=this;this.dispatchEvent(new CustomEvent("progress",{detail:{get progress(){return e.progress},get caption(){return e.caption}}}))}notifyDone(){this.dispatchEvent(new CustomEvent("done"))}}exports.EmscriptenDownloadMonitor=N;exports.ProgressObserver=W;exports.ProgressTracker=L;exports.cloneResponseMonitorProgress=D;
package/index.js CHANGED
@@ -1,37 +1,37 @@
1
- var v = (r, t, e) => {
2
- if (!t.has(r))
3
- throw TypeError("Cannot " + e);
1
+ var w = (s, e, t) => {
2
+ if (!e.has(s))
3
+ throw TypeError("Cannot " + t);
4
4
  };
5
- var a = (r, t, e) => (v(r, t, "read from private field"), e ? e.call(r) : t.get(r)), u = (r, t, e) => {
6
- if (t.has(r))
5
+ var a = (s, e, t) => (w(s, e, "read from private field"), t ? t.call(s) : e.get(s)), u = (s, e, t) => {
6
+ if (e.has(s))
7
7
  throw TypeError("Cannot add the same private member more than once");
8
- t instanceof WeakSet ? t.add(r) : t.set(r, e);
9
- }, b = (r, t, e, s) => (v(r, t, "write to private field"), s ? s.call(r, e) : t.set(r, e), e);
10
- var E = (r, t, e, s) => ({
8
+ e instanceof WeakSet ? e.add(s) : e.set(s, t);
9
+ }, y = (s, e, t, r) => (w(s, e, "write to private field"), r ? r.call(s, t) : e.set(s, t), t);
10
+ var E = (s, e, t, r) => ({
11
11
  set _(n) {
12
- b(r, t, n, e);
12
+ y(s, e, n, t);
13
13
  },
14
14
  get _() {
15
- return a(r, t, s);
15
+ return a(s, e, r);
16
16
  }
17
- }), _ = (r, t, e) => (v(r, t, "access private method"), e);
18
- const R = function() {
19
- var r;
20
- return typeof process < "u" && ((r = process.release) == null ? void 0 : r.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : (
17
+ }), b = (s, e, t) => (w(s, e, "access private method"), t);
18
+ const x = function() {
19
+ var s;
20
+ return typeof process < "u" && ((s = process.release) == null ? void 0 : s.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : (
21
21
  // @ts-ignore
22
22
  typeof WorkerGlobalScope < "u" && // @ts-ignore
23
23
  self instanceof WorkerGlobalScope ? "WORKER" : "NODE"
24
24
  );
25
25
  }();
26
- if (R === "NODE") {
27
- let r = function(e) {
28
- return new Promise(function(s, n) {
29
- e.onload = e.onerror = function(i) {
30
- e.onload = e.onerror = null, i.type === "load" ? s(e.result) : n(new Error("Failed to read the blob/file"));
26
+ if (x === "NODE") {
27
+ let s = function(t) {
28
+ return new Promise(function(r, n) {
29
+ t.onload = t.onerror = function(i) {
30
+ t.onload = t.onerror = null, i.type === "load" ? r(t.result) : n(new Error("Failed to read the blob/file"));
31
31
  };
32
32
  });
33
- }, t = function() {
34
- const e = new Uint8Array([1, 2, 3, 4]), n = new File([e], "test").stream();
33
+ }, e = function() {
34
+ const t = new Uint8Array([1, 2, 3, 4]), n = new File([t], "test").stream();
35
35
  try {
36
36
  return n.getReader({ mode: "byob" }), !0;
37
37
  } catch {
@@ -39,54 +39,218 @@ if (R === "NODE") {
39
39
  }
40
40
  };
41
41
  if (typeof File > "u") {
42
- class e extends Blob {
42
+ class t extends Blob {
43
43
  constructor(n, i, l) {
44
44
  super(n);
45
45
  let o;
46
46
  l != null && l.lastModified && (o = /* @__PURE__ */ new Date()), (!o || isNaN(o.getFullYear())) && (o = /* @__PURE__ */ new Date()), this.lastModifiedDate = o, this.lastModified = o.getMilliseconds(), this.name = i || "";
47
47
  }
48
48
  }
49
- global.File = e;
49
+ global.File = t;
50
50
  }
51
51
  typeof Blob.prototype.arrayBuffer > "u" && (Blob.prototype.arrayBuffer = function() {
52
- const s = new FileReader();
53
- return s.readAsArrayBuffer(this), r(s);
52
+ const r = new FileReader();
53
+ return r.readAsArrayBuffer(this), s(r);
54
54
  }), typeof Blob.prototype.text > "u" && (Blob.prototype.text = function() {
55
- const s = new FileReader();
56
- return s.readAsText(this), r(s);
57
- }), (typeof Blob.prototype.stream > "u" || !t()) && (Blob.prototype.stream = function() {
58
- let e = 0;
59
- const s = this;
55
+ const r = new FileReader();
56
+ return r.readAsText(this), s(r);
57
+ }), (typeof Blob.prototype.stream > "u" || !e()) && (Blob.prototype.stream = function() {
58
+ let t = 0;
59
+ const r = this;
60
60
  return new ReadableStream({
61
61
  type: "bytes",
62
62
  // 0.5 MB seems like a reasonable chunk size, let's adjust
63
63
  // this if needed.
64
64
  autoAllocateChunkSize: 512 * 1024,
65
65
  async pull(n) {
66
- const i = n.byobRequest.view, o = await s.slice(
67
- e,
68
- e + i.byteLength
69
- ).arrayBuffer(), f = new Uint8Array(o);
70
- new Uint8Array(i.buffer).set(f);
71
- const p = f.byteLength;
72
- n.byobRequest.respond(p), e += p, e >= s.size && n.close();
66
+ const i = n.byobRequest.view, o = await r.slice(
67
+ t,
68
+ t + i.byteLength
69
+ ).arrayBuffer(), c = new Uint8Array(o);
70
+ new Uint8Array(i.buffer).set(c);
71
+ const g = c.byteLength;
72
+ n.byobRequest.respond(g), t += g, t >= r.size && n.close();
73
73
  }
74
74
  });
75
75
  });
76
76
  }
77
- if (R === "NODE" && typeof CustomEvent > "u") {
78
- class r extends Event {
79
- constructor(e, s = {}) {
80
- super(e, s), this.detail = s.detail;
77
+ if (x === "NODE" && typeof CustomEvent > "u") {
78
+ class s extends Event {
79
+ constructor(t, r = {}) {
80
+ super(t, r), this.detail = r.detail;
81
81
  }
82
82
  initCustomEvent() {
83
83
  }
84
84
  }
85
- globalThis.CustomEvent = r;
85
+ globalThis.CustomEvent = s;
86
86
  }
87
- const C = 5 * 1024 * 1024;
88
- var c, h, m, T;
89
- class x extends EventTarget {
87
+ const B = (s, ...e) => {
88
+ switch (s.severity) {
89
+ case "Debug":
90
+ console.debug(s.message, ...e);
91
+ break;
92
+ case "Info":
93
+ console.info(s.message, ...e);
94
+ break;
95
+ case "Warn":
96
+ console.warn(s.message, ...e);
97
+ break;
98
+ case "Error":
99
+ console.error(s.message, ...e);
100
+ break;
101
+ case "Fatal":
102
+ console.error(s.message, ...e);
103
+ break;
104
+ default:
105
+ console.log(s.message, ...e);
106
+ }
107
+ }, D = (s, ...e) => [
108
+ typeof s == "object" ? JSON.stringify(s) : s,
109
+ ...e.map((t) => JSON.stringify(t))
110
+ ].join(" "), I = [], T = (s) => {
111
+ I.push(s);
112
+ }, M = (s) => {
113
+ if (s.raw === !0)
114
+ T(s.message);
115
+ else {
116
+ const e = S(
117
+ typeof s.message == "object" ? D(s.message) : s.message,
118
+ s.severity ?? "Info",
119
+ s.prefix ?? "JavaScript"
120
+ );
121
+ T(e);
122
+ }
123
+ };
124
+ class O extends EventTarget {
125
+ // constructor
126
+ constructor(e = []) {
127
+ super(), this.handlers = e, this.fatalErrorEvent = "playground-fatal-error";
128
+ }
129
+ /**
130
+ * Get all logs.
131
+ * @returns string[]
132
+ */
133
+ getLogs() {
134
+ return this.handlers.includes(M) ? [...I] : (this.error(`Logs aren't stored because the logToMemory handler isn't registered.
135
+ If you're using a custom logger instance, make sure to register logToMemory handler.
136
+ `), []);
137
+ }
138
+ /**
139
+ * Log message with severity.
140
+ *
141
+ * @param message any
142
+ * @param severity LogSeverity
143
+ * @param raw boolean
144
+ * @param args any
145
+ */
146
+ logMessage(e, ...t) {
147
+ for (const r of this.handlers)
148
+ r(e, ...t);
149
+ }
150
+ /**
151
+ * Log message
152
+ *
153
+ * @param message any
154
+ * @param args any
155
+ */
156
+ log(e, ...t) {
157
+ this.logMessage(
158
+ {
159
+ message: e,
160
+ severity: void 0,
161
+ prefix: "JavaScript",
162
+ raw: !1
163
+ },
164
+ ...t
165
+ );
166
+ }
167
+ /**
168
+ * Log debug message
169
+ *
170
+ * @param message any
171
+ * @param args any
172
+ */
173
+ debug(e, ...t) {
174
+ this.logMessage(
175
+ {
176
+ message: e,
177
+ severity: "Debug",
178
+ prefix: "JavaScript",
179
+ raw: !1
180
+ },
181
+ ...t
182
+ );
183
+ }
184
+ /**
185
+ * Log info message
186
+ *
187
+ * @param message any
188
+ * @param args any
189
+ */
190
+ info(e, ...t) {
191
+ this.logMessage(
192
+ {
193
+ message: e,
194
+ severity: "Info",
195
+ prefix: "JavaScript",
196
+ raw: !1
197
+ },
198
+ ...t
199
+ );
200
+ }
201
+ /**
202
+ * Log warning message
203
+ *
204
+ * @param message any
205
+ * @param args any
206
+ */
207
+ warn(e, ...t) {
208
+ this.logMessage(
209
+ {
210
+ message: e,
211
+ severity: "Warn",
212
+ prefix: "JavaScript",
213
+ raw: !1
214
+ },
215
+ ...t
216
+ );
217
+ }
218
+ /**
219
+ * Log error message
220
+ *
221
+ * @param message any
222
+ * @param args any
223
+ */
224
+ error(e, ...t) {
225
+ this.logMessage(
226
+ {
227
+ message: e,
228
+ severity: "Error",
229
+ prefix: "JavaScript",
230
+ raw: !1
231
+ },
232
+ ...t
233
+ );
234
+ }
235
+ }
236
+ const R = new O([M, B]), S = (s, e, t) => {
237
+ const r = /* @__PURE__ */ new Date(), n = new Intl.DateTimeFormat("en-GB", {
238
+ year: "numeric",
239
+ month: "short",
240
+ day: "2-digit",
241
+ timeZone: "UTC"
242
+ }).format(r).replace(/ /g, "-"), i = new Intl.DateTimeFormat("en-GB", {
243
+ hour: "2-digit",
244
+ minute: "2-digit",
245
+ second: "2-digit",
246
+ hour12: !1,
247
+ timeZone: "UTC",
248
+ timeZoneName: "short"
249
+ }).format(r);
250
+ return `[${n + " " + i}] ${t} ${e}: ${s}`;
251
+ }, F = 5 * 1024 * 1024;
252
+ var f, h, v, C;
253
+ class W extends EventTarget {
90
254
  constructor() {
91
255
  super(...arguments);
92
256
  /**
@@ -96,43 +260,43 @@ class x extends EventTarget {
96
260
  * @param loaded The number of bytes of that file loaded so far.
97
261
  * @param fileSize The total number of bytes in the loaded file.
98
262
  */
99
- u(this, m);
100
- u(this, c, {});
263
+ u(this, v);
264
+ u(this, f, {});
101
265
  u(this, h, {});
102
266
  }
103
- expectAssets(e) {
104
- for (const [s, n] of Object.entries(e)) {
105
- const i = "http://example.com/", o = new URL(s, i).pathname.split("/").pop();
106
- o in a(this, c) || (a(this, c)[o] = n), o in a(this, h) || (a(this, h)[o] = 0);
267
+ expectAssets(t) {
268
+ for (const [r, n] of Object.entries(t)) {
269
+ const i = "http://example.com/", o = new URL(r, i).pathname.split("/").pop();
270
+ o in a(this, f) || (a(this, f)[o] = n), o in a(this, h) || (a(this, h)[o] = 0);
107
271
  }
108
272
  }
109
- async monitorFetch(e) {
110
- const s = await e;
111
- return I(s, (i) => {
112
- _(this, m, T).call(this, s.url, i.detail.loaded, i.detail.total);
273
+ async monitorFetch(t) {
274
+ const r = await t;
275
+ return A(r, (i) => {
276
+ b(this, v, C).call(this, r.url, i.detail.loaded, i.detail.total);
113
277
  });
114
278
  }
115
279
  }
116
- c = new WeakMap(), h = new WeakMap(), m = new WeakSet(), T = function(e, s, n) {
117
- const i = new URL(e, "http://example.com").pathname.split("/").pop();
118
- n || (n = a(this, c)[i]), i in a(this, h) || console.warn(
280
+ f = new WeakMap(), h = new WeakMap(), v = new WeakSet(), C = function(t, r, n) {
281
+ const i = new URL(t, "http://example.com").pathname.split("/").pop();
282
+ n ? i in a(this, f) || (a(this, f)[i] = n, a(this, h)[i] = r) : n = a(this, f)[i], i in a(this, h) || R.warn(
119
283
  `Registered a download #progress of an unregistered file "${i}". This may cause a sudden **decrease** in the #progress percentage as the total number of bytes increases during the download.`
120
- ), a(this, h)[i] = s, this.dispatchEvent(
284
+ ), a(this, h)[i] = r, this.dispatchEvent(
121
285
  new CustomEvent("progress", {
122
286
  detail: {
123
287
  loaded: P(a(this, h)),
124
- total: P(a(this, c))
288
+ total: P(a(this, f))
125
289
  }
126
290
  })
127
291
  );
128
292
  };
129
- function P(r) {
130
- return Object.values(r).reduce((t, e) => t + e, 0);
293
+ function P(s) {
294
+ return Object.values(s).reduce((e, t) => e + t, 0);
131
295
  }
132
- function I(r, t) {
133
- const e = r.headers.get("content-length") || "", s = parseInt(e, 10) || C;
296
+ function A(s, e) {
297
+ const t = s.headers.get("content-length") || "", r = parseInt(t, 10) || F;
134
298
  function n(i, l) {
135
- t(
299
+ e(
136
300
  new CustomEvent("progress", {
137
301
  detail: {
138
302
  loaded: i,
@@ -144,79 +308,79 @@ function I(r, t) {
144
308
  return new Response(
145
309
  new ReadableStream({
146
310
  async start(i) {
147
- if (!r.body) {
311
+ if (!s.body) {
148
312
  i.close();
149
313
  return;
150
314
  }
151
- const l = r.body.getReader();
315
+ const l = s.body.getReader();
152
316
  let o = 0;
153
317
  for (; ; )
154
318
  try {
155
- const { done: f, value: p } = await l.read();
156
- if (p && (o += p.byteLength), f) {
319
+ const { done: c, value: g } = await l.read();
320
+ if (g && (o += g.byteLength), c) {
157
321
  n(o, o), i.close();
158
322
  break;
159
323
  } else
160
- n(o, s), i.enqueue(p);
161
- } catch (f) {
162
- console.error({ e: f }), i.error(f);
324
+ n(o, r), i.enqueue(g);
325
+ } catch (c) {
326
+ R.error({ e: c }), i.error(c);
163
327
  break;
164
328
  }
165
329
  }
166
330
  }),
167
331
  {
168
- status: r.status,
169
- statusText: r.statusText,
170
- headers: r.headers
332
+ status: s.status,
333
+ statusText: s.statusText,
334
+ headers: s.headers
171
335
  }
172
336
  );
173
337
  }
174
- var d, g, y, L;
175
- class F extends EventTarget {
338
+ var d, p, m, L;
339
+ class J extends EventTarget {
176
340
  constructor() {
177
341
  super(...arguments);
178
- u(this, y);
342
+ u(this, m);
179
343
  u(this, d, void 0);
180
- u(this, g, void 0);
181
- b(this, d, {}), b(this, g, 0), this.progress = 0, this.mode = "REAL_TIME", this.caption = "";
344
+ u(this, p, void 0);
345
+ y(this, d, {}), y(this, p, 0), this.progress = 0, this.mode = "REAL_TIME", this.caption = "";
182
346
  }
183
- partialObserver(e, s = "") {
184
- const n = ++E(this, g)._;
347
+ partialObserver(t, r = "") {
348
+ const n = ++E(this, p)._;
185
349
  return a(this, d)[n] = 0, (i) => {
186
350
  const { loaded: l, total: o } = (i == null ? void 0 : i.detail) || {};
187
- a(this, d)[n] = l / o * e, _(this, y, L).call(this, this.totalProgress, "REAL_TIME", s);
351
+ a(this, d)[n] = l / o * t, b(this, m, L).call(this, this.totalProgress, "REAL_TIME", r);
188
352
  };
189
353
  }
190
- slowlyIncrementBy(e) {
191
- const s = ++E(this, g)._;
192
- a(this, d)[s] = e, _(this, y, L).call(this, this.totalProgress, "SLOWLY_INCREMENT");
354
+ slowlyIncrementBy(t) {
355
+ const r = ++E(this, p)._;
356
+ a(this, d)[r] = t, b(this, m, L).call(this, this.totalProgress, "SLOWLY_INCREMENT");
193
357
  }
194
358
  get totalProgress() {
195
359
  return Object.values(a(this, d)).reduce(
196
- (e, s) => e + s,
360
+ (t, r) => t + r,
197
361
  0
198
362
  );
199
363
  }
200
364
  }
201
- d = new WeakMap(), g = new WeakMap(), y = new WeakSet(), L = function(e, s, n) {
365
+ d = new WeakMap(), p = new WeakMap(), m = new WeakSet(), L = function(t, r, n) {
202
366
  this.dispatchEvent(
203
367
  new CustomEvent("progress", {
204
368
  detail: {
205
- progress: e,
206
- mode: s,
369
+ progress: t,
370
+ mode: r,
207
371
  caption: n
208
372
  }
209
373
  })
210
374
  );
211
375
  };
212
- const w = 1e-5;
213
- class B extends EventTarget {
376
+ const _ = 1e-5;
377
+ class k extends EventTarget {
214
378
  constructor({
215
- weight: t = 1,
216
- caption: e = "",
217
- fillTime: s = 4
379
+ weight: e = 1,
380
+ caption: t = "",
381
+ fillTime: r = 4
218
382
  } = {}) {
219
- super(), this._selfWeight = 1, this._selfDone = !1, this._selfProgress = 0, this._selfCaption = "", this._isFilling = !1, this._subTrackers = [], this._weight = t, this._selfCaption = e, this._fillTime = s;
383
+ super(), this._selfWeight = 1, this._selfDone = !1, this._selfProgress = 0, this._selfCaption = "", this._isFilling = !1, this._subTrackers = [], this._weight = e, this._selfCaption = t, this._fillTime = r;
220
384
  }
221
385
  /**
222
386
  * Creates a new sub-tracker with a specific weight.
@@ -253,20 +417,20 @@ class B extends EventTarget {
253
417
  * subTracker1.set(100);
254
418
  * ```
255
419
  */
256
- stage(t, e = "") {
257
- if (t || (t = this._selfWeight), this._selfWeight - t < -w)
420
+ stage(e, t = "") {
421
+ if (e || (e = this._selfWeight), this._selfWeight - e < -_)
258
422
  throw new Error(
259
- `Cannot add a stage with weight ${t} as the total weight of registered stages would exceed 1.`
423
+ `Cannot add a stage with weight ${e} as the total weight of registered stages would exceed 1.`
260
424
  );
261
- this._selfWeight -= t;
262
- const s = new B({
263
- caption: e,
264
- weight: t,
425
+ this._selfWeight -= e;
426
+ const r = new k({
427
+ caption: t,
428
+ weight: e,
265
429
  fillTime: this._fillTime
266
430
  });
267
- return this._subTrackers.push(s), s.addEventListener("progress", () => this.notifyProgress()), s.addEventListener("done", () => {
431
+ return this._subTrackers.push(r), r.addEventListener("progress", () => this.notifyProgress()), r.addEventListener("done", () => {
268
432
  this.done && this.notifyDone();
269
- }), s;
433
+ }), r;
270
434
  }
271
435
  /**
272
436
  * Fills the progress bar slowly over time, simulating progress.
@@ -286,87 +450,87 @@ class B extends EventTarget {
286
450
  *
287
451
  * @param options Optional options.
288
452
  */
289
- fillSlowly({ stopBeforeFinishing: t = !0 } = {}) {
453
+ fillSlowly({ stopBeforeFinishing: e = !0 } = {}) {
290
454
  if (this._isFilling)
291
455
  return;
292
456
  this._isFilling = !0;
293
- const e = 100, s = this._fillTime / e;
457
+ const t = 100, r = this._fillTime / t;
294
458
  this._fillInterval = setInterval(() => {
295
- this.set(this._selfProgress + 1), t && this._selfProgress >= 99 && clearInterval(this._fillInterval);
296
- }, s);
459
+ this.set(this._selfProgress + 1), e && this._selfProgress >= 99 && clearInterval(this._fillInterval);
460
+ }, r);
297
461
  }
298
- set(t) {
299
- this._selfProgress = Math.min(t, 100), this.notifyProgress(), this._selfProgress + w >= 100 && this.finish();
462
+ set(e) {
463
+ this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress + _ >= 100 && this.finish();
300
464
  }
301
465
  finish() {
302
466
  this._fillInterval && clearInterval(this._fillInterval), this._selfDone = !0, this._selfProgress = 100, this._isFilling = !1, this._fillInterval = void 0, this.notifyProgress(), this.notifyDone();
303
467
  }
304
468
  get caption() {
305
- for (let t = this._subTrackers.length - 1; t >= 0; t--)
306
- if (!this._subTrackers[t].done) {
307
- const e = this._subTrackers[t].caption;
308
- if (e)
309
- return e;
469
+ for (let e = this._subTrackers.length - 1; e >= 0; e--)
470
+ if (!this._subTrackers[e].done) {
471
+ const t = this._subTrackers[e].caption;
472
+ if (t)
473
+ return t;
310
474
  }
311
475
  return this._selfCaption;
312
476
  }
313
- setCaption(t) {
314
- this._selfCaption = t, this.notifyProgress();
477
+ setCaption(e) {
478
+ this._selfCaption = e, this.notifyProgress();
315
479
  }
316
480
  get done() {
317
- return this.progress + w >= 100;
481
+ return this.progress + _ >= 100;
318
482
  }
319
483
  get progress() {
320
484
  if (this._selfDone)
321
485
  return 100;
322
- const t = this._subTrackers.reduce(
323
- (e, s) => e + s.progress * s.weight,
486
+ const e = this._subTrackers.reduce(
487
+ (t, r) => t + r.progress * r.weight,
324
488
  this._selfProgress * this._selfWeight
325
489
  );
326
- return Math.round(t * 1e4) / 1e4;
490
+ return Math.round(e * 1e4) / 1e4;
327
491
  }
328
492
  get weight() {
329
493
  return this._weight;
330
494
  }
331
495
  get observer() {
332
- return this._progressObserver || (this._progressObserver = (t) => {
333
- this.set(t);
496
+ return this._progressObserver || (this._progressObserver = (e) => {
497
+ this.set(e);
334
498
  }), this._progressObserver;
335
499
  }
336
500
  get loadingListener() {
337
- return this._loadingListener || (this._loadingListener = (t) => {
338
- this.set(t.detail.loaded / t.detail.total * 100);
501
+ return this._loadingListener || (this._loadingListener = (e) => {
502
+ this.set(e.detail.loaded / e.detail.total * 100);
339
503
  }), this._loadingListener;
340
504
  }
341
- pipe(t) {
342
- t.setProgress({
505
+ pipe(e) {
506
+ e.setProgress({
343
507
  progress: this.progress,
344
508
  caption: this.caption
345
- }), this.addEventListener("progress", (e) => {
346
- t.setProgress({
347
- progress: e.detail.progress,
348
- caption: e.detail.caption
509
+ }), this.addEventListener("progress", (t) => {
510
+ e.setProgress({
511
+ progress: t.detail.progress,
512
+ caption: t.detail.caption
349
513
  });
350
514
  }), this.addEventListener("done", () => {
351
- t.setLoaded();
515
+ e.setLoaded();
352
516
  });
353
517
  }
354
- addEventListener(t, e) {
355
- super.addEventListener(t, e);
518
+ addEventListener(e, t) {
519
+ super.addEventListener(e, t);
356
520
  }
357
- removeEventListener(t, e) {
358
- super.removeEventListener(t, e);
521
+ removeEventListener(e, t) {
522
+ super.removeEventListener(e, t);
359
523
  }
360
524
  notifyProgress() {
361
- const t = this;
525
+ const e = this;
362
526
  this.dispatchEvent(
363
527
  new CustomEvent("progress", {
364
528
  detail: {
365
529
  get progress() {
366
- return t.progress;
530
+ return e.progress;
367
531
  },
368
532
  get caption() {
369
- return t.caption;
533
+ return e.caption;
370
534
  }
371
535
  }
372
536
  })
@@ -377,8 +541,8 @@ class B extends EventTarget {
377
541
  }
378
542
  }
379
543
  export {
380
- x as EmscriptenDownloadMonitor,
381
- F as ProgressObserver,
382
- B as ProgressTracker,
383
- I as cloneResponseMonitorProgress
544
+ W as EmscriptenDownloadMonitor,
545
+ J as ProgressObserver,
546
+ k as ProgressTracker,
547
+ A as cloneResponseMonitorProgress
384
548
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/progress",
3
- "version": "0.7.0",
3
+ "version": "0.7.3",
4
4
  "description": "PHP.wasm – loading progress monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,12 +29,13 @@
29
29
  "type": "module",
30
30
  "main": "index.js",
31
31
  "types": "index.d.ts",
32
- "gitHead": "c5eba3d709f2821c4303521e8c81b962e3bcca23",
32
+ "gitHead": "e8fedf92bbead5cec963c8d6976e7143d8e68721",
33
33
  "engines": {
34
34
  "node": ">=18.18.0",
35
35
  "npm": ">=8.11.0"
36
36
  },
37
37
  "dependencies": {
38
- "@php-wasm/node-polyfills": "0.7.0"
38
+ "@php-wasm/node-polyfills": "0.7.3",
39
+ "@php-wasm/logger": "0.7.3"
39
40
  }
40
41
  }