node-opcua-xml2json 2.179.0 → 2.182.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 +6 -0
- package/dist/source/definition_parser.js.map +1 -1
- package/dist/source/fragment_cloner.d.ts +3 -3
- package/dist/source/fragment_cloner.js +11 -6
- package/dist/source/fragment_cloner.js.map +1 -1
- package/dist/source/fragment_cloner_parser.d.ts +1 -1
- package/dist/source/fragment_cloner_parser.js +2 -2
- 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 +1 -1
- package/dist/source/nodejs/xml2json_fs.js +2 -2
- package/dist/source/nodejs/xml2json_fs.js.map +1 -1
- package/dist/source/thirdparties/parser/lts.d.ts +17 -1
- package/dist/source/thirdparties/parser/lts.js +103 -14
- 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 +25 -41
- package/dist/source/xml2json.js +185 -81
- package/dist/source/xml2json.js.map +1 -1
- package/dist/source/xml2json_pojo.d.ts +2 -2
- package/dist/source/xml2json_pojo.js +4 -4
- package/dist/source/xml2json_pojo.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -4
- package/source/definition_parser.ts +11 -4
- package/source/fragment_cloner.ts +8 -5
- package/source/fragment_cloner_parser.ts +2 -2
- package/source/index.ts +5 -5
- package/source/nodejs/xml2json_fs.ts +1 -1
- package/source/thirdparties/parser/lts.ts +117 -16
- package/source/xml2Json_pojo_tools.ts +1 -1
- package/source/xml2json.ts +209 -90
- package/source/xml2json_pojo.ts +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
|
-
import type { XmlAttributes } from "../../xml2json";
|
|
3
|
-
import { unescapeXML } from "../escape";
|
|
2
|
+
import type { XmlAttributes } from "../../xml2json.js";
|
|
3
|
+
import { unescapeXML } from "../escape.js";
|
|
4
4
|
|
|
5
5
|
const STATE_TEXT = 0;
|
|
6
6
|
const STATE_IGNORE_COMMENT = 1;
|
|
@@ -14,11 +14,58 @@ const STATE_ATTR_VALUE = 8;
|
|
|
14
14
|
const STATE_CDATA = 9;
|
|
15
15
|
const STATE_IGNORE_CDATA = 10;
|
|
16
16
|
|
|
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
|
+
}
|
|
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
|
+
}
|
|
46
|
+
|
|
17
47
|
export class SaxLtx extends EventEmitter {
|
|
18
48
|
#write: (data: string) => void;
|
|
19
|
-
constructor() {
|
|
49
|
+
constructor(handlers: SaxLtxHandlers = {}) {
|
|
20
50
|
super();
|
|
21
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
|
+
|
|
22
69
|
function _handleTagOpening(
|
|
23
70
|
this: SaxLtx,
|
|
24
71
|
endTag: boolean | undefined,
|
|
@@ -26,10 +73,20 @@ export class SaxLtx extends EventEmitter {
|
|
|
26
73
|
attrs: XmlAttributes | undefined
|
|
27
74
|
) {
|
|
28
75
|
if (!endTag) {
|
|
29
|
-
|
|
76
|
+
if (onStartElement) {
|
|
77
|
+
onStartElement(tagName as string, attrs as XmlAttributes);
|
|
78
|
+
} else {
|
|
79
|
+
this.emit("startElement", tagName, attrs);
|
|
80
|
+
}
|
|
30
81
|
if (selfClosing) {
|
|
31
|
-
|
|
82
|
+
if (onEndElement) {
|
|
83
|
+
onEndElement(tagName as string, true);
|
|
84
|
+
} else {
|
|
85
|
+
this.emit("endElement", tagName, true);
|
|
86
|
+
}
|
|
32
87
|
}
|
|
88
|
+
} else if (onEndElement) {
|
|
89
|
+
onEndElement(tagName as string, false);
|
|
33
90
|
} else {
|
|
34
91
|
this.emit("endElement", tagName, false);
|
|
35
92
|
}
|
|
@@ -66,26 +123,56 @@ export class SaxLtx extends EventEmitter {
|
|
|
66
123
|
return undefined;
|
|
67
124
|
}
|
|
68
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.
|
|
69
130
|
for (; pos < data.length; pos++) {
|
|
70
131
|
switch (state) {
|
|
71
132
|
case STATE_TEXT: {
|
|
72
133
|
// if we're looping through text, fast-forward using indexOf to
|
|
73
134
|
// the next '<' character
|
|
74
135
|
const lt = data.indexOf("<", pos);
|
|
75
|
-
if (lt
|
|
76
|
-
pos =
|
|
136
|
+
if (lt === -1) {
|
|
137
|
+
pos = data.length;
|
|
138
|
+
continue;
|
|
77
139
|
}
|
|
78
|
-
|
|
140
|
+
pos = lt;
|
|
79
141
|
break;
|
|
80
142
|
}
|
|
81
143
|
case STATE_ATTR_VALUE: {
|
|
82
144
|
// if we're looping through an attribute, fast-forward using
|
|
83
145
|
// indexOf to the next end quote character
|
|
84
146
|
const quot = data.indexOf(attrQuoteChar, pos);
|
|
85
|
-
if (quot
|
|
86
|
-
pos =
|
|
147
|
+
if (quot === -1) {
|
|
148
|
+
pos = data.length;
|
|
149
|
+
continue;
|
|
87
150
|
}
|
|
88
|
-
|
|
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;
|
|
89
176
|
break;
|
|
90
177
|
}
|
|
91
178
|
case STATE_IGNORE_COMMENT: {
|
|
@@ -117,7 +204,7 @@ export class SaxLtx extends EventEmitter {
|
|
|
117
204
|
if (c === 60 /* < */) {
|
|
118
205
|
const text = endRecording();
|
|
119
206
|
if (text) {
|
|
120
|
-
|
|
207
|
+
emitText(text);
|
|
121
208
|
}
|
|
122
209
|
state = STATE_TAG_NAME;
|
|
123
210
|
recordStart = pos + 1;
|
|
@@ -126,13 +213,21 @@ export class SaxLtx extends EventEmitter {
|
|
|
126
213
|
break;
|
|
127
214
|
case STATE_CDATA:
|
|
128
215
|
if (c === 93 /* ] */) {
|
|
129
|
-
if (data.
|
|
216
|
+
if (data.startsWith("]>", pos + 1)) {
|
|
130
217
|
const cData = endRecording();
|
|
131
218
|
if (cData) {
|
|
132
|
-
|
|
219
|
+
if (onText) {
|
|
220
|
+
onText(cData);
|
|
221
|
+
} else {
|
|
222
|
+
this.emit("text", cData);
|
|
223
|
+
}
|
|
133
224
|
}
|
|
225
|
+
// skip the "]>" that closes the section
|
|
226
|
+
pos += 2;
|
|
134
227
|
state = STATE_TEXT;
|
|
135
|
-
|
|
228
|
+
recordStart = pos + 1;
|
|
229
|
+
} else if (data.length < pos + 3) {
|
|
230
|
+
// the closing "]]>" may straddle this chunk and the next
|
|
136
231
|
parseRemainder = true;
|
|
137
232
|
pos = data.length;
|
|
138
233
|
}
|
|
@@ -143,7 +238,7 @@ export class SaxLtx extends EventEmitter {
|
|
|
143
238
|
recordStart = pos + 1;
|
|
144
239
|
endTag = true;
|
|
145
240
|
} else if (c === 33 /* ! */) {
|
|
146
|
-
if (data.
|
|
241
|
+
if (data.startsWith("[CDATA[", pos + 1)) {
|
|
147
242
|
recordStart = pos + 8;
|
|
148
243
|
state = STATE_CDATA;
|
|
149
244
|
} else if (data.length < pos + 8 && "[CDATA[".startsWith(data.slice(pos + 1))) {
|
|
@@ -235,6 +330,12 @@ export class SaxLtx extends EventEmitter {
|
|
|
235
330
|
if (typeof recordStart === "number" && recordStart <= data.length) {
|
|
236
331
|
remainder = data.slice(recordStart);
|
|
237
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;
|
|
238
339
|
}
|
|
239
340
|
};
|
|
240
341
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
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 {
|
package/source/xml2json.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { assert } from "node-opcua-assert";
|
|
7
|
-
import { SaxLtx } from "./thirdparties/parser/lts";
|
|
7
|
+
import { SaxLtx } from "./thirdparties/parser/lts.js";
|
|
8
8
|
|
|
9
9
|
export type SimpleCallback = (err?: Error) => void;
|
|
10
10
|
export type Callback<T> = (err?: Error | null, result?: T) => void;
|
|
@@ -14,24 +14,38 @@ export interface Parser {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
17
|
+
* the reader states of a definition, in a table of this engine's own. A definition is a plain
|
|
18
|
+
* object the caller may hand to any number of parsers (the shared Definition reader of the
|
|
19
|
+
* nodeset loader, for one), while a reader state carries the element being read: its parent, its
|
|
20
|
+
* attributes, its text so far. So the definition is never written to; each parser builds its
|
|
21
|
+
* states from it. A definition that refers to itself, directly or through a child, gets one state
|
|
22
|
+
* per engine for the cycle, the one being built when the cycle closes.
|
|
18
23
|
* @private
|
|
19
|
-
|
|
20
|
-
* @param parser {map<ReaderState|options>}
|
|
21
|
-
* @return {map}
|
|
22
24
|
*/
|
|
23
|
-
function _coerceParser(parser: ParserLike): Parser {
|
|
25
|
+
function _coerceParser(parser: ParserLike | undefined): Parser {
|
|
26
|
+
// no prototype: an element name is looked up in this table for every element of the document,
|
|
27
|
+
// one hash probe that cannot find an Object member by mistake
|
|
28
|
+
const table: Parser = Object.create(null);
|
|
29
|
+
if (!parser) {
|
|
30
|
+
return table;
|
|
31
|
+
}
|
|
24
32
|
for (const name of Object.keys(parser)) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
delete parser[name];
|
|
29
|
-
parser[name] = new ReaderState(tmp);
|
|
33
|
+
const entry = parser[name];
|
|
34
|
+
if (!entry) {
|
|
35
|
+
continue;
|
|
30
36
|
}
|
|
37
|
+
table[name] = entry instanceof ReaderState ? entry : (statesUnderConstruction.get(entry) ?? new ReaderState(entry));
|
|
31
38
|
}
|
|
32
|
-
return
|
|
39
|
+
return table;
|
|
33
40
|
}
|
|
34
41
|
|
|
42
|
+
/**
|
|
43
|
+
* the states being built from a definition, for the duration of one root construction: the way a
|
|
44
|
+
* definition that closes a cycle finds the state already under way instead of recursing for ever
|
|
45
|
+
*/
|
|
46
|
+
let statesUnderConstruction = new WeakMap<ReaderStateParserLike, ReaderState>();
|
|
47
|
+
let constructionDepth = 0;
|
|
48
|
+
|
|
35
49
|
export interface XmlAttributes {
|
|
36
50
|
[key: string]: string;
|
|
37
51
|
}
|
|
@@ -40,7 +54,7 @@ export interface ReaderStateParser {
|
|
|
40
54
|
parser?: ParserLike;
|
|
41
55
|
init?: (this: IReaderState, name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void;
|
|
42
56
|
finish?: (this: IReaderState) => void;
|
|
43
|
-
startElement?: (this: IReaderState, name: string, attrs: XmlAttributes) => void;
|
|
57
|
+
startElement?: (this: IReaderState, name: string, attrs: XmlAttributes, qualifiedName?: string) => void;
|
|
44
58
|
endElement?: (this: IReaderState, name: string) => void;
|
|
45
59
|
}
|
|
46
60
|
|
|
@@ -58,7 +72,7 @@ export interface ReaderStateParserLike {
|
|
|
58
72
|
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
59
73
|
finish?: (this: any) => void;
|
|
60
74
|
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
61
|
-
startElement?: (this: any, name: string, attrs: XmlAttributes) => void;
|
|
75
|
+
startElement?: (this: any, name: string, attrs: XmlAttributes, qualifiedName?: string) => void;
|
|
62
76
|
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
63
77
|
endElement?: (this: any, name: string) => void;
|
|
64
78
|
}
|
|
@@ -68,9 +82,9 @@ export interface IReaderState {
|
|
|
68
82
|
|
|
69
83
|
_on_finish(): void;
|
|
70
84
|
|
|
71
|
-
_on_startElement(level: number, elementName: string, attrs: XmlAttributes): void;
|
|
85
|
+
_on_startElement(level: number, elementName: string, attrs: XmlAttributes, qualifiedName?: string): void;
|
|
72
86
|
|
|
73
|
-
_on_endElement(level: number, elementName: string): void;
|
|
87
|
+
_on_endElement(level: number, elementName: string, qualifiedName?: string): void;
|
|
74
88
|
|
|
75
89
|
_on_endElement2(level: number, elementName: string): void;
|
|
76
90
|
|
|
@@ -86,7 +100,7 @@ export interface ReaderStateBase extends IReaderState {}
|
|
|
86
100
|
export class ReaderState extends ReaderStateBase {
|
|
87
101
|
public _init?: (name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void;
|
|
88
102
|
public _finish?: () => void;
|
|
89
|
-
public _startElement?: (name: string, attrs: XmlAttributes) => void;
|
|
103
|
+
public _startElement?: (name: string, attrs: XmlAttributes, qualifiedName?: string) => void;
|
|
90
104
|
public _endElement?: (name: string) => void;
|
|
91
105
|
|
|
92
106
|
public parser: Parser;
|
|
@@ -101,21 +115,39 @@ export class ReaderState extends ReaderStateBase {
|
|
|
101
115
|
|
|
102
116
|
public parent?: IReaderState;
|
|
103
117
|
public root?: Xml2Json;
|
|
104
|
-
|
|
118
|
+
private _data?: Record<string, unknown>;
|
|
119
|
+
|
|
120
|
+
/** scratch space for the reader, fresh for every element it reads, allocated on first use */
|
|
121
|
+
public get data(): Record<string, unknown> {
|
|
122
|
+
if (!this._data) {
|
|
123
|
+
this._data = {};
|
|
124
|
+
}
|
|
125
|
+
return this._data;
|
|
126
|
+
}
|
|
127
|
+
public set data(value: Record<string, unknown> | undefined) {
|
|
128
|
+
this._data = value;
|
|
129
|
+
}
|
|
105
130
|
|
|
106
131
|
constructor(options: ReaderStateParser | ReaderState) {
|
|
107
132
|
super();
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
133
|
+
if (options instanceof ReaderStateBase) {
|
|
134
|
+
this.parser = options.parser;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this._init = options.init;
|
|
138
|
+
this._finish = options.finish;
|
|
139
|
+
this._startElement = options.startElement;
|
|
140
|
+
this._endElement = options.endElement;
|
|
141
|
+
if (constructionDepth === 0) {
|
|
142
|
+
statesUnderConstruction = new WeakMap();
|
|
143
|
+
}
|
|
144
|
+
constructionDepth += 1;
|
|
145
|
+
try {
|
|
146
|
+
statesUnderConstruction.set(options, this);
|
|
147
|
+
this.parser = _coerceParser(options.parser);
|
|
148
|
+
} finally {
|
|
149
|
+
constructionDepth -= 1;
|
|
116
150
|
}
|
|
117
|
-
|
|
118
|
-
this.parser = _coerceParser(options.parser);
|
|
119
151
|
}
|
|
120
152
|
|
|
121
153
|
/**
|
|
@@ -125,7 +157,7 @@ export class ReaderState extends ReaderStateBase {
|
|
|
125
157
|
this.name = elementName;
|
|
126
158
|
this.parent = parent;
|
|
127
159
|
this.engine = engine;
|
|
128
|
-
this.
|
|
160
|
+
this._data = undefined;
|
|
129
161
|
this.level = level;
|
|
130
162
|
this.currentLevel = this.level;
|
|
131
163
|
this.attrs = attrs;
|
|
@@ -146,17 +178,18 @@ export class ReaderState extends ReaderStateBase {
|
|
|
146
178
|
/**
|
|
147
179
|
* @protected
|
|
148
180
|
*/
|
|
149
|
-
public _on_startElement(level: number, elementName: string, attrs: XmlAttributes): void {
|
|
181
|
+
public _on_startElement(level: number, elementName: string, attrs: XmlAttributes, qualifiedName?: string): void {
|
|
150
182
|
this.currentLevel = level;
|
|
151
183
|
|
|
152
184
|
this.chunks = [];
|
|
153
185
|
this.text = "";
|
|
154
186
|
|
|
155
187
|
if (this._startElement) {
|
|
156
|
-
this._startElement(elementName, attrs);
|
|
188
|
+
this._startElement(elementName, attrs, qualifiedName);
|
|
157
189
|
}
|
|
158
|
-
|
|
159
|
-
|
|
190
|
+
const child = this.parser[elementName];
|
|
191
|
+
if (this.engine && child) {
|
|
192
|
+
this.engine._promote(child, level, elementName, attrs);
|
|
160
193
|
}
|
|
161
194
|
}
|
|
162
195
|
|
|
@@ -187,7 +220,7 @@ export class ReaderState extends ReaderStateBase {
|
|
|
187
220
|
this.chunks = [];
|
|
188
221
|
// this is the end
|
|
189
222
|
this._on_finish();
|
|
190
|
-
if (this.parent instanceof ReaderState &&
|
|
223
|
+
if (this.parent instanceof ReaderState && this.parent.parser[elementName]) {
|
|
191
224
|
this.engine?._demote(this, level, elementName);
|
|
192
225
|
}
|
|
193
226
|
}
|
|
@@ -209,15 +242,26 @@ export class ReaderState extends ReaderStateBase {
|
|
|
209
242
|
|
|
210
243
|
const regexp = /(([^:]+):)?(.*)/;
|
|
211
244
|
|
|
212
|
-
|
|
245
|
+
interface ResolvedName {
|
|
246
|
+
ns: string | undefined;
|
|
247
|
+
tag: string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// a document uses a few dozen distinct element names, each of them thousands of times
|
|
251
|
+
const resolvedNames = new Map<string, ResolvedName>();
|
|
252
|
+
|
|
253
|
+
function resolve_namespace(name: string): ResolvedName {
|
|
254
|
+
const known = resolvedNames.get(name);
|
|
255
|
+
if (known) {
|
|
256
|
+
return known;
|
|
257
|
+
}
|
|
213
258
|
const m = name.match(regexp);
|
|
214
259
|
if (!m) {
|
|
215
260
|
throw new Error("Invalid match");
|
|
216
261
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
};
|
|
262
|
+
const resolved: ResolvedName = { ns: m[2], tag: m[3] };
|
|
263
|
+
resolvedNames.set(name, resolved);
|
|
264
|
+
return resolved;
|
|
221
265
|
}
|
|
222
266
|
|
|
223
267
|
/**
|
|
@@ -253,17 +297,29 @@ function resolve_namespace(name: string) {
|
|
|
253
297
|
* done();
|
|
254
298
|
* });
|
|
255
299
|
*/
|
|
300
|
+
/** how many times a reader state is active at once, kept on the state itself */
|
|
301
|
+
const activations = Symbol("activations");
|
|
302
|
+
interface ActivationCounted {
|
|
303
|
+
[activations]?: number;
|
|
304
|
+
}
|
|
305
|
+
|
|
256
306
|
export class Xml2Json {
|
|
257
307
|
public currentLevel = 0;
|
|
258
|
-
|
|
308
|
+
// two parallel stacks rather than one record per element: an element is pushed and popped
|
|
309
|
+
// for each of the hundreds of thousands a nodeset carries
|
|
310
|
+
private state_stack: Array<IReaderState | null> = [];
|
|
311
|
+
private backup_stack: Array<Record<string, unknown> | null> = [];
|
|
259
312
|
private current_state: IReaderState | null = null;
|
|
260
|
-
private
|
|
313
|
+
private sax: SaxLtx | null = null;
|
|
314
|
+
private decoder: TextDecoder | null = null;
|
|
315
|
+
private atStart = false;
|
|
261
316
|
|
|
262
317
|
constructor(options: ReaderStateParser) {
|
|
263
318
|
const state = options instanceof ReaderStateBase ? (options as ReaderState) : new ReaderState(options);
|
|
264
319
|
state.root = this;
|
|
265
320
|
|
|
266
321
|
this.state_stack = [];
|
|
322
|
+
this.backup_stack = [];
|
|
267
323
|
this.current_state = null;
|
|
268
324
|
this._promote(state, 0);
|
|
269
325
|
}
|
|
@@ -271,6 +327,105 @@ export class Xml2Json {
|
|
|
271
327
|
public parseString(xml_text: string): Record<string, unknown> {
|
|
272
328
|
return this.__parseInternal(xml_text);
|
|
273
329
|
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* parse a document that arrives in pieces: text, or UTF-8 bytes decoded as they come (a
|
|
333
|
+
* multi-byte character or the byte-order mark may straddle two chunks)
|
|
334
|
+
*/
|
|
335
|
+
public async parseStream(
|
|
336
|
+
chunks: AsyncIterable<string | Uint8Array> | Iterable<string | Uint8Array>
|
|
337
|
+
): Promise<Record<string, unknown>> {
|
|
338
|
+
this.begin();
|
|
339
|
+
for await (const chunk of chunks) {
|
|
340
|
+
this.write(chunk);
|
|
341
|
+
}
|
|
342
|
+
return this.end();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** start a document that {@link write} will feed and {@link end} will finish */
|
|
346
|
+
public begin(): void {
|
|
347
|
+
this.currentLevel = 0;
|
|
348
|
+
this.atStart = true;
|
|
349
|
+
this.decoder = null;
|
|
350
|
+
// direct handlers rather than events, and the blank runs between tags dropped by the
|
|
351
|
+
// parser before they are unescaped: see SaxLtxHandlers
|
|
352
|
+
const sax = new SaxLtx({
|
|
353
|
+
skipBlankText: true,
|
|
354
|
+
onStartElement: (name: string, attrs: XmlAttributes) => {
|
|
355
|
+
const tag_ns = resolve_namespace(name);
|
|
356
|
+
this.currentLevel += 1;
|
|
357
|
+
if (this.current_state) {
|
|
358
|
+
// the stripped tag is what every parser matches on; the qualified name rides
|
|
359
|
+
// alongside for the one reader that must reproduce the document, the fragment
|
|
360
|
+
// cloner, which would otherwise turn <ua:ModelInfo> into <ModelInfo>
|
|
361
|
+
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs, name);
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
onEndElement: (name: string) => {
|
|
365
|
+
const tag_ns = resolve_namespace(name);
|
|
366
|
+
if (this.current_state) {
|
|
367
|
+
this.current_state._on_endElement(this.currentLevel, tag_ns.tag, name);
|
|
368
|
+
}
|
|
369
|
+
this.currentLevel -= 1;
|
|
370
|
+
if (this.currentLevel === 0) {
|
|
371
|
+
sax.emit("close");
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
onText: (text: string) => {
|
|
375
|
+
// the reader state trims what it receives; nothing blank reaches this point
|
|
376
|
+
if (this.current_state) {
|
|
377
|
+
this.current_state._on_text(text);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
this.sax = sax;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** feed the next piece of the document started by {@link begin} */
|
|
385
|
+
public write(chunk: string | Uint8Array): void {
|
|
386
|
+
const sax = this.sax;
|
|
387
|
+
if (!sax) {
|
|
388
|
+
throw new Error("Xml2Json#write: begin() must be called first");
|
|
389
|
+
}
|
|
390
|
+
let text: string;
|
|
391
|
+
if (typeof chunk === "string") {
|
|
392
|
+
text = chunk;
|
|
393
|
+
} else {
|
|
394
|
+
this.decoder = this.decoder || new TextDecoder("utf-8");
|
|
395
|
+
text = this.decoder.decode(chunk, { stream: true });
|
|
396
|
+
}
|
|
397
|
+
if (text.length === 0) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (this.atStart) {
|
|
401
|
+
this.atStart = false;
|
|
402
|
+
if (text.charCodeAt(0) === 0xfeff) {
|
|
403
|
+
text = text.slice(1);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
sax.write(text);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** finish the document started by {@link begin} and return what the reader states built */
|
|
410
|
+
public end(): Record<string, unknown> {
|
|
411
|
+
const sax = this.sax;
|
|
412
|
+
if (!sax) {
|
|
413
|
+
throw new Error("Xml2Json#end: begin() must be called first");
|
|
414
|
+
}
|
|
415
|
+
if (this.decoder) {
|
|
416
|
+
const tail = this.decoder.decode();
|
|
417
|
+
this.decoder = null;
|
|
418
|
+
if (tail.length > 0) {
|
|
419
|
+
sax.write(tail);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
sax.end("");
|
|
423
|
+
this.sax = null;
|
|
424
|
+
if (!this.current_state) {
|
|
425
|
+
return {};
|
|
426
|
+
}
|
|
427
|
+
return (this.current_state as unknown as { _pojo: Record<string, unknown> })._pojo;
|
|
428
|
+
}
|
|
274
429
|
/**
|
|
275
430
|
* @private
|
|
276
431
|
* @internal
|
|
@@ -282,13 +437,12 @@ export class Xml2Json {
|
|
|
282
437
|
// active at two levels at once. This happens when a data type refers to itself, directly or
|
|
283
438
|
// indirectly: the reader of the nested element is then the very same object as the outer one.
|
|
284
439
|
// In that case only, take a copy of the outer activation and restore it in _demote.
|
|
285
|
-
const
|
|
286
|
-
|
|
440
|
+
const counted = new_state as ActivationCounted;
|
|
441
|
+
const active = counted[activations] || 0;
|
|
442
|
+
counted[activations] = active + 1;
|
|
287
443
|
|
|
288
|
-
this.state_stack.push(
|
|
289
|
-
|
|
290
|
-
state: this.current_state
|
|
291
|
-
});
|
|
444
|
+
this.state_stack.push(this.current_state);
|
|
445
|
+
this.backup_stack.push(active > 0 ? { ...(new_state as unknown as Record<string, unknown>) } : null);
|
|
292
446
|
|
|
293
447
|
const parent = this.current_state;
|
|
294
448
|
this.current_state = new_state;
|
|
@@ -305,22 +459,18 @@ export class Xml2Json {
|
|
|
305
459
|
*/
|
|
306
460
|
public _demote(cur_state: IReaderState, level: number, elementName: string): void {
|
|
307
461
|
/// assert(this.current_state === cur_state);
|
|
308
|
-
|
|
309
|
-
if (!popped) {
|
|
462
|
+
if (this.state_stack.length === 0) {
|
|
310
463
|
throw new Error("_demote called without a matching _promote");
|
|
311
464
|
}
|
|
312
|
-
const
|
|
465
|
+
const state = this.state_stack.pop() as IReaderState | null;
|
|
466
|
+
const backup = this.backup_stack.pop() as Record<string, unknown> | null;
|
|
313
467
|
this.current_state = state;
|
|
314
468
|
if (this.current_state) {
|
|
315
469
|
this.current_state._on_endElement2(level, elementName);
|
|
316
470
|
}
|
|
317
471
|
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
this.activation_count.delete(cur_state);
|
|
321
|
-
} else {
|
|
322
|
-
this.activation_count.set(cur_state, count);
|
|
323
|
-
}
|
|
472
|
+
const counted = cur_state as ActivationCounted;
|
|
473
|
+
counted[activations] = (counted[activations] || 1) - 1;
|
|
324
474
|
if (backup) {
|
|
325
475
|
// cur_state was a nested activation of a state that is still active at an outer level:
|
|
326
476
|
// put the outer element back the way it was. The parent has already consumed the nested
|
|
@@ -338,39 +488,8 @@ export class Xml2Json {
|
|
|
338
488
|
* @internal
|
|
339
489
|
*/
|
|
340
490
|
protected __parseInternal(data: string): Record<string, unknown> {
|
|
341
|
-
|
|
342
|
-
this.
|
|
343
|
-
|
|
344
|
-
const tag_ns = resolve_namespace(name);
|
|
345
|
-
this.currentLevel += 1;
|
|
346
|
-
if (this.current_state) {
|
|
347
|
-
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
parser.on("endElement", (name: string) => {
|
|
351
|
-
const tag_ns = resolve_namespace(name);
|
|
352
|
-
if (this.current_state) {
|
|
353
|
-
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
354
|
-
}
|
|
355
|
-
this.currentLevel -= 1;
|
|
356
|
-
if (this.currentLevel === 0) {
|
|
357
|
-
parser.emit("close");
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
parser.on("text", (text: string) => {
|
|
361
|
-
text = text.trim();
|
|
362
|
-
if (text.length === 0) {
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
if (this.current_state) {
|
|
366
|
-
this.current_state._on_text(text);
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
parser.write(data);
|
|
370
|
-
parser.end("");
|
|
371
|
-
if (!this.current_state) {
|
|
372
|
-
return {};
|
|
373
|
-
}
|
|
374
|
-
return (this.current_state as unknown as { _pojo: Record<string, unknown> })._pojo;
|
|
491
|
+
this.begin();
|
|
492
|
+
this.write(data);
|
|
493
|
+
return this.end();
|
|
375
494
|
}
|
|
376
495
|
}
|
package/source/xml2json_pojo.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ReaderState2, type withPojoLambda } from "./xml2Json_pojo_tools";
|
|
2
|
-
import { type IReaderState, type ReaderState, type ReaderStateParser, Xml2Json, type XmlAttributes } from "./xml2json";
|
|
1
|
+
import { ReaderState2, type withPojoLambda } from "./xml2Json_pojo_tools.js";
|
|
2
|
+
import { type IReaderState, type ReaderState, type ReaderStateParser, Xml2Json, type XmlAttributes } from "./xml2json.js";
|
|
3
3
|
|
|
4
4
|
const json_extractor: ReaderState2 = new ReaderState2();
|
|
5
5
|
export const json_parser: ReaderStateParser = {
|