wirev8.js 4.2 KB

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