index.js.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
  63. /**
  64. * Load a gRPC object from an existing ProtoBuf.Reflect object.
  65. * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
  66. * @param {Object=} options Options to apply to the loaded object
  67. * @return {Object&lt;string, *>} The resulting gRPC object
  68. */
  69. exports.loadObject = function loadObject(value, options) {
  70. var result = {};
  71. if (value.className === 'Namespace') {
  72. _.each(value.children, function(child) {
  73. result[child.name] = loadObject(child, options);
  74. });
  75. return result;
  76. } else if (value.className === 'Service') {
  77. return client.makeProtobufClientConstructor(value, options);
  78. } else if (value.className === 'Message' || value.className === 'Enum') {
  79. return value.build();
  80. } else {
  81. return value;
  82. }
  83. };
  84. var loadObject = exports.loadObject;
  85. /**
  86. * Load a gRPC object from a .proto file. The options object can provide the
  87. * following options:
  88. * - convertFieldsToCamelCase: Loads this file with that option on protobuf.js
  89. * set as specified. See
  90. * https://github.com/dcodeIO/protobuf.js/wiki/Advanced-options for details
  91. * - binaryAsBase64: deserialize bytes values as base64 strings instead of
  92. * Buffers. Defaults to false
  93. * - longsAsStrings: deserialize long values as strings instead of objects.
  94. * Defaults to true
  95. * - deprecatedArgumentOrder: Use the beta method argument order for client
  96. * methods, with optional arguments after the callback. Defaults to false.
  97. * This option is only a temporary stopgap measure to smooth an API breakage.
  98. * It is deprecated, and new code should not use it.
  99. * @param {string|{root: string, file: string}} filename The file to load
  100. * @param {string=} format The file format to expect. Must be either 'proto' or
  101. * 'json'. Defaults to 'proto'
  102. * @param {Object=} options Options to apply to the loaded file
  103. * @return {Object&lt;string, *>} The resulting gRPC object
  104. */
  105. exports.load = function load(filename, format, options) {
  106. if (!format) {
  107. format = 'proto';
  108. }
  109. var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
  110. if(options &amp;&amp; options.hasOwnProperty('convertFieldsToCamelCase')) {
  111. ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
  112. }
  113. var builder;
  114. try {
  115. switch(format) {
  116. case 'proto':
  117. builder = ProtoBuf.loadProtoFile(filename);
  118. break;
  119. case 'json':
  120. builder = ProtoBuf.loadJsonFile(filename);
  121. break;
  122. default:
  123. throw new Error('Unrecognized format "' + format + '"');
  124. }
  125. } finally {
  126. ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
  127. }
  128. return loadObject(builder.ns, options);
  129. };
  130. var log_template = _.template(
  131. '{severity} {timestamp}\t{file}:{line}]\t{message}',
  132. {interpolate: /{([\s\S]+?)}/g});
  133. /**
  134. * Sets the logger function for the gRPC module. For debugging purposes, the C
  135. * core will log synchronously directly to stdout unless this function is
  136. * called. Note: the output format here is intended to be informational, and
  137. * is not guaranteed to stay the same in the future.
  138. * Logs will be directed to logger.error.
  139. * @param {Console} logger A Console-like object.
  140. */
  141. exports.setLogger = function setLogger(logger) {
  142. common.logger = logger;
  143. grpc.setDefaultLoggerCallback(function(file, line, severity,
  144. message, timestamp) {
  145. logger.error(log_template({
  146. file: path.basename(file),
  147. line: line,
  148. severity: severity,
  149. message: message,
  150. timestamp: timestamp.toISOString()
  151. }));
  152. });
  153. };
  154. /**
  155. * Sets the logger verbosity for gRPC module logging. The options are members
  156. * of the grpc.logVerbosity map.
  157. * @param {Number} verbosity The minimum severity to log
  158. */
  159. exports.setLogVerbosity = function setLogVerbosity(verbosity) {
  160. common.logVerbosity = verbosity;
  161. grpc.setLogVerbosity(verbosity);
  162. };
  163. /**
  164. * @see module:src/server.Server
  165. */
  166. exports.Server = server.Server;
  167. /**
  168. * @see module:src/metadata
  169. */
  170. exports.Metadata = Metadata;
  171. /**
  172. * Status name to code number mapping
  173. */
  174. exports.status = grpc.status;
  175. /**
  176. * Propagate flag name to number mapping
  177. */
  178. exports.propagate = grpc.propagate;
  179. /**
  180. * Call error name to code number mapping
  181. */
  182. exports.callError = grpc.callError;
  183. /**
  184. * Write flag name to code number mapping
  185. */
  186. exports.writeFlags = grpc.writeFlags;
  187. /**
  188. * Log verbosity setting name to code number mapping
  189. */
  190. exports.logVerbosity = grpc.logVerbosity;
  191. /**
  192. * Credentials factories
  193. */
  194. exports.credentials = require('./src/credentials.js');
  195. /**
  196. * ServerCredentials factories
  197. */
  198. exports.ServerCredentials = grpc.ServerCredentials;
  199. /**
  200. * @see module:src/client.makeClientConstructor
  201. */
  202. exports.makeGenericClientConstructor = client.makeClientConstructor;
  203. /**
  204. * @see module:src/client.getClientChannel
  205. */
  206. exports.getClientChannel = client.getClientChannel;
  207. /**
  208. * @see module:src/client.waitForClientReady
  209. */
  210. exports.waitForClientReady = client.waitForClientReady;
  211. </code></pre>
  212. </article>
  213. </section>
  214. </div>
  215. <nav>
  216. <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#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#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>
  217. </nav>
  218. <br class="clear">
  219. <footer>
  220. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Aug 18 2016 12:19:14 GMT-0700 (PDT)
  221. </footer>
  222. <script> prettyPrint(); </script>
  223. <script src="scripts/linenumber.js"> </script>
  224. </body>
  225. </html>