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
package/src/types.ts
CHANGED
|
@@ -49,6 +49,19 @@ export type DocumentMeta = {
|
|
|
49
49
|
/** Lines longer than the wrap limit are split so every line stays readable by line number. */
|
|
50
50
|
wrappedLines: number;
|
|
51
51
|
pages?: number;
|
|
52
|
+
/**
|
|
53
|
+
* The older encoding a text file was read in, because it is not UTF-8: "windows-1255" for a
|
|
54
|
+
* Hebrew CSV that Excel saved. Lines of it that are valid UTF-8 are read as UTF-8 all the same.
|
|
55
|
+
* Absent for valid UTF-8, for a file read as UTF-8 with characters that are not valid (see
|
|
56
|
+
* `replacedCharacters`), for UTF-16, for PDF, DOCX and PPTX, and on documents from before 0.6.0.
|
|
57
|
+
*/
|
|
58
|
+
encoding?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Characters of a text file that were not valid in the encoding it was read in, such as a
|
|
61
|
+
* character cut in two at the end of a UTF-8 log, or a byte that `encoding` does not have: each
|
|
62
|
+
* is stored as U+FFFD (�). Absent when there were none, and on documents from before 0.6.0.
|
|
63
|
+
*/
|
|
64
|
+
replacedCharacters?: number;
|
|
52
65
|
/**
|
|
53
66
|
* True when the uploaded file itself is kept in the store next to its text (see `keepOriginals`).
|
|
54
67
|
* Recorded here so that planning a request does not have to load the file to know.
|
|
@@ -72,6 +85,10 @@ export type DocumentSummary = {
|
|
|
72
85
|
lines: number;
|
|
73
86
|
estimatedTokens: number;
|
|
74
87
|
pages?: number;
|
|
88
|
+
/** See `DocumentMeta.encoding`. */
|
|
89
|
+
encoding?: string;
|
|
90
|
+
/** See `DocumentMeta.replacedCharacters`. */
|
|
91
|
+
replacedCharacters?: number;
|
|
75
92
|
expiresAt: string;
|
|
76
93
|
};
|
|
77
94
|
|
|
@@ -109,7 +126,17 @@ export interface DocumentStore {
|
|
|
109
126
|
getIndex?(id: string): Promise<Uint8Array | undefined>;
|
|
110
127
|
}
|
|
111
128
|
|
|
112
|
-
export type ExtractedText = {
|
|
129
|
+
export type ExtractedText = {
|
|
130
|
+
text: string;
|
|
131
|
+
pages?: number;
|
|
132
|
+
/**
|
|
133
|
+
* The older encoding the file was read in, when it is not UTF-8. Kept as `DocumentMeta.encoding`
|
|
134
|
+
* when it is a name, and not one of UTF-8 or UTF-16.
|
|
135
|
+
*/
|
|
136
|
+
encoding?: string;
|
|
137
|
+
/** Characters that were not valid in the encoding the file was read in, each now U+FFFD. Kept as `DocumentMeta.replacedCharacters`. */
|
|
138
|
+
replacedCharacters?: number;
|
|
139
|
+
};
|
|
113
140
|
|
|
114
141
|
export type ExtractInput = {
|
|
115
142
|
name: string;
|
|
@@ -121,6 +148,38 @@ export type ExtractInput = {
|
|
|
121
148
|
* always was. An extractor of your own may ignore it.
|
|
122
149
|
*/
|
|
123
150
|
scannedPages?: number;
|
|
151
|
+
/**
|
|
152
|
+
* Aborted when the ingest's time is up (`ingestTimeoutMs`) or the caller gave up
|
|
153
|
+
* (`IngestInput.signal`). The built-in extractors stop between pages and pieces; its `reason`
|
|
154
|
+
* is the error to throw. An extractor of your own should stop too: `ingest` stops waiting for it
|
|
155
|
+
* either way, but cannot stop the work it does.
|
|
156
|
+
*/
|
|
157
|
+
signal?: AbortSignal;
|
|
158
|
+
/**
|
|
159
|
+
* The most text to keep from this file, in UTF-8 bytes (`maxTextBytes`). The built-in extractors
|
|
160
|
+
* refuse a file as soon as its text grows past it. `ingest` refuses any larger result.
|
|
161
|
+
*/
|
|
162
|
+
maxTextBytes?: number;
|
|
163
|
+
/** The most pages to read from one PDF (`maxPdfPages`). The built-in PDF extractor refuses more. */
|
|
164
|
+
maxPages?: number;
|
|
165
|
+
/**
|
|
166
|
+
* Call it as the extraction goes on, so `ingest` can report progress (`IngestInput.onEvent`).
|
|
167
|
+
* The built-in extractors call it for each page of a PDF (also for the page check and the
|
|
168
|
+
* picture marks: `stage` "check" and "pictures"), each part of a DOCX or PPTX, and once when a
|
|
169
|
+
* text file not in UTF-8 is decoded ("decode"). Calls after the extractor returned are dropped. An
|
|
170
|
+
* extractor of your own may ignore it. New in 0.7.0.
|
|
171
|
+
*/
|
|
172
|
+
onProgress?: (progress: ExtractProgress) => void;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/** How far an extraction is, for `ExtractInput.onProgress`. */
|
|
176
|
+
export type ExtractProgress = {
|
|
177
|
+
/** "extract" (the default): text read; "check": pages checked before reading; "pictures": pages looked at for pictures; "decode": bytes of a text file decoded. */
|
|
178
|
+
stage?: "extract" | "check" | "pictures" | "decode";
|
|
179
|
+
done: number;
|
|
180
|
+
/** Absent when not known. */
|
|
181
|
+
total?: number;
|
|
182
|
+
unit: "pages" | "parts" | "bytes";
|
|
124
183
|
};
|
|
125
184
|
|
|
126
185
|
export type Extractor = (bytes: Uint8Array, input: ExtractInput) => Promise<ExtractedText>;
|
|
@@ -172,17 +231,68 @@ export type ToolFile = {
|
|
|
172
231
|
/** A tool's JSON result, and the files that belong to it. Most tools return no files. */
|
|
173
232
|
export type ToolOutput = { result: ToolResult; files: ToolFile[] };
|
|
174
233
|
|
|
175
|
-
/** What
|
|
176
|
-
export type
|
|
234
|
+
/** What one tool call is allowed to use of the request its result will be sent in. */
|
|
235
|
+
export type ToolCallLimits = {
|
|
177
236
|
/**
|
|
178
237
|
* The most bytes the files of this call may have together. Give the room left in your request,
|
|
179
|
-
* times 3/4 when files are sent as base64. Defaults to
|
|
238
|
+
* times 3/4 when files are sent as base64. Defaults to 3/8 of `maxRequestBytes` less 262,144
|
|
239
|
+
* (the 256 KB the library keeps free in a request).
|
|
180
240
|
*/
|
|
181
241
|
maxFileBytes?: number;
|
|
182
242
|
/** The most pages this call may show. Lower it as the pages already shown use up your token budget. */
|
|
183
243
|
maxPages?: number;
|
|
244
|
+
/**
|
|
245
|
+
* The most characters the JSON result of this call may have, when that is less than the tool's
|
|
246
|
+
* own cap (`toolResultCharacters`): the room left in the request. The result is cut to it, and
|
|
247
|
+
* says where to go on.
|
|
248
|
+
*/
|
|
249
|
+
maxResultCharacters?: number;
|
|
250
|
+
/**
|
|
251
|
+
* The most model requests a whole-file read (`analyze_whole_document`) may make: what the
|
|
252
|
+
* question has left of `limits.requests`. A part the model refuses as too large is read as two
|
|
253
|
+
* halves only while that and `maxMapChunks` allow it.
|
|
254
|
+
*/
|
|
255
|
+
maxRequests?: number;
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* Listeners for one tool call, next to its limits. New in 0.7.0: with them, the progress of two
|
|
259
|
+
* whole-file reads that run at once can be told apart.
|
|
260
|
+
*/
|
|
261
|
+
export type ToolCallHooks = {
|
|
262
|
+
/** Progress of this call (a whole-file read), in place of `ToolOptions.onProgress` for it. */
|
|
263
|
+
onProgress?: ProgressListener;
|
|
264
|
+
/**
|
|
265
|
+
* Each model request this call makes (a whole-file read), as it starts. Tell the handle it
|
|
266
|
+
* returns about each retry of that request, and when it ends.
|
|
267
|
+
*/
|
|
268
|
+
onRequest?: (request: ToolRequestStart) => ToolRequestHandle;
|
|
184
269
|
};
|
|
185
270
|
|
|
271
|
+
/** One model request of a tool call (`ToolCallHooks.onRequest`). */
|
|
272
|
+
export type ToolRequestHandle = {
|
|
273
|
+
/** The request failed and is sent again after `delayMs`. */
|
|
274
|
+
retry(notice: { attempt: number; delayMs: number; error: unknown }): void;
|
|
275
|
+
/** The request ended, after any retries. Call it once. */
|
|
276
|
+
end(end: ToolRequestEnd): void;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/** A model request of a tool call, as it starts (`ToolCallHooks.onRequest`). */
|
|
280
|
+
export type ToolRequestStart = {
|
|
281
|
+
purpose: "part" | "merge" | "final";
|
|
282
|
+
/** "part 3 of 12", "first half of part 3 of 12", "round 2: 1 of 4", "the answer". */
|
|
283
|
+
detail: string;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/** A model request of a tool call, as it ends. */
|
|
287
|
+
export type ToolRequestEnd = {
|
|
288
|
+
outcome: "ok" | "error";
|
|
289
|
+
usage?: GenerateUsage;
|
|
290
|
+
error?: unknown;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/** The name of `ToolCallLimits` before 0.5.0. */
|
|
294
|
+
export type ToolFileLimits = ToolCallLimits;
|
|
295
|
+
|
|
186
296
|
export type ToolSet = {
|
|
187
297
|
declarations: ToolDeclaration[];
|
|
188
298
|
has(name: string): boolean;
|
|
@@ -195,11 +305,19 @@ export type ToolSet = {
|
|
|
195
305
|
* Runs a tool call and also returns the files it produced, to be sent to the model with the
|
|
196
306
|
* result. Use it in place of `execute` in a loop that can send files.
|
|
197
307
|
*/
|
|
198
|
-
executeWithFiles(name: string, args: Record<string, unknown>, limits?:
|
|
308
|
+
executeWithFiles(name: string, args: Record<string, unknown>, limits?: ToolCallLimits, hooks?: ToolCallHooks): Promise<ToolOutput>;
|
|
199
309
|
/** A short, human-readable line describing a call, for progress UIs. */
|
|
200
310
|
describe(name: string, args: Record<string, unknown>): string;
|
|
311
|
+
/**
|
|
312
|
+
* The most characters the JSON result of a tool can have: what a loop sets aside in its request
|
|
313
|
+
* before it runs a call.
|
|
314
|
+
*/
|
|
315
|
+
resultCharacters(name: string): number;
|
|
201
316
|
};
|
|
202
317
|
|
|
318
|
+
/** Tokens one model request used, as far as the provider said. */
|
|
319
|
+
export type GenerateUsage = { inputTokens?: number; cachedInputTokens?: number; outputTokens?: number; thoughtTokens?: number };
|
|
320
|
+
|
|
203
321
|
/** A plain text completion. The package stays provider-neutral by taking this as a function. */
|
|
204
322
|
export type GenerateText = (request: {
|
|
205
323
|
system: string;
|
|
@@ -212,6 +330,12 @@ export type GenerateText = (request: {
|
|
|
212
330
|
* as before, without that warning.
|
|
213
331
|
*/
|
|
214
332
|
onTruncated?: () => void;
|
|
333
|
+
/**
|
|
334
|
+
* Call this once for every request that was answered, with the tokens it used, so that the cost
|
|
335
|
+
* of a whole-document read is counted. Optional: without it, `askWithFiles` still counts each
|
|
336
|
+
* answered request (in `usage.requests` and against `limits.requests`), but not its tokens.
|
|
337
|
+
*/
|
|
338
|
+
onUsage?: (usage: GenerateUsage) => void;
|
|
215
339
|
}) => Promise<string>;
|
|
216
340
|
|
|
217
341
|
export type ProgressEvent = {
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the directory of a ZIP file the way JSZip 3.10 reads it, before JSZip does, and adds up
|
|
3
|
+
* what JSZip would keep from it. JSZip reads the whole directory into memory before it unpacks any
|
|
4
|
+
* part, and the directory is written by whoever made the file: its count of parts, where it
|
|
5
|
+
* starts, and how long each record is. So the count of parts is not taken from the end record. The
|
|
6
|
+
* directory is found where JSZip will find it, and its records are walked as JSZip will walk them,
|
|
7
|
+
* each by its own lengths, until JSZip would stop, or the file is past a limit.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** What JSZip would keep in memory from the directory of a ZIP file. */
|
|
11
|
+
export type ZipDirectory = {
|
|
12
|
+
/** The parts JSZip would read, counted to one past the limit. */
|
|
13
|
+
parts: number;
|
|
14
|
+
/** Their extra fields, counted to one past the limit: JSZip keeps about 250 bytes for each. */
|
|
15
|
+
extraFields: number;
|
|
16
|
+
/**
|
|
17
|
+
* The bytes JSZip turns into text, or goes through one by one, counted until they pass the
|
|
18
|
+
* limit: the comment of the file, and for each part the name in the local header it points to
|
|
19
|
+
* (a header that many parts point to counts once for each), its comment, and the data of its
|
|
20
|
+
* ZIP64 (1), Unicode Path (0x7075) and Unicode Comment (0x6375) extra fields. That data may run
|
|
21
|
+
* past the record, over the records after it.
|
|
22
|
+
*/
|
|
23
|
+
textBytes: number;
|
|
24
|
+
/**
|
|
25
|
+
* The ZIP64 end record says it is longer than its fields. JSZip reads what follows in a loop that
|
|
26
|
+
* does not move on: it stops when it runs out of data, or never.
|
|
27
|
+
*/
|
|
28
|
+
endless: boolean;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const LOCAL = 0x04034b50;
|
|
32
|
+
const CENTRAL = 0x02014b50;
|
|
33
|
+
const END = 0x06054b50;
|
|
34
|
+
const LOCATOR = 0x07064b50;
|
|
35
|
+
const END64 = 0x06064b50;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What JSZip would keep from the directory of `bytes`, up to the limits. Undefined when JSZip
|
|
39
|
+
* would stop before it reads any part: the file is not a ZIP, or is damaged, and JSZip will say
|
|
40
|
+
* so.
|
|
41
|
+
*
|
|
42
|
+
* JSZip reads the whole directory, then the local header of each part, and stops at the first part
|
|
43
|
+
* it cannot read (a local header that is not there, an unknown way of packing). The names and
|
|
44
|
+
* comments of the parts after it are counted too: such a file is not read either way.
|
|
45
|
+
*/
|
|
46
|
+
export function readZipDirectory(bytes: Uint8Array, limits: { parts: number; extraFields: number; textBytes: number }): ZipDirectory | undefined {
|
|
47
|
+
const length = bytes.length;
|
|
48
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
49
|
+
// JSZip reads 4 bytes as a signed number, and of 8 bytes only the low 4, as a signed number.
|
|
50
|
+
const int = (at: number) => view.getInt32(at, true);
|
|
51
|
+
const short = (at: number) => view.getUint16(at, true);
|
|
52
|
+
const at4 = (at: number, signature: number) => at >= 0 && at + 4 <= length && view.getUint32(at, true) === signature;
|
|
53
|
+
// JSZip looks for a record from the end of the whole file, not only of its last 64 KB.
|
|
54
|
+
const whole = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
55
|
+
const lastIndexOf = (signature: number) => {
|
|
56
|
+
const needle = Buffer.alloc(4);
|
|
57
|
+
needle.writeUInt32LE(signature);
|
|
58
|
+
return whole.lastIndexOf(needle);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// The end record: 22 bytes, then its comment.
|
|
62
|
+
const end = lastIndexOf(END);
|
|
63
|
+
if (end < 0 || end + 22 > length || end + 22 + short(end + 20) > length) return undefined;
|
|
64
|
+
let size = int(end + 12);
|
|
65
|
+
let offset = int(end + 16);
|
|
66
|
+
let zip64 = 0;
|
|
67
|
+
if ([4, 6, 8, 10].some((field) => short(end + field) === 0xffff) || size === -1 || offset === -1) {
|
|
68
|
+
// A field too small for its value: JSZip reads the ZIP64 end record where the last ZIP64
|
|
69
|
+
// locator of the file says it is, or else the last ZIP64 end record of the file.
|
|
70
|
+
const locator = lastIndexOf(LOCATOR);
|
|
71
|
+
if (locator < 0 || locator + 20 > length || int(locator + 16) > 1) return undefined;
|
|
72
|
+
let end64 = int(locator + 8);
|
|
73
|
+
if (end64 < 0 || end64 + 4 > length) return undefined;
|
|
74
|
+
if (!at4(end64, END64)) end64 = lastIndexOf(END64);
|
|
75
|
+
if (end64 < 0 || end64 + 56 > length) return undefined;
|
|
76
|
+
if (int(end64 + 4) > 44) return { parts: 0, extraFields: 0, textBytes: 0, endless: true };
|
|
77
|
+
zip64 = 20 + 12 + int(end64 + 4);
|
|
78
|
+
size = int(end64 + 40);
|
|
79
|
+
offset = int(end64 + 48);
|
|
80
|
+
}
|
|
81
|
+
// Bytes before the archive, such as a program that unpacks it: JSZip reads every offset after them.
|
|
82
|
+
const before = end - (offset + size + zip64);
|
|
83
|
+
if (before < 0 || offset < 0 || before + offset > length) return undefined;
|
|
84
|
+
|
|
85
|
+
const directory: ZipDirectory = { parts: 0, extraFields: 0, textBytes: short(end + 20), endless: false };
|
|
86
|
+
/** Adds bytes JSZip turns into text or goes through, and says whether they are past the limit. */
|
|
87
|
+
const text = (count: number) => (directory.textBytes += count) > limits.textBytes;
|
|
88
|
+
let at = before + offset;
|
|
89
|
+
// A record: 46 bytes, the name, the extra fields and the comment.
|
|
90
|
+
while (at4(at, CENTRAL) && at + 46 <= length) {
|
|
91
|
+
// JSZip stops at an encrypted part.
|
|
92
|
+
if (short(at + 8) & 1) break;
|
|
93
|
+
const extraStart = at + 46 + short(at + 28);
|
|
94
|
+
const extraEnd = extraStart + short(at + 30);
|
|
95
|
+
if (extraEnd > length) break;
|
|
96
|
+
// Each extra field is 4 bytes and its data, read while more than 4 bytes are left. JSZip keeps
|
|
97
|
+
// the last ZIP64 field (id 1) and goes on from the end of the extra fields.
|
|
98
|
+
let field = extraStart;
|
|
99
|
+
let zip64Data = -1;
|
|
100
|
+
let zip64End = -1;
|
|
101
|
+
while (field + 4 < extraEnd) {
|
|
102
|
+
if (++directory.extraFields > limits.extraFields) return directory;
|
|
103
|
+
const id = short(field);
|
|
104
|
+
const fieldEnd = field + 4 + short(field + 2);
|
|
105
|
+
// JSZip goes through the data of these fields byte by byte, and decodes the Unicode ones as
|
|
106
|
+
// text, even when the data runs past the extra fields of the record.
|
|
107
|
+
if ((id === 1 || id === 0x7075 || id === 0x6375) && text(fieldEnd - field - 4)) return directory;
|
|
108
|
+
if (id === 1) {
|
|
109
|
+
zip64Data = field + 4;
|
|
110
|
+
zip64End = fieldEnd;
|
|
111
|
+
}
|
|
112
|
+
field = fieldEnd;
|
|
113
|
+
}
|
|
114
|
+
if (field > length) break;
|
|
115
|
+
let local = int(at + 42);
|
|
116
|
+
if (zip64Data >= 0) {
|
|
117
|
+
// The ZIP64 field holds, 8 bytes each, the sizes and the offset the record has no room for,
|
|
118
|
+
// in that order.
|
|
119
|
+
const values = [int(at + 24), int(at + 20), local].filter((value) => value === -1).length;
|
|
120
|
+
if (zip64Data + 8 * values > zip64End) break;
|
|
121
|
+
if (local === -1) local = int(zip64Data + 8 * values - 8);
|
|
122
|
+
}
|
|
123
|
+
const comment = short(at + 32);
|
|
124
|
+
const next = extraEnd + comment;
|
|
125
|
+
if (next > length) break;
|
|
126
|
+
if (++directory.parts > limits.parts) return directory;
|
|
127
|
+
if (text(comment)) return directory;
|
|
128
|
+
// The name is decoded from the local header the part points to.
|
|
129
|
+
const header = before + local;
|
|
130
|
+
if (local >= 0 && at4(header, LOCAL) && header + 30 <= length && header + 30 + short(header + 26) <= length) {
|
|
131
|
+
if (text(short(header + 26))) return directory;
|
|
132
|
+
}
|
|
133
|
+
at = next;
|
|
134
|
+
}
|
|
135
|
+
return directory;
|
|
136
|
+
}
|