UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.StreamDecoder = void 0;
20var 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 = {}));
26class 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 // readSizeRemaining >=0 here
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 // readMessageRemaining >=0 here
77 if (this.readMessageRemaining === 0) {
78 // At this point, we have read a full message
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}
95exports.StreamDecoder = StreamDecoder;
96//# sourceMappingURL=stream-decoder.js.map
\No newline at end of file