Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User/mabezeb/fix encoding detection #113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 25 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 30 additions & 3 deletions src/utils/pqUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const getDataMashupFile = async (zip: JSZip): Promise<CustomXmlFile> => {
return mashup;
};

const getCustomXmlFile = async (zip: JSZip, url: string, encoding: BufferEncoding = "utf16le"): Promise<CustomXmlFile> => {
const getCustomXmlFile = async (zip: JSZip, url: string): Promise<CustomXmlFile> => {
const parser: DOMParser = new DOMParser();
const itemsArray = await zip.file(/customXml\/item\d.xml/);

Expand All @@ -65,8 +65,14 @@ const getCustomXmlFile = async (zip: JSZip, url: string, encoding: BufferEncodin
break;
}

xmlString = Buffer.from(xmlValue)
.toString(encoding)
const buffer: Buffer = Buffer.from(xmlValue);
const encoding: string | null = detectEncoding(xmlValue);
if (!encoding){
throw new Error("Failed to detect xml encoding")
}

xmlString = buffer
.toString(encoding as BufferEncoding)
.replace(/^\ufeff/, "");
const doc: Document = parser.parseFromString(xmlString, "text/xml");

Expand Down Expand Up @@ -111,6 +117,27 @@ const validateQueryName = (queryName: string): void => {
throw new Error(EmptyQueryNameErr);
}
};

const detectEncoding = (xmlBytes: Uint8Array): string | null => {
if (!xmlBytes){
return null;
}

if (xmlBytes.length >= 3 && xmlBytes[0] === 0xEF && xmlBytes[1] === 0xBB && xmlBytes[2] === 0xBF) {
return 'utf-8';
}
mabezen marked this conversation as resolved.
Show resolved Hide resolved
if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFF && xmlBytes[1] === 0xFE) {
return 'utf-16le';
}

if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFE && xmlBytes[1] === 0xFF) {
return 'utf-16be';
}

// Default to utf-8, that not required a BOM for encoding.
return 'utf-8';
}

export default {
getBase64,
setBase64,
Expand Down
2 changes: 1 addition & 1 deletion tests/workbookQueryTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Single query template tests", () => {
});

test("ConnectedWorkbook XML exists as item1.xml", async () => {
const { found, path, xmlString } = await pqUtils.getCustomXmlFile(defaultZipFile, URLS.CONNECTED_WORKBOOK, "utf-8");
const { found, path, xmlString } = await pqUtils.getCustomXmlFile(defaultZipFile, URLS.CONNECTED_WORKBOOK);

expect(found).toBeTruthy();
expect(xmlString).toEqual(connectedWorkbookXmlMock);
Expand Down