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