@bevel-software/platform-core-frontend 0.11.2 → 0.12.1
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/THIRD-PARTY-NOTICES.md +670 -395
- package/package.json +6 -2
- package/src/lib/__tests__/api.test.ts +95 -0
- package/src/lib/api.ts +30 -2
- package/src/lib/clipboard.ts +25 -0
- package/src/modules/access/__tests__/useFileAccess.test.tsx +47 -0
- package/src/modules/access/hooks/useFileAccess.ts +23 -5
- package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +181 -0
- package/src/modules/git/components/GitSyncFailedBanner.tsx +106 -0
- package/src/modules/library/utils/clipboard.ts +4 -20
- package/src/modules/secrets-vault/components/SecretsPage.tsx +313 -313
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +82 -82
- package/src/modules/workspace/components/FileExplorer.tsx +6 -14
- package/src/modules/workspace/components/FileViewer.tsx +20 -4
- package/src/modules/workspace/components/KbPageHeader.tsx +1 -1
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +3 -1
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +17 -0
- package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +15 -0
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -2
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +88 -0
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +309 -0
- package/src/modules/workspace/components/renderers/LegacyOfficeRenderer.tsx +60 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +261 -33
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +184 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +195 -45
- package/src/modules/workspace/components/renderers/__tests__/DownloadFileButton.test.tsx +48 -0
- package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.test.tsx +176 -0
- package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +214 -76
- package/src/modules/workspace/components/renderers/__tests__/PptxRenderer.test.tsx +125 -0
- package/src/modules/workspace/components/renderers/__tests__/XlsxRenderer.test.tsx +245 -0
- package/src/modules/workspace/components/renderers/__tests__/emailBody.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/emailMessage.test.ts +404 -0
- package/src/modules/workspace/components/renderers/__tests__/pptxOutline.test.ts +448 -0
- package/src/modules/workspace/components/renderers/__tests__/readBodyCapped.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/rendererLayout.test.ts +73 -7
- package/src/modules/workspace/components/renderers/__tests__/rendererSelection.test.tsx +107 -0
- package/src/modules/workspace/components/renderers/downloadFile.ts +33 -0
- package/src/modules/workspace/components/renderers/emailBody.ts +168 -0
- package/src/modules/workspace/components/renderers/emailMessage.ts +223 -0
- package/src/modules/workspace/components/renderers/emlMessage.ts +91 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +9 -2
- package/src/modules/workspace/components/renderers/index.ts +82 -6
- package/src/modules/workspace/components/renderers/lazyRenderer.tsx +9 -2
- package/src/modules/workspace/components/renderers/msgMessage.ts +219 -0
- package/src/modules/workspace/components/renderers/pptxOutline.ts +317 -0
- package/src/modules/workspace/components/renderers/readBodyCapped.ts +97 -0
- package/src/modules/workspace/components/renderers/xmlReading.ts +0 -0
- package/src/url-suffix-imports.d.ts +12 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import JSZip from 'jszip';
|
|
2
|
+
import {
|
|
3
|
+
attrByLocalName,
|
|
4
|
+
elementText,
|
|
5
|
+
localElementBlocks,
|
|
6
|
+
prefixedAttrByLocalName,
|
|
7
|
+
relationshipTarget,
|
|
8
|
+
} from './xmlReading';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Client-side `.pptx` → text outline, the browser twin of the backend's
|
|
12
|
+
* `doc-extract/extract-pptx.ts`. The conventions are deliberately identical —
|
|
13
|
+
* slides in numeric package order, `<a:p>` paragraphs as lines, `<a:t>` runs
|
|
14
|
+
* concatenated with NO separator (PowerPoint splits runs mid-word on
|
|
15
|
+
* formatting boundaries), XML entities decoded, speaker notes attached to
|
|
16
|
+
* their slide — so what an agent reads through `read_file` and what a human
|
|
17
|
+
* sees in the viewer tell the same story. Implemented independently because
|
|
18
|
+
* the backend module is Node-only (AdmZip, Buffer); this one runs on JSZip in
|
|
19
|
+
* the browser.
|
|
20
|
+
*
|
|
21
|
+
* The XML reading lives in `xmlReading.ts` and is `htmlparser2`, matching the
|
|
22
|
+
* backend's `ooxml-text.ts`. Both were hand-rolled once, on the reasoning that
|
|
23
|
+
* the only question asked is "the character content of `<a:t>` runs, grouped
|
|
24
|
+
* by paragraph". The flaw in that reasoning is that the deck is UPLOADED, so
|
|
25
|
+
* the scan has to be right about every lexical rule of XML rather than the
|
|
26
|
+
* ones PowerPoint's own output exercises — and here it runs on the reader's
|
|
27
|
+
* main thread, which they cannot get back while it spins.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export interface PptxSlide {
|
|
31
|
+
/** 1-based slide number from the package's `slideN.xml` name. */
|
|
32
|
+
number: number;
|
|
33
|
+
/** Non-empty paragraph texts, in document order. */
|
|
34
|
+
paragraphs: string[];
|
|
35
|
+
/** Speaker-note paragraphs, empty when the slide has none. */
|
|
36
|
+
notes: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Split an XML fragment into its `<{tag}>…</{tag}>` blocks by LOCAL name —
|
|
41
|
+
* the prefix is the document's choice (see {@link localElementBlocks}) — in
|
|
42
|
+
* document order. A self-closing `<{tag}/>` yields '' — an EMPTY block, which
|
|
43
|
+
* is what an empty `<a:p/>` paragraph means. See {@link localElementBlocks}
|
|
44
|
+
* for the non-nesting, quoting and linear-time guarantees.
|
|
45
|
+
*/
|
|
46
|
+
function xmlBlocks(xml: string, localTag: string): string[] {
|
|
47
|
+
return localElementBlocks(xml, [localTag]).map((e) => e.body ?? '');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The text of one paragraph: every `<a:t>` run's character content (matched
|
|
52
|
+
* by LOCAL name `t`, whatever prefix the deck bound), concatenated with NO
|
|
53
|
+
* separator — runs split mid-word, so any separator would break words apart.
|
|
54
|
+
* A self-closing `<a:t/>` is an empty run and contributes nothing.
|
|
55
|
+
*/
|
|
56
|
+
function paragraphRunText(paragraphXml: string): string {
|
|
57
|
+
return elementText(paragraphXml, 't');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Non-empty paragraph texts of one slide/notes part, in document order. */
|
|
61
|
+
function paragraphLines(xml: string): string[] {
|
|
62
|
+
const out: string[] = [];
|
|
63
|
+
for (const p of xmlBlocks(xml, 'p')) {
|
|
64
|
+
const text = paragraphRunText(p);
|
|
65
|
+
if (text.trim() !== '') out.push(text);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Decompression bounds. A pptx is a zip, and a zip can be a bomb — a few KB
|
|
72
|
+
* that inflate to gigabytes. Entries are read through JSZip's chunked
|
|
73
|
+
* internal stream and ABORTED the moment a part exceeds 50 MB uncompressed
|
|
74
|
+
* or the whole deck exceeds 200 MB (the same numbers the backend extractors
|
|
75
|
+
* enforce), so the tab never holds more than one bounded part plus a chunk.
|
|
76
|
+
* Reads are sequential for the same reason — an unbounded Promise.all over
|
|
77
|
+
* every entry would inflate the whole deck at once.
|
|
78
|
+
*/
|
|
79
|
+
const MAX_PART_BYTES = 50 * 1024 * 1024; // 50 MB uncompressed, per entry
|
|
80
|
+
const MAX_TOTAL_BYTES = 200 * 1024 * 1024; // 200 MB uncompressed, per deck
|
|
81
|
+
|
|
82
|
+
/** Aggregate inflation budget shared by every read of one deck. */
|
|
83
|
+
interface ReadBudget {
|
|
84
|
+
remaining: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** JSZip's chunked entry stream — implemented by every JSZipObject, absent from the public typings. */
|
|
88
|
+
interface EntryStream {
|
|
89
|
+
on(event: 'data', cb: (chunk: Uint8Array) => void): EntryStream;
|
|
90
|
+
on(event: 'end', cb: () => void): EntryStream;
|
|
91
|
+
on(event: 'error', cb: (e: Error) => void): EntryStream;
|
|
92
|
+
resume(): EntryStream;
|
|
93
|
+
pause(): EntryStream;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** `entry` decoded as UTF-8, read chunk-by-chunk under the per-part and aggregate caps. */
|
|
97
|
+
function readEntryBounded(entry: JSZip.JSZipObject, budget: ReadBudget): Promise<string> {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
const stream = (entry as unknown as { internalStream(type: 'uint8array'): EntryStream }).internalStream(
|
|
100
|
+
'uint8array',
|
|
101
|
+
);
|
|
102
|
+
// Each chunk is decoded AS IT ARRIVES (streaming decode holds multi-byte
|
|
103
|
+
// sequences split across chunks) and then dropped. Collecting the chunks
|
|
104
|
+
// and assembling one big buffer at the end held a near-50 MB part in
|
|
105
|
+
// memory twice — bytes and copy — before the decoder added the string.
|
|
106
|
+
const decoder = new TextDecoder();
|
|
107
|
+
let text = '';
|
|
108
|
+
let entryBytes = 0;
|
|
109
|
+
let settled = false;
|
|
110
|
+
stream
|
|
111
|
+
.on('data', (chunk) => {
|
|
112
|
+
if (settled) return;
|
|
113
|
+
entryBytes += chunk.length;
|
|
114
|
+
budget.remaining -= chunk.length;
|
|
115
|
+
if (entryBytes > MAX_PART_BYTES || budget.remaining < 0) {
|
|
116
|
+
settled = true;
|
|
117
|
+
stream.pause(); // stop inflating — nothing further is wanted
|
|
118
|
+
reject(new Error(`${entry.name} inflates past the extraction bound`));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
text += decoder.decode(chunk, { stream: true });
|
|
122
|
+
})
|
|
123
|
+
.on('error', (e) => {
|
|
124
|
+
if (settled) return;
|
|
125
|
+
settled = true;
|
|
126
|
+
reject(e);
|
|
127
|
+
})
|
|
128
|
+
.on('end', () => {
|
|
129
|
+
if (settled) return;
|
|
130
|
+
settled = true;
|
|
131
|
+
resolve(text + decoder.decode());
|
|
132
|
+
})
|
|
133
|
+
.resume();
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** OPC relationship type whose Target is a slide's speaker-notes part. */
|
|
138
|
+
const NOTES_REL_TYPE_SUFFIX = '/notesSlide';
|
|
139
|
+
|
|
140
|
+
/** Resolve an OPC relationship Target against the part's base directory. */
|
|
141
|
+
function resolveRelTarget(baseDir: string, target: string): string {
|
|
142
|
+
const parts = target.startsWith('/')
|
|
143
|
+
? target.slice(1).split('/')
|
|
144
|
+
: [...baseDir.split('/'), ...target.split('/')];
|
|
145
|
+
const out: string[] = [];
|
|
146
|
+
for (const p of parts) {
|
|
147
|
+
if (p === '' || p === '.') continue;
|
|
148
|
+
if (p === '..') out.pop();
|
|
149
|
+
else out.push(p);
|
|
150
|
+
}
|
|
151
|
+
return out.join('/');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The OPC relationships part of `partName` — `dir/_rels/base.rels`. Derived
|
|
156
|
+
* from the part NAME, never from the slide number: when a deck ships both
|
|
157
|
+
* `slide1.xml` and `slide01.xml`, the name-ordering rule below picks
|
|
158
|
+
* `slide01.xml`, whose rels part is `slide01.xml.rels`. Rebuilding the path
|
|
159
|
+
* from the number asked for `slide1.xml.rels` — a part belonging to the OTHER
|
|
160
|
+
* file — and so either lost that slide's speaker notes or attached the losing
|
|
161
|
+
* part's.
|
|
162
|
+
*/
|
|
163
|
+
function relsPartName(partName: string): string {
|
|
164
|
+
const cut = partName.lastIndexOf('/');
|
|
165
|
+
return `${partName.slice(0, cut)}/_rels/${partName.slice(cut + 1)}.rels`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The conventional notes part for a slide part — `slide01.xml` →
|
|
170
|
+
* `notesSlide01.xml`. Derived from the name for the same reason as the rels
|
|
171
|
+
* path, so a zero-padded deck's fallback lands on the matching notes part.
|
|
172
|
+
*/
|
|
173
|
+
function conventionalNotesPart(partName: string): string {
|
|
174
|
+
const base = partName.slice(partName.lastIndexOf('/') + 1);
|
|
175
|
+
return `ppt/notesSlides/notes${base[0].toUpperCase()}${base.slice(1)}`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The note paragraphs of the slide part named `slideName`. Notes are paired
|
|
180
|
+
* through that part's `.rels` (relationship type ending `notesSlide` — the
|
|
181
|
+
* package may number notes parts differently from slides); the conventional
|
|
182
|
+
* `notesSlideN.xml` name is the fallback ONLY for a slide with no rels part
|
|
183
|
+
* at all. Mirrors the backend's `extract-pptx.ts` exactly. The notes XML is
|
|
184
|
+
* parsed to its paragraph strings here and never retained.
|
|
185
|
+
*/
|
|
186
|
+
async function readNoteLines(zip: JSZip, slideName: string, budget: ReadBudget): Promise<string[]> {
|
|
187
|
+
const rels = zip.file(relsPartName(slideName));
|
|
188
|
+
let notesPart: string | undefined;
|
|
189
|
+
if (rels) {
|
|
190
|
+
const target = relationshipTarget(await readEntryBounded(rels, budget), NOTES_REL_TYPE_SUFFIX);
|
|
191
|
+
notesPart = target !== undefined ? resolveRelTarget('ppt/slides', target) : undefined;
|
|
192
|
+
} else {
|
|
193
|
+
notesPart = conventionalNotesPart(slideName);
|
|
194
|
+
}
|
|
195
|
+
if (notesPart === undefined) return [];
|
|
196
|
+
const entry = zip.file(notesPart);
|
|
197
|
+
return entry ? paragraphLines(await readEntryBounded(entry, budget)) : [];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The slide part names `presentation.xml` lists, in ITS order, or undefined
|
|
202
|
+
* when the deck has no usable list.
|
|
203
|
+
*
|
|
204
|
+
* A deck that has been REORDERED keeps its original part filenames, so
|
|
205
|
+
* `slide3.xml` may well be the first slide. The backend follows this list, and
|
|
206
|
+
* a viewer that followed filenames instead would number the same deck
|
|
207
|
+
* differently from what `read_file` reports — the one thing these twins exist
|
|
208
|
+
* to prevent.
|
|
209
|
+
*/
|
|
210
|
+
async function presentationOrder(zip: JSZip, budget: ReadBudget): Promise<string[] | undefined> {
|
|
211
|
+
const pres = zip.file('ppt/presentation.xml');
|
|
212
|
+
const rels = zip.file('ppt/_rels/presentation.xml.rels');
|
|
213
|
+
if (!pres || !rels) return undefined;
|
|
214
|
+
try {
|
|
215
|
+
// The PREFIXED id: `<p:sldId>` carries its own bare numeric `id` before
|
|
216
|
+
// the relationship reference `r:id`, so plain local-name matching read
|
|
217
|
+
// the wrong one, resolved no target, and fell back to filename order.
|
|
218
|
+
const ids = localElementBlocks(await readEntryBounded(pres, budget), ['sldId'])
|
|
219
|
+
.map((e) => prefixedAttrByLocalName(e.attributes, 'id'))
|
|
220
|
+
.filter((v): v is string => v !== undefined);
|
|
221
|
+
if (ids.length === 0) return undefined;
|
|
222
|
+
const relsXml = await readEntryBounded(rels, budget);
|
|
223
|
+
const targetById = new Map<string, string>();
|
|
224
|
+
for (const rel of localElementBlocks(relsXml, ['Relationship'])) {
|
|
225
|
+
const id = attrByLocalName(rel.attributes, 'Id');
|
|
226
|
+
const target = attrByLocalName(rel.attributes, 'Target');
|
|
227
|
+
if (id !== undefined && target !== undefined) targetById.set(id, target);
|
|
228
|
+
}
|
|
229
|
+
const names = ids
|
|
230
|
+
.map((id) => targetById.get(id))
|
|
231
|
+
.filter((t): t is string => t !== undefined)
|
|
232
|
+
.map((t) => resolveRelTarget('ppt', t));
|
|
233
|
+
return names.length > 0 ? names : undefined;
|
|
234
|
+
} catch {
|
|
235
|
+
return undefined; // an unreadable list is simply no list
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Parse `.pptx` bytes into the outline. Throws an `Error` whose message reads
|
|
241
|
+
* "could not be parsed as a .pptx (…)" — the caller shows it verbatim — when
|
|
242
|
+
* the bytes are not a zip, hold no slides, or blow the decompression bounds.
|
|
243
|
+
*
|
|
244
|
+
* Each slide/notes part is parsed into its final paragraph strings AS IT IS
|
|
245
|
+
* READ and the decompressed XML is dropped before the next part is touched —
|
|
246
|
+
* near the 200 MB aggregate cap, retaining every part's XML until the end
|
|
247
|
+
* would hold the whole inflated deck in memory at once; this way the peak is
|
|
248
|
+
* one bounded part plus the small parsed outline.
|
|
249
|
+
*/
|
|
250
|
+
export async function extractPptxOutline(bytes: ArrayBuffer | Uint8Array): Promise<PptxSlide[]> {
|
|
251
|
+
const out: PptxSlide[] = [];
|
|
252
|
+
try {
|
|
253
|
+
const zip = await JSZip.loadAsync(bytes);
|
|
254
|
+
const budget: ReadBudget = { remaining: MAX_TOTAL_BYTES };
|
|
255
|
+
const slideEntries: Array<[number, string, JSZip.JSZipObject]> = [];
|
|
256
|
+
zip.forEach((entryName, entry) => {
|
|
257
|
+
const m = /^ppt\/slides\/slide(\d+)\.xml$/.exec(entryName);
|
|
258
|
+
if (!m) return;
|
|
259
|
+
const n = parseInt(m[1], 10);
|
|
260
|
+
// A crafted name can spell more digits than a double holds exactly —
|
|
261
|
+
// `parseInt` then rounds DISTINCT names to the same number (or to
|
|
262
|
+
// Infinity), silently dropping a part or rendering "Slide Infinity".
|
|
263
|
+
// No real deck numbers slides past 2^53; such a name is not a slide.
|
|
264
|
+
if (Number.isSafeInteger(n)) slideEntries.push([n, entryName, entry]);
|
|
265
|
+
});
|
|
266
|
+
// `slide1.xml` and `slide01.xml` both parse to number 1 — one slide per
|
|
267
|
+
// NUMBER, or the outline would emit two slides with the same number and
|
|
268
|
+
// the renderer two children with the same key. The winner is the FIRST in
|
|
269
|
+
// ascending part-name order — deterministic regardless of zip entry
|
|
270
|
+
// order, and the same policy as the backend twin (`extract-pptx.ts`), so
|
|
271
|
+
// viewer and `read_file` agree on which part a number's text comes from.
|
|
272
|
+
slideEntries.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));
|
|
273
|
+
// The winner's NAME is kept, not just its bytes: the notes lookup hangs
|
|
274
|
+
// off the part name (see `relsPartName`), so a zero-padded winner must not
|
|
275
|
+
// send it back to the number.
|
|
276
|
+
const chosen = new Map<number, [string, JSZip.JSZipObject]>();
|
|
277
|
+
for (const [n, name, entry] of slideEntries) {
|
|
278
|
+
if (!chosen.has(n)) chosen.set(n, [name, entry]);
|
|
279
|
+
}
|
|
280
|
+
// Emission order mirrors the backend: the presentation's own slide list
|
|
281
|
+
// when it resolves to parts this deck actually has, filename order
|
|
282
|
+
// otherwise — and when the LIST orders the deck the numbers are POSITIONS
|
|
283
|
+
// in it, because a reordered deck's filenames no longer mean anything
|
|
284
|
+
// positional. Numbering the same deck differently from what `read_file`
|
|
285
|
+
// reports is the one thing these twins exist to prevent.
|
|
286
|
+
const numberByName = new Map<string, number>();
|
|
287
|
+
for (const [n, [name]] of chosen) numberByName.set(name, n);
|
|
288
|
+
const listed = (await presentationOrder(zip, budget)) ?? [];
|
|
289
|
+
const fromList = [
|
|
290
|
+
...new Set(
|
|
291
|
+
listed.map((name) => numberByName.get(name)).filter((n): n is number => n !== undefined),
|
|
292
|
+
),
|
|
293
|
+
];
|
|
294
|
+
const byFilename = [...chosen.keys()].sort((a, b) => a - b);
|
|
295
|
+
const positional = fromList.length > 0;
|
|
296
|
+
const order = positional
|
|
297
|
+
? [...fromList, ...byFilename.filter((n) => !fromList.includes(n))]
|
|
298
|
+
: byFilename;
|
|
299
|
+
let position = 0;
|
|
300
|
+
for (const n of order) {
|
|
301
|
+
const [name, entry] = chosen.get(n)!;
|
|
302
|
+
const paragraphs = paragraphLines(await readEntryBounded(entry, budget));
|
|
303
|
+
position += 1;
|
|
304
|
+
out.push({
|
|
305
|
+
number: positional ? position : n,
|
|
306
|
+
paragraphs,
|
|
307
|
+
notes: await readNoteLines(zip, name, budget),
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
} catch (err) {
|
|
311
|
+
throw new Error(`could not be parsed as a .pptx (${(err as Error).message})`);
|
|
312
|
+
}
|
|
313
|
+
if (out.length === 0) {
|
|
314
|
+
throw new Error('could not be parsed as a .pptx (no ppt/slides/slideN.xml inside the archive)');
|
|
315
|
+
}
|
|
316
|
+
return out;
|
|
317
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one capped body read the binary viewers share.
|
|
3
|
+
*
|
|
4
|
+
* `.xlsx`, `.pptx` and `.eml`/`.msg` all hand a WHOLE-FILE buffer to a parser
|
|
5
|
+
* with no streaming mode, so each has to refuse an oversized response before
|
|
6
|
+
* it is in the tab. That logic lived three times, in three slightly different
|
|
7
|
+
* states — every fix landed on one copy and left the others behind. It lives
|
|
8
|
+
* here once.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `res`'s bytes, or `null` once they exceed `maxBytes`.
|
|
13
|
+
*
|
|
14
|
+
* Two bounds, because one is not enough. A DECLARED Content-Length over the
|
|
15
|
+
* cap is refused before a byte is buffered, and `abort` ends the request so
|
|
16
|
+
* the browser stops receiving a file nobody will look at — returning alone
|
|
17
|
+
* left the connection streaming for as long as the view stayed mounted. But
|
|
18
|
+
* Content-Length can be absent (chunked) or understate the body, so the
|
|
19
|
+
* stream is also counted as it arrives and abandoned the moment it crosses
|
|
20
|
+
* the cap. The early exit aborts the CONTROLLER rather than cancelling
|
|
21
|
+
* `res.body`: cancelling a stream that has already errored REJECTS, and that
|
|
22
|
+
* rejection would carry the caller into its generic error path instead of the
|
|
23
|
+
* oversized one this returns `null` for.
|
|
24
|
+
*
|
|
25
|
+
* Where the server declared a length the bytes are copied ONCE — each chunk
|
|
26
|
+
* lands directly in a buffer of exactly that size, rather than being retained
|
|
27
|
+
* as a chunk list and then copied AGAIN into a merged buffer, which peaked at
|
|
28
|
+
* twice the file. (Preallocating `maxBytes` up front would bound that peak
|
|
29
|
+
* too, and would charge a 4 KB message the whole 25/50/200 MB cap; the
|
|
30
|
+
* declared length is the size that is actually right. A server that overstates
|
|
31
|
+
* its length costs that overstatement instead — bounded by `maxBytes`, which
|
|
32
|
+
* it could have made us allocate by simply sending the bytes.) An undeclared
|
|
33
|
+
* or understated body falls back to the chunk list.
|
|
34
|
+
*
|
|
35
|
+
* Falls back to `arrayBuffer()` (capped after the fact) where the response has
|
|
36
|
+
* no stream at all, e.g. the test DOM's mocked responses.
|
|
37
|
+
*/
|
|
38
|
+
export async function readBodyCapped(
|
|
39
|
+
res: Response,
|
|
40
|
+
maxBytes: number,
|
|
41
|
+
abort: AbortController,
|
|
42
|
+
): Promise<ArrayBuffer | null> {
|
|
43
|
+
const declared = Number(res.headers?.get('content-length') ?? '');
|
|
44
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
45
|
+
abort.abort();
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
const body = res.body;
|
|
49
|
+
if (!body) {
|
|
50
|
+
const buffer = await res.arrayBuffer();
|
|
51
|
+
return buffer.byteLength > maxBytes ? null : buffer;
|
|
52
|
+
}
|
|
53
|
+
const reader = body.getReader();
|
|
54
|
+
// Preallocated only against a length the server actually declared; a
|
|
55
|
+
// fractional or absent one leaves this null and takes the chunk path.
|
|
56
|
+
let sized = Number.isInteger(declared) && declared > 0 ? new Uint8Array(declared) : null;
|
|
57
|
+
const chunks: Uint8Array[] = [];
|
|
58
|
+
let received = 0;
|
|
59
|
+
for (;;) {
|
|
60
|
+
const { done, value } = await reader.read();
|
|
61
|
+
if (done) break;
|
|
62
|
+
const offset = received;
|
|
63
|
+
received += value.byteLength;
|
|
64
|
+
if (received > maxBytes) {
|
|
65
|
+
reader.cancel().catch(() => {});
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
if (sized !== null && received <= sized.byteLength) {
|
|
69
|
+
sized.set(value, offset);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (sized !== null) {
|
|
73
|
+
// Content-Length understated the body — a decoded transfer arrives
|
|
74
|
+
// longer than the encoded length that was announced. What has landed so
|
|
75
|
+
// far becomes the first chunk and the rest accumulates behind it. Copied
|
|
76
|
+
// with `slice`, not viewed with `subarray`: a view would pin the whole
|
|
77
|
+
// preallocated buffer for as long as the chunk list lives, holding the
|
|
78
|
+
// declared length on top of the chunks and the merge it feeds.
|
|
79
|
+
chunks.push(sized.slice(0, offset));
|
|
80
|
+
sized = null;
|
|
81
|
+
}
|
|
82
|
+
chunks.push(value);
|
|
83
|
+
}
|
|
84
|
+
if (sized !== null) {
|
|
85
|
+
// Exactly as declared: the buffer already IS the answer. A short body (the
|
|
86
|
+
// server overstated) is trimmed rather than handed over with the trailing
|
|
87
|
+
// zeros nobody sent.
|
|
88
|
+
return received === sized.byteLength ? sized.buffer : sized.buffer.slice(0, received);
|
|
89
|
+
}
|
|
90
|
+
const merged = new Uint8Array(received);
|
|
91
|
+
let offset = 0;
|
|
92
|
+
for (const chunk of chunks) {
|
|
93
|
+
merged.set(chunk, offset);
|
|
94
|
+
offset += chunk.byteLength;
|
|
95
|
+
}
|
|
96
|
+
return merged.buffer;
|
|
97
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ambient declaration for Vite's `?url` asset imports (`import workerUrl from
|
|
3
|
+
* 'pdfjs-dist/build/pdf.worker.min.mjs?url'`). The enterprise app gets this
|
|
4
|
+
* from Vite's `vite/client` types; this package is published as raw source and
|
|
5
|
+
* typechecked without Vite, so it declares the module shape itself (harmless
|
|
6
|
+
* alongside vite/client in consumers — same arrangement as the CSS
|
|
7
|
+
* side-effect declaration next door).
|
|
8
|
+
*/
|
|
9
|
+
declare module '*?url' {
|
|
10
|
+
const url: string;
|
|
11
|
+
export default url;
|
|
12
|
+
}
|