Skip to content

Commit e4bc823

Browse files
committed
refactor: Update API usage of sax-wasm
- Updating to the new recommended API of sax-wasm - Reuse parseXML in xml transpiler
1 parent e09db01 commit e4bc823

File tree

2 files changed

+15
-75
lines changed

2 files changed

+15
-75
lines changed
Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
import {SaxEventType, SAXParser, Tag as SaxTag} from "sax-wasm";
1+
import {SaxEventType, Tag as SaxTag} from "sax-wasm";
22
import {ReadStream} from "node:fs";
3-
import fs from "node:fs/promises";
4-
import {finished} from "node:stream/promises";
53
import {taskStart} from "../../utils/perf.js";
64
import LinterContext, {TranspileResult} from "../LinterContext.js";
75
import Parser from "./Parser.js";
86
import {getLogger} from "@ui5/logger";
9-
import {createRequire} from "node:module";
107
import {MESSAGE} from "../messages.js";
11-
import {loadApiExtract, ApiExtract} from "../../utils/ApiExtract.js";
8+
import {loadApiExtract} from "../../utils/ApiExtract.js";
129
import ControllerByIdInfo from "./ControllerByIdInfo.js";
13-
const require = createRequire(import.meta.url);
10+
import {parseXML} from "../../utils/xmlParser.js";
1411

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

17-
let saxWasmBuffer: Buffer;
18-
let apiExtract: ApiExtract;
14+
const apiExtract = await loadApiExtract();
1915

2016
export default async function transpileXml(
2117
resourcePath: string, contentStream: ReadStream, context: LinterContext, controllerByIdInfo: ControllerByIdInfo
2218
): Promise<TranspileResult | undefined> {
23-
await init();
2419
try {
2520
const taskEnd = taskStart("Transpile XML", resourcePath, true);
2621
const res = await transpileXmlToJs(resourcePath, contentStream, context, controllerByIdInfo);
@@ -36,65 +31,20 @@ export default async function transpileXml(
3631
}
3732
}
3833

39-
let initializing: Promise<void>;
40-
async function init() {
41-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
42-
if (initializing) {
43-
return initializing;
44-
}
45-
// Get the path to the WebAssembly binary and load it
46-
const saxPath = require.resolve("sax-wasm/lib/sax-wasm.wasm");
47-
const taskEnd = taskStart("XML Transpiler initialization");
48-
49-
return initializing = Promise.all([
50-
fs.readFile(saxPath),
51-
loadApiExtract(),
52-
]).then((results) => {
53-
saxWasmBuffer = results[0];
54-
apiExtract = results[1];
55-
taskEnd();
56-
});
57-
}
58-
5934
async function transpileXmlToJs(
6035
resourcePath: string, contentStream: ReadStream, context: LinterContext, controllerByIdInfo: ControllerByIdInfo
6136
): Promise<TranspileResult> {
6237
const parser = new Parser(resourcePath, apiExtract, context, controllerByIdInfo);
6338

64-
// Initialize parser
65-
const saxParser = new SAXParser(SaxEventType.OpenTag | SaxEventType.CloseTag);
66-
67-
saxParser.eventHandler = (event, tag) => {
68-
if (tag instanceof SaxTag) {
39+
await parseXML(contentStream, (event, detail) => {
40+
if (detail instanceof SaxTag) {
6941
if (event === SaxEventType.OpenTag) {
70-
parser.pushTag(tag.toJSON() as SaxTag);
42+
parser.pushTag(detail.toJSON() as SaxTag);
7143
} else if (event === SaxEventType.CloseTag) {
72-
parser.popTag(tag.toJSON() as SaxTag);
73-
}
74-
}
75-
};
76-
77-
// Instantiate and prepare the wasm for parsing
78-
if (!await saxParser.prepareWasm(saxWasmBuffer)) {
79-
throw new Error("Unknown error during WASM Initialization");
80-
}
81-
82-
// stream from a file in the current directory
83-
contentStream.on("data", (chunk: Uint8Array) => {
84-
try {
85-
saxParser.write(chunk);
86-
} catch (err) {
87-
if (err instanceof Error) {
88-
// In case of an error, destroy the content stream to make the
89-
// error bubble up to our callers
90-
contentStream.destroy(err);
91-
} else {
92-
throw err;
44+
parser.popTag(detail.toJSON() as SaxTag);
9345
}
9446
}
9547
});
96-
await finished(contentStream);
97-
saxParser.end();
9848

9949
return parser.generate();
10050
}

src/utils/xmlParser.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Detail, Reader, 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";
6+
import {Readable} from "node:stream";
67
const require = createRequire(import.meta.url);
78

89
export function isSaxParserToJSON(tag: unknown): tag is Tag {
@@ -38,27 +39,16 @@ export async function parseXML(
3839
const saxWasmBuffer = await initSaxWasm();
3940
const saxParser = new SAXParser(SaxEventType.CloseTag + SaxEventType.OpenTag);
4041

41-
saxParser.eventHandler = parseHandler;
42-
4342
// Instantiate and prepare the wasm for parsing
4443
if (!await saxParser.prepareWasm(saxWasmBuffer)) {
4544
throw new Error("Unknown error during WASM Initialization");
4645
}
4746

48-
// stream from a file in the current directory
49-
contentStream.on("data", (chunk: Uint8Array) => {
50-
try {
51-
saxParser.write(chunk);
52-
} catch (err) {
53-
if (err instanceof Error) {
54-
// In case of an error, destroy the content stream to make the
55-
// error bubble up to our callers
56-
contentStream.destroy(err);
57-
} else {
58-
throw err;
59-
}
60-
}
61-
});
47+
const webContentStream = Readable.toWeb(contentStream);
48+
49+
for await (const [event, detail] of saxParser.parse(webContentStream.getReader())) {
50+
parseHandler(event, detail);
51+
}
52+
6253
await finished(contentStream);
63-
saxParser.end();
6454
}

0 commit comments

Comments
 (0)