unzipit 1.4.3 → 2.0.1

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/README.md CHANGED
@@ -4,18 +4,20 @@
4
4
 
5
5
  Random access unzip library for browser and node based JavaScript
6
6
 
7
- [![Build Status](https://travis-ci.org/greggman/unzipit.svg?branch=master)](https://travis-ci.org/greggman/unzipit)
7
+ [![Build and Deploy](https://github.com/greggman/unzipit/actions/workflows/build_and_deploy.yml/badge.svg)](https://github.com/greggman/unzipit/actions/workflows/build_and_deploy.yml)
8
8
  [[Live Tests](https://greggman.github.io/unzipit/test/)]
9
9
 
10
- * Less than 8k gzipped without workers, Less than 13k with.
10
+ * Less than 5k gzipped without workers, Less than 6k with.
11
11
  * [6x to 25x faster than JSZip](https://jsperf.com/jszip-vs-unzipit/4) without workers and even faster with
12
12
  * Uses far less memory.
13
13
  * Can [avoid downloading the entire zip file](#Streaming) if the server supports http range requests.
14
14
 
15
15
  # How to use
16
16
 
17
- Live Example: [https://jsfiddle.net/greggman/awez4sd7/](https://jsfiddle.net/greggman/awez4sd7/)
18
- Live Parallel Example: [https://jsfiddle.net/greggman/cgdjm07f/](https://jsfiddle.net/greggman/cgdjm07f/)
17
+ * Live Example: [https://jsfiddle.net/greggman/awez4sd7/](https://jsfiddle.net/greggman/awez4sd7/)
18
+ * Live Parallel Example: [https://jsfiddle.net/greggman/cgdjm07f/](https://jsfiddle.net/greggman/cgdjm07f/)
19
+ * Live User File Example: [https://jsfiddle.net/greggman/s2qe7m6b/](https://jsfiddle.net/greggman/s2qe7m6b/)
20
+ * Live User Drag and Drop Example: [https://jsfiddle.net/greggman/oxnhpmL8/](https://jsfiddle.net/greggman/oxnhpmL8/)
19
21
 
20
22
  ## without workers
21
23
 
@@ -277,6 +279,7 @@ entries.forEach(entry => {
277
279
  Some libraries both zip and unzip.
278
280
  IMO those should be separate libraries as there is little if any code to share between
279
281
  both. Plenty of projects only need to do one or the other.
282
+ [Here's the sister library for zip](https://greggman.github.io/zipup/).
280
283
 
281
284
  Similarly inflate and deflate libraries should be separate from zip, unzip libraries.
282
285
  You need one or the other not both. See zlib as an example.
@@ -334,6 +337,9 @@ manages them. They don't count as part of the JavaScript heap.
334
337
  In node, the examples with the file readers will only read the header and whatever entries' contents
335
338
  you ask for so similarly you can avoid having everything in memory except the things you read.
336
339
 
340
+ # Zip Creation
341
+
342
+ see: [zipup](https://greggman.github.io/zipup/)
337
343
 
338
344
  # API
339
345
 
@@ -448,6 +454,10 @@ to exit since it will wait for the workers to exit.
448
454
 
449
455
  Use a transpiler like [Babel](https://babeljs.io).
450
456
 
457
+ As of version 2 the library uses `DecompressionStream` which is built into node
458
+ since v18 and [in browsers for a while](https://caniuse.com/mdn-api_decompressionstream). For older
459
+ browsers there is [a polyfill](https://github.com/101arrowz/compression-streams-polyfill).
460
+
451
461
  ## Caching
452
462
 
453
463
  If you ask for the same entry twice it will be read twice and decompressed twice.
@@ -599,7 +609,6 @@ Follow the instructions on testing but add `?timeout=0` to the URL as in `http:
599
609
  # Acknowledgements
600
610
 
601
611
  * The code is **heavily** based on [yauzl](https://github.com/thejoshwolfe/yauzl)
602
- * The code uses the es6 module version of [uzip.js](https://www.npmjs.com/package/uzip-module)
603
612
 
604
613
  # Licence
605
614
 
@@ -0,0 +1,7 @@
1
+ import type { Reader } from './BlobReader.js';
2
+ export default class ArrayBufferReader implements Reader {
3
+ private typedArray;
4
+ constructor(arrayBufferOrView: ArrayBuffer | SharedArrayBuffer | ArrayBufferView);
5
+ getLength(): Promise<number>;
6
+ read(offset: number, length: number): Promise<Uint8Array<ArrayBuffer>>;
7
+ }
@@ -0,0 +1,12 @@
1
+ export interface Reader {
2
+ getLength(): Promise<number>;
3
+ read(offset: number, size: number): Promise<Uint8Array<ArrayBuffer>>;
4
+ sliceAsBlob?(offset: number, length: number, type?: string): Promise<Blob>;
5
+ }
6
+ export default class BlobReader implements Reader {
7
+ private blob;
8
+ constructor(blob: Blob);
9
+ getLength(): Promise<number>;
10
+ read(offset: number, length: number): Promise<Uint8Array<ArrayBuffer>>;
11
+ sliceAsBlob(offset: number, length: number, type?: string): Promise<Blob>;
12
+ }
@@ -0,0 +1,8 @@
1
+ import type { Reader } from './BlobReader.js';
2
+ export declare class HTTPRangeReader implements Reader {
3
+ private url;
4
+ private length;
5
+ constructor(url: string);
6
+ getLength(): Promise<number>;
7
+ read(offset: number, size: number): Promise<Uint8Array<ArrayBuffer>>;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export interface UnzipitOptions {
2
+ useWorkers?: boolean;
3
+ workerURL?: string;
4
+ numWorkers?: number;
5
+ }
6
+ export declare function setOptions(options: UnzipitOptions): void;
7
+ export declare function inflateRawAsync(src: Uint8Array<ArrayBuffer> | Blob, uncompressedSize: number, type?: string): Promise<ArrayBuffer | Blob>;
8
+ export declare function cleanup(): Promise<void>;
@@ -1,329 +1,41 @@
1
- /* unzipit@1.4.3, license MIT */
1
+ /* unzipit@2.0.1, license MIT */
2
2
  (function (factory) {
3
3
  typeof define === 'function' && define.amd ? define(factory) :
4
4
  factory();
5
- }((function () { 'use strict';
6
-
7
- function inflate(data, buf) {
8
- var u8=Uint8Array;
9
- if(data[0]==3 && data[1]==0) return (buf ? buf : new u8(0));
10
- var bitsF = _bitsF, bitsE = _bitsE, decodeTiny = _decodeTiny, get17 = _get17;
11
-
12
- var noBuf = (buf==null);
13
- if(noBuf) buf = new u8((data.length>>>2)<<3);
14
-
15
- var BFINAL=0, BTYPE=0, HLIT=0, HDIST=0, HCLEN=0, ML=0, MD=0;
16
- var off = 0, pos = 0;
17
- var lmap, dmap;
18
-
19
- while(BFINAL==0) {
20
- BFINAL = bitsF(data, pos , 1);
21
- BTYPE = bitsF(data, pos+1, 2); pos+=3;
22
- //console.log(BFINAL, BTYPE);
23
-
24
- if(BTYPE==0) {
25
- if((pos&7)!=0) pos+=8-(pos&7);
26
- var p8 = (pos>>>3)+4, len = data[p8-4]|(data[p8-3]<<8); //console.log(len);//bitsF(data, pos, 16),
27
- if(noBuf) buf=_check(buf, off+len);
28
- buf.set(new u8(data.buffer, data.byteOffset+p8, len), off);
29
- //for(var i=0; i<len; i++) buf[off+i] = data[p8+i];
30
- //for(var i=0; i<len; i++) if(buf[off+i] != data[p8+i]) throw "e";
31
- pos = ((p8+len)<<3); off+=len; continue;
32
- }
33
- if(noBuf) buf=_check(buf, off+(1<<17)); // really not enough in many cases (but PNG and ZIP provide buffer in advance)
34
- if(BTYPE==1) { lmap = U.flmap; dmap = U.fdmap; ML = (1<<9)-1; MD = (1<<5)-1; }
35
- if(BTYPE==2) {
36
- HLIT = bitsE(data, pos , 5)+257;
37
- HDIST = bitsE(data, pos+ 5, 5)+ 1;
38
- HCLEN = bitsE(data, pos+10, 4)+ 4; pos+=14;
39
- for(var i=0; i<38; i+=2) { U.itree[i]=0; U.itree[i+1]=0; }
40
- var tl = 1;
41
- for(var i=0; i<HCLEN; i++) { var l=bitsE(data, pos+i*3, 3); U.itree[(U.ordr[i]<<1)+1] = l; if(l>tl)tl=l; } pos+=3*HCLEN; //console.log(itree);
42
- makeCodes(U.itree, tl);
43
- codes2map(U.itree, tl, U.imap);
44
-
45
- lmap = U.lmap; dmap = U.dmap;
46
-
47
- pos = decodeTiny(U.imap, (1<<tl)-1, HLIT+HDIST, data, pos, U.ttree);
48
- var mx0 = _copyOut(U.ttree, 0, HLIT , U.ltree); ML = (1<<mx0)-1;
49
- var mx1 = _copyOut(U.ttree, HLIT, HDIST, U.dtree); MD = (1<<mx1)-1;
50
-
51
- //var ml = decodeTiny(U.imap, (1<<tl)-1, HLIT , data, pos, U.ltree); ML = (1<<(ml>>>24))-1; pos+=(ml&0xffffff);
52
- makeCodes(U.ltree, mx0);
53
- codes2map(U.ltree, mx0, lmap);
54
-
55
- //var md = decodeTiny(U.imap, (1<<tl)-1, HDIST, data, pos, U.dtree); MD = (1<<(md>>>24))-1; pos+=(md&0xffffff);
56
- makeCodes(U.dtree, mx1);
57
- codes2map(U.dtree, mx1, dmap);
58
- }
59
- //var ooff=off, opos=pos;
60
- while(true) {
61
- var code = lmap[get17(data, pos) & ML]; pos += code&15;
62
- var lit = code>>>4; //U.lhst[lit]++;
63
- if((lit>>>8)==0) { buf[off++] = lit; }
64
- else if(lit==256) { break; }
65
- else {
66
- var end = off+lit-254;
67
- if(lit>264) { var ebs = U.ldef[lit-257]; end = off + (ebs>>>3) + bitsE(data, pos, ebs&7); pos += ebs&7; }
68
- //dst[end-off]++;
69
-
70
- var dcode = dmap[get17(data, pos) & MD]; pos += dcode&15;
71
- var dlit = dcode>>>4;
72
- var dbs = U.ddef[dlit], dst = (dbs>>>4) + bitsF(data, pos, dbs&15); pos += dbs&15;
73
-
74
- //var o0 = off-dst, stp = Math.min(end-off, dst);
75
- //if(stp>20) while(off<end) { buf.copyWithin(off, o0, o0+stp); off+=stp; } else
76
- //if(end-dst<=off) buf.copyWithin(off, off-dst, end-dst); else
77
- //if(dst==1) buf.fill(buf[off-1], off, end); else
78
- if(noBuf) buf=_check(buf, off+(1<<17));
79
- while(off<end) { buf[off]=buf[off++-dst]; buf[off]=buf[off++-dst]; buf[off]=buf[off++-dst]; buf[off]=buf[off++-dst]; }
80
- off=end;
81
- //while(off!=end) { buf[off]=buf[off++-dst]; }
82
- }
83
- }
84
- //console.log(off-ooff, (pos-opos)>>>3);
85
- }
86
- //console.log(dst);
87
- //console.log(tlen, dlen, off-tlen+tcnt);
88
- return buf.length==off ? buf : buf.slice(0,off);
89
- }
90
- function _check(buf, len) {
91
- var bl=buf.length; if(len<=bl) return buf;
92
- var nbuf = new Uint8Array(Math.max(bl<<1,len)); nbuf.set(buf,0);
93
- //for(var i=0; i<bl; i+=4) { nbuf[i]=buf[i]; nbuf[i+1]=buf[i+1]; nbuf[i+2]=buf[i+2]; nbuf[i+3]=buf[i+3]; }
94
- return nbuf;
95
- }
96
-
97
- function _decodeTiny(lmap, LL, len, data, pos, tree) {
98
- var bitsE = _bitsE, get17 = _get17;
99
- var i = 0;
100
- while(i<len) {
101
- var code = lmap[get17(data, pos)&LL]; pos+=code&15;
102
- var lit = code>>>4;
103
- if(lit<=15) { tree[i]=lit; i++; }
104
- else {
105
- var ll = 0, n = 0;
106
- if(lit==16) {
107
- n = (3 + bitsE(data, pos, 2)); pos += 2; ll = tree[i-1];
108
- }
109
- else if(lit==17) {
110
- n = (3 + bitsE(data, pos, 3)); pos += 3;
111
- }
112
- else if(lit==18) {
113
- n = (11 + bitsE(data, pos, 7)); pos += 7;
114
- }
115
- var ni = i+n;
116
- while(i<ni) { tree[i]=ll; i++; }
117
- }
118
- }
119
- return pos;
120
- }
121
- function _copyOut(src, off, len, tree) {
122
- var mx=0, i=0, tl=tree.length>>>1;
123
- while(i<len) { var v=src[i+off]; tree[(i<<1)]=0; tree[(i<<1)+1]=v; if(v>mx)mx=v; i++; }
124
- while(i<tl ) { tree[(i<<1)]=0; tree[(i<<1)+1]=0; i++; }
125
- return mx;
126
- }
127
-
128
- function makeCodes(tree, MAX_BITS) { // code, length
129
- var max_code = tree.length;
130
- var code, bits, n, i, len;
131
-
132
- var bl_count = U.bl_count; for(var i=0; i<=MAX_BITS; i++) bl_count[i]=0;
133
- for(i=1; i<max_code; i+=2) bl_count[tree[i]]++;
134
-
135
- var next_code = U.next_code; // smallest code for each length
136
-
137
- code = 0;
138
- bl_count[0] = 0;
139
- for (bits = 1; bits <= MAX_BITS; bits++) {
140
- code = (code + bl_count[bits-1]) << 1;
141
- next_code[bits] = code;
142
- }
143
-
144
- for (n = 0; n < max_code; n+=2) {
145
- len = tree[n+1];
146
- if (len != 0) {
147
- tree[n] = next_code[len];
148
- next_code[len]++;
149
- }
150
- }
151
- }
152
- function codes2map(tree, MAX_BITS, map) {
153
- var max_code = tree.length;
154
- var r15 = U.rev15;
155
- for(var i=0; i<max_code; i+=2) if(tree[i+1]!=0) {
156
- var lit = i>>1;
157
- var cl = tree[i+1], val = (lit<<4)|cl; // : (0x8000 | (U.of0[lit-257]<<7) | (U.exb[lit-257]<<4) | cl);
158
- var rest = (MAX_BITS-cl), i0 = tree[i]<<rest, i1 = i0 + (1<<rest);
159
- //tree[i]=r15[i0]>>>(15-MAX_BITS);
160
- while(i0!=i1) {
161
- var p0 = r15[i0]>>>(15-MAX_BITS);
162
- map[p0]=val; i0++;
163
- }
164
- }
165
- }
166
- function revCodes(tree, MAX_BITS) {
167
- var r15 = U.rev15, imb = 15-MAX_BITS;
168
- for(var i=0; i<tree.length; i+=2) { var i0 = (tree[i]<<(MAX_BITS-tree[i+1])); tree[i] = r15[i0]>>>imb; }
169
- }
170
-
171
- function _bitsE(dt, pos, length) { return ((dt[pos>>>3] | (dt[(pos>>>3)+1]<<8) )>>>(pos&7))&((1<<length)-1); }
172
- function _bitsF(dt, pos, length) { return ((dt[pos>>>3] | (dt[(pos>>>3)+1]<<8) | (dt[(pos>>>3)+2]<<16))>>>(pos&7))&((1<<length)-1); }
173
- /*
174
- function _get9(dt, pos) {
175
- return ((dt[pos>>>3] | (dt[(pos>>>3)+1]<<8))>>>(pos&7))&511;
176
- } */
177
- function _get17(dt, pos) { // return at least 17 meaningful bytes
178
- return (dt[pos>>>3] | (dt[(pos>>>3)+1]<<8) | (dt[(pos>>>3)+2]<<16) )>>>(pos&7);
179
- }
180
- const U = function(){
181
- var u16=Uint16Array, u32=Uint32Array;
182
- return {
183
- next_code : new u16(16),
184
- bl_count : new u16(16),
185
- ordr : [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ],
186
- of0 : [3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,999,999,999],
187
- exb : [0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0, 0],
188
- ldef : new u16(32),
189
- df0 : [1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 65535, 65535],
190
- dxb : [0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0],
191
- ddef : new u32(32),
192
- flmap: new u16( 512), fltree: [],
193
- fdmap: new u16( 32), fdtree: [],
194
- lmap : new u16(32768), ltree : [], ttree:[],
195
- dmap : new u16(32768), dtree : [],
196
- imap : new u16( 512), itree : [],
197
- //rev9 : new u16( 512)
198
- rev15: new u16(1<<15),
199
- lhst : new u32(286), dhst : new u32( 30), ihst : new u32(19),
200
- lits : new u32(15000),
201
- strt : new u16(1<<16),
202
- prev : new u16(1<<15)
203
- };
204
- } ();
205
-
206
- (function(){
207
- var len = 1<<15;
208
- for(var i=0; i<len; i++) {
209
- var x = i;
210
- x = (((x & 0xaaaaaaaa) >>> 1) | ((x & 0x55555555) << 1));
211
- x = (((x & 0xcccccccc) >>> 2) | ((x & 0x33333333) << 2));
212
- x = (((x & 0xf0f0f0f0) >>> 4) | ((x & 0x0f0f0f0f) << 4));
213
- x = (((x & 0xff00ff00) >>> 8) | ((x & 0x00ff00ff) << 8));
214
- U.rev15[i] = (((x >>> 16) | (x << 16)))>>>17;
215
- }
216
-
217
- function pushV(tgt, n, sv) { while(n--!=0) tgt.push(0,sv); }
218
-
219
- for(var i=0; i<32; i++) { U.ldef[i]=(U.of0[i]<<3)|U.exb[i]; U.ddef[i]=(U.df0[i]<<4)|U.dxb[i]; }
220
-
221
- pushV(U.fltree, 144, 8); pushV(U.fltree, 255-143, 9); pushV(U.fltree, 279-255, 7); pushV(U.fltree,287-279,8);
222
- /*
223
- var i = 0;
224
- for(; i<=143; i++) U.fltree.push(0,8);
225
- for(; i<=255; i++) U.fltree.push(0,9);
226
- for(; i<=279; i++) U.fltree.push(0,7);
227
- for(; i<=287; i++) U.fltree.push(0,8);
228
- */
229
- makeCodes(U.fltree, 9);
230
- codes2map(U.fltree, 9, U.flmap);
231
- revCodes (U.fltree, 9);
232
-
233
- pushV(U.fdtree,32,5);
234
- //for(i=0;i<32; i++) U.fdtree.push(0,5);
235
- makeCodes(U.fdtree, 5);
236
- codes2map(U.fdtree, 5, U.fdmap);
237
- revCodes (U.fdtree, 5);
238
-
239
- pushV(U.itree,19,0); pushV(U.ltree,286,0); pushV(U.dtree,30,0); pushV(U.ttree,320,0);
240
- /*
241
- for(var i=0; i< 19; i++) U.itree.push(0,0);
242
- for(var i=0; i<286; i++) U.ltree.push(0,0);
243
- for(var i=0; i< 30; i++) U.dtree.push(0,0);
244
- for(var i=0; i<320; i++) U.ttree.push(0,0);
245
- */
246
- })();
247
-
248
- const crc = {
249
- table : ( function() {
250
- var tab = new Uint32Array(256);
251
- for (var n=0; n<256; n++) {
252
- var c = n;
253
- for (var k=0; k<8; k++) {
254
- if (c & 1) c = 0xedb88320 ^ (c >>> 1);
255
- else c = c >>> 1;
256
- }
257
- tab[n] = c; }
258
- return tab; })(),
259
- update : function(c, buf, off, len) {
260
- for (var i=0; i<len; i++) c = crc.table[(c ^ buf[off+i]) & 0xff] ^ (c >>> 8);
261
- return c;
262
- },
263
- crc : function(b,o,l) { return crc.update(0xffffffff,b,o,l) ^ 0xffffffff; }
264
- };
265
-
266
- function inflateRaw(file, buf) { return inflate(file, buf); }
267
-
268
- /* global SharedArrayBuffer, process */
5
+ })((function () { 'use strict';
269
6
 
7
+ var _a, _b;
270
8
  function readBlobAsArrayBuffer(blob) {
271
- if (blob.arrayBuffer) {
272
- return blob.arrayBuffer();
273
- }
274
- return new Promise((resolve, reject) => {
275
- const reader = new FileReader();
276
- reader.addEventListener('loadend', () => {
277
- resolve(reader.result);
9
+ if (blob.arrayBuffer) {
10
+ return blob.arrayBuffer();
11
+ }
12
+ return new Promise((resolve, reject) => {
13
+ const reader = new FileReader();
14
+ reader.addEventListener('loadend', () => {
15
+ resolve(reader.result);
16
+ });
17
+ reader.addEventListener('error', reject);
18
+ reader.readAsArrayBuffer(blob);
278
19
  });
279
- reader.addEventListener('error', reject);
280
- reader.readAsArrayBuffer(blob);
281
- });
282
20
  }
283
-
284
21
  async function readBlobAsUint8Array(blob) {
285
- const arrayBuffer = await readBlobAsArrayBuffer(blob);
286
- return new Uint8Array(arrayBuffer);
22
+ const arrayBuffer = await readBlobAsArrayBuffer(blob);
23
+ return new Uint8Array(arrayBuffer);
287
24
  }
288
-
289
25
  function isBlob(v) {
290
- return typeof Blob !== 'undefined' && v instanceof Blob;
26
+ return typeof Blob !== 'undefined' && v instanceof Blob;
291
27
  }
28
+ const isNode = (typeof process !== 'undefined') &&
29
+ !!(process === null || process === void 0 ? void 0 : process.versions) &&
30
+ (typeof ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node) !== 'undefined') &&
31
+ (typeof ((_b = process === null || process === void 0 ? void 0 : process.versions) === null || _b === void 0 ? void 0 : _b.electron) === 'undefined');
292
32
 
293
- const isNode =
294
- (typeof process !== 'undefined') &&
295
- process.versions &&
296
- (typeof process.versions.node !== 'undefined') &&
297
- (typeof process.versions.electron === 'undefined');
298
-
299
- /* global require */
300
-
33
+ /* global DecompressionStream */
301
34
  // note: we only handle the inflate portion in a worker
302
35
  // every other part is already async and JavaScript
303
36
  // is non blocking. I suppose if you had a million entry
304
37
  // zip file then the loop going through the directory
305
38
  // might take time but that's an unlikely situation.
306
-
307
- const msgHelper = (function() {
308
- if (isNode) {
309
- const { parentPort } = require('worker_threads');
310
-
311
- return {
312
- postMessage: parentPort.postMessage.bind(parentPort),
313
- addEventListener: parentPort.on.bind(parentPort),
314
- };
315
- } else {
316
- return {
317
- postMessage: self.postMessage.bind(self),
318
- addEventListener(type, fn) {
319
- self.addEventListener(type, (e) => {
320
- fn(e.data);
321
- });
322
- },
323
- };
324
- }
325
- }());
326
-
327
39
  // class InflateRequest {
328
40
  // id: string,
329
41
  // src: ArrayBuffer, SharedArrayBuffer, blob
@@ -335,55 +47,80 @@
335
47
  // then 50 blobs will be asked to be read at once.
336
48
  // If feels like that should happen at a higher level (user code)
337
49
  // or a lower level (the browser)?
338
- async function inflate$1(req) {
339
- const {id, src, uncompressedSize, type} = req;
340
- try {
341
- let srcData;
342
- if (isBlob(src)) {
343
- srcData = await readBlobAsUint8Array(src);
344
- } else {
345
- srcData = new Uint8Array(src);
50
+ async function decompressRaw(src) {
51
+ const ds = new DecompressionStream('deflate-raw');
52
+ const writer = ds.writable.getWriter();
53
+ writer.write(src).then(() => writer.close()).catch(() => { });
54
+ const chunks = [];
55
+ const reader = ds.readable.getReader();
56
+ for (;;) {
57
+ const { done, value } = await reader.read();
58
+ if (done) {
59
+ break;
60
+ }
61
+ chunks.push(value);
62
+ }
63
+ const size = chunks.reduce((s, c) => s + c.byteLength, 0);
64
+ const result = new Uint8Array(size);
65
+ let offset = 0;
66
+ for (const chunk of chunks) {
67
+ result.set(chunk, offset);
68
+ offset += chunk.byteLength;
69
+ }
70
+ return result;
71
+ }
72
+ async function inflate(req, postMessage) {
73
+ const { id, src, type } = req;
74
+ try {
75
+ const srcData = isBlob(src)
76
+ ? await readBlobAsUint8Array(src)
77
+ : new Uint8Array(src);
78
+ const dstData = await decompressRaw(srcData);
79
+ const transferables = [];
80
+ let data;
81
+ if (type) {
82
+ data = new Blob([dstData], { type });
83
+ }
84
+ else {
85
+ data = dstData.buffer;
86
+ transferables.push(data);
87
+ }
88
+ postMessage({ id, data }, transferables);
346
89
  }
347
- const dstData = new Uint8Array(uncompressedSize);
348
- inflateRaw(srcData, dstData);
349
- const transferables = [];
350
- let data;
351
- if (type) {
352
- data = new Blob([dstData], {type});
353
- } else {
354
- data = dstData.buffer;
355
- transferables.push(data);
90
+ catch (e) {
91
+ console.error(e);
92
+ postMessage({ id, error: `${e}` });
356
93
  }
357
- msgHelper.postMessage({
358
- id,
359
- data,
360
- }, transferables);
361
- } catch (e) {
362
- console.error(e);
363
- msgHelper.postMessage({
364
- id,
365
- error: `${e.toString()}`,
94
+ }
95
+ function handleMessage(msg, postMessage) {
96
+ const { type, data } = msg;
97
+ if (type === 'inflate') {
98
+ inflate(data, postMessage);
99
+ }
100
+ else {
101
+ throw new Error('no handler for type: ' + type);
102
+ }
103
+ }
104
+ if (isNode) {
105
+ // Use dynamic import so this works in both CJS and ESM contexts.
106
+ // The import of a built-in resolves before any messages can arrive.
107
+ const moduleId = 'node:worker_threads';
108
+ import(moduleId).then(({ parentPort }) => {
109
+ parentPort.on('message', (msg) => {
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
+ handleMessage(msg, (m, t) => parentPort.postMessage(m, t));
112
+ });
366
113
  });
367
- }
368
114
  }
369
-
370
- const handlers = {
371
- inflate: inflate$1,
372
- };
373
-
374
- msgHelper.addEventListener('message', function(e) {
375
- const {type, data} = e;
376
- const fn = handlers[type];
377
- if (!fn) {
378
- throw new Error('no handler for type: ' + type);
379
- }
380
- fn(data);
381
- });
382
-
383
- if (!isNode) {
384
- // needed for firefox AFAICT as there so no other
385
- // way to know a worker loaded successfully.?
386
- msgHelper.postMessage('start');
115
+ else {
116
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
+ const workerSelf = self;
118
+ workerSelf.addEventListener('message', (e) => {
119
+ handleMessage(e.data, (m, t) => workerSelf.postMessage(m, t));
120
+ });
121
+ // needed for firefox AFAICT as there is no other
122
+ // way to know a worker loaded successfully.
123
+ workerSelf.postMessage('start');
387
124
  }
388
125
 
389
- })));
126
+ }));
@@ -1,12 +1 @@
1
- /* unzipit@1.4.3, license MIT */
2
- 'use strict';(function(w){"function"===typeof define&&define.amd?define(w):w()})(function(){function w(a,b){var e=a.length;if(b<=e)return a;b=new Uint8Array(Math.max(e<<1,b));b.set(a,0);return b}function X(a,b,e,g){for(var f=0,c=0,m=g.length>>>1;c<e;){var p=a[c+b];g[c<<1]=0;g[(c<<1)+1]=p;p>f&&(f=p);c++}for(;c<m;)g[c<<1]=0,g[(c<<1)+1]=0,c++;return f}function z(a,b){var e=a.length,g,f;var c=d.bl_count;for(f=0;f<=b;f++)c[f]=0;for(f=1;f<e;f+=2)c[a[f]]++;f=d.next_code;var m=0;c[0]=0;for(g=1;g<=b;g++)m=
3
- m+c[g-1]<<1,f[g]=m;for(b=0;b<e;b+=2)c=a[b+1],0!=c&&(a[b]=f[c],f[c]++)}function A(a,b,e){for(var g=a.length,f=d.rev15,c=0;c<g;c+=2)if(0!=a[c+1]){var m=a[c+1],p=c>>1<<4|m,t=b-m;m=a[c]<<t;for(t=m+(1<<t);m!=t;)e[f[m]>>>15-b]=p,m++}}function Y(a,b){for(var e=d.rev15,g=15-b,f=0;f<a.length;f+=2)a[f]=e[a[f]<<b-a[f+1]]>>>g}function x(a,b,e){return(a[b>>>3]|a[(b>>>3)+1]<<8)>>>(b&7)&(1<<e)-1}function K(a,b,e){return(a[b>>>3]|a[(b>>>3)+1]<<8|a[(b>>>3)+2]<<16)>>>(b&7)&(1<<e)-1}function L(a,b){return(a[b>>>3]|
4
- a[(b>>>3)+1]<<8|a[(b>>>3)+2]<<16)>>>(b&7)}function fa(a){return a.arrayBuffer?a.arrayBuffer():new Promise((b,e)=>{const g=new FileReader;g.addEventListener("loadend",()=>{b(g.result)});g.addEventListener("error",e);g.readAsArrayBuffer(a)})}async function ha(a){a=await fa(a);return new Uint8Array(a)}const d=function(){var a=Uint16Array,b=Uint32Array;return{next_code:new a(16),bl_count:new a(16),ordr:[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15],of0:[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,
5
- 51,59,67,83,99,115,131,163,195,227,258,999,999,999],exb:[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0],ldef:new a(32),df0:[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,65535,65535],dxb:[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0],ddef:new b(32),flmap:new a(512),fltree:[],fdmap:new a(32),fdtree:[],lmap:new a(32768),ltree:[],ttree:[],dmap:new a(32768),dtree:[],imap:new a(512),itree:[],
6
- rev15:new a(32768),lhst:new b(286),dhst:new b(30),ihst:new b(19),lits:new b(15E3),strt:new a(65536),prev:new a(32768)}}();(function(){function a(g,f,c){for(;0!=f--;)g.push(0,c)}for(var b=0;32768>b;b++){var e=b;e=(e&2863311530)>>>1|(e&1431655765)<<1;e=(e&3435973836)>>>2|(e&858993459)<<2;e=(e&4042322160)>>>4|(e&252645135)<<4;e=(e&4278255360)>>>8|(e&16711935)<<8;d.rev15[b]=(e>>>16|e<<16)>>>17}for(b=0;32>b;b++)d.ldef[b]=d.of0[b]<<3|d.exb[b],d.ddef[b]=d.df0[b]<<4|d.dxb[b];a(d.fltree,144,8);a(d.fltree,
7
- 112,9);a(d.fltree,24,7);a(d.fltree,8,8);z(d.fltree,9);A(d.fltree,9,d.flmap);Y(d.fltree,9);a(d.fdtree,32,5);z(d.fdtree,5);A(d.fdtree,5,d.fdmap);Y(d.fdtree,5);a(d.itree,19,0);a(d.ltree,286,0);a(d.dtree,30,0);a(d.ttree,320,0)})();const Z={table:function(){for(var a=new Uint32Array(256),b=0;256>b;b++){for(var e=b,g=0;8>g;g++)e=e&1?3988292384^e>>>1:e>>>1;a[b]=e}return a}(),update:function(a,b,e,g){for(var f=0;f<g;f++)a=Z.table[(a^b[e+f])&255]^a>>>8;return a},crc:function(a,b,e){return Z.update(4294967295,
8
- a,b,e)^4294967295}},aa="undefined"!==typeof process&&process.versions&&"undefined"!==typeof process.versions.node&&"undefined"===typeof process.versions.electron,B=function(){if(aa){const {parentPort:a}=require("worker_threads");return{postMessage:a.postMessage.bind(a),addEventListener:a.on.bind(a)}}return{postMessage:self.postMessage.bind(self),addEventListener(a,b){self.addEventListener(a,e=>{b(e.data)})}}}(),ka={inflate:async function(a){const {id:b,src:e,uncompressedSize:g,type:f}=a;try{var c=
9
- "undefined"!==typeof Blob&&e instanceof Blob?await ha(e):new Uint8Array(e);const y=new Uint8Array(g);a=c;c=y;var m=Uint8Array;if(3==a[0]&&0==a[1])c||new m(0);else{var p=null==c;p&&(c=new m(a.length>>>2<<3));for(var t=0,u,C,M,N,O=0,P=0,l=0,h=0,D,E;0==t;)if(t=K(a,h,1),u=K(a,h+1,2),h+=3,0==u){0!=(h&7)&&(h+=8-(h&7));var k=(h>>>3)+4,F=a[k-4]|a[k-3]<<8;p&&(c=w(c,l+F));c.set(new m(a.buffer,a.byteOffset+k,F),l);h=k+F<<3;l+=F}else{p&&(c=w(c,l+131072));1==u&&(D=d.flmap,E=d.fdmap,O=511,P=31);if(2==u){C=x(a,
10
- h,5)+257;M=x(a,h+5,5)+1;N=x(a,h+10,4)+4;h+=14;for(k=0;38>k;k+=2)d.itree[k]=0,d.itree[k+1]=0;var n=1;for(k=0;k<N;k++){var Q=x(a,h+3*k,3);d.itree[(d.ordr[k]<<1)+1]=Q;Q>n&&(n=Q)}h+=3*N;z(d.itree,n);A(d.itree,n,d.imap);D=d.lmap;E=d.dmap;var ia=d.imap;k=(1<<n)-1;n=C+M;for(var G=a,q=h,R=d.ttree,S=x,ja=L,r=0;r<n;){var ba=ia[ja(G,q)&k];q+=ba&15;var v=ba>>>4;if(15>=v)R[r]=v,r++;else{var ca=0,H=0;16==v?(H=3+S(G,q,2),q+=2,ca=R[r-1]):17==v?(H=3+S(G,q,3),q+=3):18==v&&(H=11+S(G,q,7),q+=7);for(v=r+H;r<v;)R[r]=ca,
11
- r++}}h=q;var T=X(d.ttree,0,C,d.ltree);O=(1<<T)-1;var U=X(d.ttree,C,M,d.dtree);P=(1<<U)-1;z(d.ltree,T);A(d.ltree,T,D);z(d.dtree,U);A(d.dtree,U,E)}for(;;){var da=D[L(a,h)&O];h+=da&15;k=da>>>4;if(0==k>>>8)c[l++]=k;else if(256==k)break;else{n=l+k-254;if(264<k){var V=d.ldef[k-257];n=l+(V>>>3)+x(a,h,V&7);h+=V&7}var ea=E[L(a,h)&P];h+=ea&15;var W=d.ddef[ea>>>4],I=(W>>>4)+K(a,h,W&15);h+=W&15;for(p&&(c=w(c,l+131072));l<n;)c[l]=c[l++-I],c[l]=c[l++-I],c[l]=c[l++-I],c[l]=c[l++-I];l=n}}}c.length==l||c.slice(0,
12
- l)}u=[];let J;f?J=new Blob([y],{type:f}):(J=y.buffer,u.push(J));B.postMessage({id:b,data:J},u)}catch(y){console.error(y),B.postMessage({id:b,error:`${y.toString()}`})}}};B.addEventListener("message",function(a){const {type:b,data:e}=a;a=ka[b];if(!a)throw Error("no handler for type: "+b);a(e)});aa||B.postMessage("start")});
1
+ !function(e){"function"==typeof define&&define.amd?define(e):e()}(function(){"use strict";var e,r;async function n(e){const r=await function(e){return e.arrayBuffer?e.arrayBuffer():new Promise((r,n)=>{const t=new FileReader;t.addEventListener("loadend",()=>{r(t.result)}),t.addEventListener("error",n),t.readAsArrayBuffer(e)})}(e);return new Uint8Array(r)}async function t(e,r){const{id:t,src:o,type:s}=e;try{const e=(a=o,"undefined"!=typeof Blob&&a instanceof Blob?await n(o):new Uint8Array(o)),i=await async function(e){const r=new DecompressionStream("deflate-raw"),n=r.writable.getWriter();n.write(e).then(()=>n.close()).catch(()=>{});const t=[],o=r.readable.getReader();for(;;){const{done:e,value:r}=await o.read();if(e)break;t.push(r)}const s=t.reduce((e,r)=>e+r.byteLength,0),a=new Uint8Array(s);let i=0;for(const e of t)a.set(e,i),i+=e.byteLength;return a}(e),d=[];let c;s?c=new Blob([i],{type:s}):(c=i.buffer,d.push(c)),r({id:t,data:c},d)}catch(e){console.error(e),r({id:t,error:`${e}`})}var a}function o(e,r){const{type:n,data:o}=e;if("inflate"!==n)throw new Error("no handler for type: "+n);t(o,r)}if("undefined"!=typeof process&&!!(null===process||void 0===process?void 0:process.versions)&&void 0!==(null===(e=null===process||void 0===process?void 0:process.versions)||void 0===e?void 0:e.node)&&void 0===(null===(r=null===process||void 0===process?void 0:process.versions)||void 0===r?void 0:r.electron)){import("node:worker_threads").then(({parentPort:e})=>{e.on("message",r=>{o(r,(r,n)=>e.postMessage(r,n))})})}else{const e=self;e.addEventListener("message",r=>{o(r.data,(r,n)=>e.postMessage(r,n))}),e.postMessage("start")}});