@poe-platform/safe-bash 0.1.789 → 0.1.790

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 (207) hide show
  1. package/dist/image-ast/index.browser.js +147 -80
  2. package/dist/image-ast/index.js +147 -80
  3. package/dist/mp4-ast/mp4.d.ts +2 -0
  4. package/dist/pdf-ast/cos/parser.d.ts +1 -0
  5. package/dist/pdf-ast/cos/security.d.ts +2 -0
  6. package/dist/pdf-ast/cos/writer.d.ts +1 -0
  7. package/dist/pdf-ast/document.d.ts +3 -0
  8. package/dist/pdf-ast/edit/forms.d.ts +1 -0
  9. package/dist/pdf-ast/index.js +171 -82
  10. package/dist/safe-bash/chunks/{chunk-L5QQLNTO.js → chunk-22PI65AH.js} +2 -2
  11. package/dist/safe-bash/chunks/{chunk-KHJGUF2W.js → chunk-2TAGYMPK.js} +43 -42
  12. package/dist/safe-bash/chunks/{chunk-D4PHJK4T.js → chunk-2W4HTMEK.js} +52 -48
  13. package/dist/safe-bash/chunks/{chunk-FFZXP6ZC.js → chunk-3M7L7XU2.js} +2 -2
  14. package/dist/safe-bash/chunks/{chunk-XNODZRVD.js → chunk-3QAO6UYS.js} +2 -2
  15. package/dist/safe-bash/chunks/{chunk-YPETOVQU.js → chunk-3VKFK3NF.js} +2 -2
  16. package/dist/safe-bash/chunks/chunk-5OST7NOX.js +42 -0
  17. package/dist/safe-bash/chunks/{chunk-KEBSX2AX.js → chunk-6HLURHVU.js} +2 -2
  18. package/dist/safe-bash/chunks/chunk-DYEUZE6Z.js +19 -0
  19. package/dist/safe-bash/chunks/{chunk-T2TGCHKE.js → chunk-FM3WBSZT.js} +2 -2
  20. package/dist/safe-bash/chunks/chunk-GAYRKQM6.js +22 -0
  21. package/dist/safe-bash/chunks/{chunk-Q45QCA4J.js → chunk-I3KSESCR.js} +0 -7
  22. package/dist/safe-bash/chunks/{chunk-OSKDIBQS.js → chunk-JLIMK5TD.js} +1 -1
  23. package/dist/safe-bash/chunks/{chunk-2AYZCT5X.js → chunk-JQ2FAJJT.js} +27 -23
  24. package/dist/safe-bash/chunks/{chunk-QA3NVP43.js → chunk-JRE6Z3T7.js} +2 -2
  25. package/dist/safe-bash/chunks/{chunk-DC5EHLKS.js → chunk-NDIL6T7Y.js} +5 -5
  26. package/dist/safe-bash/chunks/{chunk-747G42FY.js → chunk-OPR4UAIT.js} +2 -2
  27. package/dist/safe-bash/chunks/chunk-OSMQCYSP.js +19 -0
  28. package/dist/safe-bash/chunks/chunk-OYB332PQ.js +17 -0
  29. package/dist/safe-bash/chunks/{chunk-X5ZCECM6.js → chunk-QTIH2AFE.js} +77 -69
  30. package/dist/safe-bash/chunks/{chunk-CTUHLNJM.js → chunk-SBESXWT2.js} +43 -44
  31. package/dist/safe-bash/chunks/{chunk-4FDQJUFW.js → chunk-SDEPGTOF.js} +1 -1
  32. package/dist/safe-bash/chunks/chunk-UZU5BIJI.js +32 -0
  33. package/dist/safe-bash/chunks/{chunk-BI6JV62X.js → chunk-VCCMROTL.js} +7 -10
  34. package/dist/safe-bash/chunks/chunk-W47GHRRF.js +13 -0
  35. package/dist/safe-bash/chunks/{chunk-NPMQWZUZ.js → chunk-WS7KZIUM.js} +2 -2
  36. package/dist/safe-bash/chunks/{chunk-3PZYHMGM.js → chunk-YYAERCWS.js} +7 -7
  37. package/dist/safe-bash/chunks/{chunk-5U7JECDB.js → chunk-ZPGVEUKF.js} +8 -7
  38. package/dist/safe-bash/chunks/{csvkit-K55OYAQC.js → csvkit-USUJG62I.js} +65 -11
  39. package/dist/safe-bash/chunks/{ffmpeg-BEFTQ6DZ.js → ffmpeg-KRGG5DLH.js} +21 -10
  40. package/dist/safe-bash/chunks/{gh-HQWOWLWE.js → gh-UCFDUZCX.js} +16 -8
  41. package/dist/safe-bash/chunks/{git-HO7WDPHZ.js → git-LPAF6KDG.js} +7 -9
  42. package/dist/safe-bash/chunks/{imagemagick-53BMHH6D.js → imagemagick-KGREKK4G.js} +38 -7
  43. package/dist/safe-bash/chunks/{implementation-QUTAAYMT.js → implementation-FXDRYS5A.js} +34 -8
  44. package/dist/safe-bash/chunks/{pdfimages-4OYI6FLG.js → pdfimages-UADCF4G4.js} +12 -8
  45. package/dist/safe-bash/chunks/{pdfinfo-I7U6TQPP.js → pdfinfo-O7UWPF4W.js} +38 -13
  46. package/dist/safe-bash/chunks/{pdftk-SUA6FMHI.js → pdftk-AERXIJTN.js} +13 -8
  47. package/dist/safe-bash/chunks/{pdftoppm-4C5DGEWX.js → pdftoppm-L466JQKI.js} +17 -10
  48. package/dist/safe-bash/chunks/{pdftotext-Y4WI225Y.js → pdftotext-SUSI4WXS.js} +19 -9
  49. package/dist/safe-bash/chunks/{qpdf-IORB4RV5.js → qpdf-YL24KVRH.js} +14 -8
  50. package/dist/safe-bash/chunks/{sips-WHAMJFEU.js → sips-QXEQZERX.js} +14 -7
  51. package/dist/safe-bash/chunks/{soffice-SYEK6WJS.js → soffice-TBITRML3.js} +16 -8
  52. package/dist/safe-bash/chunks/{wkhtmltopdf-ZIICWJLF.js → wkhtmltopdf-NJA2THRC.js} +27 -7
  53. package/dist/safe-bash/command-chunks/chunk-2C5BEBPY.js +1388 -0
  54. package/dist/safe-bash/command-chunks/{chunk-2VTMZTV3.js → chunk-2EXV2ZLV.js} +2 -2
  55. package/dist/safe-bash/command-chunks/{chunk-UJOO6DKY.js → chunk-6EASC5EC.js} +1 -1
  56. package/dist/safe-bash/command-chunks/{chunk-ZF3ZVGQC.js → chunk-7JRITHKW.js} +150 -80
  57. package/dist/safe-bash/{chunks/chunk-LD76P7AG.js → command-chunks/chunk-AKDMCNX4.js} +74 -39
  58. package/dist/safe-bash/command-chunks/{chunk-6F7QJMJ3.js → chunk-CBDVLMJB.js} +1 -1
  59. package/dist/safe-bash/command-chunks/chunk-H4LWTITX.js +169 -0
  60. package/dist/safe-bash/command-chunks/chunk-I2MQNBGR.js +145 -0
  61. package/dist/safe-bash/command-chunks/{chunk-5GRLAARV.js → chunk-IOXI5ZAJ.js} +1 -1
  62. package/dist/safe-bash/command-chunks/{chunk-XNHSCU6I.js → chunk-MDPIOQUD.js} +6 -6
  63. package/dist/safe-bash/command-chunks/chunk-QI4MGIN6.js +6 -0
  64. package/dist/safe-bash/command-chunks/chunk-QTYZAWSB.js +91 -0
  65. package/dist/safe-bash/command-chunks/chunk-SNK7K3XW.js +90 -0
  66. package/dist/safe-bash/command-chunks/chunk-T5OLGM43.js +314 -0
  67. package/dist/safe-bash/command-chunks/chunk-TB7IFMUZ.js +802 -0
  68. package/dist/safe-bash/command-chunks/{chunk-AT6ZSOE6.js → chunk-TPPBPIWL.js} +1 -1
  69. package/dist/safe-bash/command-chunks/chunk-VMK4XVKO.js +82 -0
  70. package/dist/safe-bash/command-chunks/chunk-VNSJKXDU.js +130 -0
  71. package/dist/safe-bash/command-chunks/chunk-YFNTMH6Z.js +6 -0
  72. package/dist/safe-bash/commands/csplit/index.browser.js +2 -2
  73. package/dist/safe-bash/commands/docx/index.browser.js +2 -2
  74. package/dist/safe-bash/commands/factor/index.browser.js +2 -2
  75. package/dist/safe-bash/commands/getopt/index.browser.js +2 -2
  76. package/dist/safe-bash/commands/hexdump/index.browser.js +2 -2
  77. package/dist/safe-bash/commands/iconv/index.browser.js +2 -2
  78. package/dist/safe-bash/commands/line-endings/index.browser.js +2 -2
  79. package/dist/safe-bash/commands/llm/command.d.ts +1 -4
  80. package/dist/safe-bash/commands/llm/command.js +1 -248
  81. package/dist/safe-bash/commands/llm/elevenlabs-stream.d.ts +1 -2
  82. package/dist/safe-bash/commands/llm/elevenlabs-stream.js +1 -117
  83. package/dist/safe-bash/commands/llm/elevenlabs.d.ts +1 -15
  84. package/dist/safe-bash/commands/llm/elevenlabs.js +1 -123
  85. package/dist/safe-bash/commands/llm/index.browser.js +20 -15
  86. package/dist/safe-bash/commands/llm/index.d.ts +1 -6
  87. package/dist/safe-bash/commands/llm/index.js +1 -4
  88. package/dist/safe-bash/commands/llm/mime.d.ts +1 -2
  89. package/dist/safe-bash/commands/llm/mime.js +1 -136
  90. package/dist/safe-bash/commands/llm/openai-http.d.ts +1 -8
  91. package/dist/safe-bash/commands/llm/openai-http.js +1 -143
  92. package/dist/safe-bash/commands/llm/openai-sse.d.ts +1 -3
  93. package/dist/safe-bash/commands/llm/openai-sse.js +1 -78
  94. package/dist/safe-bash/commands/llm/openai.d.ts +1 -14
  95. package/dist/safe-bash/commands/llm/openai.js +1 -288
  96. package/dist/safe-bash/commands/llm/providers/elevenlabs.d.ts +1 -1
  97. package/dist/safe-bash/commands/llm/providers/elevenlabs.js +1 -1
  98. package/dist/safe-bash/commands/llm/providers/index.browser.js +6 -6
  99. package/dist/safe-bash/commands/llm/providers/index.d.ts +1 -3
  100. package/dist/safe-bash/commands/llm/providers/index.js +1 -2
  101. package/dist/safe-bash/commands/llm/providers/openai.d.ts +1 -1
  102. package/dist/safe-bash/commands/llm/providers/openai.js +1 -1
  103. package/dist/safe-bash/commands/llm/providers/shared.d.ts +1 -23
  104. package/dist/safe-bash/commands/llm/providers/shared.js +1 -65
  105. package/dist/safe-bash/commands/llm/service.d.ts +1 -37
  106. package/dist/safe-bash/commands/llm/service.js +1 -151
  107. package/dist/safe-bash/commands/llm/types.d.ts +1 -52
  108. package/dist/safe-bash/commands/llm/types.js +1 -1
  109. package/dist/safe-bash/commands/mdq/index.browser.js +3 -3
  110. package/dist/safe-bash/commands/media/index.browser.js +1 -1
  111. package/dist/safe-bash/commands/network/index.browser.js +9 -9
  112. package/dist/safe-bash/commands/network/types.d.ts +2 -40
  113. package/dist/safe-bash/commands/node/index.browser.js +6 -8
  114. package/dist/safe-bash/commands/op/index.browser.js +3 -3
  115. package/dist/safe-bash/commands/op/index.js +12 -28
  116. package/dist/safe-bash/commands/pr/index.browser.js +2 -2
  117. package/dist/safe-bash/commands/python/index.browser.js +8 -8
  118. package/dist/safe-bash/commands/python/worker.browser.js +2 -2
  119. package/dist/safe-bash/commands/tsort/index.browser.js +2 -2
  120. package/dist/safe-bash/commands/xml/index.browser.js +2 -2
  121. package/dist/safe-bash/commands/yq/index.browser.js +3 -3
  122. package/dist/safe-bash/core.browser.js +9775 -10975
  123. package/dist/safe-bash/core.d.ts +0 -1
  124. package/dist/safe-bash/core.js +0 -1
  125. package/dist/safe-bash/jobs.browser.js +5 -4
  126. package/dist/safe-bash/optional-host.browser.js +61 -88
  127. package/dist/safe-bash/shell/runtime.js +34 -12
  128. package/dist/safe-bash/yq-browser/index.js +1 -1
  129. package/dist/safe-bash-command-ffmpeg/index.js +8130 -8011
  130. package/dist/safe-bash-command-git/git_rust.wasm +0 -0
  131. package/dist/safe-bash-command-git/wasm.generated.js +1 -1
  132. package/dist/safe-bash-command-imagemagick/index.js +2 -2
  133. package/dist/safe-bash-command-llm/LICENSE +21 -0
  134. package/dist/safe-bash-command-llm/command.d.ts +5 -0
  135. package/dist/safe-bash-command-llm/command.js +14 -0
  136. package/dist/safe-bash-command-llm/configuration.d.ts +18 -0
  137. package/dist/safe-bash-command-llm/elevenlabs-stream.d.ts +2 -0
  138. package/dist/safe-bash-command-llm/elevenlabs-stream.js +7 -0
  139. package/dist/safe-bash-command-llm/elevenlabs.d.ts +15 -0
  140. package/dist/safe-bash-command-llm/elevenlabs.js +10 -0
  141. package/dist/safe-bash-command-llm/index.d.ts +10 -0
  142. package/dist/safe-bash-command-llm/index.js +46 -0
  143. package/dist/safe-bash-command-llm/mime.d.ts +2 -0
  144. package/dist/safe-bash-command-llm/mime.js +9 -0
  145. package/dist/safe-bash-command-llm/model-options.d.ts +3 -0
  146. package/dist/safe-bash-command-llm/openai-http.d.ts +8 -0
  147. package/dist/safe-bash-command-llm/openai-http.js +17 -0
  148. package/dist/safe-bash-command-llm/openai-sse.d.ts +3 -0
  149. package/dist/safe-bash-command-llm/openai-sse.js +8 -0
  150. package/dist/safe-bash-command-llm/openai.d.ts +14 -0
  151. package/dist/safe-bash-command-llm/openai.js +12 -0
  152. package/dist/safe-bash-command-llm/providers/elevenlabs.d.ts +1 -0
  153. package/dist/safe-bash-command-llm/providers/elevenlabs.js +11 -0
  154. package/dist/safe-bash-command-llm/providers/index.d.ts +3 -0
  155. package/dist/safe-bash-command-llm/providers/index.js +24 -0
  156. package/dist/safe-bash-command-llm/providers/openai.d.ts +1 -0
  157. package/dist/safe-bash-command-llm/providers/openai.js +13 -0
  158. package/dist/safe-bash-command-llm/providers/shared.d.ts +23 -0
  159. package/dist/safe-bash-command-llm/providers/shared.js +13 -0
  160. package/dist/safe-bash-command-llm/schemas.d.ts +2 -0
  161. package/dist/safe-bash-command-llm/service.d.ts +42 -0
  162. package/dist/safe-bash-command-llm/service.js +9 -0
  163. package/dist/safe-bash-command-llm/templates.d.ts +27 -0
  164. package/dist/safe-bash-command-llm/types.d.ts +87 -0
  165. package/dist/safe-bash-command-llm/types.js +6 -0
  166. package/dist/safe-bash-command-mmdc/command.d.ts +4 -0
  167. package/dist/safe-bash-command-mmdc/index.d.ts +1 -1
  168. package/dist/safe-bash-command-mmdc/index.js +7084 -6687
  169. package/dist/safe-bash-command-mmdc/layout.d.ts +1 -0
  170. package/dist/safe-bash-command-mmdc/parser.d.ts +1 -0
  171. package/dist/safe-bash-command-mmdc/png.d.ts +3 -0
  172. package/dist/safe-bash-command-mmdc/raster.d.ts +1 -0
  173. package/dist/safe-bash-command-pandoc/command.js +3 -3
  174. package/dist/safe-bash-command-pandoc/index.js +3 -3
  175. package/dist/safe-bash-command-pdfimages/index.js +147 -80
  176. package/dist/safe-bash-command-pdfinfo/index.js +147 -80
  177. package/dist/safe-bash-command-pdftk/index.js +147 -80
  178. package/dist/safe-bash-command-pdftoppm/index.js +147 -80
  179. package/dist/safe-bash-command-pdftotext/index.js +147 -80
  180. package/dist/safe-bash-command-qpdf/index.js +174 -101
  181. package/dist/safe-bash-command-sips/index.js +2 -2
  182. package/dist/safe-bash-command-soffice/index.js +1 -1
  183. package/dist/safe-bash-command-ssconvert/index.js +147 -80
  184. package/dist/safe-bash-command-wkhtmltopdf/index.js +147 -80
  185. package/dist/safe-bash-contracts/default-executor.d.ts +5 -0
  186. package/dist/safe-bash-contracts/default-executor.js +37 -0
  187. package/dist/safe-bash-contracts/http.d.ts +41 -0
  188. package/dist/safe-bash-contracts/http.js +6 -0
  189. package/dist/safe-bash-io-engine/commands/search/requirements.js +3 -3
  190. package/dist/safe-bash-io-engine/commands/text-programs/shared.js +3 -3
  191. package/dist/safe-bash-io-engine/gnu-information.d.ts +1 -1
  192. package/dist/safe-bash-io-engine/gnu-information.js +1 -1
  193. package/dist/safe-bash-io-engine/internal.js +2 -2
  194. package/dist/safe-bash-query-engine/index.js +3 -3
  195. package/dist/safe-bash-query-engine/interpreter.js +2 -2
  196. package/dist/safe-bash-query-engine/query-core.js +3 -3
  197. package/package.json +2 -2
  198. package/dist/safe-bash/chunks/chunk-2G2T5MEM.js +0 -14
  199. package/dist/safe-bash/chunks/chunk-2MUJ7RLZ.js +0 -409
  200. package/dist/safe-bash/chunks/chunk-GJWUEOT6.js +0 -14
  201. package/dist/safe-bash/chunks/chunk-H7MMJS26.js +0 -37
  202. package/dist/safe-bash/chunks/chunk-LAPF2XE6.js +0 -14
  203. package/dist/safe-bash/chunks/chunk-S2LSZFZ6.js +0 -758
  204. package/dist/safe-bash/chunks/chunk-T3VDYWKZ.js +0 -21
  205. package/dist/safe-bash/chunks/chunk-VUVKC6HV.js +0 -81
  206. package/dist/safe-bash/chunks/chunk-VYW4Q6KF.js +0 -12
  207. package/dist/safe-bash/command-chunks/{chunk-BJ6DGYJA.js → chunk-KNI4GRXF.js} +3 -3
@@ -27102,6 +27102,11 @@ function tokenizeCos(bytes, start = 0, end = bytes.length) {
27102
27102
  }
27103
27103
  return tokens;
27104
27104
  }
27105
+ function drainWork(work) {
27106
+ let step = work.next();
27107
+ while (!step.done) step = work.next();
27108
+ return step.value;
27109
+ }
27105
27110
  function bytesToString3(bytes) {
27106
27111
  if (typeof bytes !== "object" || bytes?.length === void 0) {
27107
27112
  throw new Error("Invalid argument for bytesToString");
@@ -27697,7 +27702,9 @@ function concatByteArrays(chunks) {
27697
27702
  }
27698
27703
  return out;
27699
27704
  }
27700
- function serializeCosDocument(options) {
27705
+ function* serializeCosDocumentSteps(options) {
27706
+ yield;
27707
+ let work = 0;
27701
27708
  const maxOutputBytes = options.maxOutputBytes ?? Infinity;
27702
27709
  const maxObjects = options.maxObjects ?? Infinity;
27703
27710
  const sorted = [...options.objects].sort((a, b) => a.objectNumber - b.objectNumber);
@@ -27720,6 +27727,7 @@ function serializeCosDocument(options) {
27720
27727
  const packable = [];
27721
27728
  const direct = [];
27722
27729
  for (const obj of sorted) {
27730
+ if (++work % 16 === 0) yield;
27723
27731
  const isLinearized = obj.value.kind === "dict" && dictGet(obj.value, "Linearized") !== void 0;
27724
27732
  if (obj.value.kind !== "stream" && obj.generationNumber === 0 && !isLinearized) {
27725
27733
  packable.push(obj);
@@ -27734,6 +27742,7 @@ function serializeCosDocument(options) {
27734
27742
  const headerPairs = [];
27735
27743
  let relOffset = 0;
27736
27744
  for (const pObj of packable) {
27745
+ if (++work % 16 === 0) yield;
27737
27746
  headerPairs.push(`${pObj.objectNumber} ${relOffset}`);
27738
27747
  const bBytes = serializeCosNodeBytes(pObj.value);
27739
27748
  const nl = textEncoder2.encode("\n");
@@ -27759,6 +27768,7 @@ function serializeCosDocument(options) {
27759
27768
  ];
27760
27769
  const directOffsets = /* @__PURE__ */ new Map();
27761
27770
  for (const dObj of allDirect) {
27771
+ if (++work % 16 === 0) yield;
27762
27772
  directOffsets.set(dObj.objectNumber, { offset, generation: dObj.generationNumber });
27763
27773
  const prefix = textEncoder2.encode(`${dObj.objectNumber} ${dObj.generationNumber} obj
27764
27774
  `);
@@ -27769,6 +27779,7 @@ function serializeCosDocument(options) {
27769
27779
  }
27770
27780
  const compressedMap = /* @__PURE__ */ new Map();
27771
27781
  for (let idx = 0; idx < packable.length; idx++) {
27782
+ if (++work % 16 === 0) yield;
27772
27783
  compressedMap.set(packable[idx].objectNumber, idx);
27773
27784
  }
27774
27785
  const xrefOffset2 = offset;
@@ -27786,6 +27797,7 @@ function serializeCosDocument(options) {
27786
27797
  };
27787
27798
  writeEntry7(0, 0, 0, 65535);
27788
27799
  for (let i = 1; i < size2; i++) {
27800
+ if (++work % 16 === 0) yield;
27789
27801
  if (i === xrefStmNum) {
27790
27802
  writeEntry7(i, 1, xrefOffset2, 0);
27791
27803
  } else if (compressedMap.has(i)) {
@@ -27866,6 +27878,7 @@ ${xrefOffset2}
27866
27878
  let pageCount = 0;
27867
27879
  let firstPageObjNum = options.rootRef.objectNumber;
27868
27880
  for (const o of sorted) {
27881
+ if (++work % 16 === 0) yield;
27869
27882
  if (o.value.kind === "dict") {
27870
27883
  const t = dictGet(o.value, "Type");
27871
27884
  if (t?.kind === "name" && t.decoded === "Page") {
@@ -27913,6 +27926,7 @@ ${xrefOffset2}
27913
27926
  }
27914
27927
  const objectOffsets = /* @__PURE__ */ new Map();
27915
27928
  for (const obj of sorted) {
27929
+ if (++work % 16 === 0) yield;
27916
27930
  objectOffsets.set(obj.objectNumber, { offset, generation: obj.generationNumber });
27917
27931
  let value = obj.value;
27918
27932
  if (options.normalizeContent && value.kind === "stream" && value.decodedBytes) {
@@ -27944,6 +27958,7 @@ ${xrefOffset2}
27944
27958
  0000000000 65535 f
27945
27959
  `;
27946
27960
  for (let i = 1; i < size; i++) {
27961
+ if (++work % 16 === 0) yield;
27947
27962
  const entry = objectOffsets.get(i);
27948
27963
  if (entry) {
27949
27964
  xrefText += `${String(entry.offset).padStart(10, "0")} ${String(entry.generation).padStart(5, "0")} n
@@ -27990,6 +28005,7 @@ ${xrefOffset}
27990
28005
  let endFirstPage = xrefOffset;
27991
28006
  const sortedOffsets = [...objectOffsets.entries()].sort((a, b) => a[1].offset - b[1].offset);
27992
28007
  for (let i = 0; i < sortedOffsets.length; i++) {
28008
+ if (++work % 16 === 0) yield;
27993
28009
  const [objNum, info3] = sortedOffsets[i];
27994
28010
  const nextOff = sortedOffsets[i + 1]?.[1].offset ?? xrefOffset;
27995
28011
  if (objNum === hintObjAfter.objectNumber) {
@@ -28023,6 +28039,9 @@ ${xrefOffset}
28023
28039
  }
28024
28040
  return fullBytes;
28025
28041
  }
28042
+ function serializeCosDocument(options) {
28043
+ return drainWork(serializeCosDocumentSteps(options));
28044
+ }
28026
28045
  function appendIncrementalRevision(originalBytes, doc, updatedObjects) {
28027
28046
  const chunks = [originalBytes, textEncoder2.encode("\n")];
28028
28047
  let offset = originalBytes.length + 1;
@@ -28296,48 +28315,30 @@ function encryptPdfBuffer(state, objectNumber, generationNumber, plaintext) {
28296
28315
  const objKey = md5Bytes([state.fileKey, suffix]).subarray(0, Math.min(16, state.fileKey.length + 5));
28297
28316
  return rc4Transform(objKey, plaintext);
28298
28317
  }
28299
- function transformNodeStringsAndStreams(node, transform, transformStream) {
28318
+ function* transformNodeStringsAndStreamsSteps(node, transform, transformStream) {
28319
+ yield;
28300
28320
  switch (node.kind) {
28301
28321
  case "string":
28302
- return {
28303
- kind: "string",
28304
- format: "hex",
28305
- bytes: transform(node.bytes)
28306
- };
28307
- case "array":
28308
- return {
28309
- kind: "array",
28310
- items: node.items.map((item) => transformNodeStringsAndStreams(item, transform, transformStream))
28311
- };
28322
+ return { kind: "string", format: "hex", bytes: transform(node.bytes) };
28323
+ case "array": {
28324
+ const items = [];
28325
+ for (const item of node.items) items.push(yield* transformNodeStringsAndStreamsSteps(item, transform, transformStream));
28326
+ return { kind: "array", items };
28327
+ }
28312
28328
  case "dict": {
28313
- const type = dictGet(node, "Type");
28314
- const fieldType = dictGet(node, "FT");
28315
- const byteRange = dictGet(node, "ByteRange");
28329
+ const type = dictGet(node, "Type"), fieldType = dictGet(node, "FT"), byteRange = dictGet(node, "ByteRange");
28316
28330
  const signature = type?.kind === "name" && type.decoded === "Sig" || fieldType?.kind === "name" && fieldType.decoded === "Sig" || byteRange?.kind === "array" && dictGet(node, "Filter")?.kind === "name";
28317
- return {
28318
- kind: "dict",
28319
- entries: node.entries.map((e) => ({
28320
- key: e.key,
28321
- value: signature && e.key.decoded === "Contents" ? e.value : transformNodeStringsAndStreams(e.value, transform, transformStream)
28322
- }))
28323
- };
28331
+ const entries = [];
28332
+ for (const e of node.entries) entries.push({ key: e.key, value: signature && e.key.decoded === "Contents" ? e.value : yield* transformNodeStringsAndStreamsSteps(e.value, transform, transformStream) });
28333
+ return { kind: "dict", entries };
28324
28334
  }
28325
28335
  case "stream": {
28326
28336
  const type = dictGet(node.dict, "Type");
28327
28337
  if (type?.kind === "name" && type.decoded === "XRef") return node;
28328
28338
  const stream = transformStream ? transformStream(node) : { ...node, rawBytes: transform(node.rawBytes) };
28329
- const updatedDict = {
28330
- kind: "dict",
28331
- entries: stream.dict.entries.map((e) => ({
28332
- key: e.key,
28333
- value: e.key.decoded === "Length" ? cosNumber(stream.rawBytes.length) : transformNodeStringsAndStreams(e.value, transform, transformStream)
28334
- }))
28335
- };
28336
- return {
28337
- kind: "stream",
28338
- dict: updatedDict,
28339
- rawBytes: stream.rawBytes
28340
- };
28339
+ const entries = [];
28340
+ for (const e of stream.dict.entries) entries.push({ key: e.key, value: e.key.decoded === "Length" ? cosNumber(stream.rawBytes.length) : yield* transformNodeStringsAndStreamsSteps(e.value, transform, transformStream) });
28341
+ return { kind: "stream", dict: { kind: "dict", entries }, rawBytes: stream.rawBytes };
28341
28342
  }
28342
28343
  default:
28343
28344
  return node;
@@ -28406,7 +28407,8 @@ function createR6Encryption(userPassword, ownerPassword, pMask) {
28406
28407
  StrF: cosName("StdCF")
28407
28408
  }) };
28408
28409
  }
28409
- function encryptCosDocument(doc, options = {}) {
28410
+ function* encryptCosDocumentSteps(doc, options = {}) {
28411
+ yield;
28410
28412
  const userPassword = options.userPassword ?? "";
28411
28413
  const ownerPassword = options.ownerPassword ?? userPassword;
28412
28414
  const revision = options.revision ?? 6;
@@ -28428,14 +28430,15 @@ function encryptCosDocument(doc, options = {}) {
28428
28430
  const encryptObjNum = maxObjNum + 1;
28429
28431
  const encryptedObjects = [];
28430
28432
  for (const obj of doc.objects.values()) {
28431
- const transformed = transformNodeStringsAndStreams(
28433
+ yield;
28434
+ const transformed = yield* transformNodeStringsAndStreamsSteps(
28432
28435
  obj.value,
28433
28436
  (plain) => encryptPdfBuffer(state, obj.objectNumber, obj.generationNumber, plain)
28434
28437
  );
28435
28438
  encryptedObjects.push({ objectNumber: obj.objectNumber, generationNumber: obj.generationNumber, value: transformed });
28436
28439
  }
28437
28440
  encryptedObjects.push({ objectNumber: encryptObjNum, generationNumber: 0, value: encryptDict });
28438
- return serializeCosDocument({
28441
+ return yield* serializeCosDocumentSteps({
28439
28442
  objects: encryptedObjects,
28440
28443
  rootRef: doc.rootRef,
28441
28444
  infoRef: doc.infoRef,
@@ -28485,17 +28488,19 @@ function decryptStream(doc, stream, transform) {
28485
28488
  }
28486
28489
  return { kind: "stream", dict, rawBytes: bytes };
28487
28490
  }
28488
- function decryptCosDocument(doc, state) {
28491
+ function* decryptCosDocumentSteps(doc, state) {
28492
+ yield;
28489
28493
  const encryptObjNum = doc.encryptRef?.objectNumber;
28490
28494
  const handler = securityHandlers.get(state);
28491
28495
  for (const [num, obj] of doc.objects.entries()) {
28496
+ yield;
28492
28497
  if (num === encryptObjNum || handler?.plaintextObjects.has(num)) continue;
28493
28498
  if (obj.value.kind === "stream") {
28494
28499
  const type = doc.resolve(dictGet(obj.value.dict, "Type"));
28495
28500
  if (type?.kind === "name" && (type.decoded === "XRef" || type.decoded === "Metadata" && !state.encryptMetadata)) continue;
28496
28501
  }
28497
28502
  const transform = handler?.factory.createCipherTransform(obj.objectNumber, obj.generationNumber);
28498
- const decrypted = transformNodeStringsAndStreams(
28503
+ const decrypted = yield* transformNodeStringsAndStreamsSteps(
28499
28504
  obj.value,
28500
28505
  (cipher) => decryptPdfBuffer(state, obj.objectNumber, obj.generationNumber, cipher),
28501
28506
  transform ? (stream) => decryptStream(doc, stream, transform) : void 0
@@ -28609,9 +28614,12 @@ var ParsedCosDocument = class {
28609
28614
  return void 0;
28610
28615
  }
28611
28616
  };
28612
- function parseNodeFromLexer(lexer, bytes, maxDepth, repair = false) {
28617
+ function* parseNodeFromLexerSteps(lexer, bytes, maxDepth, repair = false) {
28618
+ yield;
28619
+ let work = 0;
28613
28620
  const stack = [];
28614
28621
  while (true) {
28622
+ if (++work % 16 === 0) yield;
28615
28623
  const tok = lexer.nextToken();
28616
28624
  const parent = stack.at(-1);
28617
28625
  if (!tok) {
@@ -28807,7 +28815,8 @@ function parseHeaderVersion(bytes) {
28807
28815
  }
28808
28816
  return head.slice(idx + 5, idx + 8);
28809
28817
  }
28810
- function parseObjectAtOffset(bytes, offset, maxRecursionDepth, repair = false) {
28818
+ function* parseObjectAtOffsetSteps(bytes, offset, maxRecursionDepth, repair = false) {
28819
+ yield;
28811
28820
  const lexer = new CosByteLexer(bytes, offset);
28812
28821
  const objNumTok = lexer.nextToken();
28813
28822
  const genNumTok = lexer.nextToken();
@@ -28815,7 +28824,7 @@ function parseObjectAtOffset(bytes, offset, maxRecursionDepth, repair = false) {
28815
28824
  if (objNumTok?.kind !== "number" || genNumTok?.kind !== "number" || objKwTok?.kind !== "keyword" || objKwTok.value !== "obj") {
28816
28825
  throw new PdfError("E_PARSE", `Malformed indirect object header at byte offset ${offset}`);
28817
28826
  }
28818
- const value = parseNodeFromLexer(lexer, bytes, maxRecursionDepth, repair);
28827
+ const value = yield* parseNodeFromLexerSteps(lexer, bytes, maxRecursionDepth, repair);
28819
28828
  if (!value) {
28820
28829
  throw new PdfError("E_PARSE", `Empty indirect object ${objNumTok.value} at offset ${offset}`);
28821
28830
  }
@@ -28826,7 +28835,9 @@ function parseObjectAtOffset(bytes, offset, maxRecursionDepth, repair = false) {
28826
28835
  span: { start: objNumTok.span.start, end: lexer.offset }
28827
28836
  };
28828
28837
  }
28829
- function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects) {
28838
+ function* parseXrefRevisionAtSteps(bytes, xrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects) {
28839
+ yield;
28840
+ let work = 0;
28830
28841
  if (xrefOffset < 0 || xrefOffset >= bytes.length) {
28831
28842
  throw new PdfError("E_PARSE", `Invalid xref offset: ${xrefOffset}`);
28832
28843
  }
@@ -28838,6 +28849,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
28838
28849
  if (firstTok.kind === "keyword" && firstTok.value === "xref") {
28839
28850
  const entries2 = /* @__PURE__ */ new Map();
28840
28851
  while (true) {
28852
+ if (++work % 16 === 0) yield;
28841
28853
  const saved = lexer.offset;
28842
28854
  const tok = lexer.nextToken();
28843
28855
  if (!tok) {
@@ -28861,6 +28873,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
28861
28873
  }
28862
28874
  if (count > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
28863
28875
  for (let i = 0; i < count; i++) {
28876
+ if (++work % 16 === 0) yield;
28864
28877
  const offTok = lexer.nextToken();
28865
28878
  const genTok = lexer.nextToken();
28866
28879
  const flagTok = lexer.nextToken();
@@ -28886,7 +28899,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
28886
28899
  if (entries2.size > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
28887
28900
  }
28888
28901
  }
28889
- const trailerNode = parseNodeFromLexer(lexer, bytes, maxRecursionDepth);
28902
+ const trailerNode = yield* parseNodeFromLexerSteps(lexer, bytes, maxRecursionDepth);
28890
28903
  if (!trailerNode || trailerNode.kind !== "dict") {
28891
28904
  throw new PdfError("E_PARSE", "Missing trailer dictionary after xref table");
28892
28905
  }
@@ -28898,7 +28911,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
28898
28911
  previousXrefOffset: prevNode2?.kind === "number" ? prevNode2.value : void 0
28899
28912
  };
28900
28913
  }
28901
- const xrefObj = parseObjectAtOffset(bytes, xrefOffset, maxRecursionDepth);
28914
+ const xrefObj = yield* parseObjectAtOffsetSteps(bytes, xrefOffset, maxRecursionDepth);
28902
28915
  if (xrefObj.value.kind !== "stream") {
28903
28916
  throw new PdfError("E_PARSE", `Expected xref table or XRef stream at offset ${xrefOffset}`);
28904
28917
  }
@@ -28919,6 +28932,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
28919
28932
  if (indexArray) {
28920
28933
  if (indexArray.kind !== "array" || indexArray.items.length % 2 !== 0) throw new PdfError("E_PARSE", "Invalid XRef range fields");
28921
28934
  for (let i = 0; i < indexArray.items.length; i += 2) {
28935
+ if (++work % 16 === 0) yield;
28922
28936
  const first = indexArray.items[i];
28923
28937
  const count = indexArray.items[i + 1];
28924
28938
  if (first?.kind !== "number" || count?.kind !== "number" || !Number.isSafeInteger(first.value) || first.value < 0 || !Number.isSafeInteger(count.value) || count.value < 0 || !Number.isSafeInteger(first.value + count.value)) {
@@ -28941,9 +28955,11 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
28941
28955
  return v;
28942
28956
  };
28943
28957
  for (const [startObj, count] of subsections) {
28958
+ if (++work % 16 === 0) yield;
28944
28959
  if (count > (decoded.length - pos) / stride) throw new PdfError("E_PARSE", "Truncated XRef stream");
28945
28960
  if (count > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
28946
28961
  for (let i = 0; i < count; i++) {
28962
+ if (++work % 16 === 0) yield;
28947
28963
  const objNum = startObj + i;
28948
28964
  const field0 = w0 > 0 ? readInt(w0) : 1;
28949
28965
  const field1 = readInt(w1);
@@ -28989,12 +29005,16 @@ function isAsciiWhitespace(ch3) {
28989
29005
  function isAsciiDigit(ch3) {
28990
29006
  return ch3 >= 48 && ch3 <= 57;
28991
29007
  }
28992
- function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
29008
+ function* repairScanCosDocumentSteps(bytes, maxObjects, maxRecursionDepth) {
29009
+ yield;
29010
+ let work = 0;
28993
29011
  const objects = /* @__PURE__ */ new Map();
28994
29012
  const trailers = [];
28995
29013
  let pos = 0;
28996
29014
  while (pos < bytes.length) {
29015
+ if (++work % 16 === 0) yield;
28997
29016
  while (pos < bytes.length && !isAsciiDigit(bytes[pos])) {
29017
+ if (++work % 16 === 0) yield;
28998
29018
  if (bytes[pos] === 37) {
28999
29019
  while (pos < bytes.length && bytes[pos] !== 10 && bytes[pos] !== 13) pos++;
29000
29020
  continue;
@@ -29004,7 +29024,7 @@ function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
29004
29024
  try {
29005
29025
  const token = lexer.nextToken();
29006
29026
  if (token?.kind === "keyword" && token.value === "trailer") {
29007
- const trailer = parseNodeFromLexer(lexer, bytes, maxRecursionDepth, true);
29027
+ const trailer = yield* parseNodeFromLexerSteps(lexer, bytes, maxRecursionDepth, true);
29008
29028
  if (trailer?.kind === "dict") trailers.push(trailer);
29009
29029
  pos = lexer.offset;
29010
29030
  continue;
@@ -29037,7 +29057,7 @@ function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
29037
29057
  bytes[pos + 3] === 40)) {
29038
29058
  pos += 3;
29039
29059
  try {
29040
- const parsed = parseObjectAtOffset(bytes, headerStart, maxRecursionDepth, true);
29060
+ const parsed = yield* parseObjectAtOffsetSteps(bytes, headerStart, maxRecursionDepth, true);
29041
29061
  objects.set(parsed.objectNumber, parsed);
29042
29062
  if (parsed.value.kind === "stream") {
29043
29063
  const type = dictGet(parsed.value.dict, "Type");
@@ -29054,13 +29074,17 @@ function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
29054
29074
  }
29055
29075
  return { objects, trailers };
29056
29076
  }
29057
- function unpackRecoveredObjects(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth) {
29077
+ function* unpackRecoveredObjectsSteps(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth) {
29078
+ yield;
29079
+ let work = 0;
29058
29080
  for (const obj of [...objects.values()]) {
29081
+ if (++work % 16 === 0) yield;
29059
29082
  if (obj.value.kind === "stream") {
29060
29083
  const t = dictGet(obj.value.dict, "Type");
29061
29084
  if (t?.kind === "name" && t.decoded === "ObjStm") {
29062
29085
  try {
29063
- for (const [unpackedNum, unpackedVal] of unpackObjectStream(obj.value, maxDecompressedBytes, maxRecursionDepth, true).entries()) {
29086
+ for (const [unpackedNum, unpackedVal] of (yield* unpackObjectStreamSteps(obj.value, maxDecompressedBytes, maxRecursionDepth, true)).entries()) {
29087
+ if (++work % 16 === 0) yield;
29064
29088
  if (!objects.has(unpackedNum)) {
29065
29089
  objects.set(unpackedNum, {
29066
29090
  objectNumber: unpackedNum,
@@ -29096,7 +29120,9 @@ function recoveredReferences(objects) {
29096
29120
  }
29097
29121
  return { ...rootRef ? { rootRef } : {}, ...infoRef ? { infoRef } : {} };
29098
29122
  }
29099
- function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth, repair = false) {
29123
+ function* unpackObjectStreamSteps(streamObj, maxDecompressedBytes, maxRecursionDepth, repair = false) {
29124
+ yield;
29125
+ let work = 0;
29100
29126
  const nNode = dictGet(streamObj.dict, "N");
29101
29127
  const firstNode = dictGet(streamObj.dict, "First");
29102
29128
  if (nNode?.kind !== "number" || firstNode?.kind !== "number") {
@@ -29106,6 +29132,7 @@ function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth,
29106
29132
  const headerLexer = new CosByteLexer(decoded, 0);
29107
29133
  const pairs = [];
29108
29134
  for (let i = 0; i < nNode.value; i++) {
29135
+ if (++work % 16 === 0) yield;
29109
29136
  const numTok = headerLexer.nextToken();
29110
29137
  const offTok = headerLexer.nextToken();
29111
29138
  if (numTok?.kind === "number" && offTok?.kind === "number") {
@@ -29114,9 +29141,10 @@ function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth,
29114
29141
  }
29115
29142
  const result = /* @__PURE__ */ new Map();
29116
29143
  for (const pair of pairs) {
29144
+ if (++work % 16 === 0) yield;
29117
29145
  try {
29118
29146
  const valLexer = new CosByteLexer(decoded, firstNode.value + pair.relativeOffset);
29119
- const node = parseNodeFromLexer(valLexer, decoded, maxRecursionDepth, repair);
29147
+ const node = yield* parseNodeFromLexerSteps(valLexer, decoded, maxRecursionDepth, repair);
29120
29148
  if (node) {
29121
29149
  result.set(pair.objectNumber, node);
29122
29150
  }
@@ -29126,7 +29154,9 @@ function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth,
29126
29154
  }
29127
29155
  return result;
29128
29156
  }
29129
- function parseCosDocument(bytes, options = {}) {
29157
+ function* parseCosDocumentSteps(bytes, options = {}) {
29158
+ yield;
29159
+ let work = 0;
29130
29160
  const version = parseHeaderVersion(bytes);
29131
29161
  const recovery = options.recovery ?? "strict";
29132
29162
  const maxObjects = options.maxObjects ?? Infinity;
@@ -29161,10 +29191,12 @@ function parseCosDocument(bytes, options = {}) {
29161
29191
  let currentXrefOffset = offsetTok.value;
29162
29192
  const visitedOffsets = /* @__PURE__ */ new Set();
29163
29193
  while (currentXrefOffset !== void 0 && !visitedOffsets.has(currentXrefOffset)) {
29194
+ if (++work % 16 === 0) yield;
29164
29195
  visitedOffsets.add(currentXrefOffset);
29165
- const rev = parseXrefRevisionAt(bytes, currentXrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects);
29196
+ const rev = yield* parseXrefRevisionAtSteps(bytes, currentXrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects);
29166
29197
  revisions.push(rev);
29167
29198
  for (const [num, entry] of rev.entries.entries()) {
29199
+ if (++work % 16 === 0) yield;
29168
29200
  if (!mergedXref.has(num)) {
29169
29201
  mergedXref.set(num, entry);
29170
29202
  if (mergedXref.size > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
@@ -29197,8 +29229,9 @@ function parseCosDocument(bytes, options = {}) {
29197
29229
  if (!useRepair && rootRef) {
29198
29230
  try {
29199
29231
  for (const [objNum, entry] of mergedXref.entries()) {
29232
+ if (++work % 16 === 0) yield;
29200
29233
  if (entry.type !== "uncompressed") continue;
29201
- const parsed = parseObjectAtOffset(bytes, entry.offset ?? 0, maxRecursionDepth, recovery === "repair");
29234
+ const parsed = yield* parseObjectAtOffsetSteps(bytes, entry.offset ?? 0, maxRecursionDepth, recovery === "repair");
29202
29235
  if (parsed.objectNumber !== objNum || parsed.generationNumber !== (entry.generationNumber ?? 0)) {
29203
29236
  throw new PdfError("E_PARSE", `Bad uncompressed XRef entry for object ${objNum}`);
29204
29237
  }
@@ -29216,7 +29249,7 @@ function parseCosDocument(bytes, options = {}) {
29216
29249
  throw new PdfError("E_PARSE", "PDF trailer missing /Root reference");
29217
29250
  }
29218
29251
  useRepair = true;
29219
- const scanned = repairScanCosDocument(bytes, maxObjects, maxRecursionDepth);
29252
+ const scanned = yield* repairScanCosDocumentSteps(bytes, maxObjects, maxRecursionDepth);
29220
29253
  objects = scanned.objects;
29221
29254
  revisions.length = 0;
29222
29255
  mergedXref.clear();
@@ -29251,7 +29284,7 @@ function parseCosDocument(bytes, options = {}) {
29251
29284
  encryptNode = encrypt?.kind === "ref" || encrypt?.kind === "dict" ? encrypt : void 0;
29252
29285
  encryptRef = encrypt?.kind === "ref" ? encrypt : void 0;
29253
29286
  idArray = id?.kind === "array" ? id : void 0;
29254
- if (!rootRef && !encryptNode) unpackRecoveredObjects(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
29287
+ if (!rootRef && !encryptNode) yield* unpackRecoveredObjectsSteps(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
29255
29288
  const fallback = recoveredReferences(objects);
29256
29289
  rootRef ??= fallback.rootRef;
29257
29290
  infoRef ??= fallback.infoRef;
@@ -29286,20 +29319,21 @@ function parseCosDocument(bytes, options = {}) {
29286
29319
  maxRecursionDepth
29287
29320
  });
29288
29321
  doc.encryption = encState;
29289
- decryptCosDocument(doc, encState);
29322
+ yield* decryptCosDocumentSteps(doc, encState);
29290
29323
  }
29291
29324
  if (useRepair) {
29292
- unpackRecoveredObjects(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
29325
+ yield* unpackRecoveredObjectsSteps(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
29293
29326
  doc.infoRef ??= recoveredReferences(objects).infoRef;
29294
29327
  }
29295
29328
  const objStmCache = /* @__PURE__ */ new Map();
29296
29329
  for (const [objNum, entry] of mergedXref.entries()) {
29330
+ if (++work % 16 === 0) yield;
29297
29331
  if (entry.type === "compressed" && entry.objectStreamNumber !== void 0) {
29298
29332
  let unpacked = objStmCache.get(entry.objectStreamNumber);
29299
29333
  if (!unpacked) {
29300
29334
  const stmObj = objects.get(entry.objectStreamNumber)?.value;
29301
29335
  if (stmObj?.kind === "stream") {
29302
- unpacked = unpackObjectStream(stmObj, maxDecompressedBytes, maxRecursionDepth, recovery === "repair");
29336
+ unpacked = yield* unpackObjectStreamSteps(stmObj, maxDecompressedBytes, maxRecursionDepth, recovery === "repair");
29303
29337
  objStmCache.set(entry.objectStreamNumber, unpacked);
29304
29338
  }
29305
29339
  }
@@ -29316,6 +29350,9 @@ function parseCosDocument(bytes, options = {}) {
29316
29350
  }
29317
29351
  return doc;
29318
29352
  }
29353
+ function parseCosDocument(bytes, options = {}) {
29354
+ return drainWork(parseCosDocumentSteps(bytes, options));
29355
+ }
29319
29356
  var WIN_ANSI_HIGH_BYTES = {
29320
29357
  128: 8364,
29321
29358
  130: 8218,
@@ -40346,7 +40383,8 @@ function resolveInheritedFieldEntry(doc, dict, key) {
40346
40383
  }
40347
40384
  return void 0;
40348
40385
  }
40349
- function collectFieldDicts(doc, node, prefix = "", out = [], parentFt = "", parentFf = 0, parentQ = 0, parentMaxLen) {
40386
+ function* collectFieldDictsSteps(doc, node, prefix = "", out = [], parentFt = "", parentFf = 0, parentQ = 0, parentMaxLen, yieldState = { work: 0 }) {
40387
+ if (++yieldState.work % 256 === 0) yield;
40350
40388
  const dict = doc.resolveDict(node);
40351
40389
  if (!dict) return out;
40352
40390
  const tNode = doc.resolve(dictGet(dict, "T"));
@@ -40379,7 +40417,8 @@ function collectFieldDicts(doc, node, prefix = "", out = [], parentFt = "", pare
40379
40417
  return out;
40380
40418
  }
40381
40419
  for (const kid of kidsArr.items) {
40382
- collectFieldDicts(doc, kid, fullName, out, currentFt, currentFf, currentQ, currentMaxLen);
40420
+ if (++yieldState.work % 16 === 0) yield;
40421
+ yield* collectFieldDictsSteps(doc, kid, fullName, out, currentFt, currentFf, currentQ, currentMaxLen, yieldState);
40383
40422
  }
40384
40423
  } else if (fullName) {
40385
40424
  out.push({
@@ -41384,6 +41423,9 @@ q ${sx} 0 0 ${sy} ${tx} ${ty} cm /${appearanceName} Do Q
41384
41423
  dictSet(acroForm, "NeedAppearances", cosBool(false));
41385
41424
  }
41386
41425
  }
41426
+ function collectFieldDicts(doc, node, prefix = "", out = [], parentFt = "", parentFf = 0, parentQ = 0, parentMaxLen) {
41427
+ drainWork(collectFieldDictsSteps(doc, node, prefix, out, parentFt, parentFf, parentQ, parentMaxLen));
41428
+ }
41387
41429
  var PdfPage = class {
41388
41430
  cosDoc;
41389
41431
  pageRef;
@@ -43837,14 +43879,18 @@ var PdfDocument = class _PdfDocument {
43837
43879
  });
43838
43880
  return new _PdfDocument(parseCosDocument(rawBytes));
43839
43881
  }
43840
- static load(bytes, options) {
43882
+ static *loadSteps(bytes, options) {
43883
+ yield;
43841
43884
  return new _PdfDocument(
43842
- parseCosDocument(bytes, {
43885
+ yield* parseCosDocumentSteps(bytes, {
43843
43886
  ...options,
43844
43887
  recovery: options?.recovery ?? "repair"
43845
43888
  })
43846
43889
  );
43847
43890
  }
43891
+ static load(bytes, options) {
43892
+ return drainWork(_PdfDocument.loadSteps(bytes, options));
43893
+ }
43848
43894
  rebuildPagesList() {
43849
43895
  const catalog = this.cos.resolveDict(this.cos.rootRef);
43850
43896
  const pagesNode = catalog ? dictGet(catalog, "Pages") : void 0;
@@ -43926,20 +43972,30 @@ var PdfDocument = class _PdfDocument {
43926
43972
  this.pages.splice(index, 1);
43927
43973
  this.syncPageTree();
43928
43974
  }
43929
- copyPagesFrom(sourceDoc, indices) {
43975
+ *copyPagesFromSteps(sourceDoc, indices) {
43976
+ function* cloneItems(items) {
43977
+ const result = [];
43978
+ for (const item of items) result.push(yield* cloneNode(item));
43979
+ return result;
43980
+ }
43981
+ yield;
43982
+ let work = 0;
43930
43983
  const memo = /* @__PURE__ */ new Map();
43931
43984
  const sourcePageObjNums = /* @__PURE__ */ new Set();
43932
43985
  for (let i = 0; i < sourceDoc.getPageCount(); i++) {
43986
+ if (++work % 16 === 0) yield;
43933
43987
  sourcePageObjNums.add(sourceDoc.getPage(i).ref.objectNumber);
43934
43988
  }
43935
43989
  const targetPageRefs = [];
43936
43990
  for (const idx of indices) {
43991
+ if (++work % 16 === 0) yield;
43937
43992
  const srcPage = sourceDoc.getPage(idx);
43938
43993
  const pageRef = this.cos.allocateObject({ kind: "null" });
43939
43994
  targetPageRefs.push(pageRef);
43940
43995
  memo.set(srcPage.ref.objectNumber, pageRef);
43941
43996
  }
43942
- const cloneNode = (node) => {
43997
+ const cloneNode = function* (node) {
43998
+ if (++work % 16 === 0) yield;
43943
43999
  if (node.kind === "ref") {
43944
44000
  const existing = memo.get(node.objectNumber);
43945
44001
  if (existing) return existing;
@@ -43950,12 +44006,12 @@ var PdfDocument = class _PdfDocument {
43950
44006
  if (!target) return cosRef(0);
43951
44007
  const placeholderRef = this.cos.allocateObject({ kind: "null" });
43952
44008
  memo.set(node.objectNumber, placeholderRef);
43953
- const clonedTarget = cloneNode(target);
44009
+ const clonedTarget = yield* cloneNode(target);
43954
44010
  this.cos.setObject(placeholderRef.objectNumber, clonedTarget, 0);
43955
44011
  return placeholderRef;
43956
44012
  }
43957
44013
  if (node.kind === "array") {
43958
- return cosArray(node.items.map(cloneNode));
44014
+ return cosArray(yield* cloneItems(node.items));
43959
44015
  }
43960
44016
  if (node.kind === "dict") {
43961
44017
  const typeEntry = dictGet(node, "Type");
@@ -43965,13 +44021,13 @@ var PdfDocument = class _PdfDocument {
43965
44021
  if (isPageTreeNode && entry.key.decoded === "Parent") continue;
43966
44022
  newEntries.push({
43967
44023
  key: { ...entry.key },
43968
- value: cloneNode(entry.value)
44024
+ value: yield* cloneNode(entry.value)
43969
44025
  });
43970
44026
  }
43971
44027
  return { kind: "dict", entries: newEntries };
43972
44028
  }
43973
44029
  if (node.kind === "stream") {
43974
- const clonedDict = cloneNode(node.dict);
44030
+ const clonedDict = yield* cloneNode(node.dict);
43975
44031
  return {
43976
44032
  kind: "stream",
43977
44033
  dict: clonedDict,
@@ -43980,11 +44036,12 @@ var PdfDocument = class _PdfDocument {
43980
44036
  };
43981
44037
  }
43982
44038
  return node;
43983
- };
44039
+ }.bind(this);
43984
44040
  const copiedPages = [];
43985
44041
  for (const [position, idx] of indices.entries()) {
44042
+ if (++work % 16 === 0) yield;
43986
44043
  const srcPage = sourceDoc.getPage(idx);
43987
- const clonedPageDict = cloneNode(srcPage.pageDict);
44044
+ const clonedPageDict = yield* cloneNode(srcPage.pageDict);
43988
44045
  const size = srcPage.getSize();
43989
44046
  if (!dictGet(clonedPageDict, "MediaBox")) {
43990
44047
  dictSet(
@@ -43994,13 +44051,14 @@ var PdfDocument = class _PdfDocument {
43994
44051
  );
43995
44052
  }
43996
44053
  if (!dictGet(clonedPageDict, "Resources")) {
43997
- dictSet(clonedPageDict, "Resources", cloneNode(srcPage.getResourcesDict()));
44054
+ dictSet(clonedPageDict, "Resources", yield* cloneNode(srcPage.getResourcesDict()));
43998
44055
  }
43999
44056
  if (!dictGet(clonedPageDict, "Rotate") && srcPage.getRotation() !== 0) {
44000
44057
  dictSet(clonedPageDict, "Rotate", cosNumber(srcPage.getRotation()));
44001
44058
  }
44002
44059
  const inheritedBoxKeys = ["CropBox", "BleedBox", "TrimBox", "ArtBox"];
44003
44060
  for (const boxKey of inheritedBoxKeys) {
44061
+ if (++work % 16 === 0) yield;
44004
44062
  if (!dictGet(clonedPageDict, boxKey)) {
44005
44063
  const boxVal = boxKey === "CropBox" ? srcPage.getCropBox() : boxKey === "BleedBox" ? srcPage.getBleedBox() : boxKey === "TrimBox" ? srcPage.getTrimBox() : srcPage.getArtBox();
44006
44064
  if (boxVal[0] !== 0 || boxVal[1] !== 0 || boxVal[2] !== size.width || boxVal[3] !== size.height) {
@@ -44042,8 +44100,9 @@ var PdfDocument = class _PdfDocument {
44042
44100
  };
44043
44101
  const clonedRootFieldRefs = [];
44044
44102
  for (const item of srcFieldsArr.items) {
44103
+ if (++work % 16 === 0) yield;
44045
44104
  if (!hasClonedDescendant(item)) continue;
44046
- const cloned = cloneNode(item);
44105
+ const cloned = yield* cloneNode(item);
44047
44106
  if (cloned.kind === "ref") {
44048
44107
  const clonedDict = this.cos.resolveDict(cloned);
44049
44108
  const kidsArr = clonedDict ? this.cos.resolveArray(dictGet(clonedDict, "Kids")) : void 0;
@@ -44077,6 +44136,7 @@ var PdfDocument = class _PdfDocument {
44077
44136
  dstFields.items.filter((x) => x.kind === "ref").map((x) => x.objectNumber)
44078
44137
  );
44079
44138
  for (const fRef of clonedRootFieldRefs) {
44139
+ if (++work % 16 === 0) yield;
44080
44140
  if (!existingNums.has(fRef.objectNumber)) {
44081
44141
  dstFields.items.push(fRef);
44082
44142
  existingNums.add(fRef.objectNumber);
@@ -44084,11 +44144,11 @@ var PdfDocument = class _PdfDocument {
44084
44144
  }
44085
44145
  const srcDr = dictGet(srcAcroForm, "DR");
44086
44146
  if (srcDr && !dictGet(dstAcroForm, "DR")) {
44087
- dictSet(dstAcroForm, "DR", cloneNode(srcDr));
44147
+ dictSet(dstAcroForm, "DR", yield* cloneNode(srcDr));
44088
44148
  }
44089
44149
  const srcDa = dictGet(srcAcroForm, "DA");
44090
44150
  if (srcDa && !dictGet(dstAcroForm, "DA")) {
44091
- dictSet(dstAcroForm, "DA", cloneNode(srcDa));
44151
+ dictSet(dstAcroForm, "DA", yield* cloneNode(srcDa));
44092
44152
  }
44093
44153
  }
44094
44154
  }
@@ -44097,12 +44157,15 @@ var PdfDocument = class _PdfDocument {
44097
44157
  if (srcOcProps) {
44098
44158
  const dstCatalog = this.cos.resolveDict(this.cos.rootRef);
44099
44159
  if (dstCatalog && !dictGet(dstCatalog, "OCProperties")) {
44100
- dictSet(dstCatalog, "OCProperties", cloneNode(srcOcProps));
44160
+ dictSet(dstCatalog, "OCProperties", yield* cloneNode(srcOcProps));
44101
44161
  }
44102
44162
  }
44103
44163
  this.syncPageTree();
44104
44164
  return copiedPages;
44105
44165
  }
44166
+ copyPagesFrom(sourceDoc, indices) {
44167
+ return drainWork(this.copyPagesFromSteps(sourceDoc, indices));
44168
+ }
44106
44169
  embedStandardFont(fontName) {
44107
44170
  return createStandardFontHandle(fontName);
44108
44171
  }
@@ -44302,14 +44365,15 @@ var PdfDocument = class _PdfDocument {
44302
44365
  });
44303
44366
  return buildSemanticAstFromPages(extractedPages, tablesByPage, displayLists);
44304
44367
  }
44305
- save(options = {}) {
44368
+ *saveSteps(options = {}) {
44369
+ yield;
44306
44370
  this.syncPageTree();
44307
44371
  if (this.cos.requiresFullRewrite) {
44308
44372
  pruneUnusedXObjects(this.cos, this.pages);
44309
44373
  discardUnreachableObjects(this.cos);
44310
44374
  }
44311
44375
  if (options.encrypt) {
44312
- return encryptCosDocument(this.cos, options.encrypt);
44376
+ return yield* encryptCosDocumentSteps(this.cos, options.encrypt);
44313
44377
  }
44314
44378
  const allObjects = [...this.cos.objects.values()].sort(
44315
44379
  (a, b) => a.objectNumber - b.objectNumber
@@ -44317,7 +44381,7 @@ var PdfDocument = class _PdfDocument {
44317
44381
  if (options.incremental && !this.cos.requiresFullRewrite && this.cos.bytes.length > 0) {
44318
44382
  return appendIncrementalRevision(this.cos.bytes, this.cos, allObjects);
44319
44383
  }
44320
- return serializeCosDocument({
44384
+ return yield* serializeCosDocumentSteps({
44321
44385
  objects: allObjects,
44322
44386
  rootRef: this.cos.rootRef,
44323
44387
  infoRef: this.cos.infoRef,
@@ -44328,6 +44392,9 @@ var PdfDocument = class _PdfDocument {
44328
44392
  linearize: options.linearize
44329
44393
  });
44330
44394
  }
44395
+ save(options = {}) {
44396
+ return drainWork(this.saveSteps(options));
44397
+ }
44331
44398
  };
44332
44399
  function resolveDestinationPageIndex(doc, destOrActionNode) {
44333
44400
  const cos = doc.cos;