@php-wasm/progress 0.1.28 → 0.1.31
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.
- package/index.cjs +1 -1
- package/index.js +41 -39
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=5*1024*1024;class p extends EventTarget{#e={};#t={};constructor(e=[]){super(),this.setModules(e),this.#s()}getEmscriptenOptions(){return{dataFileDownloads:this.#i()}}setModules(e){this.#e=e.reduce((t,s)=>{if(s.dependenciesTotalSize>0){const r="http://example.com/",n=new URL(s.dependencyFilename,r).pathname.split("/").pop();t[n]=Math.max(n in t?t[n]:0,s.dependenciesTotalSize)}return t},{}),this.#t=Object.fromEntries(Object.entries(this.#e).map(([t])=>[t,0]))}#s(){const e=WebAssembly.instantiateStreaming;WebAssembly.instantiateStreaming=async(t,...s)=>{const r=await t,i=r.url.substring(new URL(r.url).origin.length+1),n=u(r,({detail:{loaded:a,total:l}})=>this.#r(i,a,l));return e(n,...s)}}#i(){const e=this,t={};return new Proxy(t,{set(s,r,i){return e.#r(r,i.loaded,i.total),s[r]=new Proxy(JSON.parse(JSON.stringify(i)),{set(n,a,l){return n[a]=l,e.#r(r,n.loaded,n.total),!0}}),!0}})}#r(e,t,s){const r=new URL(e,"http://example.com").pathname.split("/").pop();s||(s=this.#e[r]),r in this.#t||console.warn(`Registered a download #progress of an unregistered file "${r}". This may cause a sudden **decrease** in the #progress percentage as the total number of bytes increases during the download.`),this.#t[e]=t,this.dispatchEvent(new CustomEvent("progress",{detail:{loaded:c(this.#t),total:c(this.#e)}}))}}function c(o){return Object.values(o).reduce((e,t)=>e+t,0)}function u(o,e){const t=o.headers.get("content-length")||"",s=parseInt(t,10)||f;function r(i,n){e(new CustomEvent("progress",{detail:{loaded:i,total:n}}))}return new Response(new ReadableStream({async start(i){if(!o.body){i.close();return}const n=o.body.getReader();let a=0;for(;;)try{const{done:l,value:h}=await n.read();if(h&&(a+=h.byteLength),l){r(a,a),i.close();break}else r(a,s),i.enqueue(h)}catch(l){console.error({e:l}),i.error(l);break}}}),{status:o.status,statusText:o.statusText,headers:o.headers})}class _ extends EventTarget{constructor(){super(...arguments),this.#e={},this.#t=0,this.progress=0,this.mode="REAL_TIME",this.caption=""}#e;#t;partialObserver(e,t=""){const s=++this.#t;return this.#e[s]=0,r=>{const{loaded:i,total:n}=r?.detail||{};this.#e[s]=i/n*e,this.#s(this.totalProgress,"REAL_TIME",t)}}slowlyIncrementBy(e){const t=++this.#t;this.#e[t]=e,this.#s(this.totalProgress,"SLOWLY_INCREMENT")}get totalProgress(){return Object.values(this.#e).reduce((e,t)=>e+t,0)}#s(e,t,s){this.dispatchEvent(new CustomEvent("progress",{detail:{progress:e,mode:t,caption:s}}))}}const d=1e-5;class g extends EventTarget{constructor({weight:e=1,caption:t="",fillTime:s=4}={}){super(),this._selfWeight=1,this._selfProgress=0,this._selfCaption="",this._isFilling=!1,this._subTrackers=[],this._weight=e,this._selfCaption=t,this._fillTime=s}stage(e,t=""){if(e||(e=this._selfWeight),this._selfWeight-e<-d)throw new Error(`Cannot add a stage with weight ${e} as the total weight of registered stages would exceed 1.`);this._selfWeight-=e;const s=new g({caption:t,weight:e,fillTime:this._fillTime});return this._subTrackers.push(s),s.addEventListener("progress",()=>this.notifyProgress()),s.addEventListener("done",()=>{this.done&&this.notifyDone()}),s}fillSlowly({stopBeforeFinishing:e=!0}={}){if(this._isFilling)return;this._isFilling=!0;const t=100,s=this._fillTime/t;this._fillInterval=setInterval(()=>{this.set(this._selfProgress+1),e&&this._selfProgress>=99&&clearInterval(this._fillInterval)},s)}set(e){this._selfProgress=Math.min(e,100),this.notifyProgress(),this._selfProgress+d>=100&&this.finish()}finish(){this._fillInterval&&clearInterval(this._fillInterval),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+d>=100}get progress(){const e=this._subTrackers.reduce((t,s)=>t+s.progress*s.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){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=p;exports.ProgressObserver=_;exports.ProgressTracker=g;exports.cloneResponseMonitorProgress=u;
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class f extends EventTarget {
|
|
2
2
|
#e = {};
|
|
3
3
|
#t = {};
|
|
4
4
|
constructor(e = []) {
|
|
@@ -6,13 +6,13 @@ class c extends EventTarget {
|
|
|
6
6
|
}
|
|
7
7
|
getEmscriptenOptions() {
|
|
8
8
|
return {
|
|
9
|
-
dataFileDownloads: this.#
|
|
9
|
+
dataFileDownloads: this.#r()
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
12
|
setModules(e) {
|
|
13
13
|
this.#e = e.reduce((t, s) => {
|
|
14
14
|
if (s.dependenciesTotalSize > 0) {
|
|
15
|
-
const
|
|
15
|
+
const i = "http://example.com/", n = new URL(s.dependencyFilename, i).pathname.split("/").pop();
|
|
16
16
|
t[n] = Math.max(
|
|
17
17
|
n in t ? t[n] : 0,
|
|
18
18
|
s.dependenciesTotalSize
|
|
@@ -30,11 +30,11 @@ class c extends EventTarget {
|
|
|
30
30
|
#s() {
|
|
31
31
|
const e = WebAssembly.instantiateStreaming;
|
|
32
32
|
WebAssembly.instantiateStreaming = async (t, ...s) => {
|
|
33
|
-
const
|
|
34
|
-
new URL(
|
|
33
|
+
const i = await t, r = i.url.substring(
|
|
34
|
+
new URL(i.url).origin.length + 1
|
|
35
35
|
), n = u(
|
|
36
|
-
|
|
37
|
-
({ detail: { loaded: a, total: l } }) => this.#r
|
|
36
|
+
i,
|
|
37
|
+
({ detail: { loaded: a, total: l } }) => this.#i(r, a, l)
|
|
38
38
|
);
|
|
39
39
|
return e(n, ...s);
|
|
40
40
|
};
|
|
@@ -44,13 +44,13 @@ class c extends EventTarget {
|
|
|
44
44
|
* to `startPHP` to monitor the download #progress of the data
|
|
45
45
|
* dependencies.
|
|
46
46
|
*/
|
|
47
|
-
#
|
|
47
|
+
#r() {
|
|
48
48
|
const e = this, t = {};
|
|
49
49
|
return new Proxy(t, {
|
|
50
|
-
set(s,
|
|
51
|
-
return e.#
|
|
50
|
+
set(s, i, r) {
|
|
51
|
+
return e.#i(i, r.loaded, r.total), s[i] = new Proxy(JSON.parse(JSON.stringify(r)), {
|
|
52
52
|
set(n, a, l) {
|
|
53
|
-
return n[a] = l, e.#
|
|
53
|
+
return n[a] = l, e.#i(i, n.loaded, n.total), !0;
|
|
54
54
|
}
|
|
55
55
|
}), !0;
|
|
56
56
|
}
|
|
@@ -63,30 +63,30 @@ class c extends EventTarget {
|
|
|
63
63
|
* @param loaded The number of bytes of that file loaded so far.
|
|
64
64
|
* @param fileSize The total number of bytes in the loaded file.
|
|
65
65
|
*/
|
|
66
|
-
#
|
|
67
|
-
const
|
|
68
|
-
s || (s = this.#e[
|
|
69
|
-
`Registered a download #progress of an unregistered file "${
|
|
66
|
+
#i(e, t, s) {
|
|
67
|
+
const i = new URL(e, "http://example.com").pathname.split("/").pop();
|
|
68
|
+
s || (s = this.#e[i]), i in this.#t || console.warn(
|
|
69
|
+
`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.`
|
|
70
70
|
), this.#t[e] = t, this.dispatchEvent(
|
|
71
71
|
new CustomEvent("progress", {
|
|
72
72
|
detail: {
|
|
73
|
-
loaded:
|
|
74
|
-
total:
|
|
73
|
+
loaded: g(this.#t),
|
|
74
|
+
total: g(this.#e)
|
|
75
75
|
}
|
|
76
76
|
})
|
|
77
77
|
);
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
|
-
function
|
|
80
|
+
function g(o) {
|
|
81
81
|
return Object.values(o).reduce((e, t) => e + t, 0);
|
|
82
82
|
}
|
|
83
83
|
function u(o, e) {
|
|
84
84
|
const t = o.headers.get("content-length") || "", s = parseInt(t, 10) || 5242880;
|
|
85
|
-
function r
|
|
85
|
+
function i(r, n) {
|
|
86
86
|
e(
|
|
87
87
|
new CustomEvent("progress", {
|
|
88
88
|
detail: {
|
|
89
|
-
loaded:
|
|
89
|
+
loaded: r,
|
|
90
90
|
total: n
|
|
91
91
|
}
|
|
92
92
|
})
|
|
@@ -94,9 +94,9 @@ function u(o, e) {
|
|
|
94
94
|
}
|
|
95
95
|
return new Response(
|
|
96
96
|
new ReadableStream({
|
|
97
|
-
async start(
|
|
97
|
+
async start(r) {
|
|
98
98
|
if (!o.body) {
|
|
99
|
-
|
|
99
|
+
r.close();
|
|
100
100
|
return;
|
|
101
101
|
}
|
|
102
102
|
const n = o.body.getReader();
|
|
@@ -105,12 +105,12 @@ function u(o, e) {
|
|
|
105
105
|
try {
|
|
106
106
|
const { done: l, value: h } = await n.read();
|
|
107
107
|
if (h && (a += h.byteLength), l) {
|
|
108
|
-
|
|
108
|
+
i(a, a), r.close();
|
|
109
109
|
break;
|
|
110
110
|
} else
|
|
111
|
-
|
|
111
|
+
i(a, s), r.enqueue(h);
|
|
112
112
|
} catch (l) {
|
|
113
|
-
console.error({ e: l }),
|
|
113
|
+
console.error({ e: l }), r.error(l);
|
|
114
114
|
break;
|
|
115
115
|
}
|
|
116
116
|
}
|
|
@@ -122,7 +122,7 @@ function u(o, e) {
|
|
|
122
122
|
}
|
|
123
123
|
);
|
|
124
124
|
}
|
|
125
|
-
class
|
|
125
|
+
class p extends EventTarget {
|
|
126
126
|
constructor() {
|
|
127
127
|
super(...arguments), this.#e = {}, this.#t = 0, this.progress = 0, this.mode = "REAL_TIME", this.caption = "";
|
|
128
128
|
}
|
|
@@ -130,9 +130,9 @@ class f extends EventTarget {
|
|
|
130
130
|
#t;
|
|
131
131
|
partialObserver(e, t = "") {
|
|
132
132
|
const s = ++this.#t;
|
|
133
|
-
return this.#e[s] = 0, (
|
|
134
|
-
const { loaded:
|
|
135
|
-
this.#e[s] =
|
|
133
|
+
return this.#e[s] = 0, (i) => {
|
|
134
|
+
const { loaded: r, total: n } = i?.detail || {};
|
|
135
|
+
this.#e[s] = r / n * e, this.#s(this.totalProgress, "REAL_TIME", t);
|
|
136
136
|
};
|
|
137
137
|
}
|
|
138
138
|
slowlyIncrementBy(e) {
|
|
@@ -157,7 +157,8 @@ class f extends EventTarget {
|
|
|
157
157
|
);
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
|
|
160
|
+
const d = 1e-5;
|
|
161
|
+
class c extends EventTarget {
|
|
161
162
|
constructor({
|
|
162
163
|
weight: e = 1,
|
|
163
164
|
caption: t = "",
|
|
@@ -201,12 +202,12 @@ class g extends EventTarget {
|
|
|
201
202
|
* ```
|
|
202
203
|
*/
|
|
203
204
|
stage(e, t = "") {
|
|
204
|
-
if (e || (e = this._selfWeight), this._selfWeight - e < -
|
|
205
|
+
if (e || (e = this._selfWeight), this._selfWeight - e < -d)
|
|
205
206
|
throw new Error(
|
|
206
207
|
`Cannot add a stage with weight ${e} as the total weight of registered stages would exceed 1.`
|
|
207
208
|
);
|
|
208
209
|
this._selfWeight -= e;
|
|
209
|
-
const s = new
|
|
210
|
+
const s = new c({
|
|
210
211
|
caption: t,
|
|
211
212
|
weight: e,
|
|
212
213
|
fillTime: this._fillTime
|
|
@@ -243,7 +244,7 @@ class g extends EventTarget {
|
|
|
243
244
|
}, s);
|
|
244
245
|
}
|
|
245
246
|
set(e) {
|
|
246
|
-
this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress +
|
|
247
|
+
this._selfProgress = Math.min(e, 100), this.notifyProgress(), this._selfProgress + d >= 100 && this.finish();
|
|
247
248
|
}
|
|
248
249
|
finish() {
|
|
249
250
|
this._fillInterval && clearInterval(this._fillInterval), this._selfProgress = 100, this._isFilling = !1, this._fillInterval = void 0, this.notifyProgress(), this.notifyDone();
|
|
@@ -261,13 +262,14 @@ class g extends EventTarget {
|
|
|
261
262
|
this._selfCaption = e, this.notifyProgress();
|
|
262
263
|
}
|
|
263
264
|
get done() {
|
|
264
|
-
return this.progress +
|
|
265
|
+
return this.progress + d >= 100;
|
|
265
266
|
}
|
|
266
267
|
get progress() {
|
|
267
|
-
|
|
268
|
-
(
|
|
268
|
+
const e = this._subTrackers.reduce(
|
|
269
|
+
(t, s) => t + s.progress * s.weight,
|
|
269
270
|
this._selfProgress * this._selfWeight
|
|
270
271
|
);
|
|
272
|
+
return Math.round(e * 1e4) / 1e4;
|
|
271
273
|
}
|
|
272
274
|
get weight() {
|
|
273
275
|
return this._weight;
|
|
@@ -318,8 +320,8 @@ class g extends EventTarget {
|
|
|
318
320
|
}
|
|
319
321
|
}
|
|
320
322
|
export {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
323
|
+
f as EmscriptenDownloadMonitor,
|
|
324
|
+
p as ProgressObserver,
|
|
325
|
+
c as ProgressTracker,
|
|
324
326
|
u as cloneResponseMonitorProgress
|
|
325
327
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/progress",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.31",
|
|
4
4
|
"description": "PHP.wasm – loading progress monitoring",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
"type": "module",
|
|
30
30
|
"main": "index.js",
|
|
31
31
|
"types": "index.d.ts",
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "04b86f80526a6fa630226ba2416163ffeed6b43d",
|
|
33
33
|
"dependencies": {}
|
|
34
34
|
}
|