src_common.js.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/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/common.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*
  20. *
  21. * Copyright 2015, 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. * This module contains functions that are common to client and server
  53. * code. None of them should be used directly by gRPC users.
  54. * @module
  55. */
  56. 'use strict';
  57. var _ = require('lodash');
  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. * @return {function(Buffer):cls} The deserialization function
  62. */
  63. exports.deserializeCls = function deserializeCls(cls) {
  64. /**
  65. * Deserialize a buffer to a message object
  66. * @param {Buffer} arg_buf The buffer to deserialize
  67. * @return {cls} The resulting object
  68. */
  69. return function deserialize(arg_buf) {
  70. // Convert to a native object with binary fields as Buffers (first argument)
  71. // and longs as strings (second argument)
  72. return cls.decode(arg_buf).toRaw(false, true);
  73. };
  74. };
  75. var deserializeCls = exports.deserializeCls;
  76. /**
  77. * Get a function that serializes objects to a buffer by protobuf class.
  78. * @param {function()} Cls The constructor of the message type to serialize
  79. * @return {function(Cls):Buffer} The serialization function
  80. */
  81. exports.serializeCls = function serializeCls(Cls) {
  82. /**
  83. * Serialize an object to a Buffer
  84. * @param {Object} arg The object to serialize
  85. * @return {Buffer} The serialized object
  86. */
  87. return function serialize(arg) {
  88. return new Buffer(new Cls(arg).encode().toBuffer());
  89. };
  90. };
  91. var serializeCls = exports.serializeCls;
  92. /**
  93. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  94. * @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
  95. * @return {string} The fully qualified name of the value
  96. */
  97. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  98. if (value === null || value === undefined) {
  99. return '';
  100. }
  101. var name = value.name;
  102. var parent_name = fullyQualifiedName(value.parent);
  103. if (parent_name !== '') {
  104. name = parent_name + '.' + name;
  105. }
  106. return name;
  107. };
  108. var fullyQualifiedName = exports.fullyQualifiedName;
  109. /**
  110. * Wrap a function to pass null-like values through without calling it. If no
  111. * function is given, just uses the identity;
  112. * @param {?function} func The function to wrap
  113. * @return {function} The wrapped function
  114. */
  115. exports.wrapIgnoreNull = function wrapIgnoreNull(func) {
  116. if (!func) {
  117. return _.identity;
  118. }
  119. return function(arg) {
  120. if (arg === null || arg === undefined) {
  121. return null;
  122. }
  123. return func(arg);
  124. };
  125. };
  126. /**
  127. * Return a map from method names to method attributes for the service.
  128. * @param {ProtoBuf.Reflect.Service} service The service to get attributes for
  129. * @return {Object} The attributes map
  130. */
  131. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service) {
  132. var prefix = '/' + fullyQualifiedName(service) + '/';
  133. return _.object(_.map(service.children, function(method) {
  134. return [_.camelCase(method.name), {
  135. path: prefix + _.capitalize(method.name),
  136. requestStream: method.requestStream,
  137. responseStream: method.responseStream,
  138. requestSerialize: serializeCls(method.resolvedRequestType.build()),
  139. requestDeserialize: deserializeCls(method.resolvedRequestType.build()),
  140. responseSerialize: serializeCls(method.resolvedResponseType.build()),
  141. responseDeserialize: deserializeCls(method.resolvedResponseType.build())
  142. }];
  143. }));
  144. };
  145. </code></pre>
  146. </article>
  147. </section>
  148. </div>
  149. <nav>
  150. <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#getClientChannel">getClientChannel</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#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#Server">Server</a></li><li><a href="global.html#ServerCredentials">ServerCredentials</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>
  151. </nav>
  152. <br class="clear">
  153. <footer>
  154. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jan 14 2016 16:17:15 GMT-0800 (PST)
  155. </footer>
  156. <script> prettyPrint(); </script>
  157. <script src="scripts/linenumber.js"> </script>
  158. </body>
  159. </html>