node-opcua-xml2json 2.179.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/fragment_cloner.d.ts +1 -1
- package/dist/source/fragment_cloner.js +6 -3
- 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 +19 -35
- package/dist/source/xml2json.js +180 -79
- 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 +1 -1
- package/source/fragment_cloner.ts +3 -2
- 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 +199 -83
- package/source/xml2json_pojo.ts +2 -2
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
|
}
|
|
@@ -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;
|
|
@@ -155,8 +187,9 @@ export class ReaderState extends ReaderStateBase {
|
|
|
155
187
|
if (this._startElement) {
|
|
156
188
|
this._startElement(elementName, attrs);
|
|
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,102 @@ 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
|
+
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
onEndElement: (name: string) => {
|
|
362
|
+
const tag_ns = resolve_namespace(name);
|
|
363
|
+
if (this.current_state) {
|
|
364
|
+
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
365
|
+
}
|
|
366
|
+
this.currentLevel -= 1;
|
|
367
|
+
if (this.currentLevel === 0) {
|
|
368
|
+
sax.emit("close");
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
onText: (text: string) => {
|
|
372
|
+
// the reader state trims what it receives; nothing blank reaches this point
|
|
373
|
+
if (this.current_state) {
|
|
374
|
+
this.current_state._on_text(text);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
this.sax = sax;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** feed the next piece of the document started by {@link begin} */
|
|
382
|
+
public write(chunk: string | Uint8Array): void {
|
|
383
|
+
const sax = this.sax;
|
|
384
|
+
if (!sax) {
|
|
385
|
+
throw new Error("Xml2Json#write: begin() must be called first");
|
|
386
|
+
}
|
|
387
|
+
let text: string;
|
|
388
|
+
if (typeof chunk === "string") {
|
|
389
|
+
text = chunk;
|
|
390
|
+
} else {
|
|
391
|
+
this.decoder = this.decoder || new TextDecoder("utf-8");
|
|
392
|
+
text = this.decoder.decode(chunk, { stream: true });
|
|
393
|
+
}
|
|
394
|
+
if (text.length === 0) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (this.atStart) {
|
|
398
|
+
this.atStart = false;
|
|
399
|
+
if (text.charCodeAt(0) === 0xfeff) {
|
|
400
|
+
text = text.slice(1);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
sax.write(text);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** finish the document started by {@link begin} and return what the reader states built */
|
|
407
|
+
public end(): Record<string, unknown> {
|
|
408
|
+
const sax = this.sax;
|
|
409
|
+
if (!sax) {
|
|
410
|
+
throw new Error("Xml2Json#end: begin() must be called first");
|
|
411
|
+
}
|
|
412
|
+
if (this.decoder) {
|
|
413
|
+
const tail = this.decoder.decode();
|
|
414
|
+
this.decoder = null;
|
|
415
|
+
if (tail.length > 0) {
|
|
416
|
+
sax.write(tail);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
sax.end("");
|
|
420
|
+
this.sax = null;
|
|
421
|
+
if (!this.current_state) {
|
|
422
|
+
return {};
|
|
423
|
+
}
|
|
424
|
+
return (this.current_state as unknown as { _pojo: Record<string, unknown> })._pojo;
|
|
425
|
+
}
|
|
274
426
|
/**
|
|
275
427
|
* @private
|
|
276
428
|
* @internal
|
|
@@ -282,13 +434,12 @@ export class Xml2Json {
|
|
|
282
434
|
// active at two levels at once. This happens when a data type refers to itself, directly or
|
|
283
435
|
// indirectly: the reader of the nested element is then the very same object as the outer one.
|
|
284
436
|
// In that case only, take a copy of the outer activation and restore it in _demote.
|
|
285
|
-
const
|
|
286
|
-
|
|
437
|
+
const counted = new_state as ActivationCounted;
|
|
438
|
+
const active = counted[activations] || 0;
|
|
439
|
+
counted[activations] = active + 1;
|
|
287
440
|
|
|
288
|
-
this.state_stack.push(
|
|
289
|
-
|
|
290
|
-
state: this.current_state
|
|
291
|
-
});
|
|
441
|
+
this.state_stack.push(this.current_state);
|
|
442
|
+
this.backup_stack.push(active > 0 ? { ...(new_state as unknown as Record<string, unknown>) } : null);
|
|
292
443
|
|
|
293
444
|
const parent = this.current_state;
|
|
294
445
|
this.current_state = new_state;
|
|
@@ -305,22 +456,18 @@ export class Xml2Json {
|
|
|
305
456
|
*/
|
|
306
457
|
public _demote(cur_state: IReaderState, level: number, elementName: string): void {
|
|
307
458
|
/// assert(this.current_state === cur_state);
|
|
308
|
-
|
|
309
|
-
if (!popped) {
|
|
459
|
+
if (this.state_stack.length === 0) {
|
|
310
460
|
throw new Error("_demote called without a matching _promote");
|
|
311
461
|
}
|
|
312
|
-
const
|
|
462
|
+
const state = this.state_stack.pop() as IReaderState | null;
|
|
463
|
+
const backup = this.backup_stack.pop() as Record<string, unknown> | null;
|
|
313
464
|
this.current_state = state;
|
|
314
465
|
if (this.current_state) {
|
|
315
466
|
this.current_state._on_endElement2(level, elementName);
|
|
316
467
|
}
|
|
317
468
|
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
this.activation_count.delete(cur_state);
|
|
321
|
-
} else {
|
|
322
|
-
this.activation_count.set(cur_state, count);
|
|
323
|
-
}
|
|
469
|
+
const counted = cur_state as ActivationCounted;
|
|
470
|
+
counted[activations] = (counted[activations] || 1) - 1;
|
|
324
471
|
if (backup) {
|
|
325
472
|
// cur_state was a nested activation of a state that is still active at an outer level:
|
|
326
473
|
// put the outer element back the way it was. The parent has already consumed the nested
|
|
@@ -338,39 +485,8 @@ export class Xml2Json {
|
|
|
338
485
|
* @internal
|
|
339
486
|
*/
|
|
340
487
|
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;
|
|
488
|
+
this.begin();
|
|
489
|
+
this.write(data);
|
|
490
|
+
return this.end();
|
|
375
491
|
}
|
|
376
492
|
}
|
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 = {
|