node-opcua-server 2.51.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 (100) hide show
  1. package/.mocharc.yml +10 -0
  2. package/LICENSE +20 -0
  3. package/dist/base_server.d.ts +110 -0
  4. package/dist/base_server.js +476 -0
  5. package/dist/base_server.js.map +1 -0
  6. package/dist/factory.d.ts +10 -0
  7. package/dist/factory.js +24 -0
  8. package/dist/factory.js.map +1 -0
  9. package/dist/history_server_capabilities.d.ts +35 -0
  10. package/dist/history_server_capabilities.js +44 -0
  11. package/dist/history_server_capabilities.js.map +1 -0
  12. package/dist/i_channel_data.d.ts +13 -0
  13. package/dist/i_channel_data.js +3 -0
  14. package/dist/i_channel_data.js.map +1 -0
  15. package/dist/i_register_server_manager.d.ts +16 -0
  16. package/dist/i_register_server_manager.js +3 -0
  17. package/dist/i_register_server_manager.js.map +1 -0
  18. package/dist/i_server_side_publish_engine.d.ts +36 -0
  19. package/dist/i_server_side_publish_engine.js +50 -0
  20. package/dist/i_server_side_publish_engine.js.map +1 -0
  21. package/dist/i_socket_data.d.ts +11 -0
  22. package/dist/i_socket_data.js +3 -0
  23. package/dist/i_socket_data.js.map +1 -0
  24. package/dist/index.d.ts +14 -0
  25. package/dist/index.js +27 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/monitored_item.d.ts +173 -0
  28. package/dist/monitored_item.js +1006 -0
  29. package/dist/monitored_item.js.map +1 -0
  30. package/dist/node_sampler.d.ts +3 -0
  31. package/dist/node_sampler.js +76 -0
  32. package/dist/node_sampler.js.map +1 -0
  33. package/dist/opcua_server.d.ts +668 -0
  34. package/dist/opcua_server.js +2407 -0
  35. package/dist/opcua_server.js.map +1 -0
  36. package/dist/queue.d.ts +11 -0
  37. package/dist/queue.js +71 -0
  38. package/dist/queue.js.map +1 -0
  39. package/dist/register_server_manager.d.ts +92 -0
  40. package/dist/register_server_manager.js +574 -0
  41. package/dist/register_server_manager.js.map +1 -0
  42. package/dist/register_server_manager_hidden.d.ts +17 -0
  43. package/dist/register_server_manager_hidden.js +28 -0
  44. package/dist/register_server_manager_hidden.js.map +1 -0
  45. package/dist/register_server_manager_mdns_only.d.ts +19 -0
  46. package/dist/register_server_manager_mdns_only.js +58 -0
  47. package/dist/register_server_manager_mdns_only.js.map +1 -0
  48. package/dist/server_capabilities.d.ts +61 -0
  49. package/dist/server_capabilities.js +109 -0
  50. package/dist/server_capabilities.js.map +1 -0
  51. package/dist/server_end_point.d.ts +180 -0
  52. package/dist/server_end_point.js +825 -0
  53. package/dist/server_end_point.js.map +1 -0
  54. package/dist/server_engine.d.ts +311 -0
  55. package/dist/server_engine.js +1659 -0
  56. package/dist/server_engine.js.map +1 -0
  57. package/dist/server_publish_engine.d.ts +109 -0
  58. package/dist/server_publish_engine.js +531 -0
  59. package/dist/server_publish_engine.js.map +1 -0
  60. package/dist/server_publish_engine_for_orphan_subscriptions.d.ts +16 -0
  61. package/dist/server_publish_engine_for_orphan_subscriptions.js +50 -0
  62. package/dist/server_publish_engine_for_orphan_subscriptions.js.map +1 -0
  63. package/dist/server_session.d.ts +176 -0
  64. package/dist/server_session.js +734 -0
  65. package/dist/server_session.js.map +1 -0
  66. package/dist/server_subscription.d.ts +393 -0
  67. package/dist/server_subscription.js +1313 -0
  68. package/dist/server_subscription.js.map +1 -0
  69. package/dist/sessions_compatible_for_transfer.d.ts +2 -0
  70. package/dist/sessions_compatible_for_transfer.js +36 -0
  71. package/dist/sessions_compatible_for_transfer.js.map +1 -0
  72. package/dist/validate_filter.d.ts +5 -0
  73. package/dist/validate_filter.js +64 -0
  74. package/dist/validate_filter.js.map +1 -0
  75. package/package.json +88 -0
  76. package/source/base_server.ts +617 -0
  77. package/source/factory.ts +25 -0
  78. package/source/history_server_capabilities.ts +75 -0
  79. package/source/i_channel_data.ts +17 -0
  80. package/source/i_register_server_manager.ts +24 -0
  81. package/source/i_server_side_publish_engine.ts +77 -0
  82. package/source/i_socket_data.ts +11 -0
  83. package/source/index.ts +14 -0
  84. package/source/monitored_item.ts +1303 -0
  85. package/source/node_sampler.ts +82 -0
  86. package/source/opcua_server.ts +3742 -0
  87. package/source/queue.ts +73 -0
  88. package/source/register_server_manager.ts +744 -0
  89. package/source/register_server_manager_hidden.ts +33 -0
  90. package/source/register_server_manager_mdns_only.ts +69 -0
  91. package/source/server_capabilities.ts +177 -0
  92. package/source/server_end_point.ts +1182 -0
  93. package/source/server_engine.ts +2167 -0
  94. package/source/server_publish_engine.ts +657 -0
  95. package/source/server_publish_engine_for_orphan_subscriptions.ts +52 -0
  96. package/source/server_session.ts +931 -0
  97. package/source/server_subscription.ts +1792 -0
  98. package/source/sessions_compatible_for_transfer.ts +33 -0
  99. package/source/validate_filter.ts +86 -0
  100. package/test_helpers/create_certificates.js +1 -0
@@ -0,0 +1,734 @@
1
+ "use strict";
2
+ /**
3
+ * @module node-opcua-server
4
+ */
5
+ // tslint:disable:no-console
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.ServerSession = void 0;
8
+ const crypto = require("crypto");
9
+ const events_1 = require("events");
10
+ const node_opcua_address_space_1 = require("node-opcua-address-space");
11
+ const node_opcua_assert_1 = require("node-opcua-assert");
12
+ const node_opcua_basic_types_1 = require("node-opcua-basic-types");
13
+ const node_opcua_common_1 = require("node-opcua-common");
14
+ const node_opcua_data_model_1 = require("node-opcua-data-model");
15
+ const node_opcua_debug_1 = require("node-opcua-debug");
16
+ const node_opcua_nodeid_1 = require("node-opcua-nodeid");
17
+ const node_opcua_object_registry_1 = require("node-opcua-object-registry");
18
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
19
+ const node_opcua_utils_1 = require("node-opcua-utils");
20
+ const node_opcua_utils_2 = require("node-opcua-utils");
21
+ const server_publish_engine_1 = require("./server_publish_engine");
22
+ const server_subscription_1 = require("./server_subscription");
23
+ const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
24
+ const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
25
+ const theWatchDog = new node_opcua_utils_1.WatchDog();
26
+ const registeredNodeNameSpace = 9999;
27
+ function compareSessionId(sessionDiagnostics1, sessionDiagnostics2) {
28
+ return sessionDiagnostics1.sessionId.toString() === sessionDiagnostics2.sessionId.toString();
29
+ }
30
+ function on_channel_abort() {
31
+ debugLog("ON CHANNEL ABORT ON SESSION!!!");
32
+ /**
33
+ * @event channel_aborted
34
+ */
35
+ this.emit("channel_aborted");
36
+ }
37
+ /**
38
+ *
39
+ * A Server session object.
40
+ *
41
+ * **from OPCUA Spec 1.02:**
42
+ *
43
+ * * Sessions are created to be independent of the underlying communications connection. Therefore, if a communication
44
+ * connection fails, the Session is not immediately affected. The exact mechanism to recover from an underlying
45
+ * communication connection error depends on the SecureChannel mapping as described in Part 6.
46
+ *
47
+ * * Sessions are terminated by the Server automatically if the Client fails to issue a Service request on the Session
48
+ * within the timeout period negotiated by the Server in the CreateSession Service response. This protects the Server
49
+ * against Client failures and against situations where a failed underlying connection cannot be re-established.
50
+ *
51
+ * * Clients shall be prepared to submit requests in a timely manner to prevent the Session from closing automatically.
52
+ *
53
+ * * Clients may explicitly terminate Sessions using the CloseSession Service.
54
+ *
55
+ * * When a Session is terminated, all outstanding requests on the Session are aborted and BadSessionClosed StatusCodes
56
+ * are returned to the Client. In addition, the Server deletes the entry for the Client from its
57
+ * SessionDiagnosticsArray Variable and notifies any other Clients who were subscribed to this entry.
58
+ *
59
+ */
60
+ class ServerSession extends events_1.EventEmitter {
61
+ constructor(parent, sessionTimeout) {
62
+ super();
63
+ this.__status = "";
64
+ this.sessionName = "";
65
+ this.keepAlive = node_opcua_utils_1.WatchDog.emptyKeepAlive;
66
+ this.parent = parent; // SessionEngine
67
+ ServerSession.registry.register(this);
68
+ (0, node_opcua_assert_1.assert)(isFinite(sessionTimeout));
69
+ (0, node_opcua_assert_1.assert)(sessionTimeout >= 0, " sessionTimeout");
70
+ this.sessionTimeout = sessionTimeout;
71
+ const authenticationTokenBuf = crypto.randomBytes(16);
72
+ this.authenticationToken = new node_opcua_nodeid_1.NodeId(node_opcua_nodeid_1.NodeIdType.BYTESTRING, authenticationTokenBuf);
73
+ // the sessionId
74
+ const ownNamespaceIndex = 1; // addressSpace.getOwnNamespace().index;
75
+ this.nodeId = new node_opcua_nodeid_1.NodeId(node_opcua_nodeid_1.NodeIdType.GUID, (0, node_opcua_basic_types_1.randomGuid)(), ownNamespaceIndex);
76
+ (0, node_opcua_assert_1.assert)(this.authenticationToken instanceof node_opcua_nodeid_1.NodeId);
77
+ (0, node_opcua_assert_1.assert)(this.nodeId instanceof node_opcua_nodeid_1.NodeId);
78
+ this._cumulatedSubscriptionCount = 0;
79
+ this.publishEngine = new server_publish_engine_1.ServerSidePublishEngine({
80
+ maxPublishRequestInQueue: ServerSession.maxPublishRequestInQueue
81
+ });
82
+ this.publishEngine.setMaxListeners(100);
83
+ theWatchDog.addSubscriber(this, this.sessionTimeout);
84
+ this.__status = "new";
85
+ /**
86
+ * the continuation point manager for this session
87
+ * @property continuationPointManager
88
+ * @type {ContinuationPointManager}
89
+ */
90
+ this.continuationPointManager = new node_opcua_address_space_1.ContinuationPointManager();
91
+ /**
92
+ * @property creationDate
93
+ * @type {Date}
94
+ */
95
+ this.creationDate = new Date();
96
+ this._registeredNodesCounter = 0;
97
+ this._registeredNodes = {};
98
+ this._registeredNodesInv = {};
99
+ }
100
+ getSessionId() {
101
+ return this.nodeId;
102
+ }
103
+ getEndpointDescription() {
104
+ return this.endpoint;
105
+ }
106
+ dispose() {
107
+ debugLog("ServerSession#dispose()");
108
+ (0, node_opcua_assert_1.assert)(!this.sessionObject, " sessionObject has not been cleared !");
109
+ this.parent = null;
110
+ this.authenticationToken = node_opcua_nodeid_1.NodeId.nullNodeId;
111
+ if (this.publishEngine) {
112
+ this.publishEngine.dispose();
113
+ this.publishEngine = null;
114
+ }
115
+ this._sessionDiagnostics = undefined;
116
+ this._registeredNodesCounter = 0;
117
+ this._registeredNodes = null;
118
+ this._registeredNodesInv = null;
119
+ this.continuationPointManager = null;
120
+ this.removeAllListeners();
121
+ this.__status = "disposed";
122
+ ServerSession.registry.unregister(this);
123
+ }
124
+ get clientConnectionTime() {
125
+ return this.creationDate;
126
+ }
127
+ get clientLastContactTime() {
128
+ return this._watchDogData.lastSeen;
129
+ }
130
+ get status() {
131
+ return this.__status;
132
+ }
133
+ set status(value) {
134
+ if (value === "active") {
135
+ this._createSessionObjectInAddressSpace();
136
+ }
137
+ this.__status = value;
138
+ }
139
+ get addressSpace() {
140
+ if (this.parent && this.parent.addressSpace) {
141
+ return this.parent.addressSpace;
142
+ }
143
+ return null;
144
+ }
145
+ get currentPublishRequestInQueue() {
146
+ return this.publishEngine ? this.publishEngine.pendingPublishRequestCount : 0;
147
+ }
148
+ updateClientLastContactTime() {
149
+ const session = this;
150
+ if (session._sessionDiagnostics && session._sessionDiagnostics.clientLastContactTime) {
151
+ const currentTime = new Date();
152
+ // do not record all ticks as this may be overwhelming,
153
+ if (currentTime.getTime() - 250 >= session._sessionDiagnostics.clientLastContactTime.getTime()) {
154
+ session._sessionDiagnostics.clientLastContactTime = currentTime;
155
+ }
156
+ }
157
+ }
158
+ /**
159
+ * @method onClientSeen
160
+ * required for watch dog
161
+ * @param currentTime {DateTime}
162
+ * @private
163
+ */
164
+ onClientSeen() {
165
+ this.updateClientLastContactTime();
166
+ if (this._sessionDiagnostics) {
167
+ // see https://opcfoundation-onlineapplications.org/mantis/view.php?id=4111
168
+ (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.hasOwnProperty("currentMonitoredItemsCount"));
169
+ (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.hasOwnProperty("currentSubscriptionsCount"));
170
+ (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.hasOwnProperty("currentPublishRequestsInQueue"));
171
+ // note : https://opcfoundation-onlineapplications.org/mantis/view.php?id=4111
172
+ // sessionDiagnostics extension object uses a different spelling
173
+ // here with an S !!!!
174
+ if (this._sessionDiagnostics.currentMonitoredItemsCount !== this.currentMonitoredItemCount) {
175
+ this._sessionDiagnostics.currentMonitoredItemsCount = this.currentMonitoredItemCount;
176
+ }
177
+ if (this._sessionDiagnostics.currentSubscriptionsCount !== this.currentSubscriptionCount) {
178
+ this._sessionDiagnostics.currentSubscriptionsCount = this.currentSubscriptionCount;
179
+ }
180
+ if (this._sessionDiagnostics.currentPublishRequestsInQueue !== this.currentPublishRequestInQueue) {
181
+ this._sessionDiagnostics.currentPublishRequestsInQueue = this.currentPublishRequestInQueue;
182
+ }
183
+ }
184
+ }
185
+ incrementTotalRequestCount() {
186
+ if (this._sessionDiagnostics && this._sessionDiagnostics.totalRequestCount) {
187
+ this._sessionDiagnostics.totalRequestCount.totalCount += 1;
188
+ }
189
+ }
190
+ incrementRequestTotalCounter(counterName) {
191
+ if (this._sessionDiagnostics) {
192
+ const propName = (0, node_opcua_utils_2.lowerFirstLetter)(counterName + "Count");
193
+ if (!this._sessionDiagnostics.hasOwnProperty(propName)) {
194
+ console.log(" cannot find", propName);
195
+ // xx return;
196
+ }
197
+ else {
198
+ // console.log(self._sessionDiagnostics.toString());
199
+ this._sessionDiagnostics[propName].totalCount += 1;
200
+ }
201
+ }
202
+ }
203
+ incrementRequestErrorCounter(counterName) {
204
+ var _a;
205
+ (_a = this.parent) === null || _a === void 0 ? void 0 : _a.incrementRejectedRequestsCount();
206
+ if (this._sessionDiagnostics) {
207
+ const propName = (0, node_opcua_utils_2.lowerFirstLetter)(counterName + "Count");
208
+ if (!this._sessionDiagnostics.hasOwnProperty(propName)) {
209
+ console.log(" cannot find", propName);
210
+ // xx return;
211
+ }
212
+ else {
213
+ this._sessionDiagnostics[propName].errorCount += 1;
214
+ }
215
+ }
216
+ }
217
+ /**
218
+ * returns rootFolder.objects.server.serverDiagnostics.sessionsDiagnosticsSummary.sessionDiagnosticsArray
219
+ */
220
+ getSessionDiagnosticsArray() {
221
+ const server = this.addressSpace.rootFolder.objects.server;
222
+ return server.serverDiagnostics.sessionsDiagnosticsSummary.sessionDiagnosticsArray;
223
+ }
224
+ /**
225
+ * returns rootFolder.objects.server.serverDiagnostics.sessionsDiagnosticsSummary.sessionSecurityDiagnosticsArray
226
+ */
227
+ getSessionSecurityDiagnosticsArray() {
228
+ const server = this.addressSpace.rootFolder.objects.server;
229
+ return server.serverDiagnostics.sessionsDiagnosticsSummary.sessionSecurityDiagnosticsArray;
230
+ }
231
+ /**
232
+ * number of active subscriptions
233
+ */
234
+ get currentSubscriptionCount() {
235
+ return this.publishEngine ? this.publishEngine.subscriptionCount : 0;
236
+ }
237
+ /**
238
+ * number of subscriptions ever created since this object is live
239
+ */
240
+ get cumulatedSubscriptionCount() {
241
+ return this._cumulatedSubscriptionCount;
242
+ }
243
+ /**
244
+ * number of monitored items
245
+ */
246
+ get currentMonitoredItemCount() {
247
+ const self = this;
248
+ return self.publishEngine ? self.publishEngine.currentMonitoredItemCount : 0;
249
+ }
250
+ /**
251
+ * retrieve an existing subscription by subscriptionId
252
+ * @method getSubscription
253
+ * @param subscriptionId {Number}
254
+ * @return {Subscription}
255
+ */
256
+ getSubscription(subscriptionId) {
257
+ const subscription = this.publishEngine.getSubscriptionById(subscriptionId);
258
+ if (subscription && subscription.state === server_subscription_1.SubscriptionState.CLOSED) {
259
+ // subscription is CLOSED but has not been notified yet
260
+ // it should be considered as excluded
261
+ return null;
262
+ }
263
+ (0, node_opcua_assert_1.assert)(!subscription || subscription.state !== server_subscription_1.SubscriptionState.CLOSED, "CLOSED subscription shall not be managed by publish engine anymore");
264
+ return subscription;
265
+ }
266
+ /**
267
+ * @method deleteSubscription
268
+ * @param subscriptionId {Number}
269
+ * @return {StatusCode}
270
+ */
271
+ deleteSubscription(subscriptionId) {
272
+ const session = this;
273
+ const subscription = session.getSubscription(subscriptionId);
274
+ if (!subscription) {
275
+ return node_opcua_status_code_1.StatusCodes.BadSubscriptionIdInvalid;
276
+ }
277
+ // xx this.publishEngine.remove_subscription(subscription);
278
+ subscription.terminate();
279
+ if (session.currentSubscriptionCount === 0) {
280
+ const local_publishEngine = session.publishEngine;
281
+ local_publishEngine.cancelPendingPublishRequest();
282
+ }
283
+ return node_opcua_status_code_1.StatusCodes.Good;
284
+ }
285
+ /**
286
+ * close a ServerSession, this will also delete the subscriptions if the flag is set.
287
+ *
288
+ * Spec extract:
289
+ *
290
+ * If a Client invokes the CloseSession Service then all Subscriptions associated with the Session are also deleted
291
+ * if the deleteSubscriptions flag is set to TRUE. If a Server terminates a Session for any other reason,
292
+ * Subscriptions associated with the Session, are not deleted. Each Subscription has its own lifetime to protect
293
+ * against data loss in the case of a Session termination. In these cases, the Subscription can be reassigned to
294
+ * another Client before its lifetime expires.
295
+ *
296
+ * @method close
297
+ * @param deleteSubscriptions : should we delete subscription ?
298
+ * @param [reason = "CloseSession"] the reason for closing the session
299
+ * (shall be "Timeout", "Terminated" or "CloseSession")
300
+ *
301
+ */
302
+ close(deleteSubscriptions, reason) {
303
+ debugLog(" closing session deleteSubscriptions = ", deleteSubscriptions);
304
+ if (this.publishEngine) {
305
+ this.publishEngine.onSessionClose();
306
+ }
307
+ theWatchDog.removeSubscriber(this);
308
+ // --------------- delete associated subscriptions ---------------------
309
+ if (!deleteSubscriptions && this.currentSubscriptionCount !== 0) {
310
+ // I don't know what to do yet if deleteSubscriptions is false
311
+ console.log("TO DO : Closing session without deleting subscription not yet implemented");
312
+ // to do: Put subscriptions in safe place for future transfer if any
313
+ }
314
+ this._deleteSubscriptions();
315
+ (0, node_opcua_assert_1.assert)(this.currentSubscriptionCount === 0);
316
+ // Post-Conditions
317
+ (0, node_opcua_assert_1.assert)(this.currentSubscriptionCount === 0);
318
+ this.status = "closed";
319
+ /**
320
+ * @event session_closed
321
+ * @param deleteSubscriptions {Boolean}
322
+ * @param reason {String}
323
+ */
324
+ this.emit("session_closed", this, deleteSubscriptions, reason);
325
+ // ---------------- shut down publish engine
326
+ if (this.publishEngine) {
327
+ // remove subscription
328
+ this.publishEngine.shutdown();
329
+ (0, node_opcua_assert_1.assert)(this.publishEngine.subscriptionCount === 0);
330
+ this.publishEngine.dispose();
331
+ this.publishEngine = null;
332
+ }
333
+ this._removeSessionObjectFromAddressSpace();
334
+ (0, node_opcua_assert_1.assert)(!this.sessionDiagnostics, "ServerSession#_removeSessionObjectFromAddressSpace must be called");
335
+ (0, node_opcua_assert_1.assert)(!this.sessionObject, "ServerSession#_removeSessionObjectFromAddressSpace must be called");
336
+ }
337
+ registerNode(nodeId) {
338
+ (0, node_opcua_assert_1.assert)(nodeId instanceof node_opcua_nodeid_1.NodeId);
339
+ const session = this;
340
+ if (nodeId.namespace === 0 && nodeId.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC) {
341
+ return nodeId;
342
+ }
343
+ const key = nodeId.toString();
344
+ const registeredNode = session._registeredNodes[key];
345
+ if (registeredNode) {
346
+ // already registered
347
+ return registeredNode;
348
+ }
349
+ const node = session.addressSpace.findNode(nodeId);
350
+ if (!node) {
351
+ return nodeId;
352
+ }
353
+ session._registeredNodesCounter += 1;
354
+ const aliasNodeId = (0, node_opcua_nodeid_1.makeNodeId)(session._registeredNodesCounter, registeredNodeNameSpace);
355
+ session._registeredNodes[key] = aliasNodeId;
356
+ session._registeredNodesInv[aliasNodeId.toString()] = node;
357
+ return aliasNodeId;
358
+ }
359
+ unRegisterNode(aliasNodeId) {
360
+ (0, node_opcua_assert_1.assert)(aliasNodeId instanceof node_opcua_nodeid_1.NodeId);
361
+ if (aliasNodeId.namespace !== registeredNodeNameSpace) {
362
+ return; // not a registered Node
363
+ }
364
+ const session = this;
365
+ const node = session._registeredNodesInv[aliasNodeId.toString()];
366
+ if (!node) {
367
+ return;
368
+ }
369
+ session._registeredNodesInv[aliasNodeId.toString()] = null;
370
+ session._registeredNodes[node.nodeId.toString()] = null;
371
+ }
372
+ resolveRegisteredNode(aliasNodeId) {
373
+ if (aliasNodeId.namespace !== registeredNodeNameSpace) {
374
+ return aliasNodeId; // not a registered Node
375
+ }
376
+ const node = this._registeredNodesInv[aliasNodeId.toString()];
377
+ if (!node) {
378
+ return aliasNodeId;
379
+ }
380
+ return node.nodeId;
381
+ }
382
+ /**
383
+ * true if the underlying channel has been closed or aborted...
384
+ */
385
+ get aborted() {
386
+ if (!this.channel) {
387
+ return true;
388
+ }
389
+ return this.channel.aborted;
390
+ }
391
+ createSubscription(parameters) {
392
+ const subscription = this.parent._createSubscriptionOnSession(this, parameters);
393
+ (0, node_opcua_assert_1.assert)(!parameters.hasOwnProperty("id"));
394
+ this.assignSubscription(subscription);
395
+ (0, node_opcua_assert_1.assert)(subscription.$session === this);
396
+ (0, node_opcua_assert_1.assert)(subscription.sessionId instanceof node_opcua_nodeid_1.NodeId);
397
+ (0, node_opcua_assert_1.assert)((0, node_opcua_nodeid_1.sameNodeId)(subscription.sessionId, this.nodeId));
398
+ return subscription;
399
+ }
400
+ _attach_channel(channel) {
401
+ (0, node_opcua_assert_1.assert)(this.nonce && this.nonce instanceof Buffer);
402
+ this.channel = channel;
403
+ this.channelId = channel.channelId;
404
+ const key = this.authenticationToken.toString();
405
+ (0, node_opcua_assert_1.assert)(!channel.sessionTokens.hasOwnProperty(key), "channel has already a session");
406
+ channel.sessionTokens[key] = this;
407
+ // when channel is aborting
408
+ this.channel_abort_event_handler = on_channel_abort.bind(this);
409
+ channel.on("abort", this.channel_abort_event_handler);
410
+ }
411
+ _detach_channel() {
412
+ const channel = this.channel;
413
+ if (!channel) {
414
+ throw new Error("expecting a valid channel");
415
+ }
416
+ (0, node_opcua_assert_1.assert)(this.nonce && this.nonce instanceof Buffer);
417
+ (0, node_opcua_assert_1.assert)(this.authenticationToken);
418
+ const key = this.authenticationToken.toString();
419
+ (0, node_opcua_assert_1.assert)(channel.sessionTokens.hasOwnProperty(key));
420
+ (0, node_opcua_assert_1.assert)(this.channel);
421
+ (0, node_opcua_assert_1.assert)(typeof this.channel_abort_event_handler === "function");
422
+ channel.removeListener("abort", this.channel_abort_event_handler);
423
+ delete channel.sessionTokens[key];
424
+ this.channel = undefined;
425
+ this.channelId = undefined;
426
+ }
427
+ _exposeSubscriptionDiagnostics(subscription) {
428
+ debugLog("ServerSession#_exposeSubscriptionDiagnostics");
429
+ (0, node_opcua_assert_1.assert)(subscription.$session === this);
430
+ const subscriptionDiagnosticsArray = this._getSubscriptionDiagnosticsArray();
431
+ const subscriptionDiagnostics = subscription.subscriptionDiagnostics;
432
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnostics.$subscription === subscription);
433
+ if (subscriptionDiagnostics && subscriptionDiagnosticsArray) {
434
+ // subscription.id,"on session", session.nodeId.toString());
435
+ (0, node_opcua_address_space_1.addElement)(subscriptionDiagnostics, subscriptionDiagnosticsArray);
436
+ }
437
+ }
438
+ _unexposeSubscriptionDiagnostics(subscription) {
439
+ const subscriptionDiagnosticsArray = this._getSubscriptionDiagnosticsArray();
440
+ const subscriptionDiagnostics = subscription.subscriptionDiagnostics;
441
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnostics instanceof node_opcua_common_1.SubscriptionDiagnosticsDataType);
442
+ if (subscriptionDiagnostics && subscriptionDiagnosticsArray) {
443
+ // console.log("GG => ServerSession **Unexposing** subscription diagnostics =>",
444
+ // subscription.id,"on session", session.nodeId.toString());
445
+ (0, node_opcua_address_space_1.removeElement)(subscriptionDiagnosticsArray, subscriptionDiagnostics);
446
+ }
447
+ debugLog("ServerSession#_unexposeSubscriptionDiagnostics");
448
+ }
449
+ /**
450
+ * @method watchdogReset
451
+ * used as a callback for the Watchdog
452
+ * @private
453
+ */
454
+ watchdogReset() {
455
+ debugLog("Session#watchdogReset: the server session has expired and must be removed from the server");
456
+ // the server session has expired and must be removed from the server
457
+ this.emit("timeout");
458
+ }
459
+ _createSessionObjectInAddressSpace() {
460
+ if (this.sessionObject) {
461
+ return;
462
+ }
463
+ (0, node_opcua_assert_1.assert)(!this.sessionObject, "ServerSession#_createSessionObjectInAddressSpace already called ?");
464
+ this.sessionObject = null;
465
+ if (!this.addressSpace) {
466
+ debugLog("ServerSession#_createSessionObjectInAddressSpace : no addressSpace");
467
+ return; // no addressSpace
468
+ }
469
+ const root = this.addressSpace.rootFolder;
470
+ (0, node_opcua_assert_1.assert)(root, "expecting a root object");
471
+ if (!root.objects) {
472
+ debugLog("ServerSession#_createSessionObjectInAddressSpace : no object folder");
473
+ return false;
474
+ }
475
+ if (!root.objects.server) {
476
+ debugLog("ServerSession#_createSessionObjectInAddressSpace : no server object");
477
+ return false;
478
+ }
479
+ // self.addressSpace.findNode(makeNodeId(ObjectIds.Server_ServerDiagnostics));
480
+ const serverDiagnosticsNode = root.objects.server.serverDiagnostics;
481
+ if (!serverDiagnosticsNode || !serverDiagnosticsNode.sessionsDiagnosticsSummary) {
482
+ debugLog("ServerSession#_createSessionObjectInAddressSpace :" + " no serverDiagnostics.sessionsDiagnosticsSummary");
483
+ return false;
484
+ }
485
+ const sessionDiagnosticsObjectType = this.addressSpace.findObjectType("SessionDiagnosticsObjectType");
486
+ const sessionDiagnosticsDataType = this.addressSpace.findDataType("SessionDiagnosticsDataType");
487
+ const sessionDiagnosticsVariableType = this.addressSpace.findVariableType("SessionDiagnosticsVariableType");
488
+ const sessionSecurityDiagnosticsDataType = this.addressSpace.findDataType("SessionSecurityDiagnosticsDataType");
489
+ const sessionSecurityDiagnosticsType = this.addressSpace.findVariableType("SessionSecurityDiagnosticsType");
490
+ const namespace = this.addressSpace.getOwnNamespace();
491
+ function createSessionDiagnosticsStuff() {
492
+ if (sessionDiagnosticsDataType && sessionDiagnosticsVariableType) {
493
+ // the extension object
494
+ this._sessionDiagnostics = this.addressSpace.constructExtensionObject(sessionDiagnosticsDataType, {});
495
+ this._sessionDiagnostics.$session = this;
496
+ // install property getter on property that are unlikely to change
497
+ if (this.parent.clientDescription) {
498
+ this._sessionDiagnostics.clientDescription = this.parent.clientDescription;
499
+ }
500
+ Object.defineProperty(this._sessionDiagnostics, "clientConnectionTime", {
501
+ get() {
502
+ return this.$session.clientConnectionTime;
503
+ }
504
+ });
505
+ Object.defineProperty(this._sessionDiagnostics, "actualSessionTimeout", {
506
+ get() {
507
+ var _a;
508
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.sessionTimeout;
509
+ }
510
+ });
511
+ Object.defineProperty(this._sessionDiagnostics, "sessionId", {
512
+ get() {
513
+ var _a;
514
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.nodeId;
515
+ }
516
+ });
517
+ Object.defineProperty(this._sessionDiagnostics, "sessionName", {
518
+ get() {
519
+ var _a;
520
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.sessionName.toString();
521
+ }
522
+ });
523
+ this.sessionDiagnostics = sessionDiagnosticsVariableType.instantiate({
524
+ browseName: new node_opcua_data_model_1.QualifiedName({ name: "SessionDiagnostics", namespaceIndex: 0 }),
525
+ componentOf: this.sessionObject,
526
+ extensionObject: this._sessionDiagnostics,
527
+ minimumSamplingInterval: 2000 // 2 seconds
528
+ });
529
+ this._sessionDiagnostics = this.sessionDiagnostics.$extensionObject;
530
+ (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.$session === this);
531
+ const sessionDiagnosticsArray = this.getSessionDiagnosticsArray();
532
+ // add sessionDiagnostics into sessionDiagnosticsArray
533
+ (0, node_opcua_address_space_1.addElement)(this._sessionDiagnostics, sessionDiagnosticsArray);
534
+ }
535
+ }
536
+ function createSessionSecurityDiagnosticsStuff() {
537
+ if (sessionSecurityDiagnosticsDataType && sessionSecurityDiagnosticsType) {
538
+ // the extension object
539
+ this._sessionSecurityDiagnostics = this.addressSpace.constructExtensionObject(sessionSecurityDiagnosticsDataType, {});
540
+ this._sessionSecurityDiagnostics.$session = this;
541
+ /*
542
+ sessionId: NodeId;
543
+ clientUserIdOfSession: UAString;
544
+ clientUserIdHistory: UAString[] | null;
545
+ authenticationMechanism: UAString;
546
+ encoding: UAString;
547
+ transportProtocol: UAString;
548
+ securityMode: MessageSecurityMode;
549
+ securityPolicyUri: UAString;
550
+ clientCertificate: ByteString;
551
+ */
552
+ Object.defineProperty(this._sessionSecurityDiagnostics, "sessionId", {
553
+ get() {
554
+ var _a;
555
+ return (_a = this.$session) === null || _a === void 0 ? void 0 : _a.nodeId;
556
+ }
557
+ });
558
+ Object.defineProperty(this._sessionSecurityDiagnostics, "clientUserIdOfSession", {
559
+ get() {
560
+ return ""; // UAString // TO DO : implement
561
+ }
562
+ });
563
+ Object.defineProperty(this._sessionSecurityDiagnostics, "clientUserIdHistory", {
564
+ get() {
565
+ return []; // UAString[] | null
566
+ }
567
+ });
568
+ Object.defineProperty(this._sessionSecurityDiagnostics, "authenticationMechanism", {
569
+ get() {
570
+ return "";
571
+ }
572
+ });
573
+ Object.defineProperty(this._sessionSecurityDiagnostics, "encoding", {
574
+ get() {
575
+ return "";
576
+ }
577
+ });
578
+ Object.defineProperty(this._sessionSecurityDiagnostics, "transportProtocol", {
579
+ get() {
580
+ return "opc.tcp";
581
+ }
582
+ });
583
+ Object.defineProperty(this._sessionSecurityDiagnostics, "securityMode", {
584
+ get() {
585
+ var _a;
586
+ const session = this.$session;
587
+ return (_a = session === null || session === void 0 ? void 0 : session.channel) === null || _a === void 0 ? void 0 : _a.securityMode;
588
+ }
589
+ });
590
+ Object.defineProperty(this._sessionSecurityDiagnostics, "securityPolicyUri", {
591
+ get() {
592
+ var _a;
593
+ const session = this.$session;
594
+ return (_a = session === null || session === void 0 ? void 0 : session.channel) === null || _a === void 0 ? void 0 : _a.securityPolicy;
595
+ }
596
+ });
597
+ Object.defineProperty(this._sessionSecurityDiagnostics, "clientCertificate", {
598
+ get() {
599
+ const session = this.$session;
600
+ return session === null || session === void 0 ? void 0 : session.channel.clientCertificate;
601
+ }
602
+ });
603
+ this.sessionSecurityDiagnostics = sessionSecurityDiagnosticsType.instantiate({
604
+ browseName: new node_opcua_data_model_1.QualifiedName({ name: "SessionSecurityDiagnostics", namespaceIndex: 0 }),
605
+ componentOf: this.sessionObject,
606
+ extensionObject: this._sessionSecurityDiagnostics,
607
+ minimumSamplingInterval: 2000 // 2 seconds
608
+ });
609
+ (0, node_opcua_address_space_1.ensureObjectIsSecure)(this.sessionSecurityDiagnostics);
610
+ this._sessionSecurityDiagnostics = this.sessionSecurityDiagnostics
611
+ .$extensionObject;
612
+ (0, node_opcua_assert_1.assert)(this._sessionSecurityDiagnostics.$session === this);
613
+ const sessionSecurityDiagnosticsArray = this.getSessionSecurityDiagnosticsArray();
614
+ // add sessionDiagnostics into sessionDiagnosticsArray
615
+ const node = (0, node_opcua_address_space_1.addElement)(this._sessionSecurityDiagnostics, sessionSecurityDiagnosticsArray);
616
+ (0, node_opcua_address_space_1.ensureObjectIsSecure)(node);
617
+ }
618
+ }
619
+ function createSessionDiagnosticSummaryUAObject() {
620
+ const references = [];
621
+ if (sessionDiagnosticsObjectType) {
622
+ references.push({
623
+ isForward: true,
624
+ nodeId: sessionDiagnosticsObjectType,
625
+ referenceType: "HasTypeDefinition"
626
+ });
627
+ }
628
+ this.sessionObject = namespace.createNode({
629
+ browseName: this.sessionName || "Session-" + this.nodeId.toString(),
630
+ componentOf: serverDiagnosticsNode.sessionsDiagnosticsSummary,
631
+ nodeClass: node_opcua_data_model_1.NodeClass.Object,
632
+ nodeId: this.nodeId,
633
+ references,
634
+ typeDefinition: sessionDiagnosticsObjectType
635
+ });
636
+ createSessionDiagnosticsStuff.call(this);
637
+ createSessionSecurityDiagnosticsStuff.call(this);
638
+ }
639
+ function createSubscriptionDiagnosticsArray() {
640
+ const subscriptionDiagnosticsArrayType = this.addressSpace.findVariableType("SubscriptionDiagnosticsArrayType");
641
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnosticsArrayType.nodeId.toString() === "ns=0;i=2171");
642
+ this.subscriptionDiagnosticsArray = (0, node_opcua_address_space_1.createExtObjArrayNode)(this.sessionObject, {
643
+ browseName: { namespaceIndex: 0, name: "SubscriptionDiagnosticsArray" },
644
+ complexVariableType: "SubscriptionDiagnosticsArrayType",
645
+ indexPropertyName: "subscriptionId",
646
+ minimumSamplingInterval: 2000,
647
+ variableType: "SubscriptionDiagnosticsType"
648
+ });
649
+ }
650
+ createSessionDiagnosticSummaryUAObject.call(this);
651
+ createSubscriptionDiagnosticsArray.call(this);
652
+ return this.sessionObject;
653
+ }
654
+ /**
655
+ *
656
+ * @private
657
+ */
658
+ _removeSessionObjectFromAddressSpace() {
659
+ // todo : dump session statistics in a file or somewhere for deeper diagnostic analysis on closed session
660
+ if (!this.addressSpace) {
661
+ return;
662
+ }
663
+ if (this.sessionDiagnostics) {
664
+ const sessionDiagnosticsArray = this.getSessionDiagnosticsArray();
665
+ (0, node_opcua_address_space_1.removeElement)(sessionDiagnosticsArray, this.sessionDiagnostics.$extensionObject);
666
+ this.addressSpace.deleteNode(this.sessionDiagnostics);
667
+ (0, node_opcua_assert_1.assert)(this._sessionDiagnostics.$session === this);
668
+ this._sessionDiagnostics.$session = null;
669
+ this._sessionDiagnostics = undefined;
670
+ this.sessionDiagnostics = undefined;
671
+ }
672
+ if (this.sessionSecurityDiagnostics) {
673
+ const sessionSecurityDiagnosticsArray = this.getSessionSecurityDiagnosticsArray();
674
+ (0, node_opcua_address_space_1.removeElement)(sessionSecurityDiagnosticsArray, this.sessionSecurityDiagnostics.$extensionObject);
675
+ this.addressSpace.deleteNode(this.sessionSecurityDiagnostics);
676
+ (0, node_opcua_assert_1.assert)(this._sessionSecurityDiagnostics.$session === this);
677
+ this._sessionSecurityDiagnostics.$session = null;
678
+ this._sessionSecurityDiagnostics = undefined;
679
+ this.sessionSecurityDiagnostics = undefined;
680
+ }
681
+ if (this.sessionObject) {
682
+ this.addressSpace.deleteNode(this.sessionObject);
683
+ this.sessionObject = null;
684
+ }
685
+ }
686
+ /**
687
+ *
688
+ * @private
689
+ */
690
+ _getSubscriptionDiagnosticsArray() {
691
+ if (!this.addressSpace) {
692
+ if (doDebug) {
693
+ console.warn("ServerSession#_getSubscriptionDiagnosticsArray : no addressSpace");
694
+ }
695
+ return null; // no addressSpace
696
+ }
697
+ const subscriptionDiagnosticsArray = this.subscriptionDiagnosticsArray;
698
+ if (!subscriptionDiagnosticsArray) {
699
+ return null; // no subscriptionDiagnosticsArray
700
+ }
701
+ (0, node_opcua_assert_1.assert)(subscriptionDiagnosticsArray.browseName.toString() === "SubscriptionDiagnosticsArray");
702
+ return subscriptionDiagnosticsArray;
703
+ }
704
+ assignSubscription(subscription) {
705
+ (0, node_opcua_assert_1.assert)(!subscription.$session);
706
+ (0, node_opcua_assert_1.assert)(this.nodeId instanceof node_opcua_nodeid_1.NodeId);
707
+ subscription.$session = this;
708
+ subscription.sessionId = this.nodeId;
709
+ this._cumulatedSubscriptionCount += 1;
710
+ // Notify the owner that a new subscription has been created
711
+ // @event new_subscription
712
+ // @param {Subscription} subscription
713
+ this.emit("new_subscription", subscription);
714
+ // add subscription diagnostics to SubscriptionDiagnosticsArray
715
+ this._exposeSubscriptionDiagnostics(subscription);
716
+ subscription.once("terminated", () => {
717
+ // Notify the owner that a new subscription has been terminated
718
+ // @event subscription_terminated
719
+ // @param {Subscription} subscription
720
+ this.emit("subscription_terminated", subscription);
721
+ });
722
+ }
723
+ _deleteSubscriptions() {
724
+ (0, node_opcua_assert_1.assert)(this.publishEngine);
725
+ const subscriptions = this.publishEngine.subscriptions;
726
+ for (const subscription of subscriptions) {
727
+ this.deleteSubscription(subscription.id);
728
+ }
729
+ }
730
+ }
731
+ exports.ServerSession = ServerSession;
732
+ ServerSession.registry = new node_opcua_object_registry_1.ObjectRegistry();
733
+ ServerSession.maxPublishRequestInQueue = 100;
734
+ //# sourceMappingURL=server_session.js.map