stream-decoder.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. */
  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(maxReadMessageLength) {
  28. this.maxReadMessageLength = maxReadMessageLength;
  29. this.readState = ReadState.NO_DATA;
  30. this.readCompressFlag = Buffer.alloc(1);
  31. this.readPartialSize = Buffer.alloc(4);
  32. this.readSizeRemaining = 4;
  33. this.readMessageSize = 0;
  34. this.readPartialMessage = [];
  35. this.readMessageRemaining = 0;
  36. }
  37. write(data) {
  38. let readHead = 0;
  39. let toRead;
  40. const result = [];
  41. while (readHead < data.length) {
  42. switch (this.readState) {
  43. case ReadState.NO_DATA:
  44. this.readCompressFlag = data.slice(readHead, readHead + 1);
  45. readHead += 1;
  46. this.readState = ReadState.READING_SIZE;
  47. this.readPartialSize.fill(0);
  48. this.readSizeRemaining = 4;
  49. this.readMessageSize = 0;
  50. this.readMessageRemaining = 0;
  51. this.readPartialMessage = [];
  52. break;
  53. case ReadState.READING_SIZE:
  54. toRead = Math.min(data.length - readHead, this.readSizeRemaining);
  55. data.copy(this.readPartialSize, 4 - this.readSizeRemaining, readHead, readHead + toRead);
  56. this.readSizeRemaining -= toRead;
  57. readHead += toRead;
  58. // readSizeRemaining >=0 here
  59. if (this.readSizeRemaining === 0) {
  60. this.readMessageSize = this.readPartialSize.readUInt32BE(0);
  61. if (this.maxReadMessageLength !== -1 && this.readMessageSize > this.maxReadMessageLength) {
  62. throw new Error(`Received message larger than max (${this.readMessageSize} vs ${this.maxReadMessageLength})`);
  63. }
  64. this.readMessageRemaining = this.readMessageSize;
  65. if (this.readMessageRemaining > 0) {
  66. this.readState = ReadState.READING_MESSAGE;
  67. }
  68. else {
  69. const message = Buffer.concat([this.readCompressFlag, this.readPartialSize], 5);
  70. this.readState = ReadState.NO_DATA;
  71. result.push(message);
  72. }
  73. }
  74. break;
  75. case ReadState.READING_MESSAGE:
  76. toRead = Math.min(data.length - readHead, this.readMessageRemaining);
  77. this.readPartialMessage.push(data.slice(readHead, readHead + toRead));
  78. this.readMessageRemaining -= toRead;
  79. readHead += toRead;
  80. // readMessageRemaining >=0 here
  81. if (this.readMessageRemaining === 0) {
  82. // At this point, we have read a full message
  83. const framedMessageBuffers = [
  84. this.readCompressFlag,
  85. this.readPartialSize,
  86. ].concat(this.readPartialMessage);
  87. const framedMessage = Buffer.concat(framedMessageBuffers, this.readMessageSize + 5);
  88. this.readState = ReadState.NO_DATA;
  89. result.push(framedMessage);
  90. }
  91. break;
  92. default:
  93. throw new Error('Unexpected read state');
  94. }
  95. }
  96. return result;
  97. }
  98. }
  99. exports.StreamDecoder = StreamDecoder;
  100. //# sourceMappingURL=stream-decoder.js.map