node-opcua-client 2.165.2 → 2.167.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 (102) hide show
  1. package/dist/alarms_and_conditions/client_alarm_tools.d.ts +1 -1
  2. package/dist/alarms_and_conditions/client_alarm_tools.js +2 -2
  3. package/dist/alarms_and_conditions/client_alarm_tools.js.map +1 -1
  4. package/dist/alarms_and_conditions/client_tools.d.ts +3 -0
  5. package/dist/alarms_and_conditions/client_tools.js +2 -2
  6. package/dist/alarms_and_conditions/client_tools.js.map +1 -1
  7. package/dist/client_base.d.ts +47 -40
  8. package/dist/client_base.js +5 -6
  9. package/dist/client_base.js.map +1 -1
  10. package/dist/client_monitored_item.d.ts +7 -7
  11. package/dist/client_monitored_item.js.map +1 -1
  12. package/dist/client_monitored_item_base.d.ts +12 -13
  13. package/dist/client_monitored_item_group.d.ts +5 -5
  14. package/dist/client_monitored_item_toolbox.d.ts +6 -7
  15. package/dist/client_monitored_item_toolbox.js.map +1 -1
  16. package/dist/client_session.d.ts +19 -23
  17. package/dist/client_session.js.map +1 -1
  18. package/dist/client_session_keepalive_manager.d.ts +1 -1
  19. package/dist/client_session_keepalive_manager.js +6 -6
  20. package/dist/client_session_keepalive_manager.js.map +1 -1
  21. package/dist/client_subscription.d.ts +11 -12
  22. package/dist/client_subscription.js.map +1 -1
  23. package/dist/client_utils.d.ts +3 -3
  24. package/dist/client_utils.js.map +1 -1
  25. package/dist/common.d.ts +12 -12
  26. package/dist/index.d.ts +26 -27
  27. package/dist/index.js +65 -66
  28. package/dist/index.js.map +1 -1
  29. package/dist/opcua_client.d.ts +17 -19
  30. package/dist/opcua_client.js.map +1 -1
  31. package/dist/private/client_base_impl.d.ts +19 -18
  32. package/dist/private/client_base_impl.js +140 -119
  33. package/dist/private/client_base_impl.js.map +1 -1
  34. package/dist/private/client_monitored_item_group_impl.d.ts +4 -5
  35. package/dist/private/client_monitored_item_group_impl.js +1 -1
  36. package/dist/private/client_monitored_item_group_impl.js.map +1 -1
  37. package/dist/private/client_monitored_item_impl.d.ts +9 -10
  38. package/dist/private/client_monitored_item_impl.js.map +1 -1
  39. package/dist/private/client_publish_engine.d.ts +3 -3
  40. package/dist/private/client_publish_engine.js +6 -7
  41. package/dist/private/client_publish_engine.js.map +1 -1
  42. package/dist/private/client_session_impl.d.ts +24 -25
  43. package/dist/private/client_session_impl.js +14 -10
  44. package/dist/private/client_session_impl.js.map +1 -1
  45. package/dist/private/client_subscription_impl.d.ts +10 -11
  46. package/dist/private/client_subscription_impl.js +12 -11
  47. package/dist/private/client_subscription_impl.js.map +1 -1
  48. package/dist/private/i_private_client.d.ts +7 -7
  49. package/dist/private/opcua_client_impl.d.ts +12 -9
  50. package/dist/private/opcua_client_impl.js +58 -48
  51. package/dist/private/opcua_client_impl.js.map +1 -1
  52. package/dist/private/performance.js.map +1 -1
  53. package/dist/private/reconnection/client_publish_engine_reconnection.d.ts +1 -1
  54. package/dist/private/reconnection/client_publish_engine_reconnection.js +4 -2
  55. package/dist/private/reconnection/client_publish_engine_reconnection.js.map +1 -1
  56. package/dist/private/reconnection/client_reconnection.d.ts +1 -1
  57. package/dist/private/reconnection/client_subscription_reconnection.d.ts +1 -1
  58. package/dist/private/reconnection/client_subscription_reconnection.js +2 -2
  59. package/dist/private/reconnection/client_subscription_reconnection.js.map +1 -1
  60. package/dist/private/reconnection/reconnection.d.ts +3 -3
  61. package/dist/private/reconnection/reconnection.js +1 -1
  62. package/dist/private/reconnection/reconnection.js.map +1 -1
  63. package/dist/tools/findservers.d.ts +7 -2
  64. package/dist/tools/findservers.js +41 -53
  65. package/dist/tools/findservers.js.map +1 -1
  66. package/dist/tools/read_history_server_capabilities.d.ts +1 -1
  67. package/dist/tools/read_history_server_capabilities.js +9 -8
  68. package/dist/tools/read_history_server_capabilities.js.map +1 -1
  69. package/dist/user_identity_info.d.ts +1 -1
  70. package/dist/verify.js.map +1 -1
  71. package/package.json +41 -40
  72. package/source/alarms_and_conditions/client_alarm_tools.ts +14 -8
  73. package/source/alarms_and_conditions/client_tools.ts +7 -6
  74. package/source/client_base.ts +93 -79
  75. package/source/client_monitored_item.ts +7 -8
  76. package/source/client_monitored_item_base.ts +12 -14
  77. package/source/client_monitored_item_group.ts +5 -5
  78. package/source/client_monitored_item_toolbox.ts +13 -14
  79. package/source/client_session.ts +37 -40
  80. package/source/client_session_keepalive_manager.ts +7 -10
  81. package/source/client_subscription.ts +11 -12
  82. package/source/client_utils.ts +6 -6
  83. package/source/common.ts +12 -12
  84. package/source/index.ts +44 -40
  85. package/source/opcua_client.ts +24 -23
  86. package/source/private/client_base_impl.ts +276 -186
  87. package/source/private/client_monitored_item_group_impl.ts +10 -14
  88. package/source/private/client_monitored_item_impl.ts +17 -17
  89. package/source/private/client_publish_engine.ts +11 -12
  90. package/source/private/client_session_impl.ts +85 -82
  91. package/source/private/client_subscription_impl.ts +42 -41
  92. package/source/private/i_private_client.ts +8 -8
  93. package/source/private/opcua_client_impl.ts +118 -97
  94. package/source/private/performance.ts +2 -3
  95. package/source/private/reconnection/client_publish_engine_reconnection.ts +16 -15
  96. package/source/private/reconnection/client_reconnection.ts +1 -1
  97. package/source/private/reconnection/client_subscription_reconnection.ts +16 -13
  98. package/source/private/reconnection/reconnection.ts +130 -33
  99. package/source/tools/findservers.ts +43 -62
  100. package/source/tools/read_history_server_capabilities.ts +20 -22
  101. package/source/user_identity_info.ts +5 -5
  102. package/source/verify.ts +25 -25
@@ -8,32 +8,36 @@ exports.ClientBaseImpl = void 0;
8
8
  * @module node-opcua-client-private
9
9
  */
10
10
  // tslint:disable:no-unused-expression
11
- const fs_1 = __importDefault(require("fs"));
12
- const path_1 = __importDefault(require("path"));
11
+ const node_fs_1 = __importDefault(require("node:fs"));
12
+ const node_path_1 = __importDefault(require("node:path"));
13
+ const global_mutex_1 = require("@ster5/global-mutex");
13
14
  const async_1 = __importDefault(require("async"));
14
15
  const chalk_1 = __importDefault(require("chalk"));
15
- const global_mutex_1 = require("@ster5/global-mutex");
16
16
  const node_opcua_assert_1 = require("node-opcua-assert");
17
+ const node_opcua_certificate_manager_1 = require("node-opcua-certificate-manager");
17
18
  const node_opcua_common_1 = require("node-opcua-common");
18
19
  const web_1 = require("node-opcua-crypto/web");
19
20
  const node_opcua_date_time_1 = require("node-opcua-date-time");
20
21
  const node_opcua_debug_1 = require("node-opcua-debug");
21
- const node_opcua_common_2 = require("node-opcua-common");
22
22
  const node_opcua_hostname_1 = require("node-opcua-hostname");
23
23
  const node_opcua_secure_channel_1 = require("node-opcua-secure-channel");
24
24
  const node_opcua_service_discovery_1 = require("node-opcua-service-discovery");
25
25
  const node_opcua_service_endpoints_1 = require("node-opcua-service-endpoints");
26
26
  const node_opcua_service_secure_channel_1 = require("node-opcua-service-secure-channel");
27
+ const node_opcua_service_session_1 = require("node-opcua-service-session");
27
28
  const node_opcua_status_code_1 = require("node-opcua-status-code");
28
29
  const node_opcua_utils_1 = require("node-opcua-utils");
29
- const node_opcua_certificate_manager_1 = require("node-opcua-certificate-manager");
30
- const node_opcua_service_session_1 = require("node-opcua-service-session");
31
30
  const client_base_1 = require("../client_base");
32
31
  const verify_1 = require("../verify");
33
32
  const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
34
33
  const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
35
34
  const errorLog = (0, node_opcua_debug_1.make_errorLog)(__filename);
36
35
  const warningLog = (0, node_opcua_debug_1.make_warningLog)(__filename);
36
+ function makeCertificateThumbPrint(certificate) {
37
+ if (!certificate)
38
+ return null;
39
+ return (0, web_1.makeSHA1Thumbprint)(Array.isArray(certificate) ? certificate[0] : certificate);
40
+ }
37
41
  const traceInternalState = false;
38
42
  const defaultConnectionStrategy = {
39
43
  initialDelay: 1000,
@@ -55,7 +59,7 @@ function __findEndpoint(endpointUrl, params, _callback) {
55
59
  };
56
60
  const securityMode = params.securityMode;
57
61
  const securityPolicy = params.securityPolicy;
58
- const connectionStrategy = params.connectionStrategy;
62
+ const _connectionStrategy = params.connectionStrategy;
59
63
  const options = {
60
64
  applicationName: params.applicationName,
61
65
  applicationUri: params.applicationUri,
@@ -77,7 +81,9 @@ function __findEndpoint(endpointUrl, params, _callback) {
77
81
  const tasks = [
78
82
  (innerCallback) => {
79
83
  // rebind backoff handler
80
- masterClient.listeners("backoff").forEach((handler) => client.on("backoff", handler));
84
+ masterClient.listeners("backoff").forEach((handler) => {
85
+ client.on("backoff", handler);
86
+ });
81
87
  // c8 ignore next
82
88
  if (doDebug) {
83
89
  client.on("backoff", (retryCount, delay) => {
@@ -102,7 +108,7 @@ function __findEndpoint(endpointUrl, params, _callback) {
102
108
  (innerCallback) => {
103
109
  client.getEndpoints((err, endpoints) => {
104
110
  if (err) {
105
- err.message = "error in getEndpoints \n" + err.message;
111
+ err.message = `error in getEndpoints \n${err.message}`;
106
112
  return innerCallback(err);
107
113
  }
108
114
  // c8 ignore next
@@ -161,9 +167,9 @@ async function _verify_serverCertificate(certificateManager, serverCertificate)
161
167
  const status1 = await certificateManager.verifyCertificate(serverCertificate);
162
168
  debugLog(status1);
163
169
  }
164
- warningLog("serverCertificate = ", (0, web_1.makeSHA1Thumbprint)(serverCertificate).toString("hex"));
170
+ warningLog("serverCertificate = ", makeCertificateThumbPrint(serverCertificate)?.toString("hex") || "none");
165
171
  warningLog("serverCertificate = ", serverCertificate.toString("base64"));
166
- throw new Error("server Certificate verification failed with err " + status?.toString());
172
+ throw new Error(`server Certificate verification failed with err ${status?.toString()}`);
167
173
  }
168
174
  }
169
175
  const forceEndpointDiscoveryOnConnect = !!parseInt(process.env.NODEOPCUA_CLIENT_FORCE_ENDPOINT_DISCOVERY || "0", 10);
@@ -225,7 +231,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
225
231
  * true if the client is trying to reconnect to the server after a connection break.
226
232
  */
227
233
  get isReconnecting() {
228
- return (!!(this._secureChannel && this._secureChannel.isConnecting) ||
234
+ return (!!this._secureChannel?.isConnecting ||
229
235
  this._internalState === "reconnecting_newchannel_connected" ||
230
236
  this._internalState === "reconnecting");
231
237
  }
@@ -304,6 +310,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
304
310
  if (doDebug) {
305
311
  debugLog(chalk_1.default.cyan(` Client ${this._instanceNumber} ${this.clientName} emitting `), chalk_1.default.magentaBright(eventName));
306
312
  }
313
+ // @ts-expect-error
307
314
  return super.emit(eventName, ...others);
308
315
  }
309
316
  constructor(options) {
@@ -313,7 +320,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
313
320
  }
314
321
  options.privateKeyFile = options.privateKeyFile || options.clientCertificateManager.privateKey;
315
322
  options.certificateFile =
316
- options.certificateFile || path_1.default.join(options.clientCertificateManager.rootDir, "own/certs/client_certificate.pem");
323
+ options.certificateFile || node_path_1.default.join(options.clientCertificateManager.rootDir, "own/certs/client_certificate.pem");
317
324
  super(options);
318
325
  this._setInternalState("uninitialized");
319
326
  this._instanceNumber = g_ClientCounter++;
@@ -335,7 +342,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
335
342
  this.defaultSecureTokenLifetime = options.defaultSecureTokenLifetime || 600000;
336
343
  this.defaultTransactionTimeout = options.defaultTransactionTimeout;
337
344
  this.tokenRenewalInterval = options.tokenRenewalInterval || 0;
338
- (0, node_opcua_assert_1.assert)(isFinite(this.tokenRenewalInterval) && this.tokenRenewalInterval >= 0);
345
+ (0, node_opcua_assert_1.assert)(Number.isFinite(this.tokenRenewalInterval) && this.tokenRenewalInterval >= 0);
339
346
  this.securityMode = (0, node_opcua_service_secure_channel_1.coerceMessageSecurityMode)(options.securityMode);
340
347
  this.securityPolicy = (0, node_opcua_secure_channel_1.coerceSecurityPolicy)(options.securityPolicy);
341
348
  this.serverCertificate = options.serverCertificate;
@@ -375,15 +382,13 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
375
382
  this._secureChannel = null;
376
383
  callback();
377
384
  });
378
- this.once("reconnection_canceled", () => {
379
- /* empty */
380
- });
381
385
  }
382
386
  _recreate_secure_channel(callback) {
383
387
  debugLog("_recreate_secure_channel... while internalState is", this._internalState);
384
388
  if (!this.knowsServerEndpoint) {
385
389
  debugLog("Cannot reconnect , server endpoint is unknown");
386
- return callback(new Error("Cannot reconnect, server endpoint is unknown - this.knowsServerEndpoint = false"));
390
+ callback(new Error("Cannot reconnect, server endpoint is unknown - this.knowsServerEndpoint = false"));
391
+ return;
387
392
  }
388
393
  (0, node_opcua_assert_1.assert)(this.knowsServerEndpoint);
389
394
  this._setInternalState("reconnecting");
@@ -400,7 +405,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
400
405
  const _when_reconnectionIsCanceled = (callback) => {
401
406
  doDebug && debugLog("attempt to recreate a new secure channel : suspended because reconnection is canceled !");
402
407
  this.emit("reconnection_canceled");
403
- return callback(new Error("Reconnection has been canceled - " + this.clientName));
408
+ return callback(new Error(`Reconnection has been canceled - ${this.clientName}`));
404
409
  };
405
410
  const _failAndRetry = (err, message, callback) => {
406
411
  debugLog("failAndRetry; ", message);
@@ -442,17 +447,18 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
442
447
  return _when_internal_error(err, callback);
443
448
  }
444
449
  if (err.message.match(/ECONNREFUSED|ECONNABORTED|ETIMEDOUT/)) {
445
- return _failAndRetry(err, "create secure channel failed with ECONNREFUSED|ECONNABORTED|ETIMEDOUT\n" + err.message, callback);
450
+ return _failAndRetry(err, `create secure channel failed with ECONNREFUSED|ECONNABORTED|ETIMEDOUT\n${err.message}`, callback);
446
451
  }
447
452
  if (err.message.match("Backoff aborted.")) {
448
- return _failAndRetry(err, "cannot create secure channel (backoff aborted)", callback);
453
+ _failAndRetry(err, "cannot create secure channel (backoff aborted)", callback);
454
+ return;
449
455
  }
450
- if (err.message.match("BadCertificateInvalid") ||
451
- err.message.match(/socket has been disconnected by third party/)) {
452
- // it is possible also that hte server has shutdown innapropriately the connection
456
+ if (err?.message.match("BadCertificateInvalid") ||
457
+ err?.message.match(/socket has been disconnected by third party/)) {
458
+ // it is possible also that hte server has shutdown innapropriately the connection
453
459
  warningLog("the server certificate has changed, we need to retrieve server certificate again: ", err.message);
454
460
  const oldServerCertificate = this.serverCertificate;
455
- warningLog("old server certificate ", oldServerCertificate ? (0, web_1.makeSHA1Thumbprint)(oldServerCertificate).toString("hex") : "undefined");
461
+ warningLog("old server certificate ", makeCertificateThumbPrint(oldServerCertificate)?.toString("hex") || "undefined");
456
462
  // the server may have shut down the channel because its certificate
457
463
  // has changed ....
458
464
  // let request the server certificate again ....
@@ -461,9 +467,9 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
461
467
  return _failAndRetry(err1, "Failing to fetch new server certificate", callback);
462
468
  }
463
469
  const newServerCertificate = this.serverCertificate;
464
- warningLog("new server certificate ", (0, web_1.makeSHA1Thumbprint)(newServerCertificate).toString("hex"));
465
- const sha1Old = oldServerCertificate ? (0, web_1.makeSHA1Thumbprint)(oldServerCertificate) : null;
466
- const sha1New = newServerCertificate ? (0, web_1.makeSHA1Thumbprint)(newServerCertificate) : null;
470
+ warningLog("new server certificate ", makeCertificateThumbPrint(newServerCertificate)?.toString("hex") || "none");
471
+ const sha1Old = makeCertificateThumbPrint(oldServerCertificate)?.toString("hex") || null;
472
+ const sha1New = makeCertificateThumbPrint(newServerCertificate)?.toString("hex") || null;
467
473
  if (sha1Old === sha1New) {
468
474
  warningLog("server certificate has not changed, but was expected to have changed");
469
475
  return _failAndRetry(new Error("Server Certificate not changed"), "Failing to fetch new server certificate", callback);
@@ -568,7 +574,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
568
574
  });
569
575
  }
570
576
  static async createCertificate(clientCertificateManager, certificateFile, applicationName, applicationUri) {
571
- if (!fs_1.default.existsSync(certificateFile)) {
577
+ if (!node_fs_1.default.existsSync(certificateFile)) {
572
578
  const hostname = (0, node_opcua_hostname_1.getHostname)();
573
579
  // this.serverInfo.applicationUri!;
574
580
  await clientCertificateManager.createSelfSignedCertificate({
@@ -582,8 +588,8 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
582
588
  });
583
589
  }
584
590
  // c8 ignore next
585
- if (!fs_1.default.existsSync(certificateFile)) {
586
- throw new Error(" cannot locate certificate file " + certificateFile);
591
+ if (!node_fs_1.default.existsSync(certificateFile)) {
592
+ throw new Error(` cannot locate certificate file ${certificateFile}`);
587
593
  }
588
594
  }
589
595
  async createDefaultCertificate() {
@@ -593,7 +599,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
593
599
  }
594
600
  this._inCreateDefaultCertificate = true;
595
601
  if (!(0, node_opcua_utils_1.checkFileExistsAndIsNotEmpty)(this.certificateFile)) {
596
- await (0, global_mutex_1.withLock)({ fileToLock: this.certificateFile + ".mutex" }, async () => {
602
+ await (0, global_mutex_1.withLock)({ fileToLock: `${this.certificateFile}.mutex` }, async () => {
597
603
  if ((0, node_opcua_utils_1.checkFileExistsAndIsNotEmpty)(this.certificateFile)) {
598
604
  // the file may have been created in between
599
605
  return;
@@ -603,15 +609,15 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
603
609
  debugLog("privateKey = ", this.privateKeyFile);
604
610
  debugLog(" = ", this.clientCertificateManager.privateKey);
605
611
  debugLog("certificateFile = ", this.certificateFile);
606
- const certificate = this.getCertificate();
607
- const privateKey = this.getPrivateKey();
612
+ const _certificate = this.getCertificate();
613
+ const _privateKey = this.getPrivateKey();
608
614
  });
609
615
  }
610
616
  this._inCreateDefaultCertificate = false;
611
617
  }
612
618
  _getBuiltApplicationUri() {
613
619
  if (!this._applicationUri) {
614
- this._applicationUri = (0, node_opcua_common_2.makeApplicationUrn)((0, node_opcua_hostname_1.getHostname)(), this.applicationName);
620
+ this._applicationUri = (0, node_opcua_common_1.makeApplicationUrn)((0, node_opcua_hostname_1.getHostname)(), this.applicationName);
615
621
  }
616
622
  return this._applicationUri;
617
623
  }
@@ -627,8 +633,8 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
627
633
  await this.clientCertificateManager.initialize();
628
634
  await this.createDefaultCertificate();
629
635
  // c8 ignore next
630
- if (!fs_1.default.existsSync(this.privateKeyFile)) {
631
- throw new Error(" cannot locate private key file " + this.privateKeyFile);
636
+ if (!node_fs_1.default.existsSync(this.privateKeyFile)) {
637
+ throw new Error(` cannot locate private key file ${this.privateKeyFile}`);
632
638
  }
633
639
  if (this.isUnusable())
634
640
  return;
@@ -643,13 +649,13 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
643
649
  debugLog(err.message);
644
650
  this.emit("connection_failed", err);
645
651
  this._setInternalState("disconnected");
646
- return callback(err);
652
+ callback(err);
647
653
  }
648
654
  _handleDisconnectionWhileConnecting(err, callback) {
649
655
  debugLog(err.message);
650
656
  this.emit("connection_failed", err);
651
657
  this._setInternalState("disconnected");
652
- return callback(err);
658
+ callback(err);
653
659
  }
654
660
  _handleSuccessfulConnection(callback) {
655
661
  debugLog(" Connected successfully to ", this.endpointUrl);
@@ -662,7 +668,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
662
668
  const callback = args[1];
663
669
  (0, node_opcua_assert_1.assert)(typeof callback === "function", "expecting a callback");
664
670
  if (typeof endpointUrl !== "string" || endpointUrl.length <= 0) {
665
- errorLog("[NODE-OPCUA-E03] OPCUAClient#connect expects a valid endpoint : " + endpointUrl);
671
+ errorLog(`[NODE-OPCUA-E03] OPCUAClient#connect expects a valid endpoint : ${endpointUrl}`);
666
672
  callback(new Error("Invalid endpoint"));
667
673
  return;
668
674
  }
@@ -696,7 +702,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
696
702
  }
697
703
  if (forceEndpointDiscoveryOnConnect) {
698
704
  debugLog("connecting with adjusted endpoint : ", adjustedEndpointUrl, " was =", endpointUrl);
699
- this._connectStep2(adjustedEndpointUrl, callback);
705
+ this._connectStep2(adjustedEndpointUrl || "", callback);
700
706
  }
701
707
  else {
702
708
  debugLog("connecting with endpoint : ", endpointUrl);
@@ -734,15 +740,13 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
734
740
  }
735
741
  debugLog(chalk_1.default.red("SecureChannel creation has failed with error :", err.message));
736
742
  if (err.message.match(/ECONNABORTED/)) {
737
- debugLog(chalk_1.default.yellow("- The client cannot to :" + endpointUrl + ". Connection has been aborted."));
743
+ debugLog(chalk_1.default.yellow(`- The client cannot to :${endpointUrl}. Connection has been aborted.`));
738
744
  err = new Error("The connection has been aborted");
739
745
  this._handleUnrecoverableConnectionFailure(err, callback);
740
746
  }
741
747
  else if (err.message.match(/ECONNREF/)) {
742
- debugLog(chalk_1.default.yellow("- The client cannot to :" + endpointUrl + ". Server is not reachable."));
743
- err = new Error("The connection cannot be established with server " +
744
- endpointUrl +
745
- " .\n" +
748
+ debugLog(chalk_1.default.yellow(`- The client cannot to :${endpointUrl}. Server is not reachable.`));
749
+ err = new Error(`The connection cannot be established with server ${endpointUrl} .\n` +
746
750
  "Please check that the server is up and running or your network configuration.\n" +
747
751
  "Err = (" +
748
752
  err.message +
@@ -754,7 +758,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
754
758
  this._handleDisconnectionWhileConnecting(err, callback);
755
759
  }
756
760
  else {
757
- err = new Error("The connection may have been rejected by server,\n" + "Err = (" + err.message + ")");
761
+ err = new Error(`The connection may have been rejected by server,\n Err = (${err.message})`);
758
762
  this._handleUnrecoverableConnectionFailure(err, callback);
759
763
  }
760
764
  }
@@ -764,16 +768,18 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
764
768
  if (!this._secureChannel) {
765
769
  // this may happen if the Server has closed the connection abruptly for some unknown reason
766
770
  // or if the tcp connection has been broken.
767
- return callback(new Error("performMessageTransaction: No SecureChannel , connection may have been canceled abruptly by server"));
771
+ callback(new Error("performMessageTransaction: No SecureChannel , connection may have been canceled abruptly by server"));
772
+ return;
768
773
  }
769
774
  if (this._internalState !== "connected" &&
770
775
  this._internalState !== "reconnecting_newchannel_connected" &&
771
776
  this._internalState !== "connecting" &&
772
777
  this._internalState !== "reconnecting") {
773
- return callback(new Error("performMessageTransaction: Invalid client state = " +
778
+ callback(new Error("performMessageTransaction: Invalid client state = " +
774
779
  this._internalState +
775
780
  " while performing a transaction " +
776
781
  request.schema.name));
782
+ return;
777
783
  }
778
784
  (0, node_opcua_assert_1.assert)(this._secureChannel);
779
785
  (0, node_opcua_assert_1.assert)(request);
@@ -812,7 +818,8 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
812
818
  }
813
819
  getEndpoints(...args) {
814
820
  if (args.length === 1) {
815
- return this.getEndpoints({}, args[0]);
821
+ this.getEndpoints({}, args[0]);
822
+ return;
816
823
  }
817
824
  const options = args[0];
818
825
  const callback = args[1];
@@ -829,29 +836,38 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
829
836
  });
830
837
  this.performMessageTransaction(request, (err, response) => {
831
838
  if (err) {
832
- return callback(err);
839
+ callback(err);
840
+ return;
833
841
  }
834
842
  this._serverEndpoints = [];
835
843
  // c8 ignore next
836
844
  if (!response || !(response instanceof node_opcua_service_endpoints_1.GetEndpointsResponse)) {
837
- return callback(new Error("Internal Error"));
845
+ callback(new Error("Internal Error"));
846
+ return;
838
847
  }
839
- if (response && response.endpoints) {
848
+ if (response?.endpoints) {
840
849
  this._serverEndpoints = response.endpoints;
841
850
  }
842
851
  callback(null, this._serverEndpoints);
843
852
  });
853
+ return;
844
854
  }
845
855
  /**
846
856
  * @deprecated
847
857
  */
848
858
  getEndpointsRequest(options, callback) {
849
859
  warningLog("note: ClientBaseImpl#getEndpointsRequest is deprecated, use ClientBaseImpl#getEndpoints instead");
850
- return this.getEndpoints(options, callback);
860
+ this.getEndpoints(options, callback);
851
861
  }
852
862
  findServers(...args) {
853
863
  if (args.length === 1) {
854
- return this.findServers({}, args[0]);
864
+ if (typeof args[0] === "function") {
865
+ this.findServers({}, args[0]);
866
+ return void 0;
867
+ }
868
+ else {
869
+ throw new Error("Invalid arguments");
870
+ }
855
871
  }
856
872
  const options = args[0];
857
873
  const callback = args[1];
@@ -862,11 +878,13 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
862
878
  });
863
879
  this.performMessageTransaction(request, (err, response) => {
864
880
  if (err) {
865
- return callback(err);
881
+ callback(err);
882
+ return;
866
883
  }
867
884
  /* c8 ignore next */
868
885
  if (!response || !(response instanceof node_opcua_service_discovery_1.FindServersResponse)) {
869
- return callback(new Error("Internal Error"));
886
+ callback(new Error("Internal Error"));
887
+ return;
870
888
  }
871
889
  response.servers = response.servers || [];
872
890
  callback(null, response.servers);
@@ -874,27 +892,31 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
874
892
  }
875
893
  findServersOnNetwork(...args) {
876
894
  if (args.length === 1) {
877
- return this.findServersOnNetwork({}, args[0]);
895
+ this.findServersOnNetwork({}, args[0]);
896
+ return;
878
897
  }
879
898
  const options = args[0];
880
899
  const callback = args[1];
881
900
  const request = new node_opcua_service_discovery_1.FindServersOnNetworkRequest(options);
882
901
  this.performMessageTransaction(request, (err, response) => {
883
902
  if (err) {
884
- return callback(err);
903
+ callback(err);
904
+ return;
885
905
  }
886
906
  /* c8 ignore next */
887
907
  if (!response || !(response instanceof node_opcua_service_discovery_1.FindServersOnNetworkResponse)) {
888
- return new Error("Internal Error");
908
+ callback(new Error("Internal Error"));
909
+ return;
889
910
  }
890
911
  response.servers = response.servers || [];
891
912
  callback(null, response.servers);
892
913
  });
914
+ return;
893
915
  }
894
916
  _removeSession(session) {
895
917
  const index = this._sessions.indexOf(session);
896
918
  if (index >= 0) {
897
- const s = this._sessions.splice(index, 1)[0];
919
+ const _s = this._sessions.splice(index, 1)[0];
898
920
  // assert(s === session);
899
921
  // assert(session._client === this);
900
922
  session._client = null;
@@ -925,23 +947,19 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
925
947
  }
926
948
  });
927
949
  }
928
- closeSession(...args) {
929
- const session = args[0];
930
- const deleteSubscriptions = args[1];
931
- const callback = args[2];
950
+ closeSession(session, deleteSubscriptions, callback) {
932
951
  (0, node_opcua_assert_1.assert)(typeof deleteSubscriptions === "boolean");
933
- (0, node_opcua_assert_1.assert)(typeof callback === "function");
934
952
  (0, node_opcua_assert_1.assert)(session);
935
953
  (0, node_opcua_assert_1.assert)(session._client === this, "session must be attached to this");
936
954
  session._closed = true;
937
955
  // todo : send close session on secure channel
938
- this._closeSession(session, deleteSubscriptions, (err, response) => {
956
+ this._closeSession(session, deleteSubscriptions, (err, _response) => {
939
957
  session.emitCloseEvent(node_opcua_status_code_1.StatusCodes.Good);
940
958
  this._removeSession(session);
941
959
  session.dispose();
942
960
  (0, node_opcua_assert_1.assert)(this._sessions.indexOf(session) === -1);
943
961
  (0, node_opcua_assert_1.assert)(session._closed, "session must indicate it is closed");
944
- callback(err ? err : undefined);
962
+ callback?.(err ? err : undefined);
945
963
  });
946
964
  }
947
965
  // eslint-disable-next-line max-statements
@@ -951,12 +969,12 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
951
969
  this._reconnectionIsCanceled = true;
952
970
  if (this._tmpClient) {
953
971
  warningLog("disconnecting client while tmpClient exists", this._tmpClient.clientName);
954
- this._tmpClient.disconnect((err) => {
972
+ this._tmpClient.disconnect((_err) => {
955
973
  this._tmpClient = undefined;
956
974
  // retry disconnect on main client
957
975
  this.disconnect(callback);
958
976
  });
959
- return;
977
+ return undefined;
960
978
  }
961
979
  if (this._internalState === "disconnected" || this._internalState === "disconnecting") {
962
980
  if (this._internalState === "disconnecting") {
@@ -965,7 +983,8 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
965
983
  if (!this._reconnectionIsCanceled && this.isReconnecting) {
966
984
  errorLog("Internal Error : _reconnectionIsCanceled should be true if isReconnecting is true");
967
985
  }
968
- return callback();
986
+ callback();
987
+ return undefined;
969
988
  }
970
989
  debugLog("disconnecting client! (will set reconnectionIsCanceled to true");
971
990
  this._reconnectionIsCanceled = true;
@@ -979,7 +998,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
979
998
  // sessions cannot be cancelled properly and must be discarded.
980
999
  this.disconnect(callback);
981
1000
  });
982
- return;
1001
+ return undefined;
983
1002
  }
984
1003
  if (this._sessions.length && !this.keepPendingSessionsOnDisconnect) {
985
1004
  debugLog("warning : disconnection : closing pending sessions");
@@ -988,7 +1007,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
988
1007
  this._close_pending_sessions(( /*err*/) => {
989
1008
  this.disconnect(callback);
990
1009
  });
991
- return;
1010
+ return undefined;
992
1011
  }
993
1012
  debugLog("Disconnecting !");
994
1013
  this._setInternalState("disconnecting");
@@ -1030,6 +1049,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1030
1049
  // this.emit("close", null);
1031
1050
  callback();
1032
1051
  }
1052
+ return undefined;
1033
1053
  }
1034
1054
  // override me !
1035
1055
  _on_connection_reestablished(callback) {
@@ -1037,35 +1057,32 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1037
1057
  }
1038
1058
  toString() {
1039
1059
  let str = "\n";
1040
- str += " defaultSecureTokenLifetime.... " + this.defaultSecureTokenLifetime + "\n";
1041
- str += " securityMode.................. " + node_opcua_service_secure_channel_1.MessageSecurityMode[this.securityMode] + "\n";
1042
- str += " securityPolicy................ " + this.securityPolicy.toString() + "\n";
1043
- str += " certificate fingerprint....... " + (0, web_1.makeSHA1Thumbprint)(this.getCertificate()).toString("hex") + "\n";
1044
- str +=
1045
- " server certificate fingerprint " +
1046
- (this.serverCertificate ? (0, web_1.makeSHA1Thumbprint)(this.serverCertificate).toString("hex") : "") +
1047
- "\n";
1060
+ str += ` defaultSecureTokenLifetime.... ${this.defaultSecureTokenLifetime}\n`;
1061
+ str += ` securityMode.................. ${node_opcua_service_secure_channel_1.MessageSecurityMode[this.securityMode]}\n`;
1062
+ str += ` securityPolicy................ ${this.securityPolicy.toString()}\n`;
1063
+ str += ` certificate fingerprint....... ${makeCertificateThumbPrint(this.getCertificate())?.toString("hex") || "none"}\n`;
1064
+ str += ` server certificate fingerprint ${makeCertificateThumbPrint(this.serverCertificate)?.toString("hex") || "none"}\n`;
1048
1065
  // this.serverCertificate = options.serverCertificate || null + "\n";
1049
- str += " keepSessionAlive.............. " + this.keepSessionAlive + "\n";
1050
- str += " bytesRead..................... " + this.bytesRead + "\n";
1051
- str += " bytesWritten.................. " + this.bytesWritten + "\n";
1052
- str += " transactionsPerformed......... " + this.transactionsPerformed + "\n";
1053
- str += " timedOutRequestCount.......... " + this.timedOutRequestCount + "\n";
1066
+ str += ` keepSessionAlive.............. ${this.keepSessionAlive}\n`;
1067
+ str += ` bytesRead..................... ${this.bytesRead}\n`;
1068
+ str += ` bytesWritten.................. ${this.bytesWritten}\n`;
1069
+ str += ` transactionsPerformed......... ${this.transactionsPerformed}\n`;
1070
+ str += ` timedOutRequestCount.......... ${this.timedOutRequestCount}\n`;
1054
1071
  str += " connectionStrategy." + "\n";
1055
- str += " .maxRetry............... " + this.connectionStrategy.maxRetry + "\n";
1056
- str += " .initialDelay........... " + this.connectionStrategy.initialDelay + "\n";
1057
- str += " .maxDelay............... " + this.connectionStrategy.maxDelay + "\n";
1058
- str += " .randomisationFactor.... " + this.connectionStrategy.randomisationFactor + "\n";
1059
- str += " keepSessionAlive.............. " + this.keepSessionAlive + "\n";
1060
- str += " applicationName............... " + this.applicationName + "\n";
1061
- str += " applicationUri................ " + this._getBuiltApplicationUri() + "\n";
1062
- str += " clientName.................... " + this.clientName + "\n";
1063
- str += " reconnectOnFailure............ " + this.reconnectOnFailure + "\n";
1064
- str += " isReconnecting................ " + this.isReconnecting + "\n";
1065
- str += " (internal state).............. " + this._internalState + "\n";
1066
- str += " sessions count................ " + this.getSessions().length + "\n";
1072
+ str += ` .maxRetry............... ${this.connectionStrategy.maxRetry}\n`;
1073
+ str += ` .initialDelay........... ${this.connectionStrategy.initialDelay}\n`;
1074
+ str += ` .maxDelay............... ${this.connectionStrategy.maxDelay}\n`;
1075
+ str += ` .randomisationFactor.... ${this.connectionStrategy.randomisationFactor}\n`;
1076
+ str += ` keepSessionAlive.............. ${this.keepSessionAlive}\n`;
1077
+ str += ` applicationName............... ${this.applicationName}\n`;
1078
+ str += ` applicationUri................ ${this._getBuiltApplicationUri()}\n`;
1079
+ str += ` clientName.................... ${this.clientName}\n`;
1080
+ str += ` reconnectOnFailure............ ${this.reconnectOnFailure}\n`;
1081
+ str += ` isReconnecting................ ${this.isReconnecting}\n`;
1082
+ str += ` (internal state).............. ${this._internalState}\n`;
1083
+ str += ` sessions count................ ${this.getSessions().length}\n`;
1067
1084
  if (this._secureChannel) {
1068
- str += "secureChannel:\n" + this._secureChannel.toString();
1085
+ str += `secureChannel:\n${this._secureChannel.toString()}`;
1069
1086
  }
1070
1087
  return str;
1071
1088
  }
@@ -1073,7 +1090,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1073
1090
  return this._sessions;
1074
1091
  }
1075
1092
  getTransportSettings() {
1076
- return this._secureChannel.getTransportSettings();
1093
+ return this._secureChannel?.getTransportSettings() || {};
1077
1094
  }
1078
1095
  _addSession(session) {
1079
1096
  (0, node_opcua_assert_1.assert)(!session._client || session._client === this);
@@ -1114,14 +1131,16 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1114
1131
  privateKeyFile,
1115
1132
  clientCertificateManager: this.clientCertificateManager
1116
1133
  };
1117
- return __findEndpoint.call(this, discoveryUrl, params, (err, result) => {
1134
+ __findEndpoint.call(this, discoveryUrl, params, (err, result) => {
1118
1135
  if (err) {
1119
- return callback(err);
1136
+ callback(err);
1137
+ return;
1120
1138
  }
1121
1139
  // c8 ignore next
1122
1140
  if (!result) {
1123
1141
  const err1 = new Error("internal error");
1124
- return callback(err1);
1142
+ callback(err1);
1143
+ return;
1125
1144
  }
1126
1145
  const endpoint = result.selectedEndpoint;
1127
1146
  if (!endpoint) {
@@ -1130,23 +1149,24 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1130
1149
  node_opcua_service_secure_channel_1.MessageSecurityMode[this.securityMode] +
1131
1150
  " policy = " +
1132
1151
  this.securityPolicy);
1133
- return callback(err1);
1152
+ callback(err1);
1153
+ return;
1134
1154
  }
1135
1155
  (0, node_opcua_assert_1.assert)(endpoint);
1136
1156
  _verify_serverCertificate(this.clientCertificateManager, endpoint.serverCertificate)
1137
1157
  .then(() => {
1138
1158
  this.serverCertificate = endpoint.serverCertificate;
1139
- callback(null, endpoint.endpointUrl);
1159
+ callback(null, endpoint.endpointUrl || "");
1140
1160
  })
1141
1161
  .catch((err1) => {
1142
1162
  warningLog("[NODE-OPCUA-W25] client's server certificate verification has failed ", err1.message);
1143
1163
  warningLog(" clientCertificateManager.rootDir = ", this.clientCertificateManager.rootDir);
1144
- const f = (b) => " " + b.toString("base64").replace(/(.{80})/g, "$1\n ");
1164
+ const f = (b) => ` ${b.toString("base64").replace(/(.{80})/g, "$1\n ")}`;
1145
1165
  const chain = (0, web_1.split_der)(endpoint.serverCertificate);
1146
1166
  warningLog(` server certificate contains ${chain.length} elements`);
1147
1167
  for (let i = 0; i < chain.length; i++) {
1148
1168
  const c = chain[i];
1149
- warningLog("certificate", i, "\n" + f(c));
1169
+ warningLog("certificate", i, `\n${f(c)}`);
1150
1170
  }
1151
1171
  if (chain.length > 1) {
1152
1172
  warningLog(" verify also that the issuer certificate is trusted and the issuer's certificate is present in the issuer.cert folder\n" +
@@ -1155,7 +1175,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1155
1175
  else {
1156
1176
  warningLog(" verify that the server certificate is trusted or that server certificate issuer's certificate is present in the issuer folder");
1157
1177
  }
1158
- return callback(err1);
1178
+ callback(err1);
1159
1179
  });
1160
1180
  });
1161
1181
  }
@@ -1205,7 +1225,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1205
1225
  // in a attempt to preserve resources in the case of a DDOS attack for instance.
1206
1226
  if (err) {
1207
1227
  const msg = session.authenticationToken ? session.authenticationToken.toString() : "";
1208
- debugLog(" failing to close session " + msg);
1228
+ debugLog(` failing to close session ${msg}`);
1209
1229
  }
1210
1230
  next();
1211
1231
  });
@@ -1217,7 +1237,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1217
1237
  .join(" "));
1218
1238
  }
1219
1239
  (0, node_opcua_assert_1.assert)(this._sessions.length === 0, " failed to disconnect exiting sessions ");
1220
- callback(err);
1240
+ callback(err || undefined);
1221
1241
  });
1222
1242
  }
1223
1243
  _install_secure_channel_event_handlers(secureChannel) {
@@ -1258,7 +1278,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1258
1278
  // secureChannel requests a new token
1259
1279
  debugLog("SecureChannel Security Token ", token.tokenId, "live time was =", token.revisedLifetime, " is about to expired , it's time to request a new token");
1260
1280
  // forward message to upper level
1261
- this.emit("lifetime_75", token, secureChannel);
1281
+ this.emit("lifetime_75", token);
1262
1282
  });
1263
1283
  secureChannel.on("security_token_renewed", (token) => {
1264
1284
  // forward message to upper level
@@ -1275,7 +1295,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1275
1295
  /**
1276
1296
  * @event connection_lost
1277
1297
  */
1278
- this.emit("connection_lost", err?.message); // instead of "close"
1298
+ this.emit("connection_lost"); // instead of "close"
1279
1299
  }
1280
1300
  // this is a normal close operation initiated by us
1281
1301
  this.emit("close", err); // instead of "close"
@@ -1289,7 +1309,7 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1289
1309
  /**
1290
1310
  * @event connection_lost
1291
1311
  */
1292
- this.emit("connection_lost", err?.message); // instead of "close"
1312
+ this.emit("connection_lost"); // instead of "close"
1293
1313
  this._repairConnection();
1294
1314
  }
1295
1315
  }
@@ -1408,10 +1428,10 @@ class ClientBaseImpl extends node_opcua_common_1.OPCUASecureObject {
1408
1428
  * @internal
1409
1429
  * @private
1410
1430
  */
1411
- __createSession_step2(session, callback) {
1431
+ __createSession_step2(_session, _callback) {
1412
1432
  throw new Error("Please override");
1413
1433
  }
1414
- _activateSession(session, userIdentity, callback) {
1434
+ _activateSession(_session, _userIdentity, _callback) {
1415
1435
  throw new Error("Please override");
1416
1436
  }
1417
1437
  }
@@ -1419,22 +1439,23 @@ exports.ClientBaseImpl = ClientBaseImpl;
1419
1439
  // tslint:disable-next-line: max-classes-per-file
1420
1440
  class TmpClient extends ClientBaseImpl {
1421
1441
  constructor(options) {
1422
- options.clientName = (options.clientName || "") + "_TmpClient";
1442
+ options.clientName = `${options.clientName || ""}_TmpClient`;
1423
1443
  super(options);
1424
1444
  }
1425
1445
  connect(endpoint, callback) {
1426
1446
  debugLog("connecting to TmpClient");
1427
1447
  // c8 ignore next
1428
1448
  if (this._internalState !== "disconnected") {
1429
- callback(new Error("TmpClient#connect: invalid internal state = " + this._internalState));
1449
+ callback?.(new Error(`TmpClient#connect: invalid internal state = ${this._internalState}`));
1430
1450
  return;
1431
1451
  }
1432
1452
  this._setInternalState("connecting");
1433
1453
  this._connectStep2(endpoint, (err) => {
1434
1454
  if (this.isUnusable()) {
1435
- return this._handleUnrecoverableConnectionFailure(new Error("premature disconnection 4"), callback);
1455
+ this._handleUnrecoverableConnectionFailure(new Error("premature disconnection 4"), callback || (() => { }));
1456
+ return;
1436
1457
  }
1437
- callback(err);
1458
+ callback?.(err);
1438
1459
  });
1439
1460
  }
1440
1461
  }