node-opcua-nodeid 2.97.0 → 2.98.1

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/nodeid.js CHANGED
@@ -1,383 +1,383 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sameNodeId = exports.resolveNodeId = exports.makeNodeId = exports.coerceNodeId = exports.NodeId = exports.NodeIdType = void 0;
4
- /**
5
- * @module node-opcua-nodeid
6
- */
7
- const node_opcua_assert_1 = require("node-opcua-assert");
8
- const node_opcua_constants_1 = require("node-opcua-constants");
9
- const node_opcua_guid_1 = require("node-opcua-guid");
10
- /**
11
- * `NodeIdType` an enumeration that specifies the possible types of a `NodeId` value.
12
- */
13
- var NodeIdType;
14
- (function (NodeIdType) {
15
- /**
16
- * @static
17
- * @property NUMERIC
18
- * @default 0x1
19
- */
20
- NodeIdType[NodeIdType["NUMERIC"] = 1] = "NUMERIC";
21
- /**
22
- * @static
23
- * @property STRING
24
- * @default 0x2
25
- */
26
- NodeIdType[NodeIdType["STRING"] = 2] = "STRING";
27
- /**
28
- * @static
29
- * @property GUID
30
- * @default 0x3
31
- */
32
- NodeIdType[NodeIdType["GUID"] = 3] = "GUID";
33
- /**
34
- * @static
35
- * @property BYTESTRING
36
- * @default 0x4
37
- */
38
- NodeIdType[NodeIdType["BYTESTRING"] = 4] = "BYTESTRING";
39
- })(NodeIdType = exports.NodeIdType || (exports.NodeIdType = {}));
40
- /*function defaultValue(identifierType: NodeIdType.BYTESTRING): null;
41
- function defaultValue(identifierType: NodeIdType.STRING): null;
42
- function defaultValue(identifierType: NodeIdType.NUMERIC): 0;
43
- function defaultValue(identifierType: NodeIdType.GUID): null;
44
- */
45
- function defaultValue(identifierType) {
46
- switch (identifierType) {
47
- case NodeIdType.GUID: return node_opcua_guid_1.emptyGuid;
48
- case NodeIdType.BYTESTRING: return null; // Buffer.alloc(0);
49
- case NodeIdType.STRING: return "";
50
- case NodeIdType.NUMERIC: return 0;
51
- }
52
- }
53
- const doDebug = false;
54
- /**
55
- * Construct a node ID
56
- *
57
- * @class NodeId
58
- * @example
59
- *
60
- * ``` javascript
61
- * const nodeId = new NodeId(NodeIdType.NUMERIC,123,1);
62
- * ```
63
- * @constructor
64
- */
65
- class NodeId {
66
- ;
67
- /**
68
- * @param identifierType - the nodeID type
69
- * @param value - the node id value. The type of Value depends on identifierType.
70
- * @param namespace - the index of the related namespace (optional , default value = 0 )
71
- */
72
- constructor(identifierType, value, namespace) {
73
- if (identifierType === null || identifierType === undefined) {
74
- this.identifierType = NodeIdType.NUMERIC;
75
- this.value = 0;
76
- this.namespace = 0;
77
- return;
78
- }
79
- this.identifierType = identifierType;
80
- this.value = value || defaultValue(identifierType);
81
- this.namespace = namespace || 0;
82
- // namespace shall be a UInt16
83
- (0, node_opcua_assert_1.assert)(this.namespace >= 0 && this.namespace <= 0xffff, "NodeId: invalid namespace value");
84
- (0, node_opcua_assert_1.assert)(this.identifierType !== NodeIdType.NUMERIC || (this.value !== null && this.value >= 0 && this.value <= 0xffffffff));
85
- (0, node_opcua_assert_1.assert)(this.identifierType !== NodeIdType.GUID || (0, node_opcua_guid_1.isValidGuid)(this.value), "NodeId: Guid is invalid");
86
- (0, node_opcua_assert_1.assert)(this.identifierType !== NodeIdType.STRING || typeof this.value === "string", "cannot empty string");
87
- if (this.identifierType === NodeIdType.GUID) {
88
- this.value = (0, node_opcua_guid_1.normalizeGuid)(value);
89
- }
90
- }
91
- /**
92
- * get the string representation of the nodeID.
93
- *
94
- * @method toString
95
- * @example
96
- *
97
- * ``` javascript
98
- * const nodeid = new NodeId(NodeIdType.NUMERIC, 123,1);
99
- * console.log(nodeid.toString());
100
- * ```
101
- *
102
- * ```
103
- * >"ns=1;i=123"
104
- * ```
105
- *
106
- * @param [options.addressSpace] {AddressSpace}
107
- * @return {String}
108
- */
109
- toString(options) {
110
- const addressSpace = options ? options.addressSpace : null;
111
- let str;
112
- const _this = this;
113
- switch (_this.identifierType) {
114
- case NodeIdType.NUMERIC:
115
- str = "ns=" + this.namespace + ";i=" + _this.value;
116
- break;
117
- case NodeIdType.STRING:
118
- str = "ns=" + this.namespace + ";s=" + _this.value;
119
- break;
120
- case NodeIdType.GUID:
121
- str = "ns=" + this.namespace + ";g=" + (0, node_opcua_guid_1.normalizeGuid)(_this.value);
122
- break;
123
- default:
124
- (0, node_opcua_assert_1.assert)(this.identifierType === NodeIdType.BYTESTRING, "invalid identifierType in NodeId : " + this.identifierType);
125
- if (this.value) {
126
- str = "ns=" + this.namespace + ";b=" + this.value.toString("base64");
127
- }
128
- else {
129
- str = "ns=" + this.namespace + ";b=<null>";
130
- }
131
- break;
132
- }
133
- if (addressSpace) {
134
- if (this.namespace === 0 && _this.identifierType === NodeIdType.NUMERIC) {
135
- // find standard browse name
136
- const name = reverse_map((this.value || 0).toString()) || "<undefined>";
137
- str += " " + name;
138
- }
139
- else if (addressSpace.findNode) {
140
- // let use the provided address space to figure out the browseNode of this node.
141
- // to make the message a little bit more useful.
142
- const n = addressSpace.findNode(this);
143
- str += " " + (n ? n.browseName.toString() : " (????)");
144
- }
145
- }
146
- return str;
147
- }
148
- /**
149
- * convert nodeId to a JSON string. same as {@link NodeId#toString }
150
- */
151
- toJSON() {
152
- return this.toString();
153
- }
154
- displayText() {
155
- if (this.namespace === 0 && this.identifierType === NodeIdType.NUMERIC) {
156
- const name = reverse_map(this.value.toString());
157
- if (name) {
158
- return name + " (" + this.toString() + ")";
159
- }
160
- }
161
- return this.toString();
162
- }
163
- /**
164
- * returns true if the NodeId is null or empty
165
- */
166
- isEmpty() {
167
- const _this = this;
168
- switch (_this.identifierType) {
169
- case NodeIdType.NUMERIC:
170
- return _this.value === 0;
171
- case NodeIdType.STRING:
172
- return !_this.value;
173
- case NodeIdType.GUID:
174
- return !_this.value || _this.value === node_opcua_guid_1.emptyGuid;
175
- default:
176
- return !_this.value || _this.value.length === 0;
177
- }
178
- }
179
- }
180
- exports.NodeId = NodeId;
181
- NodeId.NodeIdType = NodeIdType;
182
- NodeId.nullNodeId = new Proxy(new NodeId(NodeIdType.NUMERIC, 0, 0), {
183
- get: (target, prop) => {
184
- return target[prop];
185
- },
186
- set: () => {
187
- throw new Error("Cannot assign a value to constant NodeId.nullNodeId");
188
- }
189
- });
190
- const regexNamespaceI = /ns=([0-9]+);i=([0-9]+)/;
191
- const regexNamespaceS = /ns=([0-9]+);s=(.*)/;
192
- const regexNamespaceB = /ns=([0-9]+);b=(.*)/;
193
- const regexNamespaceG = /ns=([0-9]+);g=([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})/;
194
- /**
195
- * Convert a value into a nodeId:
196
- * @class opcua
197
- * @method coerceNodeId
198
- * @static
199
- *
200
- * @description:
201
- * - if nodeId is a string of form : "i=1234" => nodeId({value=1234, identifierType: NodeIdType.NUMERIC})
202
- * - if nodeId is a string of form : "s=foo" => nodeId({value="foo", identifierType: NodeIdType.STRING})
203
- * - if nodeId is a string of form : "b=ABCD=" => nodeId({value=decodeBase64("ABCD="), identifierType: NodeIdType.BYTESTRING})
204
- * - if nodeId is a {@link NodeId} : coerceNodeId returns value
205
- * @param value
206
- * @param namespace {number}
207
- */
208
- // eslint-disable-next-line max-statements
209
- function coerceNodeId(value, namespace) {
210
- let matches;
211
- let twoFirst;
212
- if (value instanceof NodeId) {
213
- return value;
214
- }
215
- value = value || 0;
216
- namespace = namespace || 0;
217
- let identifierType = NodeIdType.NUMERIC;
218
- if (typeof value === "string") {
219
- identifierType = NodeIdType.STRING;
220
- twoFirst = value.substring(0, 2);
221
- if (twoFirst === "i=") {
222
- identifierType = NodeIdType.NUMERIC;
223
- value = parseInt(value.substring(2), 10);
224
- }
225
- else if (twoFirst === "s=") {
226
- identifierType = NodeIdType.STRING;
227
- value = value.substring(2);
228
- }
229
- else if (twoFirst === "b=") {
230
- identifierType = NodeIdType.BYTESTRING;
231
- value = Buffer.from(value.substring(2), "base64");
232
- }
233
- else if (twoFirst === "g=") {
234
- identifierType = NodeIdType.GUID;
235
- value = (0, node_opcua_guid_1.normalizeGuid)(value.substring(2));
236
- (0, node_opcua_assert_1.assert)((0, node_opcua_guid_1.isValidGuid)(value));
237
- }
238
- else if ((0, node_opcua_guid_1.isValidGuid)(value)) {
239
- identifierType = NodeIdType.GUID;
240
- value = (0, node_opcua_guid_1.normalizeGuid)(value);
241
- }
242
- else if ((matches = regexNamespaceI.exec(value)) !== null) {
243
- identifierType = NodeIdType.NUMERIC;
244
- namespace = parseInt(matches[1], 10);
245
- value = parseInt(matches[2], 10);
246
- }
247
- else if ((matches = regexNamespaceS.exec(value)) !== null) {
248
- identifierType = NodeIdType.STRING;
249
- namespace = parseInt(matches[1], 10);
250
- value = matches[2];
251
- }
252
- else if ((matches = regexNamespaceB.exec(value)) !== null) {
253
- identifierType = NodeIdType.BYTESTRING;
254
- namespace = parseInt(matches[1], 10);
255
- value = Buffer.from(matches[2], "base64");
256
- }
257
- else if ((matches = regexNamespaceG.exec(value)) !== null) {
258
- identifierType = NodeIdType.GUID;
259
- namespace = parseInt(matches[1], 10);
260
- value = (0, node_opcua_guid_1.normalizeGuid)(matches[2]);
261
- }
262
- else {
263
- throw new Error("String cannot be coerced to a nodeId : " + value);
264
- }
265
- }
266
- else if (value instanceof Buffer) {
267
- identifierType = NodeIdType.BYTESTRING;
268
- }
269
- else if (value instanceof Object) {
270
- // it could be a Enum or a NodeId Like object
271
- const tmp = value;
272
- value = tmp.value;
273
- namespace = namespace || tmp.namespace;
274
- identifierType = tmp.identifierType || identifierType;
275
- return new NodeId(identifierType, value, namespace);
276
- }
277
- return new NodeId(identifierType, value, namespace);
278
- }
279
- exports.coerceNodeId = coerceNodeId;
280
- const regEx1 = /^(s|g|b|i|ns)=/;
281
- /**
282
- * construct a node Id from a value and a namespace.
283
- * @class opcua
284
- * @method makeNodeId
285
- * @static
286
- * @param {String|Buffer} value
287
- * @param [namespace]=0 {Number} the node id namespace
288
- * @return {NodeId}
289
- */
290
- function makeNodeId(value, namespace) {
291
- value = value || 0;
292
- namespace = namespace || 0;
293
- let identifierType = NodeIdType.NUMERIC;
294
- if (typeof value === "string") {
295
- if (value.match(regEx1)) {
296
- throw new Error("please use coerce NodeId instead");
297
- }
298
- // 1 2 3
299
- // 012345678901234567890123456789012345
300
- // "72962B91-FA75-4AE6-8D28-B404DC7DAF63"
301
- if ((0, node_opcua_guid_1.isValidGuid)(value)) {
302
- identifierType = NodeIdType.GUID;
303
- value = (0, node_opcua_guid_1.normalizeGuid)(value);
304
- }
305
- else {
306
- identifierType = NodeIdType.STRING;
307
- }
308
- }
309
- else if (value instanceof Buffer) {
310
- identifierType = NodeIdType.BYTESTRING;
311
- }
312
- const nodeId = new NodeId(identifierType, value, namespace);
313
- return nodeId;
314
- }
315
- exports.makeNodeId = makeNodeId;
316
- // reverse maps
317
- let _nodeIdToNameIndex = {};
318
- let _nameToNodeIdIndex = {};
319
- const regName = /[a-zA-Z_].*/;
320
- (function build_standard_nodeid_indexes() {
321
- function expand_map(directIndex) {
322
- for (const name in directIndex) {
323
- if (Object.prototype.hasOwnProperty.call(directIndex, name) && regName.exec(name) !== null) {
324
- const value = directIndex[name];
325
- _nodeIdToNameIndex[value] = name;
326
- _nameToNodeIdIndex[name] = new NodeId(NodeIdType.NUMERIC, value, 0);
327
- }
328
- }
329
- }
330
- _nodeIdToNameIndex = {};
331
- _nameToNodeIdIndex = {};
332
- expand_map(node_opcua_constants_1.ObjectIds);
333
- expand_map(node_opcua_constants_1.ObjectTypeIds);
334
- expand_map(node_opcua_constants_1.VariableIds);
335
- expand_map(node_opcua_constants_1.VariableTypeIds);
336
- expand_map(node_opcua_constants_1.MethodIds);
337
- expand_map(node_opcua_constants_1.ReferenceTypeIds);
338
- expand_map(node_opcua_constants_1.DataTypeIds);
339
- })();
340
- function reverse_map(nodeId) {
341
- return _nodeIdToNameIndex[nodeId];
342
- }
343
- /**
344
- * @class opcua
345
- * @method resolveNodeId
346
- * @static
347
- * @param nodeIdOrString
348
- * @return the nodeId
349
- */
350
- function resolveNodeId(nodeIdOrString) {
351
- let nodeId;
352
- const rawId = typeof nodeIdOrString === "string" ? _nameToNodeIdIndex[nodeIdOrString] : undefined;
353
- if (rawId !== undefined) {
354
- return rawId;
355
- }
356
- else {
357
- nodeId = coerceNodeId(nodeIdOrString);
358
- }
359
- return nodeId;
360
- }
361
- exports.resolveNodeId = resolveNodeId;
362
- NodeId.resolveNodeId = resolveNodeId;
363
- function sameNodeId(n1, n2) {
364
- if (n1.identifierType !== n2.identifierType) {
365
- return false;
366
- }
367
- if (n1.namespace !== n2.namespace) {
368
- return false;
369
- }
370
- switch (n1.identifierType) {
371
- case NodeIdType.NUMERIC:
372
- case NodeIdType.STRING:
373
- case NodeIdType.GUID:
374
- return n1.value === n2.value;
375
- case NodeIdType.BYTESTRING:
376
- return n1.value.toString("hex") === n2.value.toString("hex");
377
- default:
378
- throw new Error("Invalid identifier type");
379
- }
380
- }
381
- exports.sameNodeId = sameNodeId;
382
- NodeId.sameNodeId = sameNodeId;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sameNodeId = exports.resolveNodeId = exports.makeNodeId = exports.coerceNodeId = exports.NodeId = exports.NodeIdType = void 0;
4
+ /**
5
+ * @module node-opcua-nodeid
6
+ */
7
+ const node_opcua_assert_1 = require("node-opcua-assert");
8
+ const node_opcua_constants_1 = require("node-opcua-constants");
9
+ const node_opcua_guid_1 = require("node-opcua-guid");
10
+ /**
11
+ * `NodeIdType` an enumeration that specifies the possible types of a `NodeId` value.
12
+ */
13
+ var NodeIdType;
14
+ (function (NodeIdType) {
15
+ /**
16
+ * @static
17
+ * @property NUMERIC
18
+ * @default 0x1
19
+ */
20
+ NodeIdType[NodeIdType["NUMERIC"] = 1] = "NUMERIC";
21
+ /**
22
+ * @static
23
+ * @property STRING
24
+ * @default 0x2
25
+ */
26
+ NodeIdType[NodeIdType["STRING"] = 2] = "STRING";
27
+ /**
28
+ * @static
29
+ * @property GUID
30
+ * @default 0x3
31
+ */
32
+ NodeIdType[NodeIdType["GUID"] = 3] = "GUID";
33
+ /**
34
+ * @static
35
+ * @property BYTESTRING
36
+ * @default 0x4
37
+ */
38
+ NodeIdType[NodeIdType["BYTESTRING"] = 4] = "BYTESTRING";
39
+ })(NodeIdType = exports.NodeIdType || (exports.NodeIdType = {}));
40
+ /*function defaultValue(identifierType: NodeIdType.BYTESTRING): null;
41
+ function defaultValue(identifierType: NodeIdType.STRING): null;
42
+ function defaultValue(identifierType: NodeIdType.NUMERIC): 0;
43
+ function defaultValue(identifierType: NodeIdType.GUID): null;
44
+ */
45
+ function defaultValue(identifierType) {
46
+ switch (identifierType) {
47
+ case NodeIdType.GUID: return node_opcua_guid_1.emptyGuid;
48
+ case NodeIdType.BYTESTRING: return null; // Buffer.alloc(0);
49
+ case NodeIdType.STRING: return "";
50
+ case NodeIdType.NUMERIC: return 0;
51
+ }
52
+ }
53
+ const doDebug = false;
54
+ /**
55
+ * Construct a node ID
56
+ *
57
+ * @class NodeId
58
+ * @example
59
+ *
60
+ * ``` javascript
61
+ * const nodeId = new NodeId(NodeIdType.NUMERIC,123,1);
62
+ * ```
63
+ * @constructor
64
+ */
65
+ class NodeId {
66
+ ;
67
+ /**
68
+ * @param identifierType - the nodeID type
69
+ * @param value - the node id value. The type of Value depends on identifierType.
70
+ * @param namespace - the index of the related namespace (optional , default value = 0 )
71
+ */
72
+ constructor(identifierType, value, namespace) {
73
+ if (identifierType === null || identifierType === undefined) {
74
+ this.identifierType = NodeIdType.NUMERIC;
75
+ this.value = 0;
76
+ this.namespace = 0;
77
+ return;
78
+ }
79
+ this.identifierType = identifierType;
80
+ this.value = value || defaultValue(identifierType);
81
+ this.namespace = namespace || 0;
82
+ // namespace shall be a UInt16
83
+ (0, node_opcua_assert_1.assert)(this.namespace >= 0 && this.namespace <= 0xffff, "NodeId: invalid namespace value");
84
+ (0, node_opcua_assert_1.assert)(this.identifierType !== NodeIdType.NUMERIC || (this.value !== null && this.value >= 0 && this.value <= 0xffffffff));
85
+ (0, node_opcua_assert_1.assert)(this.identifierType !== NodeIdType.GUID || (0, node_opcua_guid_1.isValidGuid)(this.value), "NodeId: Guid is invalid");
86
+ (0, node_opcua_assert_1.assert)(this.identifierType !== NodeIdType.STRING || typeof this.value === "string", "cannot empty string");
87
+ if (this.identifierType === NodeIdType.GUID) {
88
+ this.value = (0, node_opcua_guid_1.normalizeGuid)(value);
89
+ }
90
+ }
91
+ /**
92
+ * get the string representation of the nodeID.
93
+ *
94
+ * @method toString
95
+ * @example
96
+ *
97
+ * ``` javascript
98
+ * const nodeid = new NodeId(NodeIdType.NUMERIC, 123,1);
99
+ * console.log(nodeid.toString());
100
+ * ```
101
+ *
102
+ * ```
103
+ * >"ns=1;i=123"
104
+ * ```
105
+ *
106
+ * @param [options.addressSpace] {AddressSpace}
107
+ * @return {String}
108
+ */
109
+ toString(options) {
110
+ const addressSpace = options ? options.addressSpace : null;
111
+ let str;
112
+ const _this = this;
113
+ switch (_this.identifierType) {
114
+ case NodeIdType.NUMERIC:
115
+ str = "ns=" + this.namespace + ";i=" + _this.value;
116
+ break;
117
+ case NodeIdType.STRING:
118
+ str = "ns=" + this.namespace + ";s=" + _this.value;
119
+ break;
120
+ case NodeIdType.GUID:
121
+ str = "ns=" + this.namespace + ";g=" + (0, node_opcua_guid_1.normalizeGuid)(_this.value);
122
+ break;
123
+ default:
124
+ (0, node_opcua_assert_1.assert)(this.identifierType === NodeIdType.BYTESTRING, "invalid identifierType in NodeId : " + this.identifierType);
125
+ if (this.value) {
126
+ str = "ns=" + this.namespace + ";b=" + this.value.toString("base64");
127
+ }
128
+ else {
129
+ str = "ns=" + this.namespace + ";b=<null>";
130
+ }
131
+ break;
132
+ }
133
+ if (addressSpace) {
134
+ if (this.namespace === 0 && _this.identifierType === NodeIdType.NUMERIC) {
135
+ // find standard browse name
136
+ const name = reverse_map((this.value || 0).toString()) || "<undefined>";
137
+ str += " " + name;
138
+ }
139
+ else if (addressSpace.findNode) {
140
+ // let use the provided address space to figure out the browseNode of this node.
141
+ // to make the message a little bit more useful.
142
+ const n = addressSpace.findNode(this);
143
+ str += " " + (n ? n.browseName.toString() : " (????)");
144
+ }
145
+ }
146
+ return str;
147
+ }
148
+ /**
149
+ * convert nodeId to a JSON string. same as {@link NodeId#toString }
150
+ */
151
+ toJSON() {
152
+ return this.toString();
153
+ }
154
+ displayText() {
155
+ if (this.namespace === 0 && this.identifierType === NodeIdType.NUMERIC) {
156
+ const name = reverse_map(this.value.toString());
157
+ if (name) {
158
+ return name + " (" + this.toString() + ")";
159
+ }
160
+ }
161
+ return this.toString();
162
+ }
163
+ /**
164
+ * returns true if the NodeId is null or empty
165
+ */
166
+ isEmpty() {
167
+ const _this = this;
168
+ switch (_this.identifierType) {
169
+ case NodeIdType.NUMERIC:
170
+ return _this.value === 0;
171
+ case NodeIdType.STRING:
172
+ return !_this.value;
173
+ case NodeIdType.GUID:
174
+ return !_this.value || _this.value === node_opcua_guid_1.emptyGuid;
175
+ default:
176
+ return !_this.value || _this.value.length === 0;
177
+ }
178
+ }
179
+ }
180
+ NodeId.NodeIdType = NodeIdType;
181
+ exports.NodeId = NodeId;
182
+ NodeId.nullNodeId = new Proxy(new NodeId(NodeIdType.NUMERIC, 0, 0), {
183
+ get: (target, prop) => {
184
+ return target[prop];
185
+ },
186
+ set: () => {
187
+ throw new Error("Cannot assign a value to constant NodeId.nullNodeId");
188
+ }
189
+ });
190
+ const regexNamespaceI = /ns=([0-9]+);i=([0-9]+)/;
191
+ const regexNamespaceS = /ns=([0-9]+);s=(.*)/;
192
+ const regexNamespaceB = /ns=([0-9]+);b=(.*)/;
193
+ const regexNamespaceG = /ns=([0-9]+);g=([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})/;
194
+ /**
195
+ * Convert a value into a nodeId:
196
+ * @class opcua
197
+ * @method coerceNodeId
198
+ * @static
199
+ *
200
+ * @description:
201
+ * - if nodeId is a string of form : "i=1234" => nodeId({value=1234, identifierType: NodeIdType.NUMERIC})
202
+ * - if nodeId is a string of form : "s=foo" => nodeId({value="foo", identifierType: NodeIdType.STRING})
203
+ * - if nodeId is a string of form : "b=ABCD=" => nodeId({value=decodeBase64("ABCD="), identifierType: NodeIdType.BYTESTRING})
204
+ * - if nodeId is a {@link NodeId} : coerceNodeId returns value
205
+ * @param value
206
+ * @param namespace {number}
207
+ */
208
+ // eslint-disable-next-line max-statements
209
+ function coerceNodeId(value, namespace) {
210
+ let matches;
211
+ let twoFirst;
212
+ if (value instanceof NodeId) {
213
+ return value;
214
+ }
215
+ value = value || 0;
216
+ namespace = namespace || 0;
217
+ let identifierType = NodeIdType.NUMERIC;
218
+ if (typeof value === "string") {
219
+ identifierType = NodeIdType.STRING;
220
+ twoFirst = value.substring(0, 2);
221
+ if (twoFirst === "i=") {
222
+ identifierType = NodeIdType.NUMERIC;
223
+ value = parseInt(value.substring(2), 10);
224
+ }
225
+ else if (twoFirst === "s=") {
226
+ identifierType = NodeIdType.STRING;
227
+ value = value.substring(2);
228
+ }
229
+ else if (twoFirst === "b=") {
230
+ identifierType = NodeIdType.BYTESTRING;
231
+ value = Buffer.from(value.substring(2), "base64");
232
+ }
233
+ else if (twoFirst === "g=") {
234
+ identifierType = NodeIdType.GUID;
235
+ value = (0, node_opcua_guid_1.normalizeGuid)(value.substring(2));
236
+ (0, node_opcua_assert_1.assert)((0, node_opcua_guid_1.isValidGuid)(value));
237
+ }
238
+ else if ((0, node_opcua_guid_1.isValidGuid)(value)) {
239
+ identifierType = NodeIdType.GUID;
240
+ value = (0, node_opcua_guid_1.normalizeGuid)(value);
241
+ }
242
+ else if ((matches = regexNamespaceI.exec(value)) !== null) {
243
+ identifierType = NodeIdType.NUMERIC;
244
+ namespace = parseInt(matches[1], 10);
245
+ value = parseInt(matches[2], 10);
246
+ }
247
+ else if ((matches = regexNamespaceS.exec(value)) !== null) {
248
+ identifierType = NodeIdType.STRING;
249
+ namespace = parseInt(matches[1], 10);
250
+ value = matches[2];
251
+ }
252
+ else if ((matches = regexNamespaceB.exec(value)) !== null) {
253
+ identifierType = NodeIdType.BYTESTRING;
254
+ namespace = parseInt(matches[1], 10);
255
+ value = Buffer.from(matches[2], "base64");
256
+ }
257
+ else if ((matches = regexNamespaceG.exec(value)) !== null) {
258
+ identifierType = NodeIdType.GUID;
259
+ namespace = parseInt(matches[1], 10);
260
+ value = (0, node_opcua_guid_1.normalizeGuid)(matches[2]);
261
+ }
262
+ else {
263
+ throw new Error("String cannot be coerced to a nodeId : " + value);
264
+ }
265
+ }
266
+ else if (value instanceof Buffer) {
267
+ identifierType = NodeIdType.BYTESTRING;
268
+ }
269
+ else if (value instanceof Object) {
270
+ // it could be a Enum or a NodeId Like object
271
+ const tmp = value;
272
+ value = tmp.value;
273
+ namespace = namespace || tmp.namespace;
274
+ identifierType = tmp.identifierType || identifierType;
275
+ return new NodeId(identifierType, value, namespace);
276
+ }
277
+ return new NodeId(identifierType, value, namespace);
278
+ }
279
+ exports.coerceNodeId = coerceNodeId;
280
+ const regEx1 = /^(s|g|b|i|ns)=/;
281
+ /**
282
+ * construct a node Id from a value and a namespace.
283
+ * @class opcua
284
+ * @method makeNodeId
285
+ * @static
286
+ * @param {String|Buffer} value
287
+ * @param [namespace]=0 {Number} the node id namespace
288
+ * @return {NodeId}
289
+ */
290
+ function makeNodeId(value, namespace) {
291
+ value = value || 0;
292
+ namespace = namespace || 0;
293
+ let identifierType = NodeIdType.NUMERIC;
294
+ if (typeof value === "string") {
295
+ if (value.match(regEx1)) {
296
+ throw new Error("please use coerce NodeId instead");
297
+ }
298
+ // 1 2 3
299
+ // 012345678901234567890123456789012345
300
+ // "72962B91-FA75-4AE6-8D28-B404DC7DAF63"
301
+ if ((0, node_opcua_guid_1.isValidGuid)(value)) {
302
+ identifierType = NodeIdType.GUID;
303
+ value = (0, node_opcua_guid_1.normalizeGuid)(value);
304
+ }
305
+ else {
306
+ identifierType = NodeIdType.STRING;
307
+ }
308
+ }
309
+ else if (value instanceof Buffer) {
310
+ identifierType = NodeIdType.BYTESTRING;
311
+ }
312
+ const nodeId = new NodeId(identifierType, value, namespace);
313
+ return nodeId;
314
+ }
315
+ exports.makeNodeId = makeNodeId;
316
+ // reverse maps
317
+ let _nodeIdToNameIndex = {};
318
+ let _nameToNodeIdIndex = {};
319
+ const regName = /[a-zA-Z_].*/;
320
+ (function build_standard_nodeid_indexes() {
321
+ function expand_map(directIndex) {
322
+ for (const name in directIndex) {
323
+ if (Object.prototype.hasOwnProperty.call(directIndex, name) && regName.exec(name) !== null) {
324
+ const value = directIndex[name];
325
+ _nodeIdToNameIndex[value] = name;
326
+ _nameToNodeIdIndex[name] = new NodeId(NodeIdType.NUMERIC, value, 0);
327
+ }
328
+ }
329
+ }
330
+ _nodeIdToNameIndex = {};
331
+ _nameToNodeIdIndex = {};
332
+ expand_map(node_opcua_constants_1.ObjectIds);
333
+ expand_map(node_opcua_constants_1.ObjectTypeIds);
334
+ expand_map(node_opcua_constants_1.VariableIds);
335
+ expand_map(node_opcua_constants_1.VariableTypeIds);
336
+ expand_map(node_opcua_constants_1.MethodIds);
337
+ expand_map(node_opcua_constants_1.ReferenceTypeIds);
338
+ expand_map(node_opcua_constants_1.DataTypeIds);
339
+ })();
340
+ function reverse_map(nodeId) {
341
+ return _nodeIdToNameIndex[nodeId];
342
+ }
343
+ /**
344
+ * @class opcua
345
+ * @method resolveNodeId
346
+ * @static
347
+ * @param nodeIdOrString
348
+ * @return the nodeId
349
+ */
350
+ function resolveNodeId(nodeIdOrString) {
351
+ let nodeId;
352
+ const rawId = typeof nodeIdOrString === "string" ? _nameToNodeIdIndex[nodeIdOrString] : undefined;
353
+ if (rawId !== undefined) {
354
+ return rawId;
355
+ }
356
+ else {
357
+ nodeId = coerceNodeId(nodeIdOrString);
358
+ }
359
+ return nodeId;
360
+ }
361
+ exports.resolveNodeId = resolveNodeId;
362
+ NodeId.resolveNodeId = resolveNodeId;
363
+ function sameNodeId(n1, n2) {
364
+ if (n1.identifierType !== n2.identifierType) {
365
+ return false;
366
+ }
367
+ if (n1.namespace !== n2.namespace) {
368
+ return false;
369
+ }
370
+ switch (n1.identifierType) {
371
+ case NodeIdType.NUMERIC:
372
+ case NodeIdType.STRING:
373
+ case NodeIdType.GUID:
374
+ return n1.value === n2.value;
375
+ case NodeIdType.BYTESTRING:
376
+ return n1.value.toString("hex") === n2.value.toString("hex");
377
+ default:
378
+ throw new Error("Invalid identifier type");
379
+ }
380
+ }
381
+ exports.sameNodeId = sameNodeId;
382
+ NodeId.sameNodeId = sameNodeId;
383
383
  //# sourceMappingURL=nodeid.js.map