Fix: Use browser compatable Buffer (#3057)

* Fix: Use browser compatable Buffer

* Update src/patcher/from-docx.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Dolan
2025-04-18 00:02:21 +01:00
committed by GitHub
parent fe7a75b754
commit 44ed2d6943
3 changed files with 18 additions and 6 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "docx",
"version": "9.4.0",
"version": "9.4.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "docx",
"version": "9.4.0",
"version": "9.4.1",
"license": "MIT",
"dependencies": {
"@types/node": "^22.7.5",

View File

@ -1,6 +1,6 @@
{
"name": "docx",
"version": "9.4.0",
"version": "9.4.1",
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
"type": "module",
"main": "dist/index.umd.cjs",

View File

@ -63,8 +63,20 @@ export type PatchDocumentOptions<T extends PatchDocumentOutputType = PatchDocume
};
const imageReplacer = new ImageReplacer();
const UTF16LE = Buffer.from([0xff, 0xfe]);
const UTF16BE = Buffer.from([0xfe, 0xff]);
const UTF16LE = new Uint8Array([0xff, 0xfe]);
const UTF16BE = new Uint8Array([0xfe, 0xff]);
const compareByteArrays = (a: Uint8Array, b: Uint8Array): boolean => {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
};
export const patchDocument = async <T extends PatchDocumentOutputType = PatchDocumentOutputType>({
outputType,
@ -96,7 +108,7 @@ export const patchDocument = async <T extends PatchDocumentOutputType = PatchDoc
for (const [key, value] of Object.entries(zipContent.files)) {
const binaryValue = await value.async("uint8array");
const startBytes = binaryValue.slice(0, 2);
if (UTF16LE.equals(startBytes) || UTF16BE.equals(startBytes)) {
if (compareByteArrays(startBytes, UTF16LE) || compareByteArrays(startBytes, UTF16BE)) {
binaryContentMap.set(key, binaryValue);
continue;
}