node-opcua-client 2.78.0 → 2.79.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 (38) hide show
  1. package/dist/client_monitored_item_group.d.ts +3 -0
  2. package/dist/client_monitored_item_group.js.map +1 -1
  3. package/dist/client_session.d.ts +4 -1
  4. package/dist/client_session.js.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/opcua_client.d.ts +7 -21
  9. package/dist/opcua_client.js.map +1 -1
  10. package/dist/private/client_base_impl.d.ts +2 -1
  11. package/dist/private/client_base_impl.js +1 -1
  12. package/dist/private/client_base_impl.js.map +1 -1
  13. package/dist/private/client_monitored_item_impl.js +4 -4
  14. package/dist/private/client_monitored_item_impl.js.map +1 -1
  15. package/dist/private/client_session_impl.d.ts +5 -1
  16. package/dist/private/client_session_impl.js +20 -0
  17. package/dist/private/client_session_impl.js.map +1 -1
  18. package/dist/private/i_private_client.d.ts +2 -1
  19. package/dist/private/opcua_client_impl.d.ts +7 -5
  20. package/dist/private/opcua_client_impl.js +24 -21
  21. package/dist/private/opcua_client_impl.js.map +1 -1
  22. package/dist/reconnection.js +2 -2
  23. package/dist/reconnection.js.map +1 -1
  24. package/dist/user_identity_info.d.ts +1 -0
  25. package/dist/user_identity_info.js +3 -0
  26. package/dist/user_identity_info.js.map +1 -0
  27. package/package.json +31 -31
  28. package/source/client_monitored_item_group.ts +4 -0
  29. package/source/client_session.ts +6 -0
  30. package/source/index.ts +1 -0
  31. package/source/opcua_client.ts +5 -20
  32. package/source/private/client_base_impl.ts +6 -1
  33. package/source/private/client_monitored_item_impl.ts +10 -4
  34. package/source/private/client_session_impl.ts +27 -1
  35. package/source/private/i_private_client.ts +6 -1
  36. package/source/private/opcua_client_impl.ts +64 -54
  37. package/source/reconnection.ts +32 -23
  38. package/source/user_identity_info.ts +6 -0
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=user_identity_info.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user_identity_info.js","sourceRoot":"","sources":["../source/user_identity_info.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-opcua-client",
3
- "version": "2.78.0",
3
+ "version": "2.79.0",
4
4
  "description": "pure nodejs OPCUA SDK - module -client",
5
5
  "scripts": {
6
6
  "clean": "npx rimraf node_modules dist *.tsbuildinfo certificates",
@@ -20,43 +20,43 @@
20
20
  "callbackify": "^1.1.0",
21
21
  "chalk": "4.1.2",
22
22
  "node-opcua-assert": "2.77.0",
23
- "node-opcua-basic-types": "2.77.0",
23
+ "node-opcua-basic-types": "2.79.0",
24
24
  "node-opcua-buffer-utils": "2.77.0",
25
25
  "node-opcua-certificate-manager": "2.77.0",
26
- "node-opcua-client-dynamic-extension-object": "2.78.0",
27
- "node-opcua-common": "2.78.0",
26
+ "node-opcua-client-dynamic-extension-object": "2.79.0",
27
+ "node-opcua-common": "2.79.0",
28
28
  "node-opcua-constants": "2.77.0",
29
29
  "node-opcua-crypto": "^1.11.0",
30
- "node-opcua-data-model": "2.78.0",
31
- "node-opcua-data-value": "2.78.0",
30
+ "node-opcua-data-model": "2.79.0",
31
+ "node-opcua-data-value": "2.79.0",
32
32
  "node-opcua-date-time": "2.77.0",
33
33
  "node-opcua-debug": "2.77.0",
34
- "node-opcua-extension-object": "2.78.0",
34
+ "node-opcua-extension-object": "2.79.0",
35
35
  "node-opcua-hostname": "2.77.0",
36
- "node-opcua-nodeid": "2.77.0",
36
+ "node-opcua-nodeid": "2.79.0",
37
37
  "node-opcua-object-registry": "2.77.0",
38
38
  "node-opcua-pki": "^2.17.0",
39
- "node-opcua-pseudo-session": "2.78.0",
40
- "node-opcua-schemas": "2.78.0",
41
- "node-opcua-secure-channel": "2.78.0",
42
- "node-opcua-service-browse": "2.78.0",
43
- "node-opcua-service-call": "2.78.0",
44
- "node-opcua-service-discovery": "2.78.0",
45
- "node-opcua-service-endpoints": "2.78.0",
46
- "node-opcua-service-filter": "2.78.0",
47
- "node-opcua-service-history": "2.78.0",
48
- "node-opcua-service-query": "2.78.0",
49
- "node-opcua-service-read": "2.78.0",
50
- "node-opcua-service-register-node": "2.78.0",
51
- "node-opcua-service-secure-channel": "2.78.0",
52
- "node-opcua-service-session": "2.78.0",
53
- "node-opcua-service-subscription": "2.78.0",
54
- "node-opcua-service-translate-browse-path": "2.78.0",
55
- "node-opcua-service-write": "2.78.0",
39
+ "node-opcua-pseudo-session": "2.79.0",
40
+ "node-opcua-schemas": "2.79.0",
41
+ "node-opcua-secure-channel": "2.79.0",
42
+ "node-opcua-service-browse": "2.79.0",
43
+ "node-opcua-service-call": "2.79.0",
44
+ "node-opcua-service-discovery": "2.79.0",
45
+ "node-opcua-service-endpoints": "2.79.0",
46
+ "node-opcua-service-filter": "2.79.0",
47
+ "node-opcua-service-history": "2.79.0",
48
+ "node-opcua-service-query": "2.79.0",
49
+ "node-opcua-service-read": "2.79.0",
50
+ "node-opcua-service-register-node": "2.79.0",
51
+ "node-opcua-service-secure-channel": "2.79.0",
52
+ "node-opcua-service-session": "2.79.0",
53
+ "node-opcua-service-subscription": "2.79.0",
54
+ "node-opcua-service-translate-browse-path": "2.79.0",
55
+ "node-opcua-service-write": "2.79.0",
56
56
  "node-opcua-status-code": "2.77.0",
57
- "node-opcua-types": "2.78.0",
57
+ "node-opcua-types": "2.79.0",
58
58
  "node-opcua-utils": "2.77.0",
59
- "node-opcua-variant": "2.78.0",
59
+ "node-opcua-variant": "2.79.0",
60
60
  "once": "^1.4.0",
61
61
  "thenify": "^3.3.1"
62
62
  },
@@ -64,14 +64,14 @@
64
64
  "@types/underscore": "^1.11.4",
65
65
  "dequeue": "^1.0.5",
66
66
  "mocha": "^10.0.0",
67
- "node-opcua-address-space": "2.78.0",
67
+ "node-opcua-address-space": "2.79.0",
68
68
  "node-opcua-binary-stream": "2.77.0",
69
- "node-opcua-factory": "2.78.0",
69
+ "node-opcua-factory": "2.79.0",
70
70
  "node-opcua-leak-detector": "2.77.0",
71
71
  "node-opcua-nodesets": "2.77.0",
72
72
  "should": "^13.2.3",
73
73
  "sinon": "^14.0.0",
74
- "underscore": "^1.13.4"
74
+ "underscore": "^1.13.6"
75
75
  },
76
76
  "eslintConfig": {
77
77
  "env": {
@@ -93,5 +93,5 @@
93
93
  "internet of things"
94
94
  ],
95
95
  "homepage": "http://node-opcua.github.io/",
96
- "gitHead": "31c6eee3c51f52427584364fff2fdb07bbac65be"
96
+ "gitHead": "1a6d41827954dccf2c2cdff9344cc11fc99767f3"
97
97
  }
@@ -20,6 +20,10 @@ export interface ClientMonitoredItemGroup extends EventEmitter, ClientMonitoredI
20
20
  on(event: "initialized", eventHandler: () => void): this;
21
21
  }
22
22
 
23
+ export interface ClientMonitoredItemGroup {
24
+ monitoredItems: ClientMonitoredItemBase[];
25
+ }
26
+
23
27
  export class ClientMonitoredItemGroup {
24
28
  public static create(
25
29
  subscription: ClientSubscription,
@@ -56,6 +56,7 @@ import { WriteValue, WriteValueOptions } from "node-opcua-service-write";
56
56
  import { StatusCode } from "node-opcua-status-code";
57
57
  import { DataType, Variant } from "node-opcua-variant";
58
58
  import { Callback } from "node-opcua-status-code";
59
+ import { IBasicSessionChangeUser } from "node-opcua-pseudo-session";
59
60
  import { ExtensionObject } from "node-opcua-extension-object";
60
61
  import { ArgumentDefinition, CallMethodRequestLike, MethodId } from "node-opcua-pseudo-session";
61
62
  import { AggregateFunction } from "node-opcua-constants";
@@ -423,6 +424,10 @@ export interface ClientSessionSubscriptionService {
423
424
  ): void;
424
425
  }
425
426
 
427
+ export interface ClientSessionChangeUser extends IBasicSessionChangeUser {
428
+ /* */
429
+ }
430
+
426
431
  export interface HistoryReadValueIdOptions2 extends HistoryReadValueIdOptions {
427
432
  nodeId: NodeIdLike; // nodeId is mandatory here
428
433
  }
@@ -790,4 +795,5 @@ export interface ClientSession
790
795
  ClientSessionExtensionObjectService,
791
796
  ClientSessionNamespaceService,
792
797
  ClientSessionDataTypeService,
798
+ ClientSessionChangeUser,
793
799
  IBasicSession {}
package/source/index.ts CHANGED
@@ -20,6 +20,7 @@ export * from "./tools/findservers";
20
20
  export * from "./tools/read_history_server_capabilities";
21
21
  export * from "./client_utils";
22
22
  export * from "./verify";
23
+ export * from "./user_identity_info";
23
24
 
24
25
  export { assert } from "node-opcua-assert";
25
26
  export * from "node-opcua-utils";
@@ -8,29 +8,14 @@ import { ConnectionStrategyOptions, Message, SecurityPolicy } from "node-opcua-s
8
8
  import { ApplicationDescription, EndpointDescription, UserTokenType } from "node-opcua-service-endpoints";
9
9
  import { MessageSecurityMode } from "node-opcua-service-secure-channel";
10
10
  import { X509IdentityTokenOptions } from "node-opcua-types";
11
- import { ErrorCallback } from "node-opcua-status-code";
11
+ import { CallbackT, ErrorCallback, StatusCode } from "node-opcua-status-code";
12
12
  import { FindServersRequestLike, GetEndpointsOptions, OPCUAClientBase, OPCUAClientBaseOptions } from "./client_base";
13
13
 
14
14
  import { ClientSession, ResponseCallback } from "./client_session";
15
15
  import { ClientSubscription, ClientSubscriptionOptions } from "./client_subscription";
16
16
  import { OPCUAClientImpl } from "./private/opcua_client_impl";
17
+ import { UserIdentityInfo } from "./user_identity_info";
17
18
 
18
- export interface UserIdentityInfoUserName {
19
- type: UserTokenType.UserName;
20
- userName: string;
21
- password: string;
22
- }
23
-
24
- export interface UserIdentityInfoX509 extends X509IdentityTokenOptions {
25
- type: UserTokenType.Certificate;
26
- certificateData: ByteString;
27
- privateKey: PrivateKeyPEM;
28
- }
29
- export interface AnonymousIdentity {
30
- type: UserTokenType.Anonymous;
31
- }
32
-
33
- export type UserIdentityInfo = AnonymousIdentity | UserIdentityInfoX509 | UserIdentityInfoUserName;
34
19
 
35
20
  export interface OPCUAClientOptions extends OPCUAClientBaseOptions {
36
21
  /**
@@ -136,9 +121,9 @@ export interface OPCUAClient extends OPCUAClientBase {
136
121
 
137
122
  createSession2(callback: (err: Error | null, session?: ClientSession) => void): void;
138
123
 
139
- changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo): Promise<void>;
140
-
141
- changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo, callback: (err?: Error) => void): void;
124
+ /** @deprecated */
125
+ changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo): Promise<StatusCode>;
126
+ changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo, callback: CallbackT<StatusCode>): void;
142
127
 
143
128
  closeSession(session: ClientSession, deleteSubscriptions: boolean): Promise<void>;
144
129
 
@@ -64,6 +64,7 @@ import {
64
64
  OPCUAClientBaseOptions,
65
65
  TransportSettings
66
66
  } from "../client_base";
67
+ import { UserIdentityInfo } from "../user_identity_info";
67
68
  import { performCertificateSanityCheck } from "../verify";
68
69
  import { ClientSessionImpl } from "./client_session_impl";
69
70
  import { IClientBase } from "./i_private_client";
@@ -1646,7 +1647,11 @@ export class ClientBaseImpl extends OPCUASecureObject implements OPCUAClientBase
1646
1647
  ): void {
1647
1648
  throw new Error("Please override");
1648
1649
  }
1649
- public _activateSession(session: ClientSessionImpl, callback: (err: Error | null, session?: ClientSessionImpl) => void): void {
1650
+ public _activateSession(
1651
+ session: ClientSessionImpl,
1652
+ userIdentity: UserIdentityInfo,
1653
+ callback: (err: Error | null, session?: ClientSessionImpl) => void
1654
+ ): void {
1650
1655
  throw new Error("Please override");
1651
1656
  }
1652
1657
  }
@@ -193,8 +193,11 @@ export class ClientMonitoredItemImpl extends EventEmitter implements ClientMonit
193
193
  try {
194
194
  this.emit("changed", value);
195
195
  } catch (err) {
196
- debugLog("Exception raised inside the event handler called by ClientMonitoredItem.on('change')", err);
197
- debugLog("Please verify the application using this node-opcua client");
196
+ warningLog(
197
+ "[NODE-OPCUA-W28] Exception raised inside the event handler called by ClientMonitoredItem.on('change')",
198
+ err
199
+ );
200
+ warningLog(" Please verify the application using this node-opcua client");
198
201
  }
199
202
  }
200
203
 
@@ -217,8 +220,11 @@ export class ClientMonitoredItemImpl extends EventEmitter implements ClientMonit
217
220
  try {
218
221
  this.emit("changed", eventFields);
219
222
  } catch (err) {
220
- warningLog("Exception raised inside the event handler called by ClientMonitoredItem.on('change')", err);
221
- warningLog("Please verify the application using this node-opcua client");
223
+ warningLog(
224
+ "[NODE-OPCUA-W29] Exception raised inside the event handler called by ClientMonitoredItem.on('change')",
225
+ err
226
+ );
227
+ warningLog(" Please verify the application using this node-opcua client");
222
228
  }
223
229
  }
224
230
 
@@ -87,7 +87,7 @@ import {
87
87
  BrowseNextRequest,
88
88
  BrowseNextResponse,
89
89
  HistoryReadValueIdOptions,
90
- ServiceFault,
90
+ UserTokenType,
91
91
  WriteValueOptions
92
92
  } from "node-opcua-types";
93
93
  import { buffer_ellipsis, getFunctionParameterNames, isNullOrUndefined, lowerFirstLetter } from "node-opcua-utils";
@@ -118,6 +118,7 @@ import { ClientSessionKeepAliveManager } from "../client_session_keepalive_manag
118
118
  import { ClientSubscription } from "../client_subscription";
119
119
  import { Request, Response } from "../common";
120
120
  import { repair_client_session } from "../reconnection";
121
+ import { UserIdentityInfo } from "../user_identity_info";
121
122
 
122
123
  import { ClientSidePublishEngine } from "./client_publish_engine";
123
124
  import { ClientSubscriptionImpl } from "./client_subscription_impl";
@@ -239,6 +240,7 @@ export class ClientSessionImpl extends EventEmitter implements ClientSession {
239
240
  public lastRequestSentTime: Date;
240
241
  public lastResponseReceivedTime: Date;
241
242
  public serverCertificate: Certificate;
243
+ public userIdentityInfo?: UserIdentityInfo;
242
244
  public name = "";
243
245
 
244
246
  public serverNonce?: Nonce;
@@ -313,6 +315,29 @@ export class ClientSessionImpl extends EventEmitter implements ClientSession {
313
315
  return this._publishEngine!;
314
316
  }
315
317
 
318
+ public changeUser(userIdentityInfo: UserIdentityInfo): Promise<StatusCode>;
319
+ public changeUser(userIdentityInfo: UserIdentityInfo, callback: CallbackT<StatusCode>): void;
320
+ public changeUser(userIdentityInfo: UserIdentityInfo, callback?: CallbackT<StatusCode>): any {
321
+ userIdentityInfo = userIdentityInfo || {
322
+ type: UserTokenType.Anonymous
323
+ };
324
+ if (!this._client || !this.userIdentityInfo) {
325
+ warningLog("changeUser: invalid session");
326
+ return callback!(null, StatusCodes.BadInternalError);
327
+ }
328
+
329
+ const old_userIdentity: UserIdentityInfo = this.userIdentityInfo;
330
+
331
+ this._client._activateSession(this, userIdentityInfo, (err1: Error | null, session2?: ClientSessionImpl) => {
332
+ if (err1) {
333
+ this.userIdentityInfo = old_userIdentity;
334
+ console.log("err1 = ", err1.message);
335
+ return callback!(null, StatusCodes.BadUserAccessDenied);
336
+ }
337
+ this.userIdentityInfo = userIdentityInfo;
338
+ callback!(null, StatusCodes.Good);
339
+ });
340
+ }
316
341
  /**
317
342
  * @method browse
318
343
  * @async
@@ -2321,3 +2346,4 @@ ClientSessionImpl.prototype.unregisterNodes = thenify.withCallback(ClientSession
2321
2346
  ClientSessionImpl.prototype.readNamespaceArray = thenify.withCallback(ClientSessionImpl.prototype.readNamespaceArray, opts);
2322
2347
  ClientSessionImpl.prototype.getBuiltInDataType = thenify.withCallback(ClientSessionImpl.prototype.getBuiltInDataType, opts);
2323
2348
  ClientSessionImpl.prototype.constructExtensionObject = callbackify(ClientSessionImpl.prototype.constructExtensionObject);
2349
+ ClientSessionImpl.prototype.changeUser = thenify.withCallback(ClientSessionImpl.prototype.changeUser, opts);
@@ -2,11 +2,16 @@ import { ClientSecureChannelLayer } from "node-opcua-secure-channel";
2
2
  import { EndpointDescription } from "node-opcua-service-endpoints";
3
3
  import { ClientSession, ResponseCallback } from "../client_session";
4
4
  import { Request, Response } from "../common";
5
+ import { UserIdentityInfo } from "../user_identity_info";
5
6
  import { ClientSessionImpl } from "./client_session_impl";
6
7
 
7
8
  export interface IClientBase {
8
9
  __createSession_step2(session: ClientSessionImpl, callback: (err: Error | null, session?: ClientSessionImpl) => void): void;
9
- _activateSession(session: ClientSessionImpl, callback: (err: Error | null, session?: ClientSessionImpl) => void): void;
10
+ _activateSession(
11
+ session: ClientSessionImpl,
12
+ userIdentity: UserIdentityInfo,
13
+ callback: (err: Error | null, session?: ClientSessionImpl) => void
14
+ ): void;
10
15
  _removeSession(session: ClientSessionImpl): void;
11
16
  closeSession(session: ClientSession, deleteSubscription: any, arg2: (err?: Error | undefined) => void): void;
12
17
 
@@ -38,7 +38,7 @@ import {
38
38
  UserNameIdentityToken,
39
39
  X509IdentityToken
40
40
  } from "node-opcua-service-session";
41
- import { StatusCodes } from "node-opcua-status-code";
41
+ import { CallbackT, StatusCode, StatusCodes } from "node-opcua-status-code";
42
42
  import { ErrorCallback, Callback } from "node-opcua-status-code";
43
43
 
44
44
  import { SignatureData, SignatureDataOptions, UserIdentityToken } from "node-opcua-types";
@@ -48,16 +48,14 @@ import { ClientSession } from "../client_session";
48
48
  import { ClientSubscription, ClientSubscriptionOptions } from "../client_subscription";
49
49
  import { Response } from "../common";
50
50
  import {
51
- AnonymousIdentity,
52
51
  OPCUAClient,
53
52
  OPCUAClientOptions,
54
- UserIdentityInfo,
55
- UserIdentityInfoUserName,
56
- UserIdentityInfoX509,
57
53
  WithSessionFuncP,
58
54
  WithSubscriptionFuncP,
59
55
  EndpointWithUserIdentity
60
56
  } from "../opcua_client";
57
+ import { AnonymousIdentity, UserIdentityInfo, UserIdentityInfoUserName, UserIdentityInfoX509 } from "../user_identity_info";
58
+
61
59
  import { repair_client_sessions } from "../reconnection";
62
60
  import { ClientBaseImpl } from "./client_base_impl";
63
61
  import { ClientSessionImpl } from "./client_session_impl";
@@ -332,7 +330,6 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
332
330
  private endpointMustExist: boolean;
333
331
  private requestedSessionTimeout: number;
334
332
  private ___sessionName_counter: number;
335
- private userIdentityInfo: UserIdentityInfo;
336
333
  private serverUri?: string;
337
334
  private clientNonce?: Nonce;
338
335
 
@@ -358,7 +355,6 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
358
355
  this.requestedSessionTimeout = options.requestedSessionTimeout || 60000; // 1 minute
359
356
 
360
357
  this.___sessionName_counter = 0;
361
- this.userIdentityInfo = { type: UserTokenType.Anonymous };
362
358
  this.endpoint = undefined;
363
359
  }
364
360
 
@@ -396,8 +392,6 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
396
392
  const userIdentityInfo = args[0] || { type: UserTokenType.Anonymous };
397
393
  const callback = args[1];
398
394
 
399
- this.userIdentityInfo = userIdentityInfo;
400
-
401
395
  assert(typeof callback === "function");
402
396
 
403
397
  this._createSession((err: Error | null, session?: ClientSession) => {
@@ -411,9 +405,13 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
411
405
 
412
406
  this._addSession(session as ClientSessionImpl);
413
407
 
414
- this._activateSession(session as ClientSessionImpl, (err1: Error | null, session2?: ClientSessionImpl) => {
415
- callback(err1, session2);
416
- });
408
+ this._activateSession(
409
+ session as ClientSessionImpl,
410
+ userIdentityInfo,
411
+ (err1: Error | null, session2?: ClientSessionImpl) => {
412
+ callback(err1, session2);
413
+ }
414
+ );
417
415
  }
418
416
  });
419
417
  }
@@ -460,26 +458,19 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
460
458
  * @param userIdentityInfo
461
459
  * @param callback
462
460
  * @async
461
+ * @deprecated : use session.changeUser instead
463
462
  */
464
- public async changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo): Promise<void>;
465
- public changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo, callback: (err?: Error) => void): void;
466
- /**
467
- * @internal
468
- * @param args
469
- */
463
+ public async changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo): Promise<StatusCode>;
464
+ public changeSessionIdentity(session: ClientSession, userIdentityInfo: UserIdentityInfo, callback: CallbackT<StatusCode>): void;
470
465
  public changeSessionIdentity(...args: any[]): any {
466
+ warningLog(
467
+ "[NODE-OPCUA-W27] OPCUAClient.changeSessionIdentity(session,...) is deprecated use ClientSession.changeUser instead"
468
+ );
471
469
  const session = args[0] as ClientSessionImpl;
472
470
  const userIdentityInfo = args[1] as UserIdentityInfo;
473
471
  const callback = args[2];
474
-
475
472
  assert(typeof callback === "function");
476
-
477
- const old_userIdentity = this.userIdentityInfo;
478
- this.userIdentityInfo = userIdentityInfo;
479
-
480
- this._activateSession(session as ClientSessionImpl, (err1: Error | null, session1?: ClientSessionImpl) => {
481
- callback(err1 ? err1 : undefined);
482
- });
473
+ session.changeUser(userIdentityInfo, callback);
483
474
  }
484
475
 
485
476
  /**
@@ -706,19 +697,29 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
706
697
  typeof connectionPoint === "string" ? { type: UserTokenType.Anonymous } : connectionPoint.userIdentity;
707
698
 
708
699
  await this.connect(endpointUrl);
709
- const session = await this.createSession2(userIdentity);
710
700
 
711
- let result;
701
+ this.on("backoff", (count, delay) => {
702
+ warningLog("cannot connect to ", endpointUrl, "attempt #" + count, " retrying in ", delay);
703
+ });
704
+
712
705
  try {
713
- result = await func(session);
706
+ const session = await this.createSession2(userIdentity);
707
+ let result;
708
+ try {
709
+ result = await func(session);
710
+ return result;
711
+ } catch (err) {
712
+ errorLog(err);
713
+ throw err;
714
+ } finally {
715
+ await session.close();
716
+ }
714
717
  } catch (err) {
715
- errorLog(err);
718
+ errorLog((err as Error).message);
716
719
  throw err;
717
720
  } finally {
718
- await session.close();
719
721
  await this.disconnect();
720
722
  }
721
- return result;
722
723
  }
723
724
 
724
725
  public async withSubscriptionAsync(
@@ -771,29 +772,33 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
771
772
 
772
773
  debugLog("OPCUAClientImpl#reactivateSession");
773
774
 
774
- this._activateSession(internalSession, (err: Error | null /*, newSession?: ClientSessionImpl*/) => {
775
- if (!err) {
776
- if (old_client !== this) {
777
- // remove session from old client:
778
- if (old_client) {
779
- old_client._removeSession(internalSession);
780
- assert(old_client._sessions.indexOf(internalSession) === -1);
781
- }
775
+ this._activateSession(
776
+ internalSession,
777
+ internalSession.userIdentityInfo!,
778
+ (err: Error | null /*, newSession?: ClientSessionImpl*/) => {
779
+ if (!err) {
780
+ if (old_client !== this) {
781
+ // remove session from old client:
782
+ if (old_client) {
783
+ old_client._removeSession(internalSession);
784
+ assert(old_client._sessions.indexOf(internalSession) === -1);
785
+ }
782
786
 
783
- this._addSession(internalSession);
784
- assert(internalSession._client === this);
785
- assert(!internalSession._closed, "session should not vbe closed");
786
- assert(this._sessions.indexOf(internalSession) !== -1);
787
- }
788
- callback!();
789
- } else {
790
- // istanbul ignore next
791
- if (doDebug) {
792
- debugLog(chalk.red.bgWhite("reactivateSession has failed !"), err.message);
787
+ this._addSession(internalSession);
788
+ assert(internalSession._client === this);
789
+ assert(!internalSession._closed, "session should not vbe closed");
790
+ assert(this._sessions.indexOf(internalSession) !== -1);
791
+ }
792
+ callback!();
793
+ } else {
794
+ // istanbul ignore next
795
+ if (doDebug) {
796
+ debugLog(chalk.red.bgWhite("reactivateSession has failed !"), err.message);
797
+ }
798
+ callback!(err);
793
799
  }
794
- callback!(err);
795
800
  }
796
- });
801
+ );
797
802
  }
798
803
 
799
804
  /**
@@ -939,7 +944,11 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
939
944
  * @internal
940
945
  * @private
941
946
  */
942
- public _activateSession(session: ClientSessionImpl, callback: (err: Error | null, session?: ClientSessionImpl) => void): void {
947
+ public _activateSession(
948
+ session: ClientSessionImpl,
949
+ userIdentityInfo: UserIdentityInfo,
950
+ callback: (err: Error | null, session?: ClientSessionImpl) => void
951
+ ): void {
943
952
  // see OPCUA Part 4 - $7.35
944
953
  assert(typeof callback === "function");
945
954
 
@@ -968,7 +977,7 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
968
977
  serverNonce: serverNonce! // please check this !
969
978
  };
970
979
 
971
- this.createUserIdentityToken(context, this.userIdentityInfo, (err: Error | null, data?: TokenAndSignature | null) => {
980
+ this.createUserIdentityToken(context, userIdentityInfo, (err: Error | null, data?: TokenAndSignature | null) => {
972
981
  if (err) {
973
982
  session._client = _old_client;
974
983
  return callback(err);
@@ -1044,6 +1053,7 @@ export class OPCUAClientImpl extends ClientBaseImpl implements OPCUAClient {
1044
1053
  if (this.keepSessionAlive) {
1045
1054
  session.startKeepAliveManager();
1046
1055
  }
1056
+ session.userIdentityInfo = userIdentityInfo;
1047
1057
  return callback(null, session);
1048
1058
  } else {
1049
1059
  // restore client
@@ -237,28 +237,32 @@ function repair_client_session_by_recreating_a_new_session(
237
237
  }
238
238
  debugLog(chalk.bgWhite.red(" => activating a new session ...."));
239
239
 
240
- client._activateSession(newSession, (err: Error | null, session1?: ClientSessionImpl) => {
241
- debugLog(chalk.bgWhite.cyan(" => activating a new session .... Done err=", err ? err.message : "null"));
242
- if (err) {
243
- debugLog(
244
- chalk.bgWhite.cyan(
245
- "reactivation of the new session has failed: let be smart and close it before failing this repair attempt"
246
- )
247
- );
248
- // but just on the server side, not on the client side
249
- const closeSessionRequest = new CloseSessionRequest({
250
- deleteSubscriptions: true
251
- });
252
- session.performMessageTransaction(closeSessionRequest, (err2?: Error | null) => {
253
- if (err2) {
254
- warningLog("closing session", err2.message);
255
- }
256
- innerCallback(err);
257
- });
258
- } else {
259
- innerCallback(err ? err : undefined);
240
+ client._activateSession(
241
+ newSession,
242
+ newSession.userIdentityInfo!,
243
+ (err: Error | null, session1?: ClientSessionImpl) => {
244
+ debugLog(chalk.bgWhite.cyan(" => activating a new session .... Done err=", err ? err.message : "null"));
245
+ if (err) {
246
+ debugLog(
247
+ chalk.bgWhite.cyan(
248
+ "reactivation of the new session has failed: let be smart and close it before failing this repair attempt"
249
+ )
250
+ );
251
+ // but just on the server side, not on the client side
252
+ const closeSessionRequest = new CloseSessionRequest({
253
+ deleteSubscriptions: true
254
+ });
255
+ session.performMessageTransaction(closeSessionRequest, (err2?: Error | null) => {
256
+ if (err2) {
257
+ warningLog("closing session", err2.message);
258
+ }
259
+ innerCallback(err);
260
+ });
261
+ } else {
262
+ innerCallback(err ? err : undefined);
263
+ }
260
264
  }
261
- });
265
+ );
262
266
  },
263
267
 
264
268
  function attempt_subscription_transfer(innerCallback: ErrorCallback) {
@@ -415,7 +419,7 @@ function _repair_client_session(client: IClientBase, session: ClientSessionImpl,
415
419
  debugLog(chalk.yellow(" TRYING TO REACTIVATE EXISTING SESSION"), session.sessionId.toString());
416
420
  debugLog(" SubscriptionIds :", session.getPublishEngine().getSubscriptionIds());
417
421
  }
418
- client._activateSession(session, (err: Error | null, session2?: ClientSessionImpl) => {
422
+ client._activateSession(session, session.userIdentityInfo!, (err: Error | null, session2?: ClientSessionImpl) => {
419
423
  //
420
424
  // Note: current limitation :
421
425
  // - The reconnection doesn't work yet, if connection break is caused by a server that crashes and restarts.
@@ -454,7 +458,12 @@ export function repair_client_session(client: IClientBase, session: ClientSessio
454
458
  _repair_client_session(client, session, (err) => {
455
459
  privateSession._reconnecting.reconnecting = false;
456
460
  if (err) {
457
- errorLog(chalk.red("session restoration has failed! err ="), err.message, session.sessionId.toString(), " => Let's retry");
461
+ errorLog(
462
+ chalk.red("session restoration has failed! err ="),
463
+ err.message,
464
+ session.sessionId.toString(),
465
+ " => Let's retry"
466
+ );
458
467
  setTimeout(() => {
459
468
  _repair_client_session(client, session, callback);
460
469
  }, 2000);
@@ -0,0 +1,6 @@
1
+ export {
2
+ UserIdentityInfoUserName,
3
+ UserIdentityInfoX509,
4
+ AnonymousIdentity,
5
+ UserIdentityInfo
6
+ } from "node-opcua-pseudo-session";