@cogitator-ai/wasm-tools 0.4.1 → 0.5.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 (70) hide show
  1. package/README.md +141 -6
  2. package/dist/index.d.ts +227 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +194 -0
  5. package/dist/index.js.map +1 -1
  6. package/dist/plugins/compression.d.ts +2 -0
  7. package/dist/plugins/compression.d.ts.map +1 -0
  8. package/dist/plugins/compression.js +530 -0
  9. package/dist/plugins/compression.js.map +1 -0
  10. package/dist/plugins/csv.d.ts +2 -0
  11. package/dist/plugins/csv.d.ts.map +1 -0
  12. package/dist/plugins/csv.js +138 -0
  13. package/dist/plugins/csv.js.map +1 -0
  14. package/dist/plugins/datetime.d.ts +2 -0
  15. package/dist/plugins/datetime.d.ts.map +1 -0
  16. package/dist/plugins/datetime.js +184 -0
  17. package/dist/plugins/datetime.js.map +1 -0
  18. package/dist/plugins/diff.d.ts +2 -0
  19. package/dist/plugins/diff.d.ts.map +1 -0
  20. package/dist/plugins/diff.js +187 -0
  21. package/dist/plugins/diff.js.map +1 -0
  22. package/dist/plugins/markdown.d.ts +2 -0
  23. package/dist/plugins/markdown.d.ts.map +1 -0
  24. package/dist/plugins/markdown.js +203 -0
  25. package/dist/plugins/markdown.js.map +1 -0
  26. package/dist/plugins/regex.d.ts +2 -0
  27. package/dist/plugins/regex.d.ts.map +1 -0
  28. package/dist/plugins/regex.js +119 -0
  29. package/dist/plugins/regex.js.map +1 -0
  30. package/dist/plugins/signing.d.ts +2 -0
  31. package/dist/plugins/signing.d.ts.map +1 -0
  32. package/dist/plugins/signing.js +565 -0
  33. package/dist/plugins/signing.js.map +1 -0
  34. package/dist/plugins/slug.d.ts +2 -0
  35. package/dist/plugins/slug.d.ts.map +1 -0
  36. package/dist/plugins/slug.js +180 -0
  37. package/dist/plugins/slug.js.map +1 -0
  38. package/dist/plugins/validation.d.ts +2 -0
  39. package/dist/plugins/validation.d.ts.map +1 -0
  40. package/dist/plugins/validation.js +105 -0
  41. package/dist/plugins/validation.js.map +1 -0
  42. package/dist/plugins/xml.d.ts +2 -0
  43. package/dist/plugins/xml.d.ts.map +1 -0
  44. package/dist/plugins/xml.js +293 -0
  45. package/dist/plugins/xml.js.map +1 -0
  46. package/dist/temp/compression.js +614 -0
  47. package/dist/temp/csv.js +154 -0
  48. package/dist/temp/datetime.js +196 -0
  49. package/dist/temp/diff.js +198 -0
  50. package/dist/temp/markdown.js +220 -0
  51. package/dist/temp/regex.js +140 -0
  52. package/dist/temp/signing.js +569 -0
  53. package/dist/temp/slug.js +198 -0
  54. package/dist/temp/validation.js +125 -0
  55. package/dist/temp/xml.js +289 -0
  56. package/dist/wasm/base64.wasm +0 -0
  57. package/dist/wasm/calc.wasm +0 -0
  58. package/dist/wasm/compression.wasm +0 -0
  59. package/dist/wasm/csv.wasm +0 -0
  60. package/dist/wasm/datetime.wasm +0 -0
  61. package/dist/wasm/diff.wasm +0 -0
  62. package/dist/wasm/hash.wasm +0 -0
  63. package/dist/wasm/json.wasm +0 -0
  64. package/dist/wasm/markdown.wasm +0 -0
  65. package/dist/wasm/regex.wasm +0 -0
  66. package/dist/wasm/signing.wasm +0 -0
  67. package/dist/wasm/slug.wasm +0 -0
  68. package/dist/wasm/validation.wasm +0 -0
  69. package/dist/wasm/xml.wasm +0 -0
  70. package/package.json +2 -2
@@ -0,0 +1,614 @@
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 literals = [];
157
+ const distances = [];
158
+ const lengths = [];
159
+ let pos = 0;
160
+ while (pos < len) {
161
+ const match = level > 3 ? findMatch(pos) : null;
162
+ const hash = getHash(pos);
163
+ if (!hashTable.has(hash)) {
164
+ hashTable.set(hash, []);
165
+ }
166
+ hashTable.get(hash).push(pos);
167
+ if (hashTable.get(hash).length > 64) {
168
+ hashTable.get(hash).shift();
169
+ }
170
+ if (match && match.length >= 3) {
171
+ literals.push(256 + match.length - 3);
172
+ distances.push(match.distance);
173
+ lengths.push(match.length);
174
+ for (let i = 1; i < match.length; i++) {
175
+ const h = getHash(pos + i);
176
+ if (!hashTable.has(h)) hashTable.set(h, []);
177
+ hashTable.get(h).push(pos + i);
178
+ }
179
+ pos += match.length;
180
+ } else {
181
+ literals.push(data[pos]);
182
+ distances.push(0);
183
+ lengths.push(0);
184
+ pos++;
185
+ }
186
+ }
187
+ literals.push(256);
188
+ let bitBuf = 0;
189
+ let bitCount = 0;
190
+ const writeBits = (value, bits) => {
191
+ bitBuf |= value << bitCount;
192
+ bitCount += bits;
193
+ while (bitCount >= 8) {
194
+ output.push(bitBuf & 255);
195
+ bitBuf >>= 8;
196
+ bitCount -= 8;
197
+ }
198
+ };
199
+ writeBits(1, 1);
200
+ writeBits(1, 2);
201
+ for (let i = 0; i < literals.length; i++) {
202
+ const lit = literals[i];
203
+ const dist = distances[i];
204
+ if (lit < 144) {
205
+ writeBits(reverseBits(48 + lit, 8), 8);
206
+ } else if (lit < 256) {
207
+ writeBits(reverseBits(400 + lit - 144, 9), 9);
208
+ } else if (lit === 256) {
209
+ writeBits(reverseBits(0, 7), 7);
210
+ } else {
211
+ const lengthCode = lit - 256;
212
+ if (lengthCode < 8) {
213
+ writeBits(reverseBits(1 + lengthCode, 7), 7);
214
+ } else {
215
+ writeBits(reverseBits(192 + lengthCode - 8, 8), 8);
216
+ }
217
+ if (dist > 0) {
218
+ let distCode = 0;
219
+ let distBits = 0;
220
+ let distBase = 1;
221
+ if (dist <= 4) {
222
+ distCode = dist - 1;
223
+ } else {
224
+ let d = dist - 1;
225
+ let extra = 0;
226
+ while (d >= 2 << extra) {
227
+ extra++;
228
+ }
229
+ distCode = 2 + extra * 2 + (d >= 3 << extra - 1 ? 1 : 0);
230
+ distBits = extra > 0 ? extra - 1 : 0;
231
+ distBase = (1 << distBits + 1) + 1;
232
+ }
233
+ writeBits(reverseBits(distCode, 5), 5);
234
+ if (distBits > 0) {
235
+ writeBits(dist - distBase, distBits);
236
+ }
237
+ }
238
+ }
239
+ }
240
+ if (bitCount > 0) {
241
+ output.push(bitBuf & 255);
242
+ }
243
+ return new Uint8Array(output);
244
+ }
245
+ function reverseBits(value, bits) {
246
+ let result = 0;
247
+ for (let i = 0; i < bits; i++) {
248
+ result = result << 1 | value >> i & 1;
249
+ }
250
+ return result;
251
+ }
252
+ function inflateRaw(data) {
253
+ const output = [];
254
+ let pos = 0;
255
+ let bitBuf = 0;
256
+ let bitCount = 0;
257
+ const readBits = (n) => {
258
+ while (bitCount < n) {
259
+ if (pos >= data.length) throw new Error("Unexpected end of data");
260
+ bitBuf |= data[pos++] << bitCount;
261
+ bitCount += 8;
262
+ }
263
+ const result = bitBuf & (1 << n) - 1;
264
+ bitBuf >>= n;
265
+ bitCount -= n;
266
+ return result;
267
+ };
268
+ const LITLEN_BASE = [
269
+ 3,
270
+ 4,
271
+ 5,
272
+ 6,
273
+ 7,
274
+ 8,
275
+ 9,
276
+ 10,
277
+ 11,
278
+ 13,
279
+ 15,
280
+ 17,
281
+ 19,
282
+ 23,
283
+ 27,
284
+ 31,
285
+ 35,
286
+ 43,
287
+ 51,
288
+ 59,
289
+ 67,
290
+ 83,
291
+ 99,
292
+ 115,
293
+ 131,
294
+ 163,
295
+ 195,
296
+ 227,
297
+ 258
298
+ ];
299
+ const LITLEN_EXTRA = [
300
+ 0,
301
+ 0,
302
+ 0,
303
+ 0,
304
+ 0,
305
+ 0,
306
+ 0,
307
+ 0,
308
+ 1,
309
+ 1,
310
+ 1,
311
+ 1,
312
+ 2,
313
+ 2,
314
+ 2,
315
+ 2,
316
+ 3,
317
+ 3,
318
+ 3,
319
+ 3,
320
+ 4,
321
+ 4,
322
+ 4,
323
+ 4,
324
+ 5,
325
+ 5,
326
+ 5,
327
+ 5,
328
+ 0
329
+ ];
330
+ const DIST_BASE = [
331
+ 1,
332
+ 2,
333
+ 3,
334
+ 4,
335
+ 5,
336
+ 7,
337
+ 9,
338
+ 13,
339
+ 17,
340
+ 25,
341
+ 33,
342
+ 49,
343
+ 65,
344
+ 97,
345
+ 129,
346
+ 193,
347
+ 257,
348
+ 385,
349
+ 513,
350
+ 769,
351
+ 1025,
352
+ 1537,
353
+ 2049,
354
+ 3073,
355
+ 4097,
356
+ 6145,
357
+ 8193,
358
+ 12289,
359
+ 16385,
360
+ 24577
361
+ ];
362
+ const DIST_EXTRA = [
363
+ 0,
364
+ 0,
365
+ 0,
366
+ 0,
367
+ 1,
368
+ 1,
369
+ 2,
370
+ 2,
371
+ 3,
372
+ 3,
373
+ 4,
374
+ 4,
375
+ 5,
376
+ 5,
377
+ 6,
378
+ 6,
379
+ 7,
380
+ 7,
381
+ 8,
382
+ 8,
383
+ 9,
384
+ 9,
385
+ 10,
386
+ 10,
387
+ 11,
388
+ 11,
389
+ 12,
390
+ 12,
391
+ 13,
392
+ 13
393
+ ];
394
+ let bfinal = 0;
395
+ while (!bfinal) {
396
+ bfinal = readBits(1);
397
+ const btype = readBits(2);
398
+ if (btype === 0) {
399
+ bitBuf = 0;
400
+ bitCount = 0;
401
+ const len = data[pos] | data[pos + 1] << 8;
402
+ pos += 4;
403
+ for (let i = 0; i < len; i++) {
404
+ output.push(data[pos++]);
405
+ }
406
+ } else if (btype === 1 || btype === 2) {
407
+ let litlenCodes;
408
+ let distCodes;
409
+ if (btype === 1) {
410
+ litlenCodes = [];
411
+ for (let i = 0; i <= 143; i++) litlenCodes[i] = 8;
412
+ for (let i = 144; i <= 255; i++) litlenCodes[i] = 9;
413
+ for (let i = 256; i <= 279; i++) litlenCodes[i] = 7;
414
+ for (let i = 280; i <= 287; i++) litlenCodes[i] = 8;
415
+ distCodes = new Array(32).fill(5);
416
+ } else {
417
+ const hlit = readBits(5) + 257;
418
+ const hdist = readBits(5) + 1;
419
+ const hclen = readBits(4) + 4;
420
+ const clenOrder = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
421
+ const clenLens = new Array(19).fill(0);
422
+ for (let i = 0; i < hclen; i++) {
423
+ clenLens[clenOrder[i]] = readBits(3);
424
+ }
425
+ const clenTree = buildHuffmanTree(clenLens);
426
+ const allLens = [];
427
+ while (allLens.length < hlit + hdist) {
428
+ const sym = decodeSymbol(clenTree, readBits.bind(null, 1));
429
+ if (sym < 16) {
430
+ allLens.push(sym);
431
+ } else if (sym === 16) {
432
+ const repeat = readBits(2) + 3;
433
+ const last = allLens[allLens.length - 1] || 0;
434
+ for (let i = 0; i < repeat; i++) allLens.push(last);
435
+ } else if (sym === 17) {
436
+ const repeat = readBits(3) + 3;
437
+ for (let i = 0; i < repeat; i++) allLens.push(0);
438
+ } else if (sym === 18) {
439
+ const repeat = readBits(7) + 11;
440
+ for (let i = 0; i < repeat; i++) allLens.push(0);
441
+ }
442
+ }
443
+ litlenCodes = allLens.slice(0, hlit);
444
+ distCodes = allLens.slice(hlit);
445
+ }
446
+ const litlenTree = buildHuffmanTree(litlenCodes);
447
+ const distTree = buildHuffmanTree(distCodes);
448
+ while (true) {
449
+ const sym = decodeSymbol(litlenTree, () => readBits(1));
450
+ if (sym < 256) {
451
+ output.push(sym);
452
+ } else if (sym === 256) {
453
+ break;
454
+ } else {
455
+ const lenIdx = sym - 257;
456
+ const length = LITLEN_BASE[lenIdx] + readBits(LITLEN_EXTRA[lenIdx]);
457
+ const distSym = decodeSymbol(distTree, () => readBits(1));
458
+ const distance = DIST_BASE[distSym] + readBits(DIST_EXTRA[distSym]);
459
+ const start = output.length - distance;
460
+ for (let i = 0; i < length; i++) {
461
+ output.push(output[start + i]);
462
+ }
463
+ }
464
+ }
465
+ } else {
466
+ throw new Error("Invalid block type");
467
+ }
468
+ }
469
+ return new Uint8Array(output);
470
+ }
471
+ function buildHuffmanTree(codeLens) {
472
+ const maxLen = Math.max(...codeLens, 1);
473
+ const blCount = new Array(maxLen + 1).fill(0);
474
+ for (const len of codeLens) {
475
+ if (len > 0) blCount[len]++;
476
+ }
477
+ const nextCode = new Array(maxLen + 1).fill(0);
478
+ let code = 0;
479
+ for (let bits = 1; bits <= maxLen; bits++) {
480
+ code = code + blCount[bits - 1] << 1;
481
+ nextCode[bits] = code;
482
+ }
483
+ const root = {};
484
+ for (let sym = 0; sym < codeLens.length; sym++) {
485
+ const len = codeLens[sym];
486
+ if (len === 0) continue;
487
+ const codeVal = nextCode[len]++;
488
+ let node = root;
489
+ for (let i = len - 1; i >= 0; i--) {
490
+ const bit = codeVal >> i & 1;
491
+ if (bit === 0) {
492
+ if (!node.left) node.left = {};
493
+ node = node.left;
494
+ } else {
495
+ if (!node.right) node.right = {};
496
+ node = node.right;
497
+ }
498
+ }
499
+ node.symbol = sym;
500
+ }
501
+ return root;
502
+ }
503
+ function decodeSymbol(tree, readBit) {
504
+ let node = tree;
505
+ while (node.symbol === void 0) {
506
+ const bit = readBit();
507
+ node = bit === 0 ? node.left : node.right;
508
+ if (!node) throw new Error("Invalid huffman code");
509
+ }
510
+ return node.symbol;
511
+ }
512
+ function crc32(data) {
513
+ let crc = 4294967295;
514
+ for (let i = 0; i < data.length; i++) {
515
+ crc ^= data[i];
516
+ for (let j = 0; j < 8; j++) {
517
+ crc = crc >>> 1 ^ (crc & 1 ? 3988292384 : 0);
518
+ }
519
+ }
520
+ return (crc ^ 4294967295) >>> 0;
521
+ }
522
+ function gzipCompress(data, level) {
523
+ const deflated = deflateRaw(data, level);
524
+ const crc = crc32(data);
525
+ const output = new Uint8Array(10 + deflated.length + 8);
526
+ output[0] = 31;
527
+ output[1] = 139;
528
+ output[2] = 8;
529
+ output[3] = 0;
530
+ output[4] = output[5] = output[6] = output[7] = 0;
531
+ output[8] = level >= 9 ? 2 : level <= 1 ? 4 : 0;
532
+ output[9] = 255;
533
+ output.set(deflated, 10);
534
+ const pos = 10 + deflated.length;
535
+ output[pos] = crc & 255;
536
+ output[pos + 1] = crc >> 8 & 255;
537
+ output[pos + 2] = crc >> 16 & 255;
538
+ output[pos + 3] = crc >> 24 & 255;
539
+ output[pos + 4] = data.length & 255;
540
+ output[pos + 5] = data.length >> 8 & 255;
541
+ output[pos + 6] = data.length >> 16 & 255;
542
+ output[pos + 7] = data.length >> 24 & 255;
543
+ return output;
544
+ }
545
+ function gzipDecompress(data) {
546
+ if (data[0] !== 31 || data[1] !== 139) {
547
+ throw new Error("Invalid gzip header");
548
+ }
549
+ if (data[2] !== 8) {
550
+ throw new Error("Unsupported compression method");
551
+ }
552
+ const flags = data[3];
553
+ let pos = 10;
554
+ if (flags & 4) {
555
+ const xlen = data[pos] | data[pos + 1] << 8;
556
+ pos += 2 + xlen;
557
+ }
558
+ if (flags & 8) {
559
+ while (data[pos++] !== 0) ;
560
+ }
561
+ if (flags & 16) {
562
+ while (data[pos++] !== 0) ;
563
+ }
564
+ if (flags & 2) {
565
+ pos += 2;
566
+ }
567
+ const deflatedData = data.slice(pos, data.length - 8);
568
+ return inflateRaw(deflatedData);
569
+ }
570
+ function compression() {
571
+ try {
572
+ const inputStr = Host.inputString();
573
+ const input = JSON.parse(inputStr);
574
+ const inputEncoding = input.inputEncoding ?? "utf8";
575
+ const outputEncoding = input.outputEncoding ?? "base64";
576
+ const level = input.level ?? 6;
577
+ let inputBytes;
578
+ if (inputEncoding === "base64") {
579
+ inputBytes = base64Decode(input.data);
580
+ } else {
581
+ inputBytes = utf8Encode(input.data);
582
+ }
583
+ let resultBytes;
584
+ if (input.operation === "compress") {
585
+ resultBytes = gzipCompress(inputBytes, level);
586
+ } else {
587
+ resultBytes = gzipDecompress(inputBytes);
588
+ }
589
+ let result;
590
+ if (outputEncoding === "base64") {
591
+ result = base64Encode(resultBytes);
592
+ } else {
593
+ result = utf8Decode(resultBytes);
594
+ }
595
+ const ratio = input.operation === "compress" ? resultBytes.length / inputBytes.length : inputBytes.length / resultBytes.length;
596
+ const output = {
597
+ result,
598
+ originalSize: inputBytes.length,
599
+ resultSize: resultBytes.length,
600
+ ratio: Math.round(ratio * 100) / 100
601
+ };
602
+ Host.outputString(JSON.stringify(output));
603
+ return 0;
604
+ } catch (error) {
605
+ const output = {
606
+ result: "",
607
+ originalSize: 0,
608
+ resultSize: 0,
609
+ error: error instanceof Error ? error.message : String(error)
610
+ };
611
+ Host.outputString(JSON.stringify(output));
612
+ return 1;
613
+ }
614
+ }