llm-large-files 0.4.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/CHANGELOG.md +1400 -338
  2. package/README.md +217 -49
  3. package/dist/analyze.d.ts +5 -0
  4. package/dist/analyze.d.ts.map +1 -1
  5. package/dist/analyze.js +96 -47
  6. package/dist/analyze.js.map +1 -1
  7. package/dist/anthropic/adapter.d.ts +117 -0
  8. package/dist/anthropic/adapter.d.ts.map +1 -0
  9. package/dist/anthropic/adapter.js +542 -0
  10. package/dist/anthropic/adapter.js.map +1 -0
  11. package/dist/anthropic/index.d.ts +3 -0
  12. package/dist/anthropic/index.d.ts.map +1 -0
  13. package/dist/anthropic/index.js +2 -0
  14. package/dist/anthropic/index.js.map +1 -0
  15. package/dist/ask.d.ts +200 -0
  16. package/dist/ask.d.ts.map +1 -0
  17. package/dist/ask.js +1402 -0
  18. package/dist/ask.js.map +1 -0
  19. package/dist/budget.d.ts +23 -0
  20. package/dist/budget.d.ts.map +1 -0
  21. package/dist/budget.js +64 -0
  22. package/dist/budget.js.map +1 -0
  23. package/dist/encoding.d.ts +17 -0
  24. package/dist/encoding.d.ts.map +1 -0
  25. package/dist/encoding.js +852 -0
  26. package/dist/encoding.js.map +1 -0
  27. package/dist/errors.d.ts +4 -0
  28. package/dist/errors.d.ts.map +1 -1
  29. package/dist/errors.js +15 -0
  30. package/dist/errors.js.map +1 -1
  31. package/dist/events.d.ts +333 -0
  32. package/dist/events.d.ts.map +1 -0
  33. package/dist/events.js +159 -0
  34. package/dist/events.js.map +1 -0
  35. package/dist/evidence.d.ts +42 -0
  36. package/dist/evidence.d.ts.map +1 -0
  37. package/dist/evidence.js +138 -0
  38. package/dist/evidence.js.map +1 -0
  39. package/dist/extract.d.ts +35 -3
  40. package/dist/extract.d.ts.map +1 -1
  41. package/dist/extract.js +626 -58
  42. package/dist/extract.js.map +1 -1
  43. package/dist/gemini/adapter.d.ts +59 -0
  44. package/dist/gemini/adapter.d.ts.map +1 -0
  45. package/dist/gemini/adapter.js +226 -0
  46. package/dist/gemini/adapter.js.map +1 -0
  47. package/dist/gemini/chat.d.ts +53 -77
  48. package/dist/gemini/chat.d.ts.map +1 -1
  49. package/dist/gemini/chat.js +42 -300
  50. package/dist/gemini/chat.js.map +1 -1
  51. package/dist/gemini/errors.d.ts +16 -0
  52. package/dist/gemini/errors.d.ts.map +1 -0
  53. package/dist/gemini/errors.js +46 -0
  54. package/dist/gemini/errors.js.map +1 -0
  55. package/dist/gemini/generate.d.ts +18 -21
  56. package/dist/gemini/generate.d.ts.map +1 -1
  57. package/dist/gemini/generate.js +49 -92
  58. package/dist/gemini/generate.js.map +1 -1
  59. package/dist/gemini/index.d.ts +6 -4
  60. package/dist/gemini/index.d.ts.map +1 -1
  61. package/dist/gemini/index.js +4 -3
  62. package/dist/gemini/index.js.map +1 -1
  63. package/dist/gemini/sse.d.ts +1 -2
  64. package/dist/gemini/sse.d.ts.map +1 -1
  65. package/dist/gemini/sse.js +1 -38
  66. package/dist/gemini/sse.js.map +1 -1
  67. package/dist/http.d.ts +136 -0
  68. package/dist/http.d.ts.map +1 -0
  69. package/dist/http.js +401 -0
  70. package/dist/http.js.map +1 -0
  71. package/dist/index.d.ts +18 -2
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +13 -4
  74. package/dist/index.js.map +1 -1
  75. package/dist/library.d.ts +96 -8
  76. package/dist/library.d.ts.map +1 -1
  77. package/dist/library.js +414 -96
  78. package/dist/library.js.map +1 -1
  79. package/dist/map-reduce.d.ts +37 -1
  80. package/dist/map-reduce.d.ts.map +1 -1
  81. package/dist/map-reduce.js +255 -61
  82. package/dist/map-reduce.js.map +1 -1
  83. package/dist/model.d.ts +278 -0
  84. package/dist/model.d.ts.map +1 -0
  85. package/dist/model.js +222 -0
  86. package/dist/model.js.map +1 -0
  87. package/dist/node/handlers.d.ts +20 -4
  88. package/dist/node/handlers.d.ts.map +1 -1
  89. package/dist/node/handlers.js +122 -20
  90. package/dist/node/handlers.js.map +1 -1
  91. package/dist/node/index.d.ts +2 -0
  92. package/dist/node/index.d.ts.map +1 -1
  93. package/dist/node/index.js +1 -0
  94. package/dist/node/index.js.map +1 -1
  95. package/dist/node/sse.d.ts +59 -0
  96. package/dist/node/sse.d.ts.map +1 -0
  97. package/dist/node/sse.js +174 -0
  98. package/dist/node/sse.js.map +1 -0
  99. package/dist/openai/adapter.d.ts +109 -0
  100. package/dist/openai/adapter.d.ts.map +1 -0
  101. package/dist/openai/adapter.js +567 -0
  102. package/dist/openai/adapter.js.map +1 -0
  103. package/dist/openai/index.d.ts +3 -0
  104. package/dist/openai/index.d.ts.map +1 -0
  105. package/dist/openai/index.js +2 -0
  106. package/dist/openai/index.js.map +1 -0
  107. package/dist/output-limit.d.ts +42 -0
  108. package/dist/output-limit.d.ts.map +1 -0
  109. package/dist/output-limit.js +121 -0
  110. package/dist/output-limit.js.map +1 -0
  111. package/dist/pause.d.ts +24 -0
  112. package/dist/pause.d.ts.map +1 -0
  113. package/dist/pause.js +38 -0
  114. package/dist/pause.js.map +1 -0
  115. package/dist/pdf-pages.d.ts +32 -2
  116. package/dist/pdf-pages.d.ts.map +1 -1
  117. package/dist/pdf-pages.js +275 -14
  118. package/dist/pdf-pages.js.map +1 -1
  119. package/dist/pdf-pictures.d.ts +30 -3
  120. package/dist/pdf-pictures.d.ts.map +1 -1
  121. package/dist/pdf-pictures.js +50 -8
  122. package/dist/pdf-pictures.js.map +1 -1
  123. package/dist/pdf-work.d.ts +40 -0
  124. package/dist/pdf-work.d.ts.map +1 -0
  125. package/dist/pdf-work.js +734 -0
  126. package/dist/pdf-work.js.map +1 -0
  127. package/dist/plan.d.ts +22 -4
  128. package/dist/plan.d.ts.map +1 -1
  129. package/dist/plan.js +61 -12
  130. package/dist/plan.js.map +1 -1
  131. package/dist/retry.d.ts +76 -0
  132. package/dist/retry.d.ts.map +1 -0
  133. package/dist/retry.js +141 -0
  134. package/dist/retry.js.map +1 -0
  135. package/dist/search.d.ts +8 -2
  136. package/dist/search.d.ts.map +1 -1
  137. package/dist/search.js +60 -10
  138. package/dist/search.js.map +1 -1
  139. package/dist/sse.d.ts +36 -0
  140. package/dist/sse.d.ts.map +1 -0
  141. package/dist/sse.js +153 -0
  142. package/dist/sse.js.map +1 -0
  143. package/dist/stores.d.ts.map +1 -1
  144. package/dist/stores.js +5 -2
  145. package/dist/stores.js.map +1 -1
  146. package/dist/text-index.d.ts +6 -0
  147. package/dist/text-index.d.ts.map +1 -1
  148. package/dist/text-index.js +25 -8
  149. package/dist/text-index.js.map +1 -1
  150. package/dist/text.d.ts +62 -2
  151. package/dist/text.d.ts.map +1 -1
  152. package/dist/text.js +314 -14
  153. package/dist/text.js.map +1 -1
  154. package/dist/tools.d.ts +72 -1
  155. package/dist/tools.d.ts.map +1 -1
  156. package/dist/tools.js +341 -60
  157. package/dist/tools.js.map +1 -1
  158. package/dist/types.d.ts +128 -4
  159. package/dist/types.d.ts.map +1 -1
  160. package/dist/zip-directory.d.ts +43 -0
  161. package/dist/zip-directory.d.ts.map +1 -0
  162. package/dist/zip-directory.js +130 -0
  163. package/dist/zip-directory.js.map +1 -0
  164. package/docs/README.md +288 -0
  165. package/docs/api.md +58 -184
  166. package/docs/configuration.md +34 -490
  167. package/docs/getting-started.md +412 -177
  168. package/docs/guides/anthropic.md +590 -0
  169. package/docs/guides/conversations.md +629 -0
  170. package/docs/guides/costs-and-limits.md +726 -0
  171. package/docs/guides/deployment.md +654 -0
  172. package/docs/guides/errors-and-reliability.md +735 -0
  173. package/docs/guides/explorer-model.md +399 -0
  174. package/docs/guides/openai-compatible.md +602 -0
  175. package/docs/guides/own-adapter-and-loop.md +1169 -0
  176. package/docs/guides/pdfs.md +971 -0
  177. package/docs/guides/storage.md +731 -0
  178. package/docs/guides/testing.md +1199 -0
  179. package/docs/guides/vertex-and-gemini.md +717 -0
  180. package/docs/guides/web-app.md +1589 -0
  181. package/docs/how-it-works.md +1022 -138
  182. package/docs/measurements.md +881 -0
  183. package/docs/publishing.md +274 -46
  184. package/docs/reference/anthropic.md +427 -0
  185. package/docs/reference/ask.md +892 -0
  186. package/docs/reference/errors-and-retries.md +758 -0
  187. package/docs/reference/events.md +993 -0
  188. package/docs/reference/gemini.md +823 -0
  189. package/docs/reference/library.md +1467 -0
  190. package/docs/reference/node.md +528 -0
  191. package/docs/reference/openai.md +385 -0
  192. package/docs/reference/tools.md +883 -0
  193. package/docs/security.md +902 -90
  194. package/docs/troubleshooting.md +1390 -0
  195. package/package.json +22 -4
  196. package/src/analyze.ts +93 -42
  197. package/src/anthropic/adapter.ts +611 -0
  198. package/src/anthropic/index.ts +2 -0
  199. package/src/ask.ts +1560 -0
  200. package/src/budget.ts +71 -0
  201. package/src/encoding.ts +850 -0
  202. package/src/errors.ts +17 -0
  203. package/src/events.ts +442 -0
  204. package/src/evidence.ts +165 -0
  205. package/src/extract.ts +602 -62
  206. package/src/gemini/adapter.ts +271 -0
  207. package/src/gemini/chat.ts +87 -355
  208. package/src/gemini/errors.ts +48 -0
  209. package/src/gemini/generate.ts +67 -94
  210. package/src/gemini/index.ts +6 -4
  211. package/src/gemini/sse.ts +1 -34
  212. package/src/http.ts +434 -0
  213. package/src/index.ts +55 -4
  214. package/src/library.ts +497 -97
  215. package/src/map-reduce.ts +266 -59
  216. package/src/model.ts +433 -0
  217. package/src/node/handlers.ts +137 -22
  218. package/src/node/index.ts +2 -0
  219. package/src/node/sse.ts +198 -0
  220. package/src/openai/adapter.ts +613 -0
  221. package/src/openai/index.ts +2 -0
  222. package/src/output-limit.ts +135 -0
  223. package/src/pause.ts +41 -0
  224. package/src/pdf-pages.ts +268 -15
  225. package/src/pdf-pictures.ts +56 -9
  226. package/src/pdf-work.ts +722 -0
  227. package/src/plan.ts +68 -12
  228. package/src/retry.ts +176 -0
  229. package/src/search.ts +63 -10
  230. package/src/sse.ts +159 -0
  231. package/src/stores.ts +5 -2
  232. package/src/text-index.ts +26 -7
  233. package/src/text.ts +323 -13
  234. package/src/tools.ts +338 -62
  235. package/src/types.ts +129 -5
  236. package/src/zip-directory.ts +136 -0
@@ -0,0 +1,42 @@
1
+ /**
2
+ * A model that thinks counts its thoughts in its output limit. Asked for 4,096 tokens of notes on
3
+ * a part of a log, Gemini 3.8 Flash spent 3,931 of them thinking and wrote two lines, and Gemini 4
4
+ * wrote nothing at all (November 2026). A reply that was cut off that way is asked for again with
5
+ * this limit, the least Google recommends for a Gemini 4 model. OpenAI says to keep at least
6
+ * 25,000 for its reasoning models.
7
+ */
8
+ export declare const THINKING_OUTPUT_TOKENS = 32768;
9
+ /**
10
+ * Why a reply had no text, when this error is the one `completeWithinLimit` throws for it: the
11
+ * provider's word for why it stopped ("SAFETY", "refusal"), or "" when it did not say. Undefined
12
+ * for any other error. The message of that error is the package's own.
13
+ */
14
+ export declare function noTextReason(error: unknown): string | undefined;
15
+ /** A reply to a plain completion. */
16
+ export type LimitedReply = {
17
+ text: string;
18
+ /** True when the model stopped because it reached the output limit. */
19
+ cutOff: boolean;
20
+ /**
21
+ * True when the provider stopped the reply before its end for another reason (its safety
22
+ * filters): what it wrote is kept, and said to be incomplete, as a reply cut off at the limit is.
23
+ */
24
+ stopped?: boolean;
25
+ /** Why it stopped, in the provider's word, for the message when there is no text. */
26
+ reason?: string;
27
+ /** Tokens the model spent thinking, when the provider says. */
28
+ thoughts: number;
29
+ };
30
+ /**
31
+ * Sends a completion with an output limit, and once more with `THINKING_OUTPUT_TOKENS` when the
32
+ * reply was cut off with nothing written, or with half the limit or more gone on thoughts. What
33
+ * was learned is kept under `model` (a URL, or a URL and a model name): later requests start with
34
+ * the larger limit, and a model that refused it (HTTP 400, but not for a request too large for the
35
+ * window with it) is not asked for it again, nor is one whose window, or allowance a minute, is no
36
+ * larger than that limit. Requests that arrive while the first one to a model is on its way wait for it.
37
+ */
38
+ export declare function completeWithinLimit(model: string, maxOutputTokens: number, send: (limit: number) => Promise<LimitedReply>, options?: {
39
+ signal?: AbortSignal | undefined;
40
+ onTruncated?: (() => void) | undefined;
41
+ }): Promise<string>;
42
+ //# sourceMappingURL=output-limit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output-limit.d.ts","sourceRoot":"","sources":["../src/output-limit.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAwB7C;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAE/D;AAED,qCAAqC;AACrC,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,uEAAuE;IACvE,MAAM,EAAE,OAAO,CAAC;IAChB;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,qFAAqF;IACrF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+DAA+D;IAC/D,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,YAAY,CAAC,EAAE,OAAO,GAAE;IACzI,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;IACjC,WAAW,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;CACnC,GAAG,OAAO,CAAC,MAAM,CAAC,CAgEvB"}
@@ -0,0 +1,121 @@
1
+ // A plain completion on a model that may think its output limit away: shared by the part readers
2
+ // of every adapter.
3
+ /**
4
+ * A model that thinks counts its thoughts in its output limit. Asked for 4,096 tokens of notes on
5
+ * a part of a log, Gemini 3.8 Flash spent 3,931 of them thinking and wrote two lines, and Gemini 4
6
+ * wrote nothing at all (November 2026). A reply that was cut off that way is asked for again with
7
+ * this limit, the least Google recommends for a Gemini 4 model. OpenAI says to keep at least
8
+ * 25,000 for its reasoning models.
9
+ */
10
+ export const THINKING_OUTPUT_TOKENS = 32_768;
11
+ /**
12
+ * What has been learned about a model, so that a whole-file read does not pay for the wasted
13
+ * request with every part: the output limit it needs, or 0 when it refused that limit.
14
+ */
15
+ const learnedLimits = new Map();
16
+ /** The first request to a model that is still on its way. Others wait for what it learns. */
17
+ const firstRequests = new Map();
18
+ /** Models that have answered at least once. */
19
+ const answered = new Set();
20
+ const MOST_REMEMBERED = 200;
21
+ function remember(model, limit) {
22
+ if (learnedLimits.size >= MOST_REMEMBERED && !learnedLimits.has(model))
23
+ learnedLimits.clear();
24
+ learnedLimits.set(model, limit);
25
+ }
26
+ /**
27
+ * The errors `completeWithinLimit` threw for a reply with no text, with why the reply stopped, in
28
+ * the provider's word ("" when it did not say).
29
+ */
30
+ const noTextErrors = new WeakMap();
31
+ /**
32
+ * Why a reply had no text, when this error is the one `completeWithinLimit` throws for it: the
33
+ * provider's word for why it stopped ("SAFETY", "refusal"), or "" when it did not say. Undefined
34
+ * for any other error. The message of that error is the package's own.
35
+ */
36
+ export function noTextReason(error) {
37
+ return typeof error === "object" && error !== null ? noTextErrors.get(error) : undefined;
38
+ }
39
+ /**
40
+ * Sends a completion with an output limit, and once more with `THINKING_OUTPUT_TOKENS` when the
41
+ * reply was cut off with nothing written, or with half the limit or more gone on thoughts. What
42
+ * was learned is kept under `model` (a URL, or a URL and a model name): later requests start with
43
+ * the larger limit, and a model that refused it (HTTP 400, but not for a request too large for the
44
+ * window with it) is not asked for it again, nor is one whose window, or allowance a minute, is no
45
+ * larger than that limit. Requests that arrive while the first one to a model is on its way wait for it.
46
+ */
47
+ export async function completeWithinLimit(model, maxOutputTokens, send, options = {}) {
48
+ const { signal } = options;
49
+ // The first request to a model finds out whether it thinks its limit away. The parts of a
50
+ // whole-file read are sent four at a time: the others wait for it, and one request is wasted
51
+ // on such a model, not four.
52
+ let learned = () => undefined;
53
+ if (!answered.has(model)) {
54
+ const first = firstRequests.get(model);
55
+ if (first) {
56
+ await first;
57
+ signal?.throwIfAborted();
58
+ }
59
+ else {
60
+ firstRequests.set(model, new Promise((resolve) => {
61
+ learned = () => {
62
+ if (answered.size >= MOST_REMEMBERED)
63
+ answered.clear();
64
+ answered.add(model);
65
+ firstRequests.delete(model);
66
+ resolve();
67
+ };
68
+ }));
69
+ }
70
+ }
71
+ try {
72
+ const known = learnedLimits.get(model);
73
+ const limit = Math.max(maxOutputTokens, known ?? 0);
74
+ let reply = await send(limit);
75
+ // Cut off, with nothing written or with at least half of the limit gone on thoughts: the
76
+ // limit was too low for this model, not the notes too long.
77
+ const starved = reply.cutOff && (!reply.text.trim() || reply.thoughts >= limit / 2);
78
+ if (starved && known !== 0 && limit < THINKING_OUTPUT_TOKENS) {
79
+ remember(model, THINKING_OUTPUT_TOKENS);
80
+ try {
81
+ reply = await send(THINKING_OUTPUT_TOKENS);
82
+ }
83
+ catch (error) {
84
+ if (signal?.aborted)
85
+ throw error;
86
+ // A model or a gateway that refuses the larger limit (HTTP 400) is not asked for it again.
87
+ // A refusal of this request as too large for the window with that limit is no such refusal,
88
+ // unless the window is no larger than the limit itself: a smaller part may fit. Nothing is
89
+ // learned from it, and the next request starts as this one did. A request too large for
90
+ // what the provider allows in a minute (OpenAI's or Groq's TPM, HTTP 429 or 413) is too
91
+ // large the same way, with that allowance as the window. Any other failure says nothing
92
+ // about the limit: the next request starts with it. What the first reply had is kept; with
93
+ // nothing in it, this error is the one to report.
94
+ const { status, kind, limitTokens } = (error ?? {});
95
+ if (status === 400 || kind === "context_overflow") {
96
+ const tooLarge = kind === "context_overflow" && !(typeof limitTokens === "number" && limitTokens <= THINKING_OUTPUT_TOKENS);
97
+ if (!tooLarge)
98
+ remember(model, 0);
99
+ else if (learnedLimits.get(model) === THINKING_OUTPUT_TOKENS)
100
+ learnedLimits.delete(model);
101
+ }
102
+ if (!reply.text.trim())
103
+ throw error;
104
+ }
105
+ }
106
+ if (!reply.text.trim()) {
107
+ const error = new Error(`The model returned no text${reply.reason ? ` (${reply.reason})` : ""}.`);
108
+ noTextErrors.set(error, reply.reason ?? "");
109
+ throw error;
110
+ }
111
+ // The text stops mid-list when the output limit is reached, or when a filter stopped it. Say so
112
+ // instead of passing it off as complete.
113
+ if (reply.cutOff || reply.stopped)
114
+ options.onTruncated?.();
115
+ return reply.text;
116
+ }
117
+ finally {
118
+ learned();
119
+ }
120
+ }
121
+ //# sourceMappingURL=output-limit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output-limit.js","sourceRoot":"","sources":["../src/output-limit.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,oBAAoB;AAEpB;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAE7C;;;GAGG;AACH,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAC;AAChD,6FAA6F;AAC7F,MAAM,aAAa,GAAG,IAAI,GAAG,EAAyB,CAAC;AACvD,+CAA+C;AAC/C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;AACnC,MAAM,eAAe,GAAG,GAAG,CAAC;AAE5B,SAAS,QAAQ,CAAC,KAAa,EAAE,KAAa;IAC5C,IAAI,aAAa,CAAC,IAAI,IAAI,eAAe,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC;QAAE,aAAa,CAAC,KAAK,EAAE,CAAC;IAC9F,aAAa,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAClC,CAAC;AAED;;;GAGG;AACH,MAAM,YAAY,GAAG,IAAI,OAAO,EAAkB,CAAC;AAEnD;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3F,CAAC;AAkBD;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,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AAC,CAAC,KAAK,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;YAClG,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC;YAC5C,MAAM,KAAK,CAAC;QACd,CAAC;QACD,gGAAgG;QAChG,yCAAyC;QACzC,IAAI,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,OAAO;YAAE,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;QAC3D,OAAO,KAAK,CAAC,IAAI,CAAC;IACpB,CAAC;YAAS,CAAC;QACT,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC"}
@@ -0,0 +1,24 @@
1
+ /** Lets other work on the server run (an answer streaming to another user) before going on. */
2
+ export declare const pause: () => Promise<void>;
3
+ /**
4
+ * `pause`, taken in turns: each caller waits for the pause before it, so one slice of work runs in
5
+ * a turn of the event loop. Ten uploads that pause at once then hold the server for one slice
6
+ * between two turns, not ten.
7
+ */
8
+ export declare const fairPause: () => Promise<void>;
9
+ /**
10
+ * Splits a long piece of synchronous work into slices, so that one upload does not hold the whole
11
+ * server: call `maybePause()` now and then (every few thousand items), and it pauses once the
12
+ * current slice has run for about 12 ms. It also stops the work when `signal` is aborted.
13
+ */
14
+ export declare class TimeSlicer {
15
+ private readonly signal?;
16
+ private sliceStart;
17
+ constructor(signal?: AbortSignal | undefined);
18
+ /** Whether the current slice has run its time: `maybePause` would pause now. */
19
+ get due(): boolean;
20
+ maybePause(): Promise<void>;
21
+ /** Pauses, then starts a new slice. Throws the reason of `signal` when it was aborted meanwhile. */
22
+ pauseNow(): Promise<void>;
23
+ }
24
+ //# sourceMappingURL=pause.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pause.d.ts","sourceRoot":"","sources":["../src/pause.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,eAAO,MAAM,KAAK,qBAA8D,CAAC;AAIjF;;;;GAIG;AACH,eAAO,MAAM,SAAS,QAAO,OAAO,CAAC,IAAI,CAA8B,CAAC;AAKxE;;;;GAIG;AACH,qBAAa,UAAU;IAGT,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IAFpC,OAAO,CAAC,UAAU,CAAqB;gBAEV,MAAM,CAAC,EAAE,WAAW,YAAA;IAEjD,gFAAgF;IAChF,IAAI,GAAG,IAAI,OAAO,CAEjB;IAEK,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC,oGAAoG;IAC9F,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAKhC"}
package/dist/pause.js ADDED
@@ -0,0 +1,38 @@
1
+ /** Lets other work on the server run (an answer streaming to another user) before going on. */
2
+ export const pause = () => new Promise((resolve) => setImmediate(resolve));
3
+ let turn = Promise.resolve();
4
+ /**
5
+ * `pause`, taken in turns: each caller waits for the pause before it, so one slice of work runs in
6
+ * a turn of the event loop. Ten uploads that pause at once then hold the server for one slice
7
+ * between two turns, not ten.
8
+ */
9
+ export const fairPause = () => (turn = turn.then(pause));
10
+ /** Work done between two pauses of a long loop, in milliseconds: short enough that nobody waits. */
11
+ const SLICE_MS = 12;
12
+ /**
13
+ * Splits a long piece of synchronous work into slices, so that one upload does not hold the whole
14
+ * server: call `maybePause()` now and then (every few thousand items), and it pauses once the
15
+ * current slice has run for about 12 ms. It also stops the work when `signal` is aborted.
16
+ */
17
+ export class TimeSlicer {
18
+ signal;
19
+ sliceStart = performance.now();
20
+ constructor(signal) {
21
+ this.signal = signal;
22
+ }
23
+ /** Whether the current slice has run its time: `maybePause` would pause now. */
24
+ get due() {
25
+ return performance.now() - this.sliceStart >= SLICE_MS;
26
+ }
27
+ async maybePause() {
28
+ if (this.due)
29
+ await this.pauseNow();
30
+ }
31
+ /** Pauses, then starts a new slice. Throws the reason of `signal` when it was aborted meanwhile. */
32
+ async pauseNow() {
33
+ await fairPause();
34
+ this.signal?.throwIfAborted();
35
+ this.sliceStart = performance.now();
36
+ }
37
+ }
38
+ //# sourceMappingURL=pause.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pause.js","sourceRoot":"","sources":["../src/pause.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;AAEjF,IAAI,IAAI,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;AAE5C;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,GAAkB,EAAE,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AAExE,oGAAoG;AACpG,MAAM,QAAQ,GAAG,EAAE,CAAC;AAEpB;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAGQ;IAFrB,UAAU,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAEvC,YAA6B,MAAoB;QAApB,WAAM,GAAN,MAAM,CAAc;IAAG,CAAC;IAErD,gFAAgF;IAChF,IAAI,GAAG;QACL,OAAO,WAAW,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,UAAU,IAAI,QAAQ,CAAC;IACzD,CAAC;IAED,KAAK,CAAC,UAAU;QACd,IAAI,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;IACtC,CAAC;IAED,oGAAoG;IACpG,KAAK,CAAC,QAAQ;QACZ,MAAM,SAAS,EAAE,CAAC;QAClB,IAAI,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC;QAC9B,IAAI,CAAC,UAAU,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IACtC,CAAC;CACF"}
@@ -9,6 +9,31 @@ export type PageCut = {
9
9
  /** How many pages the original has. */
10
10
  pageCount: number;
11
11
  };
12
+ /**
13
+ * Objects parsed between two pauses that let other work on the server run, when the parse cannot
14
+ * be paused by time (see `loadPdf`). A count is a poor measure: 500 objects of large arrays took
15
+ * 100 to 180 ms.
16
+ */
17
+ export declare const OBJECTS_PER_TICK = 500;
18
+ /**
19
+ * The `parseSpeed` for pdf-lib: `OBJECTS_PER_TICK`, or more for a file that declares so many
20
+ * objects that pausing that often would add seconds of waiting (300 pauses of 15 ms for 150,000).
21
+ */
22
+ export declare function objectsPerTick(bytes: Uint8Array): number;
23
+ /**
24
+ * Reads a PDF into objects with pdf-lib, which cannot be stopped once it has started, and pauses
25
+ * after every 12 to 40 ms of work so that other requests get a turn. pdf-lib itself pauses by a
26
+ * count of objects, and objects differ in cost a hundred times: a count of 500 held the server for
27
+ * 100 to 180 ms on a file of large arrays, and a file of 150,000 objects, parsed 1,500 at a time,
28
+ * for 0.2 to 0.55 s. What pdf-lib reads in one step still holds the server for as long as it
29
+ * takes, and grows with the file: one object (about 0.15 s for each MB of an array of numbers,
30
+ * about 0.03 s for each MB of a stream whose length the file does not give directly), and a
31
+ * cross-reference table written as text (0.15 s for 150,000 objects).
32
+ *
33
+ * The file is read with an empty password, which opens the many PDFs that are encrypted only to
34
+ * restrict printing or copying. Throws what pdf-lib throws.
35
+ */
36
+ export declare function loadPdf(library: PdfLibrary, bytes: Uint8Array): Promise<import("@cantoo/pdf-lib").PDFDocument>;
12
37
  /** Bytes as a string of the same length: each byte becomes the character with that code. */
13
38
  export declare function latin1(bytes: Uint8Array): string;
14
39
  export declare function loadPdfLibrary(): Promise<PdfLibrary>;
@@ -28,8 +53,13 @@ export declare function declaredObjectCount(bytes: Uint8Array): number | undefin
28
53
  * hold; undefined when the file can be read.
29
54
  */
30
55
  export declare function tooManyObjects(original: Uint8Array, most?: number): number | undefined;
31
- /** A PDF name as the characters it stands for, so that "#20" and a space are the same name. */
32
- export declare function canonicalName(raw: string): string;
56
+ /**
57
+ * A PDF name as the characters it stands for, so that "#20" and a space are the same name.
58
+ * Undefined for a name that still changes after `NAME_DECODINGS` decodings: only a file built to
59
+ * hold the server writes one ("#2323...2341" loses one level a decoding, and decoding it until
60
+ * nothing changed took 20 s for 400,000 characters). Callers treat it as a name they cannot read.
61
+ */
62
+ export declare function canonicalName(raw: string): string | undefined;
33
63
  /**
34
64
  * Cuts pages `from` to `to` (1-based, inclusive) out of a PDF. When the cut is larger than
35
65
  * `maxBytes`, pages are left off the end until it fits; `to` in the result says where it stopped.
@@ -1 +1 @@
1
- {"version":3,"file":"pdf-pages.d.ts","sourceRoot":"","sources":["../src/pdf-pages.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAW,SAAS,EAAwB,MAAM,iBAAiB,CAAC;AAG5F,KAAK,UAAU,GAAG,cAAc,iBAAiB,CAAC,CAAC;AAEnD,MAAM,MAAM,OAAO,GAAG;IACpB,uDAAuD;IACvD,KAAK,EAAE,UAAU,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,8FAA8F;IAC9F,EAAE,EAAE,MAAM,CAAC;IACX,uCAAuC;IACvC,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAcF,4FAA4F;AAC5F,wBAAgB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEhD;AAED,wBAAsB,cAAc,IAAI,OAAO,CAAC,UAAU,CAAC,CAM1D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,GAAG,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAY3I;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CAezE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,UAAU,EAAE,IAAI,SAAc,GAAG,MAAM,GAAG,SAAS,CAM3F;AAED,+FAA+F;AAC/F,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAQjD;AA8JD;;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