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.
- package/CHANGELOG.md +1400 -338
- package/README.md +217 -49
- package/dist/analyze.d.ts +5 -0
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +96 -47
- package/dist/analyze.js.map +1 -1
- package/dist/anthropic/adapter.d.ts +117 -0
- package/dist/anthropic/adapter.d.ts.map +1 -0
- package/dist/anthropic/adapter.js +542 -0
- package/dist/anthropic/adapter.js.map +1 -0
- package/dist/anthropic/index.d.ts +3 -0
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +2 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/ask.d.ts +200 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +1402 -0
- package/dist/ask.js.map +1 -0
- package/dist/budget.d.ts +23 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +64 -0
- package/dist/budget.js.map +1 -0
- package/dist/encoding.d.ts +17 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +852 -0
- package/dist/encoding.js.map +1 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +333 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +159 -0
- package/dist/events.js.map +1 -0
- package/dist/evidence.d.ts +42 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +138 -0
- package/dist/evidence.js.map +1 -0
- package/dist/extract.d.ts +35 -3
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +626 -58
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +59 -0
- package/dist/gemini/adapter.d.ts.map +1 -0
- package/dist/gemini/adapter.js +226 -0
- package/dist/gemini/adapter.js.map +1 -0
- package/dist/gemini/chat.d.ts +53 -77
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +42 -300
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/errors.d.ts +16 -0
- package/dist/gemini/errors.d.ts.map +1 -0
- package/dist/gemini/errors.js +46 -0
- package/dist/gemini/errors.js.map +1 -0
- package/dist/gemini/generate.d.ts +18 -21
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +49 -92
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +6 -4
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +4 -3
- package/dist/gemini/index.js.map +1 -1
- package/dist/gemini/sse.d.ts +1 -2
- package/dist/gemini/sse.d.ts.map +1 -1
- package/dist/gemini/sse.js +1 -38
- package/dist/gemini/sse.js.map +1 -1
- package/dist/http.d.ts +136 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +401 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +18 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -4
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +96 -8
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +414 -96
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +37 -1
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +255 -61
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +278 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +222 -0
- package/dist/model.js.map +1 -0
- package/dist/node/handlers.d.ts +20 -4
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +122 -20
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/sse.d.ts +59 -0
- package/dist/node/sse.d.ts.map +1 -0
- package/dist/node/sse.js +174 -0
- package/dist/node/sse.js.map +1 -0
- package/dist/openai/adapter.d.ts +109 -0
- package/dist/openai/adapter.d.ts.map +1 -0
- package/dist/openai/adapter.js +567 -0
- package/dist/openai/adapter.js.map +1 -0
- package/dist/openai/index.d.ts +3 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +2 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/output-limit.d.ts +42 -0
- package/dist/output-limit.d.ts.map +1 -0
- package/dist/output-limit.js +121 -0
- package/dist/output-limit.js.map +1 -0
- package/dist/pause.d.ts +24 -0
- package/dist/pause.d.ts.map +1 -0
- package/dist/pause.js +38 -0
- package/dist/pause.js.map +1 -0
- package/dist/pdf-pages.d.ts +32 -2
- package/dist/pdf-pages.d.ts.map +1 -1
- package/dist/pdf-pages.js +275 -14
- package/dist/pdf-pages.js.map +1 -1
- package/dist/pdf-pictures.d.ts +30 -3
- package/dist/pdf-pictures.d.ts.map +1 -1
- package/dist/pdf-pictures.js +50 -8
- package/dist/pdf-pictures.js.map +1 -1
- package/dist/pdf-work.d.ts +40 -0
- package/dist/pdf-work.d.ts.map +1 -0
- package/dist/pdf-work.js +734 -0
- package/dist/pdf-work.js.map +1 -0
- package/dist/plan.d.ts +22 -4
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +61 -12
- package/dist/plan.js.map +1 -1
- package/dist/retry.d.ts +76 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +141 -0
- package/dist/retry.js.map +1 -0
- package/dist/search.d.ts +8 -2
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +60 -10
- package/dist/search.js.map +1 -1
- package/dist/sse.d.ts +36 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +153 -0
- package/dist/sse.js.map +1 -0
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +5 -2
- package/dist/stores.js.map +1 -1
- package/dist/text-index.d.ts +6 -0
- package/dist/text-index.d.ts.map +1 -1
- package/dist/text-index.js +25 -8
- package/dist/text-index.js.map +1 -1
- package/dist/text.d.ts +62 -2
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +314 -14
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts +72 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +341 -60
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +128 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/zip-directory.d.ts +43 -0
- package/dist/zip-directory.d.ts.map +1 -0
- package/dist/zip-directory.js +130 -0
- package/dist/zip-directory.js.map +1 -0
- package/docs/README.md +288 -0
- package/docs/api.md +58 -184
- package/docs/configuration.md +34 -490
- package/docs/getting-started.md +412 -177
- package/docs/guides/anthropic.md +590 -0
- package/docs/guides/conversations.md +629 -0
- package/docs/guides/costs-and-limits.md +726 -0
- package/docs/guides/deployment.md +654 -0
- package/docs/guides/errors-and-reliability.md +735 -0
- package/docs/guides/explorer-model.md +399 -0
- package/docs/guides/openai-compatible.md +602 -0
- package/docs/guides/own-adapter-and-loop.md +1169 -0
- package/docs/guides/pdfs.md +971 -0
- package/docs/guides/storage.md +731 -0
- package/docs/guides/testing.md +1199 -0
- package/docs/guides/vertex-and-gemini.md +717 -0
- package/docs/guides/web-app.md +1589 -0
- package/docs/how-it-works.md +1022 -138
- package/docs/measurements.md +881 -0
- package/docs/publishing.md +274 -46
- package/docs/reference/anthropic.md +427 -0
- package/docs/reference/ask.md +892 -0
- package/docs/reference/errors-and-retries.md +758 -0
- package/docs/reference/events.md +993 -0
- package/docs/reference/gemini.md +823 -0
- package/docs/reference/library.md +1467 -0
- package/docs/reference/node.md +528 -0
- package/docs/reference/openai.md +385 -0
- package/docs/reference/tools.md +883 -0
- package/docs/security.md +902 -90
- package/docs/troubleshooting.md +1390 -0
- package/package.json +22 -4
- package/src/analyze.ts +93 -42
- package/src/anthropic/adapter.ts +611 -0
- package/src/anthropic/index.ts +2 -0
- package/src/ask.ts +1560 -0
- package/src/budget.ts +71 -0
- package/src/encoding.ts +850 -0
- package/src/errors.ts +17 -0
- package/src/events.ts +442 -0
- package/src/evidence.ts +165 -0
- package/src/extract.ts +602 -62
- package/src/gemini/adapter.ts +271 -0
- package/src/gemini/chat.ts +87 -355
- package/src/gemini/errors.ts +48 -0
- package/src/gemini/generate.ts +67 -94
- package/src/gemini/index.ts +6 -4
- package/src/gemini/sse.ts +1 -34
- package/src/http.ts +434 -0
- package/src/index.ts +55 -4
- package/src/library.ts +497 -97
- package/src/map-reduce.ts +266 -59
- package/src/model.ts +433 -0
- package/src/node/handlers.ts +137 -22
- package/src/node/index.ts +2 -0
- package/src/node/sse.ts +198 -0
- package/src/openai/adapter.ts +613 -0
- package/src/openai/index.ts +2 -0
- package/src/output-limit.ts +135 -0
- package/src/pause.ts +41 -0
- package/src/pdf-pages.ts +268 -15
- package/src/pdf-pictures.ts +56 -9
- package/src/pdf-work.ts +722 -0
- package/src/plan.ts +68 -12
- package/src/retry.ts +176 -0
- package/src/search.ts +63 -10
- package/src/sse.ts +159 -0
- package/src/stores.ts +5 -2
- package/src/text-index.ts +26 -7
- package/src/text.ts +323 -13
- package/src/tools.ts +338 -62
- package/src/types.ts +129 -5
- 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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BACnH,IAAI,MAAM,KAAK,GAAG,IAAI,IAAI,KAAK,kBAAkB,EAAE,CAAC;oBAClD,MAAM,QAAQ,GAAG,IAAI,KAAK,kBAAkB,IAAI,CAAC,CAAC,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,IAAI,sBAAsB,CAAC,CAAC;oBAC5H,IAAI,CAAC,QAAQ;wBAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;yBAC7B,IAAI,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,sBAAsB;wBAAE,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC5F,CAAC;gBACD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;oBAAE,MAAM,KAAK,CAAC;YACtC,CAAC;QACH,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;YACvB,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,6BAA6B,KAAK,CAAC,MAAM,CAAC,CAAC,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"}
|
package/dist/pause.d.ts
ADDED
|
@@ -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"}
|
package/dist/pdf-pages.d.ts
CHANGED
|
@@ -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
|
-
/**
|
|
32
|
-
|
|
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.
|
package/dist/pdf-pages.d.ts.map
CHANGED
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"pdf-pages.d.ts","sourceRoot":"","sources":["../src/pdf-pages.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAW,SAAS,EAAiD,MAAM,iBAAiB,CAAC;AAIrH,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;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,MAAM,CAAC;AAKpC;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAGxD;AA4HD;;;;;;;;;;;;GAYG;AACH,wBAAsB,OAA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