node-opcua-json 2.187.9 → 2.187.11
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/json_basic_encoding_decoding_data_value.d.ts +15 -2
- package/dist/json_basic_encoding_decoding_data_value.d.ts.map +1 -1
- package/dist/json_basic_encoding_decoding_data_value.js +15 -10
- package/dist/json_basic_encoding_decoding_data_value.js.map +1 -1
- package/dist/json_basic_encoding_decoding_extension_object.d.ts +14 -3
- package/dist/json_basic_encoding_decoding_extension_object.d.ts.map +1 -1
- package/dist/json_basic_encoding_decoding_extension_object.js +161 -35
- package/dist/json_basic_encoding_decoding_extension_object.js.map +1 -1
- package/dist/json_basic_encoding_decoding_nodeid.d.ts.map +1 -1
- package/dist/json_basic_encoding_decoding_nodeid.js +72 -43
- package/dist/json_basic_encoding_decoding_nodeid.js.map +1 -1
- package/dist/json_basic_encoding_decoding_qualifiedname.d.ts +1 -1
- package/dist/json_basic_encoding_decoding_qualifiedname.d.ts.map +1 -1
- package/dist/json_basic_encoding_decoding_qualifiedname.js +29 -15
- package/dist/json_basic_encoding_decoding_qualifiedname.js.map +1 -1
- package/dist/json_basic_encoding_decoding_variant.d.ts +6 -1
- package/dist/json_basic_encoding_decoding_variant.d.ts.map +1 -1
- package/dist/json_basic_encoding_decoding_variant.js +37 -5
- package/dist/json_basic_encoding_decoding_variant.js.map +1 -1
- package/dist/json_opcua_namespace.d.ts +26 -0
- package/dist/json_opcua_namespace.d.ts.map +1 -0
- package/dist/json_opcua_namespace.js +26 -0
- package/dist/json_opcua_namespace.js.map +1 -0
- package/package.json +3 -3
- package/source/json_basic_encoding_decoding_data_value.ts +30 -12
- package/source/json_basic_encoding_decoding_extension_object.ts +205 -38
- package/source/json_basic_encoding_decoding_nodeid.ts +79 -43
- package/source/json_basic_encoding_decoding_qualifiedname.ts +32 -17
- package/source/json_basic_encoding_decoding_variant.ts +44 -6
- package/source/json_opcua_namespace.ts +26 -0
|
@@ -45,6 +45,12 @@ import {
|
|
|
45
45
|
import { JsonEncoderMode104, JsonEncoderMode105, JsonEncodingScheme, toJsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
46
46
|
|
|
47
47
|
export interface DataValueStatusAndTimestamps {
|
|
48
|
+
/**
|
|
49
|
+
* The status associated with the value (1.04 Table 31, 1.05 Table 42).
|
|
50
|
+
* Not encoded if the value is Good (0).
|
|
51
|
+
*/
|
|
52
|
+
Status?: StatusCodeJSON | number;
|
|
53
|
+
/** @deprecated written by node-opcua before it followed Tables 31 and 42; still decoded */
|
|
48
54
|
StatusCode?: StatusCodeJSON | number;
|
|
49
55
|
|
|
50
56
|
/**
|
|
@@ -53,20 +59,27 @@ export interface DataValueStatusAndTimestamps {
|
|
|
53
59
|
*/
|
|
54
60
|
SourceTimestamp?: Date; // DateTime
|
|
55
61
|
/**
|
|
56
|
-
* The number of 10 Picosecond intervals for the SourceTimestamp.
|
|
62
|
+
* The number of 10 Picosecond intervals for the SourceTimestamp (1.05 Table 42).
|
|
57
63
|
* Not encoded if the value is 0.
|
|
58
64
|
*/
|
|
59
65
|
SourcePicoseconds?: UInt16;
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated decoded only. 1.04 Table 31 spells it so, while the XML schema of the same
|
|
68
|
+
* document and every table of 1.05 spell SourcePicoseconds, which is what is written.
|
|
69
|
+
*/
|
|
70
|
+
SourcePicoSeconds?: UInt16;
|
|
60
71
|
/**
|
|
61
72
|
* The server timestamp associated with the value.
|
|
62
73
|
* Not encoded if the value is DateTime.MinValue.
|
|
63
74
|
*/
|
|
64
75
|
ServerTimestamp?: Date;
|
|
65
76
|
/**
|
|
66
|
-
* The number of 10 Picosecond intervals for the ServerTimestamp.
|
|
77
|
+
* The number of 10 Picosecond intervals for the ServerTimestamp (1.05 Table 42).
|
|
67
78
|
* Not encoded if the value is 0.
|
|
68
79
|
*/
|
|
69
80
|
ServerPicoseconds?: UInt16;
|
|
81
|
+
/** @deprecated decoded only: the 1.04 Table 31 spelling of ServerPicoseconds (see SourcePicoSeconds). */
|
|
82
|
+
ServerPicoSeconds?: UInt16;
|
|
70
83
|
}
|
|
71
84
|
|
|
72
85
|
// https://reference.opcfoundation.org/Core/Part6/v104/docs/5.4.2.18
|
|
@@ -100,16 +113,16 @@ function _setTimestampsAndStatusCode(
|
|
|
100
113
|
// https://reference.opcfoundation.org/Core/Part6/v105/docs/5.4
|
|
101
114
|
switch (mode) {
|
|
102
115
|
case JsonEncodingScheme.DeprecatedNonReversible:
|
|
103
|
-
pojo.
|
|
116
|
+
pojo.Status = opcuaJsonEncodeStatusCode104(dataValue.statusCode, JsonEncoderMode104.NonReversible);
|
|
104
117
|
break;
|
|
105
118
|
case JsonEncodingScheme.DeprecatedReversible:
|
|
106
|
-
pojo.
|
|
119
|
+
pojo.Status = opcuaJsonEncodeStatusCode104(dataValue.statusCode, JsonEncoderMode104.Reversible);
|
|
107
120
|
break;
|
|
108
121
|
case JsonEncodingScheme.Verbose:
|
|
109
|
-
pojo.
|
|
122
|
+
pojo.Status = opcuaJsonEncodeStatusCode105(dataValue.statusCode, JsonEncoderMode105.Verbose);
|
|
110
123
|
break;
|
|
111
124
|
case JsonEncodingScheme.Compact:
|
|
112
|
-
pojo.
|
|
125
|
+
pojo.Status = opcuaJsonEncodeStatusCode105(dataValue.statusCode, JsonEncoderMode105.Compact);
|
|
113
126
|
break;
|
|
114
127
|
default:
|
|
115
128
|
throw new Error("Invalid JsonEncodingScheme");
|
|
@@ -117,11 +130,15 @@ function _setTimestampsAndStatusCode(
|
|
|
117
130
|
}
|
|
118
131
|
}
|
|
119
132
|
function _decodeTimestampsAndStatusCodeJson(pojo: DataValueStatusAndTimestamps, dataValueOpt: DataValueOptions) {
|
|
120
|
-
|
|
121
|
-
|
|
133
|
+
// both spellings of each field are accepted: the 1.04 one, the 1.05 one, and StatusCode,
|
|
134
|
+
// which node-opcua wrote before it followed Tables 31 and 42
|
|
135
|
+
const serverPicoseconds = pojo.ServerPicoseconds ?? pojo.ServerPicoSeconds;
|
|
136
|
+
if (serverPicoseconds) {
|
|
137
|
+
dataValueOpt.serverPicoseconds = serverPicoseconds;
|
|
122
138
|
}
|
|
123
|
-
|
|
124
|
-
|
|
139
|
+
const sourcePicoseconds = pojo.SourcePicoseconds ?? pojo.SourcePicoSeconds;
|
|
140
|
+
if (sourcePicoseconds) {
|
|
141
|
+
dataValueOpt.sourcePicoseconds = sourcePicoseconds;
|
|
125
142
|
}
|
|
126
143
|
if (pojo.ServerTimestamp) {
|
|
127
144
|
dataValueOpt.serverTimestamp = new Date(pojo.ServerTimestamp);
|
|
@@ -129,8 +146,9 @@ function _decodeTimestampsAndStatusCodeJson(pojo: DataValueStatusAndTimestamps,
|
|
|
129
146
|
if (pojo.SourceTimestamp) {
|
|
130
147
|
dataValueOpt.sourceTimestamp = new Date(pojo.SourceTimestamp);
|
|
131
148
|
}
|
|
132
|
-
|
|
133
|
-
|
|
149
|
+
const status = pojo.Status ?? pojo.StatusCode;
|
|
150
|
+
if (status) {
|
|
151
|
+
dataValueOpt.statusCode = opcuaJsonDecodeStatusCode(status);
|
|
134
152
|
}
|
|
135
153
|
}
|
|
136
154
|
|
|
@@ -32,9 +32,10 @@ import {
|
|
|
32
32
|
type FieldType,
|
|
33
33
|
getStandardDataTypeFactory,
|
|
34
34
|
type IStructuredTypeSchema,
|
|
35
|
-
type StructuredTypeSchema
|
|
35
|
+
type StructuredTypeSchema,
|
|
36
|
+
type StructureInfo
|
|
36
37
|
} from "node-opcua-factory";
|
|
37
|
-
import
|
|
38
|
+
import { NodeId } from "node-opcua-nodeid";
|
|
38
39
|
import { NumericRange } from "node-opcua-numeric-range";
|
|
39
40
|
import { DataType, type VariantOptions } from "node-opcua-variant";
|
|
40
41
|
import type { Pojo } from "./decode_opcua_json.js";
|
|
@@ -56,11 +57,22 @@ export interface ExtensionObjectJSON104 {
|
|
|
56
57
|
UaTypeId?: never;
|
|
57
58
|
UaBody?: never;
|
|
58
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* 1.05 clause 5.4.2.16: the fields of the Structure, with the UaTypeId field inserted among them.
|
|
62
|
+
* UaEncoding and UaBody only carry a UA Binary or UA XML body (Table 40).
|
|
63
|
+
*/
|
|
59
64
|
export interface ExtensionObjectJSON105 {
|
|
60
65
|
UaTypeId: NodeIdJSON;
|
|
61
|
-
UaBody
|
|
66
|
+
/** 1 for a UA Binary UaBody, 2 for a UA XML one; omitted for a JSON encoded Structure */
|
|
67
|
+
UaEncoding?: 1 | 2;
|
|
68
|
+
/**
|
|
69
|
+
* A UA Binary or UA XML encoded Structure. node-opcua used to nest a JSON encoded Structure
|
|
70
|
+
* here: that form is still decoded.
|
|
71
|
+
*/
|
|
72
|
+
UaBody?: Pojo | string;
|
|
62
73
|
TypeId?: never;
|
|
63
74
|
Body?: never;
|
|
75
|
+
[field: string]: unknown;
|
|
64
76
|
}
|
|
65
77
|
export type ExtensionObjectJSON = ExtensionObjectJSON104 | ExtensionObjectJSON105;
|
|
66
78
|
|
|
@@ -188,23 +200,33 @@ function getAllExtensionObjectFields(extensionObject: ExtensionObject) {
|
|
|
188
200
|
interface ExtensionObjectWithOptionalSwitchField extends ExtensionObject {
|
|
189
201
|
switchField?: number;
|
|
190
202
|
}
|
|
191
|
-
export function makeBody(
|
|
203
|
+
export function makeBody(
|
|
204
|
+
extensionObject: ExtensionObject,
|
|
205
|
+
scheme: JsonEncodingScheme,
|
|
206
|
+
namespaceArray?: string[]
|
|
207
|
+
): Pojo | undefined {
|
|
192
208
|
const switchField = (extensionObject as ExtensionObjectWithOptionalSwitchField).switchField;
|
|
193
|
-
const
|
|
209
|
+
const fields = getAllExtensionObjectFields(extensionObject);
|
|
210
|
+
if (switchField !== undefined && isUnionSchema(fields)) {
|
|
211
|
+
return makeUnionBody(extensionObject, fields, switchField, scheme, namespaceArray);
|
|
212
|
+
}
|
|
194
213
|
|
|
195
214
|
const pojo: Pojo = {};
|
|
196
|
-
if (isUnion) {
|
|
197
|
-
pojo.SwitchField = switchField;
|
|
198
|
-
}
|
|
199
|
-
const fields = getAllExtensionObjectFields(extensionObject);
|
|
200
215
|
|
|
201
216
|
let allAreDefault = true;
|
|
217
|
+
// optional fields (switchBit): an undefined value is an absent field, any other value sets
|
|
218
|
+
// the field's bit in the EncodingMask, even when the value itself is omitted as a default
|
|
219
|
+
const hasOptionalFields = fields.some((field) => field.switchBit !== undefined);
|
|
220
|
+
let encodingMask = 0;
|
|
202
221
|
|
|
203
222
|
for (const field of fields) {
|
|
204
223
|
const value = (extensionObject as unknown as Record<string, unknown>)[field.name];
|
|
205
224
|
const encoder = findEncoder(field);
|
|
206
225
|
|
|
207
|
-
const fieldName =
|
|
226
|
+
const fieldName = field.originalName;
|
|
227
|
+
if (field.switchBit !== undefined && value !== undefined) {
|
|
228
|
+
encodingMask |= 1 << field.switchBit;
|
|
229
|
+
}
|
|
208
230
|
|
|
209
231
|
// istanbul ignore next
|
|
210
232
|
if (!encoder) {
|
|
@@ -223,12 +245,14 @@ export function makeBody(extensionObject: ExtensionObject, scheme: JsonEncodingS
|
|
|
223
245
|
} else {
|
|
224
246
|
const valueAsArray = value as unknown[];
|
|
225
247
|
// in array default value must be explicitly set to default
|
|
226
|
-
pojo[fieldName] = valueAsArray.map((a: unknown) =>
|
|
248
|
+
pojo[fieldName] = valueAsArray.map((a: unknown) =>
|
|
249
|
+
a !== undefined && a !== null ? encoder(a, scheme, namespaceArray) : null
|
|
250
|
+
);
|
|
227
251
|
allAreDefault = false;
|
|
228
252
|
}
|
|
229
253
|
} else {
|
|
230
254
|
if (value !== undefined) {
|
|
231
|
-
const fieldValue = encoder(value, scheme);
|
|
255
|
+
const fieldValue = encoder(value, scheme, namespaceArray);
|
|
232
256
|
if (fieldValue !== undefined) {
|
|
233
257
|
pojo[fieldName] = fieldValue;
|
|
234
258
|
if (allAreDefault && !isDefault(field, fieldValue)) {
|
|
@@ -238,10 +262,63 @@ export function makeBody(extensionObject: ExtensionObject, scheme: JsonEncodingS
|
|
|
238
262
|
}
|
|
239
263
|
}
|
|
240
264
|
}
|
|
241
|
-
|
|
265
|
+
// 1.04 Table 33 (reversible) and 1.05 Table 46 (Compact) carry the EncodingMask;
|
|
266
|
+
// 1.04 NonReversible and 1.05 Verbose show presence by the fields themselves
|
|
267
|
+
const writesEncodingMask =
|
|
268
|
+
hasOptionalFields && (scheme === JsonEncodingScheme.DeprecatedReversible || scheme === JsonEncodingScheme.Compact);
|
|
269
|
+
if (allAreDefault && scheme === JsonEncodingScheme.Compact && encodingMask === 0) {
|
|
242
270
|
return undefined;
|
|
243
271
|
}
|
|
244
|
-
return pojo;
|
|
272
|
+
return writesEncodingMask ? { EncodingMask: encodingMask, ...pojo } : pojo;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** A Union schema has one field per choice, each with a switchValue, beside the switch field. */
|
|
276
|
+
function isUnionSchema(fields: FieldType[]): boolean {
|
|
277
|
+
return fields.some((field) => field.switchValue !== undefined);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* A Union, in the shape of each encoding:
|
|
282
|
+
* - 1.04 Reversible (Table 34, Annex H.10): `{ SwitchField, Value }`, nothing when the switch is 0;
|
|
283
|
+
* - 1.04 NonReversible (Annex H.10): the value of the current field, null when the switch is 0;
|
|
284
|
+
* - 1.05 Compact (5.4.8, Table 48): `{ SwitchField, <FieldName> }`, `{}` when the switch is 0;
|
|
285
|
+
* - 1.05 Verbose (5.4.8): `{ <FieldName> }`, `{}` when the switch is 0.
|
|
286
|
+
*/
|
|
287
|
+
function makeUnionBody(
|
|
288
|
+
extensionObject: ExtensionObject,
|
|
289
|
+
fields: FieldType[],
|
|
290
|
+
switchField: number,
|
|
291
|
+
scheme: JsonEncodingScheme,
|
|
292
|
+
namespaceArray?: string[]
|
|
293
|
+
): Pojo | undefined {
|
|
294
|
+
const field = fields.find((f) => f.switchValue === switchField);
|
|
295
|
+
if (!field) {
|
|
296
|
+
return scheme === JsonEncodingScheme.DeprecatedNonReversible ? (null as unknown as Pojo) : {};
|
|
297
|
+
}
|
|
298
|
+
const encoder = findEncoder(field);
|
|
299
|
+
// istanbul ignore next
|
|
300
|
+
if (!encoder) {
|
|
301
|
+
errorLog("cannot find encoder for field", field);
|
|
302
|
+
return {};
|
|
303
|
+
}
|
|
304
|
+
const value = (extensionObject as unknown as Record<string, unknown>)[field.name];
|
|
305
|
+
const encoded = field.isArray
|
|
306
|
+
? ((value as unknown[] | null | undefined) ?? []).map((a) =>
|
|
307
|
+
a !== undefined && a !== null ? encoder(a, scheme, namespaceArray) : null
|
|
308
|
+
)
|
|
309
|
+
: value === undefined || value === null
|
|
310
|
+
? null
|
|
311
|
+
: encoder(value, scheme, namespaceArray);
|
|
312
|
+
switch (scheme) {
|
|
313
|
+
case JsonEncodingScheme.DeprecatedReversible:
|
|
314
|
+
return { SwitchField: switchField, Value: encoded } as Pojo;
|
|
315
|
+
case JsonEncodingScheme.DeprecatedNonReversible:
|
|
316
|
+
return encoded as Pojo;
|
|
317
|
+
case JsonEncodingScheme.Compact:
|
|
318
|
+
return { SwitchField: switchField, [field.originalName]: encoded } as Pojo;
|
|
319
|
+
default:
|
|
320
|
+
return { [field.originalName]: encoded } as Pojo;
|
|
321
|
+
}
|
|
245
322
|
}
|
|
246
323
|
|
|
247
324
|
function isDefault(field: FieldType, value: unknown): boolean {
|
|
@@ -354,19 +431,38 @@ function adaptBody(
|
|
|
354
431
|
|
|
355
432
|
const pojo: Pojo = {};
|
|
356
433
|
const allFields = getAllExtensionObjectFields1(schema);
|
|
357
|
-
const isUnion = body.SwitchField !== undefined;
|
|
358
434
|
|
|
359
|
-
if (
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
const
|
|
363
|
-
|
|
435
|
+
if (isUnionSchema(allFields)) {
|
|
436
|
+
// every form is accepted: { SwitchField, Value } (1.04), { SwitchField, <FieldName> }
|
|
437
|
+
// (1.05 Compact), { <FieldName> } (1.05 Verbose) and {} (no field selected)
|
|
438
|
+
const choices = allFields.filter((f) => f.switchValue !== undefined);
|
|
439
|
+
const switchValue = body.SwitchField as number | undefined;
|
|
440
|
+
const field =
|
|
441
|
+
switchValue !== undefined
|
|
442
|
+
? choices.find((f) => f.switchValue === switchValue)
|
|
443
|
+
: choices.find((f) => body[f.originalName] !== undefined);
|
|
444
|
+
if (!field) {
|
|
445
|
+
return pojo;
|
|
446
|
+
}
|
|
447
|
+
const value = body[field.originalName] !== undefined ? body[field.originalName] : body.Value;
|
|
448
|
+
build(field, value, findDecoder(field));
|
|
364
449
|
return pojo;
|
|
365
450
|
}
|
|
451
|
+
// with an EncodingMask (1.04 Table 33, 1.05 Table 46) an optional field is present when its bit
|
|
452
|
+
// is set, and takes its default value if Compact omitted it; without one, when its name is there
|
|
453
|
+
const encodingMask = typeof body.EncodingMask === "number" ? body.EncodingMask : undefined;
|
|
366
454
|
for (const field of allFields) {
|
|
367
455
|
const value = body[field.originalName];
|
|
456
|
+
const bitSet =
|
|
457
|
+
encodingMask !== undefined && field.switchBit !== undefined ? (encodingMask & (1 << field.switchBit)) !== 0 : undefined;
|
|
458
|
+
if (bitSet === false) {
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
368
461
|
// istanbul ignore next
|
|
369
462
|
if (value === undefined) {
|
|
463
|
+
if (bitSet) {
|
|
464
|
+
build(field, defaultFieldJson(field), findDecoder(field));
|
|
465
|
+
}
|
|
370
466
|
continue;
|
|
371
467
|
}
|
|
372
468
|
const decoder = findDecoder(field);
|
|
@@ -374,6 +470,24 @@ function adaptBody(
|
|
|
374
470
|
}
|
|
375
471
|
return pojo;
|
|
376
472
|
|
|
473
|
+
/**
|
|
474
|
+
* The JSON of a present field that the CompactEncoding omitted because it had its default value;
|
|
475
|
+
* it is then decoded like any other field.
|
|
476
|
+
*/
|
|
477
|
+
function defaultFieldJson(field: FieldType): unknown {
|
|
478
|
+
if (field.isArray) {
|
|
479
|
+
return [];
|
|
480
|
+
}
|
|
481
|
+
if (field.category === "complex") {
|
|
482
|
+
return {};
|
|
483
|
+
}
|
|
484
|
+
if (field.category === "enumeration") {
|
|
485
|
+
return 0;
|
|
486
|
+
}
|
|
487
|
+
const defaultValue = (field.schema as { defaultValue?: unknown } | undefined)?.defaultValue;
|
|
488
|
+
return typeof defaultValue === "function" ? defaultValue() : (defaultValue ?? null);
|
|
489
|
+
}
|
|
490
|
+
|
|
377
491
|
function build(field: FieldType, value: unknown, decoder?: DecoderFunc<unknown> | null) {
|
|
378
492
|
if (!decoder) {
|
|
379
493
|
errorLog("Cannot find decoder for", field);
|
|
@@ -411,7 +525,7 @@ export function opcuaJsonDecodeExtensionObjectBody(
|
|
|
411
525
|
export function opcuaJsonEncodeExtensionObjectBody(
|
|
412
526
|
extensionObject: ExtensionObject | null,
|
|
413
527
|
scheme: JsonEncodingScheme,
|
|
414
|
-
|
|
528
|
+
namespaceArray?: string[]
|
|
415
529
|
): ExtensionObjectJSON | Pojo | null | undefined {
|
|
416
530
|
if (!extensionObject) {
|
|
417
531
|
if (scheme === JsonEncodingScheme.Compact) {
|
|
@@ -420,7 +534,7 @@ export function opcuaJsonEncodeExtensionObjectBody(
|
|
|
420
534
|
}
|
|
421
535
|
return null;
|
|
422
536
|
}
|
|
423
|
-
return makeBody(extensionObject, scheme);
|
|
537
|
+
return makeBody(extensionObject, scheme, namespaceArray);
|
|
424
538
|
}
|
|
425
539
|
|
|
426
540
|
/**
|
|
@@ -461,10 +575,15 @@ export function opcuaJsonEncodeExtensionObject(
|
|
|
461
575
|
|
|
462
576
|
function singleItem(extensionObject: ExtensionObject): ExtensionObjectJSON | Pojo {
|
|
463
577
|
const body = opcuaJsonEncodeExtensionObjectBody(extensionObject, scheme, namespaceArray);
|
|
464
|
-
const
|
|
578
|
+
const typeNodeId =
|
|
579
|
+
scheme === JsonEncodingScheme.DeprecatedReversible
|
|
580
|
+
? typeIdOfReversibleEncoding(extensionObject.schema)
|
|
581
|
+
: extensionObject.schema.dataTypeNodeId;
|
|
582
|
+
const typeId = opcuaJsonEncodeNodeId(typeNodeId, scheme, namespaceArray);
|
|
465
583
|
switch (scheme) {
|
|
466
584
|
case JsonEncodingScheme.DeprecatedNonReversible:
|
|
467
|
-
|
|
585
|
+
// null stays null: a Union with no field selected (Annex H.10)
|
|
586
|
+
return body === undefined ? {} : (body as Pojo);
|
|
468
587
|
case JsonEncodingScheme.DeprecatedReversible:
|
|
469
588
|
return {
|
|
470
589
|
TypeId: typeId,
|
|
@@ -472,24 +591,68 @@ export function opcuaJsonEncodeExtensionObject(
|
|
|
472
591
|
};
|
|
473
592
|
case JsonEncodingScheme.Verbose:
|
|
474
593
|
case JsonEncodingScheme.Compact:
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
return {
|
|
481
|
-
UaTypeId: typeId,
|
|
482
|
-
UaBody: body
|
|
483
|
-
// UaEncoding: 0, // always 0 for ExtensionObject in JSON (omitted if Json)
|
|
484
|
-
// use UaEncoding: 1 for binary encoding => Base64 encoded ByteString
|
|
485
|
-
// use UaEncoding: 2 for binary encoding => string as XmlElement
|
|
486
|
-
};
|
|
594
|
+
// 1.05 clause 5.4.2.16: the fields of the Structure with UaTypeId inserted among
|
|
595
|
+
// them. UaEncoding and UaBody are for UA Binary or UA XML bodies only (Table 40).
|
|
596
|
+
return { UaTypeId: typeId, ...((body as Pojo | null | undefined) ?? {}) } as ExtensionObjectJSON105;
|
|
487
597
|
default:
|
|
488
598
|
throw new Error(`Unknown JsonEncodingScheme ${scheme}`);
|
|
489
599
|
}
|
|
490
600
|
}
|
|
491
601
|
}
|
|
492
602
|
|
|
603
|
+
/**
|
|
604
|
+
* 1.04 Table 29: the TypeId of the reversible encoding is the DataTypeEncoding Node of the JSON
|
|
605
|
+
* body (its "Default JSON" encoding). It is used in namespace 0, where a decoder finds the type from
|
|
606
|
+
* it in the standard factory. Other namespaces keep the DataType Node, which 1.05 Annex H.7 also
|
|
607
|
+
* allows: a decoder resolves them through its ExtensionObjectBuilder, which works on DataType Nodes.
|
|
608
|
+
*/
|
|
609
|
+
function typeIdOfReversibleEncoding(schema: IStructuredTypeSchema): NodeId {
|
|
610
|
+
const jsonEncoding = schema.encodingDefaultJson;
|
|
611
|
+
if (schema.dataTypeNodeId.namespace === 0 && jsonEncoding && jsonEncoding.namespace === 0 && jsonEncoding.value) {
|
|
612
|
+
return new NodeId(jsonEncoding.identifierType, jsonEncoding.value, 0);
|
|
613
|
+
}
|
|
614
|
+
return schema.dataTypeNodeId;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
let structureInfoByJsonEncoding: Map<string, StructureInfo> | undefined;
|
|
618
|
+
/** The standard structure whose "Default JSON" encoding Node is this one, if any. */
|
|
619
|
+
function findStandardStructureInfoForJsonEncoding(encodingNodeId: NodeId): StructureInfo | null {
|
|
620
|
+
if (!structureInfoByJsonEncoding) {
|
|
621
|
+
structureInfoByJsonEncoding = new Map();
|
|
622
|
+
for (const structureInfo of getStandardDataTypeFactory().getStructureIterator()) {
|
|
623
|
+
const jsonEncoding = structureInfo.schema.encodingDefaultJson;
|
|
624
|
+
if (jsonEncoding?.value) {
|
|
625
|
+
structureInfoByJsonEncoding.set(
|
|
626
|
+
new NodeId(jsonEncoding.identifierType, jsonEncoding.value, 0).toString(),
|
|
627
|
+
structureInfo
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return structureInfoByJsonEncoding.get(encodingNodeId.toString()) ?? null;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* The fields of the Structure, from any of the forms a decoder can meet:
|
|
637
|
+
* 1.04 `{ TypeId, Body }`; the 1.05 form `{ UaTypeId, <fields> }` (clause 5.4.2.16); and
|
|
638
|
+
* `{ UaTypeId, UaBody: { <fields> } }`, which node-opcua wrote before it followed 5.4.2.16.
|
|
639
|
+
*/
|
|
640
|
+
function extensionObjectBody(pojo: ExtensionObjectJSON104 | ExtensionObjectJSON105): Pojo {
|
|
641
|
+
if (pojo.TypeId !== undefined) {
|
|
642
|
+
return pojo.Body as Pojo;
|
|
643
|
+
}
|
|
644
|
+
if (typeof pojo.UaBody === "string") {
|
|
645
|
+
throw new Error(
|
|
646
|
+
`ExtensionObject ${JSON.stringify(pojo.UaTypeId)}: a UA Binary or UA XML UaBody (UaEncoding ${pojo.UaEncoding}) cannot be decoded from JSON`
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
if (pojo.UaBody !== undefined && pojo.UaBody !== null) {
|
|
650
|
+
return pojo.UaBody as Pojo;
|
|
651
|
+
}
|
|
652
|
+
const { UaTypeId: _typeId, UaEncoding: _encoding, UaBody: _body, ...fields } = pojo as ExtensionObjectJSON105;
|
|
653
|
+
return fields as Pojo;
|
|
654
|
+
}
|
|
655
|
+
|
|
493
656
|
export function opcuaJsonDecodeExtensionObject(
|
|
494
657
|
_pojo: ExtensionObjectJSON104 | ExtensionObjectJSON105,
|
|
495
658
|
builder: ExtensionObjectBuilder,
|
|
@@ -499,11 +662,15 @@ export function opcuaJsonDecodeExtensionObject(
|
|
|
499
662
|
return null;
|
|
500
663
|
}
|
|
501
664
|
const dataTypeNodeId = opcuaJsonDecodeNodeId(_pojo.TypeId || _pojo.UaTypeId, builder, namespaceArray);
|
|
502
|
-
const body = _pojo
|
|
665
|
+
const body = extensionObjectBody(_pojo);
|
|
503
666
|
|
|
504
667
|
if (dataTypeNodeId.namespace === 0) {
|
|
505
668
|
const factory = getStandardDataTypeFactory();
|
|
506
|
-
|
|
669
|
+
// the TypeId is the DataType Node, or (1.04 Table 29) its Default JSON encoding Node
|
|
670
|
+
const schema =
|
|
671
|
+
factory.getStructureInfoForDataType(dataTypeNodeId) ??
|
|
672
|
+
findStandardStructureInfoForJsonEncoding(dataTypeNodeId) ??
|
|
673
|
+
factory.findStructureInfoForDataType(dataTypeNodeId);
|
|
507
674
|
if (!schema) return null;
|
|
508
675
|
const Constructor = schema.constructor;
|
|
509
676
|
if (!Constructor) {
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import { coerceNodeId, ExpandedNodeId, NodeId, NodeIdType } from "node-opcua-nodeid"; // -nodeid";
|
|
25
25
|
import type { ExtensionObjectBuilder } from "./json_basic_encoding_body_functor.js";
|
|
26
26
|
import { JsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
27
|
+
import { OPCUA_NAMESPACE_URI } from "./json_opcua_namespace.js";
|
|
27
28
|
|
|
28
29
|
// from https://reference.opcfoundation.org/Core/Part6/v105/docs/5.4
|
|
29
30
|
|
|
@@ -144,7 +145,7 @@ export function opcuaJsonEncodeNodeId104(
|
|
|
144
145
|
export function opcuaJsonEncodeNodeIdAsString(value: NodeId, namespaceArray: string[]): string {
|
|
145
146
|
const namespace = namespaceArray ? namespaceArray[value.namespace] : undefined;
|
|
146
147
|
if (namespace) {
|
|
147
|
-
if (namespace ===
|
|
148
|
+
if (namespace === OPCUA_NAMESPACE_URI) {
|
|
148
149
|
return `${nodeIdValueToString(value)}`;
|
|
149
150
|
}
|
|
150
151
|
const encodedNamespace = opcuaEncodeURIComponent(namespace);
|
|
@@ -158,13 +159,19 @@ export function opcuaJsonEncodeNodeIdAsString(value: NodeId, namespaceArray: str
|
|
|
158
159
|
}
|
|
159
160
|
}
|
|
160
161
|
export function opcuaJsonEncodeExpandedNodeIdAsString(value: ExpandedNodeId, namespaceArray: string[]): string {
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (value.namespaceUri) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
162
|
+
// the NamespaceUri of an ExpandedNodeId names its namespace (nsu=); svu= is the URI of a
|
|
163
|
+
// server, which is only known through a ServerArray this encoder does not have
|
|
164
|
+
let result: string;
|
|
165
|
+
if (value.namespaceUri && value.namespaceUri !== OPCUA_NAMESPACE_URI) {
|
|
166
|
+
result = `nsu=${opcuaEncodeURIComponent(value.namespaceUri)};${nodeIdValueToString(value)}`;
|
|
167
|
+
} else if (value.namespaceUri) {
|
|
168
|
+
result = nodeIdValueToString(value);
|
|
169
|
+
} else {
|
|
170
|
+
// the NamespaceIndex of a remote node refers to the remote server's namespace table, which
|
|
171
|
+
// is not the local one: it stays an index (OPC 10000-4 7.16, OPC 10000-6 5.4.2.10)
|
|
172
|
+
result = opcuaJsonEncodeNodeIdAsString(value, value.serverIndex !== 0 ? [] : namespaceArray);
|
|
173
|
+
}
|
|
174
|
+
if (value.serverIndex !== 0) {
|
|
168
175
|
result = `svr=${value.serverIndex};${result}`;
|
|
169
176
|
}
|
|
170
177
|
return result;
|
|
@@ -242,8 +249,11 @@ export function opcuaJsonEncodeExpandedNodeId104(
|
|
|
242
249
|
scheme: JsonEncodingScheme,
|
|
243
250
|
namespaceArray?: string[]
|
|
244
251
|
): NodeIdJSON104 {
|
|
245
|
-
|
|
246
|
-
|
|
252
|
+
// a remote node's NamespaceIndex is not in the local table: it stays a number
|
|
253
|
+
const pojo = opcuaJsonEncodeNodeId104(value, scheme, value.serverIndex !== 0 ? undefined : namespaceArray);
|
|
254
|
+
if (value.namespaceUri) {
|
|
255
|
+
// 1.04 Table 24, Annex H.3: a NamespaceUri that is specified is written as a JSON string,
|
|
256
|
+
// in the reversible form too
|
|
247
257
|
pojo.Namespace = value.namespaceUri;
|
|
248
258
|
}
|
|
249
259
|
if (value.serverIndex !== 0) {
|
|
@@ -265,43 +275,60 @@ export function opcuaDecodeURIComponent(value: string): string {
|
|
|
265
275
|
});
|
|
266
276
|
}
|
|
267
277
|
|
|
268
|
-
function
|
|
278
|
+
function makeNodeIdFromString(identifierType: string, identifierValue: string, namespaceIndex: number, text: string): NodeId {
|
|
279
|
+
switch (identifierType) {
|
|
280
|
+
case "i=":
|
|
281
|
+
return new NodeId(NodeIdType.NUMERIC, parseInt(identifierValue, 10), namespaceIndex);
|
|
282
|
+
case "s=":
|
|
283
|
+
return new NodeId(NodeIdType.STRING, identifierValue, namespaceIndex);
|
|
284
|
+
case "g=":
|
|
285
|
+
return new NodeId(NodeIdType.GUID, identifierValue, namespaceIndex);
|
|
286
|
+
case "b=":
|
|
287
|
+
return new NodeId(NodeIdType.BYTESTRING, Buffer.from(identifierValue, "base64"), namespaceIndex);
|
|
288
|
+
default:
|
|
289
|
+
throw new Error(`Invalid NodeId format: ${text}`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The NamespaceIndex of a NamespaceUri, or -1 when it cannot be mapped.
|
|
295
|
+
* The OPC UA namespace is index 0 whatever the table says.
|
|
296
|
+
*/
|
|
297
|
+
function namespaceUriToIndex(namespaceUri: string, namespaceArray?: string[]): number {
|
|
298
|
+
if (namespaceUri === OPCUA_NAMESPACE_URI) {
|
|
299
|
+
return 0;
|
|
300
|
+
}
|
|
301
|
+
return namespaceArray ? namespaceArray.indexOf(namespaceUri) : -1;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
interface ParsedNodeIdString {
|
|
305
|
+
/** the NamespaceUri of the nsu= form, percent-decoded */
|
|
306
|
+
namespaceUri?: string;
|
|
307
|
+
identifierType: string;
|
|
308
|
+
identifierValue: string;
|
|
309
|
+
}
|
|
310
|
+
function parseNodeIdString(nodeIdAsString: string): ParsedNodeIdString | null {
|
|
269
311
|
//nsu=tag:acme.com,2023:schemas:data#off%3B;b=M/RbKBsRVkePCePcx24oRA==
|
|
270
312
|
const match = nodeIdAsString.match(/^(nsu=([^;]+);)?(g=|s=|i=|b=)(.*)$/);
|
|
271
|
-
if (match) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
namespaceIndex = 0; // NAMESPACE_NOT_FOUND
|
|
280
|
-
} else {
|
|
281
|
-
// A URI which has the RFC 3986 Percent - Encoding applied to it.
|
|
282
|
-
// Any ‘;’ in the URI shall be percent encoded.
|
|
283
|
-
const decodedNamespaceUri = opcuaDecodeURIComponent(namespaceUri);
|
|
284
|
-
namespaceIndex = namespaceArray.indexOf(decodedNamespaceUri);
|
|
285
|
-
if (namespaceIndex < 0) {
|
|
286
|
-
namespaceIndex = 0; // NAMESPACE_NOT_FOUND
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
} else {
|
|
290
|
-
namespaceIndex = 0;
|
|
291
|
-
}
|
|
313
|
+
if (!match) {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
// A URI which has the RFC 3986 Percent - Encoding applied to it.
|
|
317
|
+
// Any ‘;’ in the URI shall be percent encoded.
|
|
318
|
+
const namespaceUri = match[2] ? opcuaDecodeURIComponent(match[2]) : undefined;
|
|
319
|
+
return { namespaceUri, identifierType: match[3], identifierValue: match[4] };
|
|
320
|
+
}
|
|
292
321
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return new NodeId(NodeIdType.BYTESTRING, Buffer.from(identifierValue, "base64"), namespaceIndex);
|
|
302
|
-
default:
|
|
303
|
-
throw new Error(`Invalid NodeId format: ${nodeIdAsString}`);
|
|
322
|
+
function coerceNodeIdEx(nodeIdAsString: string, namespaceArray?: string[]): NodeId {
|
|
323
|
+
const parsed = parseNodeIdString(nodeIdAsString);
|
|
324
|
+
if (parsed) {
|
|
325
|
+
const namespaceIndex = parsed.namespaceUri ? namespaceUriToIndex(parsed.namespaceUri, namespaceArray) : 0;
|
|
326
|
+
if (namespaceIndex < 0) {
|
|
327
|
+
// second abnormal state of 1.05 clause 5.4.2.10: NamespaceIndex 0, IdType String,
|
|
328
|
+
// and the JSON string as Identifier, so the node is not mistaken for one of namespace 0
|
|
329
|
+
return new NodeId(NodeIdType.STRING, nodeIdAsString, 0);
|
|
304
330
|
}
|
|
331
|
+
return makeNodeIdFromString(parsed.identifierType, parsed.identifierValue, namespaceIndex, nodeIdAsString);
|
|
305
332
|
}
|
|
306
333
|
// Fallback to coerceNodeId
|
|
307
334
|
return coerceNodeId(nodeIdAsString);
|
|
@@ -367,6 +394,15 @@ export function opcuaJsonDecodeExpandedNodeId(
|
|
|
367
394
|
builder?: ExtensionObjectBuilder | undefined,
|
|
368
395
|
namespaceArray?: string[]
|
|
369
396
|
): ExpandedNodeId {
|
|
397
|
+
if (typeof pojoOrString === "string") {
|
|
398
|
+
// 1.05 clause 5.4.2.11: with a ServerIndex of 0, a NamespaceUri that cannot be mapped
|
|
399
|
+
// to an index is kept in the NamespaceUri field of the ExpandedNodeId
|
|
400
|
+
const parsed = parseNodeIdString(pojoOrString);
|
|
401
|
+
if (parsed?.namespaceUri && namespaceUriToIndex(parsed.namespaceUri, namespaceArray) < 0) {
|
|
402
|
+
const nodeId = makeNodeIdFromString(parsed.identifierType, parsed.identifierValue, 0, pojoOrString);
|
|
403
|
+
return ExpandedNodeId.fromNodeId(nodeId, parsed.namespaceUri, 0);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
370
406
|
const nodeId = opcuaJsonDecodeNodeId(pojoOrString, builder, namespaceArray);
|
|
371
407
|
// the 1.05 string form carries svr=/svu=, which coerceNodeId does not parse; the 1.04 object
|
|
372
408
|
// form carries ServerUri as the ServerIndex
|