@docling/docling-client 2.148.0 → 2.149.1
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/PYTHON_CLIENT_PARITY.md +36 -13
- package/dist/index.cjs +636 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -1
- package/dist/index.d.ts +14 -1
- package/dist/index.js +635 -51
- package/dist/index.js.map +1 -1
- package/package.json +1 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// src/artifacts.ts
|
|
2
|
-
import { Unzip, UnzipInflate } from "fflate";
|
|
3
2
|
import ipaddr from "ipaddr.js";
|
|
4
3
|
|
|
5
4
|
// src/errors.ts
|
|
@@ -117,6 +116,611 @@ var DoclingBatchConversionError = class extends DoclingError {
|
|
|
117
116
|
}
|
|
118
117
|
};
|
|
119
118
|
|
|
119
|
+
// src/inflate.ts
|
|
120
|
+
var LENGTH_BASE = [
|
|
121
|
+
3,
|
|
122
|
+
4,
|
|
123
|
+
5,
|
|
124
|
+
6,
|
|
125
|
+
7,
|
|
126
|
+
8,
|
|
127
|
+
9,
|
|
128
|
+
10,
|
|
129
|
+
11,
|
|
130
|
+
13,
|
|
131
|
+
15,
|
|
132
|
+
17,
|
|
133
|
+
19,
|
|
134
|
+
23,
|
|
135
|
+
27,
|
|
136
|
+
31,
|
|
137
|
+
35,
|
|
138
|
+
43,
|
|
139
|
+
51,
|
|
140
|
+
59,
|
|
141
|
+
67,
|
|
142
|
+
83,
|
|
143
|
+
99,
|
|
144
|
+
115,
|
|
145
|
+
131,
|
|
146
|
+
163,
|
|
147
|
+
195,
|
|
148
|
+
227,
|
|
149
|
+
258
|
|
150
|
+
];
|
|
151
|
+
var LENGTH_EXTRA = [
|
|
152
|
+
0,
|
|
153
|
+
0,
|
|
154
|
+
0,
|
|
155
|
+
0,
|
|
156
|
+
0,
|
|
157
|
+
0,
|
|
158
|
+
0,
|
|
159
|
+
0,
|
|
160
|
+
1,
|
|
161
|
+
1,
|
|
162
|
+
1,
|
|
163
|
+
1,
|
|
164
|
+
2,
|
|
165
|
+
2,
|
|
166
|
+
2,
|
|
167
|
+
2,
|
|
168
|
+
3,
|
|
169
|
+
3,
|
|
170
|
+
3,
|
|
171
|
+
3,
|
|
172
|
+
4,
|
|
173
|
+
4,
|
|
174
|
+
4,
|
|
175
|
+
4,
|
|
176
|
+
5,
|
|
177
|
+
5,
|
|
178
|
+
5,
|
|
179
|
+
5,
|
|
180
|
+
0
|
|
181
|
+
];
|
|
182
|
+
var DISTANCE_BASE = [
|
|
183
|
+
1,
|
|
184
|
+
2,
|
|
185
|
+
3,
|
|
186
|
+
4,
|
|
187
|
+
5,
|
|
188
|
+
7,
|
|
189
|
+
9,
|
|
190
|
+
13,
|
|
191
|
+
17,
|
|
192
|
+
25,
|
|
193
|
+
33,
|
|
194
|
+
49,
|
|
195
|
+
65,
|
|
196
|
+
97,
|
|
197
|
+
129,
|
|
198
|
+
193,
|
|
199
|
+
257,
|
|
200
|
+
385,
|
|
201
|
+
513,
|
|
202
|
+
769,
|
|
203
|
+
1025,
|
|
204
|
+
1537,
|
|
205
|
+
2049,
|
|
206
|
+
3073,
|
|
207
|
+
4097,
|
|
208
|
+
6145,
|
|
209
|
+
8193,
|
|
210
|
+
12289,
|
|
211
|
+
16385,
|
|
212
|
+
24577
|
|
213
|
+
];
|
|
214
|
+
var DISTANCE_EXTRA = [
|
|
215
|
+
0,
|
|
216
|
+
0,
|
|
217
|
+
0,
|
|
218
|
+
0,
|
|
219
|
+
1,
|
|
220
|
+
1,
|
|
221
|
+
2,
|
|
222
|
+
2,
|
|
223
|
+
3,
|
|
224
|
+
3,
|
|
225
|
+
4,
|
|
226
|
+
4,
|
|
227
|
+
5,
|
|
228
|
+
5,
|
|
229
|
+
6,
|
|
230
|
+
6,
|
|
231
|
+
7,
|
|
232
|
+
7,
|
|
233
|
+
8,
|
|
234
|
+
8,
|
|
235
|
+
9,
|
|
236
|
+
9,
|
|
237
|
+
10,
|
|
238
|
+
10,
|
|
239
|
+
11,
|
|
240
|
+
11,
|
|
241
|
+
12,
|
|
242
|
+
12,
|
|
243
|
+
13,
|
|
244
|
+
13
|
|
245
|
+
];
|
|
246
|
+
var CODE_LENGTH_ORDER = [
|
|
247
|
+
16,
|
|
248
|
+
17,
|
|
249
|
+
18,
|
|
250
|
+
0,
|
|
251
|
+
8,
|
|
252
|
+
7,
|
|
253
|
+
9,
|
|
254
|
+
6,
|
|
255
|
+
10,
|
|
256
|
+
5,
|
|
257
|
+
11,
|
|
258
|
+
4,
|
|
259
|
+
12,
|
|
260
|
+
3,
|
|
261
|
+
13,
|
|
262
|
+
2,
|
|
263
|
+
14,
|
|
264
|
+
1,
|
|
265
|
+
15
|
|
266
|
+
];
|
|
267
|
+
var MAX_CODE_BITS = 15;
|
|
268
|
+
function buildHuffmanTable(lengths, count) {
|
|
269
|
+
const counts = new Uint16Array(MAX_CODE_BITS + 1);
|
|
270
|
+
let maxLength = 0;
|
|
271
|
+
for (let symbol = 0; symbol < count; symbol += 1) {
|
|
272
|
+
const length = lengths[symbol];
|
|
273
|
+
counts[length] = (counts[length] ?? 0) + 1;
|
|
274
|
+
if (length > maxLength) {
|
|
275
|
+
maxLength = length;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
counts[0] = 0;
|
|
279
|
+
let left = 1;
|
|
280
|
+
for (let length = 1; length <= MAX_CODE_BITS; length += 1) {
|
|
281
|
+
left = (left << 1) - counts[length];
|
|
282
|
+
if (left < 0) {
|
|
283
|
+
throw new Error("Invalid DEFLATE data: oversubscribed Huffman code");
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
const table = new Uint16Array(1 << maxLength);
|
|
287
|
+
const nextCode = new Uint16Array(MAX_CODE_BITS + 2);
|
|
288
|
+
let code = 0;
|
|
289
|
+
for (let length = 1; length <= MAX_CODE_BITS; length += 1) {
|
|
290
|
+
code = code + counts[length - 1] << 1;
|
|
291
|
+
nextCode[length] = code;
|
|
292
|
+
}
|
|
293
|
+
for (let symbol = 0; symbol < count; symbol += 1) {
|
|
294
|
+
const length = lengths[symbol];
|
|
295
|
+
if (length === 0) {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const assigned = nextCode[length];
|
|
299
|
+
nextCode[length] = assigned + 1;
|
|
300
|
+
let reversed = 0;
|
|
301
|
+
for (let bit = 0; bit < length; bit += 1) {
|
|
302
|
+
reversed |= (assigned >> bit & 1) << length - 1 - bit;
|
|
303
|
+
}
|
|
304
|
+
const entry = symbol << 4 | length;
|
|
305
|
+
for (let index = reversed; index < table.length; index += 1 << length) {
|
|
306
|
+
table[index] = entry;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return { table, bits: maxLength };
|
|
310
|
+
}
|
|
311
|
+
var fixedTables;
|
|
312
|
+
function getFixedTables() {
|
|
313
|
+
if (fixedTables === void 0) {
|
|
314
|
+
const literalLengths = new Uint8Array(288);
|
|
315
|
+
literalLengths.fill(8, 0, 144);
|
|
316
|
+
literalLengths.fill(9, 144, 256);
|
|
317
|
+
literalLengths.fill(7, 256, 280);
|
|
318
|
+
literalLengths.fill(8, 280, 288);
|
|
319
|
+
fixedTables = {
|
|
320
|
+
literals: buildHuffmanTable(literalLengths, 288),
|
|
321
|
+
distances: buildHuffmanTable(new Uint8Array(30).fill(5), 30)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
return fixedTables;
|
|
325
|
+
}
|
|
326
|
+
var Inflater = class {
|
|
327
|
+
#input;
|
|
328
|
+
#output;
|
|
329
|
+
#position = 0;
|
|
330
|
+
#outputPosition = 0;
|
|
331
|
+
#bitBuffer = 0;
|
|
332
|
+
#bitCount = 0;
|
|
333
|
+
constructor(input, outputSize) {
|
|
334
|
+
this.#input = input;
|
|
335
|
+
this.#output = new Uint8Array(outputSize);
|
|
336
|
+
}
|
|
337
|
+
run() {
|
|
338
|
+
let final = false;
|
|
339
|
+
while (!final) {
|
|
340
|
+
final = this.#readBits(1) === 1;
|
|
341
|
+
const type = this.#readBits(2);
|
|
342
|
+
if (type === 0) {
|
|
343
|
+
this.#stored();
|
|
344
|
+
} else if (type === 1) {
|
|
345
|
+
const { literals, distances } = getFixedTables();
|
|
346
|
+
this.#codes(literals, distances);
|
|
347
|
+
} else if (type === 2) {
|
|
348
|
+
this.#dynamic();
|
|
349
|
+
} else {
|
|
350
|
+
throw new Error("Invalid DEFLATE data: reserved block type");
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (this.#outputPosition !== this.#output.length) {
|
|
354
|
+
throw new Error("Invalid DEFLATE data: output is shorter than declared");
|
|
355
|
+
}
|
|
356
|
+
return this.#output;
|
|
357
|
+
}
|
|
358
|
+
#fill(needed) {
|
|
359
|
+
while (this.#bitCount < needed && this.#position < this.#input.length) {
|
|
360
|
+
this.#bitBuffer |= this.#input[this.#position] << this.#bitCount;
|
|
361
|
+
this.#position += 1;
|
|
362
|
+
this.#bitCount += 8;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
#readBits(count) {
|
|
366
|
+
if (count === 0) {
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
this.#fill(count);
|
|
370
|
+
if (this.#bitCount < count) {
|
|
371
|
+
throw new Error("Invalid DEFLATE data: unexpected end of input");
|
|
372
|
+
}
|
|
373
|
+
const value = this.#bitBuffer & (1 << count) - 1;
|
|
374
|
+
this.#bitBuffer >>>= count;
|
|
375
|
+
this.#bitCount -= count;
|
|
376
|
+
return value;
|
|
377
|
+
}
|
|
378
|
+
#decode(huffman) {
|
|
379
|
+
this.#fill(huffman.bits);
|
|
380
|
+
const entry = huffman.table[this.#bitBuffer & (1 << huffman.bits) - 1];
|
|
381
|
+
const length = entry & 15;
|
|
382
|
+
if (length === 0 || length > this.#bitCount) {
|
|
383
|
+
throw new Error("Invalid DEFLATE data: bad Huffman code");
|
|
384
|
+
}
|
|
385
|
+
this.#bitBuffer >>>= length;
|
|
386
|
+
this.#bitCount -= length;
|
|
387
|
+
return entry >> 4;
|
|
388
|
+
}
|
|
389
|
+
#stored() {
|
|
390
|
+
this.#position -= this.#bitCount >> 3;
|
|
391
|
+
this.#bitBuffer = 0;
|
|
392
|
+
this.#bitCount = 0;
|
|
393
|
+
if (this.#position + 4 > this.#input.length) {
|
|
394
|
+
throw new Error("Invalid DEFLATE data: unexpected end of input");
|
|
395
|
+
}
|
|
396
|
+
const length = this.#input[this.#position] | this.#input[this.#position + 1] << 8;
|
|
397
|
+
const inverse = this.#input[this.#position + 2] | this.#input[this.#position + 3] << 8;
|
|
398
|
+
if ((length ^ 65535) !== inverse) {
|
|
399
|
+
throw new Error("Invalid DEFLATE data: stored block length mismatch");
|
|
400
|
+
}
|
|
401
|
+
this.#position += 4;
|
|
402
|
+
if (this.#position + length > this.#input.length || this.#outputPosition + length > this.#output.length) {
|
|
403
|
+
throw new Error("Invalid DEFLATE data: stored block out of bounds");
|
|
404
|
+
}
|
|
405
|
+
this.#output.set(
|
|
406
|
+
this.#input.subarray(this.#position, this.#position + length),
|
|
407
|
+
this.#outputPosition
|
|
408
|
+
);
|
|
409
|
+
this.#position += length;
|
|
410
|
+
this.#outputPosition += length;
|
|
411
|
+
}
|
|
412
|
+
#dynamic() {
|
|
413
|
+
const literalCount = this.#readBits(5) + 257;
|
|
414
|
+
const distanceCount = this.#readBits(5) + 1;
|
|
415
|
+
const codeLengthCount = this.#readBits(4) + 4;
|
|
416
|
+
if (literalCount > 286 || distanceCount > 30) {
|
|
417
|
+
throw new Error("Invalid DEFLATE data: too many length or distance codes");
|
|
418
|
+
}
|
|
419
|
+
const codeLengthLengths = new Uint8Array(19);
|
|
420
|
+
for (let index2 = 0; index2 < codeLengthCount; index2 += 1) {
|
|
421
|
+
codeLengthLengths[CODE_LENGTH_ORDER[index2]] = this.#readBits(3);
|
|
422
|
+
}
|
|
423
|
+
const codeLengthTable = buildHuffmanTable(codeLengthLengths, 19);
|
|
424
|
+
const total = literalCount + distanceCount;
|
|
425
|
+
const lengths = new Uint8Array(total);
|
|
426
|
+
let index = 0;
|
|
427
|
+
while (index < total) {
|
|
428
|
+
const symbol = this.#decode(codeLengthTable);
|
|
429
|
+
if (symbol < 16) {
|
|
430
|
+
lengths[index] = symbol;
|
|
431
|
+
index += 1;
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
let repeat;
|
|
435
|
+
let value = 0;
|
|
436
|
+
if (symbol === 16) {
|
|
437
|
+
if (index === 0) {
|
|
438
|
+
throw new Error("Invalid DEFLATE data: repeat with no previous length");
|
|
439
|
+
}
|
|
440
|
+
value = lengths[index - 1];
|
|
441
|
+
repeat = 3 + this.#readBits(2);
|
|
442
|
+
} else if (symbol === 17) {
|
|
443
|
+
repeat = 3 + this.#readBits(3);
|
|
444
|
+
} else {
|
|
445
|
+
repeat = 11 + this.#readBits(7);
|
|
446
|
+
}
|
|
447
|
+
if (index + repeat > total) {
|
|
448
|
+
throw new Error("Invalid DEFLATE data: code length repeat overflows");
|
|
449
|
+
}
|
|
450
|
+
lengths.fill(value, index, index + repeat);
|
|
451
|
+
index += repeat;
|
|
452
|
+
}
|
|
453
|
+
if (lengths[256] === 0) {
|
|
454
|
+
throw new Error("Invalid DEFLATE data: missing end-of-block code");
|
|
455
|
+
}
|
|
456
|
+
this.#codes(
|
|
457
|
+
buildHuffmanTable(lengths.subarray(0, literalCount), literalCount),
|
|
458
|
+
buildHuffmanTable(lengths.subarray(literalCount), distanceCount)
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
#codes(literals, distances) {
|
|
462
|
+
const output = this.#output;
|
|
463
|
+
for (; ; ) {
|
|
464
|
+
const symbol = this.#decode(literals);
|
|
465
|
+
if (symbol < 256) {
|
|
466
|
+
if (this.#outputPosition >= output.length) {
|
|
467
|
+
throw new Error("Invalid DEFLATE data: output exceeds declared size");
|
|
468
|
+
}
|
|
469
|
+
output[this.#outputPosition] = symbol;
|
|
470
|
+
this.#outputPosition += 1;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (symbol === 256) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
const lengthIndex = symbol - 257;
|
|
477
|
+
if (lengthIndex >= 29) {
|
|
478
|
+
throw new Error("Invalid DEFLATE data: bad length symbol");
|
|
479
|
+
}
|
|
480
|
+
const length = LENGTH_BASE[lengthIndex] + this.#readBits(LENGTH_EXTRA[lengthIndex]);
|
|
481
|
+
const distanceIndex = this.#decode(distances);
|
|
482
|
+
if (distanceIndex >= 30) {
|
|
483
|
+
throw new Error("Invalid DEFLATE data: bad distance symbol");
|
|
484
|
+
}
|
|
485
|
+
const distance = DISTANCE_BASE[distanceIndex] + this.#readBits(DISTANCE_EXTRA[distanceIndex]);
|
|
486
|
+
if (distance > this.#outputPosition) {
|
|
487
|
+
throw new Error("Invalid DEFLATE data: distance too far back");
|
|
488
|
+
}
|
|
489
|
+
if (this.#outputPosition + length > output.length) {
|
|
490
|
+
throw new Error("Invalid DEFLATE data: output exceeds declared size");
|
|
491
|
+
}
|
|
492
|
+
let from = this.#outputPosition - distance;
|
|
493
|
+
for (let copied = 0; copied < length; copied += 1) {
|
|
494
|
+
output[this.#outputPosition] = output[from];
|
|
495
|
+
this.#outputPosition += 1;
|
|
496
|
+
from += 1;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
function inflateRaw(input, outputSize) {
|
|
502
|
+
return new Inflater(input, outputSize).run();
|
|
503
|
+
}
|
|
504
|
+
async function inflateRawAsync(input, outputSize) {
|
|
505
|
+
let decompressor;
|
|
506
|
+
try {
|
|
507
|
+
decompressor = new DecompressionStream("deflate-raw");
|
|
508
|
+
} catch {
|
|
509
|
+
return inflateRaw(input, outputSize);
|
|
510
|
+
}
|
|
511
|
+
const source = new ReadableStream({
|
|
512
|
+
start(controller) {
|
|
513
|
+
controller.enqueue(input.slice());
|
|
514
|
+
controller.close();
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
const reader = source.pipeThrough(decompressor).getReader();
|
|
518
|
+
const output = new Uint8Array(outputSize);
|
|
519
|
+
let written = 0;
|
|
520
|
+
try {
|
|
521
|
+
for (; ; ) {
|
|
522
|
+
const { done, value } = await reader.read();
|
|
523
|
+
if (done) {
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
if (written + value.length > outputSize) {
|
|
527
|
+
throw new Error("Invalid DEFLATE data: output exceeds declared size");
|
|
528
|
+
}
|
|
529
|
+
output.set(value, written);
|
|
530
|
+
written += value.length;
|
|
531
|
+
}
|
|
532
|
+
} finally {
|
|
533
|
+
await reader.cancel().catch(() => void 0);
|
|
534
|
+
}
|
|
535
|
+
if (written !== outputSize) {
|
|
536
|
+
throw new Error("Invalid DEFLATE data: output is shorter than declared");
|
|
537
|
+
}
|
|
538
|
+
return output;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// src/zip.ts
|
|
542
|
+
var EOCD_SIGNATURE = 101010256;
|
|
543
|
+
var EOCD64_LOCATOR_SIGNATURE = 117853008;
|
|
544
|
+
var EOCD64_SIGNATURE = 101075792;
|
|
545
|
+
var CENTRAL_SIGNATURE = 33639248;
|
|
546
|
+
var LOCAL_SIGNATURE = 67324752;
|
|
547
|
+
var EOCD_MIN_SIZE = 22;
|
|
548
|
+
var MAX_COMMENT_SIZE = 65535;
|
|
549
|
+
var MAX_DEFLATE_RATIO = 1032;
|
|
550
|
+
function readEntries(bytes, limits) {
|
|
551
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
552
|
+
const { entryCount, directoryOffset } = readEndOfCentralDirectory(bytes, view);
|
|
553
|
+
if (entryCount > limits.maxEntries) {
|
|
554
|
+
throw new DoclingArtifactDownloadError(
|
|
555
|
+
`Resource bundle exceeds ${limits.maxEntries} entries`
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
const entries = [];
|
|
559
|
+
let total = 0;
|
|
560
|
+
let cursor = directoryOffset;
|
|
561
|
+
for (let index = 0; index < entryCount; index += 1) {
|
|
562
|
+
requireRange(bytes, cursor, 46);
|
|
563
|
+
if (view.getUint32(cursor, true) !== CENTRAL_SIGNATURE) {
|
|
564
|
+
throw new Error("Invalid ZIP: bad central directory entry");
|
|
565
|
+
}
|
|
566
|
+
const flags = view.getUint16(cursor + 8, true);
|
|
567
|
+
const method = view.getUint16(cursor + 10, true);
|
|
568
|
+
let compressedSize = view.getUint32(cursor + 20, true);
|
|
569
|
+
let size = view.getUint32(cursor + 24, true);
|
|
570
|
+
const nameLength = view.getUint16(cursor + 28, true);
|
|
571
|
+
const extraLength = view.getUint16(cursor + 30, true);
|
|
572
|
+
const commentLength = view.getUint16(cursor + 32, true);
|
|
573
|
+
let localOffset = view.getUint32(cursor + 42, true);
|
|
574
|
+
requireRange(bytes, cursor + 46, nameLength + extraLength + commentLength);
|
|
575
|
+
const name = decodeName(
|
|
576
|
+
bytes.subarray(cursor + 46, cursor + 46 + nameLength),
|
|
577
|
+
(flags & 2048) !== 0
|
|
578
|
+
);
|
|
579
|
+
if (size === 4294967295 || compressedSize === 4294967295 || localOffset === 4294967295) {
|
|
580
|
+
const zip64 = readZip64Extra(
|
|
581
|
+
view,
|
|
582
|
+
cursor + 46 + nameLength,
|
|
583
|
+
extraLength,
|
|
584
|
+
size === 4294967295,
|
|
585
|
+
compressedSize === 4294967295,
|
|
586
|
+
localOffset === 4294967295
|
|
587
|
+
);
|
|
588
|
+
size = zip64.size ?? size;
|
|
589
|
+
compressedSize = zip64.compressedSize ?? compressedSize;
|
|
590
|
+
localOffset = zip64.localOffset ?? localOffset;
|
|
591
|
+
}
|
|
592
|
+
cursor += 46 + nameLength + extraLength + commentLength;
|
|
593
|
+
limits.validateName?.(name);
|
|
594
|
+
if ((flags & 1) !== 0) {
|
|
595
|
+
throw new Error(`Invalid ZIP: entry is encrypted: ${name}`);
|
|
596
|
+
}
|
|
597
|
+
if (method !== 0 && method !== 8) {
|
|
598
|
+
throw new Error(`Invalid ZIP: unsupported compression method ${method}`);
|
|
599
|
+
}
|
|
600
|
+
total += size;
|
|
601
|
+
if (total > limits.maxUncompressedBytes) {
|
|
602
|
+
throw new DoclingArtifactDownloadError(
|
|
603
|
+
`Resource bundle exceeds ${limits.maxUncompressedBytes} uncompressed bytes`
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
requireRange(bytes, localOffset, 30);
|
|
607
|
+
if (view.getUint32(localOffset, true) !== LOCAL_SIGNATURE) {
|
|
608
|
+
throw new Error("Invalid ZIP: bad local file header");
|
|
609
|
+
}
|
|
610
|
+
const dataStart = localOffset + 30 + view.getUint16(localOffset + 26, true) + view.getUint16(localOffset + 28, true);
|
|
611
|
+
requireRange(bytes, dataStart, compressedSize);
|
|
612
|
+
if (method === 0 && compressedSize !== size) {
|
|
613
|
+
throw new Error("Invalid ZIP: stored entry size mismatch");
|
|
614
|
+
}
|
|
615
|
+
if (method === 8 && size > compressedSize * MAX_DEFLATE_RATIO) {
|
|
616
|
+
throw new Error(
|
|
617
|
+
"Invalid ZIP: declared size is impossible for the compressed data"
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
entries.push({
|
|
621
|
+
name,
|
|
622
|
+
method,
|
|
623
|
+
size,
|
|
624
|
+
data: bytes.subarray(dataStart, dataStart + compressedSize)
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
return entries;
|
|
628
|
+
}
|
|
629
|
+
function newEntryMap() {
|
|
630
|
+
return /* @__PURE__ */ Object.create(null);
|
|
631
|
+
}
|
|
632
|
+
function unzipEntries(bytes, limits) {
|
|
633
|
+
const result = newEntryMap();
|
|
634
|
+
for (const entry of readEntries(bytes, limits)) {
|
|
635
|
+
result[entry.name] = entry.method === 0 ? entry.data.slice() : entry.size === 0 ? new Uint8Array(0) : inflateRaw(entry.data, entry.size);
|
|
636
|
+
}
|
|
637
|
+
return result;
|
|
638
|
+
}
|
|
639
|
+
async function unzipEntriesAsync(bytes, limits) {
|
|
640
|
+
const result = newEntryMap();
|
|
641
|
+
for (const entry of readEntries(bytes, limits)) {
|
|
642
|
+
result[entry.name] = entry.method === 0 ? entry.data.slice() : entry.size === 0 ? new Uint8Array(0) : await inflateRawAsync(entry.data, entry.size);
|
|
643
|
+
}
|
|
644
|
+
return result;
|
|
645
|
+
}
|
|
646
|
+
function readEndOfCentralDirectory(bytes, view) {
|
|
647
|
+
if (bytes.length < EOCD_MIN_SIZE) {
|
|
648
|
+
throw new Error("Invalid ZIP: too short");
|
|
649
|
+
}
|
|
650
|
+
const lowest = Math.max(0, bytes.length - EOCD_MIN_SIZE - MAX_COMMENT_SIZE);
|
|
651
|
+
let eocd = bytes.length - EOCD_MIN_SIZE;
|
|
652
|
+
while (eocd >= lowest && view.getUint32(eocd, true) !== EOCD_SIGNATURE) {
|
|
653
|
+
eocd -= 1;
|
|
654
|
+
}
|
|
655
|
+
if (eocd < lowest) {
|
|
656
|
+
throw new Error("Invalid ZIP: end of central directory not found");
|
|
657
|
+
}
|
|
658
|
+
if (view.getUint16(eocd + 4, true) !== 0 || view.getUint16(eocd + 6, true) !== 0) {
|
|
659
|
+
throw new Error("Invalid ZIP: multi-disk archives are not supported");
|
|
660
|
+
}
|
|
661
|
+
let entryCount = view.getUint16(eocd + 10, true);
|
|
662
|
+
let directoryOffset = view.getUint32(eocd + 16, true);
|
|
663
|
+
if ((entryCount === 65535 || directoryOffset === 4294967295) && eocd >= 20 && view.getUint32(eocd - 20, true) === EOCD64_LOCATOR_SIGNATURE) {
|
|
664
|
+
const recordOffset = toSafeNumber(view.getBigUint64(eocd - 20 + 8, true));
|
|
665
|
+
requireRange(bytes, recordOffset, 56);
|
|
666
|
+
if (view.getUint32(recordOffset, true) !== EOCD64_SIGNATURE) {
|
|
667
|
+
throw new Error("Invalid ZIP: bad ZIP64 end of central directory");
|
|
668
|
+
}
|
|
669
|
+
entryCount = toSafeNumber(view.getBigUint64(recordOffset + 32, true));
|
|
670
|
+
directoryOffset = toSafeNumber(view.getBigUint64(recordOffset + 48, true));
|
|
671
|
+
}
|
|
672
|
+
requireRange(bytes, directoryOffset, 0);
|
|
673
|
+
return { entryCount, directoryOffset };
|
|
674
|
+
}
|
|
675
|
+
function readZip64Extra(view, start, length, needSize, needCompressed, needOffset) {
|
|
676
|
+
const end = start + length;
|
|
677
|
+
let position = start;
|
|
678
|
+
while (position + 4 <= end) {
|
|
679
|
+
const id = view.getUint16(position, true);
|
|
680
|
+
const dataSize = view.getUint16(position + 2, true);
|
|
681
|
+
position += 4;
|
|
682
|
+
if (id === 1) {
|
|
683
|
+
let cursor = position;
|
|
684
|
+
const result = {};
|
|
685
|
+
const next = () => {
|
|
686
|
+
if (cursor + 8 > position + dataSize) {
|
|
687
|
+
throw new Error("Invalid ZIP: truncated ZIP64 extra field");
|
|
688
|
+
}
|
|
689
|
+
const value = toSafeNumber(view.getBigUint64(cursor, true));
|
|
690
|
+
cursor += 8;
|
|
691
|
+
return value;
|
|
692
|
+
};
|
|
693
|
+
if (needSize) result.size = next();
|
|
694
|
+
if (needCompressed) result.compressedSize = next();
|
|
695
|
+
if (needOffset) result.localOffset = next();
|
|
696
|
+
return result;
|
|
697
|
+
}
|
|
698
|
+
position += dataSize;
|
|
699
|
+
}
|
|
700
|
+
throw new Error("Invalid ZIP: missing ZIP64 extra field");
|
|
701
|
+
}
|
|
702
|
+
function toSafeNumber(value) {
|
|
703
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
704
|
+
throw new Error("Invalid ZIP: value out of range");
|
|
705
|
+
}
|
|
706
|
+
return Number(value);
|
|
707
|
+
}
|
|
708
|
+
function requireRange(bytes, offset, length) {
|
|
709
|
+
if (offset < 0 || length < 0 || offset + length > bytes.length) {
|
|
710
|
+
throw new Error("Invalid ZIP: truncated or out-of-range data");
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
function decodeName(raw, utf8) {
|
|
714
|
+
if (utf8) {
|
|
715
|
+
return new TextDecoder().decode(raw);
|
|
716
|
+
}
|
|
717
|
+
let result = "";
|
|
718
|
+
for (const byte of raw) {
|
|
719
|
+
result += String.fromCharCode(byte);
|
|
720
|
+
}
|
|
721
|
+
return result;
|
|
722
|
+
}
|
|
723
|
+
|
|
120
724
|
// src/artifacts.ts
|
|
121
725
|
function parseArtifactOrigin(value) {
|
|
122
726
|
try {
|
|
@@ -284,7 +888,7 @@ async function materializePresignedResult(response, input, downloader, signal) {
|
|
|
284
888
|
try {
|
|
285
889
|
const artifact = selectDocumentArtifact(item);
|
|
286
890
|
const bytes = await downloader.download(artifact.uri, signal);
|
|
287
|
-
const document = artifact.artifact_type === "resource_bundle" ?
|
|
891
|
+
const document = artifact.artifact_type === "resource_bundle" ? await documentFromBundleAsync(bytes) : documentFromJson(bytes);
|
|
288
892
|
return conversionFromArtifactItem(item, input, response.processing_time, document);
|
|
289
893
|
} catch (error) {
|
|
290
894
|
if (signal?.aborted === true || isAbortError(error)) {
|
|
@@ -312,19 +916,34 @@ function selectDocumentArtifact(item) {
|
|
|
312
916
|
"Presigned result exposes neither a 'json' nor a 'resource_bundle' artifact"
|
|
313
917
|
);
|
|
314
918
|
}
|
|
919
|
+
async function documentFromBundleAsync(bytes) {
|
|
920
|
+
let entries;
|
|
921
|
+
try {
|
|
922
|
+
entries = await unzipEntriesAsync(bytes, BUNDLE_LIMITS);
|
|
923
|
+
} catch (error) {
|
|
924
|
+
throw invalidBundleError(error);
|
|
925
|
+
}
|
|
926
|
+
return documentFromEntries(entries);
|
|
927
|
+
}
|
|
315
928
|
function documentFromBundle(bytes) {
|
|
316
929
|
let entries;
|
|
317
930
|
try {
|
|
318
931
|
entries = unzipBundle(bytes);
|
|
319
932
|
} catch (error) {
|
|
320
|
-
|
|
321
|
-
throw error;
|
|
322
|
-
}
|
|
323
|
-
throw new DoclingArtifactDownloadError(
|
|
324
|
-
"Downloaded resource bundle is not a valid ZIP",
|
|
325
|
-
{ cause: error }
|
|
326
|
-
);
|
|
933
|
+
throw invalidBundleError(error);
|
|
327
934
|
}
|
|
935
|
+
return documentFromEntries(entries);
|
|
936
|
+
}
|
|
937
|
+
function invalidBundleError(error) {
|
|
938
|
+
if (error instanceof DoclingArtifactDownloadError) {
|
|
939
|
+
return error;
|
|
940
|
+
}
|
|
941
|
+
return new DoclingArtifactDownloadError(
|
|
942
|
+
"Downloaded resource bundle is not a valid ZIP",
|
|
943
|
+
{ cause: error }
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
function documentFromEntries(entries) {
|
|
328
947
|
for (const name of Object.keys(entries)) {
|
|
329
948
|
validateBundlePath(name);
|
|
330
949
|
}
|
|
@@ -481,49 +1100,13 @@ function isSupportedImage(mimeType, bytes) {
|
|
|
481
1100
|
function startsWith(bytes, prefix) {
|
|
482
1101
|
return bytes.length >= prefix.length && prefix.every((value, index) => bytes[index] === value);
|
|
483
1102
|
}
|
|
1103
|
+
var BUNDLE_LIMITS = {
|
|
1104
|
+
maxEntries: 1e4,
|
|
1105
|
+
maxUncompressedBytes: 512 * 1024 * 1024,
|
|
1106
|
+
validateName: validateBundlePath
|
|
1107
|
+
};
|
|
484
1108
|
function unzipBundle(bytes) {
|
|
485
|
-
|
|
486
|
-
const maxEntries = 1e4;
|
|
487
|
-
const maxUncompressedBytes = 512 * 1024 * 1024;
|
|
488
|
-
let entryCount = 0;
|
|
489
|
-
let total = 0;
|
|
490
|
-
const archive = new Unzip((file) => {
|
|
491
|
-
entryCount += 1;
|
|
492
|
-
if (entryCount > maxEntries) {
|
|
493
|
-
throw new DoclingArtifactDownloadError(
|
|
494
|
-
`Resource bundle exceeds ${maxEntries} entries`
|
|
495
|
-
);
|
|
496
|
-
}
|
|
497
|
-
validateBundlePath(file.name);
|
|
498
|
-
const chunks = [];
|
|
499
|
-
file.ondata = (error, chunk, final) => {
|
|
500
|
-
if (error !== null) {
|
|
501
|
-
throw error;
|
|
502
|
-
}
|
|
503
|
-
total += chunk.byteLength;
|
|
504
|
-
if (total > maxUncompressedBytes) {
|
|
505
|
-
throw new DoclingArtifactDownloadError(
|
|
506
|
-
`Resource bundle exceeds ${maxUncompressedBytes} uncompressed bytes`
|
|
507
|
-
);
|
|
508
|
-
}
|
|
509
|
-
chunks.push(chunk);
|
|
510
|
-
if (final) {
|
|
511
|
-
const content = new Uint8Array(
|
|
512
|
-
chunks.reduce((sum, value) => sum + value.byteLength, 0)
|
|
513
|
-
);
|
|
514
|
-
let offset = 0;
|
|
515
|
-
for (const value of chunks) {
|
|
516
|
-
content.set(value, offset);
|
|
517
|
-
offset += value.byteLength;
|
|
518
|
-
}
|
|
519
|
-
entries[file.name] = content;
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
|
-
file.start();
|
|
523
|
-
});
|
|
524
|
-
archive.register(UnzipInflate);
|
|
525
|
-
archive.push(bytes, true);
|
|
526
|
-
return entries;
|
|
1109
|
+
return unzipEntries(bytes, BUNDLE_LIMITS);
|
|
527
1110
|
}
|
|
528
1111
|
function isRecord(value) {
|
|
529
1112
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
@@ -4260,6 +4843,7 @@ export {
|
|
|
4260
4843
|
appendFormValue,
|
|
4261
4844
|
applyPageLimits,
|
|
4262
4845
|
documentFromBundle,
|
|
4846
|
+
documentFromBundleAsync,
|
|
4263
4847
|
documentFromJson,
|
|
4264
4848
|
fileSourceFromBinary,
|
|
4265
4849
|
materializePresignedResult,
|