node-opcua-json 2.187.9 → 2.187.10

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.
Files changed (30) hide show
  1. package/dist/json_basic_encoding_decoding_data_value.d.ts +15 -2
  2. package/dist/json_basic_encoding_decoding_data_value.d.ts.map +1 -1
  3. package/dist/json_basic_encoding_decoding_data_value.js +15 -10
  4. package/dist/json_basic_encoding_decoding_data_value.js.map +1 -1
  5. package/dist/json_basic_encoding_decoding_extension_object.d.ts +14 -3
  6. package/dist/json_basic_encoding_decoding_extension_object.d.ts.map +1 -1
  7. package/dist/json_basic_encoding_decoding_extension_object.js +161 -35
  8. package/dist/json_basic_encoding_decoding_extension_object.js.map +1 -1
  9. package/dist/json_basic_encoding_decoding_nodeid.d.ts.map +1 -1
  10. package/dist/json_basic_encoding_decoding_nodeid.js +72 -43
  11. package/dist/json_basic_encoding_decoding_nodeid.js.map +1 -1
  12. package/dist/json_basic_encoding_decoding_qualifiedname.d.ts +1 -1
  13. package/dist/json_basic_encoding_decoding_qualifiedname.d.ts.map +1 -1
  14. package/dist/json_basic_encoding_decoding_qualifiedname.js +29 -15
  15. package/dist/json_basic_encoding_decoding_qualifiedname.js.map +1 -1
  16. package/dist/json_basic_encoding_decoding_variant.d.ts +6 -1
  17. package/dist/json_basic_encoding_decoding_variant.d.ts.map +1 -1
  18. package/dist/json_basic_encoding_decoding_variant.js +37 -5
  19. package/dist/json_basic_encoding_decoding_variant.js.map +1 -1
  20. package/dist/json_opcua_namespace.d.ts +26 -0
  21. package/dist/json_opcua_namespace.d.ts.map +1 -0
  22. package/dist/json_opcua_namespace.js +26 -0
  23. package/dist/json_opcua_namespace.js.map +1 -0
  24. package/package.json +2 -2
  25. package/source/json_basic_encoding_decoding_data_value.ts +30 -12
  26. package/source/json_basic_encoding_decoding_extension_object.ts +205 -38
  27. package/source/json_basic_encoding_decoding_nodeid.ts +79 -43
  28. package/source/json_basic_encoding_decoding_qualifiedname.ts +32 -17
  29. package/source/json_basic_encoding_decoding_variant.ts +44 -6
  30. package/source/json_opcua_namespace.ts +26 -0
@@ -25,23 +25,46 @@ import { QualifiedName } from "node-opcua-data-model";
25
25
  import type { ExtensionObjectBuilder } from "./json_basic_encoding_body_functor.js";
26
26
  import { opcuaDecodeURIComponent, opcuaEncodeURIComponent } from "./json_basic_encoding_decoding_nodeid.js";
27
27
  import { JsonEncodingScheme } from "./json_encoding_scheme.js";
28
+ import { OPCUA_NAMESPACE_URI } from "./json_opcua_namespace.js";
28
29
  export interface QualifiedNameJSON {
29
30
  /** The Name component of the QualifiedName. */
30
31
  Name: string;
31
32
  /** The NamespaceIndex component of the QualifiedName encoded as a JSON number. */
32
33
  Uri?: string | number;
33
34
  }
35
+
36
+ /**
37
+ * The string form of Part 6 1.05 clause 5.1.12 (Table 7): no prefix in the OPC UA namespace,
38
+ * `nsu=<uri>;<name>` otherwise, and `<index>:<name>` only when the index has no known URI.
39
+ */
40
+ function qualifiedNameToString(qualifiedName: QualifiedName, namespaceArray?: string[]): string {
41
+ const name = qualifiedName.name || "";
42
+ const namespaceIndex = qualifiedName.namespaceIndex || 0;
43
+ if (namespaceIndex === 0) {
44
+ return name;
45
+ }
46
+ const namespaceUri = namespaceArray?.[namespaceIndex];
47
+ if (!namespaceUri) {
48
+ return `${namespaceIndex}:${name}`;
49
+ }
50
+ return `nsu=${opcuaEncodeURIComponent(namespaceUri)};${name}`;
51
+ }
52
+
34
53
  export function opcuaJsonEncodeQualifiedName(
35
54
  qualifiedName: QualifiedName,
36
55
  scheme = JsonEncodingScheme.Compact,
37
56
  namespaceArray?: string[]
38
- ): QualifiedNameJSON | undefined {
39
- if (!qualifiedName.name || !qualifiedName.namespaceIndex) {
57
+ ): QualifiedNameJSON | string | undefined {
58
+ if (!qualifiedName.name && !qualifiedName.namespaceIndex) {
40
59
  if (scheme === JsonEncodingScheme.Compact) {
41
60
  // Compact encoding does not encode empty QualifiedName
42
61
  return undefined;
43
62
  }
44
63
  }
64
+ if (scheme === JsonEncodingScheme.Compact || scheme === JsonEncodingScheme.Verbose) {
65
+ // 1.05 clause 5.4.2.14: a JSON string, the NamespaceIndex form only on an encoding error
66
+ return qualifiedNameToString(qualifiedName, namespaceArray);
67
+ }
45
68
  const pojo: QualifiedNameJSON = {
46
69
  Name: qualifiedName.name || ""
47
70
  };
@@ -70,22 +93,10 @@ export function opcuaJsonEncodeQualifiedNameAsString(
70
93
  scheme = JsonEncodingScheme.Compact,
71
94
  namespaceArray?: string[]
72
95
  ): string {
73
- const pojo = opcuaJsonEncodeQualifiedName(qualifiedName, scheme, namespaceArray);
74
- if (!pojo) {
96
+ if (!qualifiedName.name && !qualifiedName.namespaceIndex && scheme === JsonEncodingScheme.Compact) {
75
97
  return "";
76
98
  }
77
- if (!pojo.Uri) {
78
- // if Uri is not defined, it means namespaceIndex is 0
79
- return pojo.Name;
80
- }
81
-
82
- const namespaceIndex = typeof pojo.Uri === "number" ? (pojo.Uri as number) : namespaceArray?.indexOf(pojo.Uri);
83
- if (namespaceIndex !== undefined && namespaceIndex >= 0) {
84
- const namespaceUri = namespaceArray ? namespaceArray[namespaceIndex] : pojo.Uri;
85
- const encodedUri = opcuaEncodeURIComponent(namespaceUri as string);
86
- return `nsu=${encodedUri};${pojo.Name}`;
87
- }
88
- return pojo.Name;
99
+ return qualifiedNameToString(qualifiedName, namespaceArray);
89
100
  }
90
101
 
91
102
  export function opcuaJsonDecodeQualifiedName(
@@ -124,7 +135,11 @@ export function opcuaJsonDecodeQualifiedName(
124
135
  }
125
136
  const namespaceUri = opcuaDecodeURIComponent(pojo.substring(4, endIndex));
126
137
  const name = pojo.substring(endIndex + 1);
127
- const namespaceIndex = namespaceArray ? namespaceArray.indexOf(namespaceUri) : 0;
138
+ const namespaceIndex = namespaceUri === OPCUA_NAMESPACE_URI ? 0 : (namespaceArray?.indexOf(namespaceUri) ?? -1);
139
+ if (namespaceIndex < 0) {
140
+ // 1.05 clause 5.4.2.14: an unknown URI gives NamespaceIndex 0 and the raw string as Name
141
+ return new QualifiedName({ name: pojo, namespaceIndex: 0 });
142
+ }
128
143
  return new QualifiedName({
129
144
  name,
130
145
  namespaceIndex
@@ -89,7 +89,12 @@ ncoded as JSON array (see 5.4.5)
89
89
  Value?: VariantJSONBody;
90
90
 
91
91
  /**
92
- * The dimensions of the array encoded as a JSON array of JSON numbers.
92
+ * The dimensions of the array encoded as a JSON array of JSON numbers
93
+ * (Part 6 1.05 clause 5.4.2.17, Table 41).
94
+ */
95
+ Dimensions?: number[];
96
+ /**
97
+ * @deprecated written by node-opcua before it followed Table 41; still accepted when decoding
93
98
  */
94
99
  UaDimensions?: number[];
95
100
  }
@@ -114,6 +119,33 @@ function body(variant: Variant, scheme: JsonEncodingScheme, namespaceArray?: str
114
119
  }
115
120
  }
116
121
 
122
+ /**
123
+ * Turn the flat elements of a matrix into nested JSON arrays, the outer array being the first
124
+ * dimension (Part 6 Annex H.9). The flat order is the one of 5.2.2.16: the higher rank dimensions
125
+ * are serialized first, so the last index varies fastest.
126
+ *
127
+ * Dimensions that do not account for every element (a Variant changed after construction) give
128
+ * back the flat array: every value is kept, rather than short or empty inner arrays.
129
+ */
130
+ function nestMatrix(flat: VariantJSONBodyScalar[], dimensions: number[]): unknown[] {
131
+ if (dimensions.reduce((a, b) => a * b, 1) !== flat.length) {
132
+ return flat;
133
+ }
134
+ return nest(flat, dimensions);
135
+ }
136
+ function nest(flat: VariantJSONBodyScalar[], dimensions: number[]): unknown[] {
137
+ if (dimensions.length <= 1) {
138
+ return flat;
139
+ }
140
+ const [first, ...rest] = dimensions;
141
+ const chunk = rest.reduce((a, b) => a * b, 1);
142
+ const result: unknown[] = [];
143
+ for (let i = 0; i < first; i++) {
144
+ result.push(nest(flat.slice(i * chunk, (i + 1) * chunk), rest));
145
+ }
146
+ return result;
147
+ }
148
+
117
149
  export function opcuaJsonEncodeVariant105(variant: Variant, mode: JsonEncoderMode105, namespaceArray?: string[]): VariantJSON105 {
118
150
  /**
119
151
  * For the non-reversible form, Variant values shall be encoded as a JSON object containing only
@@ -140,7 +172,7 @@ export function opcuaJsonEncodeVariant105(variant: Variant, mode: JsonEncoderMod
140
172
  pojo.Value = Value;
141
173
  }
142
174
  if (variant.arrayType === VariantArrayType.Matrix && variant.dimensions) {
143
- pojo.UaDimensions = [...variant.dimensions];
175
+ pojo.Dimensions = [...variant.dimensions];
144
176
  }
145
177
  if (mode === JsonEncoderMode105.Compact && variant.arrayType === VariantArrayType.Scalar) {
146
178
  // remove default values for compact encoding
@@ -209,8 +241,13 @@ export function opcuaJsonEncodeVariant104(
209
241
  ? JsonEncodingScheme.DeprecatedReversible
210
242
  : JsonEncodingScheme.DeprecatedNonReversible;
211
243
 
212
- if (!reversible && variant.arrayType !== VariantArrayType.Matrix) {
213
- return body(variant, scheme, namespaceArray);
244
+ if (!reversible) {
245
+ const value = body(variant, scheme, namespaceArray);
246
+ if (variant.arrayType === VariantArrayType.Matrix && variant.dimensions) {
247
+ // Annex H.8: Type and Dimensions are dropped, the matrix becomes nested arrays
248
+ return nestMatrix(value as VariantJSONBodyScalar[], variant.dimensions) as VariantJSONBody;
249
+ }
250
+ return value;
214
251
  }
215
252
 
216
253
  const pojo: VariantJSON104 = {
@@ -303,10 +340,11 @@ export function opcuaJsonDecodeVariant105(
303
340
  if (Array.isArray(pojo.Value)) {
304
341
  const value = pojo.Value.map((a) => f(a, builder, namespaceArray));
305
342
 
306
- if (pojo.UaDimensions) {
343
+ const dimensions = pojo.Dimensions ?? pojo.UaDimensions;
344
+ if (dimensions) {
307
345
  return new Variant({
308
346
  arrayType: VariantArrayType.Matrix,
309
- dimensions: pojo.UaDimensions,
347
+ dimensions,
310
348
  dataType,
311
349
  value
312
350
  });
@@ -0,0 +1,26 @@
1
+ /**
2
+ * ==========================================================================
3
+ * Copyright (c) 2021-2026 Sterfive - etienne.rossignon@sterfive.com
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
+ * IN THE SOFTWARE.
22
+ * ==========================================================================
23
+ */
24
+
25
+ /** The URI of the OPC UA namespace: always NamespaceIndex 0, whatever a namespace table says. */
26
+ export const OPCUA_NAMESPACE_URI = "http://opcfoundation.org/UA/";