@cogitator-ai/wasm-tools 0.5.11 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +84 -25
  2. package/dist/index.d.ts +22 -3
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +81 -14
  5. package/dist/index.js.map +1 -1
  6. package/dist/manager/types.d.ts +2 -0
  7. package/dist/manager/types.d.ts.map +1 -1
  8. package/dist/manager/wasm-loader.d.ts.map +1 -1
  9. package/dist/manager/wasm-loader.js +11 -2
  10. package/dist/manager/wasm-loader.js.map +1 -1
  11. package/dist/manager/wasm-tool-manager.d.ts +1 -0
  12. package/dist/manager/wasm-tool-manager.d.ts.map +1 -1
  13. package/dist/manager/wasm-tool-manager.js +56 -13
  14. package/dist/manager/wasm-tool-manager.js.map +1 -1
  15. package/dist/runtime.d.ts +11 -0
  16. package/dist/runtime.d.ts.map +1 -0
  17. package/dist/runtime.js +104 -0
  18. package/dist/runtime.js.map +1 -0
  19. package/dist/wasm/base64.wasm +0 -0
  20. package/dist/wasm/calc.wasm +0 -0
  21. package/dist/wasm/compression.wasm +0 -0
  22. package/dist/wasm/csv.wasm +0 -0
  23. package/dist/wasm/datetime.wasm +0 -0
  24. package/dist/wasm/diff.wasm +0 -0
  25. package/dist/wasm/hash.wasm +0 -0
  26. package/dist/wasm/json.wasm +0 -0
  27. package/dist/wasm/markdown.wasm +0 -0
  28. package/dist/wasm/regex.wasm +0 -0
  29. package/dist/wasm/signing.wasm +0 -0
  30. package/dist/wasm/slug.wasm +0 -0
  31. package/dist/wasm/validation.wasm +0 -0
  32. package/dist/wasm/xml.wasm +0 -0
  33. package/package.json +5 -6
  34. package/dist/__tests__/file-watcher.test.d.ts +0 -2
  35. package/dist/__tests__/file-watcher.test.d.ts.map +0 -1
  36. package/dist/__tests__/file-watcher.test.js +0 -99
  37. package/dist/__tests__/file-watcher.test.js.map +0 -1
  38. package/dist/__tests__/index.test.d.ts +0 -2
  39. package/dist/__tests__/index.test.d.ts.map +0 -1
  40. package/dist/__tests__/index.test.js +0 -226
  41. package/dist/__tests__/index.test.js.map +0 -1
  42. package/dist/__tests__/wasm-loader.test.d.ts +0 -2
  43. package/dist/__tests__/wasm-loader.test.d.ts.map +0 -1
  44. package/dist/__tests__/wasm-loader.test.js +0 -68
  45. package/dist/__tests__/wasm-loader.test.js.map +0 -1
  46. package/dist/__tests__/wasm-tool-manager.test.d.ts +0 -2
  47. package/dist/__tests__/wasm-tool-manager.test.d.ts.map +0 -1
  48. package/dist/__tests__/wasm-tool-manager.test.js +0 -166
  49. package/dist/__tests__/wasm-tool-manager.test.js.map +0 -1
  50. package/dist/plugins/base64.d.ts +0 -12
  51. package/dist/plugins/base64.d.ts.map +0 -1
  52. package/dist/plugins/base64.js +0 -144
  53. package/dist/plugins/base64.js.map +0 -1
  54. package/dist/plugins/calc.d.ts +0 -12
  55. package/dist/plugins/calc.d.ts.map +0 -1
  56. package/dist/plugins/calc.js +0 -102
  57. package/dist/plugins/calc.js.map +0 -1
  58. package/dist/plugins/compression.d.ts +0 -2
  59. package/dist/plugins/compression.d.ts.map +0 -1
  60. package/dist/plugins/compression.js +0 -549
  61. package/dist/plugins/compression.js.map +0 -1
  62. package/dist/plugins/csv.d.ts +0 -2
  63. package/dist/plugins/csv.d.ts.map +0 -1
  64. package/dist/plugins/csv.js +0 -142
  65. package/dist/plugins/csv.js.map +0 -1
  66. package/dist/plugins/datetime.d.ts +0 -2
  67. package/dist/plugins/datetime.d.ts.map +0 -1
  68. package/dist/plugins/datetime.js +0 -199
  69. package/dist/plugins/datetime.js.map +0 -1
  70. package/dist/plugins/diff.d.ts +0 -2
  71. package/dist/plugins/diff.d.ts.map +0 -1
  72. package/dist/plugins/diff.js +0 -187
  73. package/dist/plugins/diff.js.map +0 -1
  74. package/dist/plugins/hash.d.ts +0 -12
  75. package/dist/plugins/hash.d.ts.map +0 -1
  76. package/dist/plugins/hash.js +0 -310
  77. package/dist/plugins/hash.js.map +0 -1
  78. package/dist/plugins/json.d.ts +0 -12
  79. package/dist/plugins/json.d.ts.map +0 -1
  80. package/dist/plugins/json.js +0 -70
  81. package/dist/plugins/json.js.map +0 -1
  82. package/dist/plugins/markdown.d.ts +0 -2
  83. package/dist/plugins/markdown.d.ts.map +0 -1
  84. package/dist/plugins/markdown.js +0 -213
  85. package/dist/plugins/markdown.js.map +0 -1
  86. package/dist/plugins/regex.d.ts +0 -2
  87. package/dist/plugins/regex.d.ts.map +0 -1
  88. package/dist/plugins/regex.js +0 -120
  89. package/dist/plugins/regex.js.map +0 -1
  90. package/dist/plugins/signing.d.ts +0 -2
  91. package/dist/plugins/signing.d.ts.map +0 -1
  92. package/dist/plugins/signing.js +0 -589
  93. package/dist/plugins/signing.js.map +0 -1
  94. package/dist/plugins/slug.d.ts +0 -2
  95. package/dist/plugins/slug.d.ts.map +0 -1
  96. package/dist/plugins/slug.js +0 -183
  97. package/dist/plugins/slug.js.map +0 -1
  98. package/dist/plugins/validation.d.ts +0 -2
  99. package/dist/plugins/validation.d.ts.map +0 -1
  100. package/dist/plugins/validation.js +0 -105
  101. package/dist/plugins/validation.js.map +0 -1
  102. package/dist/plugins/xml.d.ts +0 -2
  103. package/dist/plugins/xml.d.ts.map +0 -1
  104. package/dist/plugins/xml.js +0 -297
  105. package/dist/plugins/xml.js.map +0 -1
  106. package/dist/temp/base64.js +0 -149
  107. package/dist/temp/calc.js +0 -108
  108. package/dist/temp/compression.js +0 -742
  109. package/dist/temp/csv.js +0 -161
  110. package/dist/temp/datetime.js +0 -217
  111. package/dist/temp/diff.js +0 -198
  112. package/dist/temp/hash.js +0 -424
  113. package/dist/temp/json.js +0 -80
  114. package/dist/temp/markdown.js +0 -224
  115. package/dist/temp/regex.js +0 -141
  116. package/dist/temp/signing.js +0 -600
  117. package/dist/temp/slug.js +0 -201
  118. package/dist/temp/validation.js +0 -125
  119. package/dist/temp/xml.js +0 -295
@@ -1,742 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/plugins/compression.ts
21
- var compression_exports = {};
22
- __export(compression_exports, {
23
- compression: () => compression
24
- });
25
- module.exports = __toCommonJS(compression_exports);
26
- var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
27
- function base64Encode(bytes) {
28
- let result = "";
29
- const len = bytes.length;
30
- for (let i = 0; i < len; i += 3) {
31
- const b1 = bytes[i];
32
- const b2 = i + 1 < len ? bytes[i + 1] : 0;
33
- const b3 = i + 2 < len ? bytes[i + 2] : 0;
34
- result += BASE64_CHARS[b1 >> 2];
35
- result += BASE64_CHARS[(b1 & 3) << 4 | b2 >> 4];
36
- result += i + 1 < len ? BASE64_CHARS[(b2 & 15) << 2 | b3 >> 6] : "=";
37
- result += i + 2 < len ? BASE64_CHARS[b3 & 63] : "=";
38
- }
39
- return result;
40
- }
41
- function base64Decode(str) {
42
- const trimmed = str.trimEnd();
43
- const cleanStr = trimmed.replace(/[^A-Za-z0-9+/]/g, "");
44
- const len = cleanStr.length;
45
- const outputLen = Math.floor(len * 3 / 4) - (trimmed.endsWith("==") ? 2 : trimmed.endsWith("=") ? 1 : 0);
46
- const bytes = new Uint8Array(outputLen);
47
- const lookup = {};
48
- for (let i = 0; i < BASE64_CHARS.length; i++) {
49
- lookup[BASE64_CHARS[i]] = i;
50
- }
51
- let byteIdx = 0;
52
- for (let i = 0; i < len; i += 4) {
53
- const b1 = lookup[cleanStr[i]] ?? 0;
54
- const b2 = lookup[cleanStr[i + 1]] ?? 0;
55
- const b3 = lookup[cleanStr[i + 2]] ?? 0;
56
- const b4 = lookup[cleanStr[i + 3]] ?? 0;
57
- bytes[byteIdx++] = b1 << 2 | b2 >> 4;
58
- if (byteIdx < outputLen) bytes[byteIdx++] = (b2 & 15) << 4 | b3 >> 2;
59
- if (byteIdx < outputLen) bytes[byteIdx++] = (b3 & 3) << 6 | b4;
60
- }
61
- return bytes;
62
- }
63
- function utf8Encode(str) {
64
- const bytes = [];
65
- for (let i = 0; i < str.length; i++) {
66
- let c = str.charCodeAt(i);
67
- if (c < 128) {
68
- bytes.push(c);
69
- } else if (c < 2048) {
70
- bytes.push(192 | c >> 6);
71
- bytes.push(128 | c & 63);
72
- } else if (c >= 55296 && c < 56320 && i + 1 < str.length) {
73
- const c2 = str.charCodeAt(++i);
74
- c = 65536 + ((c & 1023) << 10) + (c2 & 1023);
75
- bytes.push(240 | c >> 18);
76
- bytes.push(128 | c >> 12 & 63);
77
- bytes.push(128 | c >> 6 & 63);
78
- bytes.push(128 | c & 63);
79
- } else {
80
- bytes.push(224 | c >> 12);
81
- bytes.push(128 | c >> 6 & 63);
82
- bytes.push(128 | c & 63);
83
- }
84
- }
85
- return new Uint8Array(bytes);
86
- }
87
- function utf8Decode(bytes) {
88
- let result = "";
89
- let i = 0;
90
- while (i < bytes.length) {
91
- const b = bytes[i++];
92
- if (b < 128) {
93
- result += String.fromCharCode(b);
94
- } else if (b < 224) {
95
- result += String.fromCharCode((b & 31) << 6 | bytes[i++] & 63);
96
- } else if (b < 240) {
97
- result += String.fromCharCode(
98
- (b & 15) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63
99
- );
100
- } else {
101
- const cp = (b & 7) << 18 | (bytes[i++] & 63) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63;
102
- result += String.fromCodePoint(cp);
103
- }
104
- }
105
- return result;
106
- }
107
- function deflateRaw(data, level = 6) {
108
- const output = [];
109
- const len = data.length;
110
- if (level === 0 || len < 10) {
111
- let pos2 = 0;
112
- while (pos2 < len) {
113
- const blockLen = Math.min(65535, len - pos2);
114
- const isLast = pos2 + blockLen >= len;
115
- output.push(isLast ? 1 : 0);
116
- output.push(blockLen & 255);
117
- output.push(blockLen >> 8 & 255);
118
- output.push(~blockLen & 255);
119
- output.push(~blockLen >> 8 & 255);
120
- for (let i = 0; i < blockLen; i++) {
121
- output.push(data[pos2 + i]);
122
- }
123
- pos2 += blockLen;
124
- }
125
- return new Uint8Array(output);
126
- }
127
- const windowSize = 32768;
128
- const hashTable = /* @__PURE__ */ new Map();
129
- const getHash = (pos2) => {
130
- if (pos2 + 2 >= len) return 0;
131
- return (data[pos2] << 16 | data[pos2 + 1] << 8 | data[pos2 + 2]) >>> 0;
132
- };
133
- const findMatch = (pos2) => {
134
- if (pos2 + 2 >= len) return null;
135
- const hash = getHash(pos2);
136
- const candidates = hashTable.get(hash) || [];
137
- let bestDist = 0;
138
- let bestLen = 0;
139
- for (let i = candidates.length - 1; i >= 0 && i >= candidates.length - 16; i--) {
140
- const candPos = candidates[i];
141
- const dist = pos2 - candPos;
142
- if (dist > windowSize || dist <= 0) continue;
143
- let matchLen = 0;
144
- while (matchLen < 258 && pos2 + matchLen < len && data[candPos + matchLen] === data[pos2 + matchLen]) {
145
- matchLen++;
146
- }
147
- if (matchLen >= 3 && matchLen > bestLen) {
148
- bestDist = dist;
149
- bestLen = matchLen;
150
- }
151
- }
152
- if (bestLen >= 3) {
153
- return { distance: bestDist, length: bestLen };
154
- }
155
- return null;
156
- };
157
- const LITLEN_BASE = [
158
- 3,
159
- 4,
160
- 5,
161
- 6,
162
- 7,
163
- 8,
164
- 9,
165
- 10,
166
- 11,
167
- 13,
168
- 15,
169
- 17,
170
- 19,
171
- 23,
172
- 27,
173
- 31,
174
- 35,
175
- 43,
176
- 51,
177
- 59,
178
- 67,
179
- 83,
180
- 99,
181
- 115,
182
- 131,
183
- 163,
184
- 195,
185
- 227,
186
- 258
187
- ];
188
- const LITLEN_EXTRA = [
189
- 0,
190
- 0,
191
- 0,
192
- 0,
193
- 0,
194
- 0,
195
- 0,
196
- 0,
197
- 1,
198
- 1,
199
- 1,
200
- 1,
201
- 2,
202
- 2,
203
- 2,
204
- 2,
205
- 3,
206
- 3,
207
- 3,
208
- 3,
209
- 4,
210
- 4,
211
- 4,
212
- 4,
213
- 5,
214
- 5,
215
- 5,
216
- 5,
217
- 0
218
- ];
219
- const DIST_BASE = [
220
- 1,
221
- 2,
222
- 3,
223
- 4,
224
- 5,
225
- 7,
226
- 9,
227
- 13,
228
- 17,
229
- 25,
230
- 33,
231
- 49,
232
- 65,
233
- 97,
234
- 129,
235
- 193,
236
- 257,
237
- 385,
238
- 513,
239
- 769,
240
- 1025,
241
- 1537,
242
- 2049,
243
- 3073,
244
- 4097,
245
- 6145,
246
- 8193,
247
- 12289,
248
- 16385,
249
- 24577
250
- ];
251
- const DIST_EXTRA = [
252
- 0,
253
- 0,
254
- 0,
255
- 0,
256
- 1,
257
- 1,
258
- 2,
259
- 2,
260
- 3,
261
- 3,
262
- 4,
263
- 4,
264
- 5,
265
- 5,
266
- 6,
267
- 6,
268
- 7,
269
- 7,
270
- 8,
271
- 8,
272
- 9,
273
- 9,
274
- 10,
275
- 10,
276
- 11,
277
- 11,
278
- 12,
279
- 12,
280
- 13,
281
- 13
282
- ];
283
- const tokens = [];
284
- let pos = 0;
285
- while (pos < len) {
286
- const match = level > 3 ? findMatch(pos) : null;
287
- const hash = getHash(pos);
288
- if (!hashTable.has(hash)) {
289
- hashTable.set(hash, []);
290
- }
291
- hashTable.get(hash).push(pos);
292
- if (hashTable.get(hash).length > 64) {
293
- hashTable.get(hash).shift();
294
- }
295
- if (match && match.length >= 3) {
296
- tokens.push({ type: "match", length: match.length, distance: match.distance });
297
- for (let i = 1; i < match.length; i++) {
298
- const h = getHash(pos + i);
299
- if (!hashTable.has(h)) hashTable.set(h, []);
300
- hashTable.get(h).push(pos + i);
301
- }
302
- pos += match.length;
303
- } else {
304
- tokens.push({ type: "literal", value: data[pos] });
305
- pos++;
306
- }
307
- }
308
- let bitBuf = 0;
309
- let bitCount = 0;
310
- const writeBits = (value, bits) => {
311
- bitBuf |= value << bitCount;
312
- bitCount += bits;
313
- while (bitCount >= 8) {
314
- output.push(bitBuf & 255);
315
- bitBuf >>= 8;
316
- bitCount -= 8;
317
- }
318
- };
319
- const writeFixedLitLen = (code) => {
320
- if (code <= 143) {
321
- writeBits(reverseBits(48 + code, 8), 8);
322
- } else if (code <= 255) {
323
- writeBits(reverseBits(400 + code - 144, 9), 9);
324
- } else if (code === 256) {
325
- writeBits(reverseBits(0, 7), 7);
326
- } else if (code <= 279) {
327
- writeBits(reverseBits(code - 256, 7), 7);
328
- } else {
329
- writeBits(reverseBits(192 + code - 280, 8), 8);
330
- }
331
- };
332
- const findLengthCode = (length) => {
333
- for (let i = LITLEN_BASE.length - 1; i >= 0; i--) {
334
- if (length >= LITLEN_BASE[i]) return i;
335
- }
336
- return 0;
337
- };
338
- const findDistCode = (dist) => {
339
- for (let i = DIST_BASE.length - 1; i >= 0; i--) {
340
- if (dist >= DIST_BASE[i]) return i;
341
- }
342
- return 0;
343
- };
344
- writeBits(1, 1);
345
- writeBits(1, 2);
346
- for (const token of tokens) {
347
- if (token.type === "literal") {
348
- writeFixedLitLen(token.value);
349
- } else {
350
- const lenIdx = findLengthCode(token.length);
351
- const lenCode = 257 + lenIdx;
352
- writeFixedLitLen(lenCode);
353
- if (LITLEN_EXTRA[lenIdx] > 0) {
354
- writeBits(token.length - LITLEN_BASE[lenIdx], LITLEN_EXTRA[lenIdx]);
355
- }
356
- const distIdx = findDistCode(token.distance);
357
- writeBits(reverseBits(distIdx, 5), 5);
358
- if (DIST_EXTRA[distIdx] > 0) {
359
- writeBits(token.distance - DIST_BASE[distIdx], DIST_EXTRA[distIdx]);
360
- }
361
- }
362
- }
363
- writeFixedLitLen(256);
364
- if (bitCount > 0) {
365
- output.push(bitBuf & 255);
366
- }
367
- return new Uint8Array(output);
368
- }
369
- function reverseBits(value, bits) {
370
- let result = 0;
371
- for (let i = 0; i < bits; i++) {
372
- result = result << 1 | value >> i & 1;
373
- }
374
- return result;
375
- }
376
- function inflateRaw(data) {
377
- const output = [];
378
- let pos = 0;
379
- let bitBuf = 0;
380
- let bitCount = 0;
381
- const readBits = (n) => {
382
- while (bitCount < n) {
383
- if (pos >= data.length) throw new Error("Unexpected end of data");
384
- bitBuf |= data[pos++] << bitCount;
385
- bitCount += 8;
386
- }
387
- const result = bitBuf & (1 << n) - 1;
388
- bitBuf >>= n;
389
- bitCount -= n;
390
- return result;
391
- };
392
- const LITLEN_BASE = [
393
- 3,
394
- 4,
395
- 5,
396
- 6,
397
- 7,
398
- 8,
399
- 9,
400
- 10,
401
- 11,
402
- 13,
403
- 15,
404
- 17,
405
- 19,
406
- 23,
407
- 27,
408
- 31,
409
- 35,
410
- 43,
411
- 51,
412
- 59,
413
- 67,
414
- 83,
415
- 99,
416
- 115,
417
- 131,
418
- 163,
419
- 195,
420
- 227,
421
- 258
422
- ];
423
- const LITLEN_EXTRA = [
424
- 0,
425
- 0,
426
- 0,
427
- 0,
428
- 0,
429
- 0,
430
- 0,
431
- 0,
432
- 1,
433
- 1,
434
- 1,
435
- 1,
436
- 2,
437
- 2,
438
- 2,
439
- 2,
440
- 3,
441
- 3,
442
- 3,
443
- 3,
444
- 4,
445
- 4,
446
- 4,
447
- 4,
448
- 5,
449
- 5,
450
- 5,
451
- 5,
452
- 0
453
- ];
454
- const DIST_BASE = [
455
- 1,
456
- 2,
457
- 3,
458
- 4,
459
- 5,
460
- 7,
461
- 9,
462
- 13,
463
- 17,
464
- 25,
465
- 33,
466
- 49,
467
- 65,
468
- 97,
469
- 129,
470
- 193,
471
- 257,
472
- 385,
473
- 513,
474
- 769,
475
- 1025,
476
- 1537,
477
- 2049,
478
- 3073,
479
- 4097,
480
- 6145,
481
- 8193,
482
- 12289,
483
- 16385,
484
- 24577
485
- ];
486
- const DIST_EXTRA = [
487
- 0,
488
- 0,
489
- 0,
490
- 0,
491
- 1,
492
- 1,
493
- 2,
494
- 2,
495
- 3,
496
- 3,
497
- 4,
498
- 4,
499
- 5,
500
- 5,
501
- 6,
502
- 6,
503
- 7,
504
- 7,
505
- 8,
506
- 8,
507
- 9,
508
- 9,
509
- 10,
510
- 10,
511
- 11,
512
- 11,
513
- 12,
514
- 12,
515
- 13,
516
- 13
517
- ];
518
- let bfinal = 0;
519
- while (!bfinal) {
520
- bfinal = readBits(1);
521
- const btype = readBits(2);
522
- if (btype === 0) {
523
- bitBuf = 0;
524
- bitCount = 0;
525
- const len = data[pos] | data[pos + 1] << 8;
526
- pos += 4;
527
- for (let i = 0; i < len; i++) {
528
- output.push(data[pos++]);
529
- }
530
- } else if (btype === 1 || btype === 2) {
531
- let litlenCodes;
532
- let distCodes;
533
- if (btype === 1) {
534
- litlenCodes = [];
535
- for (let i = 0; i <= 143; i++) litlenCodes[i] = 8;
536
- for (let i = 144; i <= 255; i++) litlenCodes[i] = 9;
537
- for (let i = 256; i <= 279; i++) litlenCodes[i] = 7;
538
- for (let i = 280; i <= 287; i++) litlenCodes[i] = 8;
539
- distCodes = new Array(32).fill(5);
540
- } else {
541
- const hlit = readBits(5) + 257;
542
- const hdist = readBits(5) + 1;
543
- const hclen = readBits(4) + 4;
544
- const clenOrder = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
545
- const clenLens = new Array(19).fill(0);
546
- for (let i = 0; i < hclen; i++) {
547
- clenLens[clenOrder[i]] = readBits(3);
548
- }
549
- const clenTree = buildHuffmanTree(clenLens);
550
- const allLens = [];
551
- while (allLens.length < hlit + hdist) {
552
- const sym = decodeSymbol(clenTree, readBits.bind(null, 1));
553
- if (sym < 16) {
554
- allLens.push(sym);
555
- } else if (sym === 16) {
556
- const repeat = readBits(2) + 3;
557
- const last = allLens[allLens.length - 1] || 0;
558
- for (let i = 0; i < repeat; i++) allLens.push(last);
559
- } else if (sym === 17) {
560
- const repeat = readBits(3) + 3;
561
- for (let i = 0; i < repeat; i++) allLens.push(0);
562
- } else if (sym === 18) {
563
- const repeat = readBits(7) + 11;
564
- for (let i = 0; i < repeat; i++) allLens.push(0);
565
- }
566
- }
567
- litlenCodes = allLens.slice(0, hlit);
568
- distCodes = allLens.slice(hlit);
569
- }
570
- const litlenTree = buildHuffmanTree(litlenCodes);
571
- const distTree = buildHuffmanTree(distCodes);
572
- while (true) {
573
- const sym = decodeSymbol(litlenTree, () => readBits(1));
574
- if (sym < 256) {
575
- output.push(sym);
576
- } else if (sym === 256) {
577
- break;
578
- } else {
579
- const lenIdx = sym - 257;
580
- const length = LITLEN_BASE[lenIdx] + readBits(LITLEN_EXTRA[lenIdx]);
581
- const distSym = decodeSymbol(distTree, () => readBits(1));
582
- const distance = DIST_BASE[distSym] + readBits(DIST_EXTRA[distSym]);
583
- const start = output.length - distance;
584
- for (let i = 0; i < length; i++) {
585
- output.push(output[start + i]);
586
- }
587
- }
588
- }
589
- } else {
590
- throw new Error("Invalid block type");
591
- }
592
- }
593
- return new Uint8Array(output);
594
- }
595
- function buildHuffmanTree(codeLens) {
596
- const maxLen = Math.max(...codeLens, 1);
597
- const blCount = new Array(maxLen + 1).fill(0);
598
- for (const len of codeLens) {
599
- if (len > 0) blCount[len]++;
600
- }
601
- const nextCode = new Array(maxLen + 1).fill(0);
602
- let code = 0;
603
- for (let bits = 1; bits <= maxLen; bits++) {
604
- code = code + blCount[bits - 1] << 1;
605
- nextCode[bits] = code;
606
- }
607
- const root = {};
608
- for (let sym = 0; sym < codeLens.length; sym++) {
609
- const len = codeLens[sym];
610
- if (len === 0) continue;
611
- const codeVal = nextCode[len]++;
612
- let node = root;
613
- for (let i = len - 1; i >= 0; i--) {
614
- const bit = codeVal >> i & 1;
615
- if (bit === 0) {
616
- if (!node.left) node.left = {};
617
- node = node.left;
618
- } else {
619
- if (!node.right) node.right = {};
620
- node = node.right;
621
- }
622
- }
623
- node.symbol = sym;
624
- }
625
- return root;
626
- }
627
- function decodeSymbol(tree, readBit) {
628
- let node = tree;
629
- while (node.symbol === void 0) {
630
- const bit = readBit();
631
- node = bit === 0 ? node.left : node.right;
632
- if (!node) throw new Error("Invalid huffman code");
633
- }
634
- return node.symbol;
635
- }
636
- function crc32(data) {
637
- let crc = 4294967295;
638
- for (const byte of data) {
639
- crc ^= byte;
640
- for (let j = 0; j < 8; j++) {
641
- crc = crc >>> 1 ^ (crc & 1 ? 3988292384 : 0);
642
- }
643
- }
644
- return (crc ^ 4294967295) >>> 0;
645
- }
646
- function gzipCompress(data, level) {
647
- const deflated = deflateRaw(data, level);
648
- const crc = crc32(data);
649
- const output = new Uint8Array(10 + deflated.length + 8);
650
- output[0] = 31;
651
- output[1] = 139;
652
- output[2] = 8;
653
- output[3] = 0;
654
- output[4] = output[5] = output[6] = output[7] = 0;
655
- output[8] = level >= 9 ? 2 : level <= 1 ? 4 : 0;
656
- output[9] = 255;
657
- output.set(deflated, 10);
658
- const pos = 10 + deflated.length;
659
- output[pos] = crc & 255;
660
- output[pos + 1] = crc >> 8 & 255;
661
- output[pos + 2] = crc >> 16 & 255;
662
- output[pos + 3] = crc >> 24 & 255;
663
- output[pos + 4] = data.length & 255;
664
- output[pos + 5] = data.length >> 8 & 255;
665
- output[pos + 6] = data.length >> 16 & 255;
666
- output[pos + 7] = data.length >> 24 & 255;
667
- return output;
668
- }
669
- function gzipDecompress(data) {
670
- if (data[0] !== 31 || data[1] !== 139) {
671
- throw new Error("Invalid gzip header");
672
- }
673
- if (data[2] !== 8) {
674
- throw new Error("Unsupported compression method");
675
- }
676
- const flags = data[3];
677
- let pos = 10;
678
- if (flags & 4) {
679
- const xlen = data[pos] | data[pos + 1] << 8;
680
- pos += 2 + xlen;
681
- }
682
- if (flags & 8) {
683
- while (data[pos++] !== 0) ;
684
- }
685
- if (flags & 16) {
686
- while (data[pos++] !== 0) ;
687
- }
688
- if (flags & 2) {
689
- pos += 2;
690
- }
691
- const deflatedData = data.slice(pos, data.length - 8);
692
- return inflateRaw(deflatedData);
693
- }
694
- function compression() {
695
- try {
696
- const inputStr = Host.inputString();
697
- const input = JSON.parse(inputStr);
698
- const inputEncoding = input.inputEncoding ?? "utf8";
699
- const outputEncoding = input.outputEncoding ?? "base64";
700
- const level = input.level ?? 6;
701
- if (level < 0 || level > 9) {
702
- throw new Error(`Compression level must be between 0 and 9, got ${level}`);
703
- }
704
- let inputBytes;
705
- if (inputEncoding === "base64") {
706
- inputBytes = base64Decode(input.data);
707
- } else {
708
- inputBytes = utf8Encode(input.data);
709
- }
710
- let resultBytes;
711
- if (input.operation === "compress") {
712
- resultBytes = gzipCompress(inputBytes, level);
713
- } else {
714
- resultBytes = gzipDecompress(inputBytes);
715
- }
716
- let result;
717
- if (outputEncoding === "base64") {
718
- result = base64Encode(resultBytes);
719
- } else {
720
- result = utf8Decode(resultBytes);
721
- }
722
- const denominator = input.operation === "compress" ? inputBytes.length : resultBytes.length;
723
- const ratio = denominator > 0 ? (input.operation === "compress" ? resultBytes.length : inputBytes.length) / denominator : 0;
724
- const output = {
725
- result,
726
- originalSize: inputBytes.length,
727
- resultSize: resultBytes.length,
728
- ratio: Math.round(ratio * 100) / 100
729
- };
730
- Host.outputString(JSON.stringify(output));
731
- return 0;
732
- } catch (error) {
733
- const output = {
734
- result: "",
735
- originalSize: 0,
736
- resultSize: 0,
737
- error: error instanceof Error ? error.message : String(error)
738
- };
739
- Host.outputString(JSON.stringify(output));
740
- return 1;
741
- }
742
- }