node-opcua-transport 2.71.0 → 2.72.2

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 (37) hide show
  1. package/dist/source/AcknowledgeMessage.d.ts +27 -27
  2. package/dist/source/AcknowledgeMessage.js +78 -78
  3. package/dist/source/HelloMessage.d.ts +27 -27
  4. package/dist/source/HelloMessage.js +94 -94
  5. package/dist/source/TCPErrorMessage.d.ts +18 -18
  6. package/dist/source/TCPErrorMessage.js +46 -46
  7. package/dist/source/client_tcp_transport.d.ts +72 -72
  8. package/dist/source/client_tcp_transport.js +322 -322
  9. package/dist/source/index.d.ts +13 -13
  10. package/dist/source/index.js +29 -29
  11. package/dist/source/message_builder_base.d.ts +81 -81
  12. package/dist/source/message_builder_base.js +229 -229
  13. package/dist/source/server_tcp_transport.d.ts +44 -44
  14. package/dist/source/server_tcp_transport.js +228 -228
  15. package/dist/source/status_codes.d.ts +100 -100
  16. package/dist/source/status_codes.js +110 -110
  17. package/dist/source/tcp_transport.d.ts +119 -119
  18. package/dist/source/tcp_transport.js +386 -386
  19. package/dist/source/tools.d.ts +14 -14
  20. package/dist/source/tools.js +103 -103
  21. package/dist/source/utils.d.ts +3 -3
  22. package/dist/source/utils.js +9 -9
  23. package/dist/test-fixtures/fixture_full_tcp_packets.d.ts +21 -21
  24. package/dist/test-fixtures/fixture_full_tcp_packets.js +41 -41
  25. package/dist/test-fixtures/index.d.ts +1 -1
  26. package/dist/test-fixtures/index.js +17 -17
  27. package/dist/test_helpers/direct_transport.d.ts +18 -18
  28. package/dist/test_helpers/direct_transport.js +62 -62
  29. package/dist/test_helpers/fake_server.d.ts +19 -19
  30. package/dist/test_helpers/fake_server.js +54 -54
  31. package/dist/test_helpers/half_com_channel.d.ts +17 -17
  32. package/dist/test_helpers/half_com_channel.js +31 -31
  33. package/dist/test_helpers/index.d.ts +4 -4
  34. package/dist/test_helpers/index.js +20 -20
  35. package/dist/test_helpers/socket_transport.d.ts +10 -10
  36. package/dist/test_helpers/socket_transport.js +30 -30
  37. package/package.json +6 -6
@@ -1,323 +1,323 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ClientTCP_transport = void 0;
4
- /**
5
- * @module node-opcua-transport
6
- */
7
- const os = require("os");
8
- const net_1 = require("net");
9
- const chalk = require("chalk");
10
- const node_opcua_assert_1 = require("node-opcua-assert");
11
- const node_opcua_binary_stream_1 = require("node-opcua-binary-stream");
12
- const node_opcua_chunkmanager_1 = require("node-opcua-chunkmanager");
13
- const debug = require("node-opcua-debug");
14
- const tcp_transport_1 = require("./tcp_transport");
15
- const tools_1 = require("./tools");
16
- const AcknowledgeMessage_1 = require("./AcknowledgeMessage");
17
- const HelloMessage_1 = require("./HelloMessage");
18
- const TCPErrorMessage_1 = require("./TCPErrorMessage");
19
- const utils_1 = require("./utils");
20
- const doDebug = debug.checkDebugFlag(__filename);
21
- const debugLog = debug.make_debugLog(__filename);
22
- const warningLog = debug.make_warningLog(__filename);
23
- const errorLog = debug.make_errorLog(__filename);
24
- const gHostname = os.hostname();
25
- function createClientSocket(endpointUrl) {
26
- // create a socket based on Url
27
- const ep = (0, tools_1.parseEndpointUrl)(endpointUrl);
28
- const port = parseInt(ep.port, 10);
29
- const hostname = ep.hostname;
30
- let socket;
31
- switch (ep.protocol) {
32
- case "opc.tcp:":
33
- socket = (0, net_1.createConnection)({ host: hostname, port });
34
- // // Setting true for noDelay will immediately fire off data each time socket.write() is called.
35
- socket.setNoDelay(true);
36
- return socket;
37
- case "fake:":
38
- socket = (0, tcp_transport_1.getFakeTransport)();
39
- (0, node_opcua_assert_1.assert)(ep.protocol === "fake:", " Unsupported transport protocol");
40
- process.nextTick(() => socket.emit("connect"));
41
- return socket;
42
- case "websocket:":
43
- case "http:":
44
- case "https:FF":
45
- default: {
46
- const msg = "[NODE-OPCUA-E05] this transport protocol is not supported :" + ep.protocol;
47
- errorLog(msg);
48
- throw new Error(msg);
49
- }
50
- }
51
- }
52
- /**
53
- * a ClientTCP_transport connects to a remote server socket and
54
- * initiates a communication with a HEL/ACK transaction.
55
- * It negotiates the communication parameters with the other end.
56
- *
57
- * @class ClientTCP_transport
58
- * @extends TCP_transport
59
- * @constructor
60
- * @example
61
- *
62
- * ```javascript
63
- * const transport = ClientTCP_transport(url);
64
- *
65
- * transport.timeout = 10000;
66
- *
67
- * transport.connect(function(err)) {
68
- * if (err) {
69
- * // cannot connect
70
- * } else {
71
- * // connected
72
- *
73
- * }
74
- * });
75
- * ....
76
- *
77
- * transport.write(message_chunk,'F');
78
- *
79
- * ....
80
- *
81
- * transport.on("chunk",function(message_chunk) {
82
- * // do something with chunk from server...
83
- * });
84
- *
85
- *
86
- * ```
87
- *
88
- *
89
- */
90
- class ClientTCP_transport extends tcp_transport_1.TCP_transport {
91
- constructor() {
92
- super();
93
- this.connected = false;
94
- this.endpointUrl = "";
95
- this.serverUri = "";
96
- this._counter = 0;
97
- this.numberOfRetry = 0;
98
- // initially before HEL/ACK
99
- this.maxChunkCount = 1;
100
- this.maxMessageSize = 4 * 1024;
101
- this.receiveBufferSize = 4 * 1024;
102
- }
103
- dispose() {
104
- /* istanbul ignore next */
105
- if (doDebug) {
106
- debugLog(" ClientTCP_transport disposed");
107
- }
108
- super.dispose();
109
- }
110
- connect(endpointUrl, callback) {
111
- (0, node_opcua_assert_1.assert)(arguments.length === 2);
112
- (0, node_opcua_assert_1.assert)(typeof callback === "function");
113
- const ep = (0, tools_1.parseEndpointUrl)(endpointUrl);
114
- this.endpointUrl = endpointUrl;
115
- this.serverUri = "urn:" + gHostname + ":Sample";
116
- /* istanbul ignore next */
117
- if (doDebug) {
118
- debugLog(chalk.cyan("ClientTCP_transport#connect(endpointUrl = " + endpointUrl + ")"));
119
- }
120
- try {
121
- this._socket = createClientSocket(endpointUrl);
122
- }
123
- catch (err) {
124
- /* istanbul ignore next */
125
- if (doDebug) {
126
- debugLog("CreateClientSocket has failed");
127
- }
128
- return callback(err);
129
- }
130
- const _on_socket_error_after_connection = (err) => {
131
- /* istanbul ignore next */
132
- if (doDebug) {
133
- debugLog(" _on_socket_error_after_connection ClientTCP_transport Socket Error", err.message);
134
- }
135
- // EPIPE : EPIPE (Broken pipe): A write on a pipe, socket, or FIFO for which there is no process to read the
136
- // data. Commonly encountered at the net and http layers, indicative that the remote side of the stream
137
- // being written to has been closed.
138
- // ECONNRESET (Connection reset by peer): A connection was forcibly closed by a peer. This normally results
139
- // from a loss of the connection on the remote socket due to a timeout or reboot. Commonly encountered
140
- // via the http and net module
141
- if (err.message.match(/ECONNRESET|EPIPE/)) {
142
- /**
143
- * @event connection_break
144
- *
145
- */
146
- this.emit("connection_break");
147
- }
148
- };
149
- const _on_socket_connect = () => {
150
- /* istanbul ignore next */
151
- if (doDebug) {
152
- debugLog("entering _on_socket_connect");
153
- }
154
- _remove_connect_listeners();
155
- this._perform_HEL_ACK_transaction((err) => {
156
- if (!err) {
157
- /* istanbul ignore next */
158
- if (!this._socket) {
159
- throw new Error("internal error");
160
- }
161
- // install error handler to detect connection break
162
- this._socket.on("error", _on_socket_error_after_connection);
163
- this.connected = true;
164
- /**
165
- * notify the observers that the transport is connected (the socket is connected and the the HEL/ACK
166
- * transaction has been done)
167
- * @event connect
168
- *
169
- */
170
- this.emit("connect");
171
- }
172
- else {
173
- debugLog("_perform_HEL_ACK_transaction has failed with err=", err.message);
174
- }
175
- callback(err);
176
- });
177
- };
178
- const _on_socket_error_for_connect = (err) => {
179
- // this handler will catch attempt to connect to an inaccessible address.
180
- /* istanbul ignore next */
181
- if (doDebug) {
182
- debugLog(chalk.cyan("ClientTCP_transport#connect - _on_socket_error_for_connect"), err.message);
183
- }
184
- (0, node_opcua_assert_1.assert)(err instanceof Error);
185
- _remove_connect_listeners();
186
- callback(err);
187
- };
188
- const _on_socket_end_for_connect = (err) => {
189
- /* istanbul ignore next */
190
- if (doDebug) {
191
- debugLog(chalk.cyan("ClientTCP_transport#connect -> _on_socket_end_for_connect Socket has been closed by server"), err);
192
- }
193
- };
194
- const _remove_connect_listeners = () => {
195
- /* istanbul ignore next */
196
- if (!this._socket) {
197
- return;
198
- }
199
- this._socket.removeListener("error", _on_socket_error_for_connect);
200
- this._socket.removeListener("end", _on_socket_end_for_connect);
201
- };
202
- this._socket.once("error", _on_socket_error_for_connect);
203
- this._socket.once("end", _on_socket_end_for_connect);
204
- this._socket.once("connect", _on_socket_connect);
205
- this._install_socket(this._socket);
206
- }
207
- on_socket_ended(err) {
208
- debugLog("on_socket_ended", this.name, err ? err.message : "");
209
- if (this.connected) {
210
- super.on_socket_ended(err);
211
- }
212
- // if (this._socket) {
213
- // this._socket.removeAllListeners();
214
- // }
215
- }
216
- _handle_ACK_response(messageChunk, callback) {
217
- const _stream = new node_opcua_binary_stream_1.BinaryStream(messageChunk);
218
- const messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(_stream);
219
- let err;
220
- /* istanbul ignore next */
221
- if (messageHeader.isFinal !== "F") {
222
- err = new Error(" invalid ACK message");
223
- return callback(err);
224
- }
225
- let responseClass;
226
- let response;
227
- if (messageHeader.msgType === "ERR") {
228
- responseClass = TCPErrorMessage_1.TCPErrorMessage;
229
- _stream.rewind();
230
- response = (0, tools_1.decodeMessage)(_stream, responseClass);
231
- err = new Error("ACK: ERR received " + response.statusCode.toString() + " : " + response.reason);
232
- err.statusCode = response.statusCode;
233
- // istanbul ignore next
234
- if (utils_1.doTraceHelloAck) {
235
- warningLog("receiving ERR instead of Ack", response.toString());
236
- }
237
- callback(err);
238
- }
239
- else {
240
- responseClass = AcknowledgeMessage_1.AcknowledgeMessage;
241
- _stream.rewind();
242
- response = (0, tools_1.decodeMessage)(_stream, responseClass);
243
- this.parameters = response;
244
- this.setLimits(response);
245
- // istanbul ignore next
246
- if (utils_1.doTraceHelloAck) {
247
- warningLog("receiving Ack\n", response.toString());
248
- }
249
- callback();
250
- }
251
- }
252
- _send_HELLO_request() {
253
- /* istanbul ignore next */
254
- if (doDebug) {
255
- debugLog("entering _send_HELLO_request");
256
- }
257
- (0, node_opcua_assert_1.assert)(this._socket);
258
- (0, node_opcua_assert_1.assert)(isFinite(this.protocolVersion));
259
- (0, node_opcua_assert_1.assert)(this.endpointUrl.length > 0, " expecting a valid endpoint url");
260
- // Write a message to the socket as soon as the client is connected,
261
- // the server will receive it as message from the client
262
- const helloMessage = new HelloMessage_1.HelloMessage({
263
- endpointUrl: this.endpointUrl,
264
- protocolVersion: this.protocolVersion,
265
- maxChunkCount: ClientTCP_transport.defaultMaxChunk,
266
- maxMessageSize: ClientTCP_transport.defaultMaxMessageSize,
267
- receiveBufferSize: ClientTCP_transport.defaultReceiveBufferSize,
268
- sendBufferSize: ClientTCP_transport.defaultSendBufferSize
269
- });
270
- // istanbul ignore next
271
- if (utils_1.doTraceHelloAck) {
272
- warningLog(`sending Hello\n ${helloMessage.toString()}`);
273
- }
274
- const messageChunk = (0, tools_1.packTcpMessage)("HEL", helloMessage);
275
- this._write_chunk(messageChunk);
276
- }
277
- _on_ACK_response(externalCallback, err, data) {
278
- /* istanbul ignore next */
279
- if (doDebug) {
280
- debugLog("entering _on_ACK_response");
281
- }
282
- (0, node_opcua_assert_1.assert)(typeof externalCallback === "function");
283
- (0, node_opcua_assert_1.assert)(this._counter === 0, "Ack response should only be received once !");
284
- this._counter += 1;
285
- if (err || !data) {
286
- externalCallback(err || new Error("no data"));
287
- if (this._socket) {
288
- this._socket.end();
289
- // Xx this._socket.removeAllListeners();
290
- }
291
- }
292
- else {
293
- this._handle_ACK_response(data, externalCallback);
294
- }
295
- }
296
- _perform_HEL_ACK_transaction(callback) {
297
- /* istanbul ignore next */
298
- if (!this._socket) {
299
- return callback(new Error("No socket available to perform HEL/ACK transaction"));
300
- }
301
- (0, node_opcua_assert_1.assert)(this._socket, "expecting a valid socket to send a message");
302
- (0, node_opcua_assert_1.assert)(typeof callback === "function");
303
- this._counter = 0;
304
- /* istanbul ignore next */
305
- if (doDebug) {
306
- debugLog("entering _perform_HEL_ACK_transaction");
307
- }
308
- this._install_one_time_message_receiver((err, data) => {
309
- /* istanbul ignore next */
310
- if (doDebug) {
311
- debugLog("before _on_ACK_response ", err ? err.message : "");
312
- }
313
- this._on_ACK_response(callback, err, data);
314
- });
315
- this._send_HELLO_request();
316
- }
317
- }
318
- exports.ClientTCP_transport = ClientTCP_transport;
319
- ClientTCP_transport.defaultMaxChunk = 0; // 0 - no limits
320
- ClientTCP_transport.defaultMaxMessageSize = 0; // 0 - no limits
321
- ClientTCP_transport.defaultReceiveBufferSize = 1024 * 64 * 10;
322
- ClientTCP_transport.defaultSendBufferSize = 1024 * 64 * 10; // 8192 min,
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientTCP_transport = void 0;
4
+ /**
5
+ * @module node-opcua-transport
6
+ */
7
+ const os = require("os");
8
+ const net_1 = require("net");
9
+ const chalk = require("chalk");
10
+ const node_opcua_assert_1 = require("node-opcua-assert");
11
+ const node_opcua_binary_stream_1 = require("node-opcua-binary-stream");
12
+ const node_opcua_chunkmanager_1 = require("node-opcua-chunkmanager");
13
+ const debug = require("node-opcua-debug");
14
+ const tcp_transport_1 = require("./tcp_transport");
15
+ const tools_1 = require("./tools");
16
+ const AcknowledgeMessage_1 = require("./AcknowledgeMessage");
17
+ const HelloMessage_1 = require("./HelloMessage");
18
+ const TCPErrorMessage_1 = require("./TCPErrorMessage");
19
+ const utils_1 = require("./utils");
20
+ const doDebug = debug.checkDebugFlag(__filename);
21
+ const debugLog = debug.make_debugLog(__filename);
22
+ const warningLog = debug.make_warningLog(__filename);
23
+ const errorLog = debug.make_errorLog(__filename);
24
+ const gHostname = os.hostname();
25
+ function createClientSocket(endpointUrl) {
26
+ // create a socket based on Url
27
+ const ep = (0, tools_1.parseEndpointUrl)(endpointUrl);
28
+ const port = parseInt(ep.port, 10);
29
+ const hostname = ep.hostname;
30
+ let socket;
31
+ switch (ep.protocol) {
32
+ case "opc.tcp:":
33
+ socket = (0, net_1.createConnection)({ host: hostname, port });
34
+ // // Setting true for noDelay will immediately fire off data each time socket.write() is called.
35
+ socket.setNoDelay(true);
36
+ return socket;
37
+ case "fake:":
38
+ socket = (0, tcp_transport_1.getFakeTransport)();
39
+ (0, node_opcua_assert_1.assert)(ep.protocol === "fake:", " Unsupported transport protocol");
40
+ process.nextTick(() => socket.emit("connect"));
41
+ return socket;
42
+ case "websocket:":
43
+ case "http:":
44
+ case "https:FF":
45
+ default: {
46
+ const msg = "[NODE-OPCUA-E05] this transport protocol is not supported :" + ep.protocol;
47
+ errorLog(msg);
48
+ throw new Error(msg);
49
+ }
50
+ }
51
+ }
52
+ /**
53
+ * a ClientTCP_transport connects to a remote server socket and
54
+ * initiates a communication with a HEL/ACK transaction.
55
+ * It negotiates the communication parameters with the other end.
56
+ *
57
+ * @class ClientTCP_transport
58
+ * @extends TCP_transport
59
+ * @constructor
60
+ * @example
61
+ *
62
+ * ```javascript
63
+ * const transport = ClientTCP_transport(url);
64
+ *
65
+ * transport.timeout = 10000;
66
+ *
67
+ * transport.connect(function(err)) {
68
+ * if (err) {
69
+ * // cannot connect
70
+ * } else {
71
+ * // connected
72
+ *
73
+ * }
74
+ * });
75
+ * ....
76
+ *
77
+ * transport.write(message_chunk,'F');
78
+ *
79
+ * ....
80
+ *
81
+ * transport.on("chunk",function(message_chunk) {
82
+ * // do something with chunk from server...
83
+ * });
84
+ *
85
+ *
86
+ * ```
87
+ *
88
+ *
89
+ */
90
+ class ClientTCP_transport extends tcp_transport_1.TCP_transport {
91
+ constructor() {
92
+ super();
93
+ this.connected = false;
94
+ this.endpointUrl = "";
95
+ this.serverUri = "";
96
+ this._counter = 0;
97
+ this.numberOfRetry = 0;
98
+ // initially before HEL/ACK
99
+ this.maxChunkCount = 1;
100
+ this.maxMessageSize = 4 * 1024;
101
+ this.receiveBufferSize = 4 * 1024;
102
+ }
103
+ dispose() {
104
+ /* istanbul ignore next */
105
+ if (doDebug) {
106
+ debugLog(" ClientTCP_transport disposed");
107
+ }
108
+ super.dispose();
109
+ }
110
+ connect(endpointUrl, callback) {
111
+ (0, node_opcua_assert_1.assert)(arguments.length === 2);
112
+ (0, node_opcua_assert_1.assert)(typeof callback === "function");
113
+ const ep = (0, tools_1.parseEndpointUrl)(endpointUrl);
114
+ this.endpointUrl = endpointUrl;
115
+ this.serverUri = "urn:" + gHostname + ":Sample";
116
+ /* istanbul ignore next */
117
+ if (doDebug) {
118
+ debugLog(chalk.cyan("ClientTCP_transport#connect(endpointUrl = " + endpointUrl + ")"));
119
+ }
120
+ try {
121
+ this._socket = createClientSocket(endpointUrl);
122
+ }
123
+ catch (err) {
124
+ /* istanbul ignore next */
125
+ if (doDebug) {
126
+ debugLog("CreateClientSocket has failed");
127
+ }
128
+ return callback(err);
129
+ }
130
+ const _on_socket_error_after_connection = (err) => {
131
+ /* istanbul ignore next */
132
+ if (doDebug) {
133
+ debugLog(" _on_socket_error_after_connection ClientTCP_transport Socket Error", err.message);
134
+ }
135
+ // EPIPE : EPIPE (Broken pipe): A write on a pipe, socket, or FIFO for which there is no process to read the
136
+ // data. Commonly encountered at the net and http layers, indicative that the remote side of the stream
137
+ // being written to has been closed.
138
+ // ECONNRESET (Connection reset by peer): A connection was forcibly closed by a peer. This normally results
139
+ // from a loss of the connection on the remote socket due to a timeout or reboot. Commonly encountered
140
+ // via the http and net module
141
+ if (err.message.match(/ECONNRESET|EPIPE/)) {
142
+ /**
143
+ * @event connection_break
144
+ *
145
+ */
146
+ this.emit("connection_break");
147
+ }
148
+ };
149
+ const _on_socket_connect = () => {
150
+ /* istanbul ignore next */
151
+ if (doDebug) {
152
+ debugLog("entering _on_socket_connect");
153
+ }
154
+ _remove_connect_listeners();
155
+ this._perform_HEL_ACK_transaction((err) => {
156
+ if (!err) {
157
+ /* istanbul ignore next */
158
+ if (!this._socket) {
159
+ throw new Error("internal error");
160
+ }
161
+ // install error handler to detect connection break
162
+ this._socket.on("error", _on_socket_error_after_connection);
163
+ this.connected = true;
164
+ /**
165
+ * notify the observers that the transport is connected (the socket is connected and the the HEL/ACK
166
+ * transaction has been done)
167
+ * @event connect
168
+ *
169
+ */
170
+ this.emit("connect");
171
+ }
172
+ else {
173
+ debugLog("_perform_HEL_ACK_transaction has failed with err=", err.message);
174
+ }
175
+ callback(err);
176
+ });
177
+ };
178
+ const _on_socket_error_for_connect = (err) => {
179
+ // this handler will catch attempt to connect to an inaccessible address.
180
+ /* istanbul ignore next */
181
+ if (doDebug) {
182
+ debugLog(chalk.cyan("ClientTCP_transport#connect - _on_socket_error_for_connect"), err.message);
183
+ }
184
+ (0, node_opcua_assert_1.assert)(err instanceof Error);
185
+ _remove_connect_listeners();
186
+ callback(err);
187
+ };
188
+ const _on_socket_end_for_connect = (err) => {
189
+ /* istanbul ignore next */
190
+ if (doDebug) {
191
+ debugLog(chalk.cyan("ClientTCP_transport#connect -> _on_socket_end_for_connect Socket has been closed by server"), err);
192
+ }
193
+ };
194
+ const _remove_connect_listeners = () => {
195
+ /* istanbul ignore next */
196
+ if (!this._socket) {
197
+ return;
198
+ }
199
+ this._socket.removeListener("error", _on_socket_error_for_connect);
200
+ this._socket.removeListener("end", _on_socket_end_for_connect);
201
+ };
202
+ this._socket.once("error", _on_socket_error_for_connect);
203
+ this._socket.once("end", _on_socket_end_for_connect);
204
+ this._socket.once("connect", _on_socket_connect);
205
+ this._install_socket(this._socket);
206
+ }
207
+ on_socket_ended(err) {
208
+ debugLog("on_socket_ended", this.name, err ? err.message : "");
209
+ if (this.connected) {
210
+ super.on_socket_ended(err);
211
+ }
212
+ // if (this._socket) {
213
+ // this._socket.removeAllListeners();
214
+ // }
215
+ }
216
+ _handle_ACK_response(messageChunk, callback) {
217
+ const _stream = new node_opcua_binary_stream_1.BinaryStream(messageChunk);
218
+ const messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(_stream);
219
+ let err;
220
+ /* istanbul ignore next */
221
+ if (messageHeader.isFinal !== "F") {
222
+ err = new Error(" invalid ACK message");
223
+ return callback(err);
224
+ }
225
+ let responseClass;
226
+ let response;
227
+ if (messageHeader.msgType === "ERR") {
228
+ responseClass = TCPErrorMessage_1.TCPErrorMessage;
229
+ _stream.rewind();
230
+ response = (0, tools_1.decodeMessage)(_stream, responseClass);
231
+ err = new Error("ACK: ERR received " + response.statusCode.toString() + " : " + response.reason);
232
+ err.statusCode = response.statusCode;
233
+ // istanbul ignore next
234
+ if (utils_1.doTraceHelloAck) {
235
+ warningLog("receiving ERR instead of Ack", response.toString());
236
+ }
237
+ callback(err);
238
+ }
239
+ else {
240
+ responseClass = AcknowledgeMessage_1.AcknowledgeMessage;
241
+ _stream.rewind();
242
+ response = (0, tools_1.decodeMessage)(_stream, responseClass);
243
+ this.parameters = response;
244
+ this.setLimits(response);
245
+ // istanbul ignore next
246
+ if (utils_1.doTraceHelloAck) {
247
+ warningLog("receiving Ack\n", response.toString());
248
+ }
249
+ callback();
250
+ }
251
+ }
252
+ _send_HELLO_request() {
253
+ /* istanbul ignore next */
254
+ if (doDebug) {
255
+ debugLog("entering _send_HELLO_request");
256
+ }
257
+ (0, node_opcua_assert_1.assert)(this._socket);
258
+ (0, node_opcua_assert_1.assert)(isFinite(this.protocolVersion));
259
+ (0, node_opcua_assert_1.assert)(this.endpointUrl.length > 0, " expecting a valid endpoint url");
260
+ // Write a message to the socket as soon as the client is connected,
261
+ // the server will receive it as message from the client
262
+ const helloMessage = new HelloMessage_1.HelloMessage({
263
+ endpointUrl: this.endpointUrl,
264
+ protocolVersion: this.protocolVersion,
265
+ maxChunkCount: ClientTCP_transport.defaultMaxChunk,
266
+ maxMessageSize: ClientTCP_transport.defaultMaxMessageSize,
267
+ receiveBufferSize: ClientTCP_transport.defaultReceiveBufferSize,
268
+ sendBufferSize: ClientTCP_transport.defaultSendBufferSize
269
+ });
270
+ // istanbul ignore next
271
+ if (utils_1.doTraceHelloAck) {
272
+ warningLog(`sending Hello\n ${helloMessage.toString()}`);
273
+ }
274
+ const messageChunk = (0, tools_1.packTcpMessage)("HEL", helloMessage);
275
+ this._write_chunk(messageChunk);
276
+ }
277
+ _on_ACK_response(externalCallback, err, data) {
278
+ /* istanbul ignore next */
279
+ if (doDebug) {
280
+ debugLog("entering _on_ACK_response");
281
+ }
282
+ (0, node_opcua_assert_1.assert)(typeof externalCallback === "function");
283
+ (0, node_opcua_assert_1.assert)(this._counter === 0, "Ack response should only be received once !");
284
+ this._counter += 1;
285
+ if (err || !data) {
286
+ externalCallback(err || new Error("no data"));
287
+ if (this._socket) {
288
+ this._socket.end();
289
+ // Xx this._socket.removeAllListeners();
290
+ }
291
+ }
292
+ else {
293
+ this._handle_ACK_response(data, externalCallback);
294
+ }
295
+ }
296
+ _perform_HEL_ACK_transaction(callback) {
297
+ /* istanbul ignore next */
298
+ if (!this._socket) {
299
+ return callback(new Error("No socket available to perform HEL/ACK transaction"));
300
+ }
301
+ (0, node_opcua_assert_1.assert)(this._socket, "expecting a valid socket to send a message");
302
+ (0, node_opcua_assert_1.assert)(typeof callback === "function");
303
+ this._counter = 0;
304
+ /* istanbul ignore next */
305
+ if (doDebug) {
306
+ debugLog("entering _perform_HEL_ACK_transaction");
307
+ }
308
+ this._install_one_time_message_receiver((err, data) => {
309
+ /* istanbul ignore next */
310
+ if (doDebug) {
311
+ debugLog("before _on_ACK_response ", err ? err.message : "");
312
+ }
313
+ this._on_ACK_response(callback, err, data);
314
+ });
315
+ this._send_HELLO_request();
316
+ }
317
+ }
318
+ exports.ClientTCP_transport = ClientTCP_transport;
319
+ ClientTCP_transport.defaultMaxChunk = 0; // 0 - no limits
320
+ ClientTCP_transport.defaultMaxMessageSize = 0; // 0 - no limits
321
+ ClientTCP_transport.defaultReceiveBufferSize = 1024 * 64 * 10;
322
+ ClientTCP_transport.defaultSendBufferSize = 1024 * 64 * 10; // 8192 min,
323
323
  //# sourceMappingURL=client_tcp_transport.js.map