node-opcua-xml2json 2.176.0 → 2.179.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 +5 -5
- 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 +5 -3
- package/dist/source/fragment_cloner_parser.js.map +1 -1
- package/dist/source/index.d.ts +3 -3
- package/dist/source/index.js +3 -3
- 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/escape.js +9 -14
- package/dist/source/thirdparties/escape.js.map +1 -1
- package/dist/source/thirdparties/parser/lts.d.ts +1 -1
- package/dist/source/thirdparties/parser/lts.js +8 -8
- package/dist/source/thirdparties/parser/lts.js.map +1 -1
- package/dist/source/xml2Json_pojo_tools.js.map +1 -1
- package/dist/source/xml2json.d.ts +4 -4
- package/dist/source/xml2json.js +17 -15
- package/dist/source/xml2json.js.map +1 -1
- package/dist/source/xml2json_pojo.d.ts +2 -2
- package/dist/source/xml2json_pojo.js +6 -3
- package/dist/source/xml2json_pojo.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/source/definition_parser.ts +9 -5
- package/source/fragment_cloner.ts +6 -6
- package/source/fragment_cloner_parser.ts +7 -5
- package/source/index.ts +3 -3
- package/source/nodejs/xml2json_fs.ts +3 -3
- package/source/thirdparties/escape.ts +9 -20
- package/source/thirdparties/parser/lts.ts +18 -24
- package/source/xml2Json_pojo_tools.ts +2 -4
- package/source/xml2json.ts +31 -26
- package/source/xml2json_pojo.ts +8 -5
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { EventEmitter } from "events";
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import type { XmlAttributes } from "../../xml2json";
|
|
4
3
|
import { unescapeXML } from "../escape";
|
|
5
4
|
|
|
6
5
|
const STATE_TEXT = 0;
|
|
@@ -16,17 +15,15 @@ const STATE_CDATA = 9;
|
|
|
16
15
|
const STATE_IGNORE_CDATA = 10;
|
|
17
16
|
|
|
18
17
|
export class SaxLtx extends EventEmitter {
|
|
19
|
-
|
|
20
|
-
|
|
21
18
|
#write: (data: string) => void;
|
|
22
19
|
constructor() {
|
|
23
20
|
super();
|
|
24
21
|
|
|
25
22
|
function _handleTagOpening(
|
|
26
23
|
this: SaxLtx,
|
|
27
|
-
endTag: boolean| undefined,
|
|
28
|
-
tagName: string|undefined,
|
|
29
|
-
attrs:
|
|
24
|
+
endTag: boolean | undefined,
|
|
25
|
+
tagName: string | undefined,
|
|
26
|
+
attrs: XmlAttributes | undefined
|
|
30
27
|
) {
|
|
31
28
|
if (!endTag) {
|
|
32
29
|
this.emit("startElement", tagName, attrs);
|
|
@@ -36,20 +33,20 @@ export class SaxLtx extends EventEmitter {
|
|
|
36
33
|
} else {
|
|
37
34
|
this.emit("endElement", tagName, false);
|
|
38
35
|
}
|
|
39
|
-
}
|
|
36
|
+
}
|
|
40
37
|
let state = STATE_TEXT;
|
|
41
38
|
let remainder: string | null = null;
|
|
42
39
|
let parseRemainder: boolean = false;
|
|
43
40
|
let tagName: string | undefined;
|
|
44
|
-
let attrs:
|
|
45
|
-
let endTag: boolean|undefined;
|
|
41
|
+
let attrs: XmlAttributes | undefined;
|
|
42
|
+
let endTag: boolean | undefined;
|
|
46
43
|
let selfClosing: boolean | undefined;
|
|
47
44
|
let attrQuote: number;
|
|
48
45
|
let attrQuoteChar: string;
|
|
49
46
|
let recordStart: number | undefined = 0;
|
|
50
|
-
let attrName: string|undefined;
|
|
47
|
+
let attrName: string | undefined;
|
|
51
48
|
|
|
52
|
-
this.#write = function write(data: string
|
|
49
|
+
this.#write = function write(data: string) {
|
|
53
50
|
let pos = 0;
|
|
54
51
|
|
|
55
52
|
/* Anything from previous write()? */
|
|
@@ -66,7 +63,7 @@ export class SaxLtx extends EventEmitter {
|
|
|
66
63
|
recordStart = undefined;
|
|
67
64
|
return recorded;
|
|
68
65
|
}
|
|
69
|
-
return
|
|
66
|
+
return undefined;
|
|
70
67
|
}
|
|
71
68
|
|
|
72
69
|
for (; pos < data.length; pos++) {
|
|
@@ -149,10 +146,7 @@ export class SaxLtx extends EventEmitter {
|
|
|
149
146
|
if (data.substring(pos + 1, 7) === "[CDATA[") {
|
|
150
147
|
recordStart = pos + 8;
|
|
151
148
|
state = STATE_CDATA;
|
|
152
|
-
} else if (
|
|
153
|
-
data.length < pos + 8 &&
|
|
154
|
-
"[CDATA[".startsWith(data.slice(pos + 1))
|
|
155
|
-
) {
|
|
149
|
+
} else if (data.length < pos + 8 && "[CDATA[".startsWith(data.slice(pos + 1))) {
|
|
156
150
|
// We potentially have CDATA, but the chunk is ending; stop here and let the next write() decide
|
|
157
151
|
parseRemainder = true;
|
|
158
152
|
pos = data.length;
|
|
@@ -191,7 +185,7 @@ export class SaxLtx extends EventEmitter {
|
|
|
191
185
|
break;
|
|
192
186
|
case STATE_TAG:
|
|
193
187
|
if (c === 62 /* > */) {
|
|
194
|
-
_handleTagOpening.call(this, endTag, tagName, attrs
|
|
188
|
+
_handleTagOpening.call(this, endTag, tagName, attrs);
|
|
195
189
|
tagName = undefined;
|
|
196
190
|
attrs = undefined;
|
|
197
191
|
endTag = undefined;
|
|
@@ -227,8 +221,10 @@ export class SaxLtx extends EventEmitter {
|
|
|
227
221
|
break;
|
|
228
222
|
case STATE_ATTR_VALUE:
|
|
229
223
|
if (c === attrQuote) {
|
|
230
|
-
const
|
|
231
|
-
(
|
|
224
|
+
const recorded = endRecording();
|
|
225
|
+
if (recorded !== undefined && attrName !== undefined) {
|
|
226
|
+
(attrs as XmlAttributes)[attrName] = unescapeXML(recorded);
|
|
227
|
+
}
|
|
232
228
|
attrName = undefined;
|
|
233
229
|
state = STATE_TAG;
|
|
234
230
|
}
|
|
@@ -243,7 +239,6 @@ export class SaxLtx extends EventEmitter {
|
|
|
243
239
|
};
|
|
244
240
|
}
|
|
245
241
|
|
|
246
|
-
|
|
247
242
|
public write(data: string | Buffer) {
|
|
248
243
|
this.#write(data.toString());
|
|
249
244
|
}
|
|
@@ -253,7 +248,6 @@ export class SaxLtx extends EventEmitter {
|
|
|
253
248
|
}
|
|
254
249
|
|
|
255
250
|
/* Uh, yeah */
|
|
256
|
-
this.write = function write() {
|
|
251
|
+
this.write = function write() {};
|
|
257
252
|
}
|
|
258
253
|
}
|
|
259
|
-
|
|
@@ -5,13 +5,12 @@ export type withPojoLambda = (name: string, pojo: unknown) => void;
|
|
|
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
|
|
package/source/xml2json.ts
CHANGED
|
@@ -3,14 +3,12 @@
|
|
|
3
3
|
* node -> see if https://github.com/isaacs/sax-js could be used instead
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
import { assert } from "node-opcua-assert";
|
|
8
7
|
import { SaxLtx } from "./thirdparties/parser/lts";
|
|
9
8
|
|
|
10
9
|
export type SimpleCallback = (err?: Error) => void;
|
|
11
10
|
export type Callback<T> = (err?: Error | null, result?: T) => void;
|
|
12
11
|
|
|
13
|
-
|
|
14
12
|
export interface Parser {
|
|
15
13
|
[key: string]: ReaderState;
|
|
16
14
|
}
|
|
@@ -52,9 +50,16 @@ export interface ParserLike {
|
|
|
52
50
|
|
|
53
51
|
export interface ReaderStateParserLike {
|
|
54
52
|
parser?: ParserLike;
|
|
53
|
+
// `this` here is an open extension point — callers across the workspace attach domain-specific
|
|
54
|
+
// shapes (e.g. EUInformationParserLevel2) that only duck-type against ReaderState, so a concrete
|
|
55
|
+
// `this: ReaderState` breaks those call sites structurally.
|
|
56
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
55
57
|
init?: (this: any, name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void;
|
|
58
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
56
59
|
finish?: (this: any) => void;
|
|
60
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
57
61
|
startElement?: (this: any, name: string, attrs: XmlAttributes) => void;
|
|
62
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
58
63
|
endElement?: (this: any, name: string) => void;
|
|
59
64
|
}
|
|
60
65
|
|
|
@@ -72,8 +77,9 @@ export interface IReaderState {
|
|
|
72
77
|
_on_text(text: string): void;
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
|
|
76
|
-
export
|
|
80
|
+
// biome-ignore lint/suspicious/noUnsafeDeclarationMerging: interface adds typed members/overloads for this class
|
|
81
|
+
export class ReaderStateBase {}
|
|
82
|
+
export interface ReaderStateBase extends IReaderState {}
|
|
77
83
|
/**
|
|
78
84
|
* @private
|
|
79
85
|
*/
|
|
@@ -83,9 +89,9 @@ export class ReaderState extends ReaderStateBase {
|
|
|
83
89
|
public _startElement?: (name: string, attrs: XmlAttributes) => void;
|
|
84
90
|
public _endElement?: (name: string) => void;
|
|
85
91
|
|
|
86
|
-
public parser:
|
|
92
|
+
public parser: Parser;
|
|
87
93
|
public attrs?: XmlAttributes;
|
|
88
|
-
public chunks:
|
|
94
|
+
public chunks: string[] = [];
|
|
89
95
|
public text = "";
|
|
90
96
|
public name? = "";
|
|
91
97
|
public level = -1;
|
|
@@ -95,7 +101,7 @@ export class ReaderState extends ReaderStateBase {
|
|
|
95
101
|
|
|
96
102
|
public parent?: IReaderState;
|
|
97
103
|
public root?: Xml2Json;
|
|
98
|
-
public data?:
|
|
104
|
+
public data?: Record<string, unknown>;
|
|
99
105
|
|
|
100
106
|
constructor(options: ReaderStateParser | ReaderState) {
|
|
101
107
|
super();
|
|
@@ -149,7 +155,7 @@ export class ReaderState extends ReaderStateBase {
|
|
|
149
155
|
if (this._startElement) {
|
|
150
156
|
this._startElement(elementName, attrs);
|
|
151
157
|
}
|
|
152
|
-
if (this.engine && Object.
|
|
158
|
+
if (this.engine && Object.hasOwn(this.parser, elementName)) {
|
|
153
159
|
this.engine._promote(this.parser[elementName], level, elementName, attrs);
|
|
154
160
|
}
|
|
155
161
|
}
|
|
@@ -157,7 +163,7 @@ export class ReaderState extends ReaderStateBase {
|
|
|
157
163
|
/**
|
|
158
164
|
* @protected
|
|
159
165
|
*/
|
|
160
|
-
public _on_endElement2(
|
|
166
|
+
public _on_endElement2(_level: number, elementName: string): void {
|
|
161
167
|
if (this._endElement) {
|
|
162
168
|
this._endElement(elementName);
|
|
163
169
|
}
|
|
@@ -181,12 +187,8 @@ export class ReaderState extends ReaderStateBase {
|
|
|
181
187
|
this.chunks = [];
|
|
182
188
|
// this is the end
|
|
183
189
|
this._on_finish();
|
|
184
|
-
if (
|
|
185
|
-
this.
|
|
186
|
-
(this.parent as any).parser &&
|
|
187
|
-
Object.prototype.hasOwnProperty.call((this.parent as any).parser, elementName)
|
|
188
|
-
) {
|
|
189
|
-
this.engine!._demote(this, level, elementName);
|
|
190
|
+
if (this.parent instanceof ReaderState && Object.hasOwn(this.parent.parser, elementName)) {
|
|
191
|
+
this.engine?._demote(this, level, elementName);
|
|
190
192
|
}
|
|
191
193
|
}
|
|
192
194
|
}
|
|
@@ -253,7 +255,7 @@ function resolve_namespace(name: string) {
|
|
|
253
255
|
*/
|
|
254
256
|
export class Xml2Json {
|
|
255
257
|
public currentLevel = 0;
|
|
256
|
-
private state_stack:
|
|
258
|
+
private state_stack: Array<{ state: IReaderState | null; backup: Record<string, unknown> | null }> = [];
|
|
257
259
|
private current_state: IReaderState | null = null;
|
|
258
260
|
private activation_count: Map<IReaderState, number> = new Map();
|
|
259
261
|
|
|
@@ -290,6 +292,10 @@ export class Xml2Json {
|
|
|
290
292
|
|
|
291
293
|
const parent = this.current_state;
|
|
292
294
|
this.current_state = new_state;
|
|
295
|
+
// parent is null only for the root element; every _on_init implementation in this codebase
|
|
296
|
+
// names the parameter `_parent` (unused), so widening the public IReaderState signature to
|
|
297
|
+
// accept null would ripple into every consumer package that already treats it as non-null.
|
|
298
|
+
// biome-ignore lint/style/noNonNullAssertion: see comment above
|
|
293
299
|
this.current_state._on_init(name || "???", attr, parent!, level, this);
|
|
294
300
|
}
|
|
295
301
|
|
|
@@ -299,7 +305,11 @@ export class Xml2Json {
|
|
|
299
305
|
*/
|
|
300
306
|
public _demote(cur_state: IReaderState, level: number, elementName: string): void {
|
|
301
307
|
/// assert(this.current_state === cur_state);
|
|
302
|
-
const
|
|
308
|
+
const popped = this.state_stack.pop();
|
|
309
|
+
if (!popped) {
|
|
310
|
+
throw new Error("_demote called without a matching _promote");
|
|
311
|
+
}
|
|
312
|
+
const { state, backup } = popped;
|
|
303
313
|
this.current_state = state;
|
|
304
314
|
if (this.current_state) {
|
|
305
315
|
this.current_state._on_endElement2(level, elementName);
|
|
@@ -358,14 +368,9 @@ export class Xml2Json {
|
|
|
358
368
|
});
|
|
359
369
|
parser.write(data);
|
|
360
370
|
parser.end("");
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
resolve((this.current_state! as any)._pojo);
|
|
366
|
-
});
|
|
367
|
-
//parser.write(data);
|
|
368
|
-
parser.end(data);
|
|
369
|
-
})*/
|
|
371
|
+
if (!this.current_state) {
|
|
372
|
+
return {};
|
|
373
|
+
}
|
|
374
|
+
return (this.current_state as unknown as { _pojo: Record<string, unknown> })._pojo;
|
|
370
375
|
}
|
|
371
376
|
}
|
package/source/xml2json_pojo.ts
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ReaderState2, type withPojoLambda } from "./xml2Json_pojo_tools";
|
|
2
|
+
import { type IReaderState, type ReaderState, type ReaderStateParser, Xml2Json, type XmlAttributes } from "./xml2json";
|
|
3
3
|
|
|
4
4
|
const json_extractor: ReaderState2 = new ReaderState2();
|
|
5
5
|
export const json_parser: ReaderStateParser = {
|
|
6
6
|
init(this: IReaderState, elementName: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) {
|
|
7
7
|
json_extractor._on_init(elementName, attrs, parent, 0, engine);
|
|
8
8
|
},
|
|
9
|
+
// ReaderStateParser.finish declares `this: IReaderState`, which doesn't expose `.parent` — the
|
|
10
|
+
// runtime `this` here is always the ReaderState instance that owns this parser entry.
|
|
11
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
9
12
|
finish(this: any) {
|
|
10
13
|
this.parent._pojo = json_extractor._pojo;
|
|
11
14
|
}
|
|
12
15
|
};
|
|
13
16
|
|
|
14
17
|
export function startPojo(pThis: ReaderState, elementName: string, attrs: XmlAttributes, withPojo: withPojoLambda): void {
|
|
15
|
-
pThis.engine
|
|
16
|
-
json_extractor._withPojo = (name: string, pojo:
|
|
18
|
+
pThis.engine?._promote(json_extractor, pThis.engine?.currentLevel, elementName, attrs);
|
|
19
|
+
json_extractor._withPojo = (name: string, pojo: unknown) => {
|
|
17
20
|
withPojo(name, pojo);
|
|
18
|
-
pThis.engine
|
|
21
|
+
pThis.engine?._demote(json_extractor, pThis.engine?.currentLevel, elementName);
|
|
19
22
|
};
|
|
20
23
|
}
|
|
21
24
|
|