Skip to content

Commit 44ed2d6

Browse files
dolanmiuCopilot
andauthored
Fix: Use browser compatable Buffer (#3057)
* Fix: Use browser compatable Buffer * Update src/patcher/from-docx.ts Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent fe7a75b commit 44ed2d6

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "docx",
3-
"version": "9.4.0",
3+
"version": "9.4.1",
44
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
55
"type": "module",
66
"main": "dist/index.umd.cjs",

src/patcher/from-docx.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ export type PatchDocumentOptions<T extends PatchDocumentOutputType = PatchDocume
6363
};
6464

6565
const imageReplacer = new ImageReplacer();
66-
const UTF16LE = Buffer.from([0xff, 0xfe]);
67-
const UTF16BE = Buffer.from([0xfe, 0xff]);
66+
const UTF16LE = new Uint8Array([0xff, 0xfe]);
67+
const UTF16BE = new Uint8Array([0xfe, 0xff]);
68+
69+
const compareByteArrays = (a: Uint8Array, b: Uint8Array): boolean => {
70+
if (a.length !== b.length) {
71+
return false;
72+
}
73+
for (let i = 0; i < a.length; i++) {
74+
if (a[i] !== b[i]) {
75+
return false;
76+
}
77+
}
78+
return true;
79+
};
6880

6981
export const patchDocument = async <T extends PatchDocumentOutputType = PatchDocumentOutputType>({
7082
outputType,
@@ -96,7 +108,7 @@ export const patchDocument = async <T extends PatchDocumentOutputType = PatchDoc
96108
for (const [key, value] of Object.entries(zipContent.files)) {
97109
const binaryValue = await value.async("uint8array");
98110
const startBytes = binaryValue.slice(0, 2);
99-
if (UTF16LE.equals(startBytes) || UTF16BE.equals(startBytes)) {
111+
if (compareByteArrays(startBytes, UTF16LE) || compareByteArrays(startBytes, UTF16BE)) {
100112
binaryContentMap.set(key, binaryValue);
101113
continue;
102114
}

0 commit comments

Comments
 (0)