@docling/docling-client 2.149.0 → 2.150.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.
package/dist/index.cjs CHANGED
@@ -58,6 +58,7 @@ __export(index_exports, {
58
58
  appendFormValue: () => appendFormValue,
59
59
  applyPageLimits: () => applyPageLimits,
60
60
  documentFromBundle: () => documentFromBundle,
61
+ documentFromBundleAsync: () => documentFromBundleAsync,
61
62
  documentFromJson: () => documentFromJson,
62
63
  fileSourceFromBinary: () => fileSourceFromBinary,
63
64
  materializePresignedResult: () => materializePresignedResult,
@@ -74,7 +75,6 @@ __export(index_exports, {
74
75
  module.exports = __toCommonJS(index_exports);
75
76
 
76
77
  // src/artifacts.ts
77
- var import_fflate = require("fflate");
78
78
  var import_ipaddr = __toESM(require("ipaddr.js"), 1);
79
79
 
80
80
  // src/errors.ts
@@ -192,6 +192,611 @@ var DoclingBatchConversionError = class extends DoclingError {
192
192
  }
193
193
  };
194
194
 
195
+ // src/inflate.ts
196
+ var LENGTH_BASE = [
197
+ 3,
198
+ 4,
199
+ 5,
200
+ 6,
201
+ 7,
202
+ 8,
203
+ 9,
204
+ 10,
205
+ 11,
206
+ 13,
207
+ 15,
208
+ 17,
209
+ 19,
210
+ 23,
211
+ 27,
212
+ 31,
213
+ 35,
214
+ 43,
215
+ 51,
216
+ 59,
217
+ 67,
218
+ 83,
219
+ 99,
220
+ 115,
221
+ 131,
222
+ 163,
223
+ 195,
224
+ 227,
225
+ 258
226
+ ];
227
+ var LENGTH_EXTRA = [
228
+ 0,
229
+ 0,
230
+ 0,
231
+ 0,
232
+ 0,
233
+ 0,
234
+ 0,
235
+ 0,
236
+ 1,
237
+ 1,
238
+ 1,
239
+ 1,
240
+ 2,
241
+ 2,
242
+ 2,
243
+ 2,
244
+ 3,
245
+ 3,
246
+ 3,
247
+ 3,
248
+ 4,
249
+ 4,
250
+ 4,
251
+ 4,
252
+ 5,
253
+ 5,
254
+ 5,
255
+ 5,
256
+ 0
257
+ ];
258
+ var DISTANCE_BASE = [
259
+ 1,
260
+ 2,
261
+ 3,
262
+ 4,
263
+ 5,
264
+ 7,
265
+ 9,
266
+ 13,
267
+ 17,
268
+ 25,
269
+ 33,
270
+ 49,
271
+ 65,
272
+ 97,
273
+ 129,
274
+ 193,
275
+ 257,
276
+ 385,
277
+ 513,
278
+ 769,
279
+ 1025,
280
+ 1537,
281
+ 2049,
282
+ 3073,
283
+ 4097,
284
+ 6145,
285
+ 8193,
286
+ 12289,
287
+ 16385,
288
+ 24577
289
+ ];
290
+ var DISTANCE_EXTRA = [
291
+ 0,
292
+ 0,
293
+ 0,
294
+ 0,
295
+ 1,
296
+ 1,
297
+ 2,
298
+ 2,
299
+ 3,
300
+ 3,
301
+ 4,
302
+ 4,
303
+ 5,
304
+ 5,
305
+ 6,
306
+ 6,
307
+ 7,
308
+ 7,
309
+ 8,
310
+ 8,
311
+ 9,
312
+ 9,
313
+ 10,
314
+ 10,
315
+ 11,
316
+ 11,
317
+ 12,
318
+ 12,
319
+ 13,
320
+ 13
321
+ ];
322
+ var CODE_LENGTH_ORDER = [
323
+ 16,
324
+ 17,
325
+ 18,
326
+ 0,
327
+ 8,
328
+ 7,
329
+ 9,
330
+ 6,
331
+ 10,
332
+ 5,
333
+ 11,
334
+ 4,
335
+ 12,
336
+ 3,
337
+ 13,
338
+ 2,
339
+ 14,
340
+ 1,
341
+ 15
342
+ ];
343
+ var MAX_CODE_BITS = 15;
344
+ function buildHuffmanTable(lengths, count) {
345
+ const counts = new Uint16Array(MAX_CODE_BITS + 1);
346
+ let maxLength = 0;
347
+ for (let symbol = 0; symbol < count; symbol += 1) {
348
+ const length = lengths[symbol];
349
+ counts[length] = (counts[length] ?? 0) + 1;
350
+ if (length > maxLength) {
351
+ maxLength = length;
352
+ }
353
+ }
354
+ counts[0] = 0;
355
+ let left = 1;
356
+ for (let length = 1; length <= MAX_CODE_BITS; length += 1) {
357
+ left = (left << 1) - counts[length];
358
+ if (left < 0) {
359
+ throw new Error("Invalid DEFLATE data: oversubscribed Huffman code");
360
+ }
361
+ }
362
+ const table = new Uint16Array(1 << maxLength);
363
+ const nextCode = new Uint16Array(MAX_CODE_BITS + 2);
364
+ let code = 0;
365
+ for (let length = 1; length <= MAX_CODE_BITS; length += 1) {
366
+ code = code + counts[length - 1] << 1;
367
+ nextCode[length] = code;
368
+ }
369
+ for (let symbol = 0; symbol < count; symbol += 1) {
370
+ const length = lengths[symbol];
371
+ if (length === 0) {
372
+ continue;
373
+ }
374
+ const assigned = nextCode[length];
375
+ nextCode[length] = assigned + 1;
376
+ let reversed = 0;
377
+ for (let bit = 0; bit < length; bit += 1) {
378
+ reversed |= (assigned >> bit & 1) << length - 1 - bit;
379
+ }
380
+ const entry = symbol << 4 | length;
381
+ for (let index = reversed; index < table.length; index += 1 << length) {
382
+ table[index] = entry;
383
+ }
384
+ }
385
+ return { table, bits: maxLength };
386
+ }
387
+ var fixedTables;
388
+ function getFixedTables() {
389
+ if (fixedTables === void 0) {
390
+ const literalLengths = new Uint8Array(288);
391
+ literalLengths.fill(8, 0, 144);
392
+ literalLengths.fill(9, 144, 256);
393
+ literalLengths.fill(7, 256, 280);
394
+ literalLengths.fill(8, 280, 288);
395
+ fixedTables = {
396
+ literals: buildHuffmanTable(literalLengths, 288),
397
+ distances: buildHuffmanTable(new Uint8Array(30).fill(5), 30)
398
+ };
399
+ }
400
+ return fixedTables;
401
+ }
402
+ var Inflater = class {
403
+ #input;
404
+ #output;
405
+ #position = 0;
406
+ #outputPosition = 0;
407
+ #bitBuffer = 0;
408
+ #bitCount = 0;
409
+ constructor(input, outputSize) {
410
+ this.#input = input;
411
+ this.#output = new Uint8Array(outputSize);
412
+ }
413
+ run() {
414
+ let final = false;
415
+ while (!final) {
416
+ final = this.#readBits(1) === 1;
417
+ const type = this.#readBits(2);
418
+ if (type === 0) {
419
+ this.#stored();
420
+ } else if (type === 1) {
421
+ const { literals, distances } = getFixedTables();
422
+ this.#codes(literals, distances);
423
+ } else if (type === 2) {
424
+ this.#dynamic();
425
+ } else {
426
+ throw new Error("Invalid DEFLATE data: reserved block type");
427
+ }
428
+ }
429
+ if (this.#outputPosition !== this.#output.length) {
430
+ throw new Error("Invalid DEFLATE data: output is shorter than declared");
431
+ }
432
+ return this.#output;
433
+ }
434
+ #fill(needed) {
435
+ while (this.#bitCount < needed && this.#position < this.#input.length) {
436
+ this.#bitBuffer |= this.#input[this.#position] << this.#bitCount;
437
+ this.#position += 1;
438
+ this.#bitCount += 8;
439
+ }
440
+ }
441
+ #readBits(count) {
442
+ if (count === 0) {
443
+ return 0;
444
+ }
445
+ this.#fill(count);
446
+ if (this.#bitCount < count) {
447
+ throw new Error("Invalid DEFLATE data: unexpected end of input");
448
+ }
449
+ const value = this.#bitBuffer & (1 << count) - 1;
450
+ this.#bitBuffer >>>= count;
451
+ this.#bitCount -= count;
452
+ return value;
453
+ }
454
+ #decode(huffman) {
455
+ this.#fill(huffman.bits);
456
+ const entry = huffman.table[this.#bitBuffer & (1 << huffman.bits) - 1];
457
+ const length = entry & 15;
458
+ if (length === 0 || length > this.#bitCount) {
459
+ throw new Error("Invalid DEFLATE data: bad Huffman code");
460
+ }
461
+ this.#bitBuffer >>>= length;
462
+ this.#bitCount -= length;
463
+ return entry >> 4;
464
+ }
465
+ #stored() {
466
+ this.#position -= this.#bitCount >> 3;
467
+ this.#bitBuffer = 0;
468
+ this.#bitCount = 0;
469
+ if (this.#position + 4 > this.#input.length) {
470
+ throw new Error("Invalid DEFLATE data: unexpected end of input");
471
+ }
472
+ const length = this.#input[this.#position] | this.#input[this.#position + 1] << 8;
473
+ const inverse = this.#input[this.#position + 2] | this.#input[this.#position + 3] << 8;
474
+ if ((length ^ 65535) !== inverse) {
475
+ throw new Error("Invalid DEFLATE data: stored block length mismatch");
476
+ }
477
+ this.#position += 4;
478
+ if (this.#position + length > this.#input.length || this.#outputPosition + length > this.#output.length) {
479
+ throw new Error("Invalid DEFLATE data: stored block out of bounds");
480
+ }
481
+ this.#output.set(
482
+ this.#input.subarray(this.#position, this.#position + length),
483
+ this.#outputPosition
484
+ );
485
+ this.#position += length;
486
+ this.#outputPosition += length;
487
+ }
488
+ #dynamic() {
489
+ const literalCount = this.#readBits(5) + 257;
490
+ const distanceCount = this.#readBits(5) + 1;
491
+ const codeLengthCount = this.#readBits(4) + 4;
492
+ if (literalCount > 286 || distanceCount > 30) {
493
+ throw new Error("Invalid DEFLATE data: too many length or distance codes");
494
+ }
495
+ const codeLengthLengths = new Uint8Array(19);
496
+ for (let index2 = 0; index2 < codeLengthCount; index2 += 1) {
497
+ codeLengthLengths[CODE_LENGTH_ORDER[index2]] = this.#readBits(3);
498
+ }
499
+ const codeLengthTable = buildHuffmanTable(codeLengthLengths, 19);
500
+ const total = literalCount + distanceCount;
501
+ const lengths = new Uint8Array(total);
502
+ let index = 0;
503
+ while (index < total) {
504
+ const symbol = this.#decode(codeLengthTable);
505
+ if (symbol < 16) {
506
+ lengths[index] = symbol;
507
+ index += 1;
508
+ continue;
509
+ }
510
+ let repeat;
511
+ let value = 0;
512
+ if (symbol === 16) {
513
+ if (index === 0) {
514
+ throw new Error("Invalid DEFLATE data: repeat with no previous length");
515
+ }
516
+ value = lengths[index - 1];
517
+ repeat = 3 + this.#readBits(2);
518
+ } else if (symbol === 17) {
519
+ repeat = 3 + this.#readBits(3);
520
+ } else {
521
+ repeat = 11 + this.#readBits(7);
522
+ }
523
+ if (index + repeat > total) {
524
+ throw new Error("Invalid DEFLATE data: code length repeat overflows");
525
+ }
526
+ lengths.fill(value, index, index + repeat);
527
+ index += repeat;
528
+ }
529
+ if (lengths[256] === 0) {
530
+ throw new Error("Invalid DEFLATE data: missing end-of-block code");
531
+ }
532
+ this.#codes(
533
+ buildHuffmanTable(lengths.subarray(0, literalCount), literalCount),
534
+ buildHuffmanTable(lengths.subarray(literalCount), distanceCount)
535
+ );
536
+ }
537
+ #codes(literals, distances) {
538
+ const output = this.#output;
539
+ for (; ; ) {
540
+ const symbol = this.#decode(literals);
541
+ if (symbol < 256) {
542
+ if (this.#outputPosition >= output.length) {
543
+ throw new Error("Invalid DEFLATE data: output exceeds declared size");
544
+ }
545
+ output[this.#outputPosition] = symbol;
546
+ this.#outputPosition += 1;
547
+ continue;
548
+ }
549
+ if (symbol === 256) {
550
+ return;
551
+ }
552
+ const lengthIndex = symbol - 257;
553
+ if (lengthIndex >= 29) {
554
+ throw new Error("Invalid DEFLATE data: bad length symbol");
555
+ }
556
+ const length = LENGTH_BASE[lengthIndex] + this.#readBits(LENGTH_EXTRA[lengthIndex]);
557
+ const distanceIndex = this.#decode(distances);
558
+ if (distanceIndex >= 30) {
559
+ throw new Error("Invalid DEFLATE data: bad distance symbol");
560
+ }
561
+ const distance = DISTANCE_BASE[distanceIndex] + this.#readBits(DISTANCE_EXTRA[distanceIndex]);
562
+ if (distance > this.#outputPosition) {
563
+ throw new Error("Invalid DEFLATE data: distance too far back");
564
+ }
565
+ if (this.#outputPosition + length > output.length) {
566
+ throw new Error("Invalid DEFLATE data: output exceeds declared size");
567
+ }
568
+ let from = this.#outputPosition - distance;
569
+ for (let copied = 0; copied < length; copied += 1) {
570
+ output[this.#outputPosition] = output[from];
571
+ this.#outputPosition += 1;
572
+ from += 1;
573
+ }
574
+ }
575
+ }
576
+ };
577
+ function inflateRaw(input, outputSize) {
578
+ return new Inflater(input, outputSize).run();
579
+ }
580
+ async function inflateRawAsync(input, outputSize) {
581
+ let decompressor;
582
+ try {
583
+ decompressor = new DecompressionStream("deflate-raw");
584
+ } catch {
585
+ return inflateRaw(input, outputSize);
586
+ }
587
+ const source = new ReadableStream({
588
+ start(controller) {
589
+ controller.enqueue(input.slice());
590
+ controller.close();
591
+ }
592
+ });
593
+ const reader = source.pipeThrough(decompressor).getReader();
594
+ const output = new Uint8Array(outputSize);
595
+ let written = 0;
596
+ try {
597
+ for (; ; ) {
598
+ const { done, value } = await reader.read();
599
+ if (done) {
600
+ break;
601
+ }
602
+ if (written + value.length > outputSize) {
603
+ throw new Error("Invalid DEFLATE data: output exceeds declared size");
604
+ }
605
+ output.set(value, written);
606
+ written += value.length;
607
+ }
608
+ } finally {
609
+ await reader.cancel().catch(() => void 0);
610
+ }
611
+ if (written !== outputSize) {
612
+ throw new Error("Invalid DEFLATE data: output is shorter than declared");
613
+ }
614
+ return output;
615
+ }
616
+
617
+ // src/zip.ts
618
+ var EOCD_SIGNATURE = 101010256;
619
+ var EOCD64_LOCATOR_SIGNATURE = 117853008;
620
+ var EOCD64_SIGNATURE = 101075792;
621
+ var CENTRAL_SIGNATURE = 33639248;
622
+ var LOCAL_SIGNATURE = 67324752;
623
+ var EOCD_MIN_SIZE = 22;
624
+ var MAX_COMMENT_SIZE = 65535;
625
+ var MAX_DEFLATE_RATIO = 1032;
626
+ function readEntries(bytes, limits) {
627
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
628
+ const { entryCount, directoryOffset } = readEndOfCentralDirectory(bytes, view);
629
+ if (entryCount > limits.maxEntries) {
630
+ throw new DoclingArtifactDownloadError(
631
+ `Resource bundle exceeds ${limits.maxEntries} entries`
632
+ );
633
+ }
634
+ const entries = [];
635
+ let total = 0;
636
+ let cursor = directoryOffset;
637
+ for (let index = 0; index < entryCount; index += 1) {
638
+ requireRange(bytes, cursor, 46);
639
+ if (view.getUint32(cursor, true) !== CENTRAL_SIGNATURE) {
640
+ throw new Error("Invalid ZIP: bad central directory entry");
641
+ }
642
+ const flags = view.getUint16(cursor + 8, true);
643
+ const method = view.getUint16(cursor + 10, true);
644
+ let compressedSize = view.getUint32(cursor + 20, true);
645
+ let size = view.getUint32(cursor + 24, true);
646
+ const nameLength = view.getUint16(cursor + 28, true);
647
+ const extraLength = view.getUint16(cursor + 30, true);
648
+ const commentLength = view.getUint16(cursor + 32, true);
649
+ let localOffset = view.getUint32(cursor + 42, true);
650
+ requireRange(bytes, cursor + 46, nameLength + extraLength + commentLength);
651
+ const name = decodeName(
652
+ bytes.subarray(cursor + 46, cursor + 46 + nameLength),
653
+ (flags & 2048) !== 0
654
+ );
655
+ if (size === 4294967295 || compressedSize === 4294967295 || localOffset === 4294967295) {
656
+ const zip64 = readZip64Extra(
657
+ view,
658
+ cursor + 46 + nameLength,
659
+ extraLength,
660
+ size === 4294967295,
661
+ compressedSize === 4294967295,
662
+ localOffset === 4294967295
663
+ );
664
+ size = zip64.size ?? size;
665
+ compressedSize = zip64.compressedSize ?? compressedSize;
666
+ localOffset = zip64.localOffset ?? localOffset;
667
+ }
668
+ cursor += 46 + nameLength + extraLength + commentLength;
669
+ limits.validateName?.(name);
670
+ if ((flags & 1) !== 0) {
671
+ throw new Error(`Invalid ZIP: entry is encrypted: ${name}`);
672
+ }
673
+ if (method !== 0 && method !== 8) {
674
+ throw new Error(`Invalid ZIP: unsupported compression method ${method}`);
675
+ }
676
+ total += size;
677
+ if (total > limits.maxUncompressedBytes) {
678
+ throw new DoclingArtifactDownloadError(
679
+ `Resource bundle exceeds ${limits.maxUncompressedBytes} uncompressed bytes`
680
+ );
681
+ }
682
+ requireRange(bytes, localOffset, 30);
683
+ if (view.getUint32(localOffset, true) !== LOCAL_SIGNATURE) {
684
+ throw new Error("Invalid ZIP: bad local file header");
685
+ }
686
+ const dataStart = localOffset + 30 + view.getUint16(localOffset + 26, true) + view.getUint16(localOffset + 28, true);
687
+ requireRange(bytes, dataStart, compressedSize);
688
+ if (method === 0 && compressedSize !== size) {
689
+ throw new Error("Invalid ZIP: stored entry size mismatch");
690
+ }
691
+ if (method === 8 && size > compressedSize * MAX_DEFLATE_RATIO) {
692
+ throw new Error(
693
+ "Invalid ZIP: declared size is impossible for the compressed data"
694
+ );
695
+ }
696
+ entries.push({
697
+ name,
698
+ method,
699
+ size,
700
+ data: bytes.subarray(dataStart, dataStart + compressedSize)
701
+ });
702
+ }
703
+ return entries;
704
+ }
705
+ function newEntryMap() {
706
+ return /* @__PURE__ */ Object.create(null);
707
+ }
708
+ function unzipEntries(bytes, limits) {
709
+ const result = newEntryMap();
710
+ for (const entry of readEntries(bytes, limits)) {
711
+ result[entry.name] = entry.method === 0 ? entry.data.slice() : entry.size === 0 ? new Uint8Array(0) : inflateRaw(entry.data, entry.size);
712
+ }
713
+ return result;
714
+ }
715
+ async function unzipEntriesAsync(bytes, limits) {
716
+ const result = newEntryMap();
717
+ for (const entry of readEntries(bytes, limits)) {
718
+ result[entry.name] = entry.method === 0 ? entry.data.slice() : entry.size === 0 ? new Uint8Array(0) : await inflateRawAsync(entry.data, entry.size);
719
+ }
720
+ return result;
721
+ }
722
+ function readEndOfCentralDirectory(bytes, view) {
723
+ if (bytes.length < EOCD_MIN_SIZE) {
724
+ throw new Error("Invalid ZIP: too short");
725
+ }
726
+ const lowest = Math.max(0, bytes.length - EOCD_MIN_SIZE - MAX_COMMENT_SIZE);
727
+ let eocd = bytes.length - EOCD_MIN_SIZE;
728
+ while (eocd >= lowest && view.getUint32(eocd, true) !== EOCD_SIGNATURE) {
729
+ eocd -= 1;
730
+ }
731
+ if (eocd < lowest) {
732
+ throw new Error("Invalid ZIP: end of central directory not found");
733
+ }
734
+ if (view.getUint16(eocd + 4, true) !== 0 || view.getUint16(eocd + 6, true) !== 0) {
735
+ throw new Error("Invalid ZIP: multi-disk archives are not supported");
736
+ }
737
+ let entryCount = view.getUint16(eocd + 10, true);
738
+ let directoryOffset = view.getUint32(eocd + 16, true);
739
+ if ((entryCount === 65535 || directoryOffset === 4294967295) && eocd >= 20 && view.getUint32(eocd - 20, true) === EOCD64_LOCATOR_SIGNATURE) {
740
+ const recordOffset = toSafeNumber(view.getBigUint64(eocd - 20 + 8, true));
741
+ requireRange(bytes, recordOffset, 56);
742
+ if (view.getUint32(recordOffset, true) !== EOCD64_SIGNATURE) {
743
+ throw new Error("Invalid ZIP: bad ZIP64 end of central directory");
744
+ }
745
+ entryCount = toSafeNumber(view.getBigUint64(recordOffset + 32, true));
746
+ directoryOffset = toSafeNumber(view.getBigUint64(recordOffset + 48, true));
747
+ }
748
+ requireRange(bytes, directoryOffset, 0);
749
+ return { entryCount, directoryOffset };
750
+ }
751
+ function readZip64Extra(view, start, length, needSize, needCompressed, needOffset) {
752
+ const end = start + length;
753
+ let position = start;
754
+ while (position + 4 <= end) {
755
+ const id = view.getUint16(position, true);
756
+ const dataSize = view.getUint16(position + 2, true);
757
+ position += 4;
758
+ if (id === 1) {
759
+ let cursor = position;
760
+ const result = {};
761
+ const next = () => {
762
+ if (cursor + 8 > position + dataSize) {
763
+ throw new Error("Invalid ZIP: truncated ZIP64 extra field");
764
+ }
765
+ const value = toSafeNumber(view.getBigUint64(cursor, true));
766
+ cursor += 8;
767
+ return value;
768
+ };
769
+ if (needSize) result.size = next();
770
+ if (needCompressed) result.compressedSize = next();
771
+ if (needOffset) result.localOffset = next();
772
+ return result;
773
+ }
774
+ position += dataSize;
775
+ }
776
+ throw new Error("Invalid ZIP: missing ZIP64 extra field");
777
+ }
778
+ function toSafeNumber(value) {
779
+ if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
780
+ throw new Error("Invalid ZIP: value out of range");
781
+ }
782
+ return Number(value);
783
+ }
784
+ function requireRange(bytes, offset, length) {
785
+ if (offset < 0 || length < 0 || offset + length > bytes.length) {
786
+ throw new Error("Invalid ZIP: truncated or out-of-range data");
787
+ }
788
+ }
789
+ function decodeName(raw, utf8) {
790
+ if (utf8) {
791
+ return new TextDecoder().decode(raw);
792
+ }
793
+ let result = "";
794
+ for (const byte of raw) {
795
+ result += String.fromCharCode(byte);
796
+ }
797
+ return result;
798
+ }
799
+
195
800
  // src/artifacts.ts
196
801
  function parseArtifactOrigin(value) {
197
802
  try {
@@ -359,7 +964,7 @@ async function materializePresignedResult(response, input, downloader, signal) {
359
964
  try {
360
965
  const artifact = selectDocumentArtifact(item);
361
966
  const bytes = await downloader.download(artifact.uri, signal);
362
- const document = artifact.artifact_type === "resource_bundle" ? documentFromBundle(bytes) : documentFromJson(bytes);
967
+ const document = artifact.artifact_type === "resource_bundle" ? await documentFromBundleAsync(bytes) : documentFromJson(bytes);
363
968
  return conversionFromArtifactItem(item, input, response.processing_time, document);
364
969
  } catch (error) {
365
970
  if (signal?.aborted === true || isAbortError(error)) {
@@ -387,19 +992,34 @@ function selectDocumentArtifact(item) {
387
992
  "Presigned result exposes neither a 'json' nor a 'resource_bundle' artifact"
388
993
  );
389
994
  }
995
+ async function documentFromBundleAsync(bytes) {
996
+ let entries;
997
+ try {
998
+ entries = await unzipEntriesAsync(bytes, BUNDLE_LIMITS);
999
+ } catch (error) {
1000
+ throw invalidBundleError(error);
1001
+ }
1002
+ return documentFromEntries(entries);
1003
+ }
390
1004
  function documentFromBundle(bytes) {
391
1005
  let entries;
392
1006
  try {
393
1007
  entries = unzipBundle(bytes);
394
1008
  } catch (error) {
395
- if (error instanceof DoclingArtifactDownloadError) {
396
- throw error;
397
- }
398
- throw new DoclingArtifactDownloadError(
399
- "Downloaded resource bundle is not a valid ZIP",
400
- { cause: error }
401
- );
1009
+ throw invalidBundleError(error);
402
1010
  }
1011
+ return documentFromEntries(entries);
1012
+ }
1013
+ function invalidBundleError(error) {
1014
+ if (error instanceof DoclingArtifactDownloadError) {
1015
+ return error;
1016
+ }
1017
+ return new DoclingArtifactDownloadError(
1018
+ "Downloaded resource bundle is not a valid ZIP",
1019
+ { cause: error }
1020
+ );
1021
+ }
1022
+ function documentFromEntries(entries) {
403
1023
  for (const name of Object.keys(entries)) {
404
1024
  validateBundlePath(name);
405
1025
  }
@@ -556,49 +1176,13 @@ function isSupportedImage(mimeType, bytes) {
556
1176
  function startsWith(bytes, prefix) {
557
1177
  return bytes.length >= prefix.length && prefix.every((value, index) => bytes[index] === value);
558
1178
  }
1179
+ var BUNDLE_LIMITS = {
1180
+ maxEntries: 1e4,
1181
+ maxUncompressedBytes: 512 * 1024 * 1024,
1182
+ validateName: validateBundlePath
1183
+ };
559
1184
  function unzipBundle(bytes) {
560
- const entries = {};
561
- const maxEntries = 1e4;
562
- const maxUncompressedBytes = 512 * 1024 * 1024;
563
- let entryCount = 0;
564
- let total = 0;
565
- const archive = new import_fflate.Unzip((file) => {
566
- entryCount += 1;
567
- if (entryCount > maxEntries) {
568
- throw new DoclingArtifactDownloadError(
569
- `Resource bundle exceeds ${maxEntries} entries`
570
- );
571
- }
572
- validateBundlePath(file.name);
573
- const chunks = [];
574
- file.ondata = (error, chunk, final) => {
575
- if (error !== null) {
576
- throw error;
577
- }
578
- total += chunk.byteLength;
579
- if (total > maxUncompressedBytes) {
580
- throw new DoclingArtifactDownloadError(
581
- `Resource bundle exceeds ${maxUncompressedBytes} uncompressed bytes`
582
- );
583
- }
584
- chunks.push(chunk);
585
- if (final) {
586
- const content = new Uint8Array(
587
- chunks.reduce((sum, value) => sum + value.byteLength, 0)
588
- );
589
- let offset = 0;
590
- for (const value of chunks) {
591
- content.set(value, offset);
592
- offset += value.byteLength;
593
- }
594
- entries[file.name] = content;
595
- }
596
- };
597
- file.start();
598
- });
599
- archive.register(import_fflate.UnzipInflate);
600
- archive.push(bytes, true);
601
- return entries;
1185
+ return unzipEntries(bytes, BUNDLE_LIMITS);
602
1186
  }
603
1187
  function isRecord(value) {
604
1188
  return value !== null && typeof value === "object" && !Array.isArray(value);
@@ -4336,6 +4920,7 @@ function isRecord4(value) {
4336
4920
  appendFormValue,
4337
4921
  applyPageLimits,
4338
4922
  documentFromBundle,
4923
+ documentFromBundleAsync,
4339
4924
  documentFromJson,
4340
4925
  fileSourceFromBinary,
4341
4926
  materializePresignedResult,