1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.StreamDecoder = void 0;
|
20 | var ReadState;
|
21 | (function (ReadState) {
|
22 | ReadState[ReadState["NO_DATA"] = 0] = "NO_DATA";
|
23 | ReadState[ReadState["READING_SIZE"] = 1] = "READING_SIZE";
|
24 | ReadState[ReadState["READING_MESSAGE"] = 2] = "READING_MESSAGE";
|
25 | })(ReadState || (ReadState = {}));
|
26 | class StreamDecoder {
|
27 | constructor() {
|
28 | this.readState = ReadState.NO_DATA;
|
29 | this.readCompressFlag = Buffer.alloc(1);
|
30 | this.readPartialSize = Buffer.alloc(4);
|
31 | this.readSizeRemaining = 4;
|
32 | this.readMessageSize = 0;
|
33 | this.readPartialMessage = [];
|
34 | this.readMessageRemaining = 0;
|
35 | }
|
36 | write(data) {
|
37 | let readHead = 0;
|
38 | let toRead;
|
39 | const result = [];
|
40 | while (readHead < data.length) {
|
41 | switch (this.readState) {
|
42 | case ReadState.NO_DATA:
|
43 | this.readCompressFlag = data.slice(readHead, readHead + 1);
|
44 | readHead += 1;
|
45 | this.readState = ReadState.READING_SIZE;
|
46 | this.readPartialSize.fill(0);
|
47 | this.readSizeRemaining = 4;
|
48 | this.readMessageSize = 0;
|
49 | this.readMessageRemaining = 0;
|
50 | this.readPartialMessage = [];
|
51 | break;
|
52 | case ReadState.READING_SIZE:
|
53 | toRead = Math.min(data.length - readHead, this.readSizeRemaining);
|
54 | data.copy(this.readPartialSize, 4 - this.readSizeRemaining, readHead, readHead + toRead);
|
55 | this.readSizeRemaining -= toRead;
|
56 | readHead += toRead;
|
57 |
|
58 | if (this.readSizeRemaining === 0) {
|
59 | this.readMessageSize = this.readPartialSize.readUInt32BE(0);
|
60 | this.readMessageRemaining = this.readMessageSize;
|
61 | if (this.readMessageRemaining > 0) {
|
62 | this.readState = ReadState.READING_MESSAGE;
|
63 | }
|
64 | else {
|
65 | const message = Buffer.concat([this.readCompressFlag, this.readPartialSize], 5);
|
66 | this.readState = ReadState.NO_DATA;
|
67 | result.push(message);
|
68 | }
|
69 | }
|
70 | break;
|
71 | case ReadState.READING_MESSAGE:
|
72 | toRead = Math.min(data.length - readHead, this.readMessageRemaining);
|
73 | this.readPartialMessage.push(data.slice(readHead, readHead + toRead));
|
74 | this.readMessageRemaining -= toRead;
|
75 | readHead += toRead;
|
76 |
|
77 | if (this.readMessageRemaining === 0) {
|
78 |
|
79 | const framedMessageBuffers = [
|
80 | this.readCompressFlag,
|
81 | this.readPartialSize,
|
82 | ].concat(this.readPartialMessage);
|
83 | const framedMessage = Buffer.concat(framedMessageBuffers, this.readMessageSize + 5);
|
84 | this.readState = ReadState.NO_DATA;
|
85 | result.push(framedMessage);
|
86 | }
|
87 | break;
|
88 | default:
|
89 | throw new Error('Unexpected read state');
|
90 | }
|
91 | }
|
92 | return result;
|
93 | }
|
94 | }
|
95 | exports.StreamDecoder = StreamDecoder;
|
96 |
|
\ | No newline at end of file |