NodeId.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. str = "ns=" + this.namespace + ";i=" + _this.value;
  134. break;
  135. case NodeIdType.STRING:
  136. str = "ns=" + this.namespace + ";s=" + _this.value;
  137. break;
  138. case NodeIdType.GUID:
  139. str = "ns=" + this.namespace + ";g=" + normalizeGuid(_this.value);
  140. break;
  141. default:
  142. assert(this.identifierType === NodeIdType.BYTESTRING, "invalid identifierType in NodeId : " + this.identifierType);
  143. if (this.value) {
  144. str = "ns=" + this.namespace + ";b=" + (this.value as Buffer).toString("base64");
  145. } else {
  146. str = "ns=" + this.namespace + ";b=<null>";
  147. }
  148. break;
  149. }
  150. if (addressSpace) {
  151. if (this.namespace === 0 && _this.identifierType === NodeIdType.NUMERIC) {
  152. // find standard browse name
  153. const name = reverse_map((this.value || 0).toString()) || "<undefined>";
  154. str += " " + name;
  155. } else if (addressSpace.findNode) {
  156. // let use the provided address space to figure out the browseNode of this node.
  157. // to make the message a little bit more useful.
  158. const n = addressSpace.findNode(this);
  159. str += " " + (n ? n.browseName.toString() : " (????)");
  160. }
  161. }
  162. return str;
  163. }
  164. /**
  165. * convert nodeId to a JSON string. same as {@link NodeId#toString }
  166. */
  167. public toJSON(): string {
  168. return this.toString();
  169. }
  170. public displayText(): string {
  171. if (this.namespace === 0 && this.identifierType === NodeIdType.NUMERIC) {
  172. const name = reverse_map(this.value.toString());
  173. if (name) {
  174. return name + " (" + this.toString() + ")";
  175. }
  176. }
  177. return this.toString();
  178. }
  179. /**
  180. * returns true if the NodeId is null or empty
  181. */
  182. public isEmpty(): boolean {
  183. const _this = this as INodeId;
  184. switch (_this.identifierType) {
  185. case NodeIdType.NUMERIC:
  186. return _this.value === 0;
  187. case NodeIdType.STRING:
  188. return !_this.value;
  189. case NodeIdType.GUID:
  190. return !_this.value || _this.value === emptyGuid;
  191. default:
  192. return !_this.value || (_this.value as Buffer).length === 0;
  193. }
  194. }
  195. }
  196. NodeId.nullNodeId = new Proxy(
  197. new NodeId(NodeIdType.NUMERIC, 0, 0),
  198. {
  199. get: (target: NodeId, prop: string) => {
  200. return (target as any)[prop];
  201. },
  202. set: () => {
  203. throw new Error("Cannot assign a value to constant NodeId.nullNodeId");
  204. }
  205. });
  206. export type NodeIdLike = string | NodeId | number;
  207. const regexNamespaceI = /ns=([0-9]+);i=([0-9]+)/;
  208. const regexNamespaceS = /ns=([0-9]+);s=(.*)/;
  209. const regexNamespaceB = /ns=([0-9]+);b=(.*)/;
  210. 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})/;
  211. /**
  212. * Convert a value into a nodeId:
  213. * @class opcua
  214. * @method coerceNodeId
  215. * @static
  216. *
  217. * @description:
  218. * - if nodeId is a string of form : "i=1234" => nodeId({value=1234, identifierType: NodeIdType.NUMERIC})
  219. * - if nodeId is a string of form : "s=foo" => nodeId({value="foo", identifierType: NodeIdType.STRING})
  220. * - if nodeId is a string of form : "b=ABCD=" => nodeId({value=decodeBase64("ABCD="), identifierType: NodeIdType.BYTESTRING})
  221. * - if nodeId is a {@link NodeId} : coerceNodeId returns value
  222. * @param value
  223. * @param namespace {number}
  224. */
  225. // eslint-disable-next-line max-statements
  226. export function coerceNodeId(value: unknown, namespace?: number): NodeId {
  227. let matches;
  228. let twoFirst;
  229. if (value instanceof NodeId) {
  230. return value;
  231. }
  232. value = value || 0;
  233. namespace = namespace || 0;
  234. let identifierType = NodeIdType.NUMERIC;
  235. if (typeof value === "string") {
  236. identifierType = NodeIdType.STRING;
  237. twoFirst = value.substring(0, 2);
  238. if (twoFirst === "i=") {
  239. identifierType = NodeIdType.NUMERIC;
  240. value = parseInt(value.substring(2), 10);
  241. } else if (twoFirst === "s=") {
  242. identifierType = NodeIdType.STRING;
  243. value = value.substring(2);
  244. } else if (twoFirst === "b=") {
  245. identifierType = NodeIdType.BYTESTRING;
  246. value = Buffer.from(value.substring(2), "base64");
  247. } else if (twoFirst === "g=") {
  248. identifierType = NodeIdType.GUID;
  249. value = normalizeGuid(value.substring(2));
  250. assert(isValidGuid(value as string));
  251. } else if (isValidGuid(value)) {
  252. identifierType = NodeIdType.GUID;
  253. value = normalizeGuid(value);
  254. } else if ((matches = regexNamespaceI.exec(value)) !== null) {
  255. identifierType = NodeIdType.NUMERIC;
  256. namespace = parseInt(matches[1], 10);
  257. value = parseInt(matches[2], 10);
  258. } else if ((matches = regexNamespaceS.exec(value)) !== null) {
  259. identifierType = NodeIdType.STRING;
  260. namespace = parseInt(matches[1], 10);
  261. value = matches[2];
  262. } else if ((matches = regexNamespaceB.exec(value)) !== null) {
  263. identifierType = NodeIdType.BYTESTRING;
  264. namespace = parseInt(matches[1], 10);
  265. value = Buffer.from(matches[2], "base64");
  266. } else if ((matches = regexNamespaceG.exec(value)) !== null) {
  267. identifierType = NodeIdType.GUID;
  268. namespace = parseInt(matches[1], 10);
  269. value = normalizeGuid(matches[2]);
  270. } else {
  271. throw new Error("String cannot be coerced to a nodeId : " + value);
  272. }
  273. } else if (value instanceof Buffer) {
  274. identifierType = NodeIdType.BYTESTRING;
  275. } else if (value instanceof Object) {
  276. // it could be a Enum or a NodeId Like object
  277. const tmp = value as any;
  278. value = tmp.value;
  279. namespace = namespace || tmp.namespace;
  280. identifierType = tmp.identifierType || identifierType;
  281. return new NodeId(identifierType, value as any, namespace);
  282. }
  283. return new NodeId(identifierType, value as any, namespace);
  284. }
  285. const regEx1 = /^(s|g|b|i|ns)=/;
  286. /**
  287. * construct a node Id from a value and a namespace.
  288. * @class opcua
  289. * @method makeNodeId
  290. * @static
  291. * @param {String|Buffer} value
  292. * @param [namespace]=0 {Number} the node id namespace
  293. * @return {NodeId}
  294. */
  295. export function makeNodeId(value: string | Buffer | number, namespace?: number): NodeId {
  296. value = value || 0;
  297. namespace = namespace || 0;
  298. let identifierType = NodeIdType.NUMERIC;
  299. if (typeof value === "string") {
  300. if (value.match(regEx1)) {
  301. throw new Error("please use coerce NodeId instead");
  302. }
  303. // 1 2 3
  304. // 012345678901234567890123456789012345
  305. // "72962B91-FA75-4AE6-8D28-B404DC7DAF63"
  306. if (isValidGuid(value)) {
  307. identifierType = NodeIdType.GUID;
  308. value = normalizeGuid(value);
  309. } else {
  310. identifierType = NodeIdType.STRING;
  311. }
  312. } else if (value instanceof Buffer) {
  313. identifierType = NodeIdType.BYTESTRING;
  314. }
  315. const nodeId = new NodeId(identifierType, value, namespace);
  316. return nodeId;
  317. }
  318. // reverse maps
  319. let _nodeIdToNameIndex: any = {};
  320. let _nameToNodeIdIndex: any = {};
  321. const regName = /[a-zA-Z_].*/;
  322. (function build_standard_nodeid_indexes() {
  323. function expand_map(directIndex: any) {
  324. for (const name in directIndex) {
  325. if (Object.prototype.hasOwnProperty.call(directIndex, name) && regName.exec(name) !== null) {
  326. const value = directIndex[name];
  327. _nodeIdToNameIndex[value] = name;
  328. _nameToNodeIdIndex[name] = new NodeId(NodeIdType.NUMERIC, value, 0);
  329. }
  330. }
  331. }
  332. _nodeIdToNameIndex = {};
  333. _nameToNodeIdIndex = {};
  334. expand_map(ObjectIds);
  335. expand_map(ObjectTypeIds);
  336. expand_map(VariableIds);
  337. expand_map(VariableTypeIds);
  338. expand_map(MethodIds);
  339. expand_map(ReferenceTypeIds);
  340. expand_map(DataTypeIds);
  341. })();
  342. function reverse_map(nodeId: string) {
  343. return _nodeIdToNameIndex[nodeId];
  344. }
  345. /**
  346. * @class opcua
  347. * @method resolveNodeId
  348. * @static
  349. * @param nodeIdOrString
  350. * @return the nodeId
  351. */
  352. export function resolveNodeId(nodeIdOrString: NodeIdLike): NodeId {
  353. let nodeId;
  354. const rawId = typeof nodeIdOrString === "string" ? _nameToNodeIdIndex[nodeIdOrString] : undefined;
  355. if (rawId !== undefined) {
  356. return rawId;
  357. } else {
  358. nodeId = coerceNodeId(nodeIdOrString);
  359. }
  360. return nodeId;
  361. }
  362. NodeId.resolveNodeId = resolveNodeId;
  363. export function sameNodeId(n1: NodeId, n2: NodeId): boolean {
  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 as Buffer).toString("hex") === (n2.value as Buffer).toString("hex");
  377. default:
  378. throw new Error("Invalid identifier type");
  379. }
  380. }
  381. NodeId.sameNodeId = sameNodeId;