node-opcua-xml2json 2.176.0 → 2.181.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/source/definition_parser.d.ts +1 -1
- package/dist/source/definition_parser.js +3 -3
- package/dist/source/definition_parser.js.map +1 -1
- package/dist/source/fragment_cloner.d.ts +5 -5
- package/dist/source/fragment_cloner.js +11 -8
- package/dist/source/fragment_cloner.js.map +1 -1
- package/dist/source/fragment_cloner_parser.d.ts +2 -2
- package/dist/source/fragment_cloner_parser.js +7 -5
- package/dist/source/fragment_cloner_parser.js.map +1 -1
- package/dist/source/index.d.ts +5 -5
- package/dist/source/index.js +5 -5
- package/dist/source/index.js.map +1 -1
- package/dist/source/nodejs/xml2json_fs.d.ts +2 -2
- package/dist/source/nodejs/xml2json_fs.js +4 -4
- package/dist/source/nodejs/xml2json_fs.js.map +1 -1
- package/dist/source/thirdparties/escape.js +9 -14
- package/dist/source/thirdparties/escape.js.map +1 -1
- package/dist/source/thirdparties/parser/lts.d.ts +18 -2
- package/dist/source/thirdparties/parser/lts.js +110 -21
- package/dist/source/thirdparties/parser/lts.js.map +1 -1
- package/dist/source/xml2Json_pojo_tools.d.ts +1 -1
- package/dist/source/xml2Json_pojo_tools.js +2 -2
- package/dist/source/xml2Json_pojo_tools.js.map +1 -1
- package/dist/source/xml2json.d.ts +22 -38
- package/dist/source/xml2json.js +189 -86
- package/dist/source/xml2json.js.map +1 -1
- package/dist/source/xml2json_pojo.d.ts +2 -2
- package/dist/source/xml2json_pojo.js +9 -6
- package/dist/source/xml2json_pojo.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/source/definition_parser.ts +9 -5
- package/source/fragment_cloner.ts +8 -7
- package/source/fragment_cloner_parser.ts +8 -6
- package/source/index.ts +5 -5
- package/source/nodejs/xml2json_fs.ts +3 -3
- package/source/thirdparties/escape.ts +9 -20
- package/source/thirdparties/parser/lts.ts +133 -38
- package/source/xml2Json_pojo_tools.ts +3 -5
- package/source/xml2json.ts +219 -98
- package/source/xml2json_pojo.ts +8 -5
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { unescapeXML } from "../escape";
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import type { XmlAttributes } from "../../xml2json.js";
|
|
3
|
+
import { unescapeXML } from "../escape.js";
|
|
5
4
|
|
|
6
5
|
const STATE_TEXT = 0;
|
|
7
6
|
const STATE_IGNORE_COMMENT = 1;
|
|
@@ -15,41 +14,96 @@ const STATE_ATTR_VALUE = 8;
|
|
|
15
14
|
const STATE_CDATA = 9;
|
|
16
15
|
const STATE_IGNORE_CDATA = 10;
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
// where a tag name or an attribute name ends: found by the regex engine in one step rather than by
|
|
18
|
+
// the loop below one character at a time (the way text runs and attribute values already are)
|
|
19
|
+
const TAG_NAME_END = /[\s/>]/g;
|
|
20
|
+
const ATTR_NAME_END = /[\s=]/g;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Handlers called directly, without going through EventEmitter. A nodeset carries hundreds of
|
|
24
|
+
* thousands of elements and text runs, and the emit machinery (listener lookup, arguments array,
|
|
25
|
+
* apply) was a measurable share of the parse; the events are still emitted when no handler is set.
|
|
26
|
+
*/
|
|
27
|
+
export interface SaxLtxHandlers {
|
|
28
|
+
onStartElement?: (name: string, attrs: XmlAttributes) => void;
|
|
29
|
+
onEndElement?: (name: string, selfClosing: boolean) => void;
|
|
30
|
+
onText?: (text: string) => void;
|
|
31
|
+
/**
|
|
32
|
+
* drop a text run made of white space only before unescaping it: the indentation between
|
|
33
|
+
* two tags, which is most of the text runs of a pretty-printed document
|
|
34
|
+
*/
|
|
35
|
+
skipBlankText?: boolean;
|
|
36
|
+
}
|
|
19
37
|
|
|
38
|
+
function isBlank(text: string): boolean {
|
|
39
|
+
for (let i = 0; i < text.length; i++) {
|
|
40
|
+
if (text.charCodeAt(i) > 32) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
20
46
|
|
|
47
|
+
export class SaxLtx extends EventEmitter {
|
|
21
48
|
#write: (data: string) => void;
|
|
22
|
-
constructor() {
|
|
49
|
+
constructor(handlers: SaxLtxHandlers = {}) {
|
|
23
50
|
super();
|
|
24
51
|
|
|
52
|
+
const onStartElement = handlers.onStartElement;
|
|
53
|
+
const onEndElement = handlers.onEndElement;
|
|
54
|
+
const onText = handlers.onText;
|
|
55
|
+
const skipBlankText = !!handlers.skipBlankText;
|
|
56
|
+
|
|
57
|
+
const emitText = (text: string) => {
|
|
58
|
+
if (skipBlankText && isBlank(text)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const unescaped = unescapeXML(text);
|
|
62
|
+
if (onText) {
|
|
63
|
+
onText(unescaped);
|
|
64
|
+
} else {
|
|
65
|
+
this.emit("text", unescaped);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
25
69
|
function _handleTagOpening(
|
|
26
70
|
this: SaxLtx,
|
|
27
|
-
endTag: boolean| undefined,
|
|
28
|
-
tagName: string|undefined,
|
|
29
|
-
attrs:
|
|
71
|
+
endTag: boolean | undefined,
|
|
72
|
+
tagName: string | undefined,
|
|
73
|
+
attrs: XmlAttributes | undefined
|
|
30
74
|
) {
|
|
31
75
|
if (!endTag) {
|
|
32
|
-
|
|
76
|
+
if (onStartElement) {
|
|
77
|
+
onStartElement(tagName as string, attrs as XmlAttributes);
|
|
78
|
+
} else {
|
|
79
|
+
this.emit("startElement", tagName, attrs);
|
|
80
|
+
}
|
|
33
81
|
if (selfClosing) {
|
|
34
|
-
|
|
82
|
+
if (onEndElement) {
|
|
83
|
+
onEndElement(tagName as string, true);
|
|
84
|
+
} else {
|
|
85
|
+
this.emit("endElement", tagName, true);
|
|
86
|
+
}
|
|
35
87
|
}
|
|
88
|
+
} else if (onEndElement) {
|
|
89
|
+
onEndElement(tagName as string, false);
|
|
36
90
|
} else {
|
|
37
91
|
this.emit("endElement", tagName, false);
|
|
38
92
|
}
|
|
39
|
-
}
|
|
93
|
+
}
|
|
40
94
|
let state = STATE_TEXT;
|
|
41
95
|
let remainder: string | null = null;
|
|
42
96
|
let parseRemainder: boolean = false;
|
|
43
97
|
let tagName: string | undefined;
|
|
44
|
-
let attrs:
|
|
45
|
-
let endTag: boolean|undefined;
|
|
98
|
+
let attrs: XmlAttributes | undefined;
|
|
99
|
+
let endTag: boolean | undefined;
|
|
46
100
|
let selfClosing: boolean | undefined;
|
|
47
101
|
let attrQuote: number;
|
|
48
102
|
let attrQuoteChar: string;
|
|
49
103
|
let recordStart: number | undefined = 0;
|
|
50
|
-
let attrName: string|undefined;
|
|
104
|
+
let attrName: string | undefined;
|
|
51
105
|
|
|
52
|
-
this.#write = function write(data: string
|
|
106
|
+
this.#write = function write(data: string) {
|
|
53
107
|
let pos = 0;
|
|
54
108
|
|
|
55
109
|
/* Anything from previous write()? */
|
|
@@ -66,29 +120,59 @@ export class SaxLtx extends EventEmitter {
|
|
|
66
120
|
recordStart = undefined;
|
|
67
121
|
return recorded;
|
|
68
122
|
}
|
|
69
|
-
return
|
|
123
|
+
return undefined;
|
|
70
124
|
}
|
|
71
125
|
|
|
126
|
+
// Each fast-forward below looks for the end of the record being read. When the chunk
|
|
127
|
+
// holds no such end, the rest of the chunk belongs to the record: skip to the end at
|
|
128
|
+
// once. Searching again from every following character made a chunk cost the square
|
|
129
|
+
// of its unread tail, 6 seconds for a 4 MB text run delivered in 64 KB pieces.
|
|
72
130
|
for (; pos < data.length; pos++) {
|
|
73
131
|
switch (state) {
|
|
74
132
|
case STATE_TEXT: {
|
|
75
133
|
// if we're looping through text, fast-forward using indexOf to
|
|
76
134
|
// the next '<' character
|
|
77
135
|
const lt = data.indexOf("<", pos);
|
|
78
|
-
if (lt
|
|
79
|
-
pos =
|
|
136
|
+
if (lt === -1) {
|
|
137
|
+
pos = data.length;
|
|
138
|
+
continue;
|
|
80
139
|
}
|
|
81
|
-
|
|
140
|
+
pos = lt;
|
|
82
141
|
break;
|
|
83
142
|
}
|
|
84
143
|
case STATE_ATTR_VALUE: {
|
|
85
144
|
// if we're looping through an attribute, fast-forward using
|
|
86
145
|
// indexOf to the next end quote character
|
|
87
146
|
const quot = data.indexOf(attrQuoteChar, pos);
|
|
88
|
-
if (quot
|
|
89
|
-
pos =
|
|
147
|
+
if (quot === -1) {
|
|
148
|
+
pos = data.length;
|
|
149
|
+
continue;
|
|
90
150
|
}
|
|
91
|
-
|
|
151
|
+
pos = quot;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case STATE_TAG_NAME: {
|
|
155
|
+
// past the first character, which may be "/", "!" or "?", fast-forward
|
|
156
|
+
// to the end of the name
|
|
157
|
+
if (recordStart !== pos) {
|
|
158
|
+
TAG_NAME_END.lastIndex = pos;
|
|
159
|
+
const m = TAG_NAME_END.exec(data);
|
|
160
|
+
if (!m) {
|
|
161
|
+
pos = data.length;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
pos = m.index;
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case STATE_ATTR_NAME: {
|
|
169
|
+
ATTR_NAME_END.lastIndex = pos;
|
|
170
|
+
const m = ATTR_NAME_END.exec(data);
|
|
171
|
+
if (!m) {
|
|
172
|
+
pos = data.length;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
pos = m.index;
|
|
92
176
|
break;
|
|
93
177
|
}
|
|
94
178
|
case STATE_IGNORE_COMMENT: {
|
|
@@ -120,7 +204,7 @@ export class SaxLtx extends EventEmitter {
|
|
|
120
204
|
if (c === 60 /* < */) {
|
|
121
205
|
const text = endRecording();
|
|
122
206
|
if (text) {
|
|
123
|
-
|
|
207
|
+
emitText(text);
|
|
124
208
|
}
|
|
125
209
|
state = STATE_TAG_NAME;
|
|
126
210
|
recordStart = pos + 1;
|
|
@@ -129,13 +213,21 @@ export class SaxLtx extends EventEmitter {
|
|
|
129
213
|
break;
|
|
130
214
|
case STATE_CDATA:
|
|
131
215
|
if (c === 93 /* ] */) {
|
|
132
|
-
if (data.
|
|
216
|
+
if (data.startsWith("]>", pos + 1)) {
|
|
133
217
|
const cData = endRecording();
|
|
134
218
|
if (cData) {
|
|
135
|
-
|
|
219
|
+
if (onText) {
|
|
220
|
+
onText(cData);
|
|
221
|
+
} else {
|
|
222
|
+
this.emit("text", cData);
|
|
223
|
+
}
|
|
136
224
|
}
|
|
225
|
+
// skip the "]>" that closes the section
|
|
226
|
+
pos += 2;
|
|
137
227
|
state = STATE_TEXT;
|
|
138
|
-
|
|
228
|
+
recordStart = pos + 1;
|
|
229
|
+
} else if (data.length < pos + 3) {
|
|
230
|
+
// the closing "]]>" may straddle this chunk and the next
|
|
139
231
|
parseRemainder = true;
|
|
140
232
|
pos = data.length;
|
|
141
233
|
}
|
|
@@ -146,13 +238,10 @@ export class SaxLtx extends EventEmitter {
|
|
|
146
238
|
recordStart = pos + 1;
|
|
147
239
|
endTag = true;
|
|
148
240
|
} else if (c === 33 /* ! */) {
|
|
149
|
-
if (data.
|
|
241
|
+
if (data.startsWith("[CDATA[", pos + 1)) {
|
|
150
242
|
recordStart = pos + 8;
|
|
151
243
|
state = STATE_CDATA;
|
|
152
|
-
} else if (
|
|
153
|
-
data.length < pos + 8 &&
|
|
154
|
-
"[CDATA[".startsWith(data.slice(pos + 1))
|
|
155
|
-
) {
|
|
244
|
+
} else if (data.length < pos + 8 && "[CDATA[".startsWith(data.slice(pos + 1))) {
|
|
156
245
|
// We potentially have CDATA, but the chunk is ending; stop here and let the next write() decide
|
|
157
246
|
parseRemainder = true;
|
|
158
247
|
pos = data.length;
|
|
@@ -191,7 +280,7 @@ export class SaxLtx extends EventEmitter {
|
|
|
191
280
|
break;
|
|
192
281
|
case STATE_TAG:
|
|
193
282
|
if (c === 62 /* > */) {
|
|
194
|
-
_handleTagOpening.call(this, endTag, tagName, attrs
|
|
283
|
+
_handleTagOpening.call(this, endTag, tagName, attrs);
|
|
195
284
|
tagName = undefined;
|
|
196
285
|
attrs = undefined;
|
|
197
286
|
endTag = undefined;
|
|
@@ -227,8 +316,10 @@ export class SaxLtx extends EventEmitter {
|
|
|
227
316
|
break;
|
|
228
317
|
case STATE_ATTR_VALUE:
|
|
229
318
|
if (c === attrQuote) {
|
|
230
|
-
const
|
|
231
|
-
(
|
|
319
|
+
const recorded = endRecording();
|
|
320
|
+
if (recorded !== undefined && attrName !== undefined) {
|
|
321
|
+
(attrs as XmlAttributes)[attrName] = unescapeXML(recorded);
|
|
322
|
+
}
|
|
232
323
|
attrName = undefined;
|
|
233
324
|
state = STATE_TAG;
|
|
234
325
|
}
|
|
@@ -239,11 +330,16 @@ export class SaxLtx extends EventEmitter {
|
|
|
239
330
|
if (typeof recordStart === "number" && recordStart <= data.length) {
|
|
240
331
|
remainder = data.slice(recordStart);
|
|
241
332
|
recordStart = 0;
|
|
333
|
+
} else if (state === STATE_IGNORE_COMMENT || state === STATE_IGNORE_INSTRUCTION || state === STATE_IGNORE_CDATA) {
|
|
334
|
+
// the end of a comment (-->), of an instruction (?>) or of a skipped CDATA (]]>) is
|
|
335
|
+
// recognised by looking back at the characters before the '>': keep the last two
|
|
336
|
+
// so that a terminator straddling this chunk and the next is still seen
|
|
337
|
+
remainder = data.slice(-2);
|
|
338
|
+
parseRemainder = true;
|
|
242
339
|
}
|
|
243
340
|
};
|
|
244
341
|
}
|
|
245
342
|
|
|
246
|
-
|
|
247
343
|
public write(data: string | Buffer) {
|
|
248
344
|
this.#write(data.toString());
|
|
249
345
|
}
|
|
@@ -253,7 +349,6 @@ export class SaxLtx extends EventEmitter {
|
|
|
253
349
|
}
|
|
254
350
|
|
|
255
351
|
/* Uh, yeah */
|
|
256
|
-
this.write = function write() {
|
|
352
|
+
this.write = function write() {};
|
|
257
353
|
}
|
|
258
354
|
}
|
|
259
|
-
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { lowerFirstLetter } from "node-opcua-utils";
|
|
2
|
-
import { type IReaderState, ReaderStateBase, type Xml2Json, type XmlAttributes } from "./xml2json";
|
|
2
|
+
import { type IReaderState, ReaderStateBase, type Xml2Json, type XmlAttributes } from "./xml2json.js";
|
|
3
3
|
export type withPojoLambda = (name: string, pojo: unknown) => void;
|
|
4
4
|
|
|
5
5
|
export class ReaderState2 extends ReaderStateBase {
|
|
6
6
|
public _stack: unknown[];
|
|
7
7
|
public _pojo: unknown;
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
// biome-ignore lint/suspicious/noExplicitAny: explanation
|
|
10
10
|
public _element: any;
|
|
11
11
|
public text: string;
|
|
12
12
|
public _withPojo: withPojoLambda;
|
|
13
13
|
|
|
14
|
-
|
|
15
14
|
private initLevel = 0;
|
|
16
15
|
constructor() {
|
|
17
16
|
super();
|
|
@@ -25,11 +24,10 @@ export class ReaderState2 extends ReaderStateBase {
|
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
public _on_init(_elementName: string, _attrs: XmlAttributes, _parent: IReaderState, level: number, _engine: Xml2Json): void {
|
|
28
|
-
|
|
29
27
|
this.initLevel = level;
|
|
30
28
|
if (this._stack.length === 0) {
|
|
31
29
|
this._pojo = {};
|
|
32
|
-
this._element = this._pojo as Record<string, unknown
|
|
30
|
+
this._element = this._pojo as Record<string, unknown>;
|
|
33
31
|
}
|
|
34
32
|
}
|
|
35
33
|
|