stream-decoder.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2019 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. enum ReadState {
  18. NO_DATA,
  19. READING_SIZE,
  20. READING_MESSAGE,
  21. }
  22. export class StreamDecoder {
  23. private readState: ReadState = ReadState.NO_DATA;
  24. private readCompressFlag: Buffer = Buffer.alloc(1);
  25. private readPartialSize: Buffer = Buffer.alloc(4);
  26. private readSizeRemaining = 4;
  27. private readMessageSize = 0;
  28. private readPartialMessage: Buffer[] = [];
  29. private readMessageRemaining = 0;
  30. constructor(private maxReadMessageLength: number) {}
  31. write(data: Buffer): Buffer[] {
  32. let readHead = 0;
  33. let toRead: number;
  34. const result: Buffer[] = [];
  35. while (readHead < data.length) {
  36. switch (this.readState) {
  37. case ReadState.NO_DATA:
  38. this.readCompressFlag = data.slice(readHead, readHead + 1);
  39. readHead += 1;
  40. this.readState = ReadState.READING_SIZE;
  41. this.readPartialSize.fill(0);
  42. this.readSizeRemaining = 4;
  43. this.readMessageSize = 0;
  44. this.readMessageRemaining = 0;
  45. this.readPartialMessage = [];
  46. break;
  47. case ReadState.READING_SIZE:
  48. toRead = Math.min(data.length - readHead, this.readSizeRemaining);
  49. data.copy(
  50. this.readPartialSize,
  51. 4 - this.readSizeRemaining,
  52. readHead,
  53. readHead + toRead
  54. );
  55. this.readSizeRemaining -= toRead;
  56. readHead += toRead;
  57. // readSizeRemaining >=0 here
  58. if (this.readSizeRemaining === 0) {
  59. this.readMessageSize = this.readPartialSize.readUInt32BE(0);
  60. if (this.maxReadMessageLength !== -1 && this.readMessageSize > this.maxReadMessageLength) {
  61. throw new Error(`Received message larger than max (${this.readMessageSize} vs ${this.maxReadMessageLength})`);
  62. }
  63. this.readMessageRemaining = this.readMessageSize;
  64. if (this.readMessageRemaining > 0) {
  65. this.readState = ReadState.READING_MESSAGE;
  66. } else {
  67. const message = Buffer.concat(
  68. [this.readCompressFlag, this.readPartialSize],
  69. 5
  70. );
  71. this.readState = ReadState.NO_DATA;
  72. result.push(message);
  73. }
  74. }
  75. break;
  76. case ReadState.READING_MESSAGE:
  77. toRead = Math.min(data.length - readHead, this.readMessageRemaining);
  78. this.readPartialMessage.push(data.slice(readHead, readHead + toRead));
  79. this.readMessageRemaining -= toRead;
  80. readHead += toRead;
  81. // readMessageRemaining >=0 here
  82. if (this.readMessageRemaining === 0) {
  83. // At this point, we have read a full message
  84. const framedMessageBuffers = [
  85. this.readCompressFlag,
  86. this.readPartialSize,
  87. ].concat(this.readPartialMessage);
  88. const framedMessage = Buffer.concat(
  89. framedMessageBuffers,
  90. this.readMessageSize + 5
  91. );
  92. this.readState = ReadState.NO_DATA;
  93. result.push(framedMessage);
  94. }
  95. break;
  96. default:
  97. throw new Error('Unexpected read state');
  98. }
  99. }
  100. return result;
  101. }
  102. }