src_common.js.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * @module
  53. */
  54. 'use strict';
  55. var _ = require('lodash');
  56. /**
  57. * Get a function that deserializes a specific type of protobuf.
  58. * @param {function()} cls The constructor of the message type to deserialize
  59. * @return {function(Buffer):cls} The deserialization function
  60. */
  61. exports.deserializeCls = function deserializeCls(cls) {
  62. /**
  63. * Deserialize a buffer to a message object
  64. * @param {Buffer} arg_buf The buffer to deserialize
  65. * @return {cls} The resulting object
  66. */
  67. return function deserialize(arg_buf) {
  68. // Convert to a native object with binary fields as Buffers (first argument)
  69. // and longs as strings (second argument)
  70. return cls.decode(arg_buf).toRaw(false, true);
  71. };
  72. };
  73. var deserializeCls = exports.deserializeCls;
  74. /**
  75. * Get a function that serializes objects to a buffer by protobuf class.
  76. * @param {function()} Cls The constructor of the message type to serialize
  77. * @return {function(Cls):Buffer} The serialization function
  78. */
  79. exports.serializeCls = function serializeCls(Cls) {
  80. /**
  81. * Serialize an object to a Buffer
  82. * @param {Object} arg The object to serialize
  83. * @return {Buffer} The serialized object
  84. */
  85. return function serialize(arg) {
  86. return new Buffer(new Cls(arg).encode().toBuffer());
  87. };
  88. };
  89. var serializeCls = exports.serializeCls;
  90. /**
  91. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  92. * @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
  93. * @return {string} The fully qualified name of the value
  94. */
  95. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  96. if (value === null || value === undefined) {
  97. return '';
  98. }
  99. var name = value.name;
  100. if (value.className === 'Service.RPCMethod') {
  101. name = _.capitalize(name);
  102. }
  103. if (value.hasOwnProperty('parent')) {
  104. var parent_name = fullyQualifiedName(value.parent);
  105. if (parent_name !== '') {
  106. name = parent_name + '.' + name;
  107. }
  108. }
  109. return name;
  110. };
  111. var fullyQualifiedName = exports.fullyQualifiedName;
  112. /**
  113. * Wrap a function to pass null-like values through without calling it. If no
  114. * function is given, just uses the identity;
  115. * @param {?function} func The function to wrap
  116. * @return {function} The wrapped function
  117. */
  118. exports.wrapIgnoreNull = function wrapIgnoreNull(func) {
  119. if (!func) {
  120. return _.identity;
  121. }
  122. return function(arg) {
  123. if (arg === null || arg === undefined) {
  124. return null;
  125. }
  126. return func(arg);
  127. };
  128. };
  129. /**
  130. * Return a map from method names to method attributes for the service.
  131. * @param {ProtoBuf.Reflect.Service} service The service to get attributes for
  132. * @return {Object} The attributes map
  133. */
  134. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service) {
  135. var prefix = '/' + fullyQualifiedName(service) + '/';
  136. return _.object(_.map(service.children, function(method) {
  137. return [_.camelCase(method.name), {
  138. path: prefix + _.capitalize(method.name),
  139. requestStream: method.requestStream,
  140. responseStream: method.responseStream,
  141. requestSerialize: serializeCls(method.resolvedRequestType.build()),
  142. requestDeserialize: deserializeCls(method.resolvedRequestType.build()),
  143. responseSerialize: serializeCls(method.resolvedResponseType.build()),
  144. responseDeserialize: deserializeCls(method.resolvedResponseType.build())
  145. }];
  146. }));
  147. };
  148. </code></pre>
  149. </article>
  150. </section>
  151. </div>
  152. <nav>
  153. <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_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#getGoogleAuthDelegate">getGoogleAuthDelegate</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>
  154. </nav>
  155. <br class="clear">
  156. <footer>
  157. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.3</a> on Thu Oct 29 2015 13:07:25 GMT-0700 (PDT)
  158. </footer>
  159. <script> prettyPrint(); </script>
  160. <script src="scripts/linenumber.js"> </script>
  161. </body>
  162. </html>