@php-wasm/progress 0.6.16 → 0.7.0
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 +57 -74
- package/lib/emscripten-download-monitor.d.ts +0 -1
- package/package.json +3 -3
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
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;
|
package/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
var
|
|
1
|
+
var v = (r, t, e) => {
|
|
2
2
|
if (!t.has(r))
|
|
3
3
|
throw TypeError("Cannot " + e);
|
|
4
4
|
};
|
|
5
|
-
var a = (r, t, e) => (
|
|
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
6
|
if (t.has(r))
|
|
7
7
|
throw TypeError("Cannot add the same private member more than once");
|
|
8
8
|
t instanceof WeakSet ? t.add(r) : t.set(r, e);
|
|
9
|
-
},
|
|
10
|
-
var
|
|
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) => ({
|
|
11
11
|
set _(n) {
|
|
12
|
-
|
|
12
|
+
b(r, t, n, e);
|
|
13
13
|
},
|
|
14
14
|
get _() {
|
|
15
15
|
return a(r, t, s);
|
|
16
16
|
}
|
|
17
|
-
}),
|
|
18
|
-
const
|
|
17
|
+
}), _ = (r, t, e) => (v(r, t, "access private method"), e);
|
|
18
|
+
const R = function() {
|
|
19
19
|
var r;
|
|
20
20
|
return typeof process < "u" && ((r = process.release) == null ? void 0 : r.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : (
|
|
21
21
|
// @ts-ignore
|
|
@@ -23,7 +23,7 @@ const C = function() {
|
|
|
23
23
|
self instanceof WorkerGlobalScope ? "WORKER" : "NODE"
|
|
24
24
|
);
|
|
25
25
|
}();
|
|
26
|
-
if (
|
|
26
|
+
if (R === "NODE") {
|
|
27
27
|
let r = function(e) {
|
|
28
28
|
return new Promise(function(s, n) {
|
|
29
29
|
e.onload = e.onerror = function(i) {
|
|
@@ -66,15 +66,15 @@ if (C === "NODE") {
|
|
|
66
66
|
const i = n.byobRequest.view, o = await s.slice(
|
|
67
67
|
e,
|
|
68
68
|
e + i.byteLength
|
|
69
|
-
).arrayBuffer(),
|
|
70
|
-
new Uint8Array(i.buffer).set(
|
|
71
|
-
const
|
|
72
|
-
n.byobRequest.respond(
|
|
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();
|
|
73
73
|
}
|
|
74
74
|
});
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
|
-
if (
|
|
77
|
+
if (R === "NODE" && typeof CustomEvent > "u") {
|
|
78
78
|
class r extends Event {
|
|
79
79
|
constructor(e, s = {}) {
|
|
80
80
|
super(e, s), this.detail = s.detail;
|
|
@@ -84,16 +84,11 @@ if (C === "NODE" && typeof CustomEvent > "u") {
|
|
|
84
84
|
}
|
|
85
85
|
globalThis.CustomEvent = r;
|
|
86
86
|
}
|
|
87
|
-
const
|
|
88
|
-
var
|
|
89
|
-
class
|
|
87
|
+
const C = 5 * 1024 * 1024;
|
|
88
|
+
var c, h, m, T;
|
|
89
|
+
class x extends EventTarget {
|
|
90
90
|
constructor() {
|
|
91
|
-
super();
|
|
92
|
-
/**
|
|
93
|
-
* Replaces the default WebAssembly.instantiateStreaming with a version
|
|
94
|
-
* that monitors the download #progress.
|
|
95
|
-
*/
|
|
96
|
-
f(this, v);
|
|
91
|
+
super(...arguments);
|
|
97
92
|
/**
|
|
98
93
|
* Notifies about the download #progress of a file.
|
|
99
94
|
*
|
|
@@ -101,53 +96,41 @@ class A extends EventTarget {
|
|
|
101
96
|
* @param loaded The number of bytes of that file loaded so far.
|
|
102
97
|
* @param fileSize The total number of bytes in the loaded file.
|
|
103
98
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
p(this, v, I).call(this);
|
|
99
|
+
u(this, m);
|
|
100
|
+
u(this, c, {});
|
|
101
|
+
u(this, h, {});
|
|
108
102
|
}
|
|
109
103
|
expectAssets(e) {
|
|
110
104
|
for (const [s, n] of Object.entries(e)) {
|
|
111
105
|
const i = "http://example.com/", o = new URL(s, i).pathname.split("/").pop();
|
|
112
|
-
o in a(this,
|
|
106
|
+
o in a(this, c) || (a(this, c)[o] = n), o in a(this, h) || (a(this, h)[o] = 0);
|
|
113
107
|
}
|
|
114
108
|
}
|
|
115
109
|
async monitorFetch(e) {
|
|
116
110
|
const s = await e;
|
|
117
|
-
return
|
|
118
|
-
|
|
111
|
+
return I(s, (i) => {
|
|
112
|
+
_(this, m, T).call(this, s.url, i.detail.loaded, i.detail.total);
|
|
119
113
|
});
|
|
120
114
|
}
|
|
121
115
|
}
|
|
122
|
-
|
|
123
|
-
const e = WebAssembly.instantiateStreaming;
|
|
124
|
-
WebAssembly.instantiateStreaming = async (s, ...n) => {
|
|
125
|
-
const i = await s, l = i.url.substring(
|
|
126
|
-
new URL(i.url).origin.length + 1
|
|
127
|
-
), o = B(
|
|
128
|
-
i,
|
|
129
|
-
({ detail: { loaded: h, total: d } }) => p(this, b, P).call(this, l, h, d)
|
|
130
|
-
);
|
|
131
|
-
return e(o, ...n);
|
|
132
|
-
};
|
|
133
|
-
}, b = new WeakSet(), P = function(e, s, n) {
|
|
116
|
+
c = new WeakMap(), h = new WeakMap(), m = new WeakSet(), T = function(e, s, n) {
|
|
134
117
|
const i = new URL(e, "http://example.com").pathname.split("/").pop();
|
|
135
|
-
n || (n = a(this,
|
|
118
|
+
n || (n = a(this, c)[i]), i in a(this, h) || console.warn(
|
|
136
119
|
`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.`
|
|
137
|
-
), a(this,
|
|
120
|
+
), a(this, h)[i] = s, this.dispatchEvent(
|
|
138
121
|
new CustomEvent("progress", {
|
|
139
122
|
detail: {
|
|
140
|
-
loaded:
|
|
141
|
-
total:
|
|
123
|
+
loaded: P(a(this, h)),
|
|
124
|
+
total: P(a(this, c))
|
|
142
125
|
}
|
|
143
126
|
})
|
|
144
127
|
);
|
|
145
128
|
};
|
|
146
|
-
function
|
|
129
|
+
function P(r) {
|
|
147
130
|
return Object.values(r).reduce((t, e) => t + e, 0);
|
|
148
131
|
}
|
|
149
|
-
function
|
|
150
|
-
const e = r.headers.get("content-length") || "", s = parseInt(e, 10) ||
|
|
132
|
+
function I(r, t) {
|
|
133
|
+
const e = r.headers.get("content-length") || "", s = parseInt(e, 10) || C;
|
|
151
134
|
function n(i, l) {
|
|
152
135
|
t(
|
|
153
136
|
new CustomEvent("progress", {
|
|
@@ -169,14 +152,14 @@ function B(r, t) {
|
|
|
169
152
|
let o = 0;
|
|
170
153
|
for (; ; )
|
|
171
154
|
try {
|
|
172
|
-
const { done:
|
|
173
|
-
if (
|
|
155
|
+
const { done: f, value: p } = await l.read();
|
|
156
|
+
if (p && (o += p.byteLength), f) {
|
|
174
157
|
n(o, o), i.close();
|
|
175
158
|
break;
|
|
176
159
|
} else
|
|
177
|
-
n(o, s), i.enqueue(
|
|
178
|
-
} catch (
|
|
179
|
-
console.error({ e:
|
|
160
|
+
n(o, s), i.enqueue(p);
|
|
161
|
+
} catch (f) {
|
|
162
|
+
console.error({ e: f }), i.error(f);
|
|
180
163
|
break;
|
|
181
164
|
}
|
|
182
165
|
}
|
|
@@ -188,34 +171,34 @@ function B(r, t) {
|
|
|
188
171
|
}
|
|
189
172
|
);
|
|
190
173
|
}
|
|
191
|
-
var
|
|
174
|
+
var d, g, y, L;
|
|
192
175
|
class F extends EventTarget {
|
|
193
176
|
constructor() {
|
|
194
177
|
super(...arguments);
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
178
|
+
u(this, y);
|
|
179
|
+
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 = "";
|
|
199
182
|
}
|
|
200
183
|
partialObserver(e, s = "") {
|
|
201
|
-
const n = ++
|
|
202
|
-
return a(this,
|
|
184
|
+
const n = ++E(this, g)._;
|
|
185
|
+
return a(this, d)[n] = 0, (i) => {
|
|
203
186
|
const { loaded: l, total: o } = (i == null ? void 0 : i.detail) || {};
|
|
204
|
-
a(this,
|
|
187
|
+
a(this, d)[n] = l / o * e, _(this, y, L).call(this, this.totalProgress, "REAL_TIME", s);
|
|
205
188
|
};
|
|
206
189
|
}
|
|
207
190
|
slowlyIncrementBy(e) {
|
|
208
|
-
const s = ++
|
|
209
|
-
a(this,
|
|
191
|
+
const s = ++E(this, g)._;
|
|
192
|
+
a(this, d)[s] = e, _(this, y, L).call(this, this.totalProgress, "SLOWLY_INCREMENT");
|
|
210
193
|
}
|
|
211
194
|
get totalProgress() {
|
|
212
|
-
return Object.values(a(this,
|
|
195
|
+
return Object.values(a(this, d)).reduce(
|
|
213
196
|
(e, s) => e + s,
|
|
214
197
|
0
|
|
215
198
|
);
|
|
216
199
|
}
|
|
217
200
|
}
|
|
218
|
-
|
|
201
|
+
d = new WeakMap(), g = new WeakMap(), y = new WeakSet(), L = function(e, s, n) {
|
|
219
202
|
this.dispatchEvent(
|
|
220
203
|
new CustomEvent("progress", {
|
|
221
204
|
detail: {
|
|
@@ -226,8 +209,8 @@ c = new WeakMap(), y = new WeakMap(), m = new WeakSet(), R = function(e, s, n) {
|
|
|
226
209
|
})
|
|
227
210
|
);
|
|
228
211
|
};
|
|
229
|
-
const
|
|
230
|
-
class
|
|
212
|
+
const w = 1e-5;
|
|
213
|
+
class B extends EventTarget {
|
|
231
214
|
constructor({
|
|
232
215
|
weight: t = 1,
|
|
233
216
|
caption: e = "",
|
|
@@ -271,12 +254,12 @@ class O extends EventTarget {
|
|
|
271
254
|
* ```
|
|
272
255
|
*/
|
|
273
256
|
stage(t, e = "") {
|
|
274
|
-
if (t || (t = this._selfWeight), this._selfWeight - t < -
|
|
257
|
+
if (t || (t = this._selfWeight), this._selfWeight - t < -w)
|
|
275
258
|
throw new Error(
|
|
276
259
|
`Cannot add a stage with weight ${t} as the total weight of registered stages would exceed 1.`
|
|
277
260
|
);
|
|
278
261
|
this._selfWeight -= t;
|
|
279
|
-
const s = new
|
|
262
|
+
const s = new B({
|
|
280
263
|
caption: e,
|
|
281
264
|
weight: t,
|
|
282
265
|
fillTime: this._fillTime
|
|
@@ -313,7 +296,7 @@ class O extends EventTarget {
|
|
|
313
296
|
}, s);
|
|
314
297
|
}
|
|
315
298
|
set(t) {
|
|
316
|
-
this._selfProgress = Math.min(t, 100), this.notifyProgress(), this._selfProgress +
|
|
299
|
+
this._selfProgress = Math.min(t, 100), this.notifyProgress(), this._selfProgress + w >= 100 && this.finish();
|
|
317
300
|
}
|
|
318
301
|
finish() {
|
|
319
302
|
this._fillInterval && clearInterval(this._fillInterval), this._selfDone = !0, this._selfProgress = 100, this._isFilling = !1, this._fillInterval = void 0, this.notifyProgress(), this.notifyDone();
|
|
@@ -331,7 +314,7 @@ class O extends EventTarget {
|
|
|
331
314
|
this._selfCaption = t, this.notifyProgress();
|
|
332
315
|
}
|
|
333
316
|
get done() {
|
|
334
|
-
return this.progress +
|
|
317
|
+
return this.progress + w >= 100;
|
|
335
318
|
}
|
|
336
319
|
get progress() {
|
|
337
320
|
if (this._selfDone)
|
|
@@ -394,8 +377,8 @@ class O extends EventTarget {
|
|
|
394
377
|
}
|
|
395
378
|
}
|
|
396
379
|
export {
|
|
397
|
-
|
|
380
|
+
x as EmscriptenDownloadMonitor,
|
|
398
381
|
F as ProgressObserver,
|
|
399
|
-
|
|
400
|
-
|
|
382
|
+
B as ProgressTracker,
|
|
383
|
+
I as cloneResponseMonitorProgress
|
|
401
384
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/progress",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "PHP.wasm – loading progress monitoring",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"type": "module",
|
|
30
30
|
"main": "index.js",
|
|
31
31
|
"types": "index.d.ts",
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "c5eba3d709f2821c4303521e8c81b962e3bcca23",
|
|
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.
|
|
38
|
+
"@php-wasm/node-polyfills": "0.7.0"
|
|
39
39
|
}
|
|
40
40
|
}
|