src_protobuf_js_6_common.js.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. *
  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. 'use strict';
  52. var _ = require('lodash');
  53. var client = require('./client');
  54. /**
  55. * Get a function that deserializes a specific type of protobuf.
  56. * @param {function()} cls The constructor of the message type to deserialize
  57. * @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
  58. * instead of Buffers. Defaults to false
  59. * @param {bool=} longsAsStrings Deserialize long values as strings instead of
  60. * objects. Defaults to true
  61. * @return {function(Buffer):cls} The deserialization function
  62. */
  63. exports.deserializeCls = function deserializeCls(cls, options) {
  64. var conversion_options = {
  65. defaults: true,
  66. bytes: options.binaryAsBase64 ? String : Buffer,
  67. longs: options.longsAsStrings ? String : null,
  68. enums: options.enumsAsStrings ? String : null,
  69. oneofs: true
  70. };
  71. /**
  72. * Deserialize a buffer to a message object
  73. * @param {Buffer} arg_buf The buffer to deserialize
  74. * @return {cls} The resulting object
  75. */
  76. return function deserialize(arg_buf) {
  77. return cls.decode(arg_buf).toObject(conversion_options);
  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. var message = cls.fromObject(arg);
  94. return cls.encode(message).finish();
  95. };
  96. };
  97. var serializeCls = exports.serializeCls;
  98. /**
  99. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  100. * @param {ProtoBuf.ReflectionObject} value The value to get the name of
  101. * @return {string} The fully qualified name of the value
  102. */
  103. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  104. if (value === null || value === undefined) {
  105. return '';
  106. }
  107. var name = value.name;
  108. var parent_fqn = fullyQualifiedName(value.parent);
  109. if (parent_fqn !== '') {
  110. name = parent_fqn + '.' + name;
  111. }
  112. return name;
  113. };
  114. var fullyQualifiedName = exports.fullyQualifiedName;
  115. /**
  116. * Return a map from method names to method attributes for the service.
  117. * @param {ProtoBuf.Service} service The service to get attributes for
  118. * @param {Object=} options Options to apply to these attributes
  119. * @return {Object} The attributes map
  120. */
  121. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
  122. options) {
  123. var prefix = '/' + fullyQualifiedName(service) + '/';
  124. service.resolveAll();
  125. return _.zipObject(_.map(service.methods, function(method) {
  126. return _.camelCase(method.name);
  127. }), _.map(service.methods, function(method) {
  128. return {
  129. originalName: method.name,
  130. path: prefix + method.name,
  131. requestStream: !!method.requestStream,
  132. responseStream: !!method.responseStream,
  133. requestType: method.resolvedRequestType,
  134. responseType: method.resolvedResponseType,
  135. requestSerialize: serializeCls(method.resolvedRequestType),
  136. requestDeserialize: deserializeCls(method.resolvedRequestType, options),
  137. responseSerialize: serializeCls(method.resolvedResponseType),
  138. responseDeserialize: deserializeCls(method.resolvedResponseType, options)
  139. };
  140. }));
  141. };
  142. var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
  143. exports.loadObject = function loadObject(value, options) {
  144. var result = {};
  145. if (!value) {
  146. return value;
  147. }
  148. if (value.hasOwnProperty('methods')) {
  149. // It's a service object
  150. var service_attrs = getProtobufServiceAttrs(value, options);
  151. return client.makeClientConstructor(service_attrs);
  152. }
  153. if (value.hasOwnProperty('nested')) {
  154. // It's a namespace or root object
  155. _.each(value.nested, function(nested, name) {
  156. result[name] = loadObject(nested, options);
  157. });
  158. return result;
  159. }
  160. // Otherwise, it's not something we need to change
  161. return value;
  162. };
  163. /**
  164. * The primary purpose of this method is to distinguish between reflection
  165. * objects from different versions of ProtoBuf.js. This is just a heuristic,
  166. * checking for properties that are (currently) specific to this version of
  167. * ProtoBuf.js
  168. * @param {Object} obj The object to check
  169. * @return {boolean} Whether the object appears to be a Protobuf.js 6
  170. * ReflectionObject
  171. */
  172. exports.isProbablyProtobufJs6 = function isProbablyProtobufJs6(obj) {
  173. return (typeof obj.root === 'object') &amp;&amp; (typeof obj.resolve === 'function');
  174. };
  175. </code></pre>
  176. </article>
  177. </section>
  178. </div>
  179. <nav>
  180. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-src_client.html">src/client</a></li><li><a href="module-src_common.html">src/common</a></li><li><a href="module-src_credentials.html">src/credentials</a></li><li><a href="module-src_metadata.html">src/metadata</a></li><li><a href="module-src_server.html">src/server</a></li></ul><h3>Classes</h3><ul><li><a href="module-src_client.makeClientConstructor-Client.html">Client</a></li><li><a href="module-src_client-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="module-src_client-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="module-src_client-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="module-src_metadata-Metadata.html">Metadata</a></li><li><a href="module-src_server-Server.html">Server</a></li><li><a href="module-src_server-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="module-src_server-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="module-src_server-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Global</h3><ul><li><a href="global.html#callError">callError</a></li><li><a href="global.html#credentials">credentials</a></li><li><a href="global.html#deserializeCls">deserializeCls</a></li><li><a href="global.html#fullyQualifiedName">fullyQualifiedName</a></li><li><a href="global.html#getClientChannel">getClientChannel</a></li><li><a href="global.html#getProtobufServiceAttrs">getProtobufServiceAttrs</a></li><li><a href="global.html#isProbablyProtobufJs5">isProbablyProtobufJs5</a></li><li><a href="global.html#isProbablyProtobufJs6">isProbablyProtobufJs6</a></li><li><a href="global.html#load">load</a></li><li><a href="global.html#loadObject">loadObject</a></li><li><a href="global.html#logVerbosity">logVerbosity</a></li><li><a href="global.html#makeGenericClientConstructor">makeGenericClientConstructor</a></li><li><a href="global.html#Metadata">Metadata</a></li><li><a href="global.html#propagate">propagate</a></li><li><a href="global.html#serializeCls">serializeCls</a></li><li><a href="global.html#Server">Server</a></li><li><a href="global.html#ServerCredentials">ServerCredentials</a></li><li><a href="global.html#setLogger">setLogger</a></li><li><a href="global.html#setLogVerbosity">setLogVerbosity</a></li><li><a href="global.html#status">status</a></li><li><a href="global.html#waitForClientReady">waitForClientReady</a></li><li><a href="global.html#writeFlags">writeFlags</a></li></ul>
  181. </nav>
  182. <br class="clear">
  183. <footer>
  184. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu Apr 27 2017 17:35:39 GMT-0700 (PDT)
  185. </footer>
  186. <script> prettyPrint(); </script>
  187. <script src="scripts/linenumber.js"> </script>
  188. </body>
  189. </html>