@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
@@ -3646,6 +3646,11 @@ function tokenizeCos(bytes, start = 0, end = bytes.length) {
3646
3646
  }
3647
3647
  return tokens;
3648
3648
  }
3649
+ function drainWork(work) {
3650
+ let step = work.next();
3651
+ while (!step.done) step = work.next();
3652
+ return step.value;
3653
+ }
3649
3654
  function bytesToString3(bytes) {
3650
3655
  if (typeof bytes !== "object" || bytes?.length === void 0) {
3651
3656
  throw new Error("Invalid argument for bytesToString");
@@ -4067,7 +4072,9 @@ function concatByteArrays(chunks) {
4067
4072
  }
4068
4073
  return out;
4069
4074
  }
4070
- function serializeCosDocument(options) {
4075
+ function* serializeCosDocumentSteps(options) {
4076
+ yield;
4077
+ let work = 0;
4071
4078
  const maxOutputBytes = options.maxOutputBytes ?? Infinity;
4072
4079
  const maxObjects = options.maxObjects ?? Infinity;
4073
4080
  const sorted = [...options.objects].sort((a, b) => a.objectNumber - b.objectNumber);
@@ -4090,6 +4097,7 @@ function serializeCosDocument(options) {
4090
4097
  const packable = [];
4091
4098
  const direct = [];
4092
4099
  for (const obj of sorted) {
4100
+ if (++work % 16 === 0) yield;
4093
4101
  const isLinearized = obj.value.kind === "dict" && dictGet(obj.value, "Linearized") !== void 0;
4094
4102
  if (obj.value.kind !== "stream" && obj.generationNumber === 0 && !isLinearized) {
4095
4103
  packable.push(obj);
@@ -4104,6 +4112,7 @@ function serializeCosDocument(options) {
4104
4112
  const headerPairs = [];
4105
4113
  let relOffset = 0;
4106
4114
  for (const pObj of packable) {
4115
+ if (++work % 16 === 0) yield;
4107
4116
  headerPairs.push(`${pObj.objectNumber} ${relOffset}`);
4108
4117
  const bBytes = serializeCosNodeBytes(pObj.value);
4109
4118
  const nl = textEncoder2.encode("\n");
@@ -4129,6 +4138,7 @@ function serializeCosDocument(options) {
4129
4138
  ];
4130
4139
  const directOffsets = /* @__PURE__ */ new Map();
4131
4140
  for (const dObj of allDirect) {
4141
+ if (++work % 16 === 0) yield;
4132
4142
  directOffsets.set(dObj.objectNumber, { offset, generation: dObj.generationNumber });
4133
4143
  const prefix = textEncoder2.encode(`${dObj.objectNumber} ${dObj.generationNumber} obj
4134
4144
  `);
@@ -4139,6 +4149,7 @@ function serializeCosDocument(options) {
4139
4149
  }
4140
4150
  const compressedMap = /* @__PURE__ */ new Map();
4141
4151
  for (let idx = 0; idx < packable.length; idx++) {
4152
+ if (++work % 16 === 0) yield;
4142
4153
  compressedMap.set(packable[idx].objectNumber, idx);
4143
4154
  }
4144
4155
  const xrefOffset2 = offset;
@@ -4156,6 +4167,7 @@ function serializeCosDocument(options) {
4156
4167
  };
4157
4168
  writeEntry7(0, 0, 0, 65535);
4158
4169
  for (let i = 1; i < size2; i++) {
4170
+ if (++work % 16 === 0) yield;
4159
4171
  if (i === xrefStmNum) {
4160
4172
  writeEntry7(i, 1, xrefOffset2, 0);
4161
4173
  } else if (compressedMap.has(i)) {
@@ -4236,6 +4248,7 @@ ${xrefOffset2}
4236
4248
  let pageCount = 0;
4237
4249
  let firstPageObjNum = options.rootRef.objectNumber;
4238
4250
  for (const o of sorted) {
4251
+ if (++work % 16 === 0) yield;
4239
4252
  if (o.value.kind === "dict") {
4240
4253
  const t = dictGet(o.value, "Type");
4241
4254
  if (t?.kind === "name" && t.decoded === "Page") {
@@ -4283,6 +4296,7 @@ ${xrefOffset2}
4283
4296
  }
4284
4297
  const objectOffsets = /* @__PURE__ */ new Map();
4285
4298
  for (const obj of sorted) {
4299
+ if (++work % 16 === 0) yield;
4286
4300
  objectOffsets.set(obj.objectNumber, { offset, generation: obj.generationNumber });
4287
4301
  let value = obj.value;
4288
4302
  if (options.normalizeContent && value.kind === "stream" && value.decodedBytes) {
@@ -4314,6 +4328,7 @@ ${xrefOffset2}
4314
4328
  0000000000 65535 f
4315
4329
  `;
4316
4330
  for (let i = 1; i < size; i++) {
4331
+ if (++work % 16 === 0) yield;
4317
4332
  const entry = objectOffsets.get(i);
4318
4333
  if (entry) {
4319
4334
  xrefText += `${String(entry.offset).padStart(10, "0")} ${String(entry.generation).padStart(5, "0")} n
@@ -4360,6 +4375,7 @@ ${xrefOffset}
4360
4375
  let endFirstPage = xrefOffset;
4361
4376
  const sortedOffsets = [...objectOffsets.entries()].sort((a, b) => a[1].offset - b[1].offset);
4362
4377
  for (let i = 0; i < sortedOffsets.length; i++) {
4378
+ if (++work % 16 === 0) yield;
4363
4379
  const [objNum, info3] = sortedOffsets[i];
4364
4380
  const nextOff = sortedOffsets[i + 1]?.[1].offset ?? xrefOffset;
4365
4381
  if (objNum === hintObjAfter.objectNumber) {
@@ -4393,6 +4409,9 @@ ${xrefOffset}
4393
4409
  }
4394
4410
  return fullBytes;
4395
4411
  }
4412
+ function serializeCosDocument(options) {
4413
+ return drainWork(serializeCosDocumentSteps(options));
4414
+ }
4396
4415
  function appendIncrementalRevision(originalBytes, doc, updatedObjects) {
4397
4416
  const chunks = [originalBytes, textEncoder2.encode("\n")];
4398
4417
  let offset = originalBytes.length + 1;
@@ -4631,48 +4650,30 @@ function encryptPdfBuffer(state, objectNumber, generationNumber, plaintext) {
4631
4650
  const objKey = md5Bytes([state.fileKey, suffix]).subarray(0, Math.min(16, state.fileKey.length + 5));
4632
4651
  return rc4Transform(objKey, plaintext);
4633
4652
  }
4634
- function transformNodeStringsAndStreams(node, transform, transformStream) {
4653
+ function* transformNodeStringsAndStreamsSteps(node, transform, transformStream) {
4654
+ yield;
4635
4655
  switch (node.kind) {
4636
4656
  case "string":
4637
- return {
4638
- kind: "string",
4639
- format: "hex",
4640
- bytes: transform(node.bytes)
4641
- };
4642
- case "array":
4643
- return {
4644
- kind: "array",
4645
- items: node.items.map((item) => transformNodeStringsAndStreams(item, transform, transformStream))
4646
- };
4657
+ return { kind: "string", format: "hex", bytes: transform(node.bytes) };
4658
+ case "array": {
4659
+ const items = [];
4660
+ for (const item of node.items) items.push(yield* transformNodeStringsAndStreamsSteps(item, transform, transformStream));
4661
+ return { kind: "array", items };
4662
+ }
4647
4663
  case "dict": {
4648
- const type = dictGet(node, "Type");
4649
- const fieldType = dictGet(node, "FT");
4650
- const byteRange = dictGet(node, "ByteRange");
4664
+ const type = dictGet(node, "Type"), fieldType = dictGet(node, "FT"), byteRange = dictGet(node, "ByteRange");
4651
4665
  const signature = type?.kind === "name" && type.decoded === "Sig" || fieldType?.kind === "name" && fieldType.decoded === "Sig" || byteRange?.kind === "array" && dictGet(node, "Filter")?.kind === "name";
4652
- return {
4653
- kind: "dict",
4654
- entries: node.entries.map((e) => ({
4655
- key: e.key,
4656
- value: signature && e.key.decoded === "Contents" ? e.value : transformNodeStringsAndStreams(e.value, transform, transformStream)
4657
- }))
4658
- };
4666
+ const entries = [];
4667
+ 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) });
4668
+ return { kind: "dict", entries };
4659
4669
  }
4660
4670
  case "stream": {
4661
4671
  const type = dictGet(node.dict, "Type");
4662
4672
  if (type?.kind === "name" && type.decoded === "XRef") return node;
4663
4673
  const stream = transformStream ? transformStream(node) : { ...node, rawBytes: transform(node.rawBytes) };
4664
- const updatedDict = {
4665
- kind: "dict",
4666
- entries: stream.dict.entries.map((e) => ({
4667
- key: e.key,
4668
- value: e.key.decoded === "Length" ? cosNumber(stream.rawBytes.length) : transformNodeStringsAndStreams(e.value, transform, transformStream)
4669
- }))
4670
- };
4671
- return {
4672
- kind: "stream",
4673
- dict: updatedDict,
4674
- rawBytes: stream.rawBytes
4675
- };
4674
+ const entries = [];
4675
+ 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) });
4676
+ return { kind: "stream", dict: { kind: "dict", entries }, rawBytes: stream.rawBytes };
4676
4677
  }
4677
4678
  default:
4678
4679
  return node;
@@ -4741,7 +4742,8 @@ function createR6Encryption(userPassword, ownerPassword, pMask) {
4741
4742
  StrF: cosName("StdCF")
4742
4743
  }) };
4743
4744
  }
4744
- function encryptCosDocument(doc, options = {}) {
4745
+ function* encryptCosDocumentSteps(doc, options = {}) {
4746
+ yield;
4745
4747
  const userPassword = options.userPassword ?? "";
4746
4748
  const ownerPassword = options.ownerPassword ?? userPassword;
4747
4749
  const revision = options.revision ?? 6;
@@ -4763,14 +4765,15 @@ function encryptCosDocument(doc, options = {}) {
4763
4765
  const encryptObjNum = maxObjNum + 1;
4764
4766
  const encryptedObjects = [];
4765
4767
  for (const obj of doc.objects.values()) {
4766
- const transformed = transformNodeStringsAndStreams(
4768
+ yield;
4769
+ const transformed = yield* transformNodeStringsAndStreamsSteps(
4767
4770
  obj.value,
4768
4771
  (plain) => encryptPdfBuffer(state, obj.objectNumber, obj.generationNumber, plain)
4769
4772
  );
4770
4773
  encryptedObjects.push({ objectNumber: obj.objectNumber, generationNumber: obj.generationNumber, value: transformed });
4771
4774
  }
4772
4775
  encryptedObjects.push({ objectNumber: encryptObjNum, generationNumber: 0, value: encryptDict });
4773
- return serializeCosDocument({
4776
+ return yield* serializeCosDocumentSteps({
4774
4777
  objects: encryptedObjects,
4775
4778
  rootRef: doc.rootRef,
4776
4779
  infoRef: doc.infoRef,
@@ -4820,17 +4823,19 @@ function decryptStream(doc, stream, transform) {
4820
4823
  }
4821
4824
  return { kind: "stream", dict, rawBytes: bytes };
4822
4825
  }
4823
- function decryptCosDocument(doc, state) {
4826
+ function* decryptCosDocumentSteps(doc, state) {
4827
+ yield;
4824
4828
  const encryptObjNum = doc.encryptRef?.objectNumber;
4825
4829
  const handler = securityHandlers.get(state);
4826
4830
  for (const [num, obj] of doc.objects.entries()) {
4831
+ yield;
4827
4832
  if (num === encryptObjNum || handler?.plaintextObjects.has(num)) continue;
4828
4833
  if (obj.value.kind === "stream") {
4829
4834
  const type = doc.resolve(dictGet(obj.value.dict, "Type"));
4830
4835
  if (type?.kind === "name" && (type.decoded === "XRef" || type.decoded === "Metadata" && !state.encryptMetadata)) continue;
4831
4836
  }
4832
4837
  const transform = handler?.factory.createCipherTransform(obj.objectNumber, obj.generationNumber);
4833
- const decrypted = transformNodeStringsAndStreams(
4838
+ const decrypted = yield* transformNodeStringsAndStreamsSteps(
4834
4839
  obj.value,
4835
4840
  (cipher) => decryptPdfBuffer(state, obj.objectNumber, obj.generationNumber, cipher),
4836
4841
  transform ? (stream) => decryptStream(doc, stream, transform) : void 0
@@ -4838,9 +4843,12 @@ function decryptCosDocument(doc, state) {
4838
4843
  doc.objects.set(num, { objectNumber: obj.objectNumber, generationNumber: obj.generationNumber, value: decrypted, span: obj.span });
4839
4844
  }
4840
4845
  }
4841
- function parseNodeFromLexer(lexer, bytes, maxDepth, repair = false) {
4846
+ function* parseNodeFromLexerSteps(lexer, bytes, maxDepth, repair = false) {
4847
+ yield;
4848
+ let work = 0;
4842
4849
  const stack = [];
4843
4850
  while (true) {
4851
+ if (++work % 16 === 0) yield;
4844
4852
  const tok = lexer.nextToken();
4845
4853
  const parent = stack.at(-1);
4846
4854
  if (!tok) {
@@ -5034,7 +5042,8 @@ function parseHeaderVersion(bytes) {
5034
5042
  }
5035
5043
  return head.slice(idx + 5, idx + 8);
5036
5044
  }
5037
- function parseObjectAtOffset(bytes, offset, maxRecursionDepth, repair = false) {
5045
+ function* parseObjectAtOffsetSteps(bytes, offset, maxRecursionDepth, repair = false) {
5046
+ yield;
5038
5047
  const lexer = new CosByteLexer(bytes, offset);
5039
5048
  const objNumTok = lexer.nextToken();
5040
5049
  const genNumTok = lexer.nextToken();
@@ -5042,7 +5051,7 @@ function parseObjectAtOffset(bytes, offset, maxRecursionDepth, repair = false) {
5042
5051
  if (objNumTok?.kind !== "number" || genNumTok?.kind !== "number" || objKwTok?.kind !== "keyword" || objKwTok.value !== "obj") {
5043
5052
  throw new PdfError("E_PARSE", `Malformed indirect object header at byte offset ${offset}`);
5044
5053
  }
5045
- const value = parseNodeFromLexer(lexer, bytes, maxRecursionDepth, repair);
5054
+ const value = yield* parseNodeFromLexerSteps(lexer, bytes, maxRecursionDepth, repair);
5046
5055
  if (!value) {
5047
5056
  throw new PdfError("E_PARSE", `Empty indirect object ${objNumTok.value} at offset ${offset}`);
5048
5057
  }
@@ -5053,7 +5062,9 @@ function parseObjectAtOffset(bytes, offset, maxRecursionDepth, repair = false) {
5053
5062
  span: { start: objNumTok.span.start, end: lexer.offset }
5054
5063
  };
5055
5064
  }
5056
- function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects) {
5065
+ function* parseXrefRevisionAtSteps(bytes, xrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects) {
5066
+ yield;
5067
+ let work = 0;
5057
5068
  if (xrefOffset < 0 || xrefOffset >= bytes.length) {
5058
5069
  throw new PdfError("E_PARSE", `Invalid xref offset: ${xrefOffset}`);
5059
5070
  }
@@ -5065,6 +5076,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
5065
5076
  if (firstTok.kind === "keyword" && firstTok.value === "xref") {
5066
5077
  const entries2 = /* @__PURE__ */ new Map();
5067
5078
  while (true) {
5079
+ if (++work % 16 === 0) yield;
5068
5080
  const saved = lexer.offset;
5069
5081
  const tok = lexer.nextToken();
5070
5082
  if (!tok) {
@@ -5088,6 +5100,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
5088
5100
  }
5089
5101
  if (count > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
5090
5102
  for (let i = 0; i < count; i++) {
5103
+ if (++work % 16 === 0) yield;
5091
5104
  const offTok = lexer.nextToken();
5092
5105
  const genTok = lexer.nextToken();
5093
5106
  const flagTok = lexer.nextToken();
@@ -5113,7 +5126,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
5113
5126
  if (entries2.size > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
5114
5127
  }
5115
5128
  }
5116
- const trailerNode = parseNodeFromLexer(lexer, bytes, maxRecursionDepth);
5129
+ const trailerNode = yield* parseNodeFromLexerSteps(lexer, bytes, maxRecursionDepth);
5117
5130
  if (!trailerNode || trailerNode.kind !== "dict") {
5118
5131
  throw new PdfError("E_PARSE", "Missing trailer dictionary after xref table");
5119
5132
  }
@@ -5125,7 +5138,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
5125
5138
  previousXrefOffset: prevNode2?.kind === "number" ? prevNode2.value : void 0
5126
5139
  };
5127
5140
  }
5128
- const xrefObj = parseObjectAtOffset(bytes, xrefOffset, maxRecursionDepth);
5141
+ const xrefObj = yield* parseObjectAtOffsetSteps(bytes, xrefOffset, maxRecursionDepth);
5129
5142
  if (xrefObj.value.kind !== "stream") {
5130
5143
  throw new PdfError("E_PARSE", `Expected xref table or XRef stream at offset ${xrefOffset}`);
5131
5144
  }
@@ -5146,6 +5159,7 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
5146
5159
  if (indexArray) {
5147
5160
  if (indexArray.kind !== "array" || indexArray.items.length % 2 !== 0) throw new PdfError("E_PARSE", "Invalid XRef range fields");
5148
5161
  for (let i = 0; i < indexArray.items.length; i += 2) {
5162
+ if (++work % 16 === 0) yield;
5149
5163
  const first = indexArray.items[i];
5150
5164
  const count = indexArray.items[i + 1];
5151
5165
  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)) {
@@ -5168,9 +5182,11 @@ function parseXrefRevisionAt(bytes, xrefOffset, maxDecompressedBytes, maxRecursi
5168
5182
  return v;
5169
5183
  };
5170
5184
  for (const [startObj, count] of subsections) {
5185
+ if (++work % 16 === 0) yield;
5171
5186
  if (count > (decoded.length - pos) / stride) throw new PdfError("E_PARSE", "Truncated XRef stream");
5172
5187
  if (count > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
5173
5188
  for (let i = 0; i < count; i++) {
5189
+ if (++work % 16 === 0) yield;
5174
5190
  const objNum = startObj + i;
5175
5191
  const field0 = w0 > 0 ? readInt(w0) : 1;
5176
5192
  const field1 = readInt(w1);
@@ -5216,12 +5232,16 @@ function isAsciiWhitespace(ch3) {
5216
5232
  function isAsciiDigit(ch3) {
5217
5233
  return ch3 >= 48 && ch3 <= 57;
5218
5234
  }
5219
- function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
5235
+ function* repairScanCosDocumentSteps(bytes, maxObjects, maxRecursionDepth) {
5236
+ yield;
5237
+ let work = 0;
5220
5238
  const objects = /* @__PURE__ */ new Map();
5221
5239
  const trailers = [];
5222
5240
  let pos = 0;
5223
5241
  while (pos < bytes.length) {
5242
+ if (++work % 16 === 0) yield;
5224
5243
  while (pos < bytes.length && !isAsciiDigit(bytes[pos])) {
5244
+ if (++work % 16 === 0) yield;
5225
5245
  if (bytes[pos] === 37) {
5226
5246
  while (pos < bytes.length && bytes[pos] !== 10 && bytes[pos] !== 13) pos++;
5227
5247
  continue;
@@ -5231,7 +5251,7 @@ function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
5231
5251
  try {
5232
5252
  const token = lexer.nextToken();
5233
5253
  if (token?.kind === "keyword" && token.value === "trailer") {
5234
- const trailer = parseNodeFromLexer(lexer, bytes, maxRecursionDepth, true);
5254
+ const trailer = yield* parseNodeFromLexerSteps(lexer, bytes, maxRecursionDepth, true);
5235
5255
  if (trailer?.kind === "dict") trailers.push(trailer);
5236
5256
  pos = lexer.offset;
5237
5257
  continue;
@@ -5264,7 +5284,7 @@ function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
5264
5284
  bytes[pos + 3] === 40)) {
5265
5285
  pos += 3;
5266
5286
  try {
5267
- const parsed = parseObjectAtOffset(bytes, headerStart, maxRecursionDepth, true);
5287
+ const parsed = yield* parseObjectAtOffsetSteps(bytes, headerStart, maxRecursionDepth, true);
5268
5288
  objects.set(parsed.objectNumber, parsed);
5269
5289
  if (parsed.value.kind === "stream") {
5270
5290
  const type = dictGet(parsed.value.dict, "Type");
@@ -5281,13 +5301,17 @@ function repairScanCosDocument(bytes, maxObjects, maxRecursionDepth) {
5281
5301
  }
5282
5302
  return { objects, trailers };
5283
5303
  }
5284
- function unpackRecoveredObjects(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth) {
5304
+ function* unpackRecoveredObjectsSteps(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth) {
5305
+ yield;
5306
+ let work = 0;
5285
5307
  for (const obj of [...objects.values()]) {
5308
+ if (++work % 16 === 0) yield;
5286
5309
  if (obj.value.kind === "stream") {
5287
5310
  const t = dictGet(obj.value.dict, "Type");
5288
5311
  if (t?.kind === "name" && t.decoded === "ObjStm") {
5289
5312
  try {
5290
- for (const [unpackedNum, unpackedVal] of unpackObjectStream(obj.value, maxDecompressedBytes, maxRecursionDepth, true).entries()) {
5313
+ for (const [unpackedNum, unpackedVal] of (yield* unpackObjectStreamSteps(obj.value, maxDecompressedBytes, maxRecursionDepth, true)).entries()) {
5314
+ if (++work % 16 === 0) yield;
5291
5315
  if (!objects.has(unpackedNum)) {
5292
5316
  objects.set(unpackedNum, {
5293
5317
  objectNumber: unpackedNum,
@@ -5323,7 +5347,9 @@ function recoveredReferences(objects) {
5323
5347
  }
5324
5348
  return { ...rootRef ? { rootRef } : {}, ...infoRef ? { infoRef } : {} };
5325
5349
  }
5326
- function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth, repair = false) {
5350
+ function* unpackObjectStreamSteps(streamObj, maxDecompressedBytes, maxRecursionDepth, repair = false) {
5351
+ yield;
5352
+ let work = 0;
5327
5353
  const nNode = dictGet(streamObj.dict, "N");
5328
5354
  const firstNode = dictGet(streamObj.dict, "First");
5329
5355
  if (nNode?.kind !== "number" || firstNode?.kind !== "number") {
@@ -5333,6 +5359,7 @@ function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth,
5333
5359
  const headerLexer = new CosByteLexer(decoded, 0);
5334
5360
  const pairs = [];
5335
5361
  for (let i = 0; i < nNode.value; i++) {
5362
+ if (++work % 16 === 0) yield;
5336
5363
  const numTok = headerLexer.nextToken();
5337
5364
  const offTok = headerLexer.nextToken();
5338
5365
  if (numTok?.kind === "number" && offTok?.kind === "number") {
@@ -5341,9 +5368,10 @@ function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth,
5341
5368
  }
5342
5369
  const result = /* @__PURE__ */ new Map();
5343
5370
  for (const pair of pairs) {
5371
+ if (++work % 16 === 0) yield;
5344
5372
  try {
5345
5373
  const valLexer = new CosByteLexer(decoded, firstNode.value + pair.relativeOffset);
5346
- const node = parseNodeFromLexer(valLexer, decoded, maxRecursionDepth, repair);
5374
+ const node = yield* parseNodeFromLexerSteps(valLexer, decoded, maxRecursionDepth, repair);
5347
5375
  if (node) {
5348
5376
  result.set(pair.objectNumber, node);
5349
5377
  }
@@ -5353,7 +5381,9 @@ function unpackObjectStream(streamObj, maxDecompressedBytes, maxRecursionDepth,
5353
5381
  }
5354
5382
  return result;
5355
5383
  }
5356
- function parseCosDocument(bytes, options = {}) {
5384
+ function* parseCosDocumentSteps(bytes, options = {}) {
5385
+ yield;
5386
+ let work = 0;
5357
5387
  const version = parseHeaderVersion(bytes);
5358
5388
  const recovery = options.recovery ?? "strict";
5359
5389
  const maxObjects = options.maxObjects ?? Infinity;
@@ -5388,10 +5418,12 @@ function parseCosDocument(bytes, options = {}) {
5388
5418
  let currentXrefOffset = offsetTok.value;
5389
5419
  const visitedOffsets = /* @__PURE__ */ new Set();
5390
5420
  while (currentXrefOffset !== void 0 && !visitedOffsets.has(currentXrefOffset)) {
5421
+ if (++work % 16 === 0) yield;
5391
5422
  visitedOffsets.add(currentXrefOffset);
5392
- const rev = parseXrefRevisionAt(bytes, currentXrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects);
5423
+ const rev = yield* parseXrefRevisionAtSteps(bytes, currentXrefOffset, maxDecompressedBytes, maxRecursionDepth, maxObjects);
5393
5424
  revisions.push(rev);
5394
5425
  for (const [num, entry] of rev.entries.entries()) {
5426
+ if (++work % 16 === 0) yield;
5395
5427
  if (!mergedXref.has(num)) {
5396
5428
  mergedXref.set(num, entry);
5397
5429
  if (mergedXref.size > maxObjects + 1) throw new PdfError("E_LIMIT", "PDF object count limit exceeded while reading xref entries");
@@ -5424,8 +5456,9 @@ function parseCosDocument(bytes, options = {}) {
5424
5456
  if (!useRepair && rootRef) {
5425
5457
  try {
5426
5458
  for (const [objNum, entry] of mergedXref.entries()) {
5459
+ if (++work % 16 === 0) yield;
5427
5460
  if (entry.type !== "uncompressed") continue;
5428
- const parsed = parseObjectAtOffset(bytes, entry.offset ?? 0, maxRecursionDepth, recovery === "repair");
5461
+ const parsed = yield* parseObjectAtOffsetSteps(bytes, entry.offset ?? 0, maxRecursionDepth, recovery === "repair");
5429
5462
  if (parsed.objectNumber !== objNum || parsed.generationNumber !== (entry.generationNumber ?? 0)) {
5430
5463
  throw new PdfError("E_PARSE", `Bad uncompressed XRef entry for object ${objNum}`);
5431
5464
  }
@@ -5443,7 +5476,7 @@ function parseCosDocument(bytes, options = {}) {
5443
5476
  throw new PdfError("E_PARSE", "PDF trailer missing /Root reference");
5444
5477
  }
5445
5478
  useRepair = true;
5446
- const scanned = repairScanCosDocument(bytes, maxObjects, maxRecursionDepth);
5479
+ const scanned = yield* repairScanCosDocumentSteps(bytes, maxObjects, maxRecursionDepth);
5447
5480
  objects = scanned.objects;
5448
5481
  revisions.length = 0;
5449
5482
  mergedXref.clear();
@@ -5478,7 +5511,7 @@ function parseCosDocument(bytes, options = {}) {
5478
5511
  encryptNode = encrypt?.kind === "ref" || encrypt?.kind === "dict" ? encrypt : void 0;
5479
5512
  encryptRef = encrypt?.kind === "ref" ? encrypt : void 0;
5480
5513
  idArray = id?.kind === "array" ? id : void 0;
5481
- if (!rootRef && !encryptNode) unpackRecoveredObjects(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
5514
+ if (!rootRef && !encryptNode) yield* unpackRecoveredObjectsSteps(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
5482
5515
  const fallback = recoveredReferences(objects);
5483
5516
  rootRef ??= fallback.rootRef;
5484
5517
  infoRef ??= fallback.infoRef;
@@ -5513,20 +5546,21 @@ function parseCosDocument(bytes, options = {}) {
5513
5546
  maxRecursionDepth
5514
5547
  });
5515
5548
  doc.encryption = encState;
5516
- decryptCosDocument(doc, encState);
5549
+ yield* decryptCosDocumentSteps(doc, encState);
5517
5550
  }
5518
5551
  if (useRepair) {
5519
- unpackRecoveredObjects(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
5552
+ yield* unpackRecoveredObjectsSteps(objects, maxObjects, maxDecompressedBytes, maxRecursionDepth);
5520
5553
  doc.infoRef ??= recoveredReferences(objects).infoRef;
5521
5554
  }
5522
5555
  const objStmCache = /* @__PURE__ */ new Map();
5523
5556
  for (const [objNum, entry] of mergedXref.entries()) {
5557
+ if (++work % 16 === 0) yield;
5524
5558
  if (entry.type === "compressed" && entry.objectStreamNumber !== void 0) {
5525
5559
  let unpacked = objStmCache.get(entry.objectStreamNumber);
5526
5560
  if (!unpacked) {
5527
5561
  const stmObj = objects.get(entry.objectStreamNumber)?.value;
5528
5562
  if (stmObj?.kind === "stream") {
5529
- unpacked = unpackObjectStream(stmObj, maxDecompressedBytes, maxRecursionDepth, recovery === "repair");
5563
+ unpacked = yield* unpackObjectStreamSteps(stmObj, maxDecompressedBytes, maxRecursionDepth, recovery === "repair");
5530
5564
  objStmCache.set(entry.objectStreamNumber, unpacked);
5531
5565
  }
5532
5566
  }
@@ -5543,6 +5577,9 @@ function parseCosDocument(bytes, options = {}) {
5543
5577
  }
5544
5578
  return doc;
5545
5579
  }
5580
+ function parseCosDocument(bytes, options = {}) {
5581
+ return drainWork(parseCosDocumentSteps(bytes, options));
5582
+ }
5546
5583
  function isAsciiHexDigit(ch3) {
5547
5584
  return ch3 >= 48 && ch3 <= 57 || ch3 >= 65 && ch3 <= 70 || ch3 >= 97 && ch3 <= 102;
5548
5585
  }
@@ -13345,7 +13382,8 @@ function resolveInheritedFieldEntry(doc, dict, key) {
13345
13382
  }
13346
13383
  return void 0;
13347
13384
  }
13348
- function collectFieldDicts(doc, node, prefix = "", out = [], parentFt = "", parentFf = 0, parentQ = 0, parentMaxLen) {
13385
+ function* collectFieldDictsSteps(doc, node, prefix = "", out = [], parentFt = "", parentFf = 0, parentQ = 0, parentMaxLen, yieldState = { work: 0 }) {
13386
+ if (++yieldState.work % 256 === 0) yield;
13349
13387
  const dict = doc.resolveDict(node);
13350
13388
  if (!dict) return out;
13351
13389
  const tNode = doc.resolve(dictGet(dict, "T"));
@@ -13378,7 +13416,8 @@ function collectFieldDicts(doc, node, prefix = "", out = [], parentFt = "", pare
13378
13416
  return out;
13379
13417
  }
13380
13418
  for (const kid of kidsArr.items) {
13381
- collectFieldDicts(doc, kid, fullName, out, currentFt, currentFf, currentQ, currentMaxLen);
13419
+ if (++yieldState.work % 16 === 0) yield;
13420
+ yield* collectFieldDictsSteps(doc, kid, fullName, out, currentFt, currentFf, currentQ, currentMaxLen, yieldState);
13382
13421
  }
13383
13422
  } else if (fullName) {
13384
13423
  out.push({
@@ -13803,6 +13842,9 @@ function escapePdfLiteralString(str) {
13803
13842
  }
13804
13843
  return out;
13805
13844
  }
13845
+ function collectFieldDicts(doc, node, prefix = "", out = [], parentFt = "", parentFf = 0, parentQ = 0, parentMaxLen) {
13846
+ drainWork(collectFieldDictsSteps(doc, node, prefix, out, parentFt, parentFf, parentQ, parentMaxLen));
13847
+ }
13806
13848
  function createStandardFontHandle(standardName) {
13807
13849
  return {
13808
13850
  kind: "standard14",
@@ -44917,14 +44959,18 @@ var init_dist = __esm({
44917
44959
  });
44918
44960
  return new _PdfDocument(parseCosDocument(rawBytes));
44919
44961
  }
44920
- static load(bytes, options) {
44962
+ static *loadSteps(bytes, options) {
44963
+ yield;
44921
44964
  return new _PdfDocument(
44922
- parseCosDocument(bytes, {
44965
+ yield* parseCosDocumentSteps(bytes, {
44923
44966
  ...options,
44924
44967
  recovery: options?.recovery ?? "repair"
44925
44968
  })
44926
44969
  );
44927
44970
  }
44971
+ static load(bytes, options) {
44972
+ return drainWork(_PdfDocument.loadSteps(bytes, options));
44973
+ }
44928
44974
  rebuildPagesList() {
44929
44975
  const catalog = this.cos.resolveDict(this.cos.rootRef);
44930
44976
  const pagesNode = catalog ? dictGet(catalog, "Pages") : void 0;
@@ -45006,20 +45052,30 @@ var init_dist = __esm({
45006
45052
  this.pages.splice(index, 1);
45007
45053
  this.syncPageTree();
45008
45054
  }
45009
- copyPagesFrom(sourceDoc, indices) {
45055
+ *copyPagesFromSteps(sourceDoc, indices) {
45056
+ function* cloneItems(items) {
45057
+ const result = [];
45058
+ for (const item of items) result.push(yield* cloneNode(item));
45059
+ return result;
45060
+ }
45061
+ yield;
45062
+ let work = 0;
45010
45063
  const memo = /* @__PURE__ */ new Map();
45011
45064
  const sourcePageObjNums = /* @__PURE__ */ new Set();
45012
45065
  for (let i = 0; i < sourceDoc.getPageCount(); i++) {
45066
+ if (++work % 16 === 0) yield;
45013
45067
  sourcePageObjNums.add(sourceDoc.getPage(i).ref.objectNumber);
45014
45068
  }
45015
45069
  const targetPageRefs = [];
45016
45070
  for (const idx of indices) {
45071
+ if (++work % 16 === 0) yield;
45017
45072
  const srcPage = sourceDoc.getPage(idx);
45018
45073
  const pageRef = this.cos.allocateObject({ kind: "null" });
45019
45074
  targetPageRefs.push(pageRef);
45020
45075
  memo.set(srcPage.ref.objectNumber, pageRef);
45021
45076
  }
45022
- const cloneNode = (node) => {
45077
+ const cloneNode = function* (node) {
45078
+ if (++work % 16 === 0) yield;
45023
45079
  if (node.kind === "ref") {
45024
45080
  const existing = memo.get(node.objectNumber);
45025
45081
  if (existing) return existing;
@@ -45030,12 +45086,12 @@ var init_dist = __esm({
45030
45086
  if (!target) return cosRef(0);
45031
45087
  const placeholderRef = this.cos.allocateObject({ kind: "null" });
45032
45088
  memo.set(node.objectNumber, placeholderRef);
45033
- const clonedTarget = cloneNode(target);
45089
+ const clonedTarget = yield* cloneNode(target);
45034
45090
  this.cos.setObject(placeholderRef.objectNumber, clonedTarget, 0);
45035
45091
  return placeholderRef;
45036
45092
  }
45037
45093
  if (node.kind === "array") {
45038
- return cosArray(node.items.map(cloneNode));
45094
+ return cosArray(yield* cloneItems(node.items));
45039
45095
  }
45040
45096
  if (node.kind === "dict") {
45041
45097
  const typeEntry = dictGet(node, "Type");
@@ -45045,13 +45101,13 @@ var init_dist = __esm({
45045
45101
  if (isPageTreeNode && entry.key.decoded === "Parent") continue;
45046
45102
  newEntries.push({
45047
45103
  key: { ...entry.key },
45048
- value: cloneNode(entry.value)
45104
+ value: yield* cloneNode(entry.value)
45049
45105
  });
45050
45106
  }
45051
45107
  return { kind: "dict", entries: newEntries };
45052
45108
  }
45053
45109
  if (node.kind === "stream") {
45054
- const clonedDict = cloneNode(node.dict);
45110
+ const clonedDict = yield* cloneNode(node.dict);
45055
45111
  return {
45056
45112
  kind: "stream",
45057
45113
  dict: clonedDict,
@@ -45060,11 +45116,12 @@ var init_dist = __esm({
45060
45116
  };
45061
45117
  }
45062
45118
  return node;
45063
- };
45119
+ }.bind(this);
45064
45120
  const copiedPages = [];
45065
45121
  for (const [position, idx] of indices.entries()) {
45122
+ if (++work % 16 === 0) yield;
45066
45123
  const srcPage = sourceDoc.getPage(idx);
45067
- const clonedPageDict = cloneNode(srcPage.pageDict);
45124
+ const clonedPageDict = yield* cloneNode(srcPage.pageDict);
45068
45125
  const size = srcPage.getSize();
45069
45126
  if (!dictGet(clonedPageDict, "MediaBox")) {
45070
45127
  dictSet(
@@ -45074,13 +45131,14 @@ var init_dist = __esm({
45074
45131
  );
45075
45132
  }
45076
45133
  if (!dictGet(clonedPageDict, "Resources")) {
45077
- dictSet(clonedPageDict, "Resources", cloneNode(srcPage.getResourcesDict()));
45134
+ dictSet(clonedPageDict, "Resources", yield* cloneNode(srcPage.getResourcesDict()));
45078
45135
  }
45079
45136
  if (!dictGet(clonedPageDict, "Rotate") && srcPage.getRotation() !== 0) {
45080
45137
  dictSet(clonedPageDict, "Rotate", cosNumber(srcPage.getRotation()));
45081
45138
  }
45082
45139
  const inheritedBoxKeys = ["CropBox", "BleedBox", "TrimBox", "ArtBox"];
45083
45140
  for (const boxKey of inheritedBoxKeys) {
45141
+ if (++work % 16 === 0) yield;
45084
45142
  if (!dictGet(clonedPageDict, boxKey)) {
45085
45143
  const boxVal = boxKey === "CropBox" ? srcPage.getCropBox() : boxKey === "BleedBox" ? srcPage.getBleedBox() : boxKey === "TrimBox" ? srcPage.getTrimBox() : srcPage.getArtBox();
45086
45144
  if (boxVal[0] !== 0 || boxVal[1] !== 0 || boxVal[2] !== size.width || boxVal[3] !== size.height) {
@@ -45122,8 +45180,9 @@ var init_dist = __esm({
45122
45180
  };
45123
45181
  const clonedRootFieldRefs = [];
45124
45182
  for (const item of srcFieldsArr.items) {
45183
+ if (++work % 16 === 0) yield;
45125
45184
  if (!hasClonedDescendant(item)) continue;
45126
- const cloned = cloneNode(item);
45185
+ const cloned = yield* cloneNode(item);
45127
45186
  if (cloned.kind === "ref") {
45128
45187
  const clonedDict = this.cos.resolveDict(cloned);
45129
45188
  const kidsArr = clonedDict ? this.cos.resolveArray(dictGet(clonedDict, "Kids")) : void 0;
@@ -45157,6 +45216,7 @@ var init_dist = __esm({
45157
45216
  dstFields.items.filter((x) => x.kind === "ref").map((x) => x.objectNumber)
45158
45217
  );
45159
45218
  for (const fRef of clonedRootFieldRefs) {
45219
+ if (++work % 16 === 0) yield;
45160
45220
  if (!existingNums.has(fRef.objectNumber)) {
45161
45221
  dstFields.items.push(fRef);
45162
45222
  existingNums.add(fRef.objectNumber);
@@ -45164,11 +45224,11 @@ var init_dist = __esm({
45164
45224
  }
45165
45225
  const srcDr = dictGet(srcAcroForm, "DR");
45166
45226
  if (srcDr && !dictGet(dstAcroForm, "DR")) {
45167
- dictSet(dstAcroForm, "DR", cloneNode(srcDr));
45227
+ dictSet(dstAcroForm, "DR", yield* cloneNode(srcDr));
45168
45228
  }
45169
45229
  const srcDa = dictGet(srcAcroForm, "DA");
45170
45230
  if (srcDa && !dictGet(dstAcroForm, "DA")) {
45171
- dictSet(dstAcroForm, "DA", cloneNode(srcDa));
45231
+ dictSet(dstAcroForm, "DA", yield* cloneNode(srcDa));
45172
45232
  }
45173
45233
  }
45174
45234
  }
@@ -45177,12 +45237,15 @@ var init_dist = __esm({
45177
45237
  if (srcOcProps) {
45178
45238
  const dstCatalog = this.cos.resolveDict(this.cos.rootRef);
45179
45239
  if (dstCatalog && !dictGet(dstCatalog, "OCProperties")) {
45180
- dictSet(dstCatalog, "OCProperties", cloneNode(srcOcProps));
45240
+ dictSet(dstCatalog, "OCProperties", yield* cloneNode(srcOcProps));
45181
45241
  }
45182
45242
  }
45183
45243
  this.syncPageTree();
45184
45244
  return copiedPages;
45185
45245
  }
45246
+ copyPagesFrom(sourceDoc, indices) {
45247
+ return drainWork(this.copyPagesFromSteps(sourceDoc, indices));
45248
+ }
45186
45249
  embedStandardFont(fontName) {
45187
45250
  return createStandardFontHandle(fontName);
45188
45251
  }
@@ -45382,14 +45445,15 @@ var init_dist = __esm({
45382
45445
  });
45383
45446
  return buildSemanticAstFromPages(extractedPages, tablesByPage, displayLists);
45384
45447
  }
45385
- save(options = {}) {
45448
+ *saveSteps(options = {}) {
45449
+ yield;
45386
45450
  this.syncPageTree();
45387
45451
  if (this.cos.requiresFullRewrite) {
45388
45452
  pruneUnusedXObjects(this.cos, this.pages);
45389
45453
  discardUnreachableObjects(this.cos);
45390
45454
  }
45391
45455
  if (options.encrypt) {
45392
- return encryptCosDocument(this.cos, options.encrypt);
45456
+ return yield* encryptCosDocumentSteps(this.cos, options.encrypt);
45393
45457
  }
45394
45458
  const allObjects = [...this.cos.objects.values()].sort(
45395
45459
  (a, b) => a.objectNumber - b.objectNumber
@@ -45397,7 +45461,7 @@ var init_dist = __esm({
45397
45461
  if (options.incremental && !this.cos.requiresFullRewrite && this.cos.bytes.length > 0) {
45398
45462
  return appendIncrementalRevision(this.cos.bytes, this.cos, allObjects);
45399
45463
  }
45400
- return serializeCosDocument({
45464
+ return yield* serializeCosDocumentSteps({
45401
45465
  objects: allObjects,
45402
45466
  rootRef: this.cos.rootRef,
45403
45467
  infoRef: this.cos.infoRef,
@@ -45408,6 +45472,9 @@ var init_dist = __esm({
45408
45472
  linearize: options.linearize
45409
45473
  });
45410
45474
  }
45475
+ save(options = {}) {
45476
+ return drainWork(this.saveSteps(options));
45477
+ }
45411
45478
  };
45412
45479
  }
45413
45480
  });