node-opcua-packet-assembler 2.55.0 → 2.62.7
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/LICENSE +20 -20
- package/package.json +2 -2
- package/source/index.ts +1 -1
- package/source/packet_assembler.ts +124 -124
package/LICENSE
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2014-2021 Etienne Rossignon
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
-
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
-
the Software without restriction, including without limitation the rights to
|
|
8
|
-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
-
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
-
subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2021 Etienne Rossignon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-opcua-packet-assembler",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.62.7",
|
|
4
4
|
"description": "pure nodejs OPCUA SDK - module -packet-assembler",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"internet of things"
|
|
31
31
|
],
|
|
32
32
|
"homepage": "http://node-opcua.github.io/",
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "90bb9139261a0edbb531081afcc6904c74e9ee52"
|
|
34
34
|
}
|
package/source/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./packet_assembler";
|
|
1
|
+
export * from "./packet_assembler";
|
|
@@ -1,124 +1,124 @@
|
|
|
1
|
-
import { EventEmitter } from "events";
|
|
2
|
-
import { assert } from "node-opcua-assert";
|
|
3
|
-
|
|
4
|
-
const doDebug = false;
|
|
5
|
-
|
|
6
|
-
/***
|
|
7
|
-
* @class PacketAssembler
|
|
8
|
-
* @constructor
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export interface MessageHeader {
|
|
12
|
-
msgType: string;
|
|
13
|
-
isFinal: string;
|
|
14
|
-
length: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface PacketInfo {
|
|
18
|
-
length: number;
|
|
19
|
-
messageHeader: MessageHeader;
|
|
20
|
-
extra: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type ReadMessageFuncType = (data: Buffer) => PacketInfo;
|
|
24
|
-
|
|
25
|
-
export interface PacketAssemblerOptions {
|
|
26
|
-
readMessageFunc: ReadMessageFuncType;
|
|
27
|
-
|
|
28
|
-
// the minimum number of bytes that need to be received before the readMessageFunc can be called
|
|
29
|
-
minimumSizeInBytes: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class PacketAssembler extends EventEmitter {
|
|
33
|
-
private readonly _stack: Buffer[];
|
|
34
|
-
private expectedLength: number;
|
|
35
|
-
private currentLength: number;
|
|
36
|
-
private readonly readMessageFunc: ReadMessageFuncType;
|
|
37
|
-
private readonly minimumSizeInBytes: number;
|
|
38
|
-
private packetInfo?: PacketInfo;
|
|
39
|
-
|
|
40
|
-
constructor(options: PacketAssemblerOptions) {
|
|
41
|
-
super();
|
|
42
|
-
this._stack = [];
|
|
43
|
-
this.expectedLength = 0;
|
|
44
|
-
this.currentLength = 0;
|
|
45
|
-
this.readMessageFunc = options.readMessageFunc;
|
|
46
|
-
this.minimumSizeInBytes = options.minimumSizeInBytes || 8;
|
|
47
|
-
assert(typeof this.readMessageFunc === "function", "packet assembler requires a readMessageFunc");
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public feed(data: Buffer) {
|
|
51
|
-
let messageChunk;
|
|
52
|
-
|
|
53
|
-
if (this.expectedLength === 0 && this.currentLength + data.length >= this.minimumSizeInBytes) {
|
|
54
|
-
// we are at a start of a block and there is enough data provided to read the length of the block
|
|
55
|
-
// let's build the whole data block with previous blocks already read.
|
|
56
|
-
if (this._stack.length > 0) {
|
|
57
|
-
data = this._buildData(data);
|
|
58
|
-
this.currentLength = 0;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// we can extract the expected length here
|
|
62
|
-
this.packetInfo = this._readPacketInfo(data);
|
|
63
|
-
this.expectedLength = this.packetInfo.length;
|
|
64
|
-
assert(this.currentLength === 0);
|
|
65
|
-
assert(this.expectedLength > 0);
|
|
66
|
-
|
|
67
|
-
// we can now emit an event to signal the start of a new packet
|
|
68
|
-
this.emit("newMessage", this.packetInfo, data);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (this.expectedLength === 0 || this.currentLength + data.length < this.expectedLength) {
|
|
72
|
-
this._stack.push(data);
|
|
73
|
-
this.currentLength += data.length;
|
|
74
|
-
// expecting more data to complete current message chunk
|
|
75
|
-
} else if (this.currentLength + data.length === this.expectedLength) {
|
|
76
|
-
this.currentLength += data.length;
|
|
77
|
-
|
|
78
|
-
messageChunk = this._buildData(data);
|
|
79
|
-
|
|
80
|
-
// istanbul ignore next
|
|
81
|
-
if (doDebug) {
|
|
82
|
-
const packetInfo = this._readPacketInfo(messageChunk);
|
|
83
|
-
assert(this.packetInfo && this.packetInfo.length === packetInfo.length);
|
|
84
|
-
assert(messageChunk.length === packetInfo.length);
|
|
85
|
-
}
|
|
86
|
-
// reset
|
|
87
|
-
this.currentLength = 0;
|
|
88
|
-
this.expectedLength = 0;
|
|
89
|
-
|
|
90
|
-
this.emit("message", messageChunk);
|
|
91
|
-
} else {
|
|
92
|
-
// there is more data in this chunk than expected...
|
|
93
|
-
// the chunk need to be split
|
|
94
|
-
const size1 = this.expectedLength - this.currentLength;
|
|
95
|
-
if (size1 > 0) {
|
|
96
|
-
const chunk1 = data.slice(0, size1);
|
|
97
|
-
this.feed(chunk1);
|
|
98
|
-
}
|
|
99
|
-
const chunk2 = data.slice(size1);
|
|
100
|
-
if (chunk2.length > 0) {
|
|
101
|
-
this.feed(chunk2);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private _readPacketInfo(data: Buffer) {
|
|
107
|
-
return this.readMessageFunc(data);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private _buildData(data: Buffer) {
|
|
111
|
-
if (data && this._stack.length === 0) {
|
|
112
|
-
return data;
|
|
113
|
-
}
|
|
114
|
-
if (!data && this._stack.length === 1) {
|
|
115
|
-
data = this._stack[0];
|
|
116
|
-
this._stack.length = 0; // empty stack array
|
|
117
|
-
return data;
|
|
118
|
-
}
|
|
119
|
-
this._stack.push(data);
|
|
120
|
-
data = Buffer.concat(this._stack);
|
|
121
|
-
this._stack.length = 0;
|
|
122
|
-
return data;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
1
|
+
import { EventEmitter } from "events";
|
|
2
|
+
import { assert } from "node-opcua-assert";
|
|
3
|
+
|
|
4
|
+
const doDebug = false;
|
|
5
|
+
|
|
6
|
+
/***
|
|
7
|
+
* @class PacketAssembler
|
|
8
|
+
* @constructor
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface MessageHeader {
|
|
12
|
+
msgType: string;
|
|
13
|
+
isFinal: string;
|
|
14
|
+
length: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface PacketInfo {
|
|
18
|
+
length: number;
|
|
19
|
+
messageHeader: MessageHeader;
|
|
20
|
+
extra: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type ReadMessageFuncType = (data: Buffer) => PacketInfo;
|
|
24
|
+
|
|
25
|
+
export interface PacketAssemblerOptions {
|
|
26
|
+
readMessageFunc: ReadMessageFuncType;
|
|
27
|
+
|
|
28
|
+
// the minimum number of bytes that need to be received before the readMessageFunc can be called
|
|
29
|
+
minimumSizeInBytes: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class PacketAssembler extends EventEmitter {
|
|
33
|
+
private readonly _stack: Buffer[];
|
|
34
|
+
private expectedLength: number;
|
|
35
|
+
private currentLength: number;
|
|
36
|
+
private readonly readMessageFunc: ReadMessageFuncType;
|
|
37
|
+
private readonly minimumSizeInBytes: number;
|
|
38
|
+
private packetInfo?: PacketInfo;
|
|
39
|
+
|
|
40
|
+
constructor(options: PacketAssemblerOptions) {
|
|
41
|
+
super();
|
|
42
|
+
this._stack = [];
|
|
43
|
+
this.expectedLength = 0;
|
|
44
|
+
this.currentLength = 0;
|
|
45
|
+
this.readMessageFunc = options.readMessageFunc;
|
|
46
|
+
this.minimumSizeInBytes = options.minimumSizeInBytes || 8;
|
|
47
|
+
assert(typeof this.readMessageFunc === "function", "packet assembler requires a readMessageFunc");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public feed(data: Buffer) {
|
|
51
|
+
let messageChunk;
|
|
52
|
+
|
|
53
|
+
if (this.expectedLength === 0 && this.currentLength + data.length >= this.minimumSizeInBytes) {
|
|
54
|
+
// we are at a start of a block and there is enough data provided to read the length of the block
|
|
55
|
+
// let's build the whole data block with previous blocks already read.
|
|
56
|
+
if (this._stack.length > 0) {
|
|
57
|
+
data = this._buildData(data);
|
|
58
|
+
this.currentLength = 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// we can extract the expected length here
|
|
62
|
+
this.packetInfo = this._readPacketInfo(data);
|
|
63
|
+
this.expectedLength = this.packetInfo.length;
|
|
64
|
+
assert(this.currentLength === 0);
|
|
65
|
+
assert(this.expectedLength > 0);
|
|
66
|
+
|
|
67
|
+
// we can now emit an event to signal the start of a new packet
|
|
68
|
+
this.emit("newMessage", this.packetInfo, data);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (this.expectedLength === 0 || this.currentLength + data.length < this.expectedLength) {
|
|
72
|
+
this._stack.push(data);
|
|
73
|
+
this.currentLength += data.length;
|
|
74
|
+
// expecting more data to complete current message chunk
|
|
75
|
+
} else if (this.currentLength + data.length === this.expectedLength) {
|
|
76
|
+
this.currentLength += data.length;
|
|
77
|
+
|
|
78
|
+
messageChunk = this._buildData(data);
|
|
79
|
+
|
|
80
|
+
// istanbul ignore next
|
|
81
|
+
if (doDebug) {
|
|
82
|
+
const packetInfo = this._readPacketInfo(messageChunk);
|
|
83
|
+
assert(this.packetInfo && this.packetInfo.length === packetInfo.length);
|
|
84
|
+
assert(messageChunk.length === packetInfo.length);
|
|
85
|
+
}
|
|
86
|
+
// reset
|
|
87
|
+
this.currentLength = 0;
|
|
88
|
+
this.expectedLength = 0;
|
|
89
|
+
|
|
90
|
+
this.emit("message", messageChunk);
|
|
91
|
+
} else {
|
|
92
|
+
// there is more data in this chunk than expected...
|
|
93
|
+
// the chunk need to be split
|
|
94
|
+
const size1 = this.expectedLength - this.currentLength;
|
|
95
|
+
if (size1 > 0) {
|
|
96
|
+
const chunk1 = data.slice(0, size1);
|
|
97
|
+
this.feed(chunk1);
|
|
98
|
+
}
|
|
99
|
+
const chunk2 = data.slice(size1);
|
|
100
|
+
if (chunk2.length > 0) {
|
|
101
|
+
this.feed(chunk2);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private _readPacketInfo(data: Buffer) {
|
|
107
|
+
return this.readMessageFunc(data);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private _buildData(data: Buffer) {
|
|
111
|
+
if (data && this._stack.length === 0) {
|
|
112
|
+
return data;
|
|
113
|
+
}
|
|
114
|
+
if (!data && this._stack.length === 1) {
|
|
115
|
+
data = this._stack[0];
|
|
116
|
+
this._stack.length = 0; // empty stack array
|
|
117
|
+
return data;
|
|
118
|
+
}
|
|
119
|
+
this._stack.push(data);
|
|
120
|
+
data = Buffer.concat(this._stack);
|
|
121
|
+
this._stack.length = 0;
|
|
122
|
+
return data;
|
|
123
|
+
}
|
|
124
|
+
}
|