src_protobuf_js_5_common.js.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/protobuf_js_5_common.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: src/protobuf_js_5_common.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @license
  21. * Copyright 2017, Google Inc.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions are
  26. * met:
  27. *
  28. * * Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * * Redistributions in binary form must reproduce the above
  31. * copyright notice, this list of conditions and the following disclaimer
  32. * in the documentation and/or other materials provided with the
  33. * distribution.
  34. * * Neither the name of Google Inc. nor the names of its
  35. * contributors may be used to endorse or promote products derived from
  36. * this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. */
  51. /**
  52. * @module
  53. * @private
  54. */
  55. 'use strict';
  56. var _ = require('lodash');
  57. var client = require('./client');
  58. /**
  59. * Get a function that deserializes a specific type of protobuf.
  60. * @param {function()} cls The constructor of the message type to deserialize
  61. * @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
  62. * instead of Buffers. Defaults to false
  63. * @param {bool=} longsAsStrings Deserialize long values as strings instead of
  64. * objects. Defaults to true
  65. * @return {function(Buffer):cls} The deserialization function
  66. */
  67. exports.deserializeCls = function deserializeCls(cls, options) {
  68. /**
  69. * Deserialize a buffer to a message object
  70. * @param {Buffer} arg_buf The buffer to deserialize
  71. * @return {cls} The resulting object
  72. */
  73. return function deserialize(arg_buf) {
  74. // Convert to a native object with binary fields as Buffers (first argument)
  75. // and longs as strings (second argument)
  76. return cls.decode(arg_buf).toRaw(options.binaryAsBase64,
  77. options.longsAsStrings);
  78. };
  79. };
  80. var deserializeCls = exports.deserializeCls;
  81. /**
  82. * Get a function that serializes objects to a buffer by protobuf class.
  83. * @param {function()} Cls The constructor of the message type to serialize
  84. * @return {function(Cls):Buffer} The serialization function
  85. */
  86. exports.serializeCls = function serializeCls(Cls) {
  87. /**
  88. * Serialize an object to a Buffer
  89. * @param {Object} arg The object to serialize
  90. * @return {Buffer} The serialized object
  91. */
  92. return function serialize(arg) {
  93. return new Buffer(new Cls(arg).encode().toBuffer());
  94. };
  95. };
  96. var serializeCls = exports.serializeCls;
  97. /**
  98. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  99. * @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
  100. * @return {string} The fully qualified name of the value
  101. */
  102. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  103. if (value === null || value === undefined) {
  104. return '';
  105. }
  106. var name = value.name;
  107. var parent_name = fullyQualifiedName(value.parent);
  108. if (parent_name !== '') {
  109. name = parent_name + '.' + name;
  110. }
  111. return name;
  112. };
  113. var fullyQualifiedName = exports.fullyQualifiedName;
  114. /**
  115. * Return a map from method names to method attributes for the service.
  116. * @param {ProtoBuf.Reflect.Service} service The service to get attributes for
  117. * @param {Object=} options Options to apply to these attributes
  118. * @return {Object} The attributes map
  119. */
  120. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
  121. options) {
  122. var prefix = '/' + fullyQualifiedName(service) + '/';
  123. var binaryAsBase64, longsAsStrings;
  124. if (options) {
  125. binaryAsBase64 = options.binaryAsBase64;
  126. longsAsStrings = options.longsAsStrings;
  127. }
  128. /* This slightly awkward construction is used to make sure we only use
  129. lodash@3.10.1-compatible functions. A previous version used
  130. _.fromPairs, which would be cleaner, but was introduced in lodash
  131. version 4 */
  132. return _.zipObject(_.map(service.children, function(method) {
  133. return _.camelCase(method.name);
  134. }), _.map(service.children, function(method) {
  135. return {
  136. originalName: method.name,
  137. path: prefix + method.name,
  138. requestStream: method.requestStream,
  139. responseStream: method.responseStream,
  140. requestType: method.resolvedRequestType,
  141. responseType: method.resolvedResponseType,
  142. requestSerialize: serializeCls(method.resolvedRequestType.build()),
  143. requestDeserialize: deserializeCls(method.resolvedRequestType.build(),
  144. options),
  145. responseSerialize: serializeCls(method.resolvedResponseType.build()),
  146. responseDeserialize: deserializeCls(method.resolvedResponseType.build(),
  147. options)
  148. };
  149. }));
  150. };
  151. var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
  152. /**
  153. * Load a gRPC object from an existing ProtoBuf.Reflect object.
  154. * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
  155. * @param {Object=} options Options to apply to the loaded object
  156. * @return {Object&lt;string, *>} The resulting gRPC object
  157. */
  158. exports.loadObject = function loadObject(value, options) {
  159. var result = {};
  160. if (!value) {
  161. return value;
  162. }
  163. if (value.hasOwnProperty('ns')) {
  164. return loadObject(value.ns, options);
  165. }
  166. if (value.className === 'Namespace') {
  167. _.each(value.children, function(child) {
  168. result[child.name] = loadObject(child, options);
  169. });
  170. return result;
  171. } else if (value.className === 'Service') {
  172. return client.makeClientConstructor(getProtobufServiceAttrs(value, options),
  173. options);
  174. } else if (value.className === 'Message' || value.className === 'Enum') {
  175. return value.build();
  176. } else {
  177. return value;
  178. }
  179. };
  180. /**
  181. * The primary purpose of this method is to distinguish between reflection
  182. * objects from different versions of ProtoBuf.js. This is just a heuristic,
  183. * checking for properties that are (currently) specific to this version of
  184. * ProtoBuf.js
  185. * @param {Object} obj The object to check
  186. * @return {boolean} Whether the object appears to be a Protobuf.js 5
  187. * ReflectionObject
  188. */
  189. exports.isProbablyProtobufJs5 = function isProbablyProtobufJs5(obj) {
  190. return _.isArray(obj.children) &amp;&amp; (typeof obj.build === 'function');
  191. };
  192. </code></pre>
  193. </article>
  194. </section>
  195. </div>
  196. <nav>
  197. <h2><a href="index.html">Home</a></h2><h3>Externals</h3><ul><li><a href="external-Duplex.html">Duplex</a></li><li><a href="external-EventEmitter.html">EventEmitter</a></li><li><a href="external-GoogleCredential.html">GoogleCredential</a></li><li><a href="external-Readable.html">Readable</a></li><li><a href="external-Writable.html">Writable</a></li></ul><h3>Classes</h3><ul><li><a href="grpc.Client.html">Client</a></li><li><a href="grpc.credentials-CallCredentials.html">CallCredentials</a></li><li><a href="grpc.credentials-ChannelCredentials.html">ChannelCredentials</a></li><li><a href="grpc.Metadata.html">Metadata</a></li><li><a href="grpc.Server.html">Server</a></li><li><a href="grpc.ServerCredentials.html">ServerCredentials</a></li><li><a href="grpc-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="grpc-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="grpc-ClientUnaryCall.html">ClientUnaryCall</a></li><li><a href="grpc-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="grpc-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="grpc-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="grpc-ServerUnaryCall.html">ServerUnaryCall</a></li><li><a href="grpc-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Events</h3><ul><li><a href="grpc-ClientDuplexStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientDuplexStream.html#event:status">status</a></li><li><a href="grpc-ClientReadableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientReadableStream.html#event:status">status</a></li><li><a href="grpc-ClientUnaryCall.html#event:metadata">metadata</a></li><li><a href="grpc-ClientUnaryCall.html#event:status">status</a></li><li><a href="grpc-ClientWritableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientWritableStream.html#event:status">status</a></li><li><a href="grpc-ServerDuplexStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerReadableStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerUnaryCall.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerWritableStream.html#~event:cancelled">cancelled</a></li></ul><h3>Namespaces</h3><ul><li><a href="grpc.html">grpc</a></li><li><a href="grpc.credentials.html">credentials</a></li></ul>
  198. </nav>
  199. <br class="clear">
  200. <footer>
  201. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Wed Jun 28 2017 09:44:06 GMT-0700 (PDT)
  202. </footer>
  203. <script> prettyPrint(); </script>
  204. <script src="scripts/linenumber.js"> </script>
  205. </body>
  206. </html>