node-opcua-json 2.182.0 → 2.183.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.
Files changed (35) hide show
  1. package/README.md +29 -2
  2. package/dist/104/index.d.ts +57 -0
  3. package/dist/104/index.js +94 -0
  4. package/dist/104/index.js.map +1 -0
  5. package/dist/105/index.d.ts +56 -0
  6. package/dist/105/index.js +96 -0
  7. package/dist/105/index.js.map +1 -0
  8. package/dist/index.d.ts +5 -3
  9. package/dist/index.js +37 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/json_basic_encoding_body_functor.js +6 -4
  12. package/dist/json_basic_encoding_body_functor.js.map +1 -1
  13. package/dist/json_basic_encoding_decoding_data_value.d.ts +2 -1
  14. package/dist/json_basic_encoding_decoding_data_value.js +8 -7
  15. package/dist/json_basic_encoding_decoding_data_value.js.map +1 -1
  16. package/dist/json_basic_encoding_decoding_diagnosticinfo.d.ts +51 -0
  17. package/dist/json_basic_encoding_decoding_diagnosticinfo.js +89 -0
  18. package/dist/json_basic_encoding_decoding_diagnosticinfo.js.map +1 -0
  19. package/dist/json_basic_encoding_decoding_extension_object.js +5 -5
  20. package/dist/json_basic_encoding_decoding_extension_object.js.map +1 -1
  21. package/dist/json_basic_encoding_decoding_nodeid.d.ts +32 -1
  22. package/dist/json_basic_encoding_decoding_nodeid.js +21 -5
  23. package/dist/json_basic_encoding_decoding_nodeid.js.map +1 -1
  24. package/dist/json_basic_encoding_decoding_variant.js +1 -0
  25. package/dist/json_basic_encoding_decoding_variant.js.map +1 -1
  26. package/package.json +53 -17
  27. package/source/104/index.ts +118 -0
  28. package/source/105/index.ts +119 -0
  29. package/source/index.ts +15 -2
  30. package/source/json_basic_encoding_body_functor.ts +6 -4
  31. package/source/json_basic_encoding_decoding_data_value.ts +6 -6
  32. package/source/json_basic_encoding_decoding_diagnosticinfo.ts +129 -0
  33. package/source/json_basic_encoding_decoding_extension_object.ts +5 -5
  34. package/source/json_basic_encoding_decoding_nodeid.ts +29 -10
  35. package/source/json_basic_encoding_decoding_variant.ts +1 -0
@@ -0,0 +1,118 @@
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
+ /**
26
+ * The OPC UA 1.04 JSON encoding (Part 6, "reversible" and "non-reversible" forms), exposed
27
+ * under version-free names: `import { opcuaJsonEncodeDataValue } from "node-opcua-json/104"`.
28
+ *
29
+ * Everything here is locked to 1.04: the encoders take a JsonEncoderMode (Reversible or
30
+ * NonReversible) and the JSON types are the 1.04 shapes (Type/Body, TypeId/Body, NodeId objects).
31
+ * The root "node-opcua-json" entry point dispatches on JsonEncodingScheme and accepts either edition.
32
+ */
33
+
34
+ import type { ExtensionObject } from "node-opcua-extension-object";
35
+ import type { Pojo } from "../decode_opcua_json.js";
36
+ import type { ExtensionObjectBuilder } from "../json_basic_encoding_body_functor.js";
37
+ import {
38
+ type ExtensionObjectJSON104,
39
+ opcuaJsonDecodeExtensionObject as opcuaJsonDecodeExtensionObjectAny,
40
+ opcuaJsonEncodeExtensionObject as opcuaJsonEncodeExtensionObjectAny
41
+ } from "../json_basic_encoding_decoding_extension_object.js";
42
+ import { type JsonEncoderMode104, toJsonEncodingScheme } from "../json_encoding_scheme.js";
43
+
44
+ // version-agnostic parts, shared with the root entry point
45
+ export * from "../decode_opcua_json.js";
46
+ export type { ExtensionObjectConstructorFuncWithSchema } from "../extension_object_constructor.js";
47
+ export * from "../json_basic_encoding_body_functor.js";
48
+ export * from "../json_basic_encoding_decoding_byte_string.js";
49
+ // DataValue
50
+ export {
51
+ type DataValueJSON104 as DataValueJSON,
52
+ type DataValueStatusAndTimestamps,
53
+ opcuaJsonDecodeDataValue104 as opcuaJsonDecodeDataValue,
54
+ opcuaJsonEncodeDataValue104 as opcuaJsonEncodeDataValue,
55
+ opcuaJsonEncodeDataValueMQTT104 as opcuaJsonEncodeDataValueMQTT
56
+ } from "../json_basic_encoding_decoding_data_value.js";
57
+ export * from "../json_basic_encoding_decoding_date_time.js";
58
+ export * from "../json_basic_encoding_decoding_diagnosticinfo.js";
59
+ // ExtensionObject
60
+ export {
61
+ type ExtensionObjectJSON104 as ExtensionObjectJSON,
62
+ makeBody,
63
+ opcuaJsonDecodeExtensionObjectBody,
64
+ opcuaJsonEncodeExtensionObjectBody
65
+ } from "../json_basic_encoding_decoding_extension_object.js";
66
+ export * from "../json_basic_encoding_decoding_int64.js";
67
+ export * from "../json_basic_encoding_decoding_localizedtext.js";
68
+ // NodeId
69
+ export {
70
+ type NodeIdJSON104 as NodeIdJSON,
71
+ opcuaDecodeURIComponent,
72
+ opcuaEncodeURIComponent,
73
+ opcuaJsonDecodeExpandedNodeId,
74
+ opcuaJsonDecodeNodeId,
75
+ opcuaJsonEncodeExpandedNodeId104 as opcuaJsonEncodeExpandedNodeId,
76
+ opcuaJsonEncodeNodeId104 as opcuaJsonEncodeNodeId
77
+ } from "../json_basic_encoding_decoding_nodeid.js";
78
+ export * from "../json_basic_encoding_decoding_qualifiedname.js";
79
+
80
+ // StatusCode
81
+ export {
82
+ opcuaJsonDecodeStatusCode,
83
+ opcuaJsonEncodeStatusCode104 as opcuaJsonEncodeStatusCode,
84
+ type StatusCodeJSON
85
+ } from "../json_basic_encoding_decoding_statuscode.js";
86
+ // Variant
87
+ export {
88
+ opcuaJsonDecodeVariant104 as opcuaJsonDecodeVariant,
89
+ opcuaJsonEncodeVariant104 as opcuaJsonEncodeVariant,
90
+ type VariantJSON104 as VariantJSON,
91
+ type VariantJSONBody,
92
+ type VariantJSONBodyScalar
93
+ } from "../json_basic_encoding_decoding_variant.js";
94
+ export { JsonEncoderMode104 as JsonEncoderMode, JsonEncodingScheme, toJsonEncodingScheme } from "../json_encoding_scheme.js";
95
+
96
+ /**
97
+ * encode an ExtensionObject with the 1.04 rules: Reversible yields { TypeId, Body },
98
+ * NonReversible yields the bare body.
99
+ */
100
+ export function opcuaJsonEncodeExtensionObject(
101
+ extensionObject: ExtensionObject | ExtensionObject[] | null,
102
+ mode: JsonEncoderMode104,
103
+ namespaceArray?: string[]
104
+ ): ExtensionObjectJSON104 | Pojo | (ExtensionObjectJSON104 | Pojo)[] | null {
105
+ return opcuaJsonEncodeExtensionObjectAny(extensionObject, toJsonEncodingScheme(mode), namespaceArray) as
106
+ | ExtensionObjectJSON104
107
+ | Pojo
108
+ | (ExtensionObjectJSON104 | Pojo)[]
109
+ | null;
110
+ }
111
+
112
+ export function opcuaJsonDecodeExtensionObject(
113
+ pojo: ExtensionObjectJSON104,
114
+ builder: ExtensionObjectBuilder,
115
+ namespaceArray: string[]
116
+ ): ExtensionObject | ExtensionObject[] | null {
117
+ return opcuaJsonDecodeExtensionObjectAny(pojo, builder, namespaceArray);
118
+ }
@@ -0,0 +1,119 @@
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
+ /**
26
+ * The OPC UA 1.05 JSON encoding (Part 6, "Compact" and "Verbose" forms), exposed under
27
+ * version-free names: `import { opcuaJsonEncodeDataValue } from "node-opcua-json/105"`.
28
+ *
29
+ * Everything here is locked to 1.05: the encoders take a JsonEncoderMode (Compact or Verbose)
30
+ * and the JSON types are the 1.05 shapes (UaType/Value, UaTypeId/UaBody, NodeId strings).
31
+ * The root "node-opcua-json" entry point dispatches on JsonEncodingScheme and accepts either edition.
32
+ */
33
+
34
+ import type { ExtensionObject } from "node-opcua-extension-object";
35
+ import type { ExtensionObjectBuilder } from "../json_basic_encoding_body_functor.js";
36
+ import {
37
+ type ExtensionObjectJSON105,
38
+ opcuaJsonDecodeExtensionObject as opcuaJsonDecodeExtensionObjectAny,
39
+ opcuaJsonEncodeExtensionObject as opcuaJsonEncodeExtensionObjectAny
40
+ } from "../json_basic_encoding_decoding_extension_object.js";
41
+ import { type JsonEncoderMode105, toJsonEncodingScheme } from "../json_encoding_scheme.js";
42
+
43
+ // version-agnostic parts, shared with the root entry point
44
+ export * from "../decode_opcua_json.js";
45
+ export type { ExtensionObjectConstructorFuncWithSchema } from "../extension_object_constructor.js";
46
+ export * from "../json_basic_encoding_body_functor.js";
47
+ export * from "../json_basic_encoding_decoding_byte_string.js";
48
+ // DataValue
49
+ export {
50
+ type DataValueJSON105 as DataValueJSON,
51
+ type DataValueStatusAndTimestamps,
52
+ opcuaJsonDecodeDataValue105 as opcuaJsonDecodeDataValue,
53
+ opcuaJsonEncodeDataValue105 as opcuaJsonEncodeDataValue,
54
+ opcuaJsonEncodeDataValueMQTT105 as opcuaJsonEncodeDataValueMQTT
55
+ } from "../json_basic_encoding_decoding_data_value.js";
56
+ export * from "../json_basic_encoding_decoding_date_time.js";
57
+ export * from "../json_basic_encoding_decoding_diagnosticinfo.js";
58
+ // ExtensionObject
59
+ export {
60
+ type ExtensionObjectJSON105 as ExtensionObjectJSON,
61
+ makeBody,
62
+ opcuaJsonDecodeExtensionObjectBody,
63
+ opcuaJsonEncodeExtensionObjectBody
64
+ } from "../json_basic_encoding_decoding_extension_object.js";
65
+ export * from "../json_basic_encoding_decoding_int64.js";
66
+ export * from "../json_basic_encoding_decoding_localizedtext.js";
67
+ // NodeId
68
+ export {
69
+ type NodeIdJSON105 as NodeIdJSON,
70
+ opcuaDecodeURIComponent,
71
+ opcuaEncodeURIComponent,
72
+ opcuaJsonDecodeExpandedNodeId,
73
+ opcuaJsonDecodeNodeId,
74
+ opcuaJsonEncodeExpandedNodeId105 as opcuaJsonEncodeExpandedNodeId,
75
+ opcuaJsonEncodeExpandedNodeIdAsString,
76
+ opcuaJsonEncodeNodeId105 as opcuaJsonEncodeNodeId,
77
+ opcuaJsonEncodeNodeIdAsString
78
+ } from "../json_basic_encoding_decoding_nodeid.js";
79
+ export * from "../json_basic_encoding_decoding_qualifiedname.js";
80
+
81
+ // StatusCode
82
+ export {
83
+ opcuaJsonDecodeStatusCode,
84
+ opcuaJsonEncodeStatusCode105 as opcuaJsonEncodeStatusCode,
85
+ type StatusCodeJSON
86
+ } from "../json_basic_encoding_decoding_statuscode.js";
87
+ // Variant
88
+ export {
89
+ opcuaJsonDecodeVariant105 as opcuaJsonDecodeVariant,
90
+ opcuaJsonEncodeVariant105 as opcuaJsonEncodeVariant,
91
+ type VariantJSON105 as VariantJSON,
92
+ type VariantJSONBody,
93
+ type VariantJSONBodyScalar
94
+ } from "../json_basic_encoding_decoding_variant.js";
95
+ export { JsonEncoderMode105 as JsonEncoderMode, JsonEncodingScheme, toJsonEncodingScheme } from "../json_encoding_scheme.js";
96
+
97
+ /**
98
+ * encode an ExtensionObject with the 1.05 rules: { UaTypeId, UaBody }, UaBody omitted in
99
+ * Compact mode when empty; a null ExtensionObject is omitted (undefined) in Compact mode.
100
+ */
101
+ export function opcuaJsonEncodeExtensionObject(
102
+ extensionObject: ExtensionObject | ExtensionObject[] | null,
103
+ mode: JsonEncoderMode105,
104
+ namespaceArray?: string[]
105
+ ): ExtensionObjectJSON105 | ExtensionObjectJSON105[] | null | undefined {
106
+ return opcuaJsonEncodeExtensionObjectAny(extensionObject, toJsonEncodingScheme(mode), namespaceArray) as
107
+ | ExtensionObjectJSON105
108
+ | ExtensionObjectJSON105[]
109
+ | null
110
+ | undefined;
111
+ }
112
+
113
+ export function opcuaJsonDecodeExtensionObject(
114
+ pojo: ExtensionObjectJSON105,
115
+ builder: ExtensionObjectBuilder,
116
+ namespaceArray: string[]
117
+ ): ExtensionObject | ExtensionObject[] | null {
118
+ return opcuaJsonDecodeExtensionObjectAny(pojo, builder, namespaceArray);
119
+ }
package/source/index.ts CHANGED
@@ -22,6 +22,11 @@
22
22
  * ==========================================================================
23
23
  */
24
24
 
25
+ // version-locked realms: `node-opcua-json/104` and `node-opcua-json/105` expose the same
26
+ // API under version-free names (opcuaJsonEncodeDataValue, DataValueJSON, ...) bound to a
27
+ // single edition of Part 6, whereas this root entry point dispatches on JsonEncodingScheme.
28
+ export * as v104 from "./104/index.js";
29
+ export * as v105 from "./105/index.js";
25
30
  export * from "./decode_opcua_json.js";
26
31
  export type { ExtensionObjectConstructorFuncWithSchema } from "./extension_object_constructor.js";
27
32
  export * from "./json_basic_encoding_body_functor.js";
@@ -31,9 +36,16 @@ export {
31
36
  type DataValueJSON104,
32
37
  type DataValueJSON105,
33
38
  opcuaJsonDecodeDataValue,
39
+ opcuaJsonDecodeDataValue104,
40
+ opcuaJsonDecodeDataValue105,
34
41
  opcuaJsonEncodeDataValue,
35
- opcuaJsonEncodeDataValueMQTT
42
+ opcuaJsonEncodeDataValue104,
43
+ opcuaJsonEncodeDataValue105,
44
+ opcuaJsonEncodeDataValueMQTT,
45
+ opcuaJsonEncodeDataValueMQTT104,
46
+ opcuaJsonEncodeDataValueMQTT105
36
47
  } from "./json_basic_encoding_decoding_data_value.js";
48
+ export * from "./json_basic_encoding_decoding_diagnosticinfo.js";
37
49
  export * from "./json_basic_encoding_decoding_extension_object.js";
38
50
  export {
39
51
  opcuaJsonDecodeExtensionObject,
@@ -44,13 +56,14 @@ export * from "./json_basic_encoding_decoding_localizedtext.js";
44
56
  export * from "./json_basic_encoding_decoding_nodeid.js";
45
57
  export * from "./json_basic_encoding_decoding_qualifiedname.js";
46
58
  export * from "./json_basic_encoding_decoding_statuscode.js";
47
- export * from "./json_basic_encoding_decoding_statuscode.js";
48
59
  export {
49
60
  opcuaJsonDecodeVariant,
61
+ opcuaJsonDecodeVariant104,
50
62
  // the explicit 1.05 entry points: the dispatcher above overloads Compact and Verbose together
51
63
  // and so cannot return the 1.05 object shape for Verbose alone
52
64
  opcuaJsonDecodeVariant105,
53
65
  opcuaJsonEncodeVariant,
66
+ opcuaJsonEncodeVariant104,
54
67
  opcuaJsonEncodeVariant105,
55
68
  type VariantJSON,
56
69
  type VariantJSON104,
@@ -29,6 +29,7 @@ import type { ExtensionObjectConstructorFuncWithSchema } from "./extension_objec
29
29
  import { opcuaJsonDecodeByteString, opcuaJsonEncodeByteString } from "./json_basic_encoding_decoding_byte_string.js";
30
30
  import { opcuaJsonDecodeDataValue, opcuaJsonEncodeDataValue } from "./json_basic_encoding_decoding_data_value.js";
31
31
  import { opcuaJsonDecodeDateTime, opcuaJsonEncodeDateTime } from "./json_basic_encoding_decoding_date_time.js";
32
+ import { opcuaJsonDecodeDiagnosticInfo, opcuaJsonEncodeDiagnosticInfo } from "./json_basic_encoding_decoding_diagnosticinfo.js";
32
33
  import { opcuaJsonDecodeExtensionObject, opcuaJsonEncodeExtensionObject } from "./json_basic_encoding_decoding_extension_object.js";
33
34
  import {
34
35
  opcuaJsonDecodeInt64,
@@ -108,6 +109,8 @@ export function bodyEncodeFunctor(dataType: DataType): EncoderFunc<unknown> {
108
109
  case DataType.Guid:
109
110
  return opcuaJsonEncodeGuid as EncoderFunc<unknown>;
110
111
  case DataType.String:
112
+ case DataType.XmlElement:
113
+ // XmlElement values are encoded as a JSON string holding the XML text (Part 6, 5.4.2.9)
111
114
  return opcuaJsonEncodeString as EncoderFunc<unknown>;
112
115
  case DataType.ByteString:
113
116
  return opcuaJsonEncodeByteString as EncoderFunc<unknown>;
@@ -134,8 +137,7 @@ export function bodyEncodeFunctor(dataType: DataType): EncoderFunc<unknown> {
134
137
  case DataType.DataValue:
135
138
  return opcuaJsonEncodeDataValue as EncoderFunc<unknown>;
136
139
  case DataType.DiagnosticInfo:
137
- case DataType.XmlElement:
138
- throw new Error(`Unsupported yet ${DataType[dataType]}`);
140
+ return opcuaJsonEncodeDiagnosticInfo as EncoderFunc<unknown>;
139
141
  default:
140
142
  return () => null;
141
143
  }
@@ -156,6 +158,7 @@ export function bodyDecodeFunctor(dataType: DataType): DecoderFunc<unknown> {
156
158
  case DataType.String:
157
159
  case DataType.UInt16:
158
160
  case DataType.UInt32:
161
+ case DataType.XmlElement:
159
162
  return ((a: boolean | number | string) => a) as DecoderFunc<unknown>;
160
163
  case DataType.ByteString:
161
164
  return opcuaJsonDecodeByteString as DecoderFunc<unknown>;
@@ -182,8 +185,7 @@ export function bodyDecodeFunctor(dataType: DataType): DecoderFunc<unknown> {
182
185
  case DataType.DataValue:
183
186
  return opcuaJsonDecodeDataValue as DecoderFunc<unknown>;
184
187
  case DataType.DiagnosticInfo:
185
- case DataType.XmlElement:
186
- throw new Error(`Unsupported yet ${DataType[dataType]}`);
188
+ return opcuaJsonDecodeDiagnosticInfo as DecoderFunc<unknown>;
187
189
  default:
188
190
  return () => null;
189
191
  }
@@ -148,19 +148,19 @@ export function opcuaJsonEncodeDataValueMQTT(
148
148
  ): VariantJSONBody | VariantJSON105 | VariantJSON104 | DataValueJSON105 | DataValueJSON104 | null {
149
149
  switch (encoding) {
150
150
  case JsonEncodingScheme.DeprecatedNonReversible:
151
- return _opcuaJsonEncodeDataValueMQTT104(dataValue, JsonEncoderMode104.NonReversible, mask, namespaceArray);
151
+ return opcuaJsonEncodeDataValueMQTT104(dataValue, JsonEncoderMode104.NonReversible, mask, namespaceArray);
152
152
  case JsonEncodingScheme.DeprecatedReversible:
153
- return _opcuaJsonEncodeDataValueMQTT104(dataValue, JsonEncoderMode104.Reversible, mask, namespaceArray);
153
+ return opcuaJsonEncodeDataValueMQTT104(dataValue, JsonEncoderMode104.Reversible, mask, namespaceArray);
154
154
  case JsonEncodingScheme.Verbose:
155
- return _opcuaJsonEncodeDataValueMQTT105(dataValue, JsonEncoderMode105.Verbose, mask, namespaceArray);
155
+ return opcuaJsonEncodeDataValueMQTT105(dataValue, JsonEncoderMode105.Verbose, mask, namespaceArray);
156
156
  case JsonEncodingScheme.Compact: {
157
- return _opcuaJsonEncodeDataValueMQTT105(dataValue, JsonEncoderMode105.Compact, mask, namespaceArray);
157
+ return opcuaJsonEncodeDataValueMQTT105(dataValue, JsonEncoderMode105.Compact, mask, namespaceArray);
158
158
  }
159
159
  default:
160
160
  throw new Error("Invalid JsonEncodingScheme");
161
161
  }
162
162
  }
163
- function _opcuaJsonEncodeDataValueMQTT104(
163
+ export function opcuaJsonEncodeDataValueMQTT104(
164
164
  dataValue: DataValue,
165
165
  mode: JsonEncoderMode104,
166
166
  mask: DataSetFieldContentMask,
@@ -248,7 +248,7 @@ function _opcuaJsonEncodeDataValueMQTT104(
248
248
  * @param mask
249
249
  * @returns
250
250
  */
251
- export function _opcuaJsonEncodeDataValueMQTT105(
251
+ export function opcuaJsonEncodeDataValueMQTT105(
252
252
  dataValue: DataValue,
253
253
  mode: JsonEncoderMode105,
254
254
  mask: DataSetFieldContentMask,
@@ -0,0 +1,129 @@
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
+ import { DiagnosticInfo } from "node-opcua-data-model";
25
+ import type { StatusCode } from "node-opcua-status-code";
26
+ import type { ExtensionObjectBuilder } from "./json_basic_encoding_body_functor.js";
27
+ import {
28
+ opcuaJsonDecodeStatusCode,
29
+ opcuaJsonEncodeStatusCode,
30
+ type StatusCodeJSON
31
+ } from "./json_basic_encoding_decoding_statuscode.js";
32
+ import { JsonEncodingScheme } from "./json_encoding_scheme.js";
33
+
34
+ /**
35
+ * JSON form of a DiagnosticInfo
36
+ *
37
+ * reference: https://reference.opcfoundation.org/Core/Part6/v105/docs/5.4.2.13
38
+ *
39
+ * Each field is omitted when it holds its default value (-1 for the string
40
+ * table indexes, null for AdditionalInfo, Good for InnerStatusCode and null for
41
+ * InnerDiagnosticInfo), except in the Verbose encoding where every field is present.
42
+ */
43
+ export interface DiagnosticInfoJSON {
44
+ /** index into the string table of the response header, -1 when not set */
45
+ SymbolicId?: number;
46
+ /** index into the string table of the response header, -1 when not set */
47
+ NamespaceUri?: number;
48
+ /** index into the string table of the response header, -1 when not set */
49
+ Locale?: number;
50
+ /** index into the string table of the response header, -1 when not set */
51
+ LocalizedText?: number;
52
+ AdditionalInfo?: string | null;
53
+ InnerStatusCode?: StatusCodeJSON | number | null;
54
+ InnerDiagnosticInfo?: DiagnosticInfoJSON | null;
55
+ }
56
+
57
+ function isEmptyDiagnosticInfo(d: DiagnosticInfo): boolean {
58
+ return (
59
+ d.symbolicId === -1 &&
60
+ d.namespaceURI === -1 &&
61
+ d.locale === -1 &&
62
+ d.localizedText === -1 &&
63
+ !d.additionalInfo &&
64
+ (!d.innerStatusCode || d.innerStatusCode.value === 0) &&
65
+ (!d.innerDiagnosticInfo || isEmptyDiagnosticInfo(d.innerDiagnosticInfo))
66
+ );
67
+ }
68
+
69
+ export function opcuaJsonEncodeDiagnosticInfo(
70
+ diagnosticInfo: DiagnosticInfo | null | undefined,
71
+ scheme = JsonEncodingScheme.Compact,
72
+ namespaceArray?: string[]
73
+ ): DiagnosticInfoJSON | null | undefined {
74
+ const verbose = scheme === JsonEncodingScheme.Verbose;
75
+
76
+ if (!diagnosticInfo || (!verbose && isEmptyDiagnosticInfo(diagnosticInfo))) {
77
+ // an empty DiagnosticInfo is treated like a null value:
78
+ // omitted in the Compact encoding, encoded as null in the deprecated encodings
79
+ return scheme === JsonEncodingScheme.Compact ? undefined : null;
80
+ }
81
+ const d = diagnosticInfo;
82
+ const pojo: DiagnosticInfoJSON = {};
83
+
84
+ const setIndex = (key: "SymbolicId" | "NamespaceUri" | "Locale" | "LocalizedText", value: number) => {
85
+ if (verbose || value !== -1) {
86
+ pojo[key] = value;
87
+ }
88
+ };
89
+ setIndex("SymbolicId", d.symbolicId);
90
+ setIndex("NamespaceUri", d.namespaceURI);
91
+ setIndex("Locale", d.locale);
92
+ setIndex("LocalizedText", d.localizedText);
93
+
94
+ if (verbose || d.additionalInfo) {
95
+ pojo.AdditionalInfo = d.additionalInfo ?? null;
96
+ }
97
+ const innerStatusCode: StatusCode | null = d.innerStatusCode ?? null;
98
+ if (verbose || (innerStatusCode && innerStatusCode.value !== 0)) {
99
+ const encoded = innerStatusCode ? opcuaJsonEncodeStatusCode(innerStatusCode, scheme) : null;
100
+ pojo.InnerStatusCode = encoded === undefined ? null : encoded;
101
+ }
102
+ if (verbose || d.innerDiagnosticInfo) {
103
+ const inner = opcuaJsonEncodeDiagnosticInfo(d.innerDiagnosticInfo ?? null, scheme, namespaceArray);
104
+ if (verbose || inner) {
105
+ pojo.InnerDiagnosticInfo = inner === undefined ? null : inner;
106
+ }
107
+ }
108
+ return pojo;
109
+ }
110
+
111
+ export function opcuaJsonDecodeDiagnosticInfo(
112
+ pojo: DiagnosticInfoJSON | null | undefined,
113
+ _builder?: ExtensionObjectBuilder,
114
+ _namespaceArray?: string[]
115
+ ): DiagnosticInfo {
116
+ if (!pojo || typeof pojo !== "object") {
117
+ return new DiagnosticInfo();
118
+ }
119
+ const index = (value: number | undefined | null): number => (typeof value === "number" ? value : -1);
120
+ return new DiagnosticInfo({
121
+ symbolicId: index(pojo.SymbolicId),
122
+ namespaceURI: index(pojo.NamespaceUri),
123
+ locale: index(pojo.Locale),
124
+ localizedText: index(pojo.LocalizedText),
125
+ additionalInfo: pojo.AdditionalInfo ?? null,
126
+ innerStatusCode: opcuaJsonDecodeStatusCode(pojo.InnerStatusCode),
127
+ innerDiagnosticInfo: pojo.InnerDiagnosticInfo ? opcuaJsonDecodeDiagnosticInfo(pojo.InnerDiagnosticInfo) : undefined
128
+ });
129
+ }
@@ -310,12 +310,12 @@ function isDefault(field: FieldType, value: unknown): boolean {
310
310
  // DiagnosticInfo is default if it has no body
311
311
  const valueAsDiag = value as DiagnosticInfoOptions;
312
312
  return (
313
- valueAsDiag.symbolicId === 0 &&
314
- valueAsDiag.namespaceURI === 0 &&
315
- valueAsDiag.locale === 0 &&
316
- valueAsDiag.localizedText === 0 &&
313
+ (valueAsDiag.symbolicId ?? -1) === -1 &&
314
+ (valueAsDiag.namespaceURI ?? -1) === -1 &&
315
+ (valueAsDiag.locale ?? -1) === -1 &&
316
+ (valueAsDiag.localizedText ?? -1) === -1 &&
317
317
  !valueAsDiag.additionalInfo &&
318
- !valueAsDiag.innerStatusCode &&
318
+ (!valueAsDiag.innerStatusCode || valueAsDiag.innerStatusCode.value === 0) &&
319
319
  !valueAsDiag.innerDiagnosticInfo
320
320
  );
321
321
  }
@@ -44,6 +44,8 @@ export interface NodeIdJSON104 {
44
44
  IdType?: 0 | 1 | 2 | 3;
45
45
  Id: number | string;
46
46
  Namespace?: number | string;
47
+ /** ExpandedNodeId only: the ServerIndex, omitted when 0 (Part 6 1.04 clause 5.4.2.11) */
48
+ ServerUri?: number;
47
49
  }
48
50
  export type NodeIdJSON105 = string;
49
51
  export type NodeIdJSON = NodeIdJSON104 | NodeIdJSON105;
@@ -101,7 +103,7 @@ const nodeIdValueToString = (value: NodeId): string => {
101
103
  * A NamespaceIndex of 1 is always encoded as a JSON number.
102
104
  */
103
105
 
104
- function opcuaJsonEncodeNodeId104(
106
+ export function opcuaJsonEncodeNodeId104(
105
107
  value: NodeId,
106
108
  scheme = JsonEncodingScheme.DeprecatedReversible,
107
109
  namespaceArray?: string[]
@@ -168,7 +170,11 @@ export function opcuaJsonEncodeExpandedNodeIdAsString(value: ExpandedNodeId, nam
168
170
  return result;
169
171
  }
170
172
 
171
- function opcuaJsonEncodeNodeId105(value: NodeId, scheme: JsonEncodingScheme, namespaceArray?: string[]): NodeIdJSON105 | undefined {
173
+ export function opcuaJsonEncodeNodeId105(
174
+ value: NodeId,
175
+ scheme: JsonEncodingScheme,
176
+ namespaceArray?: string[]
177
+ ): NodeIdJSON105 | undefined {
172
178
  if (value.namespace === 0 && value.identifierType === NodeIdType.NUMERIC && value.value === 0) {
173
179
  if (scheme === JsonEncodingScheme.Compact) {
174
180
  return undefined;
@@ -178,7 +184,7 @@ function opcuaJsonEncodeNodeId105(value: NodeId, scheme: JsonEncodingScheme, nam
178
184
  return opcuaJsonEncodeNodeIdAsString(value, namespaceArray || []);
179
185
  }
180
186
 
181
- function opcuaJsonEncodeExpandedNodeId105(
187
+ export function opcuaJsonEncodeExpandedNodeId105(
182
188
  value: ExpandedNodeId,
183
189
  scheme: JsonEncodingScheme,
184
190
  namespaceArray?: string[]
@@ -226,12 +232,24 @@ export function opcuaJsonEncodeExpandedNodeId(
226
232
  }
227
233
  }
228
234
 
235
+ /**
236
+ * Part 6 1.04 clause 5.4.2.11: an ExpandedNodeId is the NodeId object plus a ServerUri field
237
+ * carrying the ServerIndex, omitted when 0. A NamespaceUri set on the ExpandedNodeId wins over
238
+ * the index in the non-reversible form, where the namespace is written as a URI anyway.
239
+ */
229
240
  export function opcuaJsonEncodeExpandedNodeId104(
230
- _value: ExpandedNodeId,
241
+ value: ExpandedNodeId,
231
242
  scheme: JsonEncodingScheme,
232
- _namespaceArray?: string[]
233
- ): NodeIdJSON | undefined {
234
- throw new Error(`Invalid JsonEncodingScheme: ${scheme}`);
243
+ namespaceArray?: string[]
244
+ ): NodeIdJSON104 {
245
+ const pojo = opcuaJsonEncodeNodeId104(value, scheme, namespaceArray);
246
+ if (value.namespaceUri && scheme === JsonEncodingScheme.DeprecatedNonReversible) {
247
+ pojo.Namespace = value.namespaceUri;
248
+ }
249
+ if (value.serverIndex !== 0) {
250
+ pojo.ServerUri = value.serverIndex;
251
+ }
252
+ return pojo;
235
253
  }
236
254
 
237
255
  export function opcuaEncodeURIComponent(value: string): string {
@@ -350,7 +368,8 @@ export function opcuaJsonDecodeExpandedNodeId(
350
368
  namespaceArray?: string[]
351
369
  ): ExpandedNodeId {
352
370
  const nodeId = opcuaJsonDecodeNodeId(pojoOrString, builder, namespaceArray);
353
- const result = ExpandedNodeId.fromNodeId(nodeId);
354
- // we don't suport serverIndex in JSON encoding
355
- return result;
371
+ // the 1.05 string form carries svr=/svu=, which coerceNodeId does not parse; the 1.04 object
372
+ // form carries ServerUri as the ServerIndex
373
+ const serverIndex = typeof pojoOrString === "object" && pojoOrString ? (pojoOrString.ServerUri ?? 0) : 0;
374
+ return ExpandedNodeId.fromNodeId(nodeId, undefined, serverIndex);
356
375
  }
@@ -181,6 +181,7 @@ export function opcuaJsonEncodeVariant105(variant: Variant, mode: JsonEncoderMod
181
181
  case DataType.LocalizedText:
182
182
  case DataType.ExtensionObject:
183
183
  case DataType.StatusCode:
184
+ case DataType.DiagnosticInfo:
184
185
  // these are all scalar types, so we can remove the UaType
185
186
  // delete pojo.UaType;
186
187
  break;