node-opcua-server 2.164.2 → 2.165.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,20 +9,18 @@ exports.filterDiagnosticInfo = filterDiagnosticInfo;
9
9
  /**
10
10
  * @module node-opcua-server
11
11
  */
12
- const crypto_1 = require("crypto");
13
- const util_1 = require("util");
14
- const async_1 = __importDefault(require("async"));
12
+ const node_crypto_1 = require("node:crypto");
13
+ const node_util_1 = require("node:util");
15
14
  const chalk_1 = __importDefault(require("chalk"));
16
- const node_opcua_hostname_1 = require("node-opcua-hostname");
17
- const node_opcua_assert_1 = require("node-opcua-assert");
18
- const node_opcua_utils_1 = require("node-opcua-utils");
19
15
  const node_opcua_address_space_1 = require("node-opcua-address-space");
16
+ const node_opcua_assert_1 = require("node-opcua-assert");
20
17
  const node_opcua_certificate_manager_1 = require("node-opcua-certificate-manager");
21
18
  const node_opcua_common_1 = require("node-opcua-common");
22
19
  const web_1 = require("node-opcua-crypto/web");
23
20
  const node_opcua_data_model_1 = require("node-opcua-data-model");
24
21
  const node_opcua_data_value_1 = require("node-opcua-data-value");
25
22
  const node_opcua_debug_1 = require("node-opcua-debug");
23
+ const node_opcua_hostname_1 = require("node-opcua-hostname");
26
24
  const node_opcua_object_registry_1 = require("node-opcua-object-registry");
27
25
  const node_opcua_secure_channel_1 = require("node-opcua-secure-channel");
28
26
  const node_opcua_service_browse_1 = require("node-opcua-service-browse");
@@ -39,9 +37,9 @@ const node_opcua_service_translate_browse_path_1 = require("node-opcua-service-t
39
37
  const node_opcua_service_write_1 = require("node-opcua-service-write");
40
38
  const node_opcua_status_code_1 = require("node-opcua-status-code");
41
39
  const node_opcua_types_1 = require("node-opcua-types");
40
+ const node_opcua_utils_1 = require("node-opcua-utils");
42
41
  const node_opcua_variant_1 = require("node-opcua-variant");
43
- const node_opcua_variant_2 = require("node-opcua-variant");
44
- const node_opcua_utils_2 = require("node-opcua-utils");
42
+ const thenify_ex_1 = require("thenify-ex");
45
43
  const base_server_1 = require("./base_server");
46
44
  const factory_1 = require("./factory");
47
45
  const monitored_item_1 = require("./monitored_item");
@@ -55,19 +53,16 @@ const user_manager_ua_1 = require("./user_manager_ua");
55
53
  function isSubscriptionIdInvalid(subscriptionId) {
56
54
  return subscriptionId < 0 || subscriptionId >= 0xffffffff;
57
55
  }
58
- // tslint:disable-next-line:no-var-requires
59
- const thenify_ex_1 = require("thenify-ex");
60
- // tslint:disable-next-line:no-var-requires
61
56
  const package_info = require("../package.json");
62
57
  const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
63
58
  const errorLog = (0, node_opcua_debug_1.make_errorLog)(__filename);
64
59
  const warningLog = (0, node_opcua_debug_1.make_warningLog)(__filename);
65
60
  const default_maxConnectionsPerEndpoint = 10;
66
- function g_sendError(channel, message, ResponseClass, statusCode) {
61
+ function g_sendError(channel, message, _ResponseClass, statusCode) {
67
62
  const response = new node_opcua_types_1.ServiceFault({
68
63
  responseHeader: { serviceResult: statusCode }
69
64
  });
70
- return channel.send_response("MSG", response, message);
65
+ channel.send_response("MSG", response, message);
71
66
  }
72
67
  const default_build_info = {
73
68
  manufacturerName: "NodeOPCUA : MIT Licence ( see http://node-opcua.github.io/)",
@@ -90,7 +85,7 @@ function _adjust_session_timeout(sessionTimeout) {
90
85
  }
91
86
  function channel_has_session(channel, session) {
92
87
  if (session.channel === channel) {
93
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(channel.sessionTokens, session.authenticationToken.toString()));
88
+ (0, node_opcua_assert_1.assert)(Object.hasOwn(channel.sessionTokens, session.authenticationToken.toString()));
94
89
  return true;
95
90
  }
96
91
  return false;
@@ -102,12 +97,12 @@ function moveSessionToChannel(session, channel) {
102
97
  }
103
98
  session._detach_channel();
104
99
  session._attach_channel(channel);
105
- (0, node_opcua_assert_1.assert)(session.channel.channelId === channel.channelId);
100
+ (0, node_opcua_assert_1.assert)(session.channel?.channelId === channel.channelId);
106
101
  }
107
102
  async function _attempt_to_close_some_old_unactivated_session(server) {
108
- const session = server.engine.getOldestInactiveSession();
103
+ const session = server.engine?.getOldestInactiveSession();
109
104
  if (session) {
110
- await server.engine.closeSession(session.authenticationToken, false, "Forcing");
105
+ await server.engine?.closeSession(session.authenticationToken, false, "Forcing");
111
106
  }
112
107
  }
113
108
  function getRequiredEndpointInfo(endpoint) {
@@ -129,7 +124,7 @@ function getRequiredEndpointInfo(endpoint) {
129
124
  userIdentityTokens: endpoint.userIdentityTokens
130
125
  });
131
126
  // reduce even further by explicitly setting unwanted members to null
132
- e.server.applicationName = null;
127
+ e.server.applicationName = new node_opcua_data_model_1.LocalizedText({ text: "" });
133
128
  // xx e.server.applicationType = null as any;
134
129
  e.server.gatewayServerUri = null;
135
130
  e.server.discoveryProfileUri = null;
@@ -145,9 +140,9 @@ function _serverEndpointsForCreateSessionResponse(server, endpointUrl, serverUri
145
140
  // https://reference.opcfoundation.org/v104/Core/docs/Part4/5.6.2/
146
141
  // https://reference.opcfoundation.org/v105/Core/docs/Part4/5.6.2/
147
142
  return server
148
- ._get_endpoints(endpointUrl)
143
+ .findMatchingEndpoints(endpointUrl)
149
144
  .filter((e) => !e.restricted) // remove restricted endpoints
150
- .filter((e) => (0, node_opcua_utils_2.matchUri)(e.endpointUrl, endpointUrl))
145
+ .filter((e) => (0, node_opcua_utils_1.matchUri)(e.endpointUrl, endpointUrl))
151
146
  .map(getRequiredEndpointInfo);
152
147
  }
153
148
  function adjustSecurityPolicy(channel, userTokenPolicy_securityPolicyUri) {
@@ -162,11 +157,17 @@ function adjustSecurityPolicy(channel, userTokenPolicy_securityPolicyUri) {
162
157
  }
163
158
  function findUserTokenByPolicy(endpoint_description, userTokenType, policyId) {
164
159
  (0, node_opcua_assert_1.assert)(endpoint_description instanceof node_opcua_types_1.EndpointDescription);
160
+ if (!endpoint_description.userIdentityTokens) {
161
+ return null;
162
+ }
165
163
  const r = endpoint_description.userIdentityTokens.filter((userIdentity) => userIdentity.tokenType === userTokenType && (!policyId || userIdentity.policyId === policyId));
166
164
  return r.length === 0 ? null : r[0];
167
165
  }
168
166
  function findUserTokenPolicy(endpoint_description, userTokenType) {
169
167
  (0, node_opcua_assert_1.assert)(endpoint_description instanceof node_opcua_types_1.EndpointDescription);
168
+ if (!endpoint_description.userIdentityTokens) {
169
+ return null;
170
+ }
170
171
  const r = endpoint_description.userIdentityTokens.filter((userIdentity) => {
171
172
  (0, node_opcua_assert_1.assert)(userIdentity.tokenType !== undefined);
172
173
  return userIdentity.tokenType === userTokenType;
@@ -182,6 +183,12 @@ function createAnonymousIdentityToken(endpoint_desc) {
182
183
  return new node_opcua_service_session_1.AnonymousIdentityToken({ policyId: userTokenPolicy.policyId });
183
184
  }
184
185
  function sameIdentityToken(token1, token2) {
186
+ if (!token1 && !token2) {
187
+ return true;
188
+ }
189
+ if (!token1 || !token2) {
190
+ return false;
191
+ }
185
192
  if (token1 instanceof node_opcua_service_session_1.UserNameIdentityToken) {
186
193
  if (!(token2 instanceof node_opcua_service_session_1.UserNameIdentityToken)) {
187
194
  return false;
@@ -299,7 +306,7 @@ function build_scanning_node_function(addressSpace, itemToMonitor) {
299
306
  };
300
307
  }
301
308
  }
302
- function prepareMonitoredItem(context, addressSpace, monitoredItem) {
309
+ function prepareMonitoredItem(_context, addressSpace, monitoredItem) {
303
310
  const itemToMonitor = monitoredItem.itemToMonitor;
304
311
  const readNodeFunc = build_scanning_node_function(addressSpace, itemToMonitor);
305
312
  monitoredItem.samplingFunc = readNodeFunc;
@@ -374,7 +381,7 @@ function _installRegisterServerManager(self) {
374
381
  });
375
382
  }
376
383
  function validate_applicationUri(channel, request) {
377
- const applicationUri = request.clientDescription.applicationUri;
384
+ const applicationUri = request.clientDescription.applicationUri || "";
378
385
  const clientCertificate = request.clientCertificate;
379
386
  // if session is insecure there is no need to check certificate information
380
387
  if (channel.securityMode === node_opcua_secure_channel_1.MessageSecurityMode.None) {
@@ -384,7 +391,7 @@ function validate_applicationUri(channel, request) {
384
391
  return true; // can't check
385
392
  }
386
393
  const e = (0, web_1.exploreCertificate)(clientCertificate);
387
- const uniformResourceIdentifier = e.tbsCertificate.extensions.subjectAltName?.uniformResourceIdentifier ?? null;
394
+ const uniformResourceIdentifier = e.tbsCertificate.extensions?.subjectAltName?.uniformResourceIdentifier ?? null;
388
395
  const applicationUriFromCert = uniformResourceIdentifier && uniformResourceIdentifier.length > 0 ? uniformResourceIdentifier[0] : null;
389
396
  /* c8 ignore next */
390
397
  if (applicationUriFromCert !== applicationUri) {
@@ -396,7 +403,7 @@ function validate_applicationUri(channel, request) {
396
403
  }
397
404
  function validate_security_endpoint(server, request, channel) {
398
405
  debugLog("validate_security_endpoint = ", request.endpointUrl);
399
- let endpoints = server._get_endpoints(request.endpointUrl);
406
+ let endpoints = server.findMatchingEndpoints(request.endpointUrl);
400
407
  // endpointUrl String The network address that the Client used to access the Session Endpoint.
401
408
  // The HostName portion of the URL should be one of the HostNames for the application that are
402
409
  // specified in the Server’s ApplicationInstanceCertificate (see 7.2). The Server shall raise an
@@ -408,22 +415,22 @@ function validate_security_endpoint(server, request, channel) {
408
415
  // sometime endpoints have a extra leading "/" that can be ignored
409
416
  // don't be too harsh.
410
417
  if (endpoints.length === 0 && request.endpointUrl?.endsWith("/")) {
411
- endpoints = server._get_endpoints(request.endpointUrl.slice(0, -1));
418
+ endpoints = server.findMatchingEndpoints(request.endpointUrl.slice(0, -1));
412
419
  }
413
420
  if (endpoints.length === 0) {
414
421
  // we have a UrlMismatch here
415
- const ua_server = server.engine.addressSpace.rootFolder.objects.server;
422
+ const ua_server = server.engine.addressSpace?.rootFolder.objects.server;
416
423
  if (!request.endpointUrl?.match(/localhost/i) || OPCUAServer.requestExactEndpointUrl) {
417
424
  warningLog("Cannot find suitable endpoints in available endpoints. endpointUri =", request.endpointUrl);
418
425
  }
419
- ua_server.raiseEvent("AuditUrlMismatchEventType", {
426
+ ua_server?.raiseEvent("AuditUrlMismatchEventType", {
420
427
  endpointUrl: { dataType: node_opcua_variant_1.DataType.String, value: request.endpointUrl }
421
428
  });
422
429
  if (OPCUAServer.requestExactEndpointUrl) {
423
430
  return { errCode: node_opcua_status_code_1.StatusCodes.BadServiceUnsupported };
424
431
  }
425
432
  else {
426
- endpoints = server._get_endpoints(null);
433
+ endpoints = server.findMatchingEndpoints(null);
427
434
  }
428
435
  }
429
436
  // ignore restricted endpoints
@@ -435,7 +442,7 @@ function validate_security_endpoint(server, request, channel) {
435
442
  return { errCode: node_opcua_status_code_1.StatusCodes.BadSecurityModeRejected };
436
443
  }
437
444
  const endpoints_matching_security_policy = endpoints_matching_security_mode.filter((e) => {
438
- return e.securityPolicyUri === channel.securityPolicy;
445
+ return e.securityPolicyUri === channel?.securityPolicy;
439
446
  });
440
447
  if (endpoints_matching_security_policy.length === 0) {
441
448
  return { errCode: node_opcua_status_code_1.StatusCodes.BadSecurityPolicyRejected };
@@ -443,7 +450,10 @@ function validate_security_endpoint(server, request, channel) {
443
450
  if (endpoints_matching_security_policy.length !== 1) {
444
451
  debugLog("endpoints_matching_security_policy= ", endpoints_matching_security_policy.length);
445
452
  }
446
- return { errCode: node_opcua_status_code_1.StatusCodes.Good, endpoint: endpoints_matching_security_policy[0] };
453
+ return {
454
+ errCode: node_opcua_status_code_1.StatusCodes.Good,
455
+ endpoint: endpoints_matching_security_policy[0]
456
+ };
447
457
  }
448
458
  function filterDiagnosticInfo(returnDiagnostics, response) {
449
459
  if (node_opcua_data_model_1.RESPONSE_DIAGNOSTICS_MASK_ALL & returnDiagnostics) {
@@ -477,6 +487,12 @@ const g_requestExactEndpointUrl = !!process.env.NODEOPCUA_SERVER_REQUEST_EXACT_E
477
487
  *
478
488
  */
479
489
  class OPCUAServer extends base_server_1.OPCUABaseServer {
490
+ engine;
491
+ registerServerMethod;
492
+ discoveryServerEndpointUrl;
493
+ registerServerManager;
494
+ capabilitiesForMDNS;
495
+ userCertificateManager;
480
496
  static defaultShutdownTimeout = 100; // 250 ms
481
497
  /**
482
498
  * if requestExactEndpointUrl is set to true the server will only accept createSession that have a endpointUrl that strictly matches
@@ -575,6 +591,117 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
575
591
  get isAuditing() {
576
592
  return this.engine ? this.engine.isAuditing : false;
577
593
  }
594
+ /**
595
+ * Set the current server state.
596
+ *
597
+ * Updates both the internal state and the
598
+ * `Server.ServerStatus.State` variable in the
599
+ * address space so that OPC UA reads reflect the
600
+ * new state immediately.
601
+ */
602
+ setServerState(serverState) {
603
+ this.engine.setServerState(serverState);
604
+ }
605
+ /**
606
+ * Read the current `ServerState` from the
607
+ * internal server status.
608
+ */
609
+ getServerState() {
610
+ return this.engine.getServerState();
611
+ }
612
+ /**
613
+ * Set or clear a temporary role-policy override.
614
+ *
615
+ * When set, the override's `getUserRoles(username)`
616
+ * is called **before** the default `userManager`.
617
+ * Returning a `NodeId[]` overrides the roles;
618
+ * returning `null` falls through to the default.
619
+ *
620
+ * Call with `null` to remove the override and
621
+ * restore default behavior.
622
+ */
623
+ setRolePolicyOverride(override) {
624
+ this.rolePolicyOverride = override;
625
+ }
626
+ /**
627
+ * Set `ServerConfiguration.InApplicationSetup` in
628
+ * the address space.
629
+ *
630
+ * Indicates whether the server is in its initial
631
+ * application setup phase (e.g. awaiting GDS
632
+ * provisioning).
633
+ */
634
+ setInApplicationSetup(value) {
635
+ this.engine.setInApplicationSetup(value);
636
+ }
637
+ /**
638
+ * Read the current value of
639
+ * `ServerConfiguration.InApplicationSetup`.
640
+ */
641
+ getInApplicationSetup() {
642
+ return this.engine.getInApplicationSetup();
643
+ }
644
+ /**
645
+ * Collect additional hostnames for the self-signed certificate SAN.
646
+ *
647
+ * Merges hostnames from `alternateHostname` and parsed
648
+ * `advertisedEndpoints` URLs so the certificate covers all
649
+ * configured addresses.
650
+ *
651
+ * IP literals (v4/v6) are **excluded** — they are handled by
652
+ * `getConfiguredIPs()` and placed in the SAN `iPAddress` entries.
653
+ *
654
+ * @internal
655
+ */
656
+ getConfiguredHostnames() {
657
+ return this._collectAlternateValues().hostnames;
658
+ }
659
+ /**
660
+ * Collect additional IP addresses for the self-signed certificate SAN.
661
+ *
662
+ * Merges IP literals from `alternateHostname` and parsed
663
+ * `advertisedEndpoints` URLs so the certificate covers all
664
+ * configured IP addresses.
665
+ *
666
+ * @internal
667
+ */
668
+ getConfiguredIPs() {
669
+ return this._collectAlternateValues().ips;
670
+ }
671
+ /**
672
+ * Classify all values from `alternateHostname` and
673
+ * `advertisedEndpoints` into hostnames vs IP addresses using
674
+ * `isIPAddress()` (wraps `net.isIP()`).
675
+ */
676
+ _collectAlternateValues() {
677
+ const hostnames = [];
678
+ const ips = [];
679
+ // alternateHostname
680
+ const alt = this.options.alternateHostname;
681
+ if (alt) {
682
+ const altArray = Array.isArray(alt) ? alt : [alt];
683
+ for (const value of altArray) {
684
+ if ((0, node_opcua_hostname_1.isIPAddress)(value)) {
685
+ ips.push(value);
686
+ }
687
+ else {
688
+ hostnames.push(value);
689
+ }
690
+ }
691
+ }
692
+ // advertisedEndpoints — normalize to AdvertisedEndpointConfig[]
693
+ const advList = (0, server_end_point_1.normalizeAdvertisedEndpoints)(this.options.advertisedEndpoints);
694
+ for (const config of advList) {
695
+ const { hostname } = (0, server_end_point_1.parseOpcTcpUrl)(config.url);
696
+ if ((0, node_opcua_hostname_1.isIPAddress)(hostname)) {
697
+ ips.push(hostname);
698
+ }
699
+ else {
700
+ hostnames.push(hostname);
701
+ }
702
+ }
703
+ return { hostnames, ips };
704
+ }
578
705
  static registry = new node_opcua_object_registry_1.ObjectRegistry();
579
706
  static fallbackSessionName = "Client didn't provide a meaningful sessionName ...";
580
707
  /**
@@ -658,7 +785,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
658
785
  // to check => this.serverInfo.applicationName = this.serverInfo.productName || buildInfo.productName;
659
786
  // note: applicationUri is handled in a special way
660
787
  this.engine = new server_engine_1.ServerEngine({
661
- applicationUri: () => this.serverInfo.applicationUri,
788
+ applicationUri: () => this.serverInfo.applicationUri || "",
662
789
  buildInfo,
663
790
  isAuditing: options.isAuditing,
664
791
  serverCapabilities: options.serverCapabilities,
@@ -691,6 +818,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
691
818
  host: options.host,
692
819
  allowAnonymous: options.allowAnonymous,
693
820
  alternateHostname: options.alternateHostname,
821
+ advertisedEndpoints: options.advertisedEndpoints,
694
822
  disableDiscovery: options.disableDiscovery,
695
823
  securityModes: options.securityModes,
696
824
  securityPolicies: options.securityPolicies
@@ -728,6 +856,10 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
728
856
  .then(() => {
729
857
  OPCUAServer.registry.register(this);
730
858
  this.engine.initialize(this.options, () => {
859
+ if (!this.engine.addressSpace) {
860
+ done(new Error("no addressSpace"));
861
+ return;
862
+ }
731
863
  (0, user_manager_ua_1.bindRoleSet)(this.userManager, this.engine.addressSpace);
732
864
  setImmediate(() => {
733
865
  this.emit("post_initialize");
@@ -740,34 +872,33 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
740
872
  });
741
873
  }
742
874
  start(...args) {
743
- const done = args[0];
744
- const tasks = [];
745
- tasks.push((0, util_1.callbackify)(node_opcua_hostname_1.extractFullyQualifiedDomainName));
875
+ const callback = args[0];
876
+ if (callback) {
877
+ return super.start(callback);
878
+ }
879
+ return super.start();
880
+ }
881
+ /**
882
+ * Initiate the server by starting all its endpoints
883
+ * @private
884
+ */
885
+ async startAsync() {
886
+ await (0, node_opcua_hostname_1.extractFullyQualifiedDomainName)();
746
887
  if (!this.initialized) {
747
- tasks.push((callback) => {
748
- this.initialize(callback);
749
- });
888
+ await this.initialize();
750
889
  }
751
- tasks.push((callback) => {
752
- super.start((err) => {
753
- if (err) {
754
- this.shutdown((/*err2*/ err2) => {
755
- callback(err);
756
- });
757
- }
758
- else {
759
- // we start the registration process asynchronously
760
- // as we want to make server immediately available
761
- this.registerServerManager.start().then(() => {
762
- /* empty */
763
- }).catch((err) => {
764
- /* empty */
765
- });
766
- setImmediate(callback);
767
- }
768
- });
890
+ try {
891
+ await super.startAsync();
892
+ }
893
+ catch (err) {
894
+ await this.shutdown();
895
+ throw err;
896
+ }
897
+ // we start the registration process asynchronously
898
+ // as we want to make server immediately available
899
+ this.registerServerManager?.start().catch(() => {
900
+ /* empty */
769
901
  });
770
- async_1.default.series(tasks, done);
771
902
  }
772
903
  shutdown(...args) {
773
904
  const timeout = args.length === 1 ? OPCUAServer.defaultShutdownTimeout : args[0];
@@ -788,21 +919,27 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
788
919
  this.engine.setServerState(node_opcua_common_1.ServerState.Shutdown);
789
920
  const shutdownTime = new Date(Date.now() + timeout);
790
921
  this.engine.setShutdownTime(shutdownTime);
791
- debugLog("OPCUAServer is now un-registering itself from the discovery server " + this.buildInfo);
792
- this.registerServerManager.stop()
922
+ debugLog("OPCUAServer is now un-registering itself from the discovery server ", this.buildInfo);
923
+ if (!this.registerServerManager) {
924
+ callback(new Error("invalid register server manager"));
925
+ return;
926
+ }
927
+ this.registerServerManager
928
+ .stop()
793
929
  .then(() => {
794
930
  debugLog("OPCUAServer unregistered from discovery server successfully");
795
931
  })
796
932
  .catch((err) => {
797
933
  debugLog("OPCUAServer unregistered from discovery server with err: ", err.message);
798
- }).finally(() => {
934
+ })
935
+ .finally(() => {
799
936
  setTimeout(async () => {
800
937
  await this.engine.shutdown();
801
938
  debugLog("OPCUAServer#shutdown: started");
802
939
  base_server_1.OPCUABaseServer.prototype.shutdown.call(this, (err1) => {
803
940
  debugLog("OPCUAServer#shutdown: completed");
804
941
  this.dispose();
805
- callback(err1);
942
+ callback(err1 || undefined);
806
943
  });
807
944
  }, timeout);
808
945
  });
@@ -839,14 +976,14 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
839
976
  if (typeof eventType === "string") {
840
977
  eventTypeNode = this.engine.addressSpace.findEventType(eventType);
841
978
  if (eventTypeNode) {
842
- return server.raiseEvent(eventTypeNode, options);
979
+ server.raiseEvent(eventTypeNode, options);
843
980
  }
844
981
  else {
845
982
  console.warn(" cannot find event type ", eventType);
846
983
  }
847
984
  }
848
985
  else {
849
- return server.raiseEvent(eventTypeNode, options);
986
+ server.raiseEvent(eventTypeNode, options);
850
987
  }
851
988
  }
852
989
  /**
@@ -890,30 +1027,34 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
890
1027
  const result = (0, node_opcua_secure_channel_1.verifySignature)(serverCertificate, session.nonce, clientSignature, clientCertificate, securityPolicy);
891
1028
  return result;
892
1029
  }
893
- isValidUserNameIdentityToken(channel, session, userTokenPolicy, userIdentityToken, userTokenSignature, callback) {
1030
+ isValidUserNameIdentityToken(channel, _session, userTokenPolicy, userIdentityToken, _userTokenSignature, callback) {
894
1031
  (0, node_opcua_assert_1.assert)(userIdentityToken instanceof node_opcua_service_session_1.UserNameIdentityToken);
895
1032
  const securityPolicy = adjustSecurityPolicy(channel, userTokenPolicy.securityPolicyUri);
896
1033
  if (securityPolicy === node_opcua_secure_channel_1.SecurityPolicy.None) {
897
- return callback(null, node_opcua_status_code_1.StatusCodes.Good);
1034
+ callback(null, node_opcua_status_code_1.StatusCodes.Good);
1035
+ return;
898
1036
  }
899
1037
  const cryptoFactory = (0, node_opcua_secure_channel_1.getCryptoFactory)(securityPolicy);
900
1038
  /* c8 ignore next */
901
1039
  if (!cryptoFactory) {
902
- return callback(null, node_opcua_status_code_1.StatusCodes.BadSecurityPolicyRejected);
1040
+ callback(null, node_opcua_status_code_1.StatusCodes.BadSecurityPolicyRejected);
1041
+ return;
903
1042
  }
904
1043
  /* c8 ignore next */
905
1044
  if (userIdentityToken.encryptionAlgorithm !== cryptoFactory.asymmetricEncryptionAlgorithm) {
906
1045
  errorLog("invalid encryptionAlgorithm");
907
1046
  errorLog("userTokenPolicy", userTokenPolicy.toString());
908
1047
  errorLog("userTokenPolicy", userIdentityToken.toString());
909
- return callback(null, node_opcua_status_code_1.StatusCodes.BadIdentityTokenInvalid);
1048
+ callback(null, node_opcua_status_code_1.StatusCodes.BadIdentityTokenInvalid);
1049
+ return;
910
1050
  }
911
1051
  const userName = userIdentityToken.userName;
912
1052
  const password = userIdentityToken.password;
913
1053
  if (!userName || !password) {
914
- return callback(null, node_opcua_status_code_1.StatusCodes.BadIdentityTokenInvalid);
1054
+ callback(null, node_opcua_status_code_1.StatusCodes.BadIdentityTokenInvalid);
1055
+ return;
915
1056
  }
916
- return callback(null, node_opcua_status_code_1.StatusCodes.Good);
1057
+ callback(null, node_opcua_status_code_1.StatusCodes.Good);
917
1058
  }
918
1059
  isValidX509IdentityToken(channel, session, userTokenPolicy, userIdentityToken, userTokenSignature, callback) {
919
1060
  (0, node_opcua_assert_1.assert)(userIdentityToken instanceof node_opcua_service_session_1.X509IdentityToken);
@@ -922,28 +1063,33 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
922
1063
  const cryptoFactory = (0, node_opcua_secure_channel_1.getCryptoFactory)(securityPolicy);
923
1064
  /* c8 ignore next */
924
1065
  if (!cryptoFactory) {
925
- return callback(null, node_opcua_status_code_1.StatusCodes.BadSecurityPolicyRejected);
1066
+ callback(null, node_opcua_status_code_1.StatusCodes.BadSecurityPolicyRejected);
1067
+ return;
926
1068
  }
927
1069
  if (!userTokenSignature || !userTokenSignature.signature) {
928
1070
  this.raiseEvent("AuditCreateSessionEventType", {});
929
- return callback(null, node_opcua_status_code_1.StatusCodes.BadUserSignatureInvalid);
1071
+ callback(null, node_opcua_status_code_1.StatusCodes.BadUserSignatureInvalid);
1072
+ return;
930
1073
  }
931
1074
  if (userIdentityToken.policyId !== userTokenPolicy.policyId) {
932
1075
  errorLog("invalid encryptionAlgorithm");
933
1076
  errorLog("userTokenPolicy", userTokenPolicy.toString());
934
1077
  errorLog("userTokenPolicy", userIdentityToken.toString());
935
- return callback(null, node_opcua_status_code_1.StatusCodes.BadSecurityPolicyRejected);
1078
+ callback(null, node_opcua_status_code_1.StatusCodes.BadSecurityPolicyRejected);
1079
+ return;
936
1080
  }
937
1081
  const certificate = userIdentityToken.certificateData; /* as Certificate*/
938
1082
  const nonce = session.nonce;
1083
+ if (!nonce || nonce.length === 0) {
1084
+ callback(null, node_opcua_status_code_1.StatusCodes.BadNonceInvalid);
1085
+ return;
1086
+ }
939
1087
  const serverCertificate = this.getCertificate();
940
- (0, node_opcua_assert_1.assert)(serverCertificate instanceof Buffer);
941
- (0, node_opcua_assert_1.assert)(certificate instanceof Buffer, "expecting certificate to be a Buffer");
942
- (0, node_opcua_assert_1.assert)(nonce instanceof Buffer, "expecting nonce to be a Buffer");
943
1088
  (0, node_opcua_assert_1.assert)(userTokenSignature.signature instanceof Buffer, "expecting userTokenSignature to be a Buffer");
944
1089
  // verify proof of possession by checking certificate signature & server nonce correctness
945
1090
  if (!(0, node_opcua_secure_channel_1.verifySignature)(serverCertificate, nonce, userTokenSignature, certificate, securityPolicy)) {
946
- return callback(null, node_opcua_status_code_1.StatusCodes.BadUserSignatureInvalid);
1091
+ callback(null, node_opcua_status_code_1.StatusCodes.BadUserSignatureInvalid);
1092
+ return;
947
1093
  }
948
1094
  // verify if certificate is Valid
949
1095
  this.userCertificateManager.checkCertificate(certificate, (err, certificateStatus) => {
@@ -957,31 +1103,55 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
957
1103
  break;
958
1104
  case node_opcua_status_code_1.StatusCodes.BadCertificateUntrusted:
959
1105
  this.raiseEvent("AuditCertificateUntrustedEventType", {
960
- certificate: { dataType: node_opcua_variant_1.DataType.ByteString, value: certificate },
961
- sourceName: { dataType: node_opcua_variant_1.DataType.String, value: "Security/Certificate" }
1106
+ certificate: {
1107
+ dataType: node_opcua_variant_1.DataType.ByteString,
1108
+ value: certificate
1109
+ },
1110
+ sourceName: {
1111
+ dataType: node_opcua_variant_1.DataType.String,
1112
+ value: "Security/Certificate"
1113
+ }
962
1114
  });
963
1115
  break;
964
1116
  case node_opcua_status_code_1.StatusCodes.BadCertificateTimeInvalid:
965
1117
  case node_opcua_status_code_1.StatusCodes.BadCertificateIssuerTimeInvalid:
966
1118
  this.raiseEvent("AuditCertificateExpiredEventType", {
967
- certificate: { dataType: node_opcua_variant_1.DataType.ByteString, value: certificate },
968
- sourceName: { dataType: node_opcua_variant_1.DataType.String, value: "Security/Certificate" }
1119
+ certificate: {
1120
+ dataType: node_opcua_variant_1.DataType.ByteString,
1121
+ value: certificate
1122
+ },
1123
+ sourceName: {
1124
+ dataType: node_opcua_variant_1.DataType.String,
1125
+ value: "Security/Certificate"
1126
+ }
969
1127
  });
970
1128
  break;
971
1129
  case node_opcua_status_code_1.StatusCodes.BadCertificateRevoked:
972
1130
  case node_opcua_status_code_1.StatusCodes.BadCertificateRevocationUnknown:
973
1131
  case node_opcua_status_code_1.StatusCodes.BadCertificateIssuerRevocationUnknown:
974
1132
  this.raiseEvent("AuditCertificateRevokedEventType", {
975
- certificate: { dataType: node_opcua_variant_1.DataType.ByteString, value: certificate },
976
- sourceName: { dataType: node_opcua_variant_1.DataType.String, value: "Security/Certificate" }
1133
+ certificate: {
1134
+ dataType: node_opcua_variant_1.DataType.ByteString,
1135
+ value: certificate
1136
+ },
1137
+ sourceName: {
1138
+ dataType: node_opcua_variant_1.DataType.String,
1139
+ value: "Security/Certificate"
1140
+ }
977
1141
  });
978
1142
  break;
979
1143
  case node_opcua_status_code_1.StatusCodes.BadCertificateIssuerUseNotAllowed:
980
1144
  case node_opcua_status_code_1.StatusCodes.BadCertificateUseNotAllowed:
981
1145
  case node_opcua_status_code_1.StatusCodes.BadSecurityChecksFailed:
982
1146
  this.raiseEvent("AuditCertificateMismatchEventType", {
983
- certificate: { dataType: node_opcua_variant_1.DataType.ByteString, value: certificate },
984
- sourceName: { dataType: node_opcua_variant_1.DataType.String, value: "Security/Certificate" }
1147
+ certificate: {
1148
+ dataType: node_opcua_variant_1.DataType.ByteString,
1149
+ value: certificate
1150
+ },
1151
+ sourceName: {
1152
+ dataType: node_opcua_variant_1.DataType.String,
1153
+ value: "Security/Certificate"
1154
+ }
985
1155
  });
986
1156
  break;
987
1157
  }
@@ -1020,6 +1190,10 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1020
1190
  // assert(this.isValidUserNameIdentityToken(channel, session, userTokenPolicy, userIdentityToken));
1021
1191
  const securityPolicy = adjustSecurityPolicy(channel, userTokenPolicy.securityPolicyUri);
1022
1192
  const userName = userIdentityToken.userName;
1193
+ if (!userName) {
1194
+ callback(new Error(" expecting a no null username"));
1195
+ return;
1196
+ }
1023
1197
  let password = userIdentityToken.password;
1024
1198
  // decrypt password if necessary
1025
1199
  if (securityPolicy === node_opcua_secure_channel_1.SecurityPolicy.None) {
@@ -1029,16 +1203,20 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1029
1203
  else {
1030
1204
  const serverPrivateKey = this.getPrivateKey();
1031
1205
  const serverNonce = session.nonce;
1032
- (0, node_opcua_assert_1.assert)(serverNonce instanceof Buffer);
1206
+ if (!serverNonce) {
1207
+ callback(new Error(" expecting a no null nonce"));
1208
+ return;
1209
+ }
1033
1210
  const cryptoFactory = (0, node_opcua_secure_channel_1.getCryptoFactory)(securityPolicy);
1034
1211
  /* c8 ignore next */
1035
1212
  if (!cryptoFactory) {
1036
- return callback(new Error(" Unsupported security Policy"));
1213
+ callback(new Error(" Unsupported security Policy"));
1214
+ return;
1037
1215
  }
1038
1216
  const buff = cryptoFactory.asymmetricDecrypt(password, serverPrivateKey);
1039
1217
  // server certificate may be invalid and asymmetricDecrypt may fail
1040
1218
  if (!buff || buff.length < 4) {
1041
- async_1.default.setImmediate(() => callback(null, false));
1219
+ setImmediate(() => callback(null, false));
1042
1220
  return;
1043
1221
  }
1044
1222
  const length = buff.readUInt32LE(0) - serverNonce.length;
@@ -1062,16 +1240,19 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1062
1240
  const userTokenPolicy = findUserTokenByPolicy(endpointDescription, userTokenType, userIdentityToken.policyId);
1063
1241
  if (!userTokenPolicy) {
1064
1242
  // cannot find token with this policyId
1065
- return callback(null, node_opcua_status_code_1.StatusCodes.BadIdentityTokenInvalid);
1243
+ callback(null, node_opcua_status_code_1.StatusCodes.BadIdentityTokenInvalid);
1244
+ return;
1066
1245
  }
1067
1246
  //
1068
1247
  if (userIdentityToken instanceof node_opcua_service_session_1.UserNameIdentityToken) {
1069
- return this.isValidUserNameIdentityToken(channel, session, userTokenPolicy, userIdentityToken, userTokenSignature, callback);
1248
+ this.isValidUserNameIdentityToken(channel, session, userTokenPolicy, userIdentityToken, userTokenSignature, callback);
1249
+ return;
1070
1250
  }
1071
1251
  if (userIdentityToken instanceof node_opcua_service_session_1.X509IdentityToken) {
1072
- return this.isValidX509IdentityToken(channel, session, userTokenPolicy, userIdentityToken, userTokenSignature, callback);
1252
+ this.isValidX509IdentityToken(channel, session, userTokenPolicy, userIdentityToken, userTokenSignature, callback);
1253
+ return;
1073
1254
  }
1074
- return callback(null, node_opcua_status_code_1.StatusCodes.Good);
1255
+ callback(null, node_opcua_status_code_1.StatusCodes.Good);
1075
1256
  }
1076
1257
  /**
1077
1258
  *
@@ -1089,16 +1270,18 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1089
1270
  const userTokenPolicy = findUserTokenByPolicy(session.getEndpointDescription(), userTokenType, userIdentityToken.policyId);
1090
1271
  /** c8 ignore next */
1091
1272
  if (!userTokenPolicy) {
1092
- return callback(null, false);
1273
+ callback(null, false);
1274
+ return;
1093
1275
  }
1094
1276
  // find if a userToken exists
1095
1277
  if (userIdentityToken instanceof node_opcua_service_session_1.UserNameIdentityToken) {
1096
- return this.userNameIdentityTokenAuthenticateUser(channel, session, userTokenPolicy, userIdentityToken, callback);
1278
+ this.userNameIdentityTokenAuthenticateUser(channel, session, userTokenPolicy, userIdentityToken, callback);
1279
+ return;
1097
1280
  }
1098
- async_1.default.setImmediate(callback.bind(null, null, true));
1281
+ setImmediate(callback.bind(null, null, true));
1099
1282
  }
1100
1283
  makeServerNonce() {
1101
- return (0, crypto_1.randomBytes)(32);
1284
+ return (0, node_crypto_1.randomBytes)(32);
1102
1285
  }
1103
1286
  // session services
1104
1287
  // eslint-disable-next-line max-statements
@@ -1146,12 +1329,12 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1146
1329
  // its application instance Certificate in the response.
1147
1330
  if (!request.clientNonce || request.clientNonce.length < 32) {
1148
1331
  if (channel.securityMode !== node_opcua_secure_channel_1.MessageSecurityMode.None) {
1149
- errorLog(chalk_1.default.red("SERVER with secure connection: Missing or invalid client Nonce "), request.clientNonce && request.clientNonce.toString("hex"));
1332
+ errorLog(chalk_1.default.red("SERVER with secure connection: Missing or invalid client Nonce "), request.clientNonce?.toString("hex"));
1150
1333
  return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadNonceInvalid);
1151
1334
  }
1152
1335
  }
1153
1336
  if ((0, node_opcua_secure_channel_1.nonceAlreadyBeenUsed)(request.clientNonce)) {
1154
- errorLog(chalk_1.default.red("SERVER with secure connection: Nonce has already been used"), request.clientNonce && request.clientNonce.toString("hex"));
1337
+ errorLog(chalk_1.default.red("SERVER with secure connection: Nonce has already been used"), request.clientNonce?.toString("hex"));
1155
1338
  return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadNonceInvalid);
1156
1339
  }
1157
1340
  // check application spoofing
@@ -1193,7 +1376,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1193
1376
  const serverCertificateChain = this.getCertificateChain();
1194
1377
  const hasEncryption = true;
1195
1378
  // If the securityPolicyUri is None and none of the UserTokenPolicies requires encryption
1196
- if (session.channel.securityMode === node_opcua_secure_channel_1.MessageSecurityMode.None) {
1379
+ if (session.channel?.securityMode === node_opcua_secure_channel_1.MessageSecurityMode.None) {
1197
1380
  // ToDo: Check that none of our insecure endpoint has a a UserTokenPolicy that require encryption
1198
1381
  // and set hasEncryption = false under this condition
1199
1382
  }
@@ -1224,7 +1407,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1224
1407
  // securityPolicyUri, userIdentityTokens, transportProfileUri and securityLevel with all
1225
1408
  // other parameters set to null. Only the recommended parameters shall be verified by
1226
1409
  // the client.
1227
- serverEndpoints: _serverEndpointsForCreateSessionResponse(this, session.endpoint.endpointUrl, request.serverUri),
1410
+ serverEndpoints: _serverEndpointsForCreateSessionResponse(this, session.endpoint?.endpointUrl || "", request.serverUri),
1228
1411
  // This parameter is deprecated and the array shall be empty.
1229
1412
  serverSoftwareCertificates: null,
1230
1413
  // This is a signature generated with the private key associated with the
@@ -1245,7 +1428,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1245
1428
  (0, node_opcua_assert_1.assert)(typeof reason === "string");
1246
1429
  if (this.isAuditing) {
1247
1430
  (0, node_opcua_assert_1.assert)(reason === "Timeout" || reason === "Terminated" || reason === "CloseSession" || reason === "Forcing");
1248
- const sourceName = "Session/" + reason;
1431
+ const sourceName = `Session/${reason}`;
1249
1432
  this.raiseEvent("AuditSessionEventType", {
1250
1433
  /* part 5 - 6.4.3 AuditEventType */
1251
1434
  actionTimeStamp: { dataType: "DateTime", value: new Date() },
@@ -1283,15 +1466,24 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1283
1466
  // identifier in all AuditEvents related to the Session Service Set (AuditCreateSessionEventType,
1284
1467
  // AuditActivateSessionEventType and their subtypes) and the SecureChannel Service Set
1285
1468
  // (AuditChannelEventType and its subtypes
1286
- secureChannelId: { dataType: "String", value: session.channel.channelId.toString() },
1469
+ secureChannelId: {
1470
+ dataType: "String",
1471
+ value: session.channel?.channelId?.toString() ?? ""
1472
+ },
1287
1473
  // Duration
1288
- revisedSessionTimeout: { dataType: "Duration", value: session.sessionTimeout },
1474
+ revisedSessionTimeout: {
1475
+ dataType: "Duration",
1476
+ value: session.sessionTimeout
1477
+ },
1289
1478
  // clientCertificate
1290
- clientCertificate: { dataType: "ByteString", value: session.channel.clientCertificate },
1479
+ clientCertificate: {
1480
+ dataType: "ByteString",
1481
+ value: session.channel?.clientCertificate ?? null
1482
+ },
1291
1483
  // clientCertificateThumbprint
1292
1484
  clientCertificateThumbprint: {
1293
1485
  dataType: "String",
1294
- value: thumbprint(session.channel.clientCertificate)
1486
+ value: thumbprint(session.channel?.clientCertificate)
1295
1487
  }
1296
1488
  });
1297
1489
  }
@@ -1329,16 +1521,18 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1329
1521
  server.engine.incrementRejectedSessionCount();
1330
1522
  server.engine.incrementSecurityRejectedSessionCount();
1331
1523
  }
1332
- const response1 = new node_opcua_service_session_1.ActivateSessionResponse({ responseHeader: { serviceResult: statusCode } });
1524
+ const response1 = new node_opcua_service_session_1.ActivateSessionResponse({
1525
+ responseHeader: { serviceResult: statusCode }
1526
+ });
1333
1527
  channel.send_response("MSG", response1, message);
1334
1528
  }
1335
- let response;
1336
1529
  /* c8 ignore next */
1337
1530
  if (!session) {
1338
1531
  // this may happen when the server has been restarted and a client tries to reconnect, thinking
1339
1532
  // that the previous session may still be active
1340
1533
  debugLog(chalk_1.default.yellow.bold(" Bad Session in _on_ActivateSessionRequest"), authenticationToken.toString());
1341
- return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionIdInvalid);
1534
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionIdInvalid);
1535
+ return;
1342
1536
  }
1343
1537
  // tslint:disable-next-line: no-unused-expression
1344
1538
  session.keepAlive ? session.keepAlive() : void 0;
@@ -1350,8 +1544,9 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1350
1544
  if (!channel_has_session(channel, session)) {
1351
1545
  // it looks like session activation is being using a channel that is not the
1352
1546
  // one that have been used to create the session
1353
- errorLog(" channel.sessionTokens === " + Object.keys(channel.sessionTokens).join(" "));
1354
- return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionNotActivated);
1547
+ errorLog(` channel.sessionTokens === ${Object.keys(channel.sessionTokens).join(" ")}`);
1548
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionNotActivated);
1549
+ return;
1355
1550
  }
1356
1551
  }
1357
1552
  // OpcUA 1.02 part 3 $5.6.3.1 ActiveSession Set page 29
@@ -1359,39 +1554,49 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1359
1554
  // case then the Server shall verify that the Certificate the Client used to create the new
1360
1555
  // SecureChannel is the same as the Certificate used to create the original SecureChannel.
1361
1556
  if (session.status === "active") {
1362
- if (session.channel.channelId !== channel.channelId) {
1363
- warningLog(" Session ", session.sessionName, " is being transferred from channel", chalk_1.default.cyan(session.channel.channelId.toString()), " to channel ", chalk_1.default.cyan(channel.channelId.toString()));
1557
+ if (session.channel?.channelId !== channel.channelId) {
1558
+ warningLog(" Session ", session.sessionName, " is being transferred from channel", chalk_1.default.cyan(session.channel?.channelId?.toString()), " to channel ", chalk_1.default.cyan(channel.channelId?.toString()));
1364
1559
  // session is being reassigned to a new Channel,
1365
1560
  // we shall verify that the certificate used to create the Session is the same as the current
1366
1561
  // channel certificate.
1367
- const old_channel_cert_thumbprint = thumbprint(session.channel.clientCertificate);
1562
+ const old_channel_cert_thumbprint = thumbprint(session.channel?.clientCertificate);
1368
1563
  const new_channel_cert_thumbprint = thumbprint(channel.clientCertificate);
1369
1564
  if (old_channel_cert_thumbprint !== new_channel_cert_thumbprint) {
1370
- return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadNoValidCertificates); // not sure about this code !
1565
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadNoValidCertificates); // not sure about this code !
1566
+ return;
1371
1567
  }
1372
1568
  // ... In addition the Server shall verify that the Client supplied a UserIdentityToken that is
1373
1569
  // identical to the token currently associated with the Session reassign session to new channel.
1374
1570
  if (!sameIdentityToken(session.userIdentityToken, request.userIdentityToken)) {
1375
- return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadIdentityChangeNotSupported); // not sure about this code !
1571
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadIdentityChangeNotSupported); // not sure about this code !
1572
+ return;
1376
1573
  }
1377
1574
  }
1378
1575
  }
1379
1576
  else if (session.status === "screwed") {
1380
1577
  // session has been used before being activated => this should be detected and session should be dismissed.
1381
- return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionClosed);
1578
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionClosed);
1579
+ return;
1382
1580
  }
1383
1581
  else if (session.status === "closed") {
1384
1582
  warningLog(chalk_1.default.yellow.bold(" Bad Session Closed in _on_ActivateSessionRequest"), authenticationToken.toString());
1385
- return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionClosed);
1583
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionClosed);
1584
+ return;
1386
1585
  }
1387
1586
  // verify clientSignature provided by the client
1388
1587
  if (!this.verifyClientSignature(session, channel, request.clientSignature)) {
1389
- return rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadApplicationSignatureInvalid);
1588
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadApplicationSignatureInvalid);
1589
+ return;
1590
+ }
1591
+ const endpoint = session.endpoint;
1592
+ if (!endpoint) {
1593
+ rejectConnection(this, node_opcua_status_code_1.StatusCodes.BadSessionIdInvalid);
1594
+ return;
1390
1595
  }
1391
1596
  // userIdentityToken may be missing , assume anonymous access then
1392
- request.userIdentityToken = request.userIdentityToken || createAnonymousIdentityToken(session.endpoint);
1597
+ request.userIdentityToken = request.userIdentityToken || createAnonymousIdentityToken(endpoint);
1393
1598
  // check request.userIdentityToken is correct ( expected type and correctly formed)
1394
- this.isValidUserIdentityToken(channel, session, request.userIdentityToken, request.userTokenSignature, session.endpoint, (err, statusCode) => {
1599
+ this.isValidUserIdentityToken(channel, session, request.userIdentityToken, request.userTokenSignature, endpoint, (_err, statusCode) => {
1395
1600
  if (!statusCode || statusCode.isNotGood()) {
1396
1601
  /* c8 ignore next */
1397
1602
  if (!(statusCode && statusCode instanceof node_opcua_status_code_1.StatusCode)) {
@@ -1418,7 +1623,9 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1418
1623
  // serverNonce each time the ActivateSession Service is called.
1419
1624
  session.nonce = this.makeServerNonce();
1420
1625
  session.status = "active";
1421
- response = new node_opcua_service_session_1.ActivateSessionResponse({ serverNonce: session.nonce });
1626
+ const response = new node_opcua_service_session_1.ActivateSessionResponse({
1627
+ serverNonce: session.nonce
1628
+ });
1422
1629
  channel.send_response("MSG", response, message);
1423
1630
  // send OPCUA Event Notification
1424
1631
  // see part 5 : 6.4.3 AuditEventType
@@ -1451,7 +1658,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1451
1658
  // ActivateSession completes the session transfer are expected
1452
1659
  // transient occurrences, not errors worth logging.
1453
1660
  if (request.constructor.name !== "PublishRequest") {
1454
- errorLog(chalk_1.default.red.bgWhite("ERROR: channel.channelId !== session.channelId on processing request " + request.constructor.name), channel.channelId, session.channelId);
1661
+ errorLog(chalk_1.default.red.bgWhite(`ERROR: channel.channelId !== session.channelId on processing request ${request.constructor.name}`), channel.channelId, session.channelId);
1455
1662
  }
1456
1663
  }
1457
1664
  message.session_statusCode = node_opcua_status_code_1.StatusCodes.BadSecureChannelIdInvalid;
@@ -1493,7 +1700,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1493
1700
  catch (err) {
1494
1701
  warningLog(err);
1495
1702
  // c8 ignore next
1496
- if (util_1.types.isNativeError(err)) {
1703
+ if (node_util_1.types.isNativeError(err)) {
1497
1704
  // c8 ignore next
1498
1705
  errorLog("Internal error in issuing response\nplease contact support@sterfive.com", message.request.toString(), "\n", response1.toString());
1499
1706
  }
@@ -1507,12 +1714,14 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1507
1714
  }
1508
1715
  return g_sendError(channel, message, ResponseClass, statusCode);
1509
1716
  }
1510
- let response;
1511
1717
  /* c8 ignore next */
1512
1718
  if (!message.session || message.session_statusCode !== node_opcua_status_code_1.StatusCodes.Good) {
1513
- const errMessage = "=>" + message.session_statusCode?.toString();
1514
- response = new node_opcua_types_1.ServiceFault({ responseHeader: { serviceResult: message.session_statusCode } });
1515
- debugLog(chalk_1.default.red.bold(errMessage), chalk_1.default.yellow(message.session_statusCode.toString()), response.constructor.name);
1719
+ const sessionStatusCode = message.session_statusCode ?? node_opcua_status_code_1.StatusCodes.BadInternalError;
1720
+ const errMessage = `=>${sessionStatusCode.toString()}`;
1721
+ const response = new node_opcua_types_1.ServiceFault({
1722
+ responseHeader: { serviceResult: sessionStatusCode }
1723
+ });
1724
+ debugLog(chalk_1.default.red.bold(errMessage), chalk_1.default.yellow(sessionStatusCode.toString()), response.constructor.name);
1516
1725
  return sendResponse(response);
1517
1726
  }
1518
1727
  (0, node_opcua_assert_1.assert)(message.session_statusCode.isGood());
@@ -1550,7 +1759,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1550
1759
  async _apply_on_Subscription(ResponseClass, message, channel, actionToPerform) {
1551
1760
  (0, node_opcua_assert_1.assert)(typeof actionToPerform === "function");
1552
1761
  const request = message.request;
1553
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(request, "subscriptionId"));
1762
+ (0, node_opcua_assert_1.assert)(Object.hasOwn(request, "subscriptionId"));
1554
1763
  this._apply_on_SessionObject(ResponseClass, message, channel, async (session, sendResponse, sendError) => {
1555
1764
  const subscription = session.getSubscription(request.subscriptionId);
1556
1765
  if (!subscription) {
@@ -1563,7 +1772,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1563
1772
  _apply_on_SubscriptionIds(ResponseClass, message, channel, actionToPerform) {
1564
1773
  (0, node_opcua_assert_1.assert)(typeof actionToPerform === "function");
1565
1774
  const request = message.request;
1566
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(request, "subscriptionIds"));
1775
+ (0, node_opcua_assert_1.assert)(Object.hasOwn(request, "subscriptionIds"));
1567
1776
  this._apply_on_SessionObject(ResponseClass, message, channel, async (session, sendResponse, sendError) => {
1568
1777
  const subscriptionIds = request.subscriptionIds;
1569
1778
  if (!request.subscriptionIds || request.subscriptionIds.length === 0) {
@@ -1605,7 +1814,10 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1605
1814
  if (session) {
1606
1815
  const subscriptions = session.publishEngine.subscriptions;
1607
1816
  for (const subscription of subscriptions) {
1608
- await subscription.applyOnMonitoredItem(this.options.onDeleteMonitoredItem.bind(null, subscription));
1817
+ const onDeleteFn = this.options.onDeleteMonitoredItem;
1818
+ await subscription.applyOnMonitoredItem(async (monitoredItem) => {
1819
+ await onDeleteFn(subscription, monitoredItem);
1820
+ });
1609
1821
  }
1610
1822
  }
1611
1823
  }
@@ -1619,7 +1831,6 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1619
1831
  _on_CloseSessionRequest(message, channel) {
1620
1832
  const request = message.request;
1621
1833
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_session_1.CloseSessionRequest);
1622
- let response;
1623
1834
  message.session_statusCode = node_opcua_status_code_1.StatusCodes.Good;
1624
1835
  function sendError(statusCode) {
1625
1836
  return g_sendError(channel, message, node_opcua_service_session_1.CloseSessionResponse, statusCode);
@@ -1632,20 +1843,21 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1632
1843
  // });
1633
1844
  const session = message.session;
1634
1845
  if (!session) {
1635
- return sendError(node_opcua_status_code_1.StatusCodes.BadSessionIdInvalid);
1846
+ sendError(node_opcua_status_code_1.StatusCodes.BadSessionIdInvalid);
1847
+ return;
1636
1848
  }
1637
1849
  // session has been created but not activated !
1638
- const wasNotActivated = session.status === "new";
1850
+ const _wasNotActivated = session.status === "new";
1639
1851
  (async () => {
1640
1852
  try {
1641
1853
  await this._closeSession(request.requestHeader.authenticationToken, request.deleteSubscriptions, "CloseSession");
1642
1854
  // if (false && wasNotActivated) {
1643
1855
  // return sendError(StatusCodes.BadSessionNotActivated);
1644
1856
  // }
1645
- response = new node_opcua_service_session_1.CloseSessionResponse({});
1857
+ const response = new node_opcua_service_session_1.CloseSessionResponse({});
1646
1858
  sendResponse(response);
1647
1859
  }
1648
- catch (err) {
1860
+ catch (_err) {
1649
1861
  sendError(node_opcua_status_code_1.StatusCodes.BadInternalError);
1650
1862
  }
1651
1863
  })();
@@ -1664,7 +1876,11 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1664
1876
  let response;
1665
1877
  // test view
1666
1878
  if (request.view && !request.view.viewId.isEmpty()) {
1667
- let theView = this.engine.addressSpace.findNode(request.view.viewId);
1879
+ const addressSpace = this.engine.addressSpace;
1880
+ if (!addressSpace) {
1881
+ return sendError(node_opcua_status_code_1.StatusCodes.BadInternalError);
1882
+ }
1883
+ let theView = addressSpace.findNode(request.view.viewId);
1668
1884
  if (theView && theView.nodeClass !== node_opcua_data_model_1.NodeClass.View) {
1669
1885
  // Error: theView is not a View
1670
1886
  diagnostic.localizedText = { text: "Expecting a view here" };
@@ -1686,9 +1902,9 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1686
1902
  const requestedMaxReferencesPerNode = Math.min(9876, request.requestedMaxReferencesPerNode);
1687
1903
  (0, node_opcua_assert_1.assert)(request.nodesToBrowse[0].schema.name === "BrowseDescription");
1688
1904
  const context = session.sessionContext;
1689
- const browseAll = (nodesToBrowse, callack) => {
1690
- const f = (0, util_1.callbackify)(this.engine.browseWithAutomaticExpansion).bind(this.engine);
1691
- f(request.nodesToBrowse, context, callack);
1905
+ const browseAll = (_nodesToBrowse, callack) => {
1906
+ const f = (0, node_util_1.callbackify)(this.engine.browseWithAutomaticExpansion).bind(this.engine);
1907
+ f(request.nodesToBrowse ?? [], context, callack);
1692
1908
  };
1693
1909
  // handle continuation point and requestedMaxReferencesPerNode
1694
1910
  const maxBrowseContinuationPoints = this.engine.serverCapabilities.maxBrowseContinuationPoints;
@@ -1698,7 +1914,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1698
1914
  continuationPointManager: session.continuationPointManager,
1699
1915
  requestedMaxReferencesPerNode,
1700
1916
  maxBrowseContinuationPoints
1701
- }, request.nodesToBrowse, (err, results) => {
1917
+ }, request.nodesToBrowse, (_err, results) => {
1702
1918
  if (!results) {
1703
1919
  return sendError(node_opcua_status_code_1.StatusCodes.BadInternalError);
1704
1920
  }
@@ -1763,17 +1979,17 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1763
1979
  nodeToRead.nodeId = session.resolveRegisteredNode(nodeToRead.nodeId);
1764
1980
  }
1765
1981
  // ask for a refresh of asynchronous variables
1766
- this.engine.refreshValues(request.nodesToRead, request.maxAge, (err) => {
1982
+ this.engine.refreshValues(request.nodesToRead, request.maxAge, (_err) => {
1767
1983
  this.engine.read(context, request).then((results) => {
1768
1984
  (0, node_opcua_assert_1.assert)(results[0].schema.name === "DataValue");
1769
- (0, node_opcua_assert_1.assert)(results.length === request.nodesToRead.length);
1985
+ (0, node_opcua_assert_1.assert)(results.length === request.nodesToRead?.length);
1770
1986
  const response = new node_opcua_service_read_1.ReadResponse({
1771
1987
  diagnosticInfos: undefined,
1772
1988
  results: undefined
1773
1989
  });
1774
1990
  // set it here for performance
1775
1991
  response.results = results;
1776
- (0, node_opcua_assert_1.assert)(response.diagnosticInfos.length === 0);
1992
+ (0, node_opcua_assert_1.assert)(response.diagnosticInfos?.length === 0);
1777
1993
  sendResponse(response);
1778
1994
  });
1779
1995
  });
@@ -1784,7 +2000,6 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1784
2000
  const request = message.request;
1785
2001
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_history_1.HistoryReadRequest);
1786
2002
  this._apply_on_SessionObject(node_opcua_service_history_1.HistoryReadResponse, message, channel, (session, sendResponse, sendError) => {
1787
- let response;
1788
2003
  const timestampsToReturn = request.timestampsToReturn;
1789
2004
  if (timestampsToReturn === node_opcua_service_read_1.TimestampsToReturn.Invalid) {
1790
2005
  return sendError(node_opcua_status_code_1.StatusCodes.BadTimestampsToReturnInvalid);
@@ -1819,15 +2034,15 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1819
2034
  .historyRead(context, request)
1820
2035
  .then((results) => {
1821
2036
  (0, node_opcua_assert_1.assert)(results[0].schema.name === "HistoryReadResult");
1822
- (0, node_opcua_assert_1.assert)(results.length === request.nodesToRead.length);
1823
- response = new node_opcua_service_history_1.HistoryReadResponse({
2037
+ (0, node_opcua_assert_1.assert)(results.length === request.nodesToRead?.length);
2038
+ const response = new node_opcua_service_history_1.HistoryReadResponse({
1824
2039
  diagnosticInfos: undefined,
1825
2040
  results
1826
2041
  });
1827
- (0, node_opcua_assert_1.assert)(response.diagnosticInfos.length === 0);
2042
+ (0, node_opcua_assert_1.assert)(response.diagnosticInfos?.length === 0);
1828
2043
  sendResponse(response);
1829
2044
  })
1830
- .catch((err) => {
2045
+ .catch((_err) => {
1831
2046
  return sendError(node_opcua_status_code_1.StatusCodes.BadHistoryOperationInvalid);
1832
2047
  });
1833
2048
  });
@@ -1852,7 +2067,6 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1852
2067
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_write_1.WriteRequest);
1853
2068
  (0, node_opcua_assert_1.assert)(!request.nodesToWrite || Array.isArray(request.nodesToWrite));
1854
2069
  this._apply_on_SessionObject(node_opcua_service_write_1.WriteResponse, message, channel, (session, sendResponse, sendError) => {
1855
- let response;
1856
2070
  if (!request.nodesToWrite || request.nodesToWrite.length === 0) {
1857
2071
  return sendError(node_opcua_status_code_1.StatusCodes.BadNothingToDo);
1858
2072
  }
@@ -1870,8 +2084,8 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1870
2084
  this.engine
1871
2085
  .write(context, request.nodesToWrite)
1872
2086
  .then((results) => {
1873
- (0, node_opcua_assert_1.assert)(results.length === request.nodesToWrite.length);
1874
- response = new node_opcua_service_write_1.WriteResponse({
2087
+ (0, node_opcua_assert_1.assert)(results?.length === request.nodesToWrite?.length);
2088
+ const response = new node_opcua_service_write_1.WriteResponse({
1875
2089
  diagnosticInfos: undefined,
1876
2090
  results
1877
2091
  });
@@ -1887,6 +2101,10 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1887
2101
  _on_CreateSubscriptionRequest(message, channel) {
1888
2102
  const engine = this.engine;
1889
2103
  const addressSpace = engine.addressSpace;
2104
+ if (!addressSpace) {
2105
+ g_sendError(channel, message, node_opcua_service_subscription_1.CreateSubscriptionResponse, node_opcua_status_code_1.StatusCodes.BadSessionClosed);
2106
+ return;
2107
+ }
1890
2108
  const request = message.request;
1891
2109
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.CreateSubscriptionRequest);
1892
2110
  this._apply_on_SessionObject(node_opcua_service_subscription_1.CreateSubscriptionResponse, message, channel, (session, sendResponse, sendError) => {
@@ -1947,9 +2165,13 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1947
2165
  _on_CreateMonitoredItemsRequest(message, channel) {
1948
2166
  const engine = this.engine;
1949
2167
  const addressSpace = engine.addressSpace;
2168
+ if (!addressSpace) {
2169
+ g_sendError(channel, message, node_opcua_service_subscription_1.CreateMonitoredItemsResponse, node_opcua_status_code_1.StatusCodes.BadSessionClosed);
2170
+ return;
2171
+ }
1950
2172
  const request = message.request;
1951
2173
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.CreateMonitoredItemsRequest);
1952
- this._apply_on_Subscription(node_opcua_service_subscription_1.CreateMonitoredItemsResponse, message, channel, async (session, subscription, sendResponse, sendError) => {
2174
+ this._apply_on_Subscription(node_opcua_service_subscription_1.CreateMonitoredItemsResponse, message, channel, async (_session, subscription, sendResponse, sendError) => {
1953
2175
  const timestampsToReturn = request.timestampsToReturn;
1954
2176
  if (timestampsToReturn === node_opcua_service_read_1.TimestampsToReturn.Invalid) {
1955
2177
  return sendError(node_opcua_status_code_1.StatusCodes.BadTimestampsToReturnInvalid);
@@ -1968,7 +2190,9 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1968
2190
  const resultsPromise = request.itemsToCreate.map(async (monitoredItemCreateRequest) => {
1969
2191
  const { monitoredItem, createResult } = subscription.preCreateMonitoredItem(addressSpace, timestampsToReturn, monitoredItemCreateRequest);
1970
2192
  if (monitoredItem) {
1971
- await options.onCreateMonitoredItem(subscription, monitoredItem);
2193
+ if (options.onCreateMonitoredItem) {
2194
+ await options.onCreateMonitoredItem(subscription, monitoredItem);
2195
+ }
1972
2196
  subscription.postCreateMonitoredItem(monitoredItem, monitoredItemCreateRequest, createResult);
1973
2197
  }
1974
2198
  return createResult;
@@ -1995,7 +2219,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
1995
2219
  _on_ModifySubscriptionRequest(message, channel) {
1996
2220
  const request = message.request;
1997
2221
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.ModifySubscriptionRequest);
1998
- this._apply_on_Subscription(node_opcua_service_subscription_1.ModifySubscriptionResponse, message, channel, async (session, subscription, sendResponse, sendError) => {
2222
+ this._apply_on_Subscription(node_opcua_service_subscription_1.ModifySubscriptionResponse, message, channel, async (_session, subscription, sendResponse, _sendError) => {
1999
2223
  subscription.modify(request);
2000
2224
  const response = new node_opcua_service_subscription_1.ModifySubscriptionResponse({
2001
2225
  revisedLifetimeCount: subscription.lifeTimeCount,
@@ -2008,7 +2232,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2008
2232
  _on_ModifyMonitoredItemsRequest(message, channel) {
2009
2233
  const request = message.request;
2010
2234
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.ModifyMonitoredItemsRequest);
2011
- this._apply_on_Subscription(node_opcua_service_subscription_1.ModifyMonitoredItemsResponse, message, channel, async (session, subscription, sendResponse, sendError) => {
2235
+ this._apply_on_Subscription(node_opcua_service_subscription_1.ModifyMonitoredItemsResponse, message, channel, async (_session, subscription, sendResponse, sendError) => {
2012
2236
  const timestampsToReturn = request.timestampsToReturn;
2013
2237
  if (timestampsToReturn === node_opcua_service_read_1.TimestampsToReturn.Invalid) {
2014
2238
  return sendError(node_opcua_status_code_1.StatusCodes.BadTimestampsToReturnInvalid);
@@ -2027,7 +2251,9 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2027
2251
  const monitoredItemId = item.monitoredItemId;
2028
2252
  const monitoredItem = subscription.getMonitoredItem(monitoredItemId);
2029
2253
  if (!monitoredItem) {
2030
- return new node_opcua_service_subscription_1.MonitoredItemModifyResult({ statusCode: node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid });
2254
+ return new node_opcua_service_subscription_1.MonitoredItemModifyResult({
2255
+ statusCode: node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid
2256
+ });
2031
2257
  }
2032
2258
  // adjust samplingInterval if === -1
2033
2259
  if (item.requestedParameters.samplingInterval === -1) {
@@ -2045,7 +2271,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2045
2271
  _on_PublishRequest(message, channel) {
2046
2272
  const request = message.request;
2047
2273
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.PublishRequest);
2048
- this._apply_on_SessionObject(node_opcua_service_subscription_1.PublishResponse, message, channel, (session, sendResponse, sendError) => {
2274
+ this._apply_on_SessionObject(node_opcua_service_subscription_1.PublishResponse, message, channel, (session, sendResponse, _sendError) => {
2049
2275
  (0, node_opcua_assert_1.assert)(session);
2050
2276
  (0, node_opcua_assert_1.assert)(session.publishEngine); // server.publishEngine doesn't exists, OPCUAServer has probably shut down already
2051
2277
  session.publishEngine._on_PublishRequest(request, (_request1, response) => {
@@ -2057,14 +2283,14 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2057
2283
  const request = message.request;
2058
2284
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.SetPublishingModeRequest);
2059
2285
  const publishingEnabled = request.publishingEnabled;
2060
- this._apply_on_Subscriptions(node_opcua_service_subscription_1.SetPublishingModeResponse, message, channel, async (session, subscription) => {
2286
+ this._apply_on_Subscriptions(node_opcua_service_subscription_1.SetPublishingModeResponse, message, channel, async (_session, subscription) => {
2061
2287
  return subscription.setPublishingMode(publishingEnabled);
2062
2288
  });
2063
2289
  }
2064
2290
  _on_DeleteMonitoredItemsRequest(message, channel) {
2065
2291
  const request = message.request;
2066
2292
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.DeleteMonitoredItemsRequest);
2067
- this._apply_on_Subscription(node_opcua_service_subscription_1.DeleteMonitoredItemsResponse, message, channel, async (session, subscription, sendResponse, sendError) => {
2293
+ this._apply_on_Subscription(node_opcua_service_subscription_1.DeleteMonitoredItemsResponse, message, channel, async (_session, subscription, sendResponse, sendError) => {
2068
2294
  /* c8 ignore next */
2069
2295
  if (!request.monitoredItemIds || request.monitoredItemIds.length === 0) {
2070
2296
  return sendError(node_opcua_status_code_1.StatusCodes.BadNothingToDo);
@@ -2101,7 +2327,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2101
2327
  _on_SetTriggeringRequest(message, channel) {
2102
2328
  const request = message.request;
2103
2329
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.SetTriggeringRequest);
2104
- this._apply_on_Subscription(node_opcua_service_subscription_1.SetTriggeringResponse, message, channel, async (session, subscription, sendResponse, sendError) => {
2330
+ this._apply_on_Subscription(node_opcua_service_subscription_1.SetTriggeringResponse, message, channel, async (_session, subscription, sendResponse, sendError) => {
2105
2331
  /* */
2106
2332
  const { triggeringItemId, linksToAdd, linksToRemove } = request;
2107
2333
  /**
@@ -2119,7 +2345,9 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2119
2345
  }
2120
2346
  const { addResults, removeResults, statusCode } = subscription.setTriggering(triggeringItemId, linksToAdd, linksToRemove);
2121
2347
  if (statusCode.isNotGood()) {
2122
- const response = new node_opcua_types_1.ServiceFault({ responseHeader: { serviceResult: statusCode } });
2348
+ const response = new node_opcua_types_1.ServiceFault({
2349
+ responseHeader: { serviceResult: statusCode }
2350
+ });
2123
2351
  sendResponse(response);
2124
2352
  }
2125
2353
  else {
@@ -2138,12 +2366,16 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2138
2366
  if (!this.options.onDeleteMonitoredItem) {
2139
2367
  return;
2140
2368
  }
2141
- await subscription.applyOnMonitoredItem(this.options.onDeleteMonitoredItem.bind(null, subscription));
2369
+ const t = this.options.onDeleteMonitoredItem.bind(null, subscription);
2370
+ const functor = async (monitoredItem) => {
2371
+ await t(monitoredItem);
2372
+ };
2373
+ await subscription.applyOnMonitoredItem(functor);
2142
2374
  }
2143
2375
  _on_RepublishRequest(message, channel) {
2144
2376
  const request = message.request;
2145
2377
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.RepublishRequest);
2146
- this._apply_on_Subscription(node_opcua_service_subscription_1.RepublishResponse, message, channel, async (session, subscription, sendResponse, sendError) => {
2378
+ this._apply_on_Subscription(node_opcua_service_subscription_1.RepublishResponse, message, channel, async (_session, subscription, sendResponse, sendError) => {
2147
2379
  // update diagnostic counter
2148
2380
  subscription.subscriptionDiagnostics.republishRequestCount += 1;
2149
2381
  subscription.subscriptionDiagnostics.republishMessageRequestCount += 1;
@@ -2170,7 +2402,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2170
2402
  _on_SetMonitoringModeRequest(message, channel) {
2171
2403
  const request = message.request;
2172
2404
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_subscription_1.SetMonitoringModeRequest);
2173
- this._apply_on_Subscription(node_opcua_service_subscription_1.SetMonitoringModeResponse, message, channel, async (session, subscription, sendResponse, sendError) => {
2405
+ this._apply_on_Subscription(node_opcua_service_subscription_1.SetMonitoringModeResponse, message, channel, async (_session, subscription, sendResponse, sendError) => {
2174
2406
  /* c8 ignore next */
2175
2407
  if (!request.monitoredItemIds || request.monitoredItemIds.length === 0) {
2176
2408
  return sendError(node_opcua_status_code_1.StatusCodes.BadNothingToDo);
@@ -2203,7 +2435,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2203
2435
  _on_TranslateBrowsePathsToNodeIdsRequest(message, channel) {
2204
2436
  const request = message.request;
2205
2437
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_translate_browse_path_1.TranslateBrowsePathsToNodeIdsRequest);
2206
- this._apply_on_SessionObject(node_opcua_service_translate_browse_path_1.TranslateBrowsePathsToNodeIdsResponse, message, channel, async (session, sendResponse, sendError) => {
2438
+ this._apply_on_SessionObject(node_opcua_service_translate_browse_path_1.TranslateBrowsePathsToNodeIdsResponse, message, channel, async (_session, sendResponse, sendError) => {
2207
2439
  if (!request.browsePaths || request.browsePaths.length === 0) {
2208
2440
  return sendError(node_opcua_status_code_1.StatusCodes.BadNothingToDo);
2209
2441
  }
@@ -2222,7 +2454,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2222
2454
  });
2223
2455
  sendResponse(response);
2224
2456
  })
2225
- .catch((err) => {
2457
+ .catch((_err) => {
2226
2458
  sendError(node_opcua_status_code_1.StatusCodes.BadInternalError);
2227
2459
  });
2228
2460
  });
@@ -2236,7 +2468,6 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2236
2468
  const request = message.request;
2237
2469
  (0, node_opcua_assert_1.assert)(request instanceof node_opcua_service_call_1.CallRequest);
2238
2470
  this._apply_on_SessionObject(node_opcua_service_call_1.CallResponse, message, channel, (session, sendResponse, sendError) => {
2239
- let response;
2240
2471
  if (!request.methodsToCall || request.methodsToCall.length === 0) {
2241
2472
  return sendError(node_opcua_status_code_1.StatusCodes.BadNothingToDo);
2242
2473
  }
@@ -2255,7 +2486,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2255
2486
  filterDiagnosticInfo(request.requestHeader.returnDiagnostics, response);
2256
2487
  sendResponse(response);
2257
2488
  })
2258
- .catch((err) => {
2489
+ .catch((_err) => {
2259
2490
  sendError(node_opcua_status_code_1.StatusCodes.BadInternalError);
2260
2491
  });
2261
2492
  });
@@ -2306,7 +2537,7 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2306
2537
  }
2307
2538
  /* c8 ignore next */
2308
2539
  _on_Cancel(message, channel) {
2309
- return g_sendError(channel, message, node_opcua_types_1.CancelResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2540
+ g_sendError(channel, message, node_opcua_types_1.CancelResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2310
2541
  }
2311
2542
  // NodeManagement Service Set Overview
2312
2543
  // This Service Set defines Services to add and delete AddressSpace Nodes and References between them. All added
@@ -2314,32 +2545,32 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2314
2545
  //
2315
2546
  /* c8 ignore next */
2316
2547
  _on_AddNodes(message, channel) {
2317
- return g_sendError(channel, message, node_opcua_service_node_management_1.AddNodesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2548
+ g_sendError(channel, message, node_opcua_service_node_management_1.AddNodesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2318
2549
  }
2319
2550
  /* c8 ignore next */
2320
2551
  _on_AddReferences(message, channel) {
2321
- return g_sendError(channel, message, node_opcua_service_node_management_1.AddReferencesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2552
+ g_sendError(channel, message, node_opcua_service_node_management_1.AddReferencesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2322
2553
  }
2323
2554
  /* c8 ignore next */
2324
2555
  _on_DeleteNodes(message, channel) {
2325
- return g_sendError(channel, message, node_opcua_service_node_management_1.DeleteNodesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2556
+ g_sendError(channel, message, node_opcua_service_node_management_1.DeleteNodesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2326
2557
  }
2327
2558
  /* c8 ignore next */
2328
2559
  _on_DeleteReferences(message, channel) {
2329
- return g_sendError(channel, message, node_opcua_service_node_management_1.DeleteReferencesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2560
+ g_sendError(channel, message, node_opcua_service_node_management_1.DeleteReferencesResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2330
2561
  }
2331
2562
  // Query Service
2332
2563
  /* c8 ignore next */
2333
2564
  _on_QueryFirst(message, channel) {
2334
- return g_sendError(channel, message, node_opcua_service_query_1.QueryFirstResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2565
+ g_sendError(channel, message, node_opcua_service_query_1.QueryFirstResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2335
2566
  }
2336
2567
  /* c8 ignore next */
2337
2568
  _on_QueryNext(message, channel) {
2338
- return g_sendError(channel, message, node_opcua_service_query_1.QueryNextResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2569
+ g_sendError(channel, message, node_opcua_service_query_1.QueryNextResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2339
2570
  }
2340
2571
  /* c8 ignore next */
2341
2572
  _on_HistoryUpdate(message, channel) {
2342
- return g_sendError(channel, message, node_opcua_service_history_1.HistoryUpdateResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2573
+ g_sendError(channel, message, node_opcua_service_history_1.HistoryUpdateResponse, node_opcua_status_code_1.StatusCodes.BadServiceUnsupported);
2343
2574
  }
2344
2575
  createEndpoint(port1, serverOptions) {
2345
2576
  // add the tcp/ip endpoint with no security
@@ -2367,15 +2598,15 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2367
2598
  endpointOptions.hostname = endpointOptions.hostname || hostname;
2368
2599
  endpointOptions.port = endpointOptions.port === undefined ? 26543 : endpointOptions.port;
2369
2600
  /* c8 ignore next */
2370
- if (!Object.prototype.hasOwnProperty.call(endpointOptions, "port") ||
2371
- !isFinite(endpointOptions.port) ||
2601
+ if (!Object.hasOwn(endpointOptions, "port") ||
2602
+ !Number.isFinite(endpointOptions.port) ||
2372
2603
  typeof endpointOptions.port !== "number") {
2373
2604
  throw new Error("expecting a valid port (number) when specified. alternatively you can specify port:0 and node-opcua will choose the first available port");
2374
2605
  }
2375
2606
  const port = Number(endpointOptions.port || 0);
2376
2607
  const endPoint = this.createEndpoint(port, serverOption);
2377
2608
  endpointOptions.alternateHostname = endpointOptions.alternateHostname || [];
2378
- const alternateHostname = endpointOptions.alternateHostname instanceof Array
2609
+ const alternateHostname = Array.isArray(endpointOptions.alternateHostname)
2379
2610
  ? endpointOptions.alternateHostname
2380
2611
  : [endpointOptions.alternateHostname];
2381
2612
  const allowAnonymous = endpointOptions.allowAnonymous === undefined ? true : !!endpointOptions.allowAnonymous;
@@ -2387,7 +2618,8 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2387
2618
  alternateHostname,
2388
2619
  disableDiscovery: !!endpointOptions.disableDiscovery,
2389
2620
  // xx hostname,
2390
- resourcePath: serverOption.resourcePath || ""
2621
+ resourcePath: serverOption.resourcePath || "",
2622
+ advertisedEndpoints: endpointOptions.advertisedEndpoints,
2391
2623
  // TODO userTokenTypes: endpointOptions.userTokenTypes || undefined,
2392
2624
  // TODO allowUnsecurePassword: endpointOptions.allowUnsecurePassword || false
2393
2625
  });
@@ -2397,6 +2629,10 @@ class OPCUAServer extends base_server_1.OPCUABaseServer {
2397
2629
  await super.initializeCM();
2398
2630
  await this.userCertificateManager.initialize();
2399
2631
  }
2632
+ // biome-ignore lint/suspicious/noExplicitAny: must match EventEmitter.on() signature
2633
+ on(event, eventHandler) {
2634
+ return super.on(event, eventHandler);
2635
+ }
2400
2636
  }
2401
2637
  exports.OPCUAServer = OPCUAServer;
2402
2638
  const userIdentityTokenPasswordRemoved = (userIdentityToken) => {
@@ -2431,7 +2667,7 @@ function raiseAuditActivateSessionEventType(session) {
2431
2667
  sessionId: { dataType: "NodeId", value: session.nodeId },
2432
2668
  /* part 5 - 6.4.10 AuditActivateSessionEventType */
2433
2669
  clientSoftwareCertificates: {
2434
- arrayType: node_opcua_variant_2.VariantArrayType.Array,
2670
+ arrayType: node_opcua_variant_1.VariantArrayType.Array,
2435
2671
  dataType: "ExtensionObject" /* SignedSoftwareCertificate */,
2436
2672
  value: []
2437
2673
  },
@@ -2446,12 +2682,14 @@ function raiseAuditActivateSessionEventType(session) {
2446
2682
  // use the same identifier in all AuditEvents related to the Session Service Set
2447
2683
  // (AuditCreateSessionEventType, AuditActivateSessionEventType and their subtypes) and
2448
2684
  // the SecureChannel Service Set (AuditChannelEventType and its subtypes).
2449
- secureChannelId: { dataType: "String", value: session.channel.channelId.toString() }
2685
+ secureChannelId: {
2686
+ dataType: "String",
2687
+ value: session.channel?.channelId?.toString() ?? ""
2688
+ }
2450
2689
  });
2451
2690
  }
2452
2691
  }
2453
2692
  const opts = { multiArgs: false };
2454
- OPCUAServer.prototype.start = (0, thenify_ex_1.withCallback)(OPCUAServer.prototype.start, opts);
2455
2693
  OPCUAServer.prototype.initialize = (0, thenify_ex_1.withCallback)(OPCUAServer.prototype.initialize, opts);
2456
2694
  OPCUAServer.prototype.shutdown = (0, thenify_ex_1.withCallback)(OPCUAServer.prototype.shutdown, opts);
2457
2695
  //# sourceMappingURL=opcua_server.js.map