opcjs-client 0.1.18-alpha → 0.1.26-alpha

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.
package/dist/index.d.cts CHANGED
@@ -1,8 +1,19 @@
1
- import { ISecureChannel, RequestHeader, NodeId, EndpointDescription, UserIdentityToken, Configuration, UserTokenTypeEnum, ILoggerFactory, Encoder, Decoder, ExpandedNodeId, QualifiedName, LocalizedText, NodeClassEnum } from 'opcjs-base';
1
+ import { ISecureChannel, RequestHeader, NodeId, EndpointDescription, UserIdentityToken, Configuration, UserTokenTypeEnum, ILoggerFactory, Encoder, Decoder, ExpandedNodeId, QualifiedName, LocalizedText, NodeClassEnum, UaPrimitive } from 'opcjs-base';
2
2
 
3
3
  declare abstract class ServiceBase {
4
4
  private authToken;
5
5
  protected secureChannel: ISecureChannel;
6
+ /**
7
+ * Validates the `serviceResult` value from a response header.
8
+ *
9
+ * Throws `SessionInvalidError` for session-related status codes so callers
10
+ * can detect a dropped session and act accordingly (e.g. reconnect).
11
+ * Throws a generic `Error` for all other non-Good codes.
12
+ *
13
+ * @param result - The `serviceResult` value from the response header.
14
+ * @param context - Short description used in the error message (e.g. "ReadRequest").
15
+ */
16
+ protected checkServiceResult(result: number | undefined, context: string): void;
6
17
  protected createRequestHeader(): RequestHeader;
7
18
  constructor(authToken: NodeId, secureChannel: ISecureChannel);
8
19
  }
@@ -10,11 +21,19 @@ declare abstract class ServiceBase {
10
21
  declare class SessionService extends ServiceBase {
11
22
  private configuration;
12
23
  private logger;
24
+ /**
25
+ * Creates a new session on the server (OPC UA Part 4, Section 5.7.2).
26
+ * @returns The session ID, authentication token, and selected server endpoint.
27
+ */
13
28
  createSession(): Promise<{
14
29
  sessionId: number;
15
30
  authToken: NodeId;
16
31
  endpoint: EndpointDescription;
17
32
  }>;
33
+ /**
34
+ * Activates an existing session using the supplied identity token (OPC UA Part 4, Section 5.7.3).
35
+ * @param identityToken - User identity token (anonymous, username/password, certificate, or issued token).
36
+ */
18
37
  activateSession(identityToken: UserIdentityToken): Promise<void>;
19
38
  recreate(authToken: NodeId): SessionService;
20
39
  constructor(authToken: NodeId, secureChannel: ISecureChannel, configuration: Configuration);
@@ -70,8 +89,8 @@ declare class Session {
70
89
 
71
90
  declare class ReadValueResult {
72
91
  value: unknown;
73
- status: string;
74
- constructor(value: unknown, status: string);
92
+ statusCode: number;
93
+ constructor(value: unknown, statusCode: number);
75
94
  }
76
95
 
77
96
  declare class ConfigurationClient extends Configuration {
@@ -81,8 +100,8 @@ declare class ConfigurationClient extends Configuration {
81
100
 
82
101
  declare class CallMethodResult {
83
102
  values: unknown[];
84
- status: string;
85
- constructor(values: unknown[], status: string);
103
+ statusCode: number;
104
+ constructor(values: unknown[], statusCode: number);
86
105
  }
87
106
 
88
107
  declare class BrowseNodeResult {
@@ -96,116 +115,6 @@ declare class BrowseNodeResult {
96
115
  constructor(referenceTypeId: NodeId, isForward: boolean, nodeId: ExpandedNodeId, browseName: QualifiedName, displayName: LocalizedText, nodeClass: NodeClassEnum, typeDefinition: ExpandedNodeId);
97
116
  }
98
117
 
99
- /**
100
- * Enumeration of variant types based on BuiltinType NodeIds.
101
- */
102
- declare enum BuiltInType {
103
- Null = 0,
104
- Boolean = 1,
105
- SByte = 2,
106
- Byte = 3,
107
- Int16 = 4,
108
- UInt16 = 5,
109
- Int32 = 6,
110
- UInt32 = 7,
111
- Int64 = 8,
112
- UInt64 = 9,
113
- Float = 10,
114
- Double = 11,
115
- String = 12,
116
- DateTime = 13,
117
- Guid = 14,
118
- ByteString = 15,
119
- XmlElement = 16,
120
- NodeId = 17,
121
- ExpandedNodeId = 18,
122
- StatusCode = 19,
123
- QualifiedName = 20,
124
- LocalizedText = 21,
125
- ExtensionObject = 22,
126
- DataValue = 23,
127
- Variant = 24,
128
- DiagnosticInfo = 25
129
- }
130
-
131
- /**
132
- * OPC UA Primitive Type Mappings
133
- *
134
- * We try to use the JS built in types wherever we can. But sometimes, often in the context
135
- * where Variant is involved, we need to have a wrapper type that includes the BuiltInType
136
- * for type checking and encoding purposes. In those cases, we define a wrapper type with a
137
- * value and a type field. For simple cases like boolean, we can just use the native boolean
138
- * type directly as UaBoolean.
139
- * @module primitives
140
- */
141
-
142
- type UaBoolean = boolean;
143
- /**
144
- * OPC UA Builtin Type Numeric IDs
145
- *
146
- * These correspond to the NodeId numeric identifiers defined in OPC UA Part 6, Table 1.
147
- */
148
- type UaSbyte = {
149
- value: number;
150
- readonly type: BuiltInType.SByte;
151
- };
152
- type UaByte = {
153
- value: number;
154
- readonly type: BuiltInType.Byte;
155
- };
156
- type UaInt16 = {
157
- value: number;
158
- readonly type: BuiltInType.Int16;
159
- };
160
- type UaUint16 = {
161
- value: number;
162
- readonly type: BuiltInType.UInt16;
163
- };
164
- type UaInt32 = {
165
- value: number;
166
- readonly type: BuiltInType.Int32;
167
- };
168
- type UaUint32 = {
169
- value: number;
170
- readonly type: BuiltInType.UInt32;
171
- };
172
- type UaInt64 = {
173
- value: bigint;
174
- readonly type: BuiltInType.Int64;
175
- };
176
- type UaUint64 = {
177
- value: bigint;
178
- readonly type: BuiltInType.UInt64;
179
- };
180
- type UaFloat = {
181
- value: number;
182
- readonly type: BuiltInType.Float;
183
- };
184
- type UaDouble = {
185
- value: number;
186
- readonly type: BuiltInType.Double;
187
- };
188
- /**
189
- * OPC UA String primitive type.
190
- *
191
- * A String in OPC UA can be null (encoded as length -1 in binary).
192
- * Using `string | null` instead of `string | undefined` aligns with the
193
- * OPC UA specification where null is an explicit, valid value distinct
194
- * from an empty string. Same is true for ByteString which can be null (length -1)
195
- * or a Uint8Array.
196
- */
197
- type UaString = string | null;
198
- type UaByteString = Uint8Array | null;
199
- type UaGuid = {
200
- value: string;
201
- readonly type: BuiltInType.Guid;
202
- };
203
- type UaDateTime = Date;
204
- /**
205
- * Union of all OPC UA primitive types accepted by {@link Variant.newFrom}.
206
- */
207
- type UaPrimitive = UaBoolean | UaSbyte | UaByte | UaInt16 | UaUint16 | UaInt32 | UaUint32 | UaInt64 | UaUint64 | UaFloat | UaDouble | UaString | UaDateTime | UaGuid | UaByteString;
208
-
209
118
  declare class Client {
210
119
  private configuration;
211
120
  private identity;
@@ -216,7 +125,23 @@ declare class Client {
216
125
  private session?;
217
126
  private subscriptionHandler?;
218
127
  private logger;
128
+ private secureChannel?;
129
+ private sessionHandler?;
219
130
  getSession(): Session;
131
+ /**
132
+ * (Re-)initialises all session-scoped services from the current `this.session`.
133
+ * Called both after the initial `connect()` and after a session refresh.
134
+ */
135
+ private initServices;
136
+ /**
137
+ * Executes `fn` and, if it throws a `SessionInvalidError`, creates a fresh
138
+ * session and retries the operation exactly once.
139
+ *
140
+ * This covers the reactive case: a service call reveals that the server has
141
+ * already dropped the session (e.g. due to timeout). The new session is
142
+ * established transparently before re-running the original operation.
143
+ */
144
+ private withSessionRefresh;
220
145
  connect(): Promise<void>;
221
146
  disconnect(): Promise<void>;
222
147
  read(ids: NodeId[]): Promise<ReadValueResult[]>;
@@ -237,4 +162,20 @@ declare class Client {
237
162
  constructor(endpointUrl: string, configuration: ConfigurationClient, identity: UserIdentity);
238
163
  }
239
164
 
240
- export { BrowseNodeResult, Client, ConfigurationClient, UserIdentity };
165
+ /**
166
+ * Thrown when the server signals that the current Session is no longer valid.
167
+ *
168
+ * Callers that want to react to a dropped session (e.g. by creating a new one
169
+ * and retrying the operation) should catch this specific error type instead of
170
+ * the generic `Error` class.
171
+ *
172
+ * Relevant OPC UA status codes:
173
+ * - `BadSessionIdInvalid` (0x80250000) – session no longer exists on the server
174
+ * - `BadSessionClosed` (0x80260000) – session was closed explicitly
175
+ */
176
+ declare class SessionInvalidError extends Error {
177
+ readonly statusCode: number;
178
+ constructor(statusCode: number);
179
+ }
180
+
181
+ export { BrowseNodeResult, Client, ConfigurationClient, SessionInvalidError, UserIdentity };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,19 @@
1
- import { ISecureChannel, RequestHeader, NodeId, EndpointDescription, UserIdentityToken, Configuration, UserTokenTypeEnum, ILoggerFactory, Encoder, Decoder, ExpandedNodeId, QualifiedName, LocalizedText, NodeClassEnum } from 'opcjs-base';
1
+ import { ISecureChannel, RequestHeader, NodeId, EndpointDescription, UserIdentityToken, Configuration, UserTokenTypeEnum, ILoggerFactory, Encoder, Decoder, ExpandedNodeId, QualifiedName, LocalizedText, NodeClassEnum, UaPrimitive } from 'opcjs-base';
2
2
 
3
3
  declare abstract class ServiceBase {
4
4
  private authToken;
5
5
  protected secureChannel: ISecureChannel;
6
+ /**
7
+ * Validates the `serviceResult` value from a response header.
8
+ *
9
+ * Throws `SessionInvalidError` for session-related status codes so callers
10
+ * can detect a dropped session and act accordingly (e.g. reconnect).
11
+ * Throws a generic `Error` for all other non-Good codes.
12
+ *
13
+ * @param result - The `serviceResult` value from the response header.
14
+ * @param context - Short description used in the error message (e.g. "ReadRequest").
15
+ */
16
+ protected checkServiceResult(result: number | undefined, context: string): void;
6
17
  protected createRequestHeader(): RequestHeader;
7
18
  constructor(authToken: NodeId, secureChannel: ISecureChannel);
8
19
  }
@@ -10,11 +21,19 @@ declare abstract class ServiceBase {
10
21
  declare class SessionService extends ServiceBase {
11
22
  private configuration;
12
23
  private logger;
24
+ /**
25
+ * Creates a new session on the server (OPC UA Part 4, Section 5.7.2).
26
+ * @returns The session ID, authentication token, and selected server endpoint.
27
+ */
13
28
  createSession(): Promise<{
14
29
  sessionId: number;
15
30
  authToken: NodeId;
16
31
  endpoint: EndpointDescription;
17
32
  }>;
33
+ /**
34
+ * Activates an existing session using the supplied identity token (OPC UA Part 4, Section 5.7.3).
35
+ * @param identityToken - User identity token (anonymous, username/password, certificate, or issued token).
36
+ */
18
37
  activateSession(identityToken: UserIdentityToken): Promise<void>;
19
38
  recreate(authToken: NodeId): SessionService;
20
39
  constructor(authToken: NodeId, secureChannel: ISecureChannel, configuration: Configuration);
@@ -70,8 +89,8 @@ declare class Session {
70
89
 
71
90
  declare class ReadValueResult {
72
91
  value: unknown;
73
- status: string;
74
- constructor(value: unknown, status: string);
92
+ statusCode: number;
93
+ constructor(value: unknown, statusCode: number);
75
94
  }
76
95
 
77
96
  declare class ConfigurationClient extends Configuration {
@@ -81,8 +100,8 @@ declare class ConfigurationClient extends Configuration {
81
100
 
82
101
  declare class CallMethodResult {
83
102
  values: unknown[];
84
- status: string;
85
- constructor(values: unknown[], status: string);
103
+ statusCode: number;
104
+ constructor(values: unknown[], statusCode: number);
86
105
  }
87
106
 
88
107
  declare class BrowseNodeResult {
@@ -96,116 +115,6 @@ declare class BrowseNodeResult {
96
115
  constructor(referenceTypeId: NodeId, isForward: boolean, nodeId: ExpandedNodeId, browseName: QualifiedName, displayName: LocalizedText, nodeClass: NodeClassEnum, typeDefinition: ExpandedNodeId);
97
116
  }
98
117
 
99
- /**
100
- * Enumeration of variant types based on BuiltinType NodeIds.
101
- */
102
- declare enum BuiltInType {
103
- Null = 0,
104
- Boolean = 1,
105
- SByte = 2,
106
- Byte = 3,
107
- Int16 = 4,
108
- UInt16 = 5,
109
- Int32 = 6,
110
- UInt32 = 7,
111
- Int64 = 8,
112
- UInt64 = 9,
113
- Float = 10,
114
- Double = 11,
115
- String = 12,
116
- DateTime = 13,
117
- Guid = 14,
118
- ByteString = 15,
119
- XmlElement = 16,
120
- NodeId = 17,
121
- ExpandedNodeId = 18,
122
- StatusCode = 19,
123
- QualifiedName = 20,
124
- LocalizedText = 21,
125
- ExtensionObject = 22,
126
- DataValue = 23,
127
- Variant = 24,
128
- DiagnosticInfo = 25
129
- }
130
-
131
- /**
132
- * OPC UA Primitive Type Mappings
133
- *
134
- * We try to use the JS built in types wherever we can. But sometimes, often in the context
135
- * where Variant is involved, we need to have a wrapper type that includes the BuiltInType
136
- * for type checking and encoding purposes. In those cases, we define a wrapper type with a
137
- * value and a type field. For simple cases like boolean, we can just use the native boolean
138
- * type directly as UaBoolean.
139
- * @module primitives
140
- */
141
-
142
- type UaBoolean = boolean;
143
- /**
144
- * OPC UA Builtin Type Numeric IDs
145
- *
146
- * These correspond to the NodeId numeric identifiers defined in OPC UA Part 6, Table 1.
147
- */
148
- type UaSbyte = {
149
- value: number;
150
- readonly type: BuiltInType.SByte;
151
- };
152
- type UaByte = {
153
- value: number;
154
- readonly type: BuiltInType.Byte;
155
- };
156
- type UaInt16 = {
157
- value: number;
158
- readonly type: BuiltInType.Int16;
159
- };
160
- type UaUint16 = {
161
- value: number;
162
- readonly type: BuiltInType.UInt16;
163
- };
164
- type UaInt32 = {
165
- value: number;
166
- readonly type: BuiltInType.Int32;
167
- };
168
- type UaUint32 = {
169
- value: number;
170
- readonly type: BuiltInType.UInt32;
171
- };
172
- type UaInt64 = {
173
- value: bigint;
174
- readonly type: BuiltInType.Int64;
175
- };
176
- type UaUint64 = {
177
- value: bigint;
178
- readonly type: BuiltInType.UInt64;
179
- };
180
- type UaFloat = {
181
- value: number;
182
- readonly type: BuiltInType.Float;
183
- };
184
- type UaDouble = {
185
- value: number;
186
- readonly type: BuiltInType.Double;
187
- };
188
- /**
189
- * OPC UA String primitive type.
190
- *
191
- * A String in OPC UA can be null (encoded as length -1 in binary).
192
- * Using `string | null` instead of `string | undefined` aligns with the
193
- * OPC UA specification where null is an explicit, valid value distinct
194
- * from an empty string. Same is true for ByteString which can be null (length -1)
195
- * or a Uint8Array.
196
- */
197
- type UaString = string | null;
198
- type UaByteString = Uint8Array | null;
199
- type UaGuid = {
200
- value: string;
201
- readonly type: BuiltInType.Guid;
202
- };
203
- type UaDateTime = Date;
204
- /**
205
- * Union of all OPC UA primitive types accepted by {@link Variant.newFrom}.
206
- */
207
- type UaPrimitive = UaBoolean | UaSbyte | UaByte | UaInt16 | UaUint16 | UaInt32 | UaUint32 | UaInt64 | UaUint64 | UaFloat | UaDouble | UaString | UaDateTime | UaGuid | UaByteString;
208
-
209
118
  declare class Client {
210
119
  private configuration;
211
120
  private identity;
@@ -216,7 +125,23 @@ declare class Client {
216
125
  private session?;
217
126
  private subscriptionHandler?;
218
127
  private logger;
128
+ private secureChannel?;
129
+ private sessionHandler?;
219
130
  getSession(): Session;
131
+ /**
132
+ * (Re-)initialises all session-scoped services from the current `this.session`.
133
+ * Called both after the initial `connect()` and after a session refresh.
134
+ */
135
+ private initServices;
136
+ /**
137
+ * Executes `fn` and, if it throws a `SessionInvalidError`, creates a fresh
138
+ * session and retries the operation exactly once.
139
+ *
140
+ * This covers the reactive case: a service call reveals that the server has
141
+ * already dropped the session (e.g. due to timeout). The new session is
142
+ * established transparently before re-running the original operation.
143
+ */
144
+ private withSessionRefresh;
220
145
  connect(): Promise<void>;
221
146
  disconnect(): Promise<void>;
222
147
  read(ids: NodeId[]): Promise<ReadValueResult[]>;
@@ -237,4 +162,20 @@ declare class Client {
237
162
  constructor(endpointUrl: string, configuration: ConfigurationClient, identity: UserIdentity);
238
163
  }
239
164
 
240
- export { BrowseNodeResult, Client, ConfigurationClient, UserIdentity };
165
+ /**
166
+ * Thrown when the server signals that the current Session is no longer valid.
167
+ *
168
+ * Callers that want to react to a dropped session (e.g. by creating a new one
169
+ * and retrying the operation) should catch this specific error type instead of
170
+ * the generic `Error` class.
171
+ *
172
+ * Relevant OPC UA status codes:
173
+ * - `BadSessionIdInvalid` (0x80250000) – session no longer exists on the server
174
+ * - `BadSessionClosed` (0x80260000) – session was closed explicitly
175
+ */
176
+ declare class SessionInvalidError extends Error {
177
+ readonly statusCode: number;
178
+ constructor(statusCode: number);
179
+ }
180
+
181
+ export { BrowseNodeResult, Client, ConfigurationClient, SessionInvalidError, UserIdentity };