node-opcua-json 0.19.1 → 2.182.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 (78) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +141 -4
  3. package/dist/decode_opcua_json.d.ts +31 -0
  4. package/dist/decode_opcua_json.js +61 -0
  5. package/dist/decode_opcua_json.js.map +1 -0
  6. package/dist/extension_object_constructor.d.ts +46 -0
  7. package/dist/extension_object_constructor.js +26 -0
  8. package/dist/extension_object_constructor.js.map +1 -0
  9. package/dist/index.d.ts +38 -0
  10. package/dist/index.js +66 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/json_basic_encoding_body_functor.d.ts +34 -0
  13. package/dist/json_basic_encoding_body_functor.js +170 -0
  14. package/dist/json_basic_encoding_body_functor.js.map +1 -0
  15. package/dist/json_basic_encoding_decoding_byte_string.d.ts +27 -0
  16. package/dist/json_basic_encoding_decoding_byte_string.js +27 -0
  17. package/dist/json_basic_encoding_decoding_byte_string.js.map +1 -0
  18. package/dist/json_basic_encoding_decoding_data_value.d.ts +80 -0
  19. package/dist/json_basic_encoding_decoding_data_value.js +289 -0
  20. package/dist/json_basic_encoding_decoding_data_value.js.map +1 -0
  21. package/dist/json_basic_encoding_decoding_date_time.d.ts +26 -0
  22. package/dist/json_basic_encoding_decoding_date_time.js +43 -0
  23. package/dist/json_basic_encoding_decoding_date_time.js.map +1 -0
  24. package/dist/json_basic_encoding_decoding_extension_object.d.ts +65 -0
  25. package/dist/json_basic_encoding_decoding_extension_object.js +461 -0
  26. package/dist/json_basic_encoding_decoding_extension_object.js.map +1 -0
  27. package/dist/json_basic_encoding_decoding_int64.d.ts +30 -0
  28. package/dist/json_basic_encoding_decoding_int64.js +56 -0
  29. package/dist/json_basic_encoding_decoding_int64.js.map +1 -0
  30. package/dist/json_basic_encoding_decoding_localizedtext.d.ts +33 -0
  31. package/dist/json_basic_encoding_decoding_localizedtext.js +62 -0
  32. package/dist/json_basic_encoding_decoding_localizedtext.js.map +1 -0
  33. package/dist/json_basic_encoding_decoding_nodeid.d.ts +42 -0
  34. package/dist/json_basic_encoding_decoding_nodeid.js +298 -0
  35. package/dist/json_basic_encoding_decoding_nodeid.js.map +1 -0
  36. package/dist/json_basic_encoding_decoding_qualifiedname.d.ts +35 -0
  37. package/dist/json_basic_encoding_decoding_qualifiedname.js +126 -0
  38. package/dist/json_basic_encoding_decoding_qualifiedname.js.map +1 -0
  39. package/dist/json_basic_encoding_decoding_statuscode.d.ts +39 -0
  40. package/dist/json_basic_encoding_decoding_statuscode.js +139 -0
  41. package/dist/json_basic_encoding_decoding_statuscode.js.map +1 -0
  42. package/dist/json_basic_encoding_decoding_variant.d.ts +101 -0
  43. package/dist/json_basic_encoding_decoding_variant.js +234 -0
  44. package/dist/json_basic_encoding_decoding_variant.js.map +1 -0
  45. package/dist/json_encoding_scheme.d.ts +50 -0
  46. package/dist/json_encoding_scheme.js +117 -0
  47. package/dist/json_encoding_scheme.js.map +1 -0
  48. package/package.json +57 -37
  49. package/source/decode_opcua_json.ts +67 -0
  50. package/source/extension_object_constructor.ts +48 -0
  51. package/source/index.ts +59 -0
  52. package/source/json_basic_encoding_body_functor.ts +190 -0
  53. package/source/json_basic_encoding_decoding_byte_string.ts +52 -0
  54. package/source/json_basic_encoding_decoding_data_value.ts +392 -0
  55. package/source/json_basic_encoding_decoding_date_time.ts +42 -0
  56. package/source/json_basic_encoding_decoding_extension_object.ts +531 -0
  57. package/source/json_basic_encoding_decoding_int64.ts +74 -0
  58. package/source/json_basic_encoding_decoding_localizedtext.ts +69 -0
  59. package/source/json_basic_encoding_decoding_nodeid.ts +356 -0
  60. package/source/json_basic_encoding_decoding_qualifiedname.ts +144 -0
  61. package/source/json_basic_encoding_decoding_statuscode.ts +151 -0
  62. package/source/json_basic_encoding_decoding_variant.ts +339 -0
  63. package/source/json_encoding_scheme.ts +114 -0
  64. package/LICENSE.md +0 -40
  65. package/dist2/decode_opcua_json.d.ts +0 -12
  66. package/dist2/index.d.ts +0 -17
  67. package/dist2/index.js +0 -1
  68. package/dist2/json_basic_encoding_body_functor.d.ts +0 -15
  69. package/dist2/json_basic_encoding_decoding_byte_string.d.ts +0 -8
  70. package/dist2/json_basic_encoding_decoding_data_value.d.ts +0 -26
  71. package/dist2/json_basic_encoding_decoding_date_time.d.ts +0 -6
  72. package/dist2/json_basic_encoding_decoding_extension_object.d.ts +0 -32
  73. package/dist2/json_basic_encoding_decoding_int64.d.ts +0 -9
  74. package/dist2/json_basic_encoding_decoding_localizedtext.d.ts +0 -14
  75. package/dist2/json_basic_encoding_decoding_nodeid.d.ts +0 -33
  76. package/dist2/json_basic_encoding_decoding_qualifiedname.d.ts +0 -14
  77. package/dist2/json_basic_encoding_decoding_statuscode.d.ts +0 -16
  78. package/dist2/json_basic_encoding_decoding_variant.d.ts +0 -0
@@ -0,0 +1,356 @@
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 { coerceNodeId, ExpandedNodeId, NodeId, NodeIdType } from "node-opcua-nodeid"; // -nodeid";
25
+ import type { ExtensionObjectBuilder } from "./json_basic_encoding_body_functor.js";
26
+ import { JsonEncodingScheme } from "./json_encoding_scheme.js";
27
+
28
+ // from https://reference.opcfoundation.org/Core/Part6/v105/docs/5.4
29
+
30
+ // NodeId values shall be encoded as a JSON string using the format defined in
31
+ // https://reference.opcfoundation.org/Core/Part6/v105/docs/5.1.12#_Ref122651839
32
+ // .
33
+ // NodeIds in NamespaceIndex 0 use the <identifier> form.
34
+ // All other NodeIds use the <namespace-uri> form.
35
+ //
36
+ // The first abnormal state occurs when the encoder cannot map a NamespaceIndex to a NamespaceUri.
37
+ // In this case, the encoder shall encode the NamespaceIndex using the <namespace-index > form.
38
+ // The decoder shall pass this NamespaceIndex to the application.
39
+ //
40
+ // The second abnormal state occurs when the decoder cannot convert a NamespaceUri to a NamespaceIndex.
41
+ // If this occurs the decoder shall set the NamespaceIndex to 0, the IdType to String and the
42
+ // Identifier to the JSON string.
43
+ export interface NodeIdJSON104 {
44
+ IdType?: 0 | 1 | 2 | 3;
45
+ Id: number | string;
46
+ Namespace?: number | string;
47
+ }
48
+ export type NodeIdJSON105 = string;
49
+ export type NodeIdJSON = NodeIdJSON104 | NodeIdJSON105;
50
+
51
+ const nodeIdValueToJson = (value: NodeId): string | number => {
52
+ switch (value.identifierType) {
53
+ case NodeIdType.NUMERIC:
54
+ return value.value as number;
55
+ case NodeIdType.STRING:
56
+ return value.value as string;
57
+ case NodeIdType.GUID:
58
+ return value.value as string;
59
+ case NodeIdType.BYTESTRING:
60
+ return (value.value as Buffer).toString("base64");
61
+ default:
62
+ throw new Error(`Unknown NodeId identifier type: ${value.identifierType}`);
63
+ }
64
+ };
65
+
66
+ const nodeIdValueToString = (value: NodeId): string => {
67
+ switch (value.identifierType) {
68
+ case NodeIdType.NUMERIC:
69
+ return `i=${value.value as number}`;
70
+
71
+ case NodeIdType.STRING:
72
+ return `s=${value.value as string}`;
73
+
74
+ case NodeIdType.GUID:
75
+ return `g=${value.value as string}`;
76
+ case NodeIdType.BYTESTRING:
77
+ return `b=${(value.value as Buffer).toString("base64")}`;
78
+ default:
79
+ throw new Error(`Unknown NodeId identifier type: ${value.identifierType}`);
80
+ }
81
+ };
82
+ /**
83
+ *
84
+ * IdType The IdentifierType encoded as a JSON number.
85
+ * Allowed values are:
86
+ * 0 - UInt32 Identifier encoded as a JSON number.
87
+ * 1 - A String Identifier encoded as a JSON string.
88
+ * 2 - A Guid Identifier.
89
+ * 3 - A ByteString Identifier
90
+ *
91
+ * This field is omitted for UInt32 identifiers.
92
+ *
93
+ * Id The Identifier.
94
+ * The value of the id field specifies the encoding of this field.
95
+ *
96
+ * Namespace The NamespaceIndex for the NodeId.
97
+ * The field is encoded as a JSON number for the reversible encoding.
98
+ * The field is omitted if the NamespaceIndex equals 0.
99
+ * For the non-reversible encoding, the field is the NamespaceUri associated with the NamespaceIndex,
100
+ * encoded as a JSON string.
101
+ * A NamespaceIndex of 1 is always encoded as a JSON number.
102
+ */
103
+
104
+ function opcuaJsonEncodeNodeId104(
105
+ value: NodeId,
106
+ scheme = JsonEncodingScheme.DeprecatedReversible,
107
+ namespaceArray?: string[]
108
+ ): NodeIdJSON104 {
109
+ const nodeIdIdentifierTypeToJSON = () => {
110
+ switch (value.identifierType) {
111
+ case NodeIdType.NUMERIC:
112
+ return 0;
113
+ case NodeIdType.STRING:
114
+ return 1;
115
+ case NodeIdType.GUID:
116
+ return 2;
117
+ default:
118
+ return 3;
119
+ }
120
+ };
121
+ const IdType = nodeIdIdentifierTypeToJSON();
122
+
123
+ const Id = nodeIdValueToJson(value);
124
+
125
+ const pojo: NodeIdJSON = { Id };
126
+ if (IdType !== 0) {
127
+ pojo.IdType = IdType as 1 | 2 | 3;
128
+ }
129
+ if (value.namespace === 0) {
130
+ return pojo;
131
+ }
132
+ const reversible = !(scheme === JsonEncodingScheme.DeprecatedNonReversible);
133
+
134
+ if (value.namespace === 1 || reversible || !namespaceArray) {
135
+ pojo.Namespace = value.namespace;
136
+ return pojo;
137
+ }
138
+ pojo.Namespace = namespaceArray[value.namespace];
139
+ return pojo;
140
+ }
141
+
142
+ export function opcuaJsonEncodeNodeIdAsString(value: NodeId, namespaceArray: string[]): string {
143
+ const namespace = namespaceArray ? namespaceArray[value.namespace] : undefined;
144
+ if (namespace) {
145
+ if (namespace === "http://opcfoundation.org/UA/") {
146
+ return `${nodeIdValueToString(value)}`;
147
+ }
148
+ const encodedNamespace = opcuaEncodeURIComponent(namespace);
149
+ return `nsu=${encodedNamespace};${nodeIdValueToString(value)}`;
150
+ } else {
151
+ if (value.namespace === 0) {
152
+ return nodeIdValueToString(value);
153
+ }
154
+ // If the namespace is not found, we assume it is a new namespace
155
+ return `ns=${value.namespace};${nodeIdValueToString(value)}`;
156
+ }
157
+ }
158
+ export function opcuaJsonEncodeExpandedNodeIdAsString(value: ExpandedNodeId, namespaceArray: string[]): string {
159
+ // const namespace = namespaceArray ? namespaceArray[value.namespace] : undefined;
160
+ let result = opcuaJsonEncodeNodeIdAsString(value, namespaceArray);
161
+
162
+ if (value.namespaceUri) {
163
+ const encodedNamespaceUri = opcuaEncodeURIComponent(value.namespaceUri);
164
+ result = `svu=${encodedNamespaceUri};${result}`;
165
+ } else if (value.serverIndex !== 0) {
166
+ result = `svr=${value.serverIndex};${result}`;
167
+ }
168
+ return result;
169
+ }
170
+
171
+ function opcuaJsonEncodeNodeId105(value: NodeId, scheme: JsonEncodingScheme, namespaceArray?: string[]): NodeIdJSON105 | undefined {
172
+ if (value.namespace === 0 && value.identifierType === NodeIdType.NUMERIC && value.value === 0) {
173
+ if (scheme === JsonEncodingScheme.Compact) {
174
+ return undefined;
175
+ }
176
+ }
177
+
178
+ return opcuaJsonEncodeNodeIdAsString(value, namespaceArray || []);
179
+ }
180
+
181
+ function opcuaJsonEncodeExpandedNodeId105(
182
+ value: ExpandedNodeId,
183
+ scheme: JsonEncodingScheme,
184
+ namespaceArray?: string[]
185
+ ): NodeIdJSON105 | undefined {
186
+ if (value.namespace === 0 && value.identifierType === NodeIdType.NUMERIC && value.value === 0 && value.serverIndex === 0) {
187
+ if (scheme === JsonEncodingScheme.Compact) {
188
+ return undefined;
189
+ }
190
+ }
191
+ return opcuaJsonEncodeExpandedNodeIdAsString(value, namespaceArray || []);
192
+ }
193
+
194
+ export function opcuaJsonEncodeNodeId(
195
+ value: NodeId,
196
+ scheme: JsonEncodingScheme,
197
+ namespaceArray?: string[]
198
+ ): NodeIdJSON | undefined {
199
+ switch (scheme) {
200
+ case JsonEncodingScheme.DeprecatedReversible:
201
+ return opcuaJsonEncodeNodeId104(value, scheme, namespaceArray);
202
+ case JsonEncodingScheme.DeprecatedNonReversible:
203
+ return opcuaJsonEncodeNodeId104(value, scheme, namespaceArray);
204
+ case JsonEncodingScheme.Compact:
205
+ return opcuaJsonEncodeNodeId105(value, scheme, namespaceArray);
206
+ case JsonEncodingScheme.Verbose:
207
+ return opcuaJsonEncodeNodeId105(value, scheme, namespaceArray);
208
+ default:
209
+ throw new Error(`Invalid JsonEncodingScheme: ${scheme}`);
210
+ }
211
+ }
212
+ export function opcuaJsonEncodeExpandedNodeId(
213
+ value: ExpandedNodeId,
214
+ scheme: JsonEncodingScheme,
215
+ namespaceArray?: string[]
216
+ ): NodeIdJSON | undefined {
217
+ switch (scheme) {
218
+ case JsonEncodingScheme.DeprecatedReversible:
219
+ case JsonEncodingScheme.DeprecatedNonReversible:
220
+ return opcuaJsonEncodeExpandedNodeId104(value, scheme, namespaceArray);
221
+ case JsonEncodingScheme.Compact:
222
+ case JsonEncodingScheme.Verbose:
223
+ return opcuaJsonEncodeExpandedNodeId105(value, scheme, namespaceArray);
224
+ default:
225
+ throw new Error(`Invalid JsonEncodingScheme: ${scheme}`);
226
+ }
227
+ }
228
+
229
+ export function opcuaJsonEncodeExpandedNodeId104(
230
+ _value: ExpandedNodeId,
231
+ scheme: JsonEncodingScheme,
232
+ _namespaceArray?: string[]
233
+ ): NodeIdJSON | undefined {
234
+ throw new Error(`Invalid JsonEncodingScheme: ${scheme}`);
235
+ }
236
+
237
+ export function opcuaEncodeURIComponent(value: string): string {
238
+ return value.replace(/;/g, (match) => {
239
+ // Encode semicolon as %3B
240
+ return encodeURIComponent(match);
241
+ });
242
+ }
243
+ export function opcuaDecodeURIComponent(value: string): string {
244
+ return value.replace(/%3B/g, (match) => {
245
+ // Decode %3B back to semicolon
246
+ return decodeURIComponent(match);
247
+ });
248
+ }
249
+
250
+ function coerceNodeIdEx(nodeIdAsString: string, namespaceArray?: string[]): NodeId {
251
+ //nsu=tag:acme.com,2023:schemas:data#off%3B;b=M/RbKBsRVkePCePcx24oRA==
252
+ const match = nodeIdAsString.match(/^(nsu=([^;]+);)?(g=|s=|i=|b=)(.*)$/);
253
+ if (match) {
254
+ const namespaceUri = match[2];
255
+ const identifierType = match[3];
256
+ const identifierValue = match[4];
257
+
258
+ let namespaceIndex: number;
259
+ if (namespaceUri) {
260
+ if (!namespaceArray) {
261
+ namespaceIndex = 0; // NAMESPACE_NOT_FOUND
262
+ } else {
263
+ // A URI which has the RFC 3986 Percent - Encoding applied to it.
264
+ // Any ‘;’ in the URI shall be percent encoded.
265
+ const decodedNamespaceUri = opcuaDecodeURIComponent(namespaceUri);
266
+ namespaceIndex = namespaceArray.indexOf(decodedNamespaceUri);
267
+ if (namespaceIndex < 0) {
268
+ namespaceIndex = 0; // NAMESPACE_NOT_FOUND
269
+ }
270
+ }
271
+ } else {
272
+ namespaceIndex = 0;
273
+ }
274
+
275
+ switch (identifierType) {
276
+ case "i=":
277
+ return new NodeId(NodeIdType.NUMERIC, parseInt(identifierValue, 10), namespaceIndex);
278
+ case "s=":
279
+ return new NodeId(NodeIdType.STRING, identifierValue, namespaceIndex);
280
+ case "g=":
281
+ return new NodeId(NodeIdType.GUID, identifierValue, namespaceIndex);
282
+ case "b=":
283
+ return new NodeId(NodeIdType.BYTESTRING, Buffer.from(identifierValue, "base64"), namespaceIndex);
284
+ default:
285
+ throw new Error(`Invalid NodeId format: ${nodeIdAsString}`);
286
+ }
287
+ }
288
+ // Fallback to coerceNodeId
289
+ return coerceNodeId(nodeIdAsString);
290
+ }
291
+ export function opcuaJsonDecodeNodeId(
292
+ pojoOrString: NodeIdJSON | undefined,
293
+ _builder?: ExtensionObjectBuilder | undefined,
294
+ namespaceArray?: string[]
295
+ ): NodeId {
296
+ if (pojoOrString === null || pojoOrString === undefined) {
297
+ throw new Error("opcuaJsonDecodeNodeId: pojoOrString cannot be null or undefined");
298
+ }
299
+ if (typeof pojoOrString === "string") {
300
+ return coerceNodeIdEx(pojoOrString, namespaceArray);
301
+ }
302
+ const pojo = pojoOrString as NodeIdJSON104;
303
+ const identifierType: NodeIdType = (() => {
304
+ if (!pojo.IdType) {
305
+ return NodeIdType.NUMERIC;
306
+ }
307
+ switch (pojo.IdType) {
308
+ case 2:
309
+ return NodeIdType.GUID;
310
+ case 3:
311
+ return NodeIdType.BYTESTRING;
312
+ default:
313
+ return NodeIdType.STRING;
314
+ }
315
+ })();
316
+
317
+ const namespace: number = (() => {
318
+ if (!pojo.Namespace) return 0;
319
+ if (typeof pojo.Namespace === "number") {
320
+ return pojo.Namespace;
321
+ }
322
+ const NAMESPACE_NOT_FOUND = 0xffff;
323
+ if (!namespaceArray) {
324
+ return NAMESPACE_NOT_FOUND;
325
+ }
326
+ const index = namespaceArray.indexOf(pojo.Namespace);
327
+ if (index < 0) {
328
+ return NAMESPACE_NOT_FOUND;
329
+ }
330
+ return index;
331
+ })();
332
+
333
+ const value: string | number | Buffer = (() => {
334
+ switch (identifierType) {
335
+ case NodeIdType.NUMERIC:
336
+ return typeof pojo.Id === "string" ? parseInt(pojo.Id, 10) : (pojo.Id as number | 0);
337
+ case NodeIdType.BYTESTRING:
338
+ return Buffer.from(pojo.Id as string, "base64");
339
+ default:
340
+ return pojo.Id;
341
+ }
342
+ })();
343
+
344
+ return new NodeId(identifierType, value, namespace);
345
+ }
346
+
347
+ export function opcuaJsonDecodeExpandedNodeId(
348
+ pojoOrString: NodeIdJSON | undefined,
349
+ builder?: ExtensionObjectBuilder | undefined,
350
+ namespaceArray?: string[]
351
+ ): ExpandedNodeId {
352
+ const nodeId = opcuaJsonDecodeNodeId(pojoOrString, builder, namespaceArray);
353
+ const result = ExpandedNodeId.fromNodeId(nodeId);
354
+ // we don't suport serverIndex in JSON encoding
355
+ return result;
356
+ }
@@ -0,0 +1,144 @@
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 { QualifiedName } from "node-opcua-data-model";
25
+ import type { ExtensionObjectBuilder } from "./json_basic_encoding_body_functor.js";
26
+ import { opcuaDecodeURIComponent, opcuaEncodeURIComponent } from "./json_basic_encoding_decoding_nodeid.js";
27
+ import { JsonEncodingScheme } from "./json_encoding_scheme.js";
28
+ export interface QualifiedNameJSON {
29
+ /** The Name component of the QualifiedName. */
30
+ Name: string;
31
+ /** The NamespaceIndex component of the QualifiedName encoded as a JSON number. */
32
+ Uri?: string | number;
33
+ }
34
+ export function opcuaJsonEncodeQualifiedName(
35
+ qualifiedName: QualifiedName,
36
+ scheme = JsonEncodingScheme.Compact,
37
+ namespaceArray?: string[]
38
+ ): QualifiedNameJSON | undefined {
39
+ if (!qualifiedName.name || !qualifiedName.namespaceIndex) {
40
+ if (scheme === JsonEncodingScheme.Compact) {
41
+ // Compact encoding does not encode empty QualifiedName
42
+ return undefined;
43
+ }
44
+ }
45
+ const pojo: QualifiedNameJSON = {
46
+ Name: qualifiedName.name || ""
47
+ };
48
+ /**
49
+ * The NamespaceIndex component of the QualifiedName encoded as a JSON number.
50
+ * The Uri field is omitted if the NamespaceIndex equals 0.
51
+ * For the non-reversible form, the NamespaceUri associated with the NamespaceIndex
52
+ * portion of the QualifiedName is encoded as JSON string unless
53
+ * the NamespaceIndex is 1 or if NamespaceUri is unknown.
54
+ * In these cases, the NamespaceIndex is encoded as a JSON number.
55
+ */
56
+ if (qualifiedName.namespaceIndex === 0) {
57
+ return pojo;
58
+ }
59
+ const reversible = !(scheme === JsonEncodingScheme.DeprecatedNonReversible);
60
+ if (reversible || !namespaceArray || qualifiedName.namespaceIndex === 1) {
61
+ pojo.Uri = qualifiedName.namespaceIndex;
62
+ return pojo;
63
+ }
64
+ pojo.Uri = namespaceArray[qualifiedName.namespaceIndex];
65
+
66
+ return pojo;
67
+ }
68
+ export function opcuaJsonEncodeQualifiedNameAsString(
69
+ qualifiedName: QualifiedName,
70
+ scheme = JsonEncodingScheme.Compact,
71
+ namespaceArray?: string[]
72
+ ): string {
73
+ const pojo = opcuaJsonEncodeQualifiedName(qualifiedName, scheme, namespaceArray);
74
+ if (!pojo) {
75
+ return "";
76
+ }
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;
89
+ }
90
+
91
+ export function opcuaJsonDecodeQualifiedName(
92
+ pojo: null | undefined | string | QualifiedNameJSON,
93
+ builder?: ExtensionObjectBuilder,
94
+ namespaceArray?: string[]
95
+ ): QualifiedName {
96
+ builder; // unused parameter,
97
+
98
+ if (!pojo) {
99
+ return new QualifiedName();
100
+ }
101
+ if (typeof pojo === "string") {
102
+ // example:
103
+ // - Hello
104
+ // - Hello;World
105
+ // - 3:Hello:World
106
+ // - nsu=http://widgets.com/schemas/hello;Hello;World
107
+ // - nsu=tag:acme.com,2023:schemas:data#off%3B;Boiler2
108
+
109
+ // check if starts with number + ":"
110
+ if (/^\d+:/.test(pojo)) {
111
+ const parts = pojo.split(":");
112
+ const namespaceIndex = parseInt(parts[0], 10);
113
+ const name = parts.slice(1).join(":");
114
+ return new QualifiedName({
115
+ name,
116
+ namespaceIndex
117
+ });
118
+ }
119
+ // check if starts with "nsu=" up tot he next ";"
120
+ if (pojo.startsWith("nsu=")) {
121
+ const endIndex = pojo.indexOf(";");
122
+ if (endIndex === -1) {
123
+ throw new Error(`Invalid QualifiedName format: ${pojo}`);
124
+ }
125
+ const namespaceUri = opcuaDecodeURIComponent(pojo.substring(4, endIndex));
126
+ const name = pojo.substring(endIndex + 1);
127
+ const namespaceIndex = namespaceArray ? namespaceArray.indexOf(namespaceUri) : 0;
128
+ return new QualifiedName({
129
+ name,
130
+ namespaceIndex
131
+ });
132
+ }
133
+ // otherwise, treat as a simple name
134
+ return new QualifiedName({
135
+ name: pojo,
136
+ namespaceIndex: 0
137
+ });
138
+ }
139
+ const namespaceIndex: number = pojo.Uri && typeof pojo.Uri === "number" ? pojo.Uri : 0;
140
+ return new QualifiedName({
141
+ name: pojo.Name,
142
+ namespaceIndex
143
+ });
144
+ }
@@ -0,0 +1,151 @@
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 { coerceStatusCode, type StatusCode, StatusCodes } from "node-opcua-status-code";
25
+ import { JsonEncoderMode104, JsonEncoderMode105, JsonEncodingScheme } from "./json_encoding_scheme.js";
26
+
27
+ export interface StatusCodeJSON {
28
+ Code?: number | keyof StatusCodes;
29
+ Symbol?: string;
30
+ }
31
+
32
+ export function opcuaJsonEncodeStatusCode104(
33
+ statusCode: StatusCode,
34
+ mode: JsonEncoderMode104
35
+ ): StatusCodeJSON | number | undefined {
36
+ /*
37
+ * StatusCode values shall be encoded as a JSON number for the reversible encoding.
38
+ */
39
+ if (mode === JsonEncoderMode104.Reversible) {
40
+ return statusCode.value;
41
+ }
42
+ /*
43
+ * reference: https://reference.opcfoundation.org/Core/Part6/v104/docs/5.4.2.12
44
+ *
45
+ * For the non-reversible form, StatusCode values shall be encoded as a JSON object with the fields defined in Table
46
+ *
47
+ * Symbol The string literal associated with the numeric code encoded as JSON string.
48
+ *
49
+ * e.g. 0x80AB0000 has the associated literal “BadInvalidArgument”.
50
+ * Any InfoBits in the StatusCode are ignored when looking up the symbol.
51
+ * If the string literal is not known to the encoder the field is omitted.
52
+ * The field is omitted in the CompactEncoding.
53
+ * The field is omitted if the numeric code is 0 (Good).
54
+ */
55
+
56
+ /**
57
+ * A StatusCode of Good (0) is treated like a NULL and not encoded.
58
+ * (If it is an element of an JSON array it is encoded as the JSON literal ‘null’. )
59
+ */
60
+ if (statusCode.value === 0) {
61
+ return undefined;
62
+ }
63
+
64
+ const pojo: StatusCodeJSON = {};
65
+ // The Code is omitted if the numeric code is 0(Good).
66
+ if (statusCode.value !== 0) {
67
+ // The numeric code encoded as a JSON number.
68
+ pojo.Code = statusCode.value;
69
+ }
70
+ // The string literal associated with the numeric code encoded as JSON string.
71
+ // e.g. 0x80AB0000 has the associated literal “BadInvalidArgument”.
72
+ //
73
+ // The Symbol is omitted if the numeric code is 0(Good).
74
+ if (statusCode.value === 0) {
75
+ return pojo; // Good
76
+ }
77
+ const c = coerceStatusCode(statusCode.value & 0xffff0000); // remove InfoBits and OverflowBit
78
+ pojo.Symbol = c.name;
79
+ return pojo;
80
+ }
81
+
82
+ export function opcuaJsonEncodeStatusCode105(
83
+ statusCode: StatusCode,
84
+ mode: JsonEncoderMode105
85
+ ): StatusCodeJSON | number | undefined {
86
+ /*
87
+ * For the non-reversible form, StatusCode values shall be encoded as a JSON object with the fields defined in Table
88
+ *
89
+ * Symbol The string literal associated with the numeric code encoded as JSON string.
90
+ */
91
+
92
+ const pojo: StatusCodeJSON = {};
93
+ if (statusCode.value !== 0) {
94
+ pojo.Code = statusCode.value;
95
+ }
96
+
97
+ /*
98
+ * e.g. 0x80AB0000 has the associated literal “BadInvalidArgument”.
99
+ * Any InfoBits in the StatusCode are ignored when looking up the symbol.
100
+ * If the string literal is not known to the encoder the field is omitted.
101
+ * The field is omitted in the CompactEncoding.
102
+ * The field is omitted if the numeric code is 0 (Good).
103
+ */
104
+ if (mode === JsonEncoderMode105.Verbose) {
105
+ if (statusCode.value !== 0) {
106
+ const c = coerceStatusCode(statusCode.value & 0xffff0000); // remove InfoBits and OverflowBit
107
+ if (c?.name) {
108
+ pojo.Symbol = c.name;
109
+ }
110
+ }
111
+ }
112
+ return pojo;
113
+ }
114
+
115
+ export function opcuaJsonEncodeStatusCode(statusCode: StatusCode, scheme: JsonEncodingScheme) {
116
+ switch (scheme) {
117
+ case JsonEncodingScheme.DeprecatedNonReversible:
118
+ return opcuaJsonEncodeStatusCode104(statusCode, JsonEncoderMode104.NonReversible);
119
+ case JsonEncodingScheme.DeprecatedReversible:
120
+ return opcuaJsonEncodeStatusCode104(statusCode, JsonEncoderMode104.Reversible);
121
+ case JsonEncodingScheme.Compact:
122
+ return opcuaJsonEncodeStatusCode105(statusCode, JsonEncoderMode105.Compact);
123
+ case JsonEncodingScheme.Verbose:
124
+ return opcuaJsonEncodeStatusCode105(statusCode, JsonEncoderMode105.Verbose);
125
+ default:
126
+ throw new Error("Unknown JsonEncodingScheme");
127
+ }
128
+ }
129
+
130
+ /**
131
+ *
132
+ * @param pojo
133
+ *
134
+ * @returns
135
+ */
136
+ export function opcuaJsonDecodeStatusCode(pojo: StatusCodeJSON | number | null | undefined): StatusCode {
137
+ if (!pojo) {
138
+ return StatusCodes.Good;
139
+ }
140
+ if (typeof pojo === "number") {
141
+ return coerceStatusCode(pojo);
142
+ }
143
+ if (typeof pojo.Code === "number") {
144
+ return coerceStatusCode(pojo.Code);
145
+ }
146
+ if (!pojo.Code) {
147
+ // to trap undefined or null Code
148
+ return StatusCodes.Good;
149
+ }
150
+ return coerceStatusCode(pojo.Code);
151
+ }