Debian Codegraph @

closure-library/closure/goog/net/streams/base64pbstreamparser.js

raw
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 /**
16 * @fileoverview The default base64-encoded Protobuf stream parser.
17 *
18 * A composed parser that first applies base64 stream decoding (see
19 * {@link goog.net.streams.Base64StreamDecoder}) followed by Protobuf stream
20 * parsing (see {@link goog.net.streams.PbStreamParser}).
21 */
22
23 goog.module('goog.net.streams.Base64PbStreamParser');
24
25 var Base64StreamDecoder = goog.require('goog.net.streams.Base64StreamDecoder');
26 var PbStreamParser = goog.require('goog.net.streams.PbStreamParser');
27 var StreamParser = goog.require('goog.net.streams.StreamParser');
28 var asserts = goog.require('goog.asserts');
29
30
31 /**
32 * The default base64-encoded Protobuf stream parser.
33 *
34 * @constructor
35 * @struct
36 * @implements {StreamParser}
37 * @final
38 */
39 var Base64PbStreamParser = function() {
40 /**
41 * The current error message, if any.
42 * @private {?string}
43 */
44 this.errorMessage_ = null;
45
46 /**
47 * The current position in the streamed data.
48 * @private {number}
49 */
50 this.streamPos_ = 0;
51
52 /**
53 * Base64 stream decoder
54 * @private @const {!Base64StreamDecoder}
55 */
56 this.base64Decoder_ = new Base64StreamDecoder();
57
58 /**
59 * Protobuf raw bytes stream parser
60 * @private @const
61 */
62 this.pbParser_ = new PbStreamParser();
63 };
64
65
66 /** @override */
67 Base64PbStreamParser.prototype.isInputValid = function() {
68 return this.errorMessage_ === null;
69 };
70
71
72 /** @override */
73 Base64PbStreamParser.prototype.getErrorMessage = function() {
74 return this.errorMessage_;
75 };
76
77
78 /**
79 * @param {string} input The current input string to be processed
80 * @param {string} errorMsg Additional error message
81 * @throws {!Error} Throws an error indicating where the stream is broken
82 * @private
83 */
84 Base64PbStreamParser.prototype.error_ = function(input, errorMsg) {
85 this.errorMessage_ = 'The stream is broken @' + this.streamPos_ +
86 '. Error: ' + errorMsg + '. With input:\n' + input;
87 throw new Error(this.errorMessage_);
88 };
89
90
91 /** @override */
92 Base64PbStreamParser.prototype.parse = function(input) {
93 asserts.assertString(input);
94
95 if (this.errorMessage_ !== null) {
96 this.error_(input, 'stream already broken');
97 }
98
99 var result = null;
100 try {
101 var rawBytes = this.base64Decoder_.decode(input);
102 result = (rawBytes === null) ? null : this.pbParser_.parse(rawBytes);
103 } catch (e) {
104 this.error_(input, e.message);
105 }
106
107 this.streamPos_ += input.length;
108 return result;
109 };
110
111
112 exports = Base64PbStreamParser;