NodeId.ts 14 KB

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