Debian Codegraph @

closure-library/closure/goog/labs/net/webchannel/wirev8.js

raw
1 // Copyright 2013 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 Codec functions of the v8 wire protocol. Eventually we'd want
17 * to support pluggable wire-format to improve wire efficiency and to enable
18 * binary encoding. Such support will require an interface class, which
19 * will be added later.
20 *
21 * @visibility {:internal}
22 */
23
24
25 goog.provide('goog.labs.net.webChannel.WireV8');
26
27 goog.require('goog.asserts');
28 goog.require('goog.json');
29 goog.require('goog.json.NativeJsonProcessor');
30 goog.require('goog.labs.net.webChannel.Wire');
31 goog.require('goog.structs');
32
33
34
35 /**
36 * The v8 codec class.
37 *
38 * @constructor
39 * @struct
40 */
41 goog.labs.net.webChannel.WireV8 = function() {
42 /**
43 * Parser for a response payload. The parser should return an array.
44 * @private {!goog.string.Parser}
45 */
46 this.parser_ = new goog.json.NativeJsonProcessor();
47 };
48
49
50 goog.scope(function() {
51 var WireV8 = goog.labs.net.webChannel.WireV8;
52 var Wire = goog.labs.net.webChannel.Wire;
53
54
55 /**
56 * Encodes a standalone message into the wire format.
57 *
58 * May throw exception if the message object contains any invalid elements.
59 *
60 * @param {!Object|!goog.structs.Map} message The message data.
61 * V8 only support JS objects (or Map).
62 * @param {!Array<string>} buffer The text buffer to write the message to.
63 * @param {string=} opt_prefix The prefix for each field of the object.
64 */
65 WireV8.prototype.encodeMessage = function(message, buffer, opt_prefix) {
66 var prefix = opt_prefix || '';
67 try {
68 goog.structs.forEach(message, function(value, key) {
69 var encodedValue = value;
70 if (goog.isObject(value)) {
71 encodedValue = goog.json.serialize(value);
72 } // keep the fast-path for primitive types
73 buffer.push(prefix + key + '=' + encodeURIComponent(encodedValue));
74 });
75 } catch (ex) {
76 // We send a map here because lots of the retry logic relies on map IDs,
77 // so we have to send something (possibly redundant).
78 buffer.push(
79 prefix + 'type' +
80 '=' + encodeURIComponent('_badmap'));
81 throw ex;
82 }
83 };
84
85
86 /**
87 * Encodes all the buffered messages of the forward channel.
88 *
89 * @param {!Array<Wire.QueuedMap>} messageQueue The message data.
90 * V8 only support JS objects.
91 * @param {number} count The number of messages to be encoded.
92 * @param {?function(!Object)} badMapHandler Callback for bad messages.
93 * @return {string} the encoded messages
94 */
95 WireV8.prototype.encodeMessageQueue = function(
96 messageQueue, count, badMapHandler) {
97 var offset = -1;
98 while (true) {
99 var sb = ['count=' + count];
100 // To save a bit of bandwidth, specify the base mapId and the rest as
101 // offsets from it.
102 if (offset == -1) {
103 if (count > 0) {
104 offset = messageQueue[0].mapId;
105 sb.push('ofs=' + offset);
106 } else {
107 offset = 0;
108 }
109 } else {
110 sb.push('ofs=' + offset);
111 }
112 var done = true;
113 for (var i = 0; i < count; i++) {
114 var mapId = messageQueue[i].mapId;
115 var map = messageQueue[i].map;
116 mapId -= offset;
117 if (mapId < 0) {
118 // redo the encoding in case of retry/reordering, plus extra space
119 offset = Math.max(0, messageQueue[i].mapId - 100);
120 done = false;
121 continue;
122 }
123 try {
124 this.encodeMessage(map, sb, 'req' + mapId + '_');
125 } catch (ex) {
126 if (badMapHandler) {
127 badMapHandler(map);
128 }
129 }
130 }
131 if (done) {
132 return sb.join('&');
133 }
134 }
135 };
136
137
138 /**
139 * Decodes a standalone message received from the wire. May throw exception
140 * if text is ill-formatted.
141 *
142 * Must be valid JSON as it is insecure to use eval() to decode JS literals;
143 * and eval() is disallowed in Chrome apps too.
144 *
145 * Invalid JS literals include null array elements, quotas etc.
146 *
147 * @param {string} messageText The string content as received from the wire.
148 * @return {*} The decoded message object.
149 */
150 WireV8.prototype.decodeMessage = function(messageText) {
151 var response = this.parser_.parse(messageText);
152 goog.asserts.assert(goog.isArray(response)); // throw exception
153 return response;
154 };
155 }); // goog.scope