mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* build(deps): add fflate, to zip and unzip packages in place of JSZip Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * perf: hash with docx's own SHA-1 and SHA-256, in place of hash.js hash.js was about 6.5 KB of a browser bundle, minified and gzipped, as its whole library is bundled for the two hashes docx uses: SHA-1 for the names of media files (`hashedId`) and SHA-256 for the password that protects a document. It hasn't had a release since 2018, and brought `inherits` and `minimalistic-assert` with it. The platforms' own hashes can't replace it, as browsers only hash asynchronously and documents are written synchronously. The hashes are the same: they match FIPS 180-4's examples and node:crypto, and the password hash Word accepts is unchanged. They are also faster: hashing a 5 MB image takes about half the time, and a password's 100,000 rounds about 130 ms rather than 155 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * perf: zip and unzip with fflate, in place of JSZip JSZip was about 30 KB of a browser bundle, minified and gzipped, as its prebuilt browser file brings its own copy of pako and more. fflate's zipSync and unzipSync, the only parts docx needs, are about 6 KB. Together with the hashes, the bundle of a page that packs a document goes from 123 KB to 94 KB. Nothing changes for users: - Packer and patchDocument give each output type as JSZip gave it, including "string" and "binarystring" as a character for each byte, a Blob with the .docx or .docm MIME type, and the same errors for "nodebuffer" without Buffer, a Blob without Blob, and "text", which JSZip never gave. - patchDocument and patchDetector still take a JSZip instance, which is read through its generateAsync, so the types no longer import jszip. They also still take the other forms JSZip read: a string with a character for each byte, an array of bytes, a Blob, an ArrayBuffer and bytes from another realm. - A part's text keeps a byte order mark at its start, as it did. - Packer.toStream still compiles synchronously, so it throws as before, and gives the document a turn of the event loop later. Zipping is now synchronous, and faster: packing a document with 5,000 paragraphs and 5 MB of images takes 83 ms rather than 182 ms. The zip files no longer have folder entries, which JSZip added for each folder. Word doesn't write them, and nothing reads them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: test the packer and the patcher's zip reading without JSZip These specs mocked JSZip or read the JSZip instance the compiler returned. They now build real zip files and read what the Packer and the patcher give, so they test the zipping too: - loadZip reads every form a template can be given in, a JSZip instance through its generateAsync, and leaves out folders - Packer.toStream throws when the document can't be written, and gives it even to a listener added after an await - toBlob, toArrayBuffer and toStream give real zip files, rather than what a mocked compiler gave The counts of the files in a package no longer include the folder entries JSZip added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: test zipping, unzipping and each output type of a zip file Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * chore: read and write .docx files in the scripts with fflate, in place of JSZip docx is replacing JSZip with fflate (#3834), and jszip is leaving package.json, so the scripts that read documents (compare-layout.ts, the layout probes' line checkers) and the probes that edit docx's output before writing it use fflate's unzipSync and zipSync. They leave out folder entries when they unzip, as Word's own files have none, and zip at level 6. The written documents have the same files in the same order, with the same contents, as before. check-api.ts's comment no longer says the built declarations import jszip. Those of releases up to 9.14.0 still do, and the import, which no longer resolves, is any there, which the check accepts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: load Node's types from docx's declarations, which JSZip's used to load The declarations use Node's types, such as Buffer and NodeJS.ReadableStream. They had them because they imported JSZip's declarations, which reference Node's types. Without JSZip, a project that doesn't load every @types package, such as one with `types` set, or TypeScript 6's default, would find no Buffer in docx's types, and docx's own optional entries didn't build. The reference is kept in the built declarations with `preserve="true"`, as TypeScript otherwise leaves it out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: read and make zip files with fflate in the specs of charts, embedded files, macros, shapes and the core, in place of JSZip jszip is leaving docx's dependencies, so these specs read packed documents and make changed templates with the helpers in tests/zip, which unzip and zip with fflate, as docx now does. Each test checks what it checked before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: read and make zip files with fflate in the specs of docx/html, docx/layout and the patcher, in place of JSZip jszip is leaving docx's dependencies, as docx now zips and unzips with fflate, so these specs read packed documents and make templates with the helpers in src/tests/zip.ts. Templates that were JSZip instances are now zip files' bytes, and the specs read text and bytes from the unzipped files directly, so some helpers that only awaited JSZip are no longer asynchronous. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: patch real zip files in patchDocument's spec, rather than mocking JSZip JSZip is leaving the dependencies. The spec mocked JSZip.loadAsync and passed an empty Buffer as the template, so patchDocument never unzipped anything. Each template is now a real zip file, made with the specs' zip helper, so the tests exercise the unzipping too. The tests of a template opened with JSZip pass an object with generateAsync, which is how a JSZip instance is read now. The path keys of the templates are covered by one file-level naming-convention exception, in place of the comments on each patch key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * build(deps): remove jszip, as nothing uses it now Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: refuse a zip file's entry of the wrong size before taking memory for it, and inflate only what patchDetector reads fflate takes room for the size a zip file gives an entry before inflating it, so a template of a few hundred bytes could make docx take 4 GB, which fails in a browser or a process with a memory limit. JSZip refused such an entry ("uncompressed data size mismatch"). An entry whose given size is more than DEFLATE can inflate its data to (1032 times) is now refused before fflate takes the room, and one that inflates to less than its given size after. JSZip also inflated only the entries that were read, so patchDetector, which reads only the XML parts in word/, never inflated a template's images or embedded files. unzipFiles and loadZip now take the paths to read, and patchDetector gives them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: refuse a zip file whose entries claim more compressed data than the zip file holds, before taking memory for them The size an entry inflates to was checked against its compressed size, but that is also read from the zip file, so an entry could claim compressed data larger than the zip file and pass. The entries docx reads now have their compressed sizes added up, and a total larger than the zip file is refused before fflate takes room for the entry. Together with DEFLATE's ratio, that keeps the room taken to 1032 times the zip file's size, as a real zip bomb's is. The tests damage the entry's data too, so only a refusal before inflating gives their error: without either check, each fails with fflate's own error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>