index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. 'use strict';
  34. var path = require('path');
  35. var fs = require('fs');
  36. var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem');
  37. var _ = require('lodash');
  38. var ProtoBuf = require('protobufjs');
  39. var client = require('./src/client.js');
  40. var server = require('./src/server.js');
  41. var Metadata = require('./src/metadata.js');
  42. var grpc = require('./src/grpc_extension');
  43. grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
  44. /**
  45. * Load a gRPC object from an existing ProtoBuf.Reflect object.
  46. * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
  47. * @param {Object=} options Options to apply to the loaded object
  48. * @return {Object<string, *>} The resulting gRPC object
  49. */
  50. exports.loadObject = function loadObject(value, options) {
  51. var result = {};
  52. if (value.className === 'Namespace') {
  53. _.each(value.children, function(child) {
  54. result[child.name] = loadObject(child, options);
  55. });
  56. return result;
  57. } else if (value.className === 'Service') {
  58. return client.makeProtobufClientConstructor(value, options);
  59. } else if (value.className === 'Message' || value.className === 'Enum') {
  60. return value.build();
  61. } else {
  62. return value;
  63. }
  64. };
  65. var loadObject = exports.loadObject;
  66. /**
  67. * Load a gRPC object from a .proto file. The options object can provide the
  68. * following options:
  69. * - convertFieldsToCamelCase: Loads this file with that option on protobuf.js
  70. * set as specified. See
  71. * https://github.com/dcodeIO/protobuf.js/wiki/Advanced-options for details
  72. * - binaryAsBase64: deserialize bytes values as base64 strings instead of
  73. * Buffers. Defaults to false
  74. * - longsAsStrings: deserialize long values as strings instead of objects.
  75. * Defaults to true
  76. * - deprecatedArgumentOrder: Use the beta method argument order for client
  77. * methods, with optional arguments after the callback. Defaults to false.
  78. * This option is only a temporary stopgap measure to smooth an API breakage.
  79. * It is deprecated, and new code should not use it.
  80. * @param {string|{root: string, file: string}} filename The file to load
  81. * @param {string=} format The file format to expect. Must be either 'proto' or
  82. * 'json'. Defaults to 'proto'
  83. * @param {Object=} options Options to apply to the loaded file
  84. * @return {Object<string, *>} The resulting gRPC object
  85. */
  86. exports.load = function load(filename, format, options) {
  87. if (!format) {
  88. format = 'proto';
  89. }
  90. var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
  91. if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
  92. ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
  93. }
  94. var builder;
  95. try {
  96. switch(format) {
  97. case 'proto':
  98. builder = ProtoBuf.loadProtoFile(filename);
  99. break;
  100. case 'json':
  101. builder = ProtoBuf.loadJsonFile(filename);
  102. break;
  103. default:
  104. throw new Error('Unrecognized format "' + format + '"');
  105. }
  106. } finally {
  107. ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
  108. }
  109. return loadObject(builder.ns, options);
  110. };
  111. /**
  112. * @see module:src/server.Server
  113. */
  114. exports.Server = server.Server;
  115. /**
  116. * @see module:src/metadata
  117. */
  118. exports.Metadata = Metadata;
  119. /**
  120. * Status name to code number mapping
  121. */
  122. exports.status = grpc.status;
  123. /**
  124. * Propagate flag name to number mapping
  125. */
  126. exports.propagate = grpc.propagate;
  127. /**
  128. * Call error name to code number mapping
  129. */
  130. exports.callError = grpc.callError;
  131. /**
  132. * Write flag name to code number mapping
  133. */
  134. exports.writeFlags = grpc.writeFlags;
  135. /**
  136. * Credentials factories
  137. */
  138. exports.credentials = require('./src/credentials.js');
  139. /**
  140. * ServerCredentials factories
  141. */
  142. exports.ServerCredentials = grpc.ServerCredentials;
  143. /**
  144. * @see module:src/client.makeClientConstructor
  145. */
  146. exports.makeGenericClientConstructor = client.makeClientConstructor;
  147. /**
  148. * @see module:src/client.getClientChannel
  149. */
  150. exports.getClientChannel = client.getClientChannel;
  151. /**
  152. * @see module:src/client.waitForClientReady
  153. */
  154. exports.waitForClientReady = client.waitForClientReady;