Skip to content
Draft
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
7 changes: 2 additions & 5 deletions src/linter/xmlTemplate/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {getLogger} from "@ui5/logger";
import {MESSAGE} from "../messages.js";
import {loadApiExtract, ApiExtract} from "../../utils/ApiExtract.js";
import ControllerByIdInfo from "./ControllerByIdInfo.js";
import {parseXml, initSaxWasm} from "../../utils/xmlParser.js";
import {parseXml} from "../../utils/xmlParser.js";

const log = getLogger("linter:xmlTemplate:transpiler");

Expand Down Expand Up @@ -40,10 +40,7 @@ async function init() {
}
const taskEnd = taskStart("XML Transpiler initialization");

return initializing = Promise.all([
loadApiExtract(),
initSaxWasm(),
]).then(([apiExtractRes]) => {
return initializing = loadApiExtract().then((apiExtractRes) => {
apiExtract = apiExtractRes;
taskEnd();
});
Expand Down
32 changes: 11 additions & 21 deletions src/utils/xmlParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type {ReadStream} from "node:fs";
import {SaxEventType, SAXParser, Tag, Text} from "sax-wasm";
import {SaxEvent, SaxEventType, SAXParser, Tag, Text} from "sax-wasm";
import {finished} from "node:stream/promises";
import fs from "node:fs/promises";
import {createRequire} from "node:module";
import {Directive} from "../linter/LinterContext.js";
import {Readable} from "node:stream";
const require = createRequire(import.meta.url);

export function isSaxParserToJSON(tag: unknown): tag is Tag {
Expand Down Expand Up @@ -50,43 +51,32 @@ export function extractDirective(comment: Text): Directive | undefined {
};
}

let saxWasmBuffer: Buffer;
let saxWasmBuffer: Uint8Array;
export async function initSaxWasm() {
if (!saxWasmBuffer) {
const saxPath = require.resolve("sax-wasm/lib/sax-wasm.wasm");
saxWasmBuffer = await fs.readFile(saxPath);
saxWasmBuffer = await fs.readFile(saxPath) as Uint8Array;
}

return saxWasmBuffer;
}

export async function parseXml(
contentStream: ReadStream, parseHandler: typeof SAXParser.prototype.eventHandler,
contentStream: ReadStream, parseHandler: (event: SaxEvent[0], detail: SaxEvent[1]) => void,
events: number = SaxEventType.CloseTag | SaxEventType.OpenTag | SaxEventType.Comment) {
const saxWasmBuffer = await initSaxWasm();
const saxParser = new SAXParser(events);

saxParser.eventHandler = parseHandler;

// Instantiate and prepare the wasm for parsing
if (!await saxParser.prepareWasm(saxWasmBuffer)) {
throw new Error("Unknown error during WASM Initialization");
}

// Start the stream
contentStream.on("data", (chunk: Uint8Array) => {
try {
saxParser.write(chunk);
} catch (err) {
if (err instanceof Error) {
// In case of an error, destroy the content stream to make the
// error bubble up to our callers
contentStream.destroy(err);
} else {
throw err;
}
}
});
const webContentStream = Readable.toWeb(contentStream) as ReadableStream<Uint8Array>;

for await (const [event, detail] of saxParser.parse(webContentStream.getReader())) {
parseHandler(event, detail);
}

await finished(contentStream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is finished still required? I would expect that the iterator is exhaustive? The sax-wasm README does not call finished in the ReadableStream example...

BTW: toWeb is still experimental in Node 23 (according to their docs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess not but I thought it shouldn't hurt leaving it there. But good point about the experimental API. Then we shouldn't use it, yet.

saxParser.end();
}