base64pbstreamparser.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview The default base64-encoded Protobuf stream parser.
  16. *
  17. * A composed parser that first applies base64 stream decoding (see
  18. * {@link goog.net.streams.Base64StreamDecoder}) followed by Protobuf stream
  19. * parsing (see {@link goog.net.streams.PbStreamParser}).
  20. */
  21. goog.module('goog.net.streams.Base64PbStreamParser');
  22. var Base64StreamDecoder = goog.require('goog.net.streams.Base64StreamDecoder');
  23. var PbStreamParser = goog.require('goog.net.streams.PbStreamParser');
  24. var StreamParser = goog.require('goog.net.streams.StreamParser');
  25. var asserts = goog.require('goog.asserts');
  26. /**
  27. * The default base64-encoded Protobuf stream parser.
  28. *
  29. * @constructor
  30. * @struct
  31. * @implements {StreamParser}
  32. * @final
  33. */
  34. var Base64PbStreamParser = function() {
  35. /**
  36. * The current error message, if any.
  37. * @private {?string}
  38. */
  39. this.errorMessage_ = null;
  40. /**
  41. * The current position in the streamed data.
  42. * @private {number}
  43. */
  44. this.streamPos_ = 0;
  45. /**
  46. * Base64 stream decoder
  47. * @private @const {!Base64StreamDecoder}
  48. */
  49. this.base64Decoder_ = new Base64StreamDecoder();
  50. /**
  51. * Protobuf raw bytes stream parser
  52. * @private @const
  53. */
  54. this.pbParser_ = new PbStreamParser();
  55. };
  56. /** @override */
  57. Base64PbStreamParser.prototype.isInputValid = function() {
  58. return this.errorMessage_ === null;
  59. };
  60. /** @override */
  61. Base64PbStreamParser.prototype.getErrorMessage = function() {
  62. return this.errorMessage_;
  63. };
  64. /**
  65. * @param {string} input The current input string to be processed
  66. * @param {string} errorMsg Additional error message
  67. * @throws {!Error} Throws an error indicating where the stream is broken
  68. * @private
  69. */
  70. Base64PbStreamParser.prototype.error_ = function(input, errorMsg) {
  71. this.errorMessage_ = 'The stream is broken @' + this.streamPos_ +
  72. '. Error: ' + errorMsg + '. With input:\n' + input;
  73. throw Error(this.errorMessage_);
  74. };
  75. /** @override */
  76. Base64PbStreamParser.prototype.parse = function(input) {
  77. asserts.assertString(input);
  78. if (this.errorMessage_ !== null) {
  79. this.error_(input, 'stream already broken');
  80. }
  81. var result = null;
  82. try {
  83. var rawBytes = this.base64Decoder_.decode(input);
  84. result = (rawBytes === null) ? null : this.pbParser_.parse(rawBytes);
  85. } catch (e) {
  86. this.error_(input, e.message);
  87. }
  88. this.streamPos_ += input.length;
  89. return result;
  90. };
  91. exports = Base64PbStreamParser;