src_protobuf_js_6_common.js.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/protobuf_js_6_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_6_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. var conversion_options = {
  69. defaults: true,
  70. bytes: options.binaryAsBase64 ? String : Buffer,
  71. longs: options.longsAsStrings ? String : null,
  72. enums: options.enumsAsStrings ? String : null,
  73. oneofs: true
  74. };
  75. /**
  76. * Deserialize a buffer to a message object
  77. * @param {Buffer} arg_buf The buffer to deserialize
  78. * @return {cls} The resulting object
  79. */
  80. return function deserialize(arg_buf) {
  81. return cls.toObject(cls.decode(arg_buf), conversion_options);
  82. };
  83. };
  84. var deserializeCls = exports.deserializeCls;
  85. /**
  86. * Get a function that serializes objects to a buffer by protobuf class.
  87. * @param {function()} Cls The constructor of the message type to serialize
  88. * @return {function(Cls):Buffer} The serialization function
  89. */
  90. exports.serializeCls = function serializeCls(cls) {
  91. /**
  92. * Serialize an object to a Buffer
  93. * @param {Object} arg The object to serialize
  94. * @return {Buffer} The serialized object
  95. */
  96. return function serialize(arg) {
  97. var message = cls.fromObject(arg);
  98. return cls.encode(message).finish();
  99. };
  100. };
  101. var serializeCls = exports.serializeCls;
  102. /**
  103. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  104. * @param {ProtoBuf.ReflectionObject} value The value to get the name of
  105. * @return {string} The fully qualified name of the value
  106. */
  107. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  108. if (value === null || value === undefined) {
  109. return '';
  110. }
  111. var name = value.name;
  112. var parent_fqn = fullyQualifiedName(value.parent);
  113. if (parent_fqn !== '') {
  114. name = parent_fqn + '.' + name;
  115. }
  116. return name;
  117. };
  118. var fullyQualifiedName = exports.fullyQualifiedName;
  119. /**
  120. * Return a map from method names to method attributes for the service.
  121. * @param {ProtoBuf.Service} service The service to get attributes for
  122. * @param {Object=} options Options to apply to these attributes
  123. * @return {Object} The attributes map
  124. */
  125. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
  126. options) {
  127. var prefix = '/' + fullyQualifiedName(service) + '/';
  128. service.resolveAll();
  129. return _.zipObject(_.map(service.methods, function(method) {
  130. return _.camelCase(method.name);
  131. }), _.map(service.methods, function(method) {
  132. return {
  133. originalName: method.name,
  134. path: prefix + method.name,
  135. requestStream: !!method.requestStream,
  136. responseStream: !!method.responseStream,
  137. requestType: method.resolvedRequestType,
  138. responseType: method.resolvedResponseType,
  139. requestSerialize: serializeCls(method.resolvedRequestType),
  140. requestDeserialize: deserializeCls(method.resolvedRequestType, options),
  141. responseSerialize: serializeCls(method.resolvedResponseType),
  142. responseDeserialize: deserializeCls(method.resolvedResponseType, options)
  143. };
  144. }));
  145. };
  146. var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
  147. exports.loadObject = function loadObject(value, options) {
  148. var result = {};
  149. if (!value) {
  150. return value;
  151. }
  152. if (value.hasOwnProperty('methods')) {
  153. // It's a service object
  154. var service_attrs = getProtobufServiceAttrs(value, options);
  155. return client.makeClientConstructor(service_attrs);
  156. }
  157. if (value.hasOwnProperty('nested')) {
  158. // It's a namespace or root object
  159. _.each(value.nested, function(nested, name) {
  160. result[name] = loadObject(nested, options);
  161. });
  162. return result;
  163. }
  164. // Otherwise, it's not something we need to change
  165. return value;
  166. };
  167. /**
  168. * The primary purpose of this method is to distinguish between reflection
  169. * objects from different versions of ProtoBuf.js. This is just a heuristic,
  170. * checking for properties that are (currently) specific to this version of
  171. * ProtoBuf.js
  172. * @param {Object} obj The object to check
  173. * @return {boolean} Whether the object appears to be a Protobuf.js 6
  174. * ReflectionObject
  175. */
  176. exports.isProbablyProtobufJs6 = function isProbablyProtobufJs6(obj) {
  177. return (typeof obj.root === 'object') &amp;&amp; (typeof obj.resolve === 'function');
  178. };
  179. </code></pre>
  180. </article>
  181. </section>
  182. </div>
  183. <nav>
  184. <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>
  185. </nav>
  186. <br class="clear">
  187. <footer>
  188. 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)
  189. </footer>
  190. <script> prettyPrint(); </script>
  191. <script src="scripts/linenumber.js"> </script>
  192. </body>
  193. </html>