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