src_protobuf_js_6_common.js.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 gRPC authors.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. *
  35. */
  36. /**
  37. * @module
  38. * @private
  39. */
  40. 'use strict';
  41. var _ = require('lodash');
  42. var client = require('./client');
  43. /**
  44. * Get a function that deserializes a specific type of protobuf.
  45. * @param {function()} cls The constructor of the message type to deserialize
  46. * @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
  47. * instead of Buffers. Defaults to false
  48. * @param {bool=} longsAsStrings Deserialize long values as strings instead of
  49. * objects. Defaults to true
  50. * @return {function(Buffer):cls} The deserialization function
  51. */
  52. exports.deserializeCls = function deserializeCls(cls, options) {
  53. var conversion_options = {
  54. defaults: true,
  55. bytes: options.binaryAsBase64 ? String : Buffer,
  56. longs: options.longsAsStrings ? String : null,
  57. enums: options.enumsAsStrings ? String : null,
  58. oneofs: true
  59. };
  60. /**
  61. * Deserialize a buffer to a message object
  62. * @param {Buffer} arg_buf The buffer to deserialize
  63. * @return {cls} The resulting object
  64. */
  65. return function deserialize(arg_buf) {
  66. return cls.toObject(cls.decode(arg_buf), conversion_options);
  67. };
  68. };
  69. var deserializeCls = exports.deserializeCls;
  70. /**
  71. * Get a function that serializes objects to a buffer by protobuf class.
  72. * @param {function()} Cls The constructor of the message type to serialize
  73. * @return {function(Cls):Buffer} The serialization function
  74. */
  75. exports.serializeCls = function serializeCls(cls) {
  76. /**
  77. * Serialize an object to a Buffer
  78. * @param {Object} arg The object to serialize
  79. * @return {Buffer} The serialized object
  80. */
  81. return function serialize(arg) {
  82. var message = cls.fromObject(arg);
  83. return cls.encode(message).finish();
  84. };
  85. };
  86. var serializeCls = exports.serializeCls;
  87. /**
  88. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  89. * @param {ProtoBuf.ReflectionObject} value The value to get the name of
  90. * @return {string} The fully qualified name of the value
  91. */
  92. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  93. if (value === null || value === undefined) {
  94. return '';
  95. }
  96. var name = value.name;
  97. var parent_fqn = fullyQualifiedName(value.parent);
  98. if (parent_fqn !== '') {
  99. name = parent_fqn + '.' + name;
  100. }
  101. return name;
  102. };
  103. var fullyQualifiedName = exports.fullyQualifiedName;
  104. /**
  105. * Return a map from method names to method attributes for the service.
  106. * @param {ProtoBuf.Service} service The service to get attributes for
  107. * @param {Object=} options Options to apply to these attributes
  108. * @return {Object} The attributes map
  109. */
  110. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
  111. options) {
  112. var prefix = '/' + fullyQualifiedName(service) + '/';
  113. service.resolveAll();
  114. return _.zipObject(_.map(service.methods, function(method) {
  115. return _.camelCase(method.name);
  116. }), _.map(service.methods, function(method) {
  117. return {
  118. originalName: method.name,
  119. path: prefix + method.name,
  120. requestStream: !!method.requestStream,
  121. responseStream: !!method.responseStream,
  122. requestType: method.resolvedRequestType,
  123. responseType: method.resolvedResponseType,
  124. requestSerialize: serializeCls(method.resolvedRequestType),
  125. requestDeserialize: deserializeCls(method.resolvedRequestType, options),
  126. responseSerialize: serializeCls(method.resolvedResponseType),
  127. responseDeserialize: deserializeCls(method.resolvedResponseType, options)
  128. };
  129. }));
  130. };
  131. var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
  132. exports.loadObject = function loadObject(value, options) {
  133. var result = {};
  134. if (!value) {
  135. return value;
  136. }
  137. if (value.hasOwnProperty('methods')) {
  138. // It's a service object
  139. var service_attrs = getProtobufServiceAttrs(value, options);
  140. return client.makeClientConstructor(service_attrs);
  141. }
  142. if (value.hasOwnProperty('nested')) {
  143. // It's a namespace or root object
  144. _.each(value.nested, function(nested, name) {
  145. result[name] = loadObject(nested, options);
  146. });
  147. return result;
  148. }
  149. // Otherwise, it's not something we need to change
  150. return value;
  151. };
  152. /**
  153. * The primary purpose of this method is to distinguish between reflection
  154. * objects from different versions of ProtoBuf.js. This is just a heuristic,
  155. * checking for properties that are (currently) specific to this version of
  156. * ProtoBuf.js
  157. * @param {Object} obj The object to check
  158. * @return {boolean} Whether the object appears to be a Protobuf.js 6
  159. * ReflectionObject
  160. */
  161. exports.isProbablyProtobufJs6 = function isProbablyProtobufJs6(obj) {
  162. return (typeof obj.root === 'object') &amp;&amp; (typeof obj.resolve === 'function');
  163. };
  164. </code></pre>
  165. </article>
  166. </section>
  167. </div>
  168. <nav>
  169. <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>
  170. </nav>
  171. <br class="clear">
  172. <footer>
  173. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.3</a> on Fri Sep 08 2017 11:10:31 GMT-0700 (PDT)
  174. </footer>
  175. <script> prettyPrint(); </script>
  176. <script src="scripts/linenumber.js"> </script>
  177. </body>
  178. </html>