Debian Codegraph @

closure-library/closure/goog/net/streams/jsonstreamparser.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 JSON stream parser.
17 *
18 * The default JSON parser decodes the input stream (string) under the
19 * following rules:
20 * 1. The stream represents a valid JSON array (must start with a "[" and close
21 * with the corresponding "]"). Each element of this array is assumed to be
22 * either an array or an object, and will be decoded as a JS object and
23 * delivered.
24 * 2. All JSON elements in the buffer will be decoded and delivered in a batch.
25 * 3. If a high-level API does not support batch delivery (e.g. grpc), then
26 * a wrapper is expected to deliver individual elements separately
27 * and in order.
28 * 4. The parser is expected to drop any data (without breaking the
29 * specified MIME format) that is not visible to the client: e.g. new lines
30 * for pretty printing; no-op data for keep-alive support.
31 * 5. Fail-fast: any invalid content should abort the stream by setting the
32 * state of the parser to "invalid".
33 *
34 * The parser is a streamed JSON parser and is optimized in such a way
35 * that it only scans the message boundary and the actual decoding of JSON
36 * strings and construction of JS object are done by JSON.parse (native
37 * code).
38 */
39
40 goog.provide('goog.net.streams.JsonStreamParser');
41 goog.provide('goog.net.streams.JsonStreamParser.Options');
42
43 goog.require('goog.asserts');
44 goog.require('goog.net.streams.StreamParser');
45 goog.require('goog.net.streams.utils');
46
47
48 goog.scope(function() {
49
50
51 var utils = goog.module.get('goog.net.streams.utils');
52
53
54 /**
55 * The default JSON stream parser.
56 *
57 * @param {!goog.net.streams.JsonStreamParser.Options=} opt_options
58 * Configuration for the new JsonStreamParser instance.
59 * @constructor
60 * @struct
61 * @implements {goog.net.streams.StreamParser}
62 * @final
63 * @package
64 */
65 goog.net.streams.JsonStreamParser = function(opt_options) {
66 /**
67 * The current error message, if any.
68 * @private {?string}
69 */
70 this.errorMessage_ = null;
71
72 /**
73 * The currently buffered result (parsed JSON objects).
74 * @private {!Array<string|!Object>}
75 */
76 this.result_ = [];
77
78 /**
79 * The currently buffered input.
80 * @private {string}
81 */
82 this.buffer_ = '';
83
84 /**
85 * The current stack.
86 * @private {!Array<!Parser.State_>}
87 */
88 this.stack_ = [];
89
90 /**
91 * The current depth of the nested JSON structure.
92 * @private {number}
93 */
94 this.depth_ = 0;
95
96 /**
97 * The current position in the streamed data.
98 * @private {number}
99 */
100 this.pos_ = 0;
101
102 /**
103 * The current state of whether the parser is decoding a '\' escaped string.
104 * @private {boolean}
105 */
106 this.slashed_ = false;
107
108 /**
109 * The current unicode char count. 0 means no unicode, 1-4 otherwise.
110 * @private {number}
111 */
112 this.unicodeCount_ = 0;
113
114 /**
115 * The regexp for parsing string input.
116 * @private {!RegExp}
117 */
118 this.stringInputPattern_ = /[\\"]/g;
119
120 /**
121 * The current stream state.
122 * @private {goog.net.streams.JsonStreamParser.StreamState_}
123 */
124 this.streamState_ = Parser.StreamState_.INIT;
125
126 /**
127 * The current parser state.
128 * @private {goog.net.streams.JsonStreamParser.State_}
129 */
130 this.state_ = Parser.State_.INIT;
131
132 /**
133 * Whether to deliver the raw message string without decoding into JS object.
134 * @private {boolean}
135 */
136 this.deliverMessageAsRawString_ =
137 !!(opt_options && opt_options.deliverMessageAsRawString);
138 };
139
140
141 /**
142 * Configuration spec for newly created JSON stream parser:
143 *
144 * allowCompactJsonArrayFormat: ignored.
145 *
146 * deliverMessageAsRawString: whether to deliver the raw message string without
147 * decoding into JS object. Semantically insignificant whitespaces in the
148 * input may be kept or ignored.
149 *
150 * @typedef {{
151 * allowCompactJsonArrayFormat: (boolean|undefined),
152 * deliverMessageAsRawString: (boolean|undefined),
153 * }}
154 */
155 goog.net.streams.JsonStreamParser.Options;
156
157
158 var Parser = goog.net.streams.JsonStreamParser;
159
160
161 /**
162 * The stream state.
163 * @private @enum {number}
164 */
165 Parser.StreamState_ = {
166 INIT: 0,
167 ARRAY_OPEN: 1,
168 ARRAY_END: 2,
169 INVALID: 3
170 };
171
172
173 /**
174 * The parser state.
175 * @private @enum {number}
176 */
177 Parser.State_ = {
178 INIT: 0,
179 VALUE: 1,
180 OBJECT_OPEN: 2,
181 OBJECT_END: 3,
182 ARRAY_OPEN: 4,
183 ARRAY_END: 5,
184 STRING: 6,
185 KEY_START: 7,
186 KEY_END: 8,
187 TRUE1: 9, // T and expecting RUE ...
188 TRUE2: 10,
189 TRUE3: 11,
190 FALSE1: 12, // F and expecting ALSE ...
191 FALSE2: 13,
192 FALSE3: 14,
193 FALSE4: 15,
194 NULL1: 16, // N and expecting ULL ...
195 NULL2: 17,
196 NULL3: 18,
197 NUM_DECIMAL_POINT: 19,
198 NUM_DIGIT: 20
199 };
200
201
202 /**
203 * @override
204 */
205 Parser.prototype.isInputValid = function() {
206 return this.streamState_ != Parser.StreamState_.INVALID;
207 };
208
209
210 /**
211 * @override
212 */
213 Parser.prototype.getErrorMessage = function() {
214 return this.errorMessage_;
215 };
216
217
218 /**
219 * @return {boolean} Whether the parser has reached the end of the stream
220 *
221 * TODO(updogliu): move this API to the base type.
222 */
223 Parser.prototype.done = function() {
224 return this.streamState_ === Parser.StreamState_.ARRAY_END;
225 };
226
227
228 /**
229 * Get the part of input that is after the end of the stream. Call this only
230 * when {@code this.done()} is true.
231 *
232 * @return {string} The extra input
233 *
234 * TODO(updogliu): move this API to the base type.
235 */
236 Parser.prototype.getExtraInput = function() {
237 return this.buffer_;
238 };
239
240
241 /**
242 * @param {string|!ArrayBuffer|!Array<number>} input
243 * The current input string (always)
244 * @param {number} pos The position in the current input that triggers the error
245 * @throws {!Error} Throws an error indicating where the stream is broken
246 * @private
247 */
248 Parser.prototype.error_ = function(input, pos) {
249 this.streamState_ = Parser.StreamState_.INVALID;
250 this.errorMessage_ = 'The stream is broken @' + this.pos_ + '/' + pos +
251 '. With input:\n' + input;
252 throw new Error(this.errorMessage_);
253 };
254
255
256 /**
257 * @throws {Error} Throws an error message if the input is invalid.
258 * @override
259 */
260 Parser.prototype.parse = function(input) {
261 goog.asserts.assertString(input);
262
263 // captures
264 var parser = this;
265 var stack = parser.stack_;
266 var pattern = parser.stringInputPattern_;
267 var State = Parser.State_; // enums
268
269 var num = input.length;
270
271 var streamStart = 0;
272
273 var msgStart = -1;
274
275 var i = 0;
276
277 while (i < num) {
278 switch (parser.streamState_) {
279 case Parser.StreamState_.INVALID:
280 parser.error_(input, i);
281 return null;
282
283 case Parser.StreamState_.ARRAY_END:
284 if (readMore()) {
285 parser.error_(input, i);
286 }
287 return null;
288
289 case Parser.StreamState_.INIT:
290 if (readMore()) {
291 var current = input[i++];
292 parser.pos_++;
293
294 if (current === '[') {
295 parser.streamState_ = Parser.StreamState_.ARRAY_OPEN;
296
297 streamStart = i;
298 parser.state_ = State.ARRAY_OPEN;
299
300 continue;
301 } else {
302 parser.error_(input, i);
303 }
304 }
305 return null;
306
307 case Parser.StreamState_.ARRAY_OPEN:
308 parseData();
309
310 if (parser.depth_ === 0 && parser.state_ == State.ARRAY_END) {
311 parser.streamState_ = Parser.StreamState_.ARRAY_END;
312 parser.buffer_ = input.substring(i);
313 } else {
314 if (msgStart === -1) {
315 parser.buffer_ += input.substring(streamStart);
316 } else {
317 parser.buffer_ = input.substring(msgStart);
318 }
319 }
320
321 if (parser.result_.length > 0) {
322 var msgs = parser.result_;
323 parser.result_ = [];
324 return msgs;
325 }
326 return null;
327 }
328 }
329
330 return null;
331
332 /**
333 * @return {boolean} true if the parser needs parse more data
334 */
335 function readMore() {
336 skipWhitespace();
337 return i < num;
338 }
339
340 /**
341 * Skip as many whitespaces as possible, and increments current index of
342 * stream to next available char.
343 */
344 function skipWhitespace() {
345 while (i < input.length) {
346 if (utils.isJsonWhitespace(input[i])) {
347 i++;
348 parser.pos_++;
349 continue;
350 }
351 break;
352 }
353 }
354
355 /**
356 * Parse the input JSON elements with a streamed state machine.
357 */
358 function parseData() {
359 var current;
360
361 while (true) {
362 current = input[i++];
363 if (!current) {
364 break;
365 }
366
367 parser.pos_++;
368
369 switch (parser.state_) {
370 case State.INIT:
371 if (current === '{') {
372 parser.state_ = State.OBJECT_OPEN;
373 } else if (current === '[') {
374 parser.state_ = State.ARRAY_OPEN;
375 } else if (!utils.isJsonWhitespace(current)) {
376 parser.error_(input, i);
377 }
378 continue;
379
380 case State.KEY_START:
381 case State.OBJECT_OPEN:
382 if (utils.isJsonWhitespace(current)) {
383 continue;
384 }
385 if (parser.state_ === State.KEY_START) {
386 stack.push(State.KEY_END);
387 } else {
388 if (current === '}') {
389 addMessage('{}');
390 parser.state_ = nextState();
391 continue;
392 } else {
393 stack.push(State.OBJECT_END);
394 }
395 }
396 if (current === '"') {
397 parser.state_ = State.STRING;
398 } else {
399 parser.error_(input, i);
400 }
401 continue;
402
403
404 case State.KEY_END:
405 case State.OBJECT_END:
406 if (utils.isJsonWhitespace(current)) {
407 continue;
408 }
409 if (current === ':') {
410 if (parser.state_ === State.OBJECT_END) {
411 stack.push(State.OBJECT_END);
412 parser.depth_++;
413 }
414 parser.state_ = State.VALUE;
415 } else if (current === '}') {
416 parser.depth_--;
417 addMessage();
418 parser.state_ = nextState();
419 } else if (current === ',') {
420 if (parser.state_ === State.OBJECT_END) {
421 stack.push(State.OBJECT_END);
422 }
423 parser.state_ = State.KEY_START;
424 } else {
425 parser.error_(input, i);
426 }
427 continue;
428
429 case State.ARRAY_OPEN:
430 case State.VALUE:
431 if (utils.isJsonWhitespace(current)) {
432 continue;
433 }
434 if (parser.state_ === State.ARRAY_OPEN) {
435 parser.depth_++;
436 parser.state_ = State.VALUE;
437 if (current === ']') {
438 parser.depth_--;
439 if (parser.depth_ === 0) {
440 parser.state_ = State.ARRAY_END;
441 return;
442 }
443
444 addMessage('[]');
445
446 parser.state_ = nextState();
447 continue;
448 } else {
449 stack.push(State.ARRAY_END);
450 }
451 }
452 if (current === '"')
453 parser.state_ = State.STRING;
454 else if (current === '{')
455 parser.state_ = State.OBJECT_OPEN;
456 else if (current === '[')
457 parser.state_ = State.ARRAY_OPEN;
458 else if (current === 't')
459 parser.state_ = State.TRUE1;
460 else if (current === 'f')
461 parser.state_ = State.FALSE1;
462 else if (current === 'n')
463 parser.state_ = State.NULL1;
464 else if (current === '-') {
465 // continue
466 } else if ('0123456789'.indexOf(current) !== -1) {
467 parser.state_ = State.NUM_DIGIT;
468 } else {
469 parser.error_(input, i);
470 }
471 continue;
472
473 case State.ARRAY_END:
474 if (current === ',') {
475 stack.push(State.ARRAY_END);
476 parser.state_ = State.VALUE;
477
478 if (parser.depth_ === 1) {
479 msgStart = i; // skip ',', including a leading one
480 }
481 } else if (current === ']') {
482 parser.depth_--;
483 if (parser.depth_ === 0) {
484 return;
485 }
486
487 addMessage();
488 parser.state_ = nextState();
489 } else if (utils.isJsonWhitespace(current)) {
490 continue;
491 } else {
492 parser.error_(input, i);
493 }
494 continue;
495
496 case State.STRING:
497 var old = i;
498
499 STRING_LOOP: while (true) {
500 while (parser.unicodeCount_ > 0) {
501 current = input[i++];
502 if (parser.unicodeCount_ === 4) {
503 parser.unicodeCount_ = 0;
504 } else {
505 parser.unicodeCount_++;
506 }
507 if (!current) {
508 break STRING_LOOP;
509 }
510 }
511
512 if (current === '"' && !parser.slashed_) {
513 parser.state_ = nextState();
514 break;
515 }
516 if (current === '\\' && !parser.slashed_) {
517 parser.slashed_ = true;
518 current = input[i++];
519 if (!current) {
520 break;
521 }
522 }
523 if (parser.slashed_) {
524 parser.slashed_ = false;
525 if (current === 'u') {
526 parser.unicodeCount_ = 1;
527 }
528 current = input[i++];
529 if (!current) {
530 break;
531 } else {
532 continue;
533 }
534 }
535
536 pattern.lastIndex = i;
537 var patternResult = pattern.exec(input);
538 if (!patternResult) {
539 i = input.length + 1;
540 break;
541 }
542 i = patternResult.index + 1;
543 current = input[patternResult.index];
544 if (!current) {
545 break;
546 }
547 }
548
549 parser.pos_ += (i - old);
550
551 continue;
552
553 case State.TRUE1:
554 if (!current) {
555 continue;
556 }
557 if (current === 'r') {
558 parser.state_ = State.TRUE2;
559 } else {
560 parser.error_(input, i);
561 }
562 continue;
563
564 case State.TRUE2:
565 if (!current) {
566 continue;
567 }
568 if (current === 'u') {
569 parser.state_ = State.TRUE3;
570 } else {
571 parser.error_(input, i);
572 }
573 continue;
574
575 case State.TRUE3:
576 if (!current) {
577 continue;
578 }
579 if (current === 'e') {
580 parser.state_ = nextState();
581 } else {
582 parser.error_(input, i);
583 }
584 continue;
585
586 case State.FALSE1:
587 if (!current) {
588 continue;
589 }
590 if (current === 'a') {
591 parser.state_ = State.FALSE2;
592 } else {
593 parser.error_(input, i);
594 }
595 continue;
596
597 case State.FALSE2:
598 if (!current) {
599 continue;
600 }
601 if (current === 'l') {
602 parser.state_ = State.FALSE3;
603 } else {
604 parser.error_(input, i);
605 }
606 continue;
607
608 case State.FALSE3:
609 if (!current) {
610 continue;
611 }
612 if (current === 's') {
613 parser.state_ = State.FALSE4;
614 } else {
615 parser.error_(input, i);
616 }
617 continue;
618
619 case State.FALSE4:
620 if (!current) {
621 continue;
622 }
623 if (current === 'e') {
624 parser.state_ = nextState();
625 } else {
626 parser.error_(input, i);
627 }
628 continue;
629
630 case State.NULL1:
631 if (!current) {
632 continue;
633 }
634 if (current === 'u') {
635 parser.state_ = State.NULL2;
636 } else {
637 parser.error_(input, i);
638 }
639 continue;
640
641 case State.NULL2:
642 if (!current) {
643 continue;
644 }
645 if (current === 'l') {
646 parser.state_ = State.NULL3;
647 } else {
648 parser.error_(input, i);
649 }
650 continue;
651
652 case State.NULL3:
653 if (!current) {
654 continue;
655 }
656 if (current === 'l') {
657 parser.state_ = nextState();
658 } else {
659 parser.error_(input, i);
660 }
661 continue;
662
663 case State.NUM_DECIMAL_POINT:
664 if (current === '.') {
665 parser.state_ = State.NUM_DIGIT;
666 } else {
667 parser.error_(input, i);
668 }
669 continue;
670
671 case State.NUM_DIGIT: // no need for a full validation here
672 if ('0123456789.eE+-'.indexOf(current) !== -1) {
673 continue;
674 } else {
675 i--;
676 parser.pos_--;
677 parser.state_ = nextState();
678 }
679 continue;
680
681 default:
682 parser.error_(input, i);
683 }
684 }
685 }
686
687 /**
688 * @return {!goog.net.streams.JsonStreamParser.State_} the next state
689 * from the stack, or the general VALUE state.
690 */
691 function nextState() {
692 var state = stack.pop();
693 if (state != null) {
694 return state;
695 } else {
696 return State.VALUE;
697 }
698 }
699
700 /**
701 * @param {(string)=} opt_data The message to add
702 */
703 function addMessage(opt_data) {
704 if (parser.depth_ > 1) {
705 return;
706 }
707
708 goog.asserts.assert(opt_data !== ''); // '' not possible
709
710 if (!opt_data) {
711 if (msgStart === -1) {
712 opt_data = parser.buffer_ + input.substring(streamStart, i);
713 } else {
714 opt_data = input.substring(msgStart, i);
715 }
716 }
717
718 if (parser.deliverMessageAsRawString_) {
719 parser.result_.push(opt_data);
720 } else {
721 parser.result_.push(
722 goog.asserts.assertInstanceof(JSON.parse(opt_data), Object));
723 }
724 msgStart = i;
725 }
726 };
727
728 }); // goog.scope