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

xml: use fast-xml-parser #66

Merged
merged 1 commit into from
Mar 9, 2025
Merged
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
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
"@types/w3c-web-usb": "^1.0.10"
},
"//dependencies": {
"crc-32": "crc32s are used in the gpt header calculations"
"crc-32": "crc32s are used in the gpt header calculations",
"fast-xml-parser": "parse XML to JS object, fast, works in browser and node"
},
"dependencies": {
"crc-32": "^1.2.2"
"crc-32": "^1.2.2",
"fast-xml-parser": "^5.0.8"
},
"//peerDependencies": {
"typescript": "does type checking, generates JS bundles and type declarations for language servers"
Expand Down
30 changes: 19 additions & 11 deletions src/xml.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { XMLParser } from "fast-xml-parser";

/**
* @param {string} tagName
* @param {Record<string, any>} [attributes={}]
* @returns {string}
*/
export function toXml(tagName, attributes = {}) {
const attrs = Object.entries(attributes).map(([key, value]) => `${key}="${value}"`).join(" ");
return `<?xml version="1.0" ?><data><${tagName}${attrs ? ` ${attrs}` : ''} /></data>`;
return `<?xml version="1.0" ?><data><${tagName}${attrs ? ` ${attrs}` : ""} /></data>`;
}

export class xmlParser {
decoder = new TextDecoder();
parser = new DOMParser();
parser = new XMLParser({
attributeNamePrefix: "",
htmlEntities: true,
ignoreAttributes: false,
processEntities: true,
trimValues: false,
});

/**
* @param {Uint8Array} input
* @yields {Document[]}
*/
* #parseXmlDocuments(input) {
for (const xml of this.decoder.decode(input).split("<?xml")) {
yield this.parser.parseFromString(`<?xml${xml}`, "text/xml");
if (!xml) continue;
yield this.parser.parse(`<?xml${xml}`).data;
}
}

Expand All @@ -29,9 +38,7 @@ export class xmlParser {
getResponse(input) {
const content = {};
for (const doc of this.#parseXmlDocuments(input)) {
for (const el of doc.querySelectorAll("response")) {
for (const attr of el.attributes) content[attr.name] = attr.value;
}
Object.assign(content, doc.response);
}
return content;
}
Expand All @@ -43,11 +50,12 @@ export class xmlParser {
getLog(input) {
const data = [];
for (const doc of this.#parseXmlDocuments(input)) {
for (const el of doc.querySelectorAll("log")) {
for (const attr of el.attributes) {
if (attr.name !== "value") continue;
data.push(attr.value);
break;
if ("log" in doc) {
if (Array.isArray(doc.log)) {
for (const log of doc.log)
if ("value" in log) data.push(log.value);
} else if ("value" in doc.log) {
data.push(doc.log.value);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/xml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ describe("xmlParser", () => {
});
});

// TODO: unclear whether this scenario occurs
test.skip("parse multiple response documents", () => {
// unclear whether this scenario occurs
test("parse multiple response documents", () => {
const xml = `<?xml version="1.0" ?><data><response value="ACK"/></data>
<?xml version="1.0" ?><data><response value="DONE"/></data>`;
const result = parser.getResponse(encoder.encode(xml));
Expand Down