blob: 0da3440eb77241fb7a2162817036d8e29c054d5d [file] [log] [blame]
murgatroid99cca5ffa2015-01-15 14:06:56 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
murgatroid99cca5ffa2015-01-15 14:06:56 -08004 * 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
murgatroid99ffac55d2017-05-05 13:54:03 -070034/**
35 * @module
36 */
37
murgatroid99dca966d2015-02-19 14:37:18 -080038'use strict';
39
murgatroid996d6009f2015-10-15 09:57:31 -070040var path = require('path');
murgatroid996f607662016-04-27 16:38:33 -070041var fs = require('fs');
murgatroid996d6009f2015-10-15 09:57:31 -070042
43var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem');
44
murgatroid9955739d52015-06-03 10:58:21 -070045var _ = require('lodash');
murgatroid99cca5ffa2015-01-15 14:06:56 -080046
47var ProtoBuf = require('protobufjs');
48
murgatroid99e7879552015-02-12 12:21:15 -080049var client = require('./src/client.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080050
murgatroid99e7879552015-02-12 12:21:15 -080051var server = require('./src/server.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080052
murgatroid999b9708a2016-06-01 11:42:20 -070053var common = require('./src/common.js');
54
murgatroid9984e3cde2015-08-20 11:27:05 -070055var Metadata = require('./src/metadata.js');
56
murgatroid99e190f352016-01-20 13:52:08 -080057var grpc = require('./src/grpc_extension');
murgatroid99cca5ffa2015-01-15 14:06:56 -080058
murgatroid99b5b5f022017-03-16 16:42:10 -070059var protobuf_js_5_common = require('./src/protobuf_js_5_common');
60var protobuf_js_6_common = require('./src/protobuf_js_6_common');
61
murgatroid997229f882017-05-10 16:24:05 -070062var constants = require('./src/constants.js');
63
murgatroid996f607662016-04-27 16:38:33 -070064grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
65
murgatroid99b3068542017-03-21 11:25:01 -070066/**
67 * Load a ProtoBuf.js object as a gRPC object. The options object can provide
68 * the following options:
69 * - binaryAsBase64: deserialize bytes values as base64 strings instead of
70 * Buffers. Defaults to false
71 * - longsAsStrings: deserialize long values as strings instead of objects.
72 * Defaults to true
murgatroid99ac7f90d2017-03-23 18:00:09 -070073 * - enumsAsStrings: deserialize enum values as strings instead of numbers.
74 * Defaults to true
murgatroid99b3068542017-03-21 11:25:01 -070075 * - deprecatedArgumentOrder: Use the beta method argument order for client
76 * methods, with optional arguments after the callback. Defaults to false.
77 * This option is only a temporary stopgap measure to smooth an API breakage.
78 * It is deprecated, and new code should not use it.
79 * - protobufjsVersion: Available values are 5, 6, and 'detect'. 5 and 6
80 * respectively indicate that an object from the corresponding version of
81 * ProtoBuf.js is provided in the value argument. If the option is 'detect',
82 * gRPC will guess what the version is based on the structure of the value.
83 * Defaults to 'detect'.
84 * @param {Object} value The ProtoBuf.js reflection object to load
85 * @param {Object=} options Options to apply to the loaded file
86 * @return {Object<string, *>} The resulting gRPC object
87 */
murgatroid99c02910b2016-02-17 12:59:26 -080088exports.loadObject = function loadObject(value, options) {
murgatroid99b1a02312017-03-17 13:45:15 -070089 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b3068542017-03-21 11:25:01 -070090 options = _.defaults(options, {'protobufjsVersion': 'detect'});
91 var protobufjsVersion;
92 if (options.protobufjsVersion === 'detect') {
93 if (protobuf_js_6_common.isProbablyProtobufJs6(value)) {
94 protobufjsVersion = 6;
95 } else if (protobuf_js_5_common.isProbablyProtobufJs5(value)) {
96 protobufjsVersion = 5;
97 } else {
98 var error_message = 'Could not detect ProtoBuf.js version. Please ' +
99 'specify the version number with the "protobufjs_version" option';
100 throw new Error(error_message);
101 }
murgatroid99b1a02312017-03-17 13:45:15 -0700102 } else {
murgatroid99b3068542017-03-21 11:25:01 -0700103 protobufjsVersion = options.protobufjsVersion;
104 }
105 switch (protobufjsVersion) {
106 case 6: return protobuf_js_6_common.loadObject(value, options);
107 case 5:
murgatroid99b1a02312017-03-17 13:45:15 -0700108 var deprecation_message = 'Calling grpc.loadObject with an object ' +
109 'generated by ProtoBuf.js 5 is deprecated. Please upgrade to ' +
110 'ProtoBuf.js 6.';
111 common.log(grpc.logVerbosity.INFO, deprecation_message);
112 return protobuf_js_5_common.loadObject(value, options);
murgatroid99b3068542017-03-21 11:25:01 -0700113 default:
114 throw new Error('Unrecognized protobufjsVersion', protobufjsVersion);
murgatroid99cca5ffa2015-01-15 14:06:56 -0800115 }
murgatroid999cd90a62015-07-27 11:23:13 -0700116};
117
118var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800119
murgatroid99b5b5f022017-03-16 16:42:10 -0700120function applyProtoRoot(filename, root) {
121 if (_.isString(filename)) {
122 return filename;
123 }
124 filename.root = path.resolve(filename.root) + '/';
125 root.resolvePath = function(originPath, importPath, alreadyNormalized) {
126 return ProtoBuf.util.path.resolve(filename.root,
127 importPath,
128 alreadyNormalized);
129 };
130 return filename.file;
131}
132
murgatroid99cca5ffa2015-01-15 14:06:56 -0800133/**
murgatroid99654d2542016-02-17 15:36:28 -0800134 * Load a gRPC object from a .proto file. The options object can provide the
135 * following options:
murgatroid99b3068542017-03-21 11:25:01 -0700136 * - convertFieldsToCamelCase: Load this file with field names in camel case
137 * instead of their original case
murgatroid99654d2542016-02-17 15:36:28 -0800138 * - binaryAsBase64: deserialize bytes values as base64 strings instead of
139 * Buffers. Defaults to false
140 * - longsAsStrings: deserialize long values as strings instead of objects.
141 * Defaults to true
murgatroid99ac7f90d2017-03-23 18:00:09 -0700142 * - enumsAsStrings: deserialize enum values as strings instead of numbers.
143 * Defaults to true
murgatroid9927b8d902016-03-22 14:46:37 -0700144 * - deprecatedArgumentOrder: Use the beta method argument order for client
145 * methods, with optional arguments after the callback. Defaults to false.
146 * This option is only a temporary stopgap measure to smooth an API breakage.
147 * It is deprecated, and new code should not use it.
murgatroid99c02910b2016-02-17 12:59:26 -0800148 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -0700149 * @param {string=} format The file format to expect. Must be either 'proto' or
150 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -0800151 * @param {Object=} options Options to apply to the loaded file
murgatroid99cca5ffa2015-01-15 14:06:56 -0800152 * @return {Object<string, *>} The resulting gRPC object
153 */
murgatroid99c02910b2016-02-17 12:59:26 -0800154exports.load = function load(filename, format, options) {
murgatroid99b5b5f022017-03-16 16:42:10 -0700155 /* Note: format is currently unused, because the API for loading a proto
156 file or a JSON file is identical in Protobuf.js 6. In the future, there is
157 still the possibility of adding other formats that would be loaded
158 differently */
159 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b3068542017-03-21 11:25:01 -0700160 options.protobufjs_version = 6;
murgatroid99b5b5f022017-03-16 16:42:10 -0700161 var root = new ProtoBuf.Root();
162 var parse_options = {keepCase: !options.convertFieldsToCamelCase};
163 return loadObject(root.loadSync(applyProtoRoot(filename, root),
164 parse_options),
165 options);
murgatroid999cd90a62015-07-27 11:23:13 -0700166};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800167
murgatroid991d2f2892016-06-02 14:33:22 -0700168var log_template = _.template(
169 '{severity} {timestamp}\t{file}:{line}]\t{message}',
170 {interpolate: /{([\s\S]+?)}/g});
171
murgatroid99cca5ffa2015-01-15 14:06:56 -0800172/**
murgatroid999b9708a2016-06-01 11:42:20 -0700173 * Sets the logger function for the gRPC module. For debugging purposes, the C
174 * core will log synchronously directly to stdout unless this function is
175 * called. Note: the output format here is intended to be informational, and
176 * is not guaranteed to stay the same in the future.
177 * Logs will be directed to logger.error.
178 * @param {Console} logger A Console-like object.
179 */
180exports.setLogger = function setLogger(logger) {
181 common.logger = logger;
murgatroid991d2f2892016-06-02 14:33:22 -0700182 grpc.setDefaultLoggerCallback(function(file, line, severity,
183 message, timestamp) {
184 logger.error(log_template({
185 file: path.basename(file),
186 line: line,
187 severity: severity,
188 message: message,
189 timestamp: timestamp.toISOString()
190 }));
murgatroid999b9708a2016-06-01 11:42:20 -0700191 });
192};
193
194/**
195 * Sets the logger verbosity for gRPC module logging. The options are members
196 * of the grpc.logVerbosity map.
197 * @param {Number} verbosity The minimum severity to log
198 */
199exports.setLogVerbosity = function setLogVerbosity(verbosity) {
200 common.logVerbosity = verbosity;
201 grpc.setLogVerbosity(verbosity);
202};
203
204/**
murgatroid999cd90a62015-07-27 11:23:13 -0700205 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800206 */
murgatroid99366e64d2015-07-15 17:01:49 -0700207exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800208
209/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700210 * @see module:src/metadata
211 */
212exports.Metadata = Metadata;
213
214/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800215 * Status name to code number mapping
216 */
murgatroid997229f882017-05-10 16:24:05 -0700217exports.status = constants.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700218
murgatroid99cca5ffa2015-01-15 14:06:56 -0800219/**
murgatroid990b094572015-08-14 10:48:45 -0700220 * Propagate flag name to number mapping
221 */
murgatroid997229f882017-05-10 16:24:05 -0700222exports.propagate = constants.propagate;
murgatroid990b094572015-08-14 10:48:45 -0700223
224/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800225 * Call error name to code number mapping
226 */
murgatroid997229f882017-05-10 16:24:05 -0700227exports.callError = constants.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800228
229/**
murgatroid994a1474f2015-08-17 14:00:31 -0700230 * Write flag name to code number mapping
231 */
murgatroid997229f882017-05-10 16:24:05 -0700232exports.writeFlags = constants.writeFlags;
murgatroid994a1474f2015-08-17 14:00:31 -0700233
234/**
murgatroid999b9708a2016-06-01 11:42:20 -0700235 * Log verbosity setting name to code number mapping
236 */
murgatroid997229f882017-05-10 16:24:05 -0700237exports.logVerbosity = constants.logVerbosity;
murgatroid999b9708a2016-06-01 11:42:20 -0700238
239/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800240 * Credentials factories
241 */
murgatroid99153b09d2015-09-25 16:04:03 -0700242exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800243
244/**
245 * ServerCredentials factories
246 */
247exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800248
murgatroid999cd90a62015-07-27 11:23:13 -0700249/**
250 * @see module:src/client.makeClientConstructor
251 */
murgatroid99e023e982015-03-18 17:17:33 -0700252exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700253
254/**
255 * @see module:src/client.getClientChannel
256 */
257exports.getClientChannel = client.getClientChannel;
258
259/**
260 * @see module:src/client.waitForClientReady
261 */
262exports.waitForClientReady = client.waitForClientReady;
murgatroid999030c812016-09-16 13:25:08 -0700263
264exports.closeClient = function closeClient(client_obj) {
murgatroid99ffac55d2017-05-05 13:54:03 -0700265 client.Client.prototype.close.apply(client_obj);
murgatroid999030c812016-09-16 13:25:08 -0700266};
murgatroid99ffac55d2017-05-05 13:54:03 -0700267
268/**
269 * @see module:src/client.Client
270 */
271exports.Client = client.Client;