node-opcua-transport 2.75.0 → 2.76.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.
- package/dist/source/AcknowledgeMessage.d.ts +27 -27
- package/dist/source/AcknowledgeMessage.js +78 -78
- package/dist/source/HelloMessage.d.ts +27 -27
- package/dist/source/HelloMessage.js +94 -94
- package/dist/source/TCPErrorMessage.d.ts +18 -18
- package/dist/source/TCPErrorMessage.js +46 -46
- package/dist/source/client_tcp_transport.d.ts +86 -86
- package/dist/source/client_tcp_transport.js +331 -331
- package/dist/source/index.d.ts +13 -13
- package/dist/source/index.js +29 -29
- package/dist/source/message_builder_base.d.ts +112 -112
- package/dist/source/message_builder_base.js +244 -244
- package/dist/source/server_tcp_transport.d.ts +44 -44
- package/dist/source/server_tcp_transport.js +228 -228
- package/dist/source/status_codes.d.ts +100 -100
- package/dist/source/status_codes.js +110 -110
- package/dist/source/tcp_transport.d.ts +136 -136
- package/dist/source/tcp_transport.js +376 -376
- package/dist/source/tools.d.ts +14 -14
- package/dist/source/tools.js +103 -103
- package/dist/source/utils.d.ts +3 -3
- package/dist/source/utils.js +9 -9
- package/dist/test-fixtures/fixture_full_tcp_packets.d.ts +21 -21
- package/dist/test-fixtures/fixture_full_tcp_packets.js +41 -41
- package/dist/test-fixtures/index.d.ts +1 -1
- package/dist/test-fixtures/index.js +17 -17
- package/dist/test_helpers/direct_transport.d.ts +18 -18
- package/dist/test_helpers/direct_transport.js +62 -62
- package/dist/test_helpers/fake_server.d.ts +19 -19
- package/dist/test_helpers/fake_server.js +54 -54
- package/dist/test_helpers/half_com_channel.d.ts +17 -17
- package/dist/test_helpers/half_com_channel.js +31 -31
- package/dist/test_helpers/index.d.ts +4 -4
- package/dist/test_helpers/index.js +20 -20
- package/dist/test_helpers/socket_transport.d.ts +10 -10
- package/dist/test_helpers/socket_transport.js +30 -30
- package/package.json +13 -16
|
@@ -1,245 +1,245 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MessageBuilderBase = exports.readRawMessageHeader = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* @module node-opcua-transport
|
|
6
|
-
*/
|
|
7
|
-
const events_1 = require("events");
|
|
8
|
-
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
9
|
-
const node_opcua_basic_types_1 = require("node-opcua-basic-types");
|
|
10
|
-
const node_opcua_binary_stream_1 = require("node-opcua-binary-stream");
|
|
11
|
-
const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils");
|
|
12
|
-
const node_opcua_chunkmanager_1 = require("node-opcua-chunkmanager");
|
|
13
|
-
const node_opcua_debug_1 = require("node-opcua-debug");
|
|
14
|
-
const node_opcua_packet_assembler_1 = require("node-opcua-packet-assembler");
|
|
15
|
-
const node_opcua_utils_1 = require("node-opcua-utils");
|
|
16
|
-
const status_codes_1 = require("./status_codes");
|
|
17
|
-
const doPerfMonitoring = process.env.NODEOPCUADEBUG && process.env.NODEOPCUADEBUG.indexOf("PERF") >= 0;
|
|
18
|
-
const errorLog = (0, node_opcua_debug_1.make_errorLog)("MessageBuilder");
|
|
19
|
-
const debugLog = (0, node_opcua_debug_1.make_debugLog)("MessageBuilder");
|
|
20
|
-
const warningLog = (0, node_opcua_debug_1.make_warningLog)("MessageBuilder");
|
|
21
|
-
function readRawMessageHeader(data) {
|
|
22
|
-
const messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(new node_opcua_binary_stream_1.BinaryStream(data));
|
|
23
|
-
return {
|
|
24
|
-
extra: "",
|
|
25
|
-
length: messageHeader.length,
|
|
26
|
-
messageHeader
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
exports.readRawMessageHeader = readRawMessageHeader;
|
|
30
|
-
/**
|
|
31
|
-
* @class MessageBuilderBase
|
|
32
|
-
* @extends EventEmitter
|
|
33
|
-
* @uses PacketAssembler
|
|
34
|
-
* @constructor
|
|
35
|
-
* @param options {Object}
|
|
36
|
-
* @param [options.signatureLength=0] {number}
|
|
37
|
-
*
|
|
38
|
-
*/
|
|
39
|
-
class MessageBuilderBase extends events_1.EventEmitter {
|
|
40
|
-
constructor(options) {
|
|
41
|
-
super();
|
|
42
|
-
this.id = "";
|
|
43
|
-
this._tick0 = 0;
|
|
44
|
-
this._tick1 = 0;
|
|
45
|
-
this._hasReceivedError = false;
|
|
46
|
-
this.blocks = [];
|
|
47
|
-
this.messageChunks = [];
|
|
48
|
-
this._expectedChannelId = 0;
|
|
49
|
-
options = options || {
|
|
50
|
-
maxMessageSize: 0,
|
|
51
|
-
maxChunkCount: 0,
|
|
52
|
-
maxChunkSize: 0
|
|
53
|
-
};
|
|
54
|
-
this.signatureLength = options.signatureLength || 0;
|
|
55
|
-
this.maxMessageSize = options.maxMessageSize || MessageBuilderBase.defaultMaxMessageSize;
|
|
56
|
-
this.maxChunkCount = options.maxChunkCount || MessageBuilderBase.defaultMaxChunkCount;
|
|
57
|
-
this.maxChunkSize = options.maxChunkSize || MessageBuilderBase.defaultMaxChunkSize;
|
|
58
|
-
this.options = options;
|
|
59
|
-
this._packetAssembler = new node_opcua_packet_assembler_1.PacketAssembler({
|
|
60
|
-
minimumSizeInBytes: 8,
|
|
61
|
-
maxChunkSize: this.maxChunkSize,
|
|
62
|
-
readChunkFunc: readRawMessageHeader
|
|
63
|
-
});
|
|
64
|
-
this._packetAssembler.on("chunk", (messageChunk) => this._feed_messageChunk(messageChunk));
|
|
65
|
-
this._packetAssembler.on("startChunk", (info, data) => {
|
|
66
|
-
if (doPerfMonitoring) {
|
|
67
|
-
// record tick 0: when the first data is received
|
|
68
|
-
this._tick0 = (0, node_opcua_utils_1.get_clock_tick)();
|
|
69
|
-
}
|
|
70
|
-
this.emit("startChunk", info, data);
|
|
71
|
-
});
|
|
72
|
-
this._packetAssembler.on("error", (err) => {
|
|
73
|
-
warningLog("packet assembler ", err.message);
|
|
74
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpMessageTooLarge, "packet assembler: " + err.message);
|
|
75
|
-
});
|
|
76
|
-
this._securityDefeated = false;
|
|
77
|
-
this.totalBodySize = 0;
|
|
78
|
-
this.totalMessageSize = 0;
|
|
79
|
-
this.channelId = 0;
|
|
80
|
-
this.offsetBodyStart = 0;
|
|
81
|
-
this.sequenceHeader = null;
|
|
82
|
-
this._init_new();
|
|
83
|
-
}
|
|
84
|
-
dispose() {
|
|
85
|
-
this.removeAllListeners();
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Feed message builder with some data
|
|
89
|
-
* @method feed
|
|
90
|
-
* @param data
|
|
91
|
-
*/
|
|
92
|
-
feed(data) {
|
|
93
|
-
if (!this._securityDefeated && !this._hasReceivedError) {
|
|
94
|
-
this._packetAssembler.feed(data);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
_decodeMessageBody(fullMessageBody) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
_read_headers(binaryStream) {
|
|
101
|
-
try {
|
|
102
|
-
this.messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(binaryStream);
|
|
103
|
-
(0, node_opcua_assert_1.assert)(binaryStream.length === 8, "expecting message header to be 8 bytes");
|
|
104
|
-
this.channelId = binaryStream.readUInt32();
|
|
105
|
-
(0, node_opcua_assert_1.assert)(binaryStream.length === 12);
|
|
106
|
-
// verifying secure ChannelId
|
|
107
|
-
if (this._expectedChannelId && this.channelId !== this._expectedChannelId) {
|
|
108
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpSecureChannelUnknown, "Invalid secure channel Id");
|
|
109
|
-
}
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, "_read_headers error " + err.message);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
_report_abandon(channelId, tokenId, sequenceHeader) {
|
|
117
|
-
// the server has not been able to send a complete message and has abandoned the request
|
|
118
|
-
// the connection can probably continue
|
|
119
|
-
this._hasReceivedError = false; ///
|
|
120
|
-
this.emit("abandon", sequenceHeader.requestId);
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
_report_error(statusCode, errorMessage) {
|
|
124
|
-
var _a;
|
|
125
|
-
this._hasReceivedError = true;
|
|
126
|
-
debugLog("Error ", this.id, errorMessage);
|
|
127
|
-
// xx errorLog(new Error());
|
|
128
|
-
this.emit("error", new Error(errorMessage), statusCode, ((_a = this.sequenceHeader) === null || _a === void 0 ? void 0 : _a.requestId) || null);
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
_init_new() {
|
|
132
|
-
this._securityDefeated = false;
|
|
133
|
-
this._hasReceivedError = false;
|
|
134
|
-
this.totalBodySize = 0;
|
|
135
|
-
this.totalMessageSize = 0;
|
|
136
|
-
this.blocks = [];
|
|
137
|
-
this.messageChunks = [];
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* append a message chunk
|
|
141
|
-
* @method _append
|
|
142
|
-
* @param chunk
|
|
143
|
-
* @private
|
|
144
|
-
*/
|
|
145
|
-
_append(chunk) {
|
|
146
|
-
if (this._hasReceivedError) {
|
|
147
|
-
// the message builder is in error mode and further message chunks should be discarded.
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
if (this.messageChunks.length + 1 > this.maxChunkCount) {
|
|
151
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpMessageTooLarge, `max chunk count exceeded: ${this.maxChunkCount}`);
|
|
152
|
-
}
|
|
153
|
-
this.messageChunks.push(chunk);
|
|
154
|
-
this.totalMessageSize += chunk.length;
|
|
155
|
-
if (this.totalMessageSize > this.maxMessageSize) {
|
|
156
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpMessageTooLarge, `max message size exceeded: ${this.maxMessageSize}`);
|
|
157
|
-
}
|
|
158
|
-
const binaryStream = new node_opcua_binary_stream_1.BinaryStream(chunk);
|
|
159
|
-
if (!this._read_headers(binaryStream)) {
|
|
160
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, `Invalid message header detected`);
|
|
161
|
-
}
|
|
162
|
-
(0, node_opcua_assert_1.assert)(binaryStream.length >= 12);
|
|
163
|
-
// verify message chunk length
|
|
164
|
-
if (this.messageHeader.length !== chunk.length) {
|
|
165
|
-
// tslint:disable:max-line-length
|
|
166
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, `Invalid messageChunk size: the provided chunk is ${chunk.length} bytes long but header specifies ${this.messageHeader.length}`);
|
|
167
|
-
}
|
|
168
|
-
// the start of the message body block
|
|
169
|
-
const offsetBodyStart = binaryStream.length;
|
|
170
|
-
// the end of the message body block
|
|
171
|
-
const offsetBodyEnd = binaryStream.buffer.length;
|
|
172
|
-
this.totalBodySize += offsetBodyEnd - offsetBodyStart;
|
|
173
|
-
this.offsetBodyStart = offsetBodyStart;
|
|
174
|
-
// add message body to a queue
|
|
175
|
-
// note : Buffer.slice create a shared memory !
|
|
176
|
-
// use Buffer.clone
|
|
177
|
-
const sharedBuffer = chunk.slice(this.offsetBodyStart, offsetBodyEnd);
|
|
178
|
-
const clonedBuffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(sharedBuffer.length);
|
|
179
|
-
sharedBuffer.copy(clonedBuffer, 0, 0);
|
|
180
|
-
this.blocks.push(clonedBuffer);
|
|
181
|
-
return true;
|
|
182
|
-
}
|
|
183
|
-
_feed_messageChunk(chunk) {
|
|
184
|
-
(0, node_opcua_assert_1.assert)(chunk);
|
|
185
|
-
const messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(new node_opcua_binary_stream_1.BinaryStream(chunk));
|
|
186
|
-
this.emit("chunk", chunk);
|
|
187
|
-
if (messageHeader.isFinal === "F") {
|
|
188
|
-
if (messageHeader.msgType === "ERR") {
|
|
189
|
-
const binaryStream = new node_opcua_binary_stream_1.BinaryStream(chunk);
|
|
190
|
-
binaryStream.length = 8;
|
|
191
|
-
const errorCode = (0, node_opcua_basic_types_1.decodeStatusCode)(binaryStream);
|
|
192
|
-
const message = (0, node_opcua_basic_types_1.decodeString)(binaryStream);
|
|
193
|
-
this._report_error(errorCode, message || "Error message not specified");
|
|
194
|
-
return true;
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
this._append(chunk);
|
|
198
|
-
// last message
|
|
199
|
-
if (this._hasReceivedError) {
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
const fullMessageBody = this.blocks.length === 1 ? this.blocks[0] : Buffer.concat(this.blocks);
|
|
203
|
-
if (doPerfMonitoring) {
|
|
204
|
-
// record tick 1: when a complete message has been received ( all chunks assembled)
|
|
205
|
-
this._tick1 = (0, node_opcua_utils_1.get_clock_tick)();
|
|
206
|
-
}
|
|
207
|
-
this.emit("full_message_body", fullMessageBody);
|
|
208
|
-
this._decodeMessageBody(fullMessageBody);
|
|
209
|
-
// be ready for next block
|
|
210
|
-
this._init_new();
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
else if (messageHeader.isFinal === "A") {
|
|
215
|
-
try {
|
|
216
|
-
// only valid for MSG, according to spec
|
|
217
|
-
const stream = new node_opcua_binary_stream_1.BinaryStream(chunk);
|
|
218
|
-
(0, node_opcua_chunkmanager_1.readMessageHeader)(stream);
|
|
219
|
-
(0, node_opcua_assert_1.assert)(stream.length === 8);
|
|
220
|
-
// instead of
|
|
221
|
-
// const securityHeader = new SymmetricAlgorithmSecurityHeader();
|
|
222
|
-
// securityHeader.decode(stream);
|
|
223
|
-
const channelId = stream.readUInt32();
|
|
224
|
-
const tokenId = (0, node_opcua_basic_types_1.decodeUInt32)(stream);
|
|
225
|
-
const sequenceHeader = new node_opcua_chunkmanager_1.SequenceHeader();
|
|
226
|
-
sequenceHeader.decode(stream);
|
|
227
|
-
return this._report_abandon(channelId, tokenId, sequenceHeader);
|
|
228
|
-
}
|
|
229
|
-
catch (err) {
|
|
230
|
-
warningLog((0, node_opcua_debug_1.hexDump)(chunk));
|
|
231
|
-
warningLog("Cannot interpret message chunk: ", err.message);
|
|
232
|
-
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, "Error decoding message header " + err.message);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
else if (messageHeader.isFinal === "C") {
|
|
236
|
-
return this._append(chunk);
|
|
237
|
-
}
|
|
238
|
-
return false;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
exports.MessageBuilderBase = MessageBuilderBase;
|
|
242
|
-
MessageBuilderBase.defaultMaxChunkCount = 1000;
|
|
243
|
-
MessageBuilderBase.defaultMaxMessageSize = 1024 * 64;
|
|
244
|
-
MessageBuilderBase.defaultMaxChunkSize = 1024 * 8;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageBuilderBase = exports.readRawMessageHeader = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @module node-opcua-transport
|
|
6
|
+
*/
|
|
7
|
+
const events_1 = require("events");
|
|
8
|
+
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
9
|
+
const node_opcua_basic_types_1 = require("node-opcua-basic-types");
|
|
10
|
+
const node_opcua_binary_stream_1 = require("node-opcua-binary-stream");
|
|
11
|
+
const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils");
|
|
12
|
+
const node_opcua_chunkmanager_1 = require("node-opcua-chunkmanager");
|
|
13
|
+
const node_opcua_debug_1 = require("node-opcua-debug");
|
|
14
|
+
const node_opcua_packet_assembler_1 = require("node-opcua-packet-assembler");
|
|
15
|
+
const node_opcua_utils_1 = require("node-opcua-utils");
|
|
16
|
+
const status_codes_1 = require("./status_codes");
|
|
17
|
+
const doPerfMonitoring = process.env.NODEOPCUADEBUG && process.env.NODEOPCUADEBUG.indexOf("PERF") >= 0;
|
|
18
|
+
const errorLog = (0, node_opcua_debug_1.make_errorLog)("MessageBuilder");
|
|
19
|
+
const debugLog = (0, node_opcua_debug_1.make_debugLog)("MessageBuilder");
|
|
20
|
+
const warningLog = (0, node_opcua_debug_1.make_warningLog)("MessageBuilder");
|
|
21
|
+
function readRawMessageHeader(data) {
|
|
22
|
+
const messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(new node_opcua_binary_stream_1.BinaryStream(data));
|
|
23
|
+
return {
|
|
24
|
+
extra: "",
|
|
25
|
+
length: messageHeader.length,
|
|
26
|
+
messageHeader
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
exports.readRawMessageHeader = readRawMessageHeader;
|
|
30
|
+
/**
|
|
31
|
+
* @class MessageBuilderBase
|
|
32
|
+
* @extends EventEmitter
|
|
33
|
+
* @uses PacketAssembler
|
|
34
|
+
* @constructor
|
|
35
|
+
* @param options {Object}
|
|
36
|
+
* @param [options.signatureLength=0] {number}
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
class MessageBuilderBase extends events_1.EventEmitter {
|
|
40
|
+
constructor(options) {
|
|
41
|
+
super();
|
|
42
|
+
this.id = "";
|
|
43
|
+
this._tick0 = 0;
|
|
44
|
+
this._tick1 = 0;
|
|
45
|
+
this._hasReceivedError = false;
|
|
46
|
+
this.blocks = [];
|
|
47
|
+
this.messageChunks = [];
|
|
48
|
+
this._expectedChannelId = 0;
|
|
49
|
+
options = options || {
|
|
50
|
+
maxMessageSize: 0,
|
|
51
|
+
maxChunkCount: 0,
|
|
52
|
+
maxChunkSize: 0
|
|
53
|
+
};
|
|
54
|
+
this.signatureLength = options.signatureLength || 0;
|
|
55
|
+
this.maxMessageSize = options.maxMessageSize || MessageBuilderBase.defaultMaxMessageSize;
|
|
56
|
+
this.maxChunkCount = options.maxChunkCount || MessageBuilderBase.defaultMaxChunkCount;
|
|
57
|
+
this.maxChunkSize = options.maxChunkSize || MessageBuilderBase.defaultMaxChunkSize;
|
|
58
|
+
this.options = options;
|
|
59
|
+
this._packetAssembler = new node_opcua_packet_assembler_1.PacketAssembler({
|
|
60
|
+
minimumSizeInBytes: 8,
|
|
61
|
+
maxChunkSize: this.maxChunkSize,
|
|
62
|
+
readChunkFunc: readRawMessageHeader
|
|
63
|
+
});
|
|
64
|
+
this._packetAssembler.on("chunk", (messageChunk) => this._feed_messageChunk(messageChunk));
|
|
65
|
+
this._packetAssembler.on("startChunk", (info, data) => {
|
|
66
|
+
if (doPerfMonitoring) {
|
|
67
|
+
// record tick 0: when the first data is received
|
|
68
|
+
this._tick0 = (0, node_opcua_utils_1.get_clock_tick)();
|
|
69
|
+
}
|
|
70
|
+
this.emit("startChunk", info, data);
|
|
71
|
+
});
|
|
72
|
+
this._packetAssembler.on("error", (err) => {
|
|
73
|
+
warningLog("packet assembler ", err.message);
|
|
74
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpMessageTooLarge, "packet assembler: " + err.message);
|
|
75
|
+
});
|
|
76
|
+
this._securityDefeated = false;
|
|
77
|
+
this.totalBodySize = 0;
|
|
78
|
+
this.totalMessageSize = 0;
|
|
79
|
+
this.channelId = 0;
|
|
80
|
+
this.offsetBodyStart = 0;
|
|
81
|
+
this.sequenceHeader = null;
|
|
82
|
+
this._init_new();
|
|
83
|
+
}
|
|
84
|
+
dispose() {
|
|
85
|
+
this.removeAllListeners();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Feed message builder with some data
|
|
89
|
+
* @method feed
|
|
90
|
+
* @param data
|
|
91
|
+
*/
|
|
92
|
+
feed(data) {
|
|
93
|
+
if (!this._securityDefeated && !this._hasReceivedError) {
|
|
94
|
+
this._packetAssembler.feed(data);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
_decodeMessageBody(fullMessageBody) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
_read_headers(binaryStream) {
|
|
101
|
+
try {
|
|
102
|
+
this.messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(binaryStream);
|
|
103
|
+
(0, node_opcua_assert_1.assert)(binaryStream.length === 8, "expecting message header to be 8 bytes");
|
|
104
|
+
this.channelId = binaryStream.readUInt32();
|
|
105
|
+
(0, node_opcua_assert_1.assert)(binaryStream.length === 12);
|
|
106
|
+
// verifying secure ChannelId
|
|
107
|
+
if (this._expectedChannelId && this.channelId !== this._expectedChannelId) {
|
|
108
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpSecureChannelUnknown, "Invalid secure channel Id");
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, "_read_headers error " + err.message);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
_report_abandon(channelId, tokenId, sequenceHeader) {
|
|
117
|
+
// the server has not been able to send a complete message and has abandoned the request
|
|
118
|
+
// the connection can probably continue
|
|
119
|
+
this._hasReceivedError = false; ///
|
|
120
|
+
this.emit("abandon", sequenceHeader.requestId);
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
_report_error(statusCode, errorMessage) {
|
|
124
|
+
var _a;
|
|
125
|
+
this._hasReceivedError = true;
|
|
126
|
+
debugLog("Error ", this.id, errorMessage);
|
|
127
|
+
// xx errorLog(new Error());
|
|
128
|
+
this.emit("error", new Error(errorMessage), statusCode, ((_a = this.sequenceHeader) === null || _a === void 0 ? void 0 : _a.requestId) || null);
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
_init_new() {
|
|
132
|
+
this._securityDefeated = false;
|
|
133
|
+
this._hasReceivedError = false;
|
|
134
|
+
this.totalBodySize = 0;
|
|
135
|
+
this.totalMessageSize = 0;
|
|
136
|
+
this.blocks = [];
|
|
137
|
+
this.messageChunks = [];
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* append a message chunk
|
|
141
|
+
* @method _append
|
|
142
|
+
* @param chunk
|
|
143
|
+
* @private
|
|
144
|
+
*/
|
|
145
|
+
_append(chunk) {
|
|
146
|
+
if (this._hasReceivedError) {
|
|
147
|
+
// the message builder is in error mode and further message chunks should be discarded.
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (this.messageChunks.length + 1 > this.maxChunkCount) {
|
|
151
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpMessageTooLarge, `max chunk count exceeded: ${this.maxChunkCount}`);
|
|
152
|
+
}
|
|
153
|
+
this.messageChunks.push(chunk);
|
|
154
|
+
this.totalMessageSize += chunk.length;
|
|
155
|
+
if (this.totalMessageSize > this.maxMessageSize) {
|
|
156
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpMessageTooLarge, `max message size exceeded: ${this.maxMessageSize}`);
|
|
157
|
+
}
|
|
158
|
+
const binaryStream = new node_opcua_binary_stream_1.BinaryStream(chunk);
|
|
159
|
+
if (!this._read_headers(binaryStream)) {
|
|
160
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, `Invalid message header detected`);
|
|
161
|
+
}
|
|
162
|
+
(0, node_opcua_assert_1.assert)(binaryStream.length >= 12);
|
|
163
|
+
// verify message chunk length
|
|
164
|
+
if (this.messageHeader.length !== chunk.length) {
|
|
165
|
+
// tslint:disable:max-line-length
|
|
166
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, `Invalid messageChunk size: the provided chunk is ${chunk.length} bytes long but header specifies ${this.messageHeader.length}`);
|
|
167
|
+
}
|
|
168
|
+
// the start of the message body block
|
|
169
|
+
const offsetBodyStart = binaryStream.length;
|
|
170
|
+
// the end of the message body block
|
|
171
|
+
const offsetBodyEnd = binaryStream.buffer.length;
|
|
172
|
+
this.totalBodySize += offsetBodyEnd - offsetBodyStart;
|
|
173
|
+
this.offsetBodyStart = offsetBodyStart;
|
|
174
|
+
// add message body to a queue
|
|
175
|
+
// note : Buffer.slice create a shared memory !
|
|
176
|
+
// use Buffer.clone
|
|
177
|
+
const sharedBuffer = chunk.slice(this.offsetBodyStart, offsetBodyEnd);
|
|
178
|
+
const clonedBuffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(sharedBuffer.length);
|
|
179
|
+
sharedBuffer.copy(clonedBuffer, 0, 0);
|
|
180
|
+
this.blocks.push(clonedBuffer);
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
_feed_messageChunk(chunk) {
|
|
184
|
+
(0, node_opcua_assert_1.assert)(chunk);
|
|
185
|
+
const messageHeader = (0, node_opcua_chunkmanager_1.readMessageHeader)(new node_opcua_binary_stream_1.BinaryStream(chunk));
|
|
186
|
+
this.emit("chunk", chunk);
|
|
187
|
+
if (messageHeader.isFinal === "F") {
|
|
188
|
+
if (messageHeader.msgType === "ERR") {
|
|
189
|
+
const binaryStream = new node_opcua_binary_stream_1.BinaryStream(chunk);
|
|
190
|
+
binaryStream.length = 8;
|
|
191
|
+
const errorCode = (0, node_opcua_basic_types_1.decodeStatusCode)(binaryStream);
|
|
192
|
+
const message = (0, node_opcua_basic_types_1.decodeString)(binaryStream);
|
|
193
|
+
this._report_error(errorCode, message || "Error message not specified");
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
this._append(chunk);
|
|
198
|
+
// last message
|
|
199
|
+
if (this._hasReceivedError) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
const fullMessageBody = this.blocks.length === 1 ? this.blocks[0] : Buffer.concat(this.blocks);
|
|
203
|
+
if (doPerfMonitoring) {
|
|
204
|
+
// record tick 1: when a complete message has been received ( all chunks assembled)
|
|
205
|
+
this._tick1 = (0, node_opcua_utils_1.get_clock_tick)();
|
|
206
|
+
}
|
|
207
|
+
this.emit("full_message_body", fullMessageBody);
|
|
208
|
+
this._decodeMessageBody(fullMessageBody);
|
|
209
|
+
// be ready for next block
|
|
210
|
+
this._init_new();
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else if (messageHeader.isFinal === "A") {
|
|
215
|
+
try {
|
|
216
|
+
// only valid for MSG, according to spec
|
|
217
|
+
const stream = new node_opcua_binary_stream_1.BinaryStream(chunk);
|
|
218
|
+
(0, node_opcua_chunkmanager_1.readMessageHeader)(stream);
|
|
219
|
+
(0, node_opcua_assert_1.assert)(stream.length === 8);
|
|
220
|
+
// instead of
|
|
221
|
+
// const securityHeader = new SymmetricAlgorithmSecurityHeader();
|
|
222
|
+
// securityHeader.decode(stream);
|
|
223
|
+
const channelId = stream.readUInt32();
|
|
224
|
+
const tokenId = (0, node_opcua_basic_types_1.decodeUInt32)(stream);
|
|
225
|
+
const sequenceHeader = new node_opcua_chunkmanager_1.SequenceHeader();
|
|
226
|
+
sequenceHeader.decode(stream);
|
|
227
|
+
return this._report_abandon(channelId, tokenId, sequenceHeader);
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
warningLog((0, node_opcua_debug_1.hexDump)(chunk));
|
|
231
|
+
warningLog("Cannot interpret message chunk: ", err.message);
|
|
232
|
+
return this._report_error(status_codes_1.StatusCodes2.BadTcpInternalError, "Error decoding message header " + err.message);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else if (messageHeader.isFinal === "C") {
|
|
236
|
+
return this._append(chunk);
|
|
237
|
+
}
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.MessageBuilderBase = MessageBuilderBase;
|
|
242
|
+
MessageBuilderBase.defaultMaxChunkCount = 1000;
|
|
243
|
+
MessageBuilderBase.defaultMaxMessageSize = 1024 * 64;
|
|
244
|
+
MessageBuilderBase.defaultMaxChunkSize = 1024 * 8;
|
|
245
245
|
//# sourceMappingURL=message_builder_base.js.map
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
/**
|
|
4
|
-
* @module node-opcua-transport
|
|
5
|
-
*/
|
|
6
|
-
import { Socket } from "net";
|
|
7
|
-
import { StatusCode } from "node-opcua-status-code";
|
|
8
|
-
import { ErrorCallback } from "node-opcua-status-code";
|
|
9
|
-
import { TCP_transport } from "./tcp_transport";
|
|
10
|
-
/**
|
|
11
|
-
* @class ServerTCP_transport
|
|
12
|
-
* @extends TCP_transport
|
|
13
|
-
* @constructor
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
export declare class ServerTCP_transport extends TCP_transport {
|
|
17
|
-
static throttleTime: number;
|
|
18
|
-
private _aborted;
|
|
19
|
-
private _helloReceived;
|
|
20
|
-
constructor();
|
|
21
|
-
protected _write_chunk(messageChunk: Buffer): void;
|
|
22
|
-
/**
|
|
23
|
-
* Initialize the server transport.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* The ServerTCP_transport initialization process starts by waiting for the client to send a "HEL" message.
|
|
27
|
-
*
|
|
28
|
-
* The ServerTCP_transport replies with a "ACK" message and then start waiting for further messages of any size.
|
|
29
|
-
*
|
|
30
|
-
* The callback function received an error:
|
|
31
|
-
* - if no message from the client is received within the ```self.timeout``` period,
|
|
32
|
-
* - or, if the connection has dropped within the same interval.
|
|
33
|
-
* - if the protocol version specified within the HEL message is invalid or is greater
|
|
34
|
-
* than ```self.protocolVersion```
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*/
|
|
38
|
-
init(socket: Socket, callback: ErrorCallback): void;
|
|
39
|
-
abortWithError(statusCode: StatusCode, extraErrorDescription: string, callback: ErrorCallback): void;
|
|
40
|
-
private _abortWithError;
|
|
41
|
-
private _send_ACK_response;
|
|
42
|
-
private _install_HEL_message_receiver;
|
|
43
|
-
private _on_HEL_message;
|
|
44
|
-
}
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/**
|
|
4
|
+
* @module node-opcua-transport
|
|
5
|
+
*/
|
|
6
|
+
import { Socket } from "net";
|
|
7
|
+
import { StatusCode } from "node-opcua-status-code";
|
|
8
|
+
import { ErrorCallback } from "node-opcua-status-code";
|
|
9
|
+
import { TCP_transport } from "./tcp_transport";
|
|
10
|
+
/**
|
|
11
|
+
* @class ServerTCP_transport
|
|
12
|
+
* @extends TCP_transport
|
|
13
|
+
* @constructor
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
export declare class ServerTCP_transport extends TCP_transport {
|
|
17
|
+
static throttleTime: number;
|
|
18
|
+
private _aborted;
|
|
19
|
+
private _helloReceived;
|
|
20
|
+
constructor();
|
|
21
|
+
protected _write_chunk(messageChunk: Buffer): void;
|
|
22
|
+
/**
|
|
23
|
+
* Initialize the server transport.
|
|
24
|
+
*
|
|
25
|
+
*
|
|
26
|
+
* The ServerTCP_transport initialization process starts by waiting for the client to send a "HEL" message.
|
|
27
|
+
*
|
|
28
|
+
* The ServerTCP_transport replies with a "ACK" message and then start waiting for further messages of any size.
|
|
29
|
+
*
|
|
30
|
+
* The callback function received an error:
|
|
31
|
+
* - if no message from the client is received within the ```self.timeout``` period,
|
|
32
|
+
* - or, if the connection has dropped within the same interval.
|
|
33
|
+
* - if the protocol version specified within the HEL message is invalid or is greater
|
|
34
|
+
* than ```self.protocolVersion```
|
|
35
|
+
*
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
init(socket: Socket, callback: ErrorCallback): void;
|
|
39
|
+
abortWithError(statusCode: StatusCode, extraErrorDescription: string, callback: ErrorCallback): void;
|
|
40
|
+
private _abortWithError;
|
|
41
|
+
private _send_ACK_response;
|
|
42
|
+
private _install_HEL_message_receiver;
|
|
43
|
+
private _on_HEL_message;
|
|
44
|
+
}
|