index.js.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: index.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: index.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. 'use strict';
  52. var path = require('path');
  53. var fs = require('fs');
  54. var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem');
  55. var _ = require('lodash');
  56. var ProtoBuf = require('protobufjs');
  57. var client = require('./src/client.js');
  58. var server = require('./src/server.js');
  59. var common = require('./src/common.js');
  60. var Metadata = require('./src/metadata.js');
  61. var grpc = require('./src/grpc_extension');
  62. var protobuf_js_5_common = require('./src/protobuf_js_5_common');
  63. var protobuf_js_6_common = require('./src/protobuf_js_6_common');
  64. grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
  65. /**
  66. * Load a ProtoBuf.js object as a gRPC object. The options object can provide
  67. * the following options:
  68. * - binaryAsBase64: deserialize bytes values as base64 strings instead of
  69. * Buffers. Defaults to false
  70. * - longsAsStrings: deserialize long values as strings instead of objects.
  71. * Defaults to true
  72. * - enumsAsStrings: deserialize enum values as strings instead of numbers.
  73. * Defaults to true
  74. * - deprecatedArgumentOrder: Use the beta method argument order for client
  75. * methods, with optional arguments after the callback. Defaults to false.
  76. * This option is only a temporary stopgap measure to smooth an API breakage.
  77. * It is deprecated, and new code should not use it.
  78. * - protobufjsVersion: Available values are 5, 6, and 'detect'. 5 and 6
  79. * respectively indicate that an object from the corresponding version of
  80. * ProtoBuf.js is provided in the value argument. If the option is 'detect',
  81. * gRPC will guess what the version is based on the structure of the value.
  82. * Defaults to 'detect'.
  83. * @param {Object} value The ProtoBuf.js reflection object to load
  84. * @param {Object=} options Options to apply to the loaded file
  85. * @return {Object&lt;string, *>} The resulting gRPC object
  86. */
  87. exports.loadObject = function loadObject(value, options) {
  88. options = _.defaults(options, common.defaultGrpcOptions);
  89. options = _.defaults(options, {'protobufjsVersion': 'detect'});
  90. var protobufjsVersion;
  91. if (options.protobufjsVersion === 'detect') {
  92. if (protobuf_js_6_common.isProbablyProtobufJs6(value)) {
  93. protobufjsVersion = 6;
  94. } else if (protobuf_js_5_common.isProbablyProtobufJs5(value)) {
  95. protobufjsVersion = 5;
  96. } else {
  97. var error_message = 'Could not detect ProtoBuf.js version. Please ' +
  98. 'specify the version number with the "protobufjs_version" option';
  99. throw new Error(error_message);
  100. }
  101. } else {
  102. protobufjsVersion = options.protobufjsVersion;
  103. }
  104. switch (protobufjsVersion) {
  105. case 6: return protobuf_js_6_common.loadObject(value, options);
  106. case 5:
  107. var deprecation_message = 'Calling grpc.loadObject with an object ' +
  108. 'generated by ProtoBuf.js 5 is deprecated. Please upgrade to ' +
  109. 'ProtoBuf.js 6.';
  110. common.log(grpc.logVerbosity.INFO, deprecation_message);
  111. return protobuf_js_5_common.loadObject(value, options);
  112. default:
  113. throw new Error('Unrecognized protobufjsVersion', protobufjsVersion);
  114. }
  115. };
  116. var loadObject = exports.loadObject;
  117. function applyProtoRoot(filename, root) {
  118. if (_.isString(filename)) {
  119. return filename;
  120. }
  121. filename.root = path.resolve(filename.root) + '/';
  122. root.resolvePath = function(originPath, importPath, alreadyNormalized) {
  123. return ProtoBuf.util.path.resolve(filename.root,
  124. importPath,
  125. alreadyNormalized);
  126. };
  127. return filename.file;
  128. }
  129. /**
  130. * Load a gRPC object from a .proto file. The options object can provide the
  131. * following options:
  132. * - convertFieldsToCamelCase: Load this file with field names in camel case
  133. * instead of their original case
  134. * - binaryAsBase64: deserialize bytes values as base64 strings instead of
  135. * Buffers. Defaults to false
  136. * - longsAsStrings: deserialize long values as strings instead of objects.
  137. * Defaults to true
  138. * - enumsAsStrings: deserialize enum values as strings instead of numbers.
  139. * Defaults to true
  140. * - deprecatedArgumentOrder: Use the beta method argument order for client
  141. * methods, with optional arguments after the callback. Defaults to false.
  142. * This option is only a temporary stopgap measure to smooth an API breakage.
  143. * It is deprecated, and new code should not use it.
  144. * @param {string|{root: string, file: string}} filename The file to load
  145. * @param {string=} format The file format to expect. Must be either 'proto' or
  146. * 'json'. Defaults to 'proto'
  147. * @param {Object=} options Options to apply to the loaded file
  148. * @return {Object&lt;string, *>} The resulting gRPC object
  149. */
  150. exports.load = function load(filename, format, options) {
  151. /* Note: format is currently unused, because the API for loading a proto
  152. file or a JSON file is identical in Protobuf.js 6. In the future, there is
  153. still the possibility of adding other formats that would be loaded
  154. differently */
  155. options = _.defaults(options, common.defaultGrpcOptions);
  156. options.protobufjs_version = 6;
  157. var root = new ProtoBuf.Root();
  158. var parse_options = {keepCase: !options.convertFieldsToCamelCase};
  159. return loadObject(root.loadSync(applyProtoRoot(filename, root),
  160. parse_options),
  161. options);
  162. };
  163. var log_template = _.template(
  164. '{severity} {timestamp}\t{file}:{line}]\t{message}',
  165. {interpolate: /{([\s\S]+?)}/g});
  166. /**
  167. * Sets the logger function for the gRPC module. For debugging purposes, the C
  168. * core will log synchronously directly to stdout unless this function is
  169. * called. Note: the output format here is intended to be informational, and
  170. * is not guaranteed to stay the same in the future.
  171. * Logs will be directed to logger.error.
  172. * @param {Console} logger A Console-like object.
  173. */
  174. exports.setLogger = function setLogger(logger) {
  175. common.logger = logger;
  176. grpc.setDefaultLoggerCallback(function(file, line, severity,
  177. message, timestamp) {
  178. logger.error(log_template({
  179. file: path.basename(file),
  180. line: line,
  181. severity: severity,
  182. message: message,
  183. timestamp: timestamp.toISOString()
  184. }));
  185. });
  186. };
  187. /**
  188. * Sets the logger verbosity for gRPC module logging. The options are members
  189. * of the grpc.logVerbosity map.
  190. * @param {Number} verbosity The minimum severity to log
  191. */
  192. exports.setLogVerbosity = function setLogVerbosity(verbosity) {
  193. common.logVerbosity = verbosity;
  194. grpc.setLogVerbosity(verbosity);
  195. };
  196. /**
  197. * @see module:src/server.Server
  198. */
  199. exports.Server = server.Server;
  200. /**
  201. * @see module:src/metadata
  202. */
  203. exports.Metadata = Metadata;
  204. /**
  205. * Status name to code number mapping
  206. */
  207. exports.status = grpc.status;
  208. /**
  209. * Propagate flag name to number mapping
  210. */
  211. exports.propagate = grpc.propagate;
  212. /**
  213. * Call error name to code number mapping
  214. */
  215. exports.callError = grpc.callError;
  216. /**
  217. * Write flag name to code number mapping
  218. */
  219. exports.writeFlags = grpc.writeFlags;
  220. /**
  221. * Log verbosity setting name to code number mapping
  222. */
  223. exports.logVerbosity = grpc.logVerbosity;
  224. /**
  225. * Credentials factories
  226. */
  227. exports.credentials = require('./src/credentials.js');
  228. /**
  229. * ServerCredentials factories
  230. */
  231. exports.ServerCredentials = grpc.ServerCredentials;
  232. /**
  233. * @see module:src/client.makeClientConstructor
  234. */
  235. exports.makeGenericClientConstructor = client.makeClientConstructor;
  236. /**
  237. * @see module:src/client.getClientChannel
  238. */
  239. exports.getClientChannel = client.getClientChannel;
  240. /**
  241. * @see module:src/client.waitForClientReady
  242. */
  243. exports.waitForClientReady = client.waitForClientReady;
  244. exports.closeClient = function closeClient(client_obj) {
  245. client.getClientChannel(client_obj).close();
  246. };
  247. </code></pre>
  248. </article>
  249. </section>
  250. </div>
  251. <nav>
  252. <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>
  253. </nav>
  254. <br class="clear">
  255. <footer>
  256. 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)
  257. </footer>
  258. <script> prettyPrint(); </script>
  259. <script src="scripts/linenumber.js"> </script>
  260. </body>
  261. </html>