1- import { SaxEventType , SAXParser , Tag as SaxTag } from "sax-wasm" ;
1+ import { SaxEventType , Tag as SaxTag } from "sax-wasm" ;
22import { ReadStream } from "node:fs" ;
3- import fs from "node:fs/promises" ;
4- import { finished } from "node:stream/promises" ;
53import { taskStart } from "../../utils/perf.js" ;
64import LinterContext , { TranspileResult } from "../LinterContext.js" ;
75import Parser from "./Parser.js" ;
86import { getLogger } from "@ui5/logger" ;
9- import { createRequire } from "node:module" ;
107import { MESSAGE } from "../messages.js" ;
11- import { loadApiExtract , ApiExtract } from "../../utils/ApiExtract.js" ;
8+ import { loadApiExtract } from "../../utils/ApiExtract.js" ;
129import ControllerByIdInfo from "./ControllerByIdInfo.js" ;
13- const require = createRequire ( import . meta . url ) ;
10+ import { parseXML } from "../../utils/xmlParser.js" ;
1411
1512const log = getLogger ( "linter:xmlTemplate:transpiler" ) ;
1613
17- let saxWasmBuffer : Buffer ;
18- let apiExtract : ApiExtract ;
14+ const apiExtract = await loadApiExtract ( ) ;
1915
2016export 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-
5934async 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}
0 commit comments