@poe-platform/safe-bash 0.1.814 → 0.1.815

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 (59) hide show
  1. package/dist/mp4-ast/index.d.ts +1 -0
  2. package/dist/mp4-ast/mp4-fragment-source.d.ts +35 -0
  3. package/dist/mp4-ast/mp4-sample-source.d.ts +5 -2
  4. package/dist/mp4-ast/types.d.ts +11 -0
  5. package/dist/pdf-ast/cos/object-reader.d.ts +3 -2
  6. package/dist/pdf-ast/edit/forms.d.ts +1 -0
  7. package/dist/pdf-ast/edit/retained-form-data.d.ts +9 -0
  8. package/dist/pdf-ast/index.d.ts +1 -0
  9. package/dist/pdf-ast/index.js +476 -55
  10. package/dist/safe-bash/command-chunks/{biff-KRHJ63K3-VQPJCVIL.js → biff-A7XPBWAK-DCLG5QKF.js} +1775 -1667
  11. package/dist/safe-bash/command-chunks/{chunk-AVGXFWEB.js → chunk-44F5TOHZ.js} +179 -58
  12. package/dist/safe-bash/command-chunks/{chunk-PIVH35EQ.js → chunk-6CDDKNNA.js} +1 -0
  13. package/dist/safe-bash/command-chunks/{chunk-K4XNUJVJ.js → chunk-6EINUY5G.js} +149 -165
  14. package/dist/safe-bash/command-chunks/{chunk-5BIQWWLB.js → chunk-CFMJXJS5.js} +1 -1
  15. package/dist/safe-bash/command-chunks/{chunk-3JM7E42I.js → chunk-GPGLTWNR.js} +106 -32
  16. package/dist/safe-bash/command-chunks/{chunk-2ETEJOYX.js → chunk-HQ2UN2L2.js} +8 -8
  17. package/dist/safe-bash/command-chunks/{chunk-CK2WFJZN.js → chunk-KASXIRKD.js} +12 -14
  18. package/dist/safe-bash/command-chunks/chunk-KHUT7BB5.js +65 -0
  19. package/dist/safe-bash/command-chunks/{chunk-LAPOMHGY.js → chunk-NEYITAAO.js} +114 -97
  20. package/dist/safe-bash/command-chunks/{chunk-FF7UGJWS.js → chunk-QDVDBN2N.js} +1 -1
  21. package/dist/safe-bash/command-chunks/{chunk-SW37YCBW.js → chunk-U3C6IT2C.js} +44 -30
  22. package/dist/safe-bash/command-chunks/{chunk-G3TAX3CV.js → chunk-WAT2562G.js} +610 -539
  23. package/dist/safe-bash/command-chunks/{chunk-VT7UBPAE.js → chunk-YW2VYN4V.js} +23 -11
  24. package/dist/safe-bash/command-chunks/{chunk-GQGMQMIM.js → chunk-ZIGENXGC.js} +243 -1
  25. package/dist/safe-bash/command-chunks/{command-engine-7V6PZR7G.js → command-engine-M7S4QIXV.js} +12 -4
  26. package/dist/safe-bash/command-chunks/{engine-UYJENSWK-VSCGKSQV.js → engine-YUYZJIB4-NZUDFEHL.js} +1 -1
  27. package/dist/safe-bash-command-ffmpeg/index.js +1 -1
  28. package/dist/safe-bash-command-ffprobe/index.js +2 -2
  29. package/dist/safe-bash-command-ffprobe/media.d.ts +8 -2
  30. package/dist/safe-bash-command-ffprobe/media.js +4 -2
  31. package/dist/safe-bash-command-pandoc/command.js +3 -3
  32. package/dist/safe-bash-command-pandoc/index.js +8 -8
  33. package/dist/safe-bash-command-patch/index.js +7 -6
  34. package/dist/safe-bash-command-patch/patch-formats.d.ts +6 -5
  35. package/dist/safe-bash-command-patch/patch-formats.js +3 -2
  36. package/dist/safe-bash-command-patch/patch-gnu-paths.d.ts +7 -7
  37. package/dist/safe-bash-command-patch/patch-gnu-reject.d.ts +1 -1
  38. package/dist/safe-bash-command-patch/patch-gnu-reject.js +3 -2
  39. package/dist/safe-bash-command-patch/patch-publication.js +3 -2
  40. package/dist/safe-bash-command-patch/patch-text.d.ts +13 -0
  41. package/dist/safe-bash-command-patch/patch.js +7 -6
  42. package/dist/safe-bash-command-patch/stored-patch.d.ts +29 -4
  43. package/dist/safe-bash-command-patch/stored-target.d.ts +2 -1
  44. package/dist/safe-bash-command-patch/unified.d.ts +18 -4
  45. package/dist/safe-bash-command-patch/unified.js +2 -1
  46. package/dist/safe-bash-command-pdftk/index.js +30 -20
  47. package/dist/safe-bash-command-pptx/engine.js +1 -1
  48. package/dist/safe-bash-command-ssconvert/index.js +2 -2
  49. package/dist/safe-bash-presentation-engine/index.js +1 -1
  50. package/dist/safe-bash-presentation-engine/notes.d.ts +1 -0
  51. package/dist/safe-bash-presentation-engine/notes.js +7 -1
  52. package/dist/safe-bash-presentation-engine/retained-notes.d.ts +27 -0
  53. package/dist/safe-bash-xml-engine/document.js +2 -2
  54. package/dist/safe-bash-xml-engine/index.js +2 -2
  55. package/dist/safe-bash-xml-engine/stored-document.js +1 -1
  56. package/dist/spreadsheet-format-xls/biff-properties-merge.js +40 -21
  57. package/dist/spreadsheet-format-xls/biff-property-values.d.ts +31 -0
  58. package/dist/spreadsheet-format-xls/biff-property-values.js +158 -0
  59. package/package.json +3 -2
@@ -49365,45 +49365,73 @@ var PdfReferenceSet = class {
49365
49365
  // packages/pdf-ast/src/cos/filter-dictionary.ts
49366
49366
  async function resolvePdfStreamDictionary(dict, lookup, active, options) {
49367
49367
  let remaining = options.maxNodes ?? 65536;
49368
- const resolve2 = async (node, depth) => {
49369
- if (--remaining < 0 || depth >= (options.maxRecursionDepth ?? 100)) throw new PdfError("E_LIMIT", "PDF filter resolution limit exceeded");
49370
- if (node.kind === "ref") {
49371
- if (active.has(node.objectNumber)) throw new PdfError("E_PARSE", "PDF filter reference cycle");
49372
- const object = await lookup(node);
49373
- if (!object) throw new PdfError("E_PARSE", "Missing PDF filter reference");
49374
- active.add(node.objectNumber);
49375
- try {
49376
- return await resolve2(object.value, depth + 1);
49377
- } finally {
49378
- active.delete(node.objectNumber);
49368
+ const resolve2 = async (node, depth, parent = options.referencePath) => {
49369
+ let path = parent, owned, failed = false;
49370
+ try {
49371
+ for (; ; ) {
49372
+ options.signal?.throwIfAborted();
49373
+ if (--remaining < 0 || depth >= (options.maxRecursionDepth ?? 100)) throw new PdfError("E_LIMIT", "PDF filter resolution limit exceeded");
49374
+ if (node.kind !== "ref") break;
49375
+ if (active.has(node.objectNumber)) throw new PdfError("E_PARSE", "PDF filter reference cycle");
49376
+ for (let ancestor = path; ancestor; ancestor = ancestor.parent) {
49377
+ const seen = await ancestor.seen.has(node.objectNumber).catch((error) => {
49378
+ options.onBackingError?.(error);
49379
+ throw error;
49380
+ });
49381
+ if (seen) throw new PdfError("E_PARSE", "PDF filter reference cycle");
49382
+ }
49383
+ owned ??= { seen: new PdfReferenceSet(options.referenceStorage, options.maxTraversalStagingBytes, options.signal), parent, depth: 0 };
49384
+ path = owned;
49385
+ const object = await lookup(node, path);
49386
+ if (!object) throw new PdfError("E_PARSE", "Missing PDF filter reference");
49387
+ await path.seen.add(node.objectNumber).catch((error) => {
49388
+ options.onBackingError?.(error);
49389
+ throw error;
49390
+ });
49391
+ path.depth++;
49392
+ depth++;
49393
+ node = object.value;
49394
+ if (path.depth % 256 === 0) await new Promise((resolve3) => setTimeout(resolve3, 0));
49379
49395
  }
49380
- }
49381
- if (node.kind === "array") {
49382
- const items = [];
49383
- const { storedItems, ...ordinary } = node;
49384
- if (storedItems) {
49385
- const reader = readStoredItems(storedItems, options.signal);
49386
- try {
49387
- for (; ; ) {
49388
- const item = await reader.next().catch((error) => {
49389
- options.onBackingError?.(error);
49390
- throw error;
49391
- });
49392
- if (item.done) break;
49393
- items.push(await resolve2(item.value, depth + 1));
49396
+ if (node.kind === "array") {
49397
+ const items = [];
49398
+ const { storedItems, ...ordinary } = node;
49399
+ if (storedItems) {
49400
+ const reader = readStoredItems(storedItems, options.signal);
49401
+ try {
49402
+ for (; ; ) {
49403
+ const item = await reader.next().catch((error) => {
49404
+ options.onBackingError?.(error);
49405
+ throw error;
49406
+ });
49407
+ if (item.done) break;
49408
+ items.push(await resolve2(item.value, depth + 1, path));
49409
+ }
49410
+ } finally {
49411
+ await reader.return();
49394
49412
  }
49395
- } finally {
49396
- await reader.return();
49413
+ } else for (const item of node.items) items.push(await resolve2(item, depth + 1, path));
49414
+ return { ...ordinary, items };
49415
+ }
49416
+ if (node.kind === "dict") {
49417
+ const entries2 = [];
49418
+ for (const entry of node.entries) entries2.push({ ...entry, value: await resolve2(entry.value, depth + 1, path) });
49419
+ return { ...node, entries: entries2 };
49420
+ }
49421
+ return node;
49422
+ } catch (error) {
49423
+ failed = true;
49424
+ throw error;
49425
+ } finally {
49426
+ try {
49427
+ await owned?.seen.close();
49428
+ } catch (error) {
49429
+ if (!failed) {
49430
+ options.onBackingError?.(error);
49431
+ await Promise.reject(error);
49397
49432
  }
49398
- } else for (const item of node.items) items.push(await resolve2(item, depth + 1));
49399
- return { ...ordinary, items };
49400
- }
49401
- if (node.kind === "dict") {
49402
- const entries2 = [];
49403
- for (const entry of node.entries) entries2.push({ ...entry, value: await resolve2(entry.value, depth + 1) });
49404
- return { ...node, entries: entries2 };
49433
+ }
49405
49434
  }
49406
- return node;
49407
49435
  };
49408
49436
  const entries = [];
49409
49437
  for (const entry of dict.entries) entries.push(["Filter", "F", "DecodeParms", "DP", "Type"].includes(entry.key.decoded) ? { ...entry, value: await resolve2(entry.value, 0) } : entry);
@@ -49556,7 +49584,7 @@ var PdfObjectReader = class {
49556
49584
  // Recursive length/object-stream lookups share one failure identity so an
49557
49585
  // outer recovery pass cannot mistake a caller-storage error for PDF syntax.
49558
49586
  backingFailures = /* @__PURE__ */ new WeakMap();
49559
- lengthPaths = /* @__PURE__ */ new WeakMap();
49587
+ referencePaths = /* @__PURE__ */ new WeakMap();
49560
49588
  repairedOffsets = /* @__PURE__ */ new Map();
49561
49589
  options;
49562
49590
  capacity;
@@ -49586,10 +49614,7 @@ var PdfObjectReader = class {
49586
49614
  const object2 = await this.load(objectNumber, generationNumber, /* @__PURE__ */ new Set());
49587
49615
  if (object2?.value.kind !== "dict" || !object2.stream) throw new PdfError("E_PARSE", "Expected an indexed PDF stream");
49588
49616
  const active = /* @__PURE__ */ new Set([objectNumber]);
49589
- return { object: object2, dict: await resolvePdfStreamDictionary(object2.value, (ref) => this.load(ref.objectNumber, ref.generationNumber, active), active, { ...this.options, onBackingError: (error) => {
49590
- this.backingFailures.set(active, { error });
49591
- this.options.onBackingError?.(error);
49592
- } }) };
49617
+ return { object: object2, dict: await this.resolveStreamDictionary(object2.value, active) };
49593
49618
  });
49594
49619
  const span = object.stream;
49595
49620
  const input = () => this.source.stream(span.start, span.end - span.start, this.options.signal);
@@ -49597,16 +49622,36 @@ var PdfObjectReader = class {
49597
49622
  const decodeOptions = { ...this.options, stopBeforeImageCodec: options.stopBeforeImageCodec ?? false, raw: options.raw ?? false };
49598
49623
  yield* security ? decodePdfEncryptedStreamChunks(security, objectNumber, generationNumber, dict, input, decodeOptions) : decodePdfStreamChunks(dict, input, decodeOptions);
49599
49624
  }
49625
+ resolveStreamDictionary(dict, active) {
49626
+ return resolvePdfStreamDictionary(dict, async (ref, path) => {
49627
+ const parent = this.referencePaths.get(active);
49628
+ this.referencePaths.set(active, path);
49629
+ try {
49630
+ return await this.load(ref.objectNumber, ref.generationNumber, active);
49631
+ } finally {
49632
+ if (parent) this.referencePaths.set(active, parent);
49633
+ else this.referencePaths.delete(active);
49634
+ }
49635
+ }, active, {
49636
+ ...this.options,
49637
+ referenceStorage: this.storage,
49638
+ referencePath: this.referencePaths.get(active),
49639
+ onBackingError: (error) => {
49640
+ this.backingFailures.set(active, { error });
49641
+ this.options.onBackingError?.(error);
49642
+ }
49643
+ });
49644
+ }
49600
49645
  /** A stream-valued /Length is invalid as a length, but its references still
49601
49646
  * need cycle, admission and backing-error validation. Walk those discarded
49602
49647
  * values in source order instead of retaining one parser and AST per link. */
49603
49648
  async resolveLength(reference, active) {
49604
49649
  const path = {
49605
49650
  seen: new PdfReferenceSet(this.storage, this.options.maxTraversalStagingBytes, this.options.signal),
49606
- parent: this.lengthPaths.get(active),
49651
+ parent: this.referencePaths.get(active),
49607
49652
  depth: 0
49608
49653
  };
49609
- this.lengthPaths.set(active, path);
49654
+ this.referencePaths.set(active, path);
49610
49655
  let next = reference, result, failed = false;
49611
49656
  try {
49612
49657
  while (next) {
@@ -49637,8 +49682,8 @@ var PdfObjectReader = class {
49637
49682
  failed = true;
49638
49683
  throw error;
49639
49684
  } finally {
49640
- if (path.parent) this.lengthPaths.set(active, path.parent);
49641
- else this.lengthPaths.delete(active);
49685
+ if (path.parent) this.referencePaths.set(active, path.parent);
49686
+ else this.referencePaths.delete(active);
49642
49687
  try {
49643
49688
  await path.seen.close();
49644
49689
  } catch (error) {
@@ -49654,7 +49699,7 @@ var PdfObjectReader = class {
49654
49699
  this.options.signal?.throwIfAborted();
49655
49700
  if (active.has(objectNumber)) throw new PdfError("E_PARSE", "PDF indirect object cycle");
49656
49701
  let depth = active.size;
49657
- for (let path = this.lengthPaths.get(active); path; path = path.parent) {
49702
+ for (let path = this.referencePaths.get(active); path; path = path.parent) {
49658
49703
  let seen;
49659
49704
  try {
49660
49705
  seen = await path.seen.has(objectNumber);
@@ -49774,10 +49819,7 @@ var PdfObjectReader = class {
49774
49819
  active.add(number);
49775
49820
  let dict;
49776
49821
  try {
49777
- dict = await resolvePdfStreamDictionary(object.value, (ref) => this.load(ref.objectNumber, ref.generationNumber, active), active, { ...this.options, onBackingError: (error) => {
49778
- this.backingFailures.set(active, { error });
49779
- this.options.onBackingError?.(error);
49780
- } });
49822
+ dict = await this.resolveStreamDictionary(object.value, active);
49781
49823
  } finally {
49782
49824
  active.delete(number);
49783
49825
  }
@@ -56189,7 +56231,7 @@ function* generateDocumentFormAppearancesSteps(doc) {
56189
56231
  function generateDocumentFormAppearances(doc) {
56190
56232
  return drainWork(generateDocumentFormAppearancesSteps(doc));
56191
56233
  }
56192
- function decodeXmlEntities(text) {
56234
+ function decodeFormDataEntities(text) {
56193
56235
  let out = "";
56194
56236
  let i = 0;
56195
56237
  while (i < text.length) {
@@ -56253,7 +56295,7 @@ function extractAttributeValue(tagBody, attrName) {
56253
56295
  if (quote !== '"' && quote !== "'") return void 0;
56254
56296
  const end = tagBody.indexOf(quote, after + 1);
56255
56297
  if (end === -1) return void 0;
56256
- return decodeXmlEntities(tagBody.slice(after + 1, end));
56298
+ return decodeFormDataEntities(tagBody.slice(after + 1, end));
56257
56299
  }
56258
56300
  function parseXfdfString(text, out) {
56259
56301
  const fieldStack = [];
@@ -56285,7 +56327,7 @@ function parseXfdfString(text, out) {
56285
56327
  } else if (tagName === "value" && !selfClosing && fieldStack.length > 0) {
56286
56328
  const closeVal = text.indexOf("</value>", pos);
56287
56329
  if (closeVal !== -1) {
56288
- const rawVal = decodeXmlEntities(text.slice(pos, closeVal));
56330
+ const rawVal = decodeFormDataEntities(text.slice(pos, closeVal));
56289
56331
  const fullFieldName = fieldStack.filter(Boolean).join(".");
56290
56332
  if (fullFieldName) {
56291
56333
  if (rawVal === "Off") out.set(fullFieldName, false);
@@ -56493,9 +56535,9 @@ function parseFormDataBytes(bytes) {
56493
56535
  if (line.startsWith("FieldType:")) {
56494
56536
  curType = line.slice("FieldType:".length).trim();
56495
56537
  } else if (line.startsWith("FieldName:")) {
56496
- curName = decodeXmlEntities(line.slice("FieldName:".length).trim());
56538
+ curName = decodeFormDataEntities(line.slice("FieldName:".length).trim());
56497
56539
  } else if (line.startsWith("FieldValue:") && curName) {
56498
- const val = decodeXmlEntities(line.slice("FieldValue:".length).trim());
56540
+ const val = decodeFormDataEntities(line.slice("FieldValue:".length).trim());
56499
56541
  if (val === "Yes" || val === "On") {
56500
56542
  out.set(curName, true);
56501
56543
  } else if (val === "Off" || val === "false" || curType === "Button" && val === "") {
@@ -58655,7 +58697,7 @@ async function openStoredPdfObjectReader(store, storage, options) {
58655
58697
  validateIdentity(number, generation);
58656
58698
  const object = number === 0 ? void 0 : await store.get(number);
58657
58699
  if (object?.generationNumber !== generation || !object.stream || object.value.kind !== "dict") throw new PdfError("E_PARSE", "Expected an indexed PDF stream");
58658
- const dict = await resolvePdfStreamDictionary(object.value, (ref) => reader.get(ref.objectNumber, ref.generationNumber), /* @__PURE__ */ new Set([number]), options);
58700
+ const dict = await resolvePdfStreamDictionary(object.value, (ref) => reader.get(ref.objectNumber, ref.generationNumber), /* @__PURE__ */ new Set([number]), { ...options, referenceStorage: storage });
58659
58701
  const snapshot = object.stream;
58660
58702
  async function* input() {
58661
58703
  for await (const bytes of snapshot.chunks) {
@@ -62848,6 +62890,7 @@ async function generateRetainedFormAppearances(document2, store, storage, signal
62848
62890
  const source = await PdfFileSource.fromStream(storage.fs, storage.directory, chunks, { signal });
62849
62891
  let failed2 = false;
62850
62892
  try {
62893
+ if (update && dict.kind === "dict") dictSet(dict, "Length", cosNumber(source.size));
62851
62894
  const ref = await store.allocate();
62852
62895
  await store.set({ objectNumber: ref.objectNumber, generationNumber: 0, value: dict, stream: { length: source.size, chunks: source.stream(0, source.size, signal), decoded: true } });
62853
62896
  return ref;
@@ -65627,6 +65670,382 @@ var QpdfJsonDocument = class _QpdfJsonDocument {
65627
65670
  })();
65628
65671
  }
65629
65672
  };
65673
+
65674
+ // packages/pdf-ast/src/edit/retained-form-data.ts
65675
+ import { IntegerTable as IntegerTable18, PagedStorage as PagedStorage30 } from "@poe-platform/safe-fs/storage";
65676
+
65677
+ // packages/pdf-ast/src/edit/retained-form-data-fdf.ts
65678
+ import { IntegerTable as IntegerTable17, PagedStorage as PagedStorage29 } from "@poe-platform/safe-fs/storage";
65679
+ async function parseRetainedFdf(source, storage, set, signal) {
65680
+ const backing = new PagedStorage29({ fs: storage.fs, cwd: storage.directory, env: {}, signal }, 4), tokens = new IntegerTable17(backing), starts = new IntegerTable17(backing), children = new IntegerTable17(backing), visited = new IntegerTable17(backing), names = new PdfNameIndex(storage, Infinity, signal);
65681
+ let count = 0, cursor = 0, head = -1, work = 0, failed = false;
65682
+ const cache = /* @__PURE__ */ new Map();
65683
+ async function checkpoint() {
65684
+ signal.throwIfAborted();
65685
+ if (++work % 128 === 0) {
65686
+ await new Promise((resolve2) => setTimeout(resolve2, 0));
65687
+ signal.throwIfAborted();
65688
+ }
65689
+ }
65690
+ async function token2(index2) {
65691
+ if (index2 < 0 || index2 >= count) return;
65692
+ const cached = cache.get(index2);
65693
+ if (cached) return cached;
65694
+ const at = Number(await tokens.get(BigInt(index2))) - 1, result = (await readStoredRecord(backing, at, signal)).value;
65695
+ if (cache.size >= 8) cache.delete(cache.keys().next().value);
65696
+ cache.set(index2, result);
65697
+ return result;
65698
+ }
65699
+ async function objectKey(object) {
65700
+ return BigInt((await names.intern(String(object))).index);
65701
+ }
65702
+ async function push(frame) {
65703
+ head = await appendStoredRecord(backing, { frame, previous: head }, -1, signal);
65704
+ }
65705
+ function dict(prefix) {
65706
+ return { kind: "dict", prefix, partial: "", value: "", hasValue: false, children: -1 };
65707
+ }
65708
+ function text(token3) {
65709
+ return token3.kind === "name" ? token3.decoded : token3.kind === "string" || token3.kind === "hex-string" ? decodePdfString({ kind: "string", bytes: token3.bytes, encoding: token3.kind === "string" ? "literal" : "hex" }) : "";
65710
+ }
65711
+ async function reference(index2) {
65712
+ const a = await token2(index2), b = await token2(index2 + 1), c = await token2(index2 + 2);
65713
+ return a?.kind === "number" && b?.kind === "number" && c?.kind === "keyword" && c.value === "R" ? a.value : void 0;
65714
+ }
65715
+ try {
65716
+ const lexer = new CosRangeLexer(source, { signal, compactNumbers: true });
65717
+ for (let next = await lexer.nextToken(); next; next = await lexer.nextToken()) {
65718
+ await checkpoint();
65719
+ const at = await appendStoredRecord(backing, next, -1, signal);
65720
+ await tokens.set(BigInt(count++), BigInt(at + 1));
65721
+ }
65722
+ for (let i = 0; i < count; i++) {
65723
+ await checkpoint();
65724
+ const a = await token2(i), b = await token2(i + 1), c = await token2(i + 2), d = await token2(i + 3);
65725
+ if (a?.kind === "number" && b?.kind === "number" && c?.kind === "keyword" && c.value === "obj" && d?.kind === "dict-start") await starts.set(await objectKey(a.value), BigInt(i + 5));
65726
+ if (a?.kind === "name" && a.decoded === "Kids" && b?.kind === "array-start") {
65727
+ let depth = 1;
65728
+ for (let k = i + 2; k < count && depth > 0; k++) {
65729
+ await checkpoint();
65730
+ const next = await token2(k);
65731
+ if (next?.kind === "array-start") depth++;
65732
+ else if (next?.kind === "array-end") depth--;
65733
+ else if (depth === 1) {
65734
+ const ref = await reference(k);
65735
+ if (ref !== void 0) {
65736
+ await children.set(await objectKey(ref), 1n);
65737
+ k += 2;
65738
+ }
65739
+ }
65740
+ }
65741
+ }
65742
+ }
65743
+ await push({ kind: "scan" });
65744
+ while (head !== -1) {
65745
+ await checkpoint();
65746
+ const record = await readStoredRecord(backing, head, signal);
65747
+ head = record.value.previous;
65748
+ const frame = record.value.frame;
65749
+ if (frame.kind === "restore") {
65750
+ cursor = frame.cursor;
65751
+ continue;
65752
+ }
65753
+ if (frame.kind === "ref") {
65754
+ const key = await objectKey(frame.object), start = await starts.get(key);
65755
+ if (start && !await visited.get(key)) {
65756
+ await visited.set(key, 1n);
65757
+ cursor = Number(start) - 1;
65758
+ await push(dict(frame.prefix));
65759
+ }
65760
+ continue;
65761
+ }
65762
+ if (frame.kind === "children") {
65763
+ let ref = frame.refs;
65764
+ while (ref !== -1) {
65765
+ const row = await readStoredRecord(backing, ref, signal);
65766
+ await push({ kind: "ref", object: row.value.object, prefix: frame.prefix });
65767
+ ref = row.value.previous;
65768
+ }
65769
+ cursor = frame.start;
65770
+ await push({ kind: "range", end: frame.end, prefix: frame.prefix });
65771
+ continue;
65772
+ }
65773
+ if (frame.kind === "scan" || frame.kind === "range") {
65774
+ const end = frame.kind === "range" ? frame.end : count;
65775
+ while (cursor < end) {
65776
+ await checkpoint();
65777
+ const a = await token2(cursor);
65778
+ if (frame.kind === "scan" && a?.kind === "number") {
65779
+ const b = await token2(cursor + 1), c = await token2(cursor + 2);
65780
+ if (b?.kind === "number" && c?.kind === "keyword" && c.value === "obj") {
65781
+ const key = await objectKey(a.value);
65782
+ if (await children.get(key) || await visited.get(key)) {
65783
+ cursor += 3;
65784
+ while (cursor < count) {
65785
+ await checkpoint();
65786
+ const t = await token2(cursor++);
65787
+ if (t?.kind === "keyword" && t.value === "endobj") break;
65788
+ }
65789
+ continue;
65790
+ }
65791
+ }
65792
+ }
65793
+ cursor++;
65794
+ if (a?.kind === "dict-start") {
65795
+ await push(frame);
65796
+ await push(dict(frame.kind === "range" ? frame.prefix : ""));
65797
+ break;
65798
+ }
65799
+ }
65800
+ continue;
65801
+ }
65802
+ let nested = false;
65803
+ while (cursor < count) {
65804
+ await checkpoint();
65805
+ const next = await token2(cursor++);
65806
+ if (next?.kind === "dict-end") break;
65807
+ if (next?.kind === "dict-start") {
65808
+ await push(frame);
65809
+ await push(dict(frame.prefix && frame.partial ? `${frame.prefix}.${frame.partial}` : frame.partial || frame.prefix));
65810
+ nested = true;
65811
+ break;
65812
+ }
65813
+ if (next?.kind !== "name") continue;
65814
+ const value = await token2(cursor);
65815
+ if (next.decoded === "T" && (value?.kind === "string" || value?.kind === "hex-string" || value?.kind === "name")) {
65816
+ frame.partial = text(value);
65817
+ cursor++;
65818
+ } else if (next.decoded === "V" && value) {
65819
+ if (value.kind === "string" || value.kind === "hex-string" || value.kind === "name") {
65820
+ const decoded = text(value);
65821
+ frame.value = value.kind === "name" ? decoded === "Off" ? false : decoded === "Yes" || decoded === "On" ? true : decoded : decoded;
65822
+ frame.hasValue = true;
65823
+ cursor++;
65824
+ } else if (value.kind === "number" || value.kind === "boolean") {
65825
+ frame.value = value.kind === "number" ? String(value.value) : value.value;
65826
+ frame.hasValue = true;
65827
+ cursor++;
65828
+ } else if (value.kind === "array-start") {
65829
+ cursor++;
65830
+ let depth = 1, joined = "", items = 0;
65831
+ while (cursor < count && depth > 0) {
65832
+ await checkpoint();
65833
+ const t = await token2(cursor++);
65834
+ if (t?.kind === "array-start") depth++;
65835
+ else if (t?.kind === "array-end") depth--;
65836
+ else if (depth === 1 && t && (t.kind === "string" || t.kind === "hex-string" || t.kind === "name")) {
65837
+ joined += (items++ ? "," : "") + text(t);
65838
+ }
65839
+ }
65840
+ frame.value = joined;
65841
+ frame.hasValue = true;
65842
+ }
65843
+ } else if ((next.decoded === "Kids" || next.decoded === "Fields") && value?.kind === "array-start") {
65844
+ cursor++;
65845
+ const start = cursor;
65846
+ let depth = 1, refs = -1;
65847
+ while (cursor < count && depth > 0) {
65848
+ await checkpoint();
65849
+ const t = await token2(cursor++);
65850
+ if (t?.kind === "array-start") depth++;
65851
+ else if (t?.kind === "array-end") depth--;
65852
+ else if (depth === 1) {
65853
+ const ref = await reference(cursor - 1);
65854
+ if (ref !== void 0) {
65855
+ refs = await appendStoredRecord(backing, { object: ref, previous: refs }, -1, signal);
65856
+ cursor += 2;
65857
+ }
65858
+ }
65859
+ }
65860
+ frame.children = await appendStoredRecord(backing, { start, end: cursor - 1, refs, previous: frame.children }, -1, signal);
65861
+ }
65862
+ }
65863
+ if (nested) continue;
65864
+ const name = frame.prefix && frame.partial ? `${frame.prefix}.${frame.partial}` : frame.partial || frame.prefix;
65865
+ if (name && frame.hasValue) await set(name, frame.value);
65866
+ if (frame.children !== -1) {
65867
+ await push({ kind: "restore", cursor });
65868
+ let child = frame.children;
65869
+ while (child !== -1) {
65870
+ const row = (await readStoredRecord(backing, child, signal)).value;
65871
+ await push({ kind: "children", start: row.start, end: row.end, refs: row.refs, prefix: name });
65872
+ child = row.previous;
65873
+ }
65874
+ }
65875
+ }
65876
+ } catch (error) {
65877
+ failed = true;
65878
+ throw error;
65879
+ } finally {
65880
+ const results = await Promise.allSettled([backing.close(), names.close()]);
65881
+ if (!failed) {
65882
+ for (const result of results) if (result.status === "rejected") await Promise.reject(result.reason);
65883
+ }
65884
+ }
65885
+ }
65886
+
65887
+ // packages/pdf-ast/src/edit/retained-form-data.ts
65888
+ var FormText = class {
65889
+ constructor(backing, signal) {
65890
+ this.backing = backing;
65891
+ this.signal = signal;
65892
+ this.base = backing.allocate(0);
65893
+ }
65894
+ backing;
65895
+ signal;
65896
+ length = 0;
65897
+ base;
65898
+ async load(source) {
65899
+ const decoder = new TextDecoder();
65900
+ const append = async (text) => {
65901
+ for (let start = 0; start < text.length; start += 4096) {
65902
+ this.signal.throwIfAborted();
65903
+ const length = Math.min(4096, text.length - start), bytes = new Uint8Array(length * 2), view = new DataView(bytes.buffer);
65904
+ for (let i = 0; i < length; i++) view.setUint16(i * 2, text.charCodeAt(start + i), true);
65905
+ await this.backing.write(this.backing.allocate(bytes.length), bytes);
65906
+ this.length += length;
65907
+ }
65908
+ };
65909
+ for await (const chunk of source.stream(0, source.size, this.signal)) await append(decoder.decode(chunk, { stream: true }));
65910
+ await append(decoder.decode());
65911
+ }
65912
+ async read(start, end) {
65913
+ let result = "";
65914
+ for (let at = start; at < end; at += 4096) {
65915
+ this.signal.throwIfAborted();
65916
+ const length = Math.min(4096, end - at), bytes = await this.backing.read(this.base + at * 2, length * 2), view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
65917
+ const units = [];
65918
+ for (let i = 0; i < length; i++) units.push(view.getUint16(i * 2, true));
65919
+ result += String.fromCharCode(...units);
65920
+ }
65921
+ return result;
65922
+ }
65923
+ async indexOf(needle, start = 0) {
65924
+ for (let at = start; at < this.length; at += 4096) {
65925
+ const part = await this.read(at, Math.min(this.length, at + 4096 + needle.length - 1)), index2 = part.indexOf(needle);
65926
+ if (index2 >= 0) return at + index2;
65927
+ }
65928
+ return -1;
65929
+ }
65930
+ async prefix() {
65931
+ for (let at = 0; at < this.length; at += 4096) {
65932
+ const part = await this.read(at, Math.min(this.length, at + 4096)), trimmed = part.trimStart();
65933
+ if (trimmed) return this.read(at + part.length - trimmed.length, Math.min(this.length, at + part.length - trimmed.length + 5));
65934
+ }
65935
+ return "";
65936
+ }
65937
+ };
65938
+ async function* parseRetainedFormData(source, storage, options = {}) {
65939
+ const signal = options.signal ?? new AbortController().signal, context = { fs: storage.fs, cwd: storage.directory, env: {}, signal };
65940
+ const textBacking = new PagedStorage30(context, 2), state = new PagedStorage30(context, 4), names = new PdfNameIndex(storage, Infinity, signal), values = new IntegerTable18(state);
65941
+ const text = new FormText(textBacking, signal);
65942
+ let count = 0, failed = false, work = 0;
65943
+ async function checkpoint() {
65944
+ signal.throwIfAborted();
65945
+ if (++work % 64 === 0) {
65946
+ await new Promise((resolve2) => setTimeout(resolve2, 0));
65947
+ signal.throwIfAborted();
65948
+ }
65949
+ }
65950
+ async function set(name, value, append = false) {
65951
+ await checkpoint();
65952
+ const key = await names.intern(name), at = await values.get(BigInt(key.index));
65953
+ if (key.added) count++;
65954
+ if (append && typeof value === "string" && at) {
65955
+ const previous = (await readStoredRecord(state, Number(at) - 1, signal)).value.value;
65956
+ if (typeof previous === "string" && previous.length) value = `${previous},${value}`;
65957
+ }
65958
+ const offset = await appendStoredRecord(state, { name, value }, -1, signal);
65959
+ await values.set(BigInt(key.index), BigInt(offset + 1));
65960
+ }
65961
+ try {
65962
+ await text.load(source);
65963
+ const prefix = await text.prefix();
65964
+ if (prefix === "%FDF-" || await text.indexOf("/FDF") >= 0 && await text.indexOf("/Fields") >= 0) await parseRetainedFdf(source, storage, set, signal);
65965
+ else if (prefix === "<?xml" || prefix === "<xfdf" || await text.indexOf("<fields") >= 0) {
65966
+ let position = 0, head = -1, field2 = "";
65967
+ while (position < text.length) {
65968
+ await checkpoint();
65969
+ const lt = await text.indexOf("<", position);
65970
+ if (lt < 0) break;
65971
+ const gt = await text.indexOf(">", lt + 1);
65972
+ if (gt < 0) break;
65973
+ const raw = (await text.read(lt + 1, gt)).trim();
65974
+ position = gt + 1;
65975
+ if (raw.startsWith("?") || raw.startsWith("!")) continue;
65976
+ if (raw.startsWith("/")) {
65977
+ if (raw.slice(1).trim().toLowerCase() === "field" && head !== -1) {
65978
+ const row = (await readStoredRecord(state, head, signal)).value;
65979
+ field2 = row.field;
65980
+ head = row.previous;
65981
+ }
65982
+ continue;
65983
+ }
65984
+ const selfClosing = raw.endsWith("/"), tag = selfClosing ? raw.slice(0, -1).trim() : raw, space = tag.indexOf(" "), name = (space < 0 ? tag : tag.slice(0, space)).toLowerCase();
65985
+ if (name === "field") {
65986
+ let part = "";
65987
+ const at = tag.indexOf("name=");
65988
+ if (at >= 0) {
65989
+ const quote = tag[at + 5];
65990
+ if (quote === '"' || quote === "'") {
65991
+ const end = tag.indexOf(quote, at + 6);
65992
+ if (end >= 0) part = decodeFormDataEntities(tag.slice(at + 6, end));
65993
+ }
65994
+ }
65995
+ if (!selfClosing) {
65996
+ head = await appendStoredRecord(state, { field: field2, previous: head }, -1, signal);
65997
+ field2 = field2 && part ? `${field2}.${part}` : field2 || part;
65998
+ }
65999
+ } else if (name === "value" && !selfClosing && head !== -1) {
66000
+ const close = await text.indexOf("</value>", position);
66001
+ if (close >= 0) {
66002
+ const value = decodeFormDataEntities(await text.read(position, close));
66003
+ if (field2) await set(field2, value === "Off" ? false : value === "Yes" || value === "On" ? true : value, true);
66004
+ position = close + 8;
66005
+ }
66006
+ }
66007
+ }
66008
+ } else {
66009
+ const report = await text.indexOf("FieldName:") >= 0;
66010
+ let position = 0, name = "", type = "";
66011
+ while (position < text.length) {
66012
+ await checkpoint();
66013
+ const newline = await text.indexOf("\n", position), end = newline < 0 ? text.length : newline, line = (await text.read(position, end)).trim();
66014
+ position = end + 1;
66015
+ if (report) {
66016
+ if (line === "---") {
66017
+ name = "";
66018
+ type = "";
66019
+ } else if (line.startsWith("FieldType:")) type = line.slice(10).trim();
66020
+ else if (line.startsWith("FieldName:")) name = decodeFormDataEntities(line.slice(10).trim());
66021
+ else if (line.startsWith("FieldValue:") && name) {
66022
+ const value = decodeFormDataEntities(line.slice(11).trim());
66023
+ await set(name, value === "Yes" || value === "On" ? true : value === "Off" || value === "false" || type === "Button" && value === "" ? false : value, true);
66024
+ }
66025
+ } else {
66026
+ if (!line || line.startsWith("#")) continue;
66027
+ const equals = line.indexOf("="), colon = line.indexOf(":"), separator = equals >= 0 ? equals : colon;
66028
+ if (separator > 0) {
66029
+ const name2 = line.slice(0, separator).trim(), value = line.slice(separator + 1).trim();
66030
+ await set(name2, value === "true" || value === "Yes" || value === "On" ? true : value === "false" || value === "Off" ? false : value);
66031
+ }
66032
+ }
66033
+ }
66034
+ }
66035
+ for (let index2 = 0; index2 < count; index2++) {
66036
+ await checkpoint();
66037
+ yield (await readStoredRecord(state, Number(await values.get(BigInt(index2))) - 1, signal)).value;
66038
+ }
66039
+ } catch (error) {
66040
+ failed = true;
66041
+ throw error;
66042
+ } finally {
66043
+ const results = await Promise.allSettled([textBacking.close(), state.close(), names.close()]);
66044
+ if (!failed) {
66045
+ for (const result of results) if (result.status === "rejected") await Promise.reject(result.reason);
66046
+ }
66047
+ }
66048
+ }
65630
66049
  export {
65631
66050
  CosByteLexer,
65632
66051
  CosRangeLexer,
@@ -65692,6 +66111,7 @@ export {
65692
66111
  decodeCcittFax,
65693
66112
  decodeCcittFaxChunks,
65694
66113
  decodeFlate,
66114
+ decodeFormDataEntities,
65695
66115
  decodeInlineImageNodeToRgba,
65696
66116
  decodeInlineImageNodeToRgbaSteps,
65697
66117
  decodeJbig2ToRgba,
@@ -65815,6 +66235,7 @@ export {
65815
66235
  parseCosRangeObject,
65816
66236
  parseCosRangeValue,
65817
66237
  parseFormDataBytes,
66238
+ parseRetainedFormData,
65818
66239
  parseToUnicodeCMap,
65819
66240
  parseToUnicodeCMapSteps,
65820
66241
  parseTrueTypeFont,