@coralogix/browser 1.0.73 → 1.0.76

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 (47) hide show
  1. package/README.md +41 -0
  2. package/package.json +3 -2
  3. package/src/CoralogixExporter.d.ts +2 -0
  4. package/src/CoralogixExporter.js +33 -7
  5. package/src/CoralogixExporter.js.map +1 -1
  6. package/src/constants.d.ts +4 -0
  7. package/src/constants.js +4 -0
  8. package/src/constants.js.map +1 -1
  9. package/src/helpers.d.ts +7 -0
  10. package/src/helpers.js +16 -1
  11. package/src/helpers.js.map +1 -1
  12. package/src/index.js +26 -8
  13. package/src/index.js.map +1 -1
  14. package/src/instrumentations/CoralogixLongTaskInstrumentation.js +1 -1
  15. package/src/instrumentations/CoralogixLongTaskInstrumentation.js.map +1 -1
  16. package/src/instrumentations/CoralogixResourcesInstrumentation.js +1 -1
  17. package/src/instrumentations/CoralogixResourcesInstrumentation.js.map +1 -1
  18. package/src/instrumentations/CoralogixWebVitalsInstrumentation.js +8 -3
  19. package/src/instrumentations/CoralogixWebVitalsInstrumentation.js.map +1 -1
  20. package/src/label-providers/url-based-label-provider.d.ts +16 -0
  21. package/src/label-providers/url-based-label-provider.js +24 -0
  22. package/src/label-providers/url-based-label-provider.js.map +1 -0
  23. package/src/session/session.consts.d.ts +5 -0
  24. package/src/session/session.consts.js +8 -0
  25. package/src/session/session.consts.js.map +1 -0
  26. package/src/session/session.model.d.ts +74 -0
  27. package/src/session/session.model.js +11 -0
  28. package/src/session/session.model.js.map +1 -0
  29. package/src/session/sessionManager.d.ts +17 -0
  30. package/src/{sessionManager.js → session/sessionManager.js} +22 -4
  31. package/src/session/sessionManager.js.map +1 -0
  32. package/src/session/sessionRecorder.d.ts +32 -0
  33. package/src/session/sessionRecorder.js +178 -0
  34. package/src/session/sessionRecorder.js.map +1 -0
  35. package/src/types.d.ts +27 -2
  36. package/src/types.js +5 -0
  37. package/src/types.js.map +1 -1
  38. package/src/utils.d.ts +4 -0
  39. package/src/utils.js +28 -3
  40. package/src/utils.js.map +1 -1
  41. package/src/version.d.ts +1 -1
  42. package/src/version.js +1 -1
  43. package/src/workers/session-recording-worker.d.ts +1 -0
  44. package/src/workers/session-recording-worker.js +3729 -0
  45. package/src/workers/session-recording-worker.js.map +1 -0
  46. package/src/sessionManager.d.ts +0 -12
  47. package/src/sessionManager.js.map +0 -1
@@ -0,0 +1,3729 @@
1
+ // @ts-nocheck
2
+ export function getWorkerString() {
3
+ const MAX_CHUNK_SIZE = 1024 * 1024; //1MB
4
+ let coralogixGzipFunc;
5
+ /*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */
6
+ (function (global, factory) {
7
+ typeof exports === 'object' && typeof module !== 'undefined'
8
+ ? factory(exports)
9
+ : typeof define === 'function' && define.amd
10
+ ? define(['exports'], factory)
11
+ : ((global =
12
+ typeof globalThis !== 'undefined' ? globalThis : global || self),
13
+ factory((global.pako = {})));
14
+ })(this, function (exports) {
15
+ 'use strict';
16
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
17
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
18
+ //
19
+ // This software is provided 'as-is', without any express or implied
20
+ // warranty. In no event will the authors be held liable for any damages
21
+ // arising from the use of this software.
22
+ //
23
+ // Permission is granted to anyone to use this software for any purpose,
24
+ // including commercial applications, and to alter it and redistribute it
25
+ // freely, subject to the following restrictions:
26
+ //
27
+ // 1. The origin of this software must not be misrepresented; you must not
28
+ // claim that you wrote the original software. If you use this software
29
+ // in a product, an acknowledgment in the product documentation would be
30
+ // appreciated but is not required.
31
+ // 2. Altered source versions must be plainly marked as such, and must not be
32
+ // misrepresented as being the original software.
33
+ // 3. This notice may not be removed or altered from any source distribution.
34
+ /* eslint-disable space-unary-ops */
35
+ /* Public constants ==========================================================*/
36
+ /* ===========================================================================*/
37
+ //const Z_FILTERED = 1;
38
+ //const Z_HUFFMAN_ONLY = 2;
39
+ //const Z_RLE = 3;
40
+ const Z_FIXED$1 = 4;
41
+ //const Z_DEFAULT_STRATEGY = 0;
42
+ /* Possible values of the data_type field (though see inflate()) */
43
+ const Z_BINARY = 0;
44
+ const Z_TEXT = 1;
45
+ //const Z_ASCII = 1; // = Z_TEXT
46
+ const Z_UNKNOWN$1 = 2;
47
+ /*============================================================================*/
48
+ function zero$1(buf) {
49
+ let len = buf.length;
50
+ while (--len >= 0) {
51
+ buf[len] = 0;
52
+ }
53
+ }
54
+ // From zutil.h
55
+ const STORED_BLOCK = 0;
56
+ const STATIC_TREES = 1;
57
+ const DYN_TREES = 2;
58
+ /* The three kinds of block type */
59
+ const MIN_MATCH$1 = 3;
60
+ const MAX_MATCH$1 = 258;
61
+ /* The minimum and maximum match lengths */
62
+ // From deflate.h
63
+ /* ===========================================================================
64
+ * Internal compression state.
65
+ */
66
+ const LENGTH_CODES$1 = 29;
67
+ /* number of length codes, not counting the special END_BLOCK code */
68
+ const LITERALS$1 = 256;
69
+ /* number of literal bytes 0..255 */
70
+ const L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
71
+ /* number of Literal or Length codes, including the END_BLOCK code */
72
+ const D_CODES$1 = 30;
73
+ /* number of distance codes */
74
+ const BL_CODES$1 = 19;
75
+ /* number of codes used to transfer the bit lengths */
76
+ const HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
77
+ /* maximum heap size */
78
+ const MAX_BITS$1 = 15;
79
+ /* All codes must not exceed MAX_BITS bits */
80
+ const Buf_size = 16;
81
+ /* size of bit buffer in bi_buf */
82
+ /* ===========================================================================
83
+ * Constants
84
+ */
85
+ const MAX_BL_BITS = 7;
86
+ /* Bit length codes must not exceed MAX_BL_BITS bits */
87
+ const END_BLOCK = 256;
88
+ /* end of block literal code */
89
+ const REP_3_6 = 16;
90
+ /* repeat previous bit length 3-6 times (2 bits of repeat count) */
91
+ const REPZ_3_10 = 17;
92
+ /* repeat a zero length 3-10 times (3 bits of repeat count) */
93
+ const REPZ_11_138 = 18;
94
+ /* repeat a zero length 11-138 times (7 bits of repeat count) */
95
+ /* eslint-disable comma-spacing,array-bracket-spacing */
96
+ const extra_lbits =
97
+ /* extra bits for each length code */
98
+ new Uint8Array([
99
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
100
+ 5, 5, 5, 5, 0,
101
+ ]);
102
+ const extra_dbits =
103
+ /* extra bits for each distance code */
104
+ new Uint8Array([
105
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
106
+ 10, 11, 11, 12, 12, 13, 13,
107
+ ]);
108
+ const extra_blbits =
109
+ /* extra bits for each bit length code */
110
+ new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7]);
111
+ const bl_order = new Uint8Array([
112
+ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
113
+ ]);
114
+ /* eslint-enable comma-spacing,array-bracket-spacing */
115
+ /* The lengths of the bit length codes are sent in order of decreasing
116
+ * probability, to avoid transmitting the lengths for unused bit length codes.
117
+ */
118
+ /* ===========================================================================
119
+ * Local data. These are initialized only once.
120
+ */
121
+ // We pre-fill arrays with 0 to avoid uninitialized gaps
122
+ const DIST_CODE_LEN = 512; /* see definition of array dist_code below */
123
+ // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
124
+ const static_ltree = new Array((L_CODES$1 + 2) * 2);
125
+ zero$1(static_ltree);
126
+ /* The static literal tree. Since the bit lengths are imposed, there is no
127
+ * need for the L_CODES extra codes used during heap construction. However
128
+ * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
129
+ * below).
130
+ */
131
+ const static_dtree = new Array(D_CODES$1 * 2);
132
+ zero$1(static_dtree);
133
+ /* The static distance tree. (Actually a trivial tree since all codes use
134
+ * 5 bits.)
135
+ */
136
+ const _dist_code = new Array(DIST_CODE_LEN);
137
+ zero$1(_dist_code);
138
+ /* Distance codes. The first 256 values correspond to the distances
139
+ * 3 .. 258, the last 256 values correspond to the top 8 bits of
140
+ * the 15 bit distances.
141
+ */
142
+ const _length_code = new Array(MAX_MATCH$1 - MIN_MATCH$1 + 1);
143
+ zero$1(_length_code);
144
+ /* length code for each normalized match length (0 == MIN_MATCH) */
145
+ const base_length = new Array(LENGTH_CODES$1);
146
+ zero$1(base_length);
147
+ /* First normalized length for each code (0 = MIN_MATCH) */
148
+ const base_dist = new Array(D_CODES$1);
149
+ zero$1(base_dist);
150
+ /* First normalized distance for each code (0 = distance of 1) */
151
+ function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
152
+ this.static_tree = static_tree; /* static tree or NULL */
153
+ this.extra_bits = extra_bits; /* extra bits for each code or NULL */
154
+ this.extra_base = extra_base; /* base index for extra_bits */
155
+ this.elems = elems; /* max number of elements in the tree */
156
+ this.max_length = max_length; /* max bit length for the codes */
157
+ // show if `static_tree` has data or dummy - needed for monomorphic objects
158
+ this.has_stree = static_tree && static_tree.length;
159
+ }
160
+ let static_l_desc;
161
+ let static_d_desc;
162
+ let static_bl_desc;
163
+ function TreeDesc(dyn_tree, stat_desc) {
164
+ this.dyn_tree = dyn_tree; /* the dynamic tree */
165
+ this.max_code = 0; /* largest code with non zero frequency */
166
+ this.stat_desc = stat_desc; /* the corresponding static tree */
167
+ }
168
+ const d_code = (dist) => {
169
+ return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
170
+ };
171
+ /* ===========================================================================
172
+ * Output a short LSB first on the stream.
173
+ * IN assertion: there is enough room in pendingBuf.
174
+ */
175
+ const put_short = (s, w) => {
176
+ // put_byte(s, (uch)((w) & 0xff));
177
+ // put_byte(s, (uch)((ush)(w) >> 8));
178
+ s.pending_buf[s.pending++] = w & 0xff;
179
+ s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
180
+ };
181
+ /* ===========================================================================
182
+ * Send a value on a given number of bits.
183
+ * IN assertion: length <= 16 and value fits in length bits.
184
+ */
185
+ const send_bits = (s, value, length) => {
186
+ if (s.bi_valid > Buf_size - length) {
187
+ s.bi_buf |= (value << s.bi_valid) & 0xffff;
188
+ put_short(s, s.bi_buf);
189
+ s.bi_buf = value >> (Buf_size - s.bi_valid);
190
+ s.bi_valid += length - Buf_size;
191
+ }
192
+ else {
193
+ s.bi_buf |= (value << s.bi_valid) & 0xffff;
194
+ s.bi_valid += length;
195
+ }
196
+ };
197
+ const send_code = (s, c, tree) => {
198
+ send_bits(s, tree[c * 2] /*.Code*/, tree[c * 2 + 1] /*.Len*/);
199
+ };
200
+ /* ===========================================================================
201
+ * Reverse the first len bits of a code, using straightforward code (a faster
202
+ * method would use a table)
203
+ * IN assertion: 1 <= len <= 15
204
+ */
205
+ const bi_reverse = (code, len) => {
206
+ let res = 0;
207
+ do {
208
+ res |= code & 1;
209
+ code >>>= 1;
210
+ res <<= 1;
211
+ } while (--len > 0);
212
+ return res >>> 1;
213
+ };
214
+ /* ===========================================================================
215
+ * Flush the bit buffer, keeping at most 7 bits in it.
216
+ */
217
+ const bi_flush = (s) => {
218
+ if (s.bi_valid === 16) {
219
+ put_short(s, s.bi_buf);
220
+ s.bi_buf = 0;
221
+ s.bi_valid = 0;
222
+ }
223
+ else if (s.bi_valid >= 8) {
224
+ s.pending_buf[s.pending++] = s.bi_buf & 0xff;
225
+ s.bi_buf >>= 8;
226
+ s.bi_valid -= 8;
227
+ }
228
+ };
229
+ /* ===========================================================================
230
+ * Compute the optimal bit lengths for a tree and update the total bit length
231
+ * for the current block.
232
+ * IN assertion: the fields freq and dad are set, heap[heap_max] and
233
+ * above are the tree nodes sorted by increasing frequency.
234
+ * OUT assertions: the field len is set to the optimal bit length, the
235
+ * array bl_count contains the frequencies for each bit length.
236
+ * The length opt_len is updated; static_len is also updated if stree is
237
+ * not null.
238
+ */
239
+ const gen_bitlen = (s, desc) => {
240
+ // deflate_state *s;
241
+ // tree_desc *desc; /* the tree descriptor */
242
+ const tree = desc.dyn_tree;
243
+ const max_code = desc.max_code;
244
+ const stree = desc.stat_desc.static_tree;
245
+ const has_stree = desc.stat_desc.has_stree;
246
+ const extra = desc.stat_desc.extra_bits;
247
+ const base = desc.stat_desc.extra_base;
248
+ const max_length = desc.stat_desc.max_length;
249
+ let h; /* heap index */
250
+ let n, m; /* iterate over the tree elements */
251
+ let bits; /* bit length */
252
+ let xbits; /* extra bits */
253
+ let f; /* frequency */
254
+ let overflow = 0; /* number of elements with bit length too large */
255
+ for (bits = 0; bits <= MAX_BITS$1; bits++) {
256
+ s.bl_count[bits] = 0;
257
+ }
258
+ /* In a first pass, compute the optimal bit lengths (which may
259
+ * overflow in the case of the bit length tree).
260
+ */
261
+ tree[s.heap[s.heap_max] * 2 + 1] /*.Len*/ = 0; /* root of the heap */
262
+ for (h = s.heap_max + 1; h < HEAP_SIZE$1; h++) {
263
+ n = s.heap[h];
264
+ bits = tree[tree[n * 2 + 1] /*.Dad*/ * 2 + 1] /*.Len*/ + 1;
265
+ if (bits > max_length) {
266
+ bits = max_length;
267
+ overflow++;
268
+ }
269
+ tree[n * 2 + 1] /*.Len*/ = bits;
270
+ /* We overwrite tree[n].Dad which is no longer needed */
271
+ if (n > max_code) {
272
+ continue;
273
+ } /* not a leaf node */
274
+ s.bl_count[bits]++;
275
+ xbits = 0;
276
+ if (n >= base) {
277
+ xbits = extra[n - base];
278
+ }
279
+ f = tree[n * 2] /*.Freq*/;
280
+ s.opt_len += f * (bits + xbits);
281
+ if (has_stree) {
282
+ s.static_len += f * (stree[n * 2 + 1] /*.Len*/ + xbits);
283
+ }
284
+ }
285
+ if (overflow === 0) {
286
+ return;
287
+ }
288
+ // Tracev((stderr,"\nbit length overflow\n"));
289
+ /* This happens for example on obj2 and pic of the Calgary corpus */
290
+ /* Find the first bit length which could increase: */
291
+ do {
292
+ bits = max_length - 1;
293
+ while (s.bl_count[bits] === 0) {
294
+ bits--;
295
+ }
296
+ s.bl_count[bits]--; /* move one leaf down the tree */
297
+ s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
298
+ s.bl_count[max_length]--;
299
+ /* The brother of the overflow item also moves one step up,
300
+ * but this does not affect bl_count[max_length]
301
+ */
302
+ overflow -= 2;
303
+ } while (overflow > 0);
304
+ /* Now recompute all bit lengths, scanning in increasing frequency.
305
+ * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
306
+ * lengths instead of fixing only the wrong ones. This idea is taken
307
+ * from 'ar' written by Haruhiko Okumura.)
308
+ */
309
+ for (bits = max_length; bits !== 0; bits--) {
310
+ n = s.bl_count[bits];
311
+ while (n !== 0) {
312
+ m = s.heap[--h];
313
+ if (m > max_code) {
314
+ continue;
315
+ }
316
+ if (tree[m * 2 + 1] /*.Len*/ !== bits) {
317
+ // Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
318
+ s.opt_len +=
319
+ (bits - tree[m * 2 + 1]) /*.Len*/ * tree[m * 2] /*.Freq*/;
320
+ tree[m * 2 + 1] /*.Len*/ = bits;
321
+ }
322
+ n--;
323
+ }
324
+ }
325
+ };
326
+ /* ===========================================================================
327
+ * Generate the codes for a given tree and bit counts (which need not be
328
+ * optimal).
329
+ * IN assertion: the array bl_count contains the bit length statistics for
330
+ * the given tree and the field len is set for all tree elements.
331
+ * OUT assertion: the field code is set for all tree elements of non
332
+ * zero code length.
333
+ */
334
+ const gen_codes = (tree, max_code, bl_count) => {
335
+ // ct_data *tree; /* the tree to decorate */
336
+ // int max_code; /* largest code with non zero frequency */
337
+ // ushf *bl_count; /* number of codes at each bit length */
338
+ const next_code = new Array(MAX_BITS$1 + 1); /* next code value for each bit length */
339
+ let code = 0; /* running code value */
340
+ let bits; /* bit index */
341
+ let n; /* code index */
342
+ /* The distribution counts are first used to generate the code values
343
+ * without bit reversal.
344
+ */
345
+ for (bits = 1; bits <= MAX_BITS$1; bits++) {
346
+ code = (code + bl_count[bits - 1]) << 1;
347
+ next_code[bits] = code;
348
+ }
349
+ /* Check that the bit counts in bl_count are consistent. The last code
350
+ * must be all ones.
351
+ */
352
+ //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
353
+ // "inconsistent bit counts");
354
+ //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
355
+ for (n = 0; n <= max_code; n++) {
356
+ let len = tree[n * 2 + 1]; /*.Len*/
357
+ if (len === 0) {
358
+ continue;
359
+ }
360
+ /* Now reverse the bits */
361
+ tree[n * 2] /*.Code*/ = bi_reverse(next_code[len]++, len);
362
+ //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
363
+ // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
364
+ }
365
+ };
366
+ /* ===========================================================================
367
+ * Initialize the various 'constant' tables.
368
+ */
369
+ const tr_static_init = () => {
370
+ let n; /* iterates over tree elements */
371
+ let bits; /* bit counter */
372
+ let length; /* length value */
373
+ let code; /* code value */
374
+ let dist; /* distance index */
375
+ const bl_count = new Array(MAX_BITS$1 + 1);
376
+ /* number of codes at each bit length for an optimal tree */
377
+ // do check in _tr_init()
378
+ //if (static_init_done) return;
379
+ /* For some embedded targets, global variables are not initialized: */
380
+ /*#ifdef NO_INIT_GLOBAL_POINTERS
381
+ static_l_desc.static_tree = static_ltree;
382
+ static_l_desc.extra_bits = extra_lbits;
383
+ static_d_desc.static_tree = static_dtree;
384
+ static_d_desc.extra_bits = extra_dbits;
385
+ static_bl_desc.extra_bits = extra_blbits;
386
+ #endif*/
387
+ /* Initialize the mapping length (0..255) -> length code (0..28) */
388
+ length = 0;
389
+ for (code = 0; code < LENGTH_CODES$1 - 1; code++) {
390
+ base_length[code] = length;
391
+ for (n = 0; n < 1 << extra_lbits[code]; n++) {
392
+ _length_code[length++] = code;
393
+ }
394
+ }
395
+ //Assert (length == 256, "tr_static_init: length != 256");
396
+ /* Note that the length 255 (match length 258) can be represented
397
+ * in two different ways: code 284 + 5 bits or code 285, so we
398
+ * overwrite length_code[255] to use the best encoding:
399
+ */
400
+ _length_code[length - 1] = code;
401
+ /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
402
+ dist = 0;
403
+ for (code = 0; code < 16; code++) {
404
+ base_dist[code] = dist;
405
+ for (n = 0; n < 1 << extra_dbits[code]; n++) {
406
+ _dist_code[dist++] = code;
407
+ }
408
+ }
409
+ //Assert (dist == 256, "tr_static_init: dist != 256");
410
+ dist >>= 7; /* from now on, all distances are divided by 128 */
411
+ for (; code < D_CODES$1; code++) {
412
+ base_dist[code] = dist << 7;
413
+ for (n = 0; n < 1 << (extra_dbits[code] - 7); n++) {
414
+ _dist_code[256 + dist++] = code;
415
+ }
416
+ }
417
+ //Assert (dist == 256, "tr_static_init: 256+dist != 512");
418
+ /* Construct the codes of the static literal tree */
419
+ for (bits = 0; bits <= MAX_BITS$1; bits++) {
420
+ bl_count[bits] = 0;
421
+ }
422
+ n = 0;
423
+ while (n <= 143) {
424
+ static_ltree[n * 2 + 1] /*.Len*/ = 8;
425
+ n++;
426
+ bl_count[8]++;
427
+ }
428
+ while (n <= 255) {
429
+ static_ltree[n * 2 + 1] /*.Len*/ = 9;
430
+ n++;
431
+ bl_count[9]++;
432
+ }
433
+ while (n <= 279) {
434
+ static_ltree[n * 2 + 1] /*.Len*/ = 7;
435
+ n++;
436
+ bl_count[7]++;
437
+ }
438
+ while (n <= 287) {
439
+ static_ltree[n * 2 + 1] /*.Len*/ = 8;
440
+ n++;
441
+ bl_count[8]++;
442
+ }
443
+ /* Codes 286 and 287 do not exist, but we must include them in the
444
+ * tree construction to get a canonical Huffman tree (longest code
445
+ * all ones)
446
+ */
447
+ gen_codes(static_ltree, L_CODES$1 + 1, bl_count);
448
+ /* The static distance tree is trivial: */
449
+ for (n = 0; n < D_CODES$1; n++) {
450
+ static_dtree[n * 2 + 1] /*.Len*/ = 5;
451
+ static_dtree[n * 2] /*.Code*/ = bi_reverse(n, 5);
452
+ }
453
+ // Now data ready and we can init static trees
454
+ static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS$1 + 1, L_CODES$1, MAX_BITS$1);
455
+ static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES$1, MAX_BITS$1);
456
+ static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES$1, MAX_BL_BITS);
457
+ //static_init_done = true;
458
+ };
459
+ /* ===========================================================================
460
+ * Initialize a new block.
461
+ */
462
+ const init_block = (s) => {
463
+ let n; /* iterates over tree elements */
464
+ /* Initialize the trees. */
465
+ for (n = 0; n < L_CODES$1; n++) {
466
+ s.dyn_ltree[n * 2] /*.Freq*/ = 0;
467
+ }
468
+ for (n = 0; n < D_CODES$1; n++) {
469
+ s.dyn_dtree[n * 2] /*.Freq*/ = 0;
470
+ }
471
+ for (n = 0; n < BL_CODES$1; n++) {
472
+ s.bl_tree[n * 2] /*.Freq*/ = 0;
473
+ }
474
+ s.dyn_ltree[END_BLOCK * 2] /*.Freq*/ = 1;
475
+ s.opt_len = s.static_len = 0;
476
+ s.sym_next = s.matches = 0;
477
+ };
478
+ /* ===========================================================================
479
+ * Flush the bit buffer and align the output on a byte boundary
480
+ */
481
+ const bi_windup = (s) => {
482
+ if (s.bi_valid > 8) {
483
+ put_short(s, s.bi_buf);
484
+ }
485
+ else if (s.bi_valid > 0) {
486
+ //put_byte(s, (Byte)s->bi_buf);
487
+ s.pending_buf[s.pending++] = s.bi_buf;
488
+ }
489
+ s.bi_buf = 0;
490
+ s.bi_valid = 0;
491
+ };
492
+ /* ===========================================================================
493
+ * Compares to subtrees, using the tree depth as tie breaker when
494
+ * the subtrees have equal frequency. This minimizes the worst case length.
495
+ */
496
+ const smaller = (tree, n, m, depth) => {
497
+ const _n2 = n * 2;
498
+ const _m2 = m * 2;
499
+ return (tree[_n2] /*.Freq*/ < tree[_m2] /*.Freq*/ ||
500
+ (tree[_n2] /*.Freq*/ === tree[_m2] /*.Freq*/ && depth[n] <= depth[m]));
501
+ };
502
+ /* ===========================================================================
503
+ * Restore the heap property by moving down the tree starting at node k,
504
+ * exchanging a node with the smallest of its two sons if necessary, stopping
505
+ * when the heap property is re-established (each father smaller than its
506
+ * two sons).
507
+ */
508
+ const pqdownheap = (s, tree, k) => {
509
+ // deflate_state *s;
510
+ // ct_data *tree; /* the tree to restore */
511
+ // int k; /* node to move down */
512
+ const v = s.heap[k];
513
+ let j = k << 1; /* left son of k */
514
+ while (j <= s.heap_len) {
515
+ /* Set j to the smallest of the two sons: */
516
+ if (j < s.heap_len &&
517
+ smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
518
+ j++;
519
+ }
520
+ /* Exit if v is smaller than both sons */
521
+ if (smaller(tree, v, s.heap[j], s.depth)) {
522
+ break;
523
+ }
524
+ /* Exchange v with the smallest son */
525
+ s.heap[k] = s.heap[j];
526
+ k = j;
527
+ /* And continue down the tree, setting j to the left son of k */
528
+ j <<= 1;
529
+ }
530
+ s.heap[k] = v;
531
+ };
532
+ // inlined manually
533
+ // const SMALLEST = 1;
534
+ /* ===========================================================================
535
+ * Send the block data compressed using the given Huffman trees
536
+ */
537
+ const compress_block = (s, ltree, dtree) => {
538
+ // deflate_state *s;
539
+ // const ct_data *ltree; /* literal tree */
540
+ // const ct_data *dtree; /* distance tree */
541
+ let dist; /* distance of matched string */
542
+ let lc; /* match length or unmatched char (if dist == 0) */
543
+ let sx = 0; /* running index in sym_buf */
544
+ let code; /* the code to send */
545
+ let extra; /* number of extra bits to send */
546
+ if (s.sym_next !== 0) {
547
+ do {
548
+ dist = s.pending_buf[s.sym_buf + sx++] & 0xff;
549
+ dist += (s.pending_buf[s.sym_buf + sx++] & 0xff) << 8;
550
+ lc = s.pending_buf[s.sym_buf + sx++];
551
+ if (dist === 0) {
552
+ send_code(s, lc, ltree); /* send a literal byte */
553
+ //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
554
+ }
555
+ else {
556
+ /* Here, lc is the match length - MIN_MATCH */
557
+ code = _length_code[lc];
558
+ send_code(s, code + LITERALS$1 + 1, ltree); /* send the length code */
559
+ extra = extra_lbits[code];
560
+ if (extra !== 0) {
561
+ lc -= base_length[code];
562
+ send_bits(s, lc, extra); /* send the extra length bits */
563
+ }
564
+ dist--; /* dist is now the match distance - 1 */
565
+ code = d_code(dist);
566
+ //Assert (code < D_CODES, "bad d_code");
567
+ send_code(s, code, dtree); /* send the distance code */
568
+ extra = extra_dbits[code];
569
+ if (extra !== 0) {
570
+ dist -= base_dist[code];
571
+ send_bits(s, dist, extra); /* send the extra distance bits */
572
+ }
573
+ } /* literal or match pair ? */
574
+ /* Check that the overlay between pending_buf and sym_buf is ok: */
575
+ //Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
576
+ } while (sx < s.sym_next);
577
+ }
578
+ send_code(s, END_BLOCK, ltree);
579
+ };
580
+ /* ===========================================================================
581
+ * Construct one Huffman tree and assigns the code bit strings and lengths.
582
+ * Update the total bit length for the current block.
583
+ * IN assertion: the field freq is set for all tree elements.
584
+ * OUT assertions: the fields len and code are set to the optimal bit length
585
+ * and corresponding code. The length opt_len is updated; static_len is
586
+ * also updated if stree is not null. The field max_code is set.
587
+ */
588
+ const build_tree = (s, desc) => {
589
+ // deflate_state *s;
590
+ // tree_desc *desc; /* the tree descriptor */
591
+ const tree = desc.dyn_tree;
592
+ const stree = desc.stat_desc.static_tree;
593
+ const has_stree = desc.stat_desc.has_stree;
594
+ const elems = desc.stat_desc.elems;
595
+ let n, m; /* iterate over heap elements */
596
+ let max_code = -1; /* largest code with non zero frequency */
597
+ let node; /* new node being created */
598
+ /* Construct the initial heap, with least frequent element in
599
+ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
600
+ * heap[0] is not used.
601
+ */
602
+ s.heap_len = 0;
603
+ s.heap_max = HEAP_SIZE$1;
604
+ for (n = 0; n < elems; n++) {
605
+ if (tree[n * 2] /*.Freq*/ !== 0) {
606
+ s.heap[++s.heap_len] = max_code = n;
607
+ s.depth[n] = 0;
608
+ }
609
+ else {
610
+ tree[n * 2 + 1] /*.Len*/ = 0;
611
+ }
612
+ }
613
+ /* The pkzip format requires that at least one distance code exists,
614
+ * and that at least one bit should be sent even if there is only one
615
+ * possible code. So to avoid special checks later on we force at least
616
+ * two codes of non zero frequency.
617
+ */
618
+ while (s.heap_len < 2) {
619
+ node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
620
+ tree[node * 2] /*.Freq*/ = 1;
621
+ s.depth[node] = 0;
622
+ s.opt_len--;
623
+ if (has_stree) {
624
+ s.static_len -= stree[node * 2 + 1] /*.Len*/;
625
+ }
626
+ /* node is 0 or 1 so it does not have extra bits */
627
+ }
628
+ desc.max_code = max_code;
629
+ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
630
+ * establish sub-heaps of increasing lengths:
631
+ */
632
+ for (n = s.heap_len >> 1 /*int /2*/; n >= 1; n--) {
633
+ pqdownheap(s, tree, n);
634
+ }
635
+ /* Construct the Huffman tree by repeatedly combining the least two
636
+ * frequent nodes.
637
+ */
638
+ node = elems; /* next internal node of the tree */
639
+ do {
640
+ //pqremove(s, tree, n); /* n = node of least frequency */
641
+ /*** pqremove ***/
642
+ n = s.heap[1 /*SMALLEST*/];
643
+ s.heap[1 /*SMALLEST*/] = s.heap[s.heap_len--];
644
+ pqdownheap(s, tree, 1 /*SMALLEST*/);
645
+ /***/
646
+ m = s.heap[1 /*SMALLEST*/]; /* m = node of next least frequency */
647
+ s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
648
+ s.heap[--s.heap_max] = m;
649
+ /* Create a new node father of n and m */
650
+ tree[node * 2] /*.Freq*/ =
651
+ tree[n * 2] /*.Freq*/ + tree[m * 2] /*.Freq*/;
652
+ s.depth[node] =
653
+ (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
654
+ tree[n * 2 + 1] /*.Dad*/ = tree[m * 2 + 1] /*.Dad*/ = node;
655
+ /* and insert the new node in the heap */
656
+ s.heap[1 /*SMALLEST*/] = node++;
657
+ pqdownheap(s, tree, 1 /*SMALLEST*/);
658
+ } while (s.heap_len >= 2);
659
+ s.heap[--s.heap_max] = s.heap[1 /*SMALLEST*/];
660
+ /* At this point, the fields freq and dad are set. We can now
661
+ * generate the bit lengths.
662
+ */
663
+ gen_bitlen(s, desc);
664
+ /* The field len is now set, we can generate the bit codes */
665
+ gen_codes(tree, max_code, s.bl_count);
666
+ };
667
+ /* ===========================================================================
668
+ * Scan a literal or distance tree to determine the frequencies of the codes
669
+ * in the bit length tree.
670
+ */
671
+ const scan_tree = (s, tree, max_code) => {
672
+ // deflate_state *s;
673
+ // ct_data *tree; /* the tree to be scanned */
674
+ // int max_code; /* and its largest code of non zero frequency */
675
+ let n; /* iterates over all tree elements */
676
+ let prevlen = -1; /* last emitted length */
677
+ let curlen; /* length of current code */
678
+ let nextlen = tree[0 * 2 + 1]; /*.Len*/ /* length of next code */
679
+ let count = 0; /* repeat count of the current code */
680
+ let max_count = 7; /* max repeat count */
681
+ let min_count = 4; /* min repeat count */
682
+ if (nextlen === 0) {
683
+ max_count = 138;
684
+ min_count = 3;
685
+ }
686
+ tree[(max_code + 1) * 2 + 1] /*.Len*/ = 0xffff; /* guard */
687
+ for (n = 0; n <= max_code; n++) {
688
+ curlen = nextlen;
689
+ nextlen = tree[(n + 1) * 2 + 1] /*.Len*/;
690
+ if (++count < max_count && curlen === nextlen) {
691
+ continue;
692
+ }
693
+ else if (count < min_count) {
694
+ s.bl_tree[curlen * 2] /*.Freq*/ += count;
695
+ }
696
+ else if (curlen !== 0) {
697
+ if (curlen !== prevlen) {
698
+ s.bl_tree[curlen * 2] /*.Freq*/++;
699
+ }
700
+ s.bl_tree[REP_3_6 * 2] /*.Freq*/++;
701
+ }
702
+ else if (count <= 10) {
703
+ s.bl_tree[REPZ_3_10 * 2] /*.Freq*/++;
704
+ }
705
+ else {
706
+ s.bl_tree[REPZ_11_138 * 2] /*.Freq*/++;
707
+ }
708
+ count = 0;
709
+ prevlen = curlen;
710
+ if (nextlen === 0) {
711
+ max_count = 138;
712
+ min_count = 3;
713
+ }
714
+ else if (curlen === nextlen) {
715
+ max_count = 6;
716
+ min_count = 3;
717
+ }
718
+ else {
719
+ max_count = 7;
720
+ min_count = 4;
721
+ }
722
+ }
723
+ };
724
+ /* ===========================================================================
725
+ * Send a literal or distance tree in compressed form, using the codes in
726
+ * bl_tree.
727
+ */
728
+ const send_tree = (s, tree, max_code) => {
729
+ // deflate_state *s;
730
+ // ct_data *tree; /* the tree to be scanned */
731
+ // int max_code; /* and its largest code of non zero frequency */
732
+ let n; /* iterates over all tree elements */
733
+ let prevlen = -1; /* last emitted length */
734
+ let curlen; /* length of current code */
735
+ let nextlen = tree[0 * 2 + 1]; /*.Len*/ /* length of next code */
736
+ let count = 0; /* repeat count of the current code */
737
+ let max_count = 7; /* max repeat count */
738
+ let min_count = 4; /* min repeat count */
739
+ /* tree[max_code+1].Len = -1; */ /* guard already set */
740
+ if (nextlen === 0) {
741
+ max_count = 138;
742
+ min_count = 3;
743
+ }
744
+ for (n = 0; n <= max_code; n++) {
745
+ curlen = nextlen;
746
+ nextlen = tree[(n + 1) * 2 + 1] /*.Len*/;
747
+ if (++count < max_count && curlen === nextlen) {
748
+ continue;
749
+ }
750
+ else if (count < min_count) {
751
+ do {
752
+ send_code(s, curlen, s.bl_tree);
753
+ } while (--count !== 0);
754
+ }
755
+ else if (curlen !== 0) {
756
+ if (curlen !== prevlen) {
757
+ send_code(s, curlen, s.bl_tree);
758
+ count--;
759
+ }
760
+ //Assert(count >= 3 && count <= 6, " 3_6?");
761
+ send_code(s, REP_3_6, s.bl_tree);
762
+ send_bits(s, count - 3, 2);
763
+ }
764
+ else if (count <= 10) {
765
+ send_code(s, REPZ_3_10, s.bl_tree);
766
+ send_bits(s, count - 3, 3);
767
+ }
768
+ else {
769
+ send_code(s, REPZ_11_138, s.bl_tree);
770
+ send_bits(s, count - 11, 7);
771
+ }
772
+ count = 0;
773
+ prevlen = curlen;
774
+ if (nextlen === 0) {
775
+ max_count = 138;
776
+ min_count = 3;
777
+ }
778
+ else if (curlen === nextlen) {
779
+ max_count = 6;
780
+ min_count = 3;
781
+ }
782
+ else {
783
+ max_count = 7;
784
+ min_count = 4;
785
+ }
786
+ }
787
+ };
788
+ /* ===========================================================================
789
+ * Construct the Huffman tree for the bit lengths and return the index in
790
+ * bl_order of the last bit length code to send.
791
+ */
792
+ const build_bl_tree = (s) => {
793
+ let max_blindex; /* index of last bit length code of non zero freq */
794
+ /* Determine the bit length frequencies for literal and distance trees */
795
+ scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
796
+ scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
797
+ /* Build the bit length tree: */
798
+ build_tree(s, s.bl_desc);
799
+ /* opt_len now includes the length of the tree representations, except
800
+ * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
801
+ */
802
+ /* Determine the number of bit length codes to send. The pkzip format
803
+ * requires that at least 4 bit length codes be sent. (appnote.txt says
804
+ * 3 but the actual value used is 4.)
805
+ */
806
+ for (max_blindex = BL_CODES$1 - 1; max_blindex >= 3; max_blindex--) {
807
+ if (s.bl_tree[bl_order[max_blindex] * 2 + 1] /*.Len*/ !== 0) {
808
+ break;
809
+ }
810
+ }
811
+ /* Update opt_len to include the bit length tree and counts */
812
+ s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
813
+ //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
814
+ // s->opt_len, s->static_len));
815
+ return max_blindex;
816
+ };
817
+ /* ===========================================================================
818
+ * Send the header for a block using dynamic Huffman trees: the counts, the
819
+ * lengths of the bit length codes, the literal tree and the distance tree.
820
+ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
821
+ */
822
+ const send_all_trees = (s, lcodes, dcodes, blcodes) => {
823
+ // deflate_state *s;
824
+ // int lcodes, dcodes, blcodes; /* number of codes for each tree */
825
+ let rank; /* index in bl_order */
826
+ //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
827
+ //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
828
+ // "too many codes");
829
+ //Tracev((stderr, "\nbl counts: "));
830
+ send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
831
+ send_bits(s, dcodes - 1, 5);
832
+ send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
833
+ for (rank = 0; rank < blcodes; rank++) {
834
+ //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
835
+ send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1] /*.Len*/, 3);
836
+ }
837
+ //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
838
+ send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
839
+ //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
840
+ send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
841
+ //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
842
+ };
843
+ /* ===========================================================================
844
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
845
+ * - TEXT if the two conditions below are satisfied:
846
+ * a) There are no non-portable control characters belonging to the
847
+ * "block list" (0..6, 14..25, 28..31).
848
+ * b) There is at least one printable character belonging to the
849
+ * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
850
+ * - BINARY otherwise.
851
+ * - The following partially-portable control characters form a
852
+ * "gray list" that is ignored in this detection algorithm:
853
+ * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
854
+ * IN assertion: the fields Freq of dyn_ltree are set.
855
+ */
856
+ const detect_data_type = (s) => {
857
+ /* block_mask is the bit mask of block-listed bytes
858
+ * set bits 0..6, 14..25, and 28..31
859
+ * 0xf3ffc07f = binary 11110011111111111100000001111111
860
+ */
861
+ let block_mask = 0xf3ffc07f;
862
+ let n;
863
+ /* Check for non-textual ("block-listed") bytes. */
864
+ for (n = 0; n <= 31; n++, block_mask >>>= 1) {
865
+ if (block_mask & 1 && s.dyn_ltree[n * 2] /*.Freq*/ !== 0) {
866
+ return Z_BINARY;
867
+ }
868
+ }
869
+ /* Check for textual ("allow-listed") bytes. */
870
+ if (s.dyn_ltree[9 * 2] /*.Freq*/ !== 0 ||
871
+ s.dyn_ltree[10 * 2] /*.Freq*/ !== 0 ||
872
+ s.dyn_ltree[13 * 2] /*.Freq*/ !== 0) {
873
+ return Z_TEXT;
874
+ }
875
+ for (n = 32; n < LITERALS$1; n++) {
876
+ if (s.dyn_ltree[n * 2] /*.Freq*/ !== 0) {
877
+ return Z_TEXT;
878
+ }
879
+ }
880
+ /* There are no "block-listed" or "allow-listed" bytes:
881
+ * this stream either is empty or has tolerated ("gray-listed") bytes only.
882
+ */
883
+ return Z_BINARY;
884
+ };
885
+ let static_init_done = false;
886
+ /* ===========================================================================
887
+ * Initialize the tree data structures for a new zlib stream.
888
+ */
889
+ const _tr_init$1 = (s) => {
890
+ if (!static_init_done) {
891
+ tr_static_init();
892
+ static_init_done = true;
893
+ }
894
+ s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
895
+ s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
896
+ s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
897
+ s.bi_buf = 0;
898
+ s.bi_valid = 0;
899
+ /* Initialize the first block of the first file: */
900
+ init_block(s);
901
+ };
902
+ /* ===========================================================================
903
+ * Send a stored block
904
+ */
905
+ const _tr_stored_block$1 = (s, buf, stored_len, last) => {
906
+ //DeflateState *s;
907
+ //charf *buf; /* input block */
908
+ //ulg stored_len; /* length of input block */
909
+ //int last; /* one if this is the last block for a file */
910
+ send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
911
+ bi_windup(s); /* align on byte boundary */
912
+ put_short(s, stored_len);
913
+ put_short(s, ~stored_len);
914
+ if (stored_len) {
915
+ s.pending_buf.set(s.window.subarray(buf, buf + stored_len), s.pending);
916
+ }
917
+ s.pending += stored_len;
918
+ };
919
+ /* ===========================================================================
920
+ * Send one empty static block to give enough lookahead for inflate.
921
+ * This takes 10 bits, of which 7 may remain in the bit buffer.
922
+ */
923
+ const _tr_align$1 = (s) => {
924
+ send_bits(s, STATIC_TREES << 1, 3);
925
+ send_code(s, END_BLOCK, static_ltree);
926
+ bi_flush(s);
927
+ };
928
+ /* ===========================================================================
929
+ * Determine the best encoding for the current block: dynamic trees, static
930
+ * trees or store, and write out the encoded block.
931
+ */
932
+ const _tr_flush_block$1 = (s, buf, stored_len, last) => {
933
+ //DeflateState *s;
934
+ //charf *buf; /* input block, or NULL if too old */
935
+ //ulg stored_len; /* length of input block */
936
+ //int last; /* one if this is the last block for a file */
937
+ let opt_lenb, static_lenb; /* opt_len and static_len in bytes */
938
+ let max_blindex = 0; /* index of last bit length code of non zero freq */
939
+ /* Build the Huffman trees unless a stored block is forced */
940
+ if (s.level > 0) {
941
+ /* Check if the file is binary or text */
942
+ if (s.strm.data_type === Z_UNKNOWN$1) {
943
+ s.strm.data_type = detect_data_type(s);
944
+ }
945
+ /* Construct the literal and distance trees */
946
+ build_tree(s, s.l_desc);
947
+ // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
948
+ // s->static_len));
949
+ build_tree(s, s.d_desc);
950
+ // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
951
+ // s->static_len));
952
+ /* At this point, opt_len and static_len are the total bit lengths of
953
+ * the compressed block data, excluding the tree representations.
954
+ */
955
+ /* Build the bit length tree for the above two trees, and get the index
956
+ * in bl_order of the last bit length code to send.
957
+ */
958
+ max_blindex = build_bl_tree(s);
959
+ /* Determine the best encoding. Compute the block lengths in bytes. */
960
+ opt_lenb = (s.opt_len + 3 + 7) >>> 3;
961
+ static_lenb = (s.static_len + 3 + 7) >>> 3;
962
+ // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
963
+ // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
964
+ // s->sym_next / 3));
965
+ if (static_lenb <= opt_lenb) {
966
+ opt_lenb = static_lenb;
967
+ }
968
+ }
969
+ else {
970
+ // Assert(buf != (char*)0, "lost buf");
971
+ opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
972
+ }
973
+ if (stored_len + 4 <= opt_lenb && buf !== -1) {
974
+ /* 4: two words for the lengths */
975
+ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
976
+ * Otherwise we can't have processed more than WSIZE input bytes since
977
+ * the last block flush, because compression would have been
978
+ * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
979
+ * transform a block into a stored block.
980
+ */
981
+ _tr_stored_block$1(s, buf, stored_len, last);
982
+ }
983
+ else if (s.strategy === Z_FIXED$1 || static_lenb === opt_lenb) {
984
+ send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
985
+ compress_block(s, static_ltree, static_dtree);
986
+ }
987
+ else {
988
+ send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
989
+ send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
990
+ compress_block(s, s.dyn_ltree, s.dyn_dtree);
991
+ }
992
+ // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
993
+ /* The above check is made mod 2^32, for files larger than 512 MB
994
+ * and uLong implemented on 32 bits.
995
+ */
996
+ init_block(s);
997
+ if (last) {
998
+ bi_windup(s);
999
+ }
1000
+ // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1001
+ // s->compressed_len-7*last));
1002
+ };
1003
+ /* ===========================================================================
1004
+ * Save the match info and tally the frequency counts. Return true if
1005
+ * the current block must be flushed.
1006
+ */
1007
+ const _tr_tally$1 = (s, dist, lc) => {
1008
+ // deflate_state *s;
1009
+ // unsigned dist; /* distance of matched string */
1010
+ // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1011
+ s.pending_buf[s.sym_buf + s.sym_next++] = dist;
1012
+ s.pending_buf[s.sym_buf + s.sym_next++] = dist >> 8;
1013
+ s.pending_buf[s.sym_buf + s.sym_next++] = lc;
1014
+ if (dist === 0) {
1015
+ /* lc is the unmatched char */
1016
+ s.dyn_ltree[lc * 2] /*.Freq*/++;
1017
+ }
1018
+ else {
1019
+ s.matches++;
1020
+ /* Here, lc is the match length - MIN_MATCH */
1021
+ dist--; /* dist = match distance - 1 */
1022
+ //Assert((ush)dist < (ush)MAX_DIST(s) &&
1023
+ // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1024
+ // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1025
+ s.dyn_ltree[(_length_code[lc] + LITERALS$1 + 1) * 2] /*.Freq*/++;
1026
+ s.dyn_dtree[d_code(dist) * 2] /*.Freq*/++;
1027
+ }
1028
+ return s.sym_next === s.sym_end;
1029
+ };
1030
+ var _tr_init_1 = _tr_init$1;
1031
+ var _tr_stored_block_1 = _tr_stored_block$1;
1032
+ var _tr_flush_block_1 = _tr_flush_block$1;
1033
+ var _tr_tally_1 = _tr_tally$1;
1034
+ var _tr_align_1 = _tr_align$1;
1035
+ var trees = {
1036
+ _tr_init: _tr_init_1,
1037
+ _tr_stored_block: _tr_stored_block_1,
1038
+ _tr_flush_block: _tr_flush_block_1,
1039
+ _tr_tally: _tr_tally_1,
1040
+ _tr_align: _tr_align_1,
1041
+ };
1042
+ // Note: adler32 takes 12% for level 0 and 2% for level 6.
1043
+ // It isn't worth it to make additional optimizations as in original.
1044
+ // Small size is preferable.
1045
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
1046
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1047
+ //
1048
+ // This software is provided 'as-is', without any express or implied
1049
+ // warranty. In no event will the authors be held liable for any damages
1050
+ // arising from the use of this software.
1051
+ //
1052
+ // Permission is granted to anyone to use this software for any purpose,
1053
+ // including commercial applications, and to alter it and redistribute it
1054
+ // freely, subject to the following restrictions:
1055
+ //
1056
+ // 1. The origin of this software must not be misrepresented; you must not
1057
+ // claim that you wrote the original software. If you use this software
1058
+ // in a product, an acknowledgment in the product documentation would be
1059
+ // appreciated but is not required.
1060
+ // 2. Altered source versions must be plainly marked as such, and must not be
1061
+ // misrepresented as being the original software.
1062
+ // 3. This notice may not be removed or altered from any source distribution.
1063
+ const adler32 = (adler, buf, len, pos) => {
1064
+ let s1 = (adler & 0xffff) | 0, s2 = ((adler >>> 16) & 0xffff) | 0, n = 0;
1065
+ while (len !== 0) {
1066
+ // Set limit ~ twice less than 5552, to keep
1067
+ // s2 in 31-bits, because we force signed ints.
1068
+ // in other case %= will fail.
1069
+ n = len > 2000 ? 2000 : len;
1070
+ len -= n;
1071
+ do {
1072
+ s1 = (s1 + buf[pos++]) | 0;
1073
+ s2 = (s2 + s1) | 0;
1074
+ } while (--n);
1075
+ s1 %= 65521;
1076
+ s2 %= 65521;
1077
+ }
1078
+ return s1 | (s2 << 16) | 0;
1079
+ };
1080
+ var adler32_1 = adler32;
1081
+ // Note: we can't get significant speed boost here.
1082
+ // So write code to minimize size - no pregenerated tables
1083
+ // and array tools dependencies.
1084
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
1085
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1086
+ //
1087
+ // This software is provided 'as-is', without any express or implied
1088
+ // warranty. In no event will the authors be held liable for any damages
1089
+ // arising from the use of this software.
1090
+ //
1091
+ // Permission is granted to anyone to use this software for any purpose,
1092
+ // including commercial applications, and to alter it and redistribute it
1093
+ // freely, subject to the following restrictions:
1094
+ //
1095
+ // 1. The origin of this software must not be misrepresented; you must not
1096
+ // claim that you wrote the original software. If you use this software
1097
+ // in a product, an acknowledgment in the product documentation would be
1098
+ // appreciated but is not required.
1099
+ // 2. Altered source versions must be plainly marked as such, and must not be
1100
+ // misrepresented as being the original software.
1101
+ // 3. This notice may not be removed or altered from any source distribution.
1102
+ // Use ordinary array, since untyped makes no boost here
1103
+ const makeTable = () => {
1104
+ let c, table = [];
1105
+ for (var n = 0; n < 256; n++) {
1106
+ c = n;
1107
+ for (var k = 0; k < 8; k++) {
1108
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
1109
+ }
1110
+ table[n] = c;
1111
+ }
1112
+ return table;
1113
+ };
1114
+ // Create table on load. Just 255 signed longs. Not a problem.
1115
+ const crcTable = new Uint32Array(makeTable());
1116
+ const crc32 = (crc, buf, len, pos) => {
1117
+ const t = crcTable;
1118
+ const end = pos + len;
1119
+ crc ^= -1;
1120
+ for (let i = pos; i < end; i++) {
1121
+ crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xff];
1122
+ }
1123
+ return crc ^ -1; // >>> 0;
1124
+ };
1125
+ var crc32_1 = crc32;
1126
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
1127
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1128
+ //
1129
+ // This software is provided 'as-is', without any express or implied
1130
+ // warranty. In no event will the authors be held liable for any damages
1131
+ // arising from the use of this software.
1132
+ //
1133
+ // Permission is granted to anyone to use this software for any purpose,
1134
+ // including commercial applications, and to alter it and redistribute it
1135
+ // freely, subject to the following restrictions:
1136
+ //
1137
+ // 1. The origin of this software must not be misrepresented; you must not
1138
+ // claim that you wrote the original software. If you use this software
1139
+ // in a product, an acknowledgment in the product documentation would be
1140
+ // appreciated but is not required.
1141
+ // 2. Altered source versions must be plainly marked as such, and must not be
1142
+ // misrepresented as being the original software.
1143
+ // 3. This notice may not be removed or altered from any source distribution.
1144
+ var messages = {
1145
+ 2: 'need dictionary' /* Z_NEED_DICT 2 */,
1146
+ 1: 'stream end' /* Z_STREAM_END 1 */,
1147
+ 0: '' /* Z_OK 0 */,
1148
+ '-1': 'file error' /* Z_ERRNO (-1) */,
1149
+ '-2': 'stream error' /* Z_STREAM_ERROR (-2) */,
1150
+ '-3': 'data error' /* Z_DATA_ERROR (-3) */,
1151
+ '-4': 'insufficient memory' /* Z_MEM_ERROR (-4) */,
1152
+ '-5': 'buffer error' /* Z_BUF_ERROR (-5) */,
1153
+ '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */,
1154
+ };
1155
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
1156
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1157
+ //
1158
+ // This software is provided 'as-is', without any express or implied
1159
+ // warranty. In no event will the authors be held liable for any damages
1160
+ // arising from the use of this software.
1161
+ //
1162
+ // Permission is granted to anyone to use this software for any purpose,
1163
+ // including commercial applications, and to alter it and redistribute it
1164
+ // freely, subject to the following restrictions:
1165
+ //
1166
+ // 1. The origin of this software must not be misrepresented; you must not
1167
+ // claim that you wrote the original software. If you use this software
1168
+ // in a product, an acknowledgment in the product documentation would be
1169
+ // appreciated but is not required.
1170
+ // 2. Altered source versions must be plainly marked as such, and must not be
1171
+ // misrepresented as being the original software.
1172
+ // 3. This notice may not be removed or altered from any source distribution.
1173
+ var constants$1 = {
1174
+ /* Allowed flush values; see deflate() and inflate() below for details */
1175
+ Z_NO_FLUSH: 0,
1176
+ Z_PARTIAL_FLUSH: 1,
1177
+ Z_SYNC_FLUSH: 2,
1178
+ Z_FULL_FLUSH: 3,
1179
+ Z_FINISH: 4,
1180
+ Z_BLOCK: 5,
1181
+ Z_TREES: 6,
1182
+ /* Return codes for the compression/decompression functions. Negative values
1183
+ * are errors, positive values are used for special but normal events.
1184
+ */
1185
+ Z_OK: 0,
1186
+ Z_STREAM_END: 1,
1187
+ Z_NEED_DICT: 2,
1188
+ Z_ERRNO: -1,
1189
+ Z_STREAM_ERROR: -2,
1190
+ Z_DATA_ERROR: -3,
1191
+ Z_MEM_ERROR: -4,
1192
+ Z_BUF_ERROR: -5,
1193
+ //Z_VERSION_ERROR: -6,
1194
+ /* compression levels */
1195
+ Z_NO_COMPRESSION: 0,
1196
+ Z_BEST_SPEED: 1,
1197
+ Z_BEST_COMPRESSION: 9,
1198
+ Z_DEFAULT_COMPRESSION: -1,
1199
+ Z_FILTERED: 1,
1200
+ Z_HUFFMAN_ONLY: 2,
1201
+ Z_RLE: 3,
1202
+ Z_FIXED: 4,
1203
+ Z_DEFAULT_STRATEGY: 0,
1204
+ /* Possible values of the data_type field (though see inflate()) */
1205
+ Z_BINARY: 0,
1206
+ Z_TEXT: 1,
1207
+ //Z_ASCII: 1, // = Z_TEXT (deprecated)
1208
+ Z_UNKNOWN: 2,
1209
+ /* The deflate compression method */
1210
+ Z_DEFLATED: 8,
1211
+ //Z_NULL: null // Use -1 or null inline, depending on var type
1212
+ };
1213
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
1214
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1215
+ //
1216
+ // This software is provided 'as-is', without any express or implied
1217
+ // warranty. In no event will the authors be held liable for any damages
1218
+ // arising from the use of this software.
1219
+ //
1220
+ // Permission is granted to anyone to use this software for any purpose,
1221
+ // including commercial applications, and to alter it and redistribute it
1222
+ // freely, subject to the following restrictions:
1223
+ //
1224
+ // 1. The origin of this software must not be misrepresented; you must not
1225
+ // claim that you wrote the original software. If you use this software
1226
+ // in a product, an acknowledgment in the product documentation would be
1227
+ // appreciated but is not required.
1228
+ // 2. Altered source versions must be plainly marked as such, and must not be
1229
+ // misrepresented as being the original software.
1230
+ // 3. This notice may not be removed or altered from any source distribution.
1231
+ const { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align, } = trees;
1232
+ /* Public constants ==========================================================*/
1233
+ /* ===========================================================================*/
1234
+ const { Z_NO_FLUSH: Z_NO_FLUSH$1, Z_PARTIAL_FLUSH, Z_FULL_FLUSH: Z_FULL_FLUSH$1, Z_FINISH: Z_FINISH$1, Z_BLOCK, Z_OK: Z_OK$1, Z_STREAM_END: Z_STREAM_END$1, Z_STREAM_ERROR, Z_DATA_ERROR, Z_BUF_ERROR, Z_DEFAULT_COMPRESSION: Z_DEFAULT_COMPRESSION$1, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, Z_FIXED, Z_DEFAULT_STRATEGY: Z_DEFAULT_STRATEGY$1, Z_UNKNOWN, Z_DEFLATED: Z_DEFLATED$1, } = constants$1;
1235
+ /*============================================================================*/
1236
+ const MAX_MEM_LEVEL = 9;
1237
+ /* Maximum value for memLevel in deflateInit2 */
1238
+ const MAX_WBITS = 15;
1239
+ /* 32K LZ77 window */
1240
+ const DEF_MEM_LEVEL = 8;
1241
+ const LENGTH_CODES = 29;
1242
+ /* number of length codes, not counting the special END_BLOCK code */
1243
+ const LITERALS = 256;
1244
+ /* number of literal bytes 0..255 */
1245
+ const L_CODES = LITERALS + 1 + LENGTH_CODES;
1246
+ /* number of Literal or Length codes, including the END_BLOCK code */
1247
+ const D_CODES = 30;
1248
+ /* number of distance codes */
1249
+ const BL_CODES = 19;
1250
+ /* number of codes used to transfer the bit lengths */
1251
+ const HEAP_SIZE = 2 * L_CODES + 1;
1252
+ /* maximum heap size */
1253
+ const MAX_BITS = 15;
1254
+ /* All codes must not exceed MAX_BITS bits */
1255
+ const MIN_MATCH = 3;
1256
+ const MAX_MATCH = 258;
1257
+ const MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;
1258
+ const PRESET_DICT = 0x20;
1259
+ const INIT_STATE = 42; /* zlib header -> BUSY_STATE */
1260
+ //#ifdef GZIP
1261
+ const GZIP_STATE = 57; /* gzip header -> BUSY_STATE | EXTRA_STATE */
1262
+ //#endif
1263
+ const EXTRA_STATE = 69; /* gzip extra block -> NAME_STATE */
1264
+ const NAME_STATE = 73; /* gzip file name -> COMMENT_STATE */
1265
+ const COMMENT_STATE = 91; /* gzip comment -> HCRC_STATE */
1266
+ const HCRC_STATE = 103; /* gzip header CRC -> BUSY_STATE */
1267
+ const BUSY_STATE = 113; /* deflate -> FINISH_STATE */
1268
+ const FINISH_STATE = 666; /* stream complete */
1269
+ const BS_NEED_MORE = 1; /* block not completed, need more input or more output */
1270
+ const BS_BLOCK_DONE = 2; /* block flush performed */
1271
+ const BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
1272
+ const BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
1273
+ const OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
1274
+ const err = (strm, errorCode) => {
1275
+ strm.msg = messages[errorCode];
1276
+ return errorCode;
1277
+ };
1278
+ const rank = (f) => {
1279
+ return f * 2 - (f > 4 ? 9 : 0);
1280
+ };
1281
+ const zero = (buf) => {
1282
+ let len = buf.length;
1283
+ while (--len >= 0) {
1284
+ buf[len] = 0;
1285
+ }
1286
+ };
1287
+ /* ===========================================================================
1288
+ * Slide the hash table when sliding the window down (could be avoided with 32
1289
+ * bit values at the expense of memory usage). We slide even when level == 0 to
1290
+ * keep the hash table consistent if we switch back to level > 0 later.
1291
+ */
1292
+ const slide_hash = (s) => {
1293
+ let n, m;
1294
+ let p;
1295
+ let wsize = s.w_size;
1296
+ n = s.hash_size;
1297
+ p = n;
1298
+ do {
1299
+ m = s.head[--p];
1300
+ s.head[p] = m >= wsize ? m - wsize : 0;
1301
+ } while (--n);
1302
+ n = wsize;
1303
+ //#ifndef FASTEST
1304
+ p = n;
1305
+ do {
1306
+ m = s.prev[--p];
1307
+ s.prev[p] = m >= wsize ? m - wsize : 0;
1308
+ /* If n is not on any hash chain, prev[n] is garbage but
1309
+ * its value will never be used.
1310
+ */
1311
+ } while (--n);
1312
+ //#endif
1313
+ };
1314
+ /* eslint-disable new-cap */
1315
+ let HASH_ZLIB = (s, prev, data) => ((prev << s.hash_shift) ^ data) & s.hash_mask;
1316
+ // This hash causes less collisions, https://github.com/nodeca/pako/issues/135
1317
+ // But breaks binary compatibility
1318
+ //let HASH_FAST = (s, prev, data) => ((prev << 8) + (prev >> 8) + (data << 4)) & s.hash_mask;
1319
+ let HASH = HASH_ZLIB;
1320
+ /* =========================================================================
1321
+ * Flush as much pending output as possible. All deflate() output, except for
1322
+ * some deflate_stored() output, goes through this function so some
1323
+ * applications may wish to modify it to avoid allocating a large
1324
+ * strm->next_out buffer and copying into it. (See also read_buf()).
1325
+ */
1326
+ const flush_pending = (strm) => {
1327
+ const s = strm.state;
1328
+ //_tr_flush_bits(s);
1329
+ let len = s.pending;
1330
+ if (len > strm.avail_out) {
1331
+ len = strm.avail_out;
1332
+ }
1333
+ if (len === 0) {
1334
+ return;
1335
+ }
1336
+ strm.output.set(s.pending_buf.subarray(s.pending_out, s.pending_out + len), strm.next_out);
1337
+ strm.next_out += len;
1338
+ s.pending_out += len;
1339
+ strm.total_out += len;
1340
+ strm.avail_out -= len;
1341
+ s.pending -= len;
1342
+ if (s.pending === 0) {
1343
+ s.pending_out = 0;
1344
+ }
1345
+ };
1346
+ const flush_block_only = (s, last) => {
1347
+ _tr_flush_block(s, s.block_start >= 0 ? s.block_start : -1, s.strstart - s.block_start, last);
1348
+ s.block_start = s.strstart;
1349
+ flush_pending(s.strm);
1350
+ };
1351
+ const put_byte = (s, b) => {
1352
+ s.pending_buf[s.pending++] = b;
1353
+ };
1354
+ /* =========================================================================
1355
+ * Put a short in the pending buffer. The 16-bit value is put in MSB order.
1356
+ * IN assertion: the stream state is correct and there is enough room in
1357
+ * pending_buf.
1358
+ */
1359
+ const putShortMSB = (s, b) => {
1360
+ // put_byte(s, (Byte)(b >> 8));
1361
+ // put_byte(s, (Byte)(b & 0xff));
1362
+ s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
1363
+ s.pending_buf[s.pending++] = b & 0xff;
1364
+ };
1365
+ /* ===========================================================================
1366
+ * Read a new buffer from the current input stream, update the adler32
1367
+ * and total number of bytes read. All deflate() input goes through
1368
+ * this function so some applications may wish to modify it to avoid
1369
+ * allocating a large strm->input buffer and copying from it.
1370
+ * (See also flush_pending()).
1371
+ */
1372
+ const read_buf = (strm, buf, start, size) => {
1373
+ let len = strm.avail_in;
1374
+ if (len > size) {
1375
+ len = size;
1376
+ }
1377
+ if (len === 0) {
1378
+ return 0;
1379
+ }
1380
+ strm.avail_in -= len;
1381
+ // zmemcpy(buf, strm->next_in, len);
1382
+ buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start);
1383
+ if (strm.state.wrap === 1) {
1384
+ strm.adler = adler32_1(strm.adler, buf, len, start);
1385
+ }
1386
+ else if (strm.state.wrap === 2) {
1387
+ strm.adler = crc32_1(strm.adler, buf, len, start);
1388
+ }
1389
+ strm.next_in += len;
1390
+ strm.total_in += len;
1391
+ return len;
1392
+ };
1393
+ /* ===========================================================================
1394
+ * Set match_start to the longest match starting at the given string and
1395
+ * return its length. Matches shorter or equal to prev_length are discarded,
1396
+ * in which case the result is equal to prev_length and match_start is
1397
+ * garbage.
1398
+ * IN assertions: cur_match is the head of the hash chain for the current
1399
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1400
+ * OUT assertion: the match length is not greater than s->lookahead.
1401
+ */
1402
+ const longest_match = (s, cur_match) => {
1403
+ let chain_length = s.max_chain_length; /* max hash chain length */
1404
+ let scan = s.strstart; /* current string */
1405
+ let match; /* matched string */
1406
+ let len; /* length of current match */
1407
+ let best_len = s.prev_length; /* best match length so far */
1408
+ let nice_match = s.nice_match; /* stop if match long enough */
1409
+ const limit = s.strstart > s.w_size - MIN_LOOKAHEAD
1410
+ ? s.strstart - (s.w_size - MIN_LOOKAHEAD)
1411
+ : 0; /*NIL*/
1412
+ const _win = s.window; // shortcut
1413
+ const wmask = s.w_mask;
1414
+ const prev = s.prev;
1415
+ /* Stop when cur_match becomes <= limit. To simplify the code,
1416
+ * we prevent matches with the string of window index 0.
1417
+ */
1418
+ const strend = s.strstart + MAX_MATCH;
1419
+ let scan_end1 = _win[scan + best_len - 1];
1420
+ let scan_end = _win[scan + best_len];
1421
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1422
+ * It is easy to get rid of this optimization if necessary.
1423
+ */
1424
+ // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1425
+ /* Do not waste too much time if we already have a good match: */
1426
+ if (s.prev_length >= s.good_match) {
1427
+ chain_length >>= 2;
1428
+ }
1429
+ /* Do not look for matches beyond the end of the input. This is necessary
1430
+ * to make deflate deterministic.
1431
+ */
1432
+ if (nice_match > s.lookahead) {
1433
+ nice_match = s.lookahead;
1434
+ }
1435
+ // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1436
+ do {
1437
+ // Assert(cur_match < s->strstart, "no future");
1438
+ match = cur_match;
1439
+ /* Skip to next match if the match length cannot increase
1440
+ * or if the match length is less than 2. Note that the checks below
1441
+ * for insufficient lookahead only occur occasionally for performance
1442
+ * reasons. Therefore uninitialized memory will be accessed, and
1443
+ * conditional jumps will be made that depend on those values.
1444
+ * However the length of the match is limited to the lookahead, so
1445
+ * the output of deflate is not affected by the uninitialized values.
1446
+ */
1447
+ if (_win[match + best_len] !== scan_end ||
1448
+ _win[match + best_len - 1] !== scan_end1 ||
1449
+ _win[match] !== _win[scan] ||
1450
+ _win[++match] !== _win[scan + 1]) {
1451
+ continue;
1452
+ }
1453
+ /* The check at best_len-1 can be removed because it will be made
1454
+ * again later. (This heuristic is not always a win.)
1455
+ * It is not necessary to compare scan[2] and match[2] since they
1456
+ * are always equal when the other bytes match, given that
1457
+ * the hash keys are equal and that HASH_BITS >= 8.
1458
+ */
1459
+ scan += 2;
1460
+ match++;
1461
+ // Assert(*scan == *match, "match[2]?");
1462
+ /* We check for insufficient lookahead only every 8th comparison;
1463
+ * the 256th check will be made at strstart+258.
1464
+ */
1465
+ do {
1466
+ /*jshint noempty:false*/
1467
+ } while (_win[++scan] === _win[++match] &&
1468
+ _win[++scan] === _win[++match] &&
1469
+ _win[++scan] === _win[++match] &&
1470
+ _win[++scan] === _win[++match] &&
1471
+ _win[++scan] === _win[++match] &&
1472
+ _win[++scan] === _win[++match] &&
1473
+ _win[++scan] === _win[++match] &&
1474
+ _win[++scan] === _win[++match] &&
1475
+ scan < strend);
1476
+ // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1477
+ len = MAX_MATCH - (strend - scan);
1478
+ scan = strend - MAX_MATCH;
1479
+ if (len > best_len) {
1480
+ s.match_start = cur_match;
1481
+ best_len = len;
1482
+ if (len >= nice_match) {
1483
+ break;
1484
+ }
1485
+ scan_end1 = _win[scan + best_len - 1];
1486
+ scan_end = _win[scan + best_len];
1487
+ }
1488
+ } while ((cur_match = prev[cur_match & wmask]) > limit &&
1489
+ --chain_length !== 0);
1490
+ if (best_len <= s.lookahead) {
1491
+ return best_len;
1492
+ }
1493
+ return s.lookahead;
1494
+ };
1495
+ /* ===========================================================================
1496
+ * Fill the window when the lookahead becomes insufficient.
1497
+ * Updates strstart and lookahead.
1498
+ *
1499
+ * IN assertion: lookahead < MIN_LOOKAHEAD
1500
+ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1501
+ * At least one byte has been read, or avail_in == 0; reads are
1502
+ * performed for at least two bytes (required for the zip translate_eol
1503
+ * option -- not supported here).
1504
+ */
1505
+ const fill_window = (s) => {
1506
+ const _w_size = s.w_size;
1507
+ let n, more, str;
1508
+ //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1509
+ do {
1510
+ more = s.window_size - s.lookahead - s.strstart;
1511
+ // JS ints have 32 bit, block below not needed
1512
+ /* Deal with !@#$% 64K limit: */
1513
+ //if (sizeof(int) <= 2) {
1514
+ // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1515
+ // more = wsize;
1516
+ //
1517
+ // } else if (more == (unsigned)(-1)) {
1518
+ // /* Very unlikely, but possible on 16 bit machine if
1519
+ // * strstart == 0 && lookahead == 1 (input done a byte at time)
1520
+ // */
1521
+ // more--;
1522
+ // }
1523
+ //}
1524
+ /* If the window is almost full and there is insufficient lookahead,
1525
+ * move the upper half to the lower one to make room in the upper half.
1526
+ */
1527
+ if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
1528
+ s.window.set(s.window.subarray(_w_size, _w_size + _w_size - more), 0);
1529
+ s.match_start -= _w_size;
1530
+ s.strstart -= _w_size;
1531
+ /* we now have strstart >= MAX_DIST */
1532
+ s.block_start -= _w_size;
1533
+ if (s.insert > s.strstart) {
1534
+ s.insert = s.strstart;
1535
+ }
1536
+ slide_hash(s);
1537
+ more += _w_size;
1538
+ }
1539
+ if (s.strm.avail_in === 0) {
1540
+ break;
1541
+ }
1542
+ /* If there was no sliding:
1543
+ * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1544
+ * more == window_size - lookahead - strstart
1545
+ * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1546
+ * => more >= window_size - 2*WSIZE + 2
1547
+ * In the BIG_MEM or MMAP case (not yet supported),
1548
+ * window_size == input_size + MIN_LOOKAHEAD &&
1549
+ * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1550
+ * Otherwise, window_size == 2*WSIZE so more >= 2.
1551
+ * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1552
+ */
1553
+ //Assert(more >= 2, "more < 2");
1554
+ n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
1555
+ s.lookahead += n;
1556
+ /* Initialize the hash value now that we have some input: */
1557
+ if (s.lookahead + s.insert >= MIN_MATCH) {
1558
+ str = s.strstart - s.insert;
1559
+ s.ins_h = s.window[str];
1560
+ /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
1561
+ s.ins_h = HASH(s, s.ins_h, s.window[str + 1]);
1562
+ //#if MIN_MATCH != 3
1563
+ // Call update_hash() MIN_MATCH-3 more times
1564
+ //#endif
1565
+ while (s.insert) {
1566
+ /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
1567
+ s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
1568
+ s.prev[str & s.w_mask] = s.head[s.ins_h];
1569
+ s.head[s.ins_h] = str;
1570
+ str++;
1571
+ s.insert--;
1572
+ if (s.lookahead + s.insert < MIN_MATCH) {
1573
+ break;
1574
+ }
1575
+ }
1576
+ }
1577
+ /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1578
+ * but this is not important since only literal bytes will be emitted.
1579
+ */
1580
+ } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
1581
+ /* If the WIN_INIT bytes after the end of the current data have never been
1582
+ * written, then zero those bytes in order to avoid memory check reports of
1583
+ * the use of uninitialized (or uninitialised as Julian writes) bytes by
1584
+ * the longest match routines. Update the high water mark for the next
1585
+ * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1586
+ * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1587
+ */
1588
+ // if (s.high_water < s.window_size) {
1589
+ // const curr = s.strstart + s.lookahead;
1590
+ // let init = 0;
1591
+ //
1592
+ // if (s.high_water < curr) {
1593
+ // /* Previous high water mark below current data -- zero WIN_INIT
1594
+ // * bytes or up to end of window, whichever is less.
1595
+ // */
1596
+ // init = s.window_size - curr;
1597
+ // if (init > WIN_INIT)
1598
+ // init = WIN_INIT;
1599
+ // zmemzero(s->window + curr, (unsigned)init);
1600
+ // s->high_water = curr + init;
1601
+ // }
1602
+ // else if (s->high_water < (ulg)curr + WIN_INIT) {
1603
+ // /* High water mark at or above current data, but below current data
1604
+ // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1605
+ // * to end of window, whichever is less.
1606
+ // */
1607
+ // init = (ulg)curr + WIN_INIT - s->high_water;
1608
+ // if (init > s->window_size - s->high_water)
1609
+ // init = s->window_size - s->high_water;
1610
+ // zmemzero(s->window + s->high_water, (unsigned)init);
1611
+ // s->high_water += init;
1612
+ // }
1613
+ // }
1614
+ //
1615
+ // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1616
+ // "not enough room for search");
1617
+ };
1618
+ /* ===========================================================================
1619
+ * Copy without compression as much as possible from the input stream, return
1620
+ * the current block state.
1621
+ *
1622
+ * In case deflateParams() is used to later switch to a non-zero compression
1623
+ * level, s->matches (otherwise unused when storing) keeps track of the number
1624
+ * of hash table slides to perform. If s->matches is 1, then one hash table
1625
+ * slide will be done when switching. If s->matches is 2, the maximum value
1626
+ * allowed here, then the hash table will be cleared, since two or more slides
1627
+ * is the same as a clear.
1628
+ *
1629
+ * deflate_stored() is written to minimize the number of times an input byte is
1630
+ * copied. It is most efficient with large input and output buffers, which
1631
+ * maximizes the opportunites to have a single copy from next_in to next_out.
1632
+ */
1633
+ const deflate_stored = (s, flush) => {
1634
+ /* Smallest worthy block size when not flushing or finishing. By default
1635
+ * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1636
+ * large input and output buffers, the stored block size will be larger.
1637
+ */
1638
+ let min_block = s.pending_buf_size - 5 > s.w_size ? s.w_size : s.pending_buf_size - 5;
1639
+ /* Copy as many min_block or larger stored blocks directly to next_out as
1640
+ * possible. If flushing, copy the remaining available input to next_out as
1641
+ * stored blocks, if there is enough space.
1642
+ */
1643
+ let len, left, have, last = 0;
1644
+ let used = s.strm.avail_in;
1645
+ do {
1646
+ /* Set len to the maximum size block that we can copy directly with the
1647
+ * available input data and output space. Set left to how much of that
1648
+ * would be copied from what's left in the window.
1649
+ */
1650
+ len = 65535 /* MAX_STORED */; /* maximum deflate stored block length */
1651
+ have = (s.bi_valid + 42) >> 3; /* number of header bytes */
1652
+ if (s.strm.avail_out < have) {
1653
+ /* need room for header */
1654
+ break;
1655
+ }
1656
+ /* maximum stored block length that will fit in avail_out: */
1657
+ have = s.strm.avail_out - have;
1658
+ left = s.strstart - s.block_start; /* bytes left in window */
1659
+ if (len > left + s.strm.avail_in) {
1660
+ len = left + s.strm.avail_in; /* limit len to the input */
1661
+ }
1662
+ if (len > have) {
1663
+ len = have; /* limit len to the output */
1664
+ }
1665
+ /* If the stored block would be less than min_block in length, or if
1666
+ * unable to copy all of the available input when flushing, then try
1667
+ * copying to the window and the pending buffer instead. Also don't
1668
+ * write an empty block when flushing -- deflate() does that.
1669
+ */
1670
+ if (len < min_block &&
1671
+ ((len === 0 && flush !== Z_FINISH$1) ||
1672
+ flush === Z_NO_FLUSH$1 ||
1673
+ len !== left + s.strm.avail_in)) {
1674
+ break;
1675
+ }
1676
+ /* Make a dummy stored block in pending to get the header bytes,
1677
+ * including any pending bits. This also updates the debugging counts.
1678
+ */
1679
+ last = flush === Z_FINISH$1 && len === left + s.strm.avail_in ? 1 : 0;
1680
+ _tr_stored_block(s, 0, 0, last);
1681
+ /* Replace the lengths in the dummy stored block with len. */
1682
+ s.pending_buf[s.pending - 4] = len;
1683
+ s.pending_buf[s.pending - 3] = len >> 8;
1684
+ s.pending_buf[s.pending - 2] = ~len;
1685
+ s.pending_buf[s.pending - 1] = ~len >> 8;
1686
+ /* Write the stored block header bytes. */
1687
+ flush_pending(s.strm);
1688
+ //#ifdef ZLIB_DEBUG
1689
+ // /* Update debugging counts for the data about to be copied. */
1690
+ // s->compressed_len += len << 3;
1691
+ // s->bits_sent += len << 3;
1692
+ //#endif
1693
+ /* Copy uncompressed bytes from the window to next_out. */
1694
+ if (left) {
1695
+ if (left > len) {
1696
+ left = len;
1697
+ }
1698
+ //zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1699
+ s.strm.output.set(s.window.subarray(s.block_start, s.block_start + left), s.strm.next_out);
1700
+ s.strm.next_out += left;
1701
+ s.strm.avail_out -= left;
1702
+ s.strm.total_out += left;
1703
+ s.block_start += left;
1704
+ len -= left;
1705
+ }
1706
+ /* Copy uncompressed bytes directly from next_in to next_out, updating
1707
+ * the check value.
1708
+ */
1709
+ if (len) {
1710
+ read_buf(s.strm, s.strm.output, s.strm.next_out, len);
1711
+ s.strm.next_out += len;
1712
+ s.strm.avail_out -= len;
1713
+ s.strm.total_out += len;
1714
+ }
1715
+ } while (last === 0);
1716
+ /* Update the sliding window with the last s->w_size bytes of the copied
1717
+ * data, or append all of the copied data to the existing window if less
1718
+ * than s->w_size bytes were copied. Also update the number of bytes to
1719
+ * insert in the hash tables, in the event that deflateParams() switches to
1720
+ * a non-zero compression level.
1721
+ */
1722
+ used -= s.strm.avail_in; /* number of input bytes directly copied */
1723
+ if (used) {
1724
+ /* If any input was used, then no unused input remains in the window,
1725
+ * therefore s->block_start == s->strstart.
1726
+ */
1727
+ if (used >= s.w_size) {
1728
+ /* supplant the previous history */
1729
+ s.matches = 2; /* clear hash */
1730
+ //zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1731
+ s.window.set(s.strm.input.subarray(s.strm.next_in - s.w_size, s.strm.next_in), 0);
1732
+ s.strstart = s.w_size;
1733
+ s.insert = s.strstart;
1734
+ }
1735
+ else {
1736
+ if (s.window_size - s.strstart <= used) {
1737
+ /* Slide the window down. */
1738
+ s.strstart -= s.w_size;
1739
+ //zmemcpy(s->window, s->window + s->w_size, s->strstart);
1740
+ s.window.set(s.window.subarray(s.w_size, s.w_size + s.strstart), 0);
1741
+ if (s.matches < 2) {
1742
+ s.matches++; /* add a pending slide_hash() */
1743
+ }
1744
+ if (s.insert > s.strstart) {
1745
+ s.insert = s.strstart;
1746
+ }
1747
+ }
1748
+ //zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1749
+ s.window.set(s.strm.input.subarray(s.strm.next_in - used, s.strm.next_in), s.strstart);
1750
+ s.strstart += used;
1751
+ s.insert += used > s.w_size - s.insert ? s.w_size - s.insert : used;
1752
+ }
1753
+ s.block_start = s.strstart;
1754
+ }
1755
+ if (s.high_water < s.strstart) {
1756
+ s.high_water = s.strstart;
1757
+ }
1758
+ /* If the last block was written to next_out, then done. */
1759
+ if (last) {
1760
+ return BS_FINISH_DONE;
1761
+ }
1762
+ /* If flushing and all input has been consumed, then done. */
1763
+ if (flush !== Z_NO_FLUSH$1 &&
1764
+ flush !== Z_FINISH$1 &&
1765
+ s.strm.avail_in === 0 &&
1766
+ s.strstart === s.block_start) {
1767
+ return BS_BLOCK_DONE;
1768
+ }
1769
+ /* Fill the window with any remaining input. */
1770
+ have = s.window_size - s.strstart;
1771
+ if (s.strm.avail_in > have && s.block_start >= s.w_size) {
1772
+ /* Slide the window down. */
1773
+ s.block_start -= s.w_size;
1774
+ s.strstart -= s.w_size;
1775
+ //zmemcpy(s->window, s->window + s->w_size, s->strstart);
1776
+ s.window.set(s.window.subarray(s.w_size, s.w_size + s.strstart), 0);
1777
+ if (s.matches < 2) {
1778
+ s.matches++; /* add a pending slide_hash() */
1779
+ }
1780
+ have += s.w_size; /* more space now */
1781
+ if (s.insert > s.strstart) {
1782
+ s.insert = s.strstart;
1783
+ }
1784
+ }
1785
+ if (have > s.strm.avail_in) {
1786
+ have = s.strm.avail_in;
1787
+ }
1788
+ if (have) {
1789
+ read_buf(s.strm, s.window, s.strstart, have);
1790
+ s.strstart += have;
1791
+ s.insert += have > s.w_size - s.insert ? s.w_size - s.insert : have;
1792
+ }
1793
+ if (s.high_water < s.strstart) {
1794
+ s.high_water = s.strstart;
1795
+ }
1796
+ /* There was not enough avail_out to write a complete worthy or flushed
1797
+ * stored block to next_out. Write a stored block to pending instead, if we
1798
+ * have enough input for a worthy block, or if flushing and there is enough
1799
+ * room for the remaining input as a stored block in the pending buffer.
1800
+ */
1801
+ have = (s.bi_valid + 42) >> 3; /* number of header bytes */
1802
+ /* maximum stored block length that will fit in pending: */
1803
+ have =
1804
+ s.pending_buf_size - have > 65535 /* MAX_STORED */
1805
+ ? 65535 /* MAX_STORED */
1806
+ : s.pending_buf_size - have;
1807
+ min_block = have > s.w_size ? s.w_size : have;
1808
+ left = s.strstart - s.block_start;
1809
+ if (left >= min_block ||
1810
+ ((left || flush === Z_FINISH$1) &&
1811
+ flush !== Z_NO_FLUSH$1 &&
1812
+ s.strm.avail_in === 0 &&
1813
+ left <= have)) {
1814
+ len = left > have ? have : left;
1815
+ last =
1816
+ flush === Z_FINISH$1 && s.strm.avail_in === 0 && len === left ? 1 : 0;
1817
+ _tr_stored_block(s, s.block_start, len, last);
1818
+ s.block_start += len;
1819
+ flush_pending(s.strm);
1820
+ }
1821
+ /* We've done all we can with the available input and output. */
1822
+ return last ? BS_FINISH_STARTED : BS_NEED_MORE;
1823
+ };
1824
+ /* ===========================================================================
1825
+ * Compress as much as possible from the input stream, return the current
1826
+ * block state.
1827
+ * This function does not perform lazy evaluation of matches and inserts
1828
+ * new strings in the dictionary only for unmatched strings or for short
1829
+ * matches. It is used only for the fast compression options.
1830
+ */
1831
+ const deflate_fast = (s, flush) => {
1832
+ let hash_head; /* head of the hash chain */
1833
+ let bflush; /* set if current block must be flushed */
1834
+ for (;;) {
1835
+ /* Make sure that we always have enough lookahead, except
1836
+ * at the end of the input file. We need MAX_MATCH bytes
1837
+ * for the next match, plus MIN_MATCH bytes to insert the
1838
+ * string following the next match.
1839
+ */
1840
+ if (s.lookahead < MIN_LOOKAHEAD) {
1841
+ fill_window(s);
1842
+ if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$1) {
1843
+ return BS_NEED_MORE;
1844
+ }
1845
+ if (s.lookahead === 0) {
1846
+ break; /* flush the current block */
1847
+ }
1848
+ }
1849
+ /* Insert the string window[strstart .. strstart+2] in the
1850
+ * dictionary, and set hash_head to the head of the hash chain:
1851
+ */
1852
+ hash_head = 0 /*NIL*/;
1853
+ if (s.lookahead >= MIN_MATCH) {
1854
+ /*** INSERT_STRING(s, s.strstart, hash_head); ***/
1855
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
1856
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
1857
+ s.head[s.ins_h] = s.strstart;
1858
+ /***/
1859
+ }
1860
+ /* Find the longest match, discarding those <= prev_length.
1861
+ * At this point we have always match_length < MIN_MATCH
1862
+ */
1863
+ if (hash_head !== 0 /*NIL*/ &&
1864
+ s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD) {
1865
+ /* To simplify the code, we prevent matches with the string
1866
+ * of window index 0 (in particular we have to avoid a match
1867
+ * of the string with itself at the start of the input file).
1868
+ */
1869
+ s.match_length = longest_match(s, hash_head);
1870
+ /* longest_match() sets match_start */
1871
+ }
1872
+ if (s.match_length >= MIN_MATCH) {
1873
+ // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
1874
+ /*** _tr_tally_dist(s, s.strstart - s.match_start,
1875
+ s.match_length - MIN_MATCH, bflush); ***/
1876
+ bflush = _tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
1877
+ s.lookahead -= s.match_length;
1878
+ /* Insert new strings in the hash table only if the match length
1879
+ * is not too large. This saves time but degrades compression.
1880
+ */
1881
+ if (s.match_length <= s.max_lazy_match /*max_insert_length*/ &&
1882
+ s.lookahead >= MIN_MATCH) {
1883
+ s.match_length--; /* string at strstart already in table */
1884
+ do {
1885
+ s.strstart++;
1886
+ /*** INSERT_STRING(s, s.strstart, hash_head); ***/
1887
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
1888
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
1889
+ s.head[s.ins_h] = s.strstart;
1890
+ /***/
1891
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1892
+ * always MIN_MATCH bytes ahead.
1893
+ */
1894
+ } while (--s.match_length !== 0);
1895
+ s.strstart++;
1896
+ }
1897
+ else {
1898
+ s.strstart += s.match_length;
1899
+ s.match_length = 0;
1900
+ s.ins_h = s.window[s.strstart];
1901
+ /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
1902
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + 1]);
1903
+ //#if MIN_MATCH != 3
1904
+ // Call UPDATE_HASH() MIN_MATCH-3 more times
1905
+ //#endif
1906
+ /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1907
+ * matter since it will be recomputed at next deflate call.
1908
+ */
1909
+ }
1910
+ }
1911
+ else {
1912
+ /* No match, output a literal byte */
1913
+ //Tracevv((stderr,"%c", s.window[s.strstart]));
1914
+ /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
1915
+ bflush = _tr_tally(s, 0, s.window[s.strstart]);
1916
+ s.lookahead--;
1917
+ s.strstart++;
1918
+ }
1919
+ if (bflush) {
1920
+ /*** FLUSH_BLOCK(s, 0); ***/
1921
+ flush_block_only(s, false);
1922
+ if (s.strm.avail_out === 0) {
1923
+ return BS_NEED_MORE;
1924
+ }
1925
+ /***/
1926
+ }
1927
+ }
1928
+ s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
1929
+ if (flush === Z_FINISH$1) {
1930
+ /*** FLUSH_BLOCK(s, 1); ***/
1931
+ flush_block_only(s, true);
1932
+ if (s.strm.avail_out === 0) {
1933
+ return BS_FINISH_STARTED;
1934
+ }
1935
+ /***/
1936
+ return BS_FINISH_DONE;
1937
+ }
1938
+ if (s.sym_next) {
1939
+ /*** FLUSH_BLOCK(s, 0); ***/
1940
+ flush_block_only(s, false);
1941
+ if (s.strm.avail_out === 0) {
1942
+ return BS_NEED_MORE;
1943
+ }
1944
+ /***/
1945
+ }
1946
+ return BS_BLOCK_DONE;
1947
+ };
1948
+ /* ===========================================================================
1949
+ * Same as above, but achieves better compression. We use a lazy
1950
+ * evaluation for matches: a match is finally adopted only if there is
1951
+ * no better match at the next window position.
1952
+ */
1953
+ const deflate_slow = (s, flush) => {
1954
+ let hash_head; /* head of hash chain */
1955
+ let bflush; /* set if current block must be flushed */
1956
+ let max_insert;
1957
+ /* Process the input block. */
1958
+ for (;;) {
1959
+ /* Make sure that we always have enough lookahead, except
1960
+ * at the end of the input file. We need MAX_MATCH bytes
1961
+ * for the next match, plus MIN_MATCH bytes to insert the
1962
+ * string following the next match.
1963
+ */
1964
+ if (s.lookahead < MIN_LOOKAHEAD) {
1965
+ fill_window(s);
1966
+ if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$1) {
1967
+ return BS_NEED_MORE;
1968
+ }
1969
+ if (s.lookahead === 0) {
1970
+ break;
1971
+ } /* flush the current block */
1972
+ }
1973
+ /* Insert the string window[strstart .. strstart+2] in the
1974
+ * dictionary, and set hash_head to the head of the hash chain:
1975
+ */
1976
+ hash_head = 0 /*NIL*/;
1977
+ if (s.lookahead >= MIN_MATCH) {
1978
+ /*** INSERT_STRING(s, s.strstart, hash_head); ***/
1979
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
1980
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
1981
+ s.head[s.ins_h] = s.strstart;
1982
+ /***/
1983
+ }
1984
+ /* Find the longest match, discarding those <= prev_length.
1985
+ */
1986
+ s.prev_length = s.match_length;
1987
+ s.prev_match = s.match_start;
1988
+ s.match_length = MIN_MATCH - 1;
1989
+ if (hash_head !== 0 /*NIL*/ &&
1990
+ s.prev_length < s.max_lazy_match &&
1991
+ s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD /*MAX_DIST(s)*/) {
1992
+ /* To simplify the code, we prevent matches with the string
1993
+ * of window index 0 (in particular we have to avoid a match
1994
+ * of the string with itself at the start of the input file).
1995
+ */
1996
+ s.match_length = longest_match(s, hash_head);
1997
+ /* longest_match() sets match_start */
1998
+ if (s.match_length <= 5 &&
1999
+ (s.strategy === Z_FILTERED ||
2000
+ (s.match_length === MIN_MATCH &&
2001
+ s.strstart - s.match_start > 4096)) /*TOO_FAR*/) {
2002
+ /* If prev_match is also MIN_MATCH, match_start is garbage
2003
+ * but we will ignore the current match anyway.
2004
+ */
2005
+ s.match_length = MIN_MATCH - 1;
2006
+ }
2007
+ }
2008
+ /* If there was a match at the previous step and the current
2009
+ * match is not better, output the previous match:
2010
+ */
2011
+ if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
2012
+ max_insert = s.strstart + s.lookahead - MIN_MATCH;
2013
+ /* Do not insert strings in hash table beyond this. */
2014
+ //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
2015
+ /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
2016
+ s.prev_length - MIN_MATCH, bflush);***/
2017
+ bflush = _tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
2018
+ /* Insert in hash table all strings up to the end of the match.
2019
+ * strstart-1 and strstart are already inserted. If there is not
2020
+ * enough lookahead, the last two strings are not inserted in
2021
+ * the hash table.
2022
+ */
2023
+ s.lookahead -= s.prev_length - 1;
2024
+ s.prev_length -= 2;
2025
+ do {
2026
+ if (++s.strstart <= max_insert) {
2027
+ /*** INSERT_STRING(s, s.strstart, hash_head); ***/
2028
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
2029
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
2030
+ s.head[s.ins_h] = s.strstart;
2031
+ /***/
2032
+ }
2033
+ } while (--s.prev_length !== 0);
2034
+ s.match_available = 0;
2035
+ s.match_length = MIN_MATCH - 1;
2036
+ s.strstart++;
2037
+ if (bflush) {
2038
+ /*** FLUSH_BLOCK(s, 0); ***/
2039
+ flush_block_only(s, false);
2040
+ if (s.strm.avail_out === 0) {
2041
+ return BS_NEED_MORE;
2042
+ }
2043
+ /***/
2044
+ }
2045
+ }
2046
+ else if (s.match_available) {
2047
+ /* If there was no match at the previous position, output a
2048
+ * single literal. If there was a match but the current match
2049
+ * is longer, truncate the previous match to a single literal.
2050
+ */
2051
+ //Tracevv((stderr,"%c", s->window[s->strstart-1]));
2052
+ /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
2053
+ bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
2054
+ if (bflush) {
2055
+ /*** FLUSH_BLOCK_ONLY(s, 0) ***/
2056
+ flush_block_only(s, false);
2057
+ /***/
2058
+ }
2059
+ s.strstart++;
2060
+ s.lookahead--;
2061
+ if (s.strm.avail_out === 0) {
2062
+ return BS_NEED_MORE;
2063
+ }
2064
+ }
2065
+ else {
2066
+ /* There is no previous match to compare with, wait for
2067
+ * the next step to decide.
2068
+ */
2069
+ s.match_available = 1;
2070
+ s.strstart++;
2071
+ s.lookahead--;
2072
+ }
2073
+ }
2074
+ //Assert (flush != Z_NO_FLUSH, "no flush?");
2075
+ if (s.match_available) {
2076
+ //Tracevv((stderr,"%c", s->window[s->strstart-1]));
2077
+ /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
2078
+ bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
2079
+ s.match_available = 0;
2080
+ }
2081
+ s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
2082
+ if (flush === Z_FINISH$1) {
2083
+ /*** FLUSH_BLOCK(s, 1); ***/
2084
+ flush_block_only(s, true);
2085
+ if (s.strm.avail_out === 0) {
2086
+ return BS_FINISH_STARTED;
2087
+ }
2088
+ /***/
2089
+ return BS_FINISH_DONE;
2090
+ }
2091
+ if (s.sym_next) {
2092
+ /*** FLUSH_BLOCK(s, 0); ***/
2093
+ flush_block_only(s, false);
2094
+ if (s.strm.avail_out === 0) {
2095
+ return BS_NEED_MORE;
2096
+ }
2097
+ /***/
2098
+ }
2099
+ return BS_BLOCK_DONE;
2100
+ };
2101
+ /* ===========================================================================
2102
+ * For Z_RLE, simply look for runs of bytes, generate matches only of distance
2103
+ * one. Do not maintain a hash table. (It will be regenerated if this run of
2104
+ * deflate switches away from Z_RLE.)
2105
+ */
2106
+ const deflate_rle = (s, flush) => {
2107
+ let bflush; /* set if current block must be flushed */
2108
+ let prev; /* byte at distance one to match */
2109
+ let scan, strend; /* scan goes up to strend for length of run */
2110
+ const _win = s.window;
2111
+ for (;;) {
2112
+ /* Make sure that we always have enough lookahead, except
2113
+ * at the end of the input file. We need MAX_MATCH bytes
2114
+ * for the longest run, plus one for the unrolled loop.
2115
+ */
2116
+ if (s.lookahead <= MAX_MATCH) {
2117
+ fill_window(s);
2118
+ if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH$1) {
2119
+ return BS_NEED_MORE;
2120
+ }
2121
+ if (s.lookahead === 0) {
2122
+ break;
2123
+ } /* flush the current block */
2124
+ }
2125
+ /* See how many times the previous byte repeats */
2126
+ s.match_length = 0;
2127
+ if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
2128
+ scan = s.strstart - 1;
2129
+ prev = _win[scan];
2130
+ if (prev === _win[++scan] &&
2131
+ prev === _win[++scan] &&
2132
+ prev === _win[++scan]) {
2133
+ strend = s.strstart + MAX_MATCH;
2134
+ do {
2135
+ /*jshint noempty:false*/
2136
+ } while (prev === _win[++scan] &&
2137
+ prev === _win[++scan] &&
2138
+ prev === _win[++scan] &&
2139
+ prev === _win[++scan] &&
2140
+ prev === _win[++scan] &&
2141
+ prev === _win[++scan] &&
2142
+ prev === _win[++scan] &&
2143
+ prev === _win[++scan] &&
2144
+ scan < strend);
2145
+ s.match_length = MAX_MATCH - (strend - scan);
2146
+ if (s.match_length > s.lookahead) {
2147
+ s.match_length = s.lookahead;
2148
+ }
2149
+ }
2150
+ //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
2151
+ }
2152
+ /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2153
+ if (s.match_length >= MIN_MATCH) {
2154
+ //check_match(s, s.strstart, s.strstart - 1, s.match_length);
2155
+ /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
2156
+ bflush = _tr_tally(s, 1, s.match_length - MIN_MATCH);
2157
+ s.lookahead -= s.match_length;
2158
+ s.strstart += s.match_length;
2159
+ s.match_length = 0;
2160
+ }
2161
+ else {
2162
+ /* No match, output a literal byte */
2163
+ //Tracevv((stderr,"%c", s->window[s->strstart]));
2164
+ /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
2165
+ bflush = _tr_tally(s, 0, s.window[s.strstart]);
2166
+ s.lookahead--;
2167
+ s.strstart++;
2168
+ }
2169
+ if (bflush) {
2170
+ /*** FLUSH_BLOCK(s, 0); ***/
2171
+ flush_block_only(s, false);
2172
+ if (s.strm.avail_out === 0) {
2173
+ return BS_NEED_MORE;
2174
+ }
2175
+ /***/
2176
+ }
2177
+ }
2178
+ s.insert = 0;
2179
+ if (flush === Z_FINISH$1) {
2180
+ /*** FLUSH_BLOCK(s, 1); ***/
2181
+ flush_block_only(s, true);
2182
+ if (s.strm.avail_out === 0) {
2183
+ return BS_FINISH_STARTED;
2184
+ }
2185
+ /***/
2186
+ return BS_FINISH_DONE;
2187
+ }
2188
+ if (s.sym_next) {
2189
+ /*** FLUSH_BLOCK(s, 0); ***/
2190
+ flush_block_only(s, false);
2191
+ if (s.strm.avail_out === 0) {
2192
+ return BS_NEED_MORE;
2193
+ }
2194
+ /***/
2195
+ }
2196
+ return BS_BLOCK_DONE;
2197
+ };
2198
+ /* ===========================================================================
2199
+ * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
2200
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
2201
+ */
2202
+ const deflate_huff = (s, flush) => {
2203
+ let bflush; /* set if current block must be flushed */
2204
+ for (;;) {
2205
+ /* Make sure that we have a literal to write. */
2206
+ if (s.lookahead === 0) {
2207
+ fill_window(s);
2208
+ if (s.lookahead === 0) {
2209
+ if (flush === Z_NO_FLUSH$1) {
2210
+ return BS_NEED_MORE;
2211
+ }
2212
+ break; /* flush the current block */
2213
+ }
2214
+ }
2215
+ /* Output a literal byte */
2216
+ s.match_length = 0;
2217
+ //Tracevv((stderr,"%c", s->window[s->strstart]));
2218
+ /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
2219
+ bflush = _tr_tally(s, 0, s.window[s.strstart]);
2220
+ s.lookahead--;
2221
+ s.strstart++;
2222
+ if (bflush) {
2223
+ /*** FLUSH_BLOCK(s, 0); ***/
2224
+ flush_block_only(s, false);
2225
+ if (s.strm.avail_out === 0) {
2226
+ return BS_NEED_MORE;
2227
+ }
2228
+ /***/
2229
+ }
2230
+ }
2231
+ s.insert = 0;
2232
+ if (flush === Z_FINISH$1) {
2233
+ /*** FLUSH_BLOCK(s, 1); ***/
2234
+ flush_block_only(s, true);
2235
+ if (s.strm.avail_out === 0) {
2236
+ return BS_FINISH_STARTED;
2237
+ }
2238
+ /***/
2239
+ return BS_FINISH_DONE;
2240
+ }
2241
+ if (s.sym_next) {
2242
+ /*** FLUSH_BLOCK(s, 0); ***/
2243
+ flush_block_only(s, false);
2244
+ if (s.strm.avail_out === 0) {
2245
+ return BS_NEED_MORE;
2246
+ }
2247
+ /***/
2248
+ }
2249
+ return BS_BLOCK_DONE;
2250
+ };
2251
+ /* Values for max_lazy_match, good_match and max_chain_length, depending on
2252
+ * the desired pack level (0..9). The values given below have been tuned to
2253
+ * exclude worst case performance for pathological files. Better values may be
2254
+ * found for specific files.
2255
+ */
2256
+ function Config(good_length, max_lazy, nice_length, max_chain, func) {
2257
+ this.good_length = good_length;
2258
+ this.max_lazy = max_lazy;
2259
+ this.nice_length = nice_length;
2260
+ this.max_chain = max_chain;
2261
+ this.func = func;
2262
+ }
2263
+ const configuration_table = [
2264
+ /* good lazy nice chain */
2265
+ new Config(0, 0, 0, 0, deflate_stored) /* 0 store only */,
2266
+ new Config(4, 4, 8, 4, deflate_fast) /* 1 max speed, no lazy matches */,
2267
+ new Config(4, 5, 16, 8, deflate_fast) /* 2 */,
2268
+ new Config(4, 6, 32, 32, deflate_fast) /* 3 */,
2269
+ new Config(4, 4, 16, 16, deflate_slow) /* 4 lazy matches */,
2270
+ new Config(8, 16, 32, 32, deflate_slow) /* 5 */,
2271
+ new Config(8, 16, 128, 128, deflate_slow) /* 6 */,
2272
+ new Config(8, 32, 128, 256, deflate_slow) /* 7 */,
2273
+ new Config(32, 128, 258, 1024, deflate_slow) /* 8 */,
2274
+ new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */,
2275
+ ];
2276
+ /* ===========================================================================
2277
+ * Initialize the "longest match" routines for a new zlib stream
2278
+ */
2279
+ const lm_init = (s) => {
2280
+ s.window_size = 2 * s.w_size;
2281
+ /*** CLEAR_HASH(s); ***/
2282
+ zero(s.head); // Fill with NIL (= 0);
2283
+ /* Set the default configuration parameters:
2284
+ */
2285
+ s.max_lazy_match = configuration_table[s.level].max_lazy;
2286
+ s.good_match = configuration_table[s.level].good_length;
2287
+ s.nice_match = configuration_table[s.level].nice_length;
2288
+ s.max_chain_length = configuration_table[s.level].max_chain;
2289
+ s.strstart = 0;
2290
+ s.block_start = 0;
2291
+ s.lookahead = 0;
2292
+ s.insert = 0;
2293
+ s.match_length = s.prev_length = MIN_MATCH - 1;
2294
+ s.match_available = 0;
2295
+ s.ins_h = 0;
2296
+ };
2297
+ function DeflateState() {
2298
+ this.strm = null; /* pointer back to this zlib stream */
2299
+ this.status = 0; /* as the name implies */
2300
+ this.pending_buf = null; /* output still pending */
2301
+ this.pending_buf_size = 0; /* size of pending_buf */
2302
+ this.pending_out = 0; /* next pending byte to output to the stream */
2303
+ this.pending = 0; /* nb of bytes in the pending buffer */
2304
+ this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
2305
+ this.gzhead = null; /* gzip header information to write */
2306
+ this.gzindex = 0; /* where in extra, name, or comment */
2307
+ this.method = Z_DEFLATED$1; /* can only be DEFLATED */
2308
+ this.last_flush = -1; /* value of flush param for previous deflate call */
2309
+ this.w_size = 0; /* LZ77 window size (32K by default) */
2310
+ this.w_bits = 0; /* log2(w_size) (8..16) */
2311
+ this.w_mask = 0; /* w_size - 1 */
2312
+ this.window = null;
2313
+ /* Sliding window. Input bytes are read into the second half of the window,
2314
+ * and move to the first half later to keep a dictionary of at least wSize
2315
+ * bytes. With this organization, matches are limited to a distance of
2316
+ * wSize-MAX_MATCH bytes, but this ensures that IO is always
2317
+ * performed with a length multiple of the block size.
2318
+ */
2319
+ this.window_size = 0;
2320
+ /* Actual size of window: 2*wSize, except when the user input buffer
2321
+ * is directly used as sliding window.
2322
+ */
2323
+ this.prev = null;
2324
+ /* Link to older string with same hash index. To limit the size of this
2325
+ * array to 64K, this link is maintained only for the last 32K strings.
2326
+ * An index in this array is thus a window index modulo 32K.
2327
+ */
2328
+ this.head = null; /* Heads of the hash chains or NIL. */
2329
+ this.ins_h = 0; /* hash index of string to be inserted */
2330
+ this.hash_size = 0; /* number of elements in hash table */
2331
+ this.hash_bits = 0; /* log2(hash_size) */
2332
+ this.hash_mask = 0; /* hash_size-1 */
2333
+ this.hash_shift = 0;
2334
+ /* Number of bits by which ins_h must be shifted at each input
2335
+ * step. It must be such that after MIN_MATCH steps, the oldest
2336
+ * byte no longer takes part in the hash key, that is:
2337
+ * hash_shift * MIN_MATCH >= hash_bits
2338
+ */
2339
+ this.block_start = 0;
2340
+ /* Window position at the beginning of the current output block. Gets
2341
+ * negative when the window is moved backwards.
2342
+ */
2343
+ this.match_length = 0; /* length of best match */
2344
+ this.prev_match = 0; /* previous match */
2345
+ this.match_available = 0; /* set if previous match exists */
2346
+ this.strstart = 0; /* start of string to insert */
2347
+ this.match_start = 0; /* start of matching string */
2348
+ this.lookahead = 0; /* number of valid bytes ahead in window */
2349
+ this.prev_length = 0;
2350
+ /* Length of the best match at previous step. Matches not greater than this
2351
+ * are discarded. This is used in the lazy match evaluation.
2352
+ */
2353
+ this.max_chain_length = 0;
2354
+ /* To speed up deflation, hash chains are never searched beyond this
2355
+ * length. A higher limit improves compression ratio but degrades the
2356
+ * speed.
2357
+ */
2358
+ this.max_lazy_match = 0;
2359
+ /* Attempt to find a better match only when the current match is strictly
2360
+ * smaller than this value. This mechanism is used only for compression
2361
+ * levels >= 4.
2362
+ */
2363
+ // That's alias to max_lazy_match, don't use directly
2364
+ //this.max_insert_length = 0;
2365
+ /* Insert new strings in the hash table only if the match length is not
2366
+ * greater than this length. This saves time but degrades compression.
2367
+ * max_insert_length is used only for compression levels <= 3.
2368
+ */
2369
+ this.level = 0; /* compression level (1..9) */
2370
+ this.strategy = 0; /* favor or force Huffman coding*/
2371
+ this.good_match = 0;
2372
+ /* Use a faster search when the previous match is longer than this */
2373
+ this.nice_match = 0; /* Stop searching when current match exceeds this */
2374
+ /* used by trees.c: */
2375
+ /* Didn't use ct_data typedef below to suppress compiler warning */
2376
+ // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
2377
+ // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
2378
+ // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
2379
+ // Use flat array of DOUBLE size, with interleaved fata,
2380
+ // because JS does not support effective
2381
+ this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2);
2382
+ this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2);
2383
+ this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2);
2384
+ zero(this.dyn_ltree);
2385
+ zero(this.dyn_dtree);
2386
+ zero(this.bl_tree);
2387
+ this.l_desc = null; /* desc. for literal tree */
2388
+ this.d_desc = null; /* desc. for distance tree */
2389
+ this.bl_desc = null; /* desc. for bit length tree */
2390
+ //ush bl_count[MAX_BITS+1];
2391
+ this.bl_count = new Uint16Array(MAX_BITS + 1);
2392
+ /* number of codes at each bit length for an optimal tree */
2393
+ //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
2394
+ this.heap = new Uint16Array(2 * L_CODES + 1); /* heap used to build the Huffman trees */
2395
+ zero(this.heap);
2396
+ this.heap_len = 0; /* number of elements in the heap */
2397
+ this.heap_max = 0; /* element of largest frequency */
2398
+ /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
2399
+ * The same heap array is used to build all trees.
2400
+ */
2401
+ this.depth = new Uint16Array(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
2402
+ zero(this.depth);
2403
+ /* Depth of each subtree used as tie breaker for trees of equal frequency
2404
+ */
2405
+ this.sym_buf = 0; /* buffer for distances and literals/lengths */
2406
+ this.lit_bufsize = 0;
2407
+ /* Size of match buffer for literals/lengths. There are 4 reasons for
2408
+ * limiting lit_bufsize to 64K:
2409
+ * - frequencies can be kept in 16 bit counters
2410
+ * - if compression is not successful for the first block, all input
2411
+ * data is still in the window so we can still emit a stored block even
2412
+ * when input comes from standard input. (This can also be done for
2413
+ * all blocks if lit_bufsize is not greater than 32K.)
2414
+ * - if compression is not successful for a file smaller than 64K, we can
2415
+ * even emit a stored file instead of a stored block (saving 5 bytes).
2416
+ * This is applicable only for zip (not gzip or zlib).
2417
+ * - creating new Huffman trees less frequently may not provide fast
2418
+ * adaptation to changes in the input data statistics. (Take for
2419
+ * example a binary file with poorly compressible code followed by
2420
+ * a highly compressible string table.) Smaller buffer sizes give
2421
+ * fast adaptation but have of course the overhead of transmitting
2422
+ * trees more frequently.
2423
+ * - I can't count above 4
2424
+ */
2425
+ this.sym_next = 0; /* running index in sym_buf */
2426
+ this.sym_end = 0; /* symbol table full when sym_next reaches this */
2427
+ this.opt_len = 0; /* bit length of current block with optimal trees */
2428
+ this.static_len = 0; /* bit length of current block with static trees */
2429
+ this.matches = 0; /* number of string matches in current block */
2430
+ this.insert = 0; /* bytes at end of window left to insert */
2431
+ this.bi_buf = 0;
2432
+ /* Output buffer. bits are inserted starting at the bottom (least
2433
+ * significant bits).
2434
+ */
2435
+ this.bi_valid = 0;
2436
+ /* Number of valid bits in bi_buf. All bits above the last valid bit
2437
+ * are always zero.
2438
+ */
2439
+ // Used for window memory init. We safely ignore it for JS. That makes
2440
+ // sense only for pointers and memory check tools.
2441
+ //this.high_water = 0;
2442
+ /* High water mark offset in window for initialized bytes -- bytes above
2443
+ * this are set to zero in order to avoid memory check warnings when
2444
+ * longest match routines access bytes past the input. This is then
2445
+ * updated to the new high water mark.
2446
+ */
2447
+ }
2448
+ /* =========================================================================
2449
+ * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
2450
+ */
2451
+ const deflateStateCheck = (strm) => {
2452
+ if (!strm) {
2453
+ return 1;
2454
+ }
2455
+ const s = strm.state;
2456
+ if (!s ||
2457
+ s.strm !== strm ||
2458
+ (s.status !== INIT_STATE &&
2459
+ //#ifdef GZIP
2460
+ s.status !== GZIP_STATE &&
2461
+ //#endif
2462
+ s.status !== EXTRA_STATE &&
2463
+ s.status !== NAME_STATE &&
2464
+ s.status !== COMMENT_STATE &&
2465
+ s.status !== HCRC_STATE &&
2466
+ s.status !== BUSY_STATE &&
2467
+ s.status !== FINISH_STATE)) {
2468
+ return 1;
2469
+ }
2470
+ return 0;
2471
+ };
2472
+ const deflateResetKeep = (strm) => {
2473
+ if (deflateStateCheck(strm)) {
2474
+ return err(strm, Z_STREAM_ERROR);
2475
+ }
2476
+ strm.total_in = strm.total_out = 0;
2477
+ strm.data_type = Z_UNKNOWN;
2478
+ const s = strm.state;
2479
+ s.pending = 0;
2480
+ s.pending_out = 0;
2481
+ if (s.wrap < 0) {
2482
+ s.wrap = -s.wrap;
2483
+ /* was made negative by deflate(..., Z_FINISH); */
2484
+ }
2485
+ s.status =
2486
+ //#ifdef GZIP
2487
+ s.wrap === 2
2488
+ ? GZIP_STATE
2489
+ : //#endif
2490
+ s.wrap
2491
+ ? INIT_STATE
2492
+ : BUSY_STATE;
2493
+ strm.adler =
2494
+ s.wrap === 2
2495
+ ? 0 // crc32(0, Z_NULL, 0)
2496
+ : 1; // adler32(0, Z_NULL, 0)
2497
+ s.last_flush = -2;
2498
+ _tr_init(s);
2499
+ return Z_OK$1;
2500
+ };
2501
+ const deflateReset = (strm) => {
2502
+ const ret = deflateResetKeep(strm);
2503
+ if (ret === Z_OK$1) {
2504
+ lm_init(strm.state);
2505
+ }
2506
+ return ret;
2507
+ };
2508
+ const deflateSetHeader = (strm, head) => {
2509
+ if (deflateStateCheck(strm) || strm.state.wrap !== 2) {
2510
+ return Z_STREAM_ERROR;
2511
+ }
2512
+ strm.state.gzhead = head;
2513
+ return Z_OK$1;
2514
+ };
2515
+ const deflateInit2 = (strm, level, method, windowBits, memLevel, strategy) => {
2516
+ if (!strm) {
2517
+ // === Z_NULL
2518
+ return Z_STREAM_ERROR;
2519
+ }
2520
+ let wrap = 1;
2521
+ if (level === Z_DEFAULT_COMPRESSION$1) {
2522
+ level = 6;
2523
+ }
2524
+ if (windowBits < 0) {
2525
+ /* suppress zlib wrapper */
2526
+ wrap = 0;
2527
+ windowBits = -windowBits;
2528
+ }
2529
+ else if (windowBits > 15) {
2530
+ wrap = 2; /* write gzip wrapper instead */
2531
+ windowBits -= 16;
2532
+ }
2533
+ if (memLevel < 1 ||
2534
+ memLevel > MAX_MEM_LEVEL ||
2535
+ method !== Z_DEFLATED$1 ||
2536
+ windowBits < 8 ||
2537
+ windowBits > 15 ||
2538
+ level < 0 ||
2539
+ level > 9 ||
2540
+ strategy < 0 ||
2541
+ strategy > Z_FIXED ||
2542
+ (windowBits === 8 && wrap !== 1)) {
2543
+ return err(strm, Z_STREAM_ERROR);
2544
+ }
2545
+ if (windowBits === 8) {
2546
+ windowBits = 9;
2547
+ }
2548
+ /* until 256-byte window bug fixed */
2549
+ const s = new DeflateState();
2550
+ strm.state = s;
2551
+ s.strm = strm;
2552
+ s.status = INIT_STATE; /* to pass state test in deflateReset() */
2553
+ s.wrap = wrap;
2554
+ s.gzhead = null;
2555
+ s.w_bits = windowBits;
2556
+ s.w_size = 1 << s.w_bits;
2557
+ s.w_mask = s.w_size - 1;
2558
+ s.hash_bits = memLevel + 7;
2559
+ s.hash_size = 1 << s.hash_bits;
2560
+ s.hash_mask = s.hash_size - 1;
2561
+ s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
2562
+ s.window = new Uint8Array(s.w_size * 2);
2563
+ s.head = new Uint16Array(s.hash_size);
2564
+ s.prev = new Uint16Array(s.w_size);
2565
+ // Don't need mem init magic for JS.
2566
+ //s.high_water = 0; /* nothing written to s->window yet */
2567
+ s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
2568
+ /* We overlay pending_buf and sym_buf. This works since the average size
2569
+ * for length/distance pairs over any compressed block is assured to be 31
2570
+ * bits or less.
2571
+ *
2572
+ * Analysis: The longest fixed codes are a length code of 8 bits plus 5
2573
+ * extra bits, for lengths 131 to 257. The longest fixed distance codes are
2574
+ * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
2575
+ * possible fixed-codes length/distance pair is then 31 bits total.
2576
+ *
2577
+ * sym_buf starts one-fourth of the way into pending_buf. So there are
2578
+ * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
2579
+ * in sym_buf is three bytes -- two for the distance and one for the
2580
+ * literal/length. As each symbol is consumed, the pointer to the next
2581
+ * sym_buf value to read moves forward three bytes. From that symbol, up to
2582
+ * 31 bits are written to pending_buf. The closest the written pending_buf
2583
+ * bits gets to the next sym_buf symbol to read is just before the last
2584
+ * code is written. At that time, 31*(n-2) bits have been written, just
2585
+ * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
2586
+ * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
2587
+ * symbols are written.) The closest the writing gets to what is unread is
2588
+ * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
2589
+ * can range from 128 to 32768.
2590
+ *
2591
+ * Therefore, at a minimum, there are 142 bits of space between what is
2592
+ * written and what is read in the overlain buffers, so the symbols cannot
2593
+ * be overwritten by the compressed data. That space is actually 139 bits,
2594
+ * due to the three-bit fixed-code block header.
2595
+ *
2596
+ * That covers the case where either Z_FIXED is specified, forcing fixed
2597
+ * codes, or when the use of fixed codes is chosen, because that choice
2598
+ * results in a smaller compressed block than dynamic codes. That latter
2599
+ * condition then assures that the above analysis also covers all dynamic
2600
+ * blocks. A dynamic-code block will only be chosen to be emitted if it has
2601
+ * fewer bits than a fixed-code block would for the same set of symbols.
2602
+ * Therefore its average symbol length is assured to be less than 31. So
2603
+ * the compressed data for a dynamic block also cannot overwrite the
2604
+ * symbols from which it is being constructed.
2605
+ */
2606
+ s.pending_buf_size = s.lit_bufsize * 4;
2607
+ s.pending_buf = new Uint8Array(s.pending_buf_size);
2608
+ // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
2609
+ //s->sym_buf = s->pending_buf + s->lit_bufsize;
2610
+ s.sym_buf = s.lit_bufsize;
2611
+ //s->sym_end = (s->lit_bufsize - 1) * 3;
2612
+ s.sym_end = (s.lit_bufsize - 1) * 3;
2613
+ /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
2614
+ * on 16 bit machines and because stored blocks are restricted to
2615
+ * 64K-1 bytes.
2616
+ */
2617
+ s.level = level;
2618
+ s.strategy = strategy;
2619
+ s.method = method;
2620
+ return deflateReset(strm);
2621
+ };
2622
+ const deflateInit = (strm, level) => {
2623
+ return deflateInit2(strm, level, Z_DEFLATED$1, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY$1);
2624
+ };
2625
+ /* ========================================================================= */
2626
+ const deflate$1 = (strm, flush) => {
2627
+ if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
2628
+ return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
2629
+ }
2630
+ const s = strm.state;
2631
+ if (!strm.output ||
2632
+ (strm.avail_in !== 0 && !strm.input) ||
2633
+ (s.status === FINISH_STATE && flush !== Z_FINISH$1)) {
2634
+ return err(strm, strm.avail_out === 0 ? Z_BUF_ERROR : Z_STREAM_ERROR);
2635
+ }
2636
+ const old_flush = s.last_flush;
2637
+ s.last_flush = flush;
2638
+ /* Flush as much pending output as possible */
2639
+ if (s.pending !== 0) {
2640
+ flush_pending(strm);
2641
+ if (strm.avail_out === 0) {
2642
+ /* Since avail_out is 0, deflate will be called again with
2643
+ * more output space, but possibly with both pending and
2644
+ * avail_in equal to zero. There won't be anything to do,
2645
+ * but this is not an error situation so make sure we
2646
+ * return OK instead of BUF_ERROR at next call of deflate:
2647
+ */
2648
+ s.last_flush = -1;
2649
+ return Z_OK$1;
2650
+ }
2651
+ /* Make sure there is something to do and avoid duplicate consecutive
2652
+ * flushes. For repeated and useless calls with Z_FINISH, we keep
2653
+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
2654
+ */
2655
+ }
2656
+ else if (strm.avail_in === 0 &&
2657
+ rank(flush) <= rank(old_flush) &&
2658
+ flush !== Z_FINISH$1) {
2659
+ return err(strm, Z_BUF_ERROR);
2660
+ }
2661
+ /* User must not provide more input after the first FINISH: */
2662
+ if (s.status === FINISH_STATE && strm.avail_in !== 0) {
2663
+ return err(strm, Z_BUF_ERROR);
2664
+ }
2665
+ /* Write the header */
2666
+ if (s.status === INIT_STATE && s.wrap === 0) {
2667
+ s.status = BUSY_STATE;
2668
+ }
2669
+ if (s.status === INIT_STATE) {
2670
+ /* zlib header */
2671
+ let header = (Z_DEFLATED$1 + ((s.w_bits - 8) << 4)) << 8;
2672
+ let level_flags = -1;
2673
+ if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
2674
+ level_flags = 0;
2675
+ }
2676
+ else if (s.level < 6) {
2677
+ level_flags = 1;
2678
+ }
2679
+ else if (s.level === 6) {
2680
+ level_flags = 2;
2681
+ }
2682
+ else {
2683
+ level_flags = 3;
2684
+ }
2685
+ header |= level_flags << 6;
2686
+ if (s.strstart !== 0) {
2687
+ header |= PRESET_DICT;
2688
+ }
2689
+ header += 31 - (header % 31);
2690
+ putShortMSB(s, header);
2691
+ /* Save the adler32 of the preset dictionary: */
2692
+ if (s.strstart !== 0) {
2693
+ putShortMSB(s, strm.adler >>> 16);
2694
+ putShortMSB(s, strm.adler & 0xffff);
2695
+ }
2696
+ strm.adler = 1; // adler32(0L, Z_NULL, 0);
2697
+ s.status = BUSY_STATE;
2698
+ /* Compression must start with an empty pending buffer */
2699
+ flush_pending(strm);
2700
+ if (s.pending !== 0) {
2701
+ s.last_flush = -1;
2702
+ return Z_OK$1;
2703
+ }
2704
+ }
2705
+ //#ifdef GZIP
2706
+ if (s.status === GZIP_STATE) {
2707
+ /* gzip header */
2708
+ strm.adler = 0; //crc32(0L, Z_NULL, 0);
2709
+ put_byte(s, 31);
2710
+ put_byte(s, 139);
2711
+ put_byte(s, 8);
2712
+ if (!s.gzhead) {
2713
+ // s->gzhead == Z_NULL
2714
+ put_byte(s, 0);
2715
+ put_byte(s, 0);
2716
+ put_byte(s, 0);
2717
+ put_byte(s, 0);
2718
+ put_byte(s, 0);
2719
+ put_byte(s, s.level === 9
2720
+ ? 2
2721
+ : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2
2722
+ ? 4
2723
+ : 0);
2724
+ put_byte(s, OS_CODE);
2725
+ s.status = BUSY_STATE;
2726
+ /* Compression must start with an empty pending buffer */
2727
+ flush_pending(strm);
2728
+ if (s.pending !== 0) {
2729
+ s.last_flush = -1;
2730
+ return Z_OK$1;
2731
+ }
2732
+ }
2733
+ else {
2734
+ put_byte(s, (s.gzhead.text ? 1 : 0) +
2735
+ (s.gzhead.hcrc ? 2 : 0) +
2736
+ (!s.gzhead.extra ? 0 : 4) +
2737
+ (!s.gzhead.name ? 0 : 8) +
2738
+ (!s.gzhead.comment ? 0 : 16));
2739
+ put_byte(s, s.gzhead.time & 0xff);
2740
+ put_byte(s, (s.gzhead.time >> 8) & 0xff);
2741
+ put_byte(s, (s.gzhead.time >> 16) & 0xff);
2742
+ put_byte(s, (s.gzhead.time >> 24) & 0xff);
2743
+ put_byte(s, s.level === 9
2744
+ ? 2
2745
+ : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2
2746
+ ? 4
2747
+ : 0);
2748
+ put_byte(s, s.gzhead.os & 0xff);
2749
+ if (s.gzhead.extra && s.gzhead.extra.length) {
2750
+ put_byte(s, s.gzhead.extra.length & 0xff);
2751
+ put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
2752
+ }
2753
+ if (s.gzhead.hcrc) {
2754
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending, 0);
2755
+ }
2756
+ s.gzindex = 0;
2757
+ s.status = EXTRA_STATE;
2758
+ }
2759
+ }
2760
+ if (s.status === EXTRA_STATE) {
2761
+ if (s.gzhead.extra /* != Z_NULL*/) {
2762
+ let beg = s.pending; /* start of bytes to update crc */
2763
+ let left = (s.gzhead.extra.length & 0xffff) - s.gzindex;
2764
+ while (s.pending + left > s.pending_buf_size) {
2765
+ let copy = s.pending_buf_size - s.pending;
2766
+ // zmemcpy(s.pending_buf + s.pending,
2767
+ // s.gzhead.extra + s.gzindex, copy);
2768
+ s.pending_buf.set(s.gzhead.extra.subarray(s.gzindex, s.gzindex + copy), s.pending);
2769
+ s.pending = s.pending_buf_size;
2770
+ //--- HCRC_UPDATE(beg) ---//
2771
+ if (s.gzhead.hcrc && s.pending > beg) {
2772
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
2773
+ }
2774
+ //---//
2775
+ s.gzindex += copy;
2776
+ flush_pending(strm);
2777
+ if (s.pending !== 0) {
2778
+ s.last_flush = -1;
2779
+ return Z_OK$1;
2780
+ }
2781
+ beg = 0;
2782
+ left -= copy;
2783
+ }
2784
+ // JS specific: s.gzhead.extra may be TypedArray or Array for backward compatibility
2785
+ // TypedArray.slice and TypedArray.from don't exist in IE10-IE11
2786
+ let gzhead_extra = new Uint8Array(s.gzhead.extra);
2787
+ // zmemcpy(s->pending_buf + s->pending,
2788
+ // s->gzhead->extra + s->gzindex, left);
2789
+ s.pending_buf.set(gzhead_extra.subarray(s.gzindex, s.gzindex + left), s.pending);
2790
+ s.pending += left;
2791
+ //--- HCRC_UPDATE(beg) ---//
2792
+ if (s.gzhead.hcrc && s.pending > beg) {
2793
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
2794
+ }
2795
+ //---//
2796
+ s.gzindex = 0;
2797
+ }
2798
+ s.status = NAME_STATE;
2799
+ }
2800
+ if (s.status === NAME_STATE) {
2801
+ if (s.gzhead.name /* != Z_NULL*/) {
2802
+ let beg = s.pending; /* start of bytes to update crc */
2803
+ let val;
2804
+ do {
2805
+ if (s.pending === s.pending_buf_size) {
2806
+ //--- HCRC_UPDATE(beg) ---//
2807
+ if (s.gzhead.hcrc && s.pending > beg) {
2808
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
2809
+ }
2810
+ //---//
2811
+ flush_pending(strm);
2812
+ if (s.pending !== 0) {
2813
+ s.last_flush = -1;
2814
+ return Z_OK$1;
2815
+ }
2816
+ beg = 0;
2817
+ }
2818
+ // JS specific: little magic to add zero terminator to end of string
2819
+ if (s.gzindex < s.gzhead.name.length) {
2820
+ val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
2821
+ }
2822
+ else {
2823
+ val = 0;
2824
+ }
2825
+ put_byte(s, val);
2826
+ } while (val !== 0);
2827
+ //--- HCRC_UPDATE(beg) ---//
2828
+ if (s.gzhead.hcrc && s.pending > beg) {
2829
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
2830
+ }
2831
+ //---//
2832
+ s.gzindex = 0;
2833
+ }
2834
+ s.status = COMMENT_STATE;
2835
+ }
2836
+ if (s.status === COMMENT_STATE) {
2837
+ if (s.gzhead.comment /* != Z_NULL*/) {
2838
+ let beg = s.pending; /* start of bytes to update crc */
2839
+ let val;
2840
+ do {
2841
+ if (s.pending === s.pending_buf_size) {
2842
+ //--- HCRC_UPDATE(beg) ---//
2843
+ if (s.gzhead.hcrc && s.pending > beg) {
2844
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
2845
+ }
2846
+ //---//
2847
+ flush_pending(strm);
2848
+ if (s.pending !== 0) {
2849
+ s.last_flush = -1;
2850
+ return Z_OK$1;
2851
+ }
2852
+ beg = 0;
2853
+ }
2854
+ // JS specific: little magic to add zero terminator to end of string
2855
+ if (s.gzindex < s.gzhead.comment.length) {
2856
+ val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
2857
+ }
2858
+ else {
2859
+ val = 0;
2860
+ }
2861
+ put_byte(s, val);
2862
+ } while (val !== 0);
2863
+ //--- HCRC_UPDATE(beg) ---//
2864
+ if (s.gzhead.hcrc && s.pending > beg) {
2865
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
2866
+ }
2867
+ //---//
2868
+ }
2869
+ s.status = HCRC_STATE;
2870
+ }
2871
+ if (s.status === HCRC_STATE) {
2872
+ if (s.gzhead.hcrc) {
2873
+ if (s.pending + 2 > s.pending_buf_size) {
2874
+ flush_pending(strm);
2875
+ if (s.pending !== 0) {
2876
+ s.last_flush = -1;
2877
+ return Z_OK$1;
2878
+ }
2879
+ }
2880
+ put_byte(s, strm.adler & 0xff);
2881
+ put_byte(s, (strm.adler >> 8) & 0xff);
2882
+ strm.adler = 0; //crc32(0L, Z_NULL, 0);
2883
+ }
2884
+ s.status = BUSY_STATE;
2885
+ /* Compression must start with an empty pending buffer */
2886
+ flush_pending(strm);
2887
+ if (s.pending !== 0) {
2888
+ s.last_flush = -1;
2889
+ return Z_OK$1;
2890
+ }
2891
+ }
2892
+ //#endif
2893
+ /* Start a new block or continue the current one.
2894
+ */
2895
+ if (strm.avail_in !== 0 ||
2896
+ s.lookahead !== 0 ||
2897
+ (flush !== Z_NO_FLUSH$1 && s.status !== FINISH_STATE)) {
2898
+ let bstate = s.level === 0
2899
+ ? deflate_stored(s, flush)
2900
+ : s.strategy === Z_HUFFMAN_ONLY
2901
+ ? deflate_huff(s, flush)
2902
+ : s.strategy === Z_RLE
2903
+ ? deflate_rle(s, flush)
2904
+ : configuration_table[s.level].func(s, flush);
2905
+ if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
2906
+ s.status = FINISH_STATE;
2907
+ }
2908
+ if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
2909
+ if (strm.avail_out === 0) {
2910
+ s.last_flush = -1;
2911
+ /* avoid BUF_ERROR next call, see above */
2912
+ }
2913
+ return Z_OK$1;
2914
+ /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
2915
+ * of deflate should use the same flush parameter to make sure
2916
+ * that the flush is complete. So we don't have to output an
2917
+ * empty block here, this will be done at next call. This also
2918
+ * ensures that for a very small output buffer, we emit at most
2919
+ * one empty block.
2920
+ */
2921
+ }
2922
+ if (bstate === BS_BLOCK_DONE) {
2923
+ if (flush === Z_PARTIAL_FLUSH) {
2924
+ _tr_align(s);
2925
+ }
2926
+ else if (flush !== Z_BLOCK) {
2927
+ /* FULL_FLUSH or SYNC_FLUSH */
2928
+ _tr_stored_block(s, 0, 0, false);
2929
+ /* For a full flush, this empty block will be recognized
2930
+ * as a special marker by inflate_sync().
2931
+ */
2932
+ if (flush === Z_FULL_FLUSH$1) {
2933
+ /*** CLEAR_HASH(s); ***/ /* forget history */
2934
+ zero(s.head); // Fill with NIL (= 0);
2935
+ if (s.lookahead === 0) {
2936
+ s.strstart = 0;
2937
+ s.block_start = 0;
2938
+ s.insert = 0;
2939
+ }
2940
+ }
2941
+ }
2942
+ flush_pending(strm);
2943
+ if (strm.avail_out === 0) {
2944
+ s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
2945
+ return Z_OK$1;
2946
+ }
2947
+ }
2948
+ }
2949
+ if (flush !== Z_FINISH$1) {
2950
+ return Z_OK$1;
2951
+ }
2952
+ if (s.wrap <= 0) {
2953
+ return Z_STREAM_END$1;
2954
+ }
2955
+ /* Write the trailer */
2956
+ if (s.wrap === 2) {
2957
+ put_byte(s, strm.adler & 0xff);
2958
+ put_byte(s, (strm.adler >> 8) & 0xff);
2959
+ put_byte(s, (strm.adler >> 16) & 0xff);
2960
+ put_byte(s, (strm.adler >> 24) & 0xff);
2961
+ put_byte(s, strm.total_in & 0xff);
2962
+ put_byte(s, (strm.total_in >> 8) & 0xff);
2963
+ put_byte(s, (strm.total_in >> 16) & 0xff);
2964
+ put_byte(s, (strm.total_in >> 24) & 0xff);
2965
+ }
2966
+ else {
2967
+ putShortMSB(s, strm.adler >>> 16);
2968
+ putShortMSB(s, strm.adler & 0xffff);
2969
+ }
2970
+ flush_pending(strm);
2971
+ /* If avail_out is zero, the application will call deflate again
2972
+ * to flush the rest.
2973
+ */
2974
+ if (s.wrap > 0) {
2975
+ s.wrap = -s.wrap;
2976
+ }
2977
+ /* write the trailer only once! */
2978
+ return s.pending !== 0 ? Z_OK$1 : Z_STREAM_END$1;
2979
+ };
2980
+ const deflateEnd = (strm) => {
2981
+ if (deflateStateCheck(strm)) {
2982
+ return Z_STREAM_ERROR;
2983
+ }
2984
+ const status = strm.state.status;
2985
+ strm.state = null;
2986
+ return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK$1;
2987
+ };
2988
+ /* =========================================================================
2989
+ * Initializes the compression dictionary from the given byte
2990
+ * sequence without producing any compressed output.
2991
+ */
2992
+ const deflateSetDictionary = (strm, dictionary) => {
2993
+ let dictLength = dictionary.length;
2994
+ if (deflateStateCheck(strm)) {
2995
+ return Z_STREAM_ERROR;
2996
+ }
2997
+ const s = strm.state;
2998
+ const wrap = s.wrap;
2999
+ if (wrap === 2 ||
3000
+ (wrap === 1 && s.status !== INIT_STATE) ||
3001
+ s.lookahead) {
3002
+ return Z_STREAM_ERROR;
3003
+ }
3004
+ /* when using zlib wrappers, compute Adler-32 for provided dictionary */
3005
+ if (wrap === 1) {
3006
+ /* adler32(strm->adler, dictionary, dictLength); */
3007
+ strm.adler = adler32_1(strm.adler, dictionary, dictLength, 0);
3008
+ }
3009
+ s.wrap = 0; /* avoid computing Adler-32 in read_buf */
3010
+ /* if dictionary would fill window, just replace the history */
3011
+ if (dictLength >= s.w_size) {
3012
+ if (wrap === 0) {
3013
+ /* already empty otherwise */
3014
+ /*** CLEAR_HASH(s); ***/
3015
+ zero(s.head); // Fill with NIL (= 0);
3016
+ s.strstart = 0;
3017
+ s.block_start = 0;
3018
+ s.insert = 0;
3019
+ }
3020
+ /* use the tail */
3021
+ // dictionary = dictionary.slice(dictLength - s.w_size);
3022
+ let tmpDict = new Uint8Array(s.w_size);
3023
+ tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0);
3024
+ dictionary = tmpDict;
3025
+ dictLength = s.w_size;
3026
+ }
3027
+ /* insert dictionary into window and hash */
3028
+ const avail = strm.avail_in;
3029
+ const next = strm.next_in;
3030
+ const input = strm.input;
3031
+ strm.avail_in = dictLength;
3032
+ strm.next_in = 0;
3033
+ strm.input = dictionary;
3034
+ fill_window(s);
3035
+ while (s.lookahead >= MIN_MATCH) {
3036
+ let str = s.strstart;
3037
+ let n = s.lookahead - (MIN_MATCH - 1);
3038
+ do {
3039
+ /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
3040
+ s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
3041
+ s.prev[str & s.w_mask] = s.head[s.ins_h];
3042
+ s.head[s.ins_h] = str;
3043
+ str++;
3044
+ } while (--n);
3045
+ s.strstart = str;
3046
+ s.lookahead = MIN_MATCH - 1;
3047
+ fill_window(s);
3048
+ }
3049
+ s.strstart += s.lookahead;
3050
+ s.block_start = s.strstart;
3051
+ s.insert = s.lookahead;
3052
+ s.lookahead = 0;
3053
+ s.match_length = s.prev_length = MIN_MATCH - 1;
3054
+ s.match_available = 0;
3055
+ strm.next_in = next;
3056
+ strm.input = input;
3057
+ strm.avail_in = avail;
3058
+ s.wrap = wrap;
3059
+ return Z_OK$1;
3060
+ };
3061
+ var deflateInit_1 = deflateInit;
3062
+ var deflateInit2_1 = deflateInit2;
3063
+ var deflateReset_1 = deflateReset;
3064
+ var deflateResetKeep_1 = deflateResetKeep;
3065
+ var deflateSetHeader_1 = deflateSetHeader;
3066
+ var deflate_2$1 = deflate$1;
3067
+ var deflateEnd_1 = deflateEnd;
3068
+ var deflateSetDictionary_1 = deflateSetDictionary;
3069
+ var deflateInfo = 'pako deflate (from Nodeca project)';
3070
+ /* Not implemented
3071
+ module.exports.deflateBound = deflateBound;
3072
+ module.exports.deflateCopy = deflateCopy;
3073
+ module.exports.deflateGetDictionary = deflateGetDictionary;
3074
+ module.exports.deflateParams = deflateParams;
3075
+ module.exports.deflatePending = deflatePending;
3076
+ module.exports.deflatePrime = deflatePrime;
3077
+ module.exports.deflateTune = deflateTune;
3078
+ */
3079
+ var deflate_1$1 = {
3080
+ deflateInit: deflateInit_1,
3081
+ deflateInit2: deflateInit2_1,
3082
+ deflateReset: deflateReset_1,
3083
+ deflateResetKeep: deflateResetKeep_1,
3084
+ deflateSetHeader: deflateSetHeader_1,
3085
+ deflate: deflate_2$1,
3086
+ deflateEnd: deflateEnd_1,
3087
+ deflateSetDictionary: deflateSetDictionary_1,
3088
+ deflateInfo: deflateInfo,
3089
+ };
3090
+ const _has = (obj, key) => {
3091
+ return Object.prototype.hasOwnProperty.call(obj, key);
3092
+ };
3093
+ var assign = function (obj /*from1, from2, from3, ...*/) {
3094
+ const sources = Array.prototype.slice.call(arguments, 1);
3095
+ while (sources.length) {
3096
+ const source = sources.shift();
3097
+ if (!source) {
3098
+ continue;
3099
+ }
3100
+ if (typeof source !== 'object') {
3101
+ throw new TypeError(source + 'must be non-object');
3102
+ }
3103
+ for (const p in source) {
3104
+ if (_has(source, p)) {
3105
+ obj[p] = source[p];
3106
+ }
3107
+ }
3108
+ }
3109
+ return obj;
3110
+ };
3111
+ // Join array of chunks to single array.
3112
+ var flattenChunks = (chunks) => {
3113
+ // calculate data length
3114
+ let len = 0;
3115
+ for (let i = 0, l = chunks.length; i < l; i++) {
3116
+ len += chunks[i].length;
3117
+ }
3118
+ // join chunks
3119
+ const result = new Uint8Array(len);
3120
+ for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
3121
+ let chunk = chunks[i];
3122
+ result.set(chunk, pos);
3123
+ pos += chunk.length;
3124
+ }
3125
+ return result;
3126
+ };
3127
+ var common = {
3128
+ assign: assign,
3129
+ flattenChunks: flattenChunks,
3130
+ };
3131
+ // String encode/decode helpers
3132
+ // Quick check if we can use fast array to bin string conversion
3133
+ //
3134
+ // - apply(Array) can fail on Android 2.2
3135
+ // - apply(Uint8Array) can fail on iOS 5.1 Safari
3136
+ //
3137
+ let STR_APPLY_UIA_OK = true;
3138
+ try {
3139
+ String.fromCharCode.apply(null, new Uint8Array(1));
3140
+ }
3141
+ catch (__) {
3142
+ STR_APPLY_UIA_OK = false;
3143
+ }
3144
+ // Table with utf8 lengths (calculated by first byte of sequence)
3145
+ // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
3146
+ // because max possible codepoint is 0x10ffff
3147
+ const _utf8len = new Uint8Array(256);
3148
+ for (let q = 0; q < 256; q++) {
3149
+ _utf8len[q] =
3150
+ q >= 252
3151
+ ? 6
3152
+ : q >= 248
3153
+ ? 5
3154
+ : q >= 240
3155
+ ? 4
3156
+ : q >= 224
3157
+ ? 3
3158
+ : q >= 192
3159
+ ? 2
3160
+ : 1;
3161
+ }
3162
+ _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
3163
+ // convert string to array (typed, when possible)
3164
+ var string2buf = (str) => {
3165
+ if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) {
3166
+ return new TextEncoder().encode(str);
3167
+ }
3168
+ let buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
3169
+ // count binary size
3170
+ for (m_pos = 0; m_pos < str_len; m_pos++) {
3171
+ c = str.charCodeAt(m_pos);
3172
+ if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
3173
+ c2 = str.charCodeAt(m_pos + 1);
3174
+ if ((c2 & 0xfc00) === 0xdc00) {
3175
+ c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
3176
+ m_pos++;
3177
+ }
3178
+ }
3179
+ buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
3180
+ }
3181
+ // allocate buffer
3182
+ buf = new Uint8Array(buf_len);
3183
+ // convert
3184
+ for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
3185
+ c = str.charCodeAt(m_pos);
3186
+ if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
3187
+ c2 = str.charCodeAt(m_pos + 1);
3188
+ if ((c2 & 0xfc00) === 0xdc00) {
3189
+ c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
3190
+ m_pos++;
3191
+ }
3192
+ }
3193
+ if (c < 0x80) {
3194
+ /* one byte */
3195
+ buf[i++] = c;
3196
+ }
3197
+ else if (c < 0x800) {
3198
+ /* two bytes */
3199
+ buf[i++] = 0xc0 | (c >>> 6);
3200
+ buf[i++] = 0x80 | (c & 0x3f);
3201
+ }
3202
+ else if (c < 0x10000) {
3203
+ /* three bytes */
3204
+ buf[i++] = 0xe0 | (c >>> 12);
3205
+ buf[i++] = 0x80 | ((c >>> 6) & 0x3f);
3206
+ buf[i++] = 0x80 | (c & 0x3f);
3207
+ }
3208
+ else {
3209
+ /* four bytes */
3210
+ buf[i++] = 0xf0 | (c >>> 18);
3211
+ buf[i++] = 0x80 | ((c >>> 12) & 0x3f);
3212
+ buf[i++] = 0x80 | ((c >>> 6) & 0x3f);
3213
+ buf[i++] = 0x80 | (c & 0x3f);
3214
+ }
3215
+ }
3216
+ return buf;
3217
+ };
3218
+ // Helper
3219
+ const buf2binstring = (buf, len) => {
3220
+ // On Chrome, the arguments in a function call that are allowed is `65534`.
3221
+ // If the length of the buffer is smaller than that, we can use this optimization,
3222
+ // otherwise we will take a slower path.
3223
+ if (len < 65534) {
3224
+ if (buf.subarray && STR_APPLY_UIA_OK) {
3225
+ return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
3226
+ }
3227
+ }
3228
+ let result = '';
3229
+ for (let i = 0; i < len; i++) {
3230
+ result += String.fromCharCode(buf[i]);
3231
+ }
3232
+ return result;
3233
+ };
3234
+ // convert array to string
3235
+ var buf2string = (buf, max) => {
3236
+ const len = max || buf.length;
3237
+ if (typeof TextDecoder === 'function' && TextDecoder.prototype.decode) {
3238
+ return new TextDecoder().decode(buf.subarray(0, max));
3239
+ }
3240
+ let i, out;
3241
+ // Reserve max possible length (2 words per char)
3242
+ // NB: by unknown reasons, Array is significantly faster for
3243
+ // String.fromCharCode.apply than Uint16Array.
3244
+ const utf16buf = new Array(len * 2);
3245
+ for (out = 0, i = 0; i < len;) {
3246
+ let c = buf[i++];
3247
+ // quick process ascii
3248
+ if (c < 0x80) {
3249
+ utf16buf[out++] = c;
3250
+ continue;
3251
+ }
3252
+ let c_len = _utf8len[c];
3253
+ // skip 5 & 6 byte codes
3254
+ if (c_len > 4) {
3255
+ utf16buf[out++] = 0xfffd;
3256
+ i += c_len - 1;
3257
+ continue;
3258
+ }
3259
+ // apply mask on first byte
3260
+ c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
3261
+ // join the rest
3262
+ while (c_len > 1 && i < len) {
3263
+ c = (c << 6) | (buf[i++] & 0x3f);
3264
+ c_len--;
3265
+ }
3266
+ // terminated by end of string?
3267
+ if (c_len > 1) {
3268
+ utf16buf[out++] = 0xfffd;
3269
+ continue;
3270
+ }
3271
+ if (c < 0x10000) {
3272
+ utf16buf[out++] = c;
3273
+ }
3274
+ else {
3275
+ c -= 0x10000;
3276
+ utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
3277
+ utf16buf[out++] = 0xdc00 | (c & 0x3ff);
3278
+ }
3279
+ }
3280
+ return buf2binstring(utf16buf, out);
3281
+ };
3282
+ // Calculate max possible position in utf8 buffer,
3283
+ // that will not break sequence. If that's not possible
3284
+ // - (very small limits) return max size as is.
3285
+ //
3286
+ // buf[] - utf8 bytes array
3287
+ // max - length limit (mandatory);
3288
+ var utf8border = (buf, max) => {
3289
+ max = max || buf.length;
3290
+ if (max > buf.length) {
3291
+ max = buf.length;
3292
+ }
3293
+ // go back from last position, until start of sequence found
3294
+ let pos = max - 1;
3295
+ while (pos >= 0 && (buf[pos] & 0xc0) === 0x80) {
3296
+ pos--;
3297
+ }
3298
+ // Very small and broken sequence,
3299
+ // return max, because we should return something anyway.
3300
+ if (pos < 0) {
3301
+ return max;
3302
+ }
3303
+ // If we came to start of buffer - that means buffer is too small,
3304
+ // return max too.
3305
+ if (pos === 0) {
3306
+ return max;
3307
+ }
3308
+ return pos + _utf8len[buf[pos]] > max ? pos : max;
3309
+ };
3310
+ var strings = {
3311
+ string2buf: string2buf,
3312
+ buf2string: buf2string,
3313
+ utf8border: utf8border,
3314
+ };
3315
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
3316
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
3317
+ //
3318
+ // This software is provided 'as-is', without any express or implied
3319
+ // warranty. In no event will the authors be held liable for any damages
3320
+ // arising from the use of this software.
3321
+ //
3322
+ // Permission is granted to anyone to use this software for any purpose,
3323
+ // including commercial applications, and to alter it and redistribute it
3324
+ // freely, subject to the following restrictions:
3325
+ //
3326
+ // 1. The origin of this software must not be misrepresented; you must not
3327
+ // claim that you wrote the original software. If you use this software
3328
+ // in a product, an acknowledgment in the product documentation would be
3329
+ // appreciated but is not required.
3330
+ // 2. Altered source versions must be plainly marked as such, and must not be
3331
+ // misrepresented as being the original software.
3332
+ // 3. This notice may not be removed or altered from any source distribution.
3333
+ function ZStream() {
3334
+ /* next input byte */
3335
+ this.input = null; // JS specific, because we have no pointers
3336
+ this.next_in = 0;
3337
+ /* number of bytes available at input */
3338
+ this.avail_in = 0;
3339
+ /* total number of input bytes read so far */
3340
+ this.total_in = 0;
3341
+ /* next output byte should be put there */
3342
+ this.output = null; // JS specific, because we have no pointers
3343
+ this.next_out = 0;
3344
+ /* remaining free space at output */
3345
+ this.avail_out = 0;
3346
+ /* total number of bytes output so far */
3347
+ this.total_out = 0;
3348
+ /* last error message, NULL if no error */
3349
+ this.msg = '' /*Z_NULL*/;
3350
+ /* not visible by applications */
3351
+ this.state = null;
3352
+ /* best guess about the data type: binary or text */
3353
+ this.data_type = 2 /*Z_UNKNOWN*/;
3354
+ /* adler32 value of the uncompressed data */
3355
+ this.adler = 0;
3356
+ }
3357
+ var zstream = ZStream;
3358
+ const toString = Object.prototype.toString;
3359
+ /* Public constants ==========================================================*/
3360
+ /* ===========================================================================*/
3361
+ const { Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH, Z_OK, Z_STREAM_END, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY, Z_DEFLATED, } = constants$1;
3362
+ /* ===========================================================================*/
3363
+ /**
3364
+ * class Deflate
3365
+ *
3366
+ * Generic JS-style wrapper for zlib calls. If you don't need
3367
+ * streaming behaviour - use more simple functions: [[deflate]],
3368
+ * [[deflateRaw]] and [[gzip]].
3369
+ **/
3370
+ /* internal
3371
+ * Deflate.chunks -> Array
3372
+ *
3373
+ * Chunks of output data, if [[Deflate#onData]] not overridden.
3374
+ **/
3375
+ /**
3376
+ * Deflate.result -> Uint8Array
3377
+ *
3378
+ * Compressed result, generated by default [[Deflate#onData]]
3379
+ * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
3380
+ * (call [[Deflate#push]] with `Z_FINISH` / `true` param).
3381
+ **/
3382
+ /**
3383
+ * Deflate.err -> Number
3384
+ *
3385
+ * Error code after deflate finished. 0 (Z_OK) on success.
3386
+ * You will not need it in real life, because deflate errors
3387
+ * are possible only on wrong options or bad `onData` / `onEnd`
3388
+ * custom handlers.
3389
+ **/
3390
+ /**
3391
+ * Deflate.msg -> String
3392
+ *
3393
+ * Error message, if [[Deflate.err]] != 0
3394
+ **/
3395
+ /**
3396
+ * new Deflate(options)
3397
+ * - options (Object): zlib deflate options.
3398
+ *
3399
+ * Creates new deflator instance with specified params. Throws exception
3400
+ * on bad params. Supported options:
3401
+ *
3402
+ * - `level`
3403
+ * - `windowBits`
3404
+ * - `memLevel`
3405
+ * - `strategy`
3406
+ * - `dictionary`
3407
+ *
3408
+ * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
3409
+ * for more information on these.
3410
+ *
3411
+ * Additional options, for internal needs:
3412
+ *
3413
+ * - `chunkSize` - size of generated data chunks (16K by default)
3414
+ * - `raw` (Boolean) - do raw deflate
3415
+ * - `gzip` (Boolean) - create gzip wrapper
3416
+ * - `header` (Object) - custom header for gzip
3417
+ * - `text` (Boolean) - true if compressed data believed to be text
3418
+ * - `time` (Number) - modification time, unix timestamp
3419
+ * - `os` (Number) - operation system code
3420
+ * - `extra` (Array) - array of bytes with extra data (max 65536)
3421
+ * - `name` (String) - file name (binary string)
3422
+ * - `comment` (String) - comment (binary string)
3423
+ * - `hcrc` (Boolean) - true if header crc should be added
3424
+ *
3425
+ * ##### Example:
3426
+ *
3427
+ * ```javascript
3428
+ * const pako = require('pako')
3429
+ * , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
3430
+ * , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
3431
+ *
3432
+ * const deflate = new pako.Deflate({ level: 3});
3433
+ *
3434
+ * deflate.push(chunk1, false);
3435
+ * deflate.push(chunk2, true); // true -> last chunk
3436
+ *
3437
+ * if (deflate.err) { throw new Error(deflate.err); }
3438
+ *
3439
+ * console.log(deflate.result);
3440
+ * ```
3441
+ **/
3442
+ function Deflate(options) {
3443
+ this.options = common.assign({
3444
+ level: Z_DEFAULT_COMPRESSION,
3445
+ method: Z_DEFLATED,
3446
+ chunkSize: 16384,
3447
+ windowBits: 15,
3448
+ memLevel: 8,
3449
+ strategy: Z_DEFAULT_STRATEGY,
3450
+ }, options || {});
3451
+ let opt = this.options;
3452
+ if (opt.raw && opt.windowBits > 0) {
3453
+ opt.windowBits = -opt.windowBits;
3454
+ }
3455
+ else if (opt.gzip && opt.windowBits > 0 && opt.windowBits < 16) {
3456
+ opt.windowBits += 16;
3457
+ }
3458
+ this.err = 0; // error code, if happens (0 = Z_OK)
3459
+ this.msg = ''; // error message
3460
+ this.ended = false; // used to avoid multiple onEnd() calls
3461
+ this.chunks = []; // chunks of compressed data
3462
+ this.strm = new zstream();
3463
+ this.strm.avail_out = 0;
3464
+ let status = deflate_1$1.deflateInit2(this.strm, opt.level, opt.method, opt.windowBits, opt.memLevel, opt.strategy);
3465
+ if (status !== Z_OK) {
3466
+ throw new Error(messages[status]);
3467
+ }
3468
+ if (opt.header) {
3469
+ deflate_1$1.deflateSetHeader(this.strm, opt.header);
3470
+ }
3471
+ if (opt.dictionary) {
3472
+ let dict;
3473
+ // Convert data if needed
3474
+ if (typeof opt.dictionary === 'string') {
3475
+ // If we need to compress text, change encoding to utf8.
3476
+ dict = strings.string2buf(opt.dictionary);
3477
+ }
3478
+ else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
3479
+ dict = new Uint8Array(opt.dictionary);
3480
+ }
3481
+ else {
3482
+ dict = opt.dictionary;
3483
+ }
3484
+ status = deflate_1$1.deflateSetDictionary(this.strm, dict);
3485
+ if (status !== Z_OK) {
3486
+ throw new Error(messages[status]);
3487
+ }
3488
+ this._dict_set = true;
3489
+ }
3490
+ }
3491
+ /**
3492
+ * Deflate#push(data[, flush_mode]) -> Boolean
3493
+ * - data (Uint8Array|ArrayBuffer|String): input data. Strings will be
3494
+ * converted to utf8 byte sequence.
3495
+ * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
3496
+ * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
3497
+ *
3498
+ * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
3499
+ * new compressed chunks. Returns `true` on success. The last data block must
3500
+ * have `flush_mode` Z_FINISH (or `true`). That will flush internal pending
3501
+ * buffers and call [[Deflate#onEnd]].
3502
+ *
3503
+ * On fail call [[Deflate#onEnd]] with error code and return false.
3504
+ *
3505
+ * ##### Example
3506
+ *
3507
+ * ```javascript
3508
+ * push(chunk, false); // push one of data chunks
3509
+ * ...
3510
+ * push(chunk, true); // push last chunk
3511
+ * ```
3512
+ **/
3513
+ Deflate.prototype.push = function (data, flush_mode) {
3514
+ const strm = this.strm;
3515
+ const chunkSize = this.options.chunkSize;
3516
+ let status, _flush_mode;
3517
+ if (this.ended) {
3518
+ return false;
3519
+ }
3520
+ if (flush_mode === ~~flush_mode)
3521
+ _flush_mode = flush_mode;
3522
+ else
3523
+ _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH;
3524
+ // Convert data if needed
3525
+ if (typeof data === 'string') {
3526
+ // If we need to compress text, change encoding to utf8.
3527
+ strm.input = strings.string2buf(data);
3528
+ }
3529
+ else if (toString.call(data) === '[object ArrayBuffer]') {
3530
+ strm.input = new Uint8Array(data);
3531
+ }
3532
+ else {
3533
+ strm.input = data;
3534
+ }
3535
+ strm.next_in = 0;
3536
+ strm.avail_in = strm.input.length;
3537
+ for (;;) {
3538
+ if (strm.avail_out === 0) {
3539
+ strm.output = new Uint8Array(chunkSize);
3540
+ strm.next_out = 0;
3541
+ strm.avail_out = chunkSize;
3542
+ }
3543
+ // Make sure avail_out > 6 to avoid repeating markers
3544
+ if ((_flush_mode === Z_SYNC_FLUSH || _flush_mode === Z_FULL_FLUSH) &&
3545
+ strm.avail_out <= 6) {
3546
+ this.onData(strm.output.subarray(0, strm.next_out));
3547
+ strm.avail_out = 0;
3548
+ continue;
3549
+ }
3550
+ status = deflate_1$1.deflate(strm, _flush_mode);
3551
+ // Ended => flush and finish
3552
+ if (status === Z_STREAM_END) {
3553
+ if (strm.next_out > 0) {
3554
+ this.onData(strm.output.subarray(0, strm.next_out));
3555
+ }
3556
+ status = deflate_1$1.deflateEnd(this.strm);
3557
+ this.onEnd(status);
3558
+ this.ended = true;
3559
+ return status === Z_OK;
3560
+ }
3561
+ // Flush if out buffer full
3562
+ if (strm.avail_out === 0) {
3563
+ this.onData(strm.output);
3564
+ continue;
3565
+ }
3566
+ // Flush if requested and has data
3567
+ if (_flush_mode > 0 && strm.next_out > 0) {
3568
+ this.onData(strm.output.subarray(0, strm.next_out));
3569
+ strm.avail_out = 0;
3570
+ continue;
3571
+ }
3572
+ if (strm.avail_in === 0)
3573
+ break;
3574
+ }
3575
+ return true;
3576
+ };
3577
+ /**
3578
+ * Deflate#onData(chunk) -> Void
3579
+ * - chunk (Uint8Array): output data.
3580
+ *
3581
+ * By default, stores data blocks in `chunks[]` property and glue
3582
+ * those in `onEnd`. Override this handler, if you need another behaviour.
3583
+ **/
3584
+ Deflate.prototype.onData = function (chunk) {
3585
+ this.chunks.push(chunk);
3586
+ };
3587
+ /**
3588
+ * Deflate#onEnd(status) -> Void
3589
+ * - status (Number): deflate status. 0 (Z_OK) on success,
3590
+ * other if not.
3591
+ *
3592
+ * Called once after you tell deflate that the input stream is
3593
+ * complete (Z_FINISH). By default - join collected chunks,
3594
+ * free memory and fill `results` / `err` properties.
3595
+ **/
3596
+ Deflate.prototype.onEnd = function (status) {
3597
+ // On success - join
3598
+ if (status === Z_OK) {
3599
+ this.result = common.flattenChunks(this.chunks);
3600
+ }
3601
+ this.chunks = [];
3602
+ this.err = status;
3603
+ this.msg = this.strm.msg;
3604
+ };
3605
+ /**
3606
+ * deflate(data[, options]) -> Uint8Array
3607
+ * - data (Uint8Array|ArrayBuffer|String): input data to compress.
3608
+ * - options (Object): zlib deflate options.
3609
+ *
3610
+ * Compress `data` with deflate algorithm and `options`.
3611
+ *
3612
+ * Supported options are:
3613
+ *
3614
+ * - level
3615
+ * - windowBits
3616
+ * - memLevel
3617
+ * - strategy
3618
+ * - dictionary
3619
+ *
3620
+ * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
3621
+ * for more information on these.
3622
+ *
3623
+ * Sugar (options):
3624
+ *
3625
+ * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
3626
+ * negative windowBits implicitly.
3627
+ *
3628
+ * ##### Example:
3629
+ *
3630
+ * ```javascript
3631
+ * const pako = require('pako')
3632
+ * const data = new Uint8Array([1,2,3,4,5,6,7,8,9]);
3633
+ *
3634
+ * console.log(pako.deflate(data));
3635
+ * ```
3636
+ **/
3637
+ function deflate(input, options) {
3638
+ const deflator = new Deflate(options);
3639
+ deflator.push(input, true);
3640
+ // That will never happens, if you don't cheat with options :)
3641
+ if (deflator.err) {
3642
+ throw deflator.msg || messages[deflator.err];
3643
+ }
3644
+ return deflator.result;
3645
+ }
3646
+ /**
3647
+ * deflateRaw(data[, options]) -> Uint8Array
3648
+ * - data (Uint8Array|ArrayBuffer|String): input data to compress.
3649
+ * - options (Object): zlib deflate options.
3650
+ *
3651
+ * The same as [[deflate]], but creates raw data, without wrapper
3652
+ * (header and adler32 crc).
3653
+ **/
3654
+ function deflateRaw(input, options) {
3655
+ options = options || {};
3656
+ options.raw = true;
3657
+ return deflate(input, options);
3658
+ }
3659
+ /**
3660
+ * gzip(data[, options]) -> Uint8Array
3661
+ * - data (Uint8Array|ArrayBuffer|String): input data to compress.
3662
+ * - options (Object): zlib deflate options.
3663
+ *
3664
+ * The same as [[deflate]], but create gzip wrapper instead of
3665
+ * deflate one.
3666
+ **/
3667
+ function gzip(input, options) {
3668
+ options = options || {};
3669
+ options.gzip = true;
3670
+ return deflate(input, options);
3671
+ }
3672
+ var Deflate_1 = Deflate;
3673
+ var deflate_2 = deflate;
3674
+ var deflateRaw_1 = deflateRaw;
3675
+ var gzip_1 = gzip;
3676
+ var constants = constants$1;
3677
+ var deflate_1 = {
3678
+ Deflate: Deflate_1,
3679
+ deflate: deflate_2,
3680
+ deflateRaw: deflateRaw_1,
3681
+ gzip: gzip_1,
3682
+ constants: constants,
3683
+ };
3684
+ exports.Deflate = Deflate_1;
3685
+ exports.constants = constants;
3686
+ exports['default'] = deflate_1;
3687
+ exports.deflate = deflate_2;
3688
+ exports.deflateRaw = deflateRaw_1;
3689
+ exports.gzip = gzip_1;
3690
+ coralogixGzipFunc = gzip_1;
3691
+ Object.defineProperty(exports, '__esModule', { value: true });
3692
+ });
3693
+ self.onmessage = ({ data }) => {
3694
+ const recordsData = omitSquareBrackets(JSON.stringify(data.records));
3695
+ const gzipData = coralogixGzipFunc(recordsData);
3696
+ const gzipSize = gzipData.byteLength;
3697
+ if (gzipSize >= MAX_CHUNK_SIZE) {
3698
+ const totalChunks = Math.ceil(gzipSize / MAX_CHUNK_SIZE);
3699
+ for (let i = 0; i < totalChunks; i++) {
3700
+ const start = i * MAX_CHUNK_SIZE;
3701
+ const end = (i + 1) * MAX_CHUNK_SIZE;
3702
+ const chunk = gzipData.slice(start, end);
3703
+ createBlobAndSend(chunk, i, totalChunks - 1, 'splitRecordData');
3704
+ }
3705
+ }
3706
+ else {
3707
+ createBlobAndSend(gzipData);
3708
+ }
3709
+ };
3710
+ self.onerror = (err) => {
3711
+ // todo: wrap with debug flag
3712
+ console.warn('Coralogix Browser SDK - Error from session recording worker:\n', err);
3713
+ };
3714
+ function omitSquareBrackets(json) {
3715
+ return json.slice(1, -1);
3716
+ }
3717
+ function createBlobAndSend(gzipData, chunkIndex, totalChunks, event = 'sendRecordData') {
3718
+ const blob = new Blob([gzipData], {
3719
+ type: 'application/octet-stream',
3720
+ });
3721
+ self.postMessage({
3722
+ gzipBlob: blob,
3723
+ chunkIndex,
3724
+ totalChunks,
3725
+ event,
3726
+ });
3727
+ }
3728
+ }
3729
+ //# sourceMappingURL=session-recording-worker.js.map