Skip to content

Commit 1f84f7c

Browse files
committed
refactor: Update API usage of sax-wasm
Updating to the new recommended API of sax-wasm. This also prevents TypeScript 5.9 errors caused by the used node types which are pinned to the minimum supported version of Node.js.
1 parent d4025d8 commit 1f84f7c

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

src/linter/xmlTemplate/transpiler.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {getLogger} from "@ui5/logger";
77
import {MESSAGE} from "../messages.js";
88
import {loadApiExtract, ApiExtract} from "../../utils/ApiExtract.js";
99
import ControllerByIdInfo from "./ControllerByIdInfo.js";
10-
import {parseXml, initSaxWasm} from "../../utils/xmlParser.js";
10+
import {parseXml} from "../../utils/xmlParser.js";
1111

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

@@ -40,10 +40,7 @@ async function init() {
4040
}
4141
const taskEnd = taskStart("XML Transpiler initialization");
4242

43-
return initializing = Promise.all([
44-
loadApiExtract(),
45-
initSaxWasm(),
46-
]).then(([apiExtractRes]) => {
43+
return initializing = loadApiExtract().then((apiExtractRes) => {
4744
apiExtract = apiExtractRes;
4845
taskEnd();
4946
});

src/utils/xmlParser.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type {ReadStream} from "node:fs";
2-
import {SaxEventType, SAXParser, Tag, Text} from "sax-wasm";
2+
import {SaxEvent, SaxEventType, SAXParser, Tag, Text} from "sax-wasm";
33
import {finished} from "node:stream/promises";
44
import fs from "node:fs/promises";
55
import {createRequire} from "node:module";
66
import {Directive} from "../linter/LinterContext.js";
7+
import {Readable} from "node:stream";
78
const require = createRequire(import.meta.url);
89

910
export function isSaxParserToJSON(tag: unknown): tag is Tag {
@@ -50,43 +51,32 @@ export function extractDirective(comment: Text): Directive | undefined {
5051
};
5152
}
5253

53-
let saxWasmBuffer: Buffer;
54+
let saxWasmBuffer: Uint8Array;
5455
export async function initSaxWasm() {
5556
if (!saxWasmBuffer) {
5657
const saxPath = require.resolve("sax-wasm/lib/sax-wasm.wasm");
57-
saxWasmBuffer = await fs.readFile(saxPath);
58+
saxWasmBuffer = await fs.readFile(saxPath) as Uint8Array;
5859
}
5960

6061
return saxWasmBuffer;
6162
}
6263

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

69-
saxParser.eventHandler = parseHandler;
70-
7170
// Instantiate and prepare the wasm for parsing
7271
if (!await saxParser.prepareWasm(saxWasmBuffer)) {
7372
throw new Error("Unknown error during WASM Initialization");
7473
}
7574

76-
// Start the stream
77-
contentStream.on("data", (chunk: Uint8Array) => {
78-
try {
79-
saxParser.write(chunk);
80-
} catch (err) {
81-
if (err instanceof Error) {
82-
// In case of an error, destroy the content stream to make the
83-
// error bubble up to our callers
84-
contentStream.destroy(err);
85-
} else {
86-
throw err;
87-
}
88-
}
89-
});
75+
const webContentStream = Readable.toWeb(contentStream) as ReadableStream<Uint8Array>;
76+
77+
for await (const [event, detail] of saxParser.parse(webContentStream.getReader())) {
78+
parseHandler(event, detail);
79+
}
80+
9081
await finished(contentStream);
91-
saxParser.end();
9282
}

0 commit comments

Comments
 (0)