blob: 071bfd7927bab798c2bf0920355be32c54cd7242 [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
murgatroid99dca966d2015-02-19 14:37:18 -080034'use strict';
35
murgatroid996d6009f2015-10-15 09:57:31 -070036var path = require('path');
murgatroid996f607662016-04-27 16:38:33 -070037var fs = require('fs');
murgatroid996d6009f2015-10-15 09:57:31 -070038
39var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem');
40
murgatroid9955739d52015-06-03 10:58:21 -070041var _ = require('lodash');
murgatroid99cca5ffa2015-01-15 14:06:56 -080042
43var ProtoBuf = require('protobufjs');
44
murgatroid99e7879552015-02-12 12:21:15 -080045var client = require('./src/client.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080046
murgatroid99e7879552015-02-12 12:21:15 -080047var server = require('./src/server.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080048
murgatroid999b9708a2016-06-01 11:42:20 -070049var common = require('./src/common.js');
50
murgatroid9984e3cde2015-08-20 11:27:05 -070051var Metadata = require('./src/metadata.js');
52
murgatroid99e190f352016-01-20 13:52:08 -080053var grpc = require('./src/grpc_extension');
murgatroid99cca5ffa2015-01-15 14:06:56 -080054
murgatroid99b5b5f022017-03-16 16:42:10 -070055var protobuf_js_5_common = require('./src/protobuf_js_5_common');
56var protobuf_js_6_common = require('./src/protobuf_js_6_common');
57
murgatroid996f607662016-04-27 16:38:33 -070058grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
59
murgatroid99b3068542017-03-21 11:25:01 -070060/**
61 * Load a ProtoBuf.js object as a gRPC object. The options object can provide
62 * the following options:
63 * - binaryAsBase64: deserialize bytes values as base64 strings instead of
64 * Buffers. Defaults to false
65 * - longsAsStrings: deserialize long values as strings instead of objects.
66 * Defaults to true
murgatroid99ac7f90d2017-03-23 18:00:09 -070067 * - enumsAsStrings: deserialize enum values as strings instead of numbers.
68 * Defaults to true
murgatroid99b3068542017-03-21 11:25:01 -070069 * - deprecatedArgumentOrder: Use the beta method argument order for client
70 * methods, with optional arguments after the callback. Defaults to false.
71 * This option is only a temporary stopgap measure to smooth an API breakage.
72 * It is deprecated, and new code should not use it.
73 * - protobufjsVersion: Available values are 5, 6, and 'detect'. 5 and 6
74 * respectively indicate that an object from the corresponding version of
75 * ProtoBuf.js is provided in the value argument. If the option is 'detect',
76 * gRPC will guess what the version is based on the structure of the value.
77 * Defaults to 'detect'.
78 * @param {Object} value The ProtoBuf.js reflection object to load
79 * @param {Object=} options Options to apply to the loaded file
80 * @return {Object<string, *>} The resulting gRPC object
81 */
murgatroid99c02910b2016-02-17 12:59:26 -080082exports.loadObject = function loadObject(value, options) {
murgatroid99b1a02312017-03-17 13:45:15 -070083 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b3068542017-03-21 11:25:01 -070084 options = _.defaults(options, {'protobufjsVersion': 'detect'});
85 var protobufjsVersion;
86 if (options.protobufjsVersion === 'detect') {
87 if (protobuf_js_6_common.isProbablyProtobufJs6(value)) {
88 protobufjsVersion = 6;
89 } else if (protobuf_js_5_common.isProbablyProtobufJs5(value)) {
90 protobufjsVersion = 5;
91 } else {
92 var error_message = 'Could not detect ProtoBuf.js version. Please ' +
93 'specify the version number with the "protobufjs_version" option';
94 throw new Error(error_message);
95 }
murgatroid99b1a02312017-03-17 13:45:15 -070096 } else {
murgatroid99b3068542017-03-21 11:25:01 -070097 protobufjsVersion = options.protobufjsVersion;
98 }
99 switch (protobufjsVersion) {
100 case 6: return protobuf_js_6_common.loadObject(value, options);
101 case 5:
murgatroid99b1a02312017-03-17 13:45:15 -0700102 var deprecation_message = 'Calling grpc.loadObject with an object ' +
103 'generated by ProtoBuf.js 5 is deprecated. Please upgrade to ' +
104 'ProtoBuf.js 6.';
105 common.log(grpc.logVerbosity.INFO, deprecation_message);
106 return protobuf_js_5_common.loadObject(value, options);
murgatroid99b3068542017-03-21 11:25:01 -0700107 default:
108 throw new Error('Unrecognized protobufjsVersion', protobufjsVersion);
murgatroid99cca5ffa2015-01-15 14:06:56 -0800109 }
murgatroid999cd90a62015-07-27 11:23:13 -0700110};
111
112var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800113
murgatroid99b5b5f022017-03-16 16:42:10 -0700114function applyProtoRoot(filename, root) {
115 if (_.isString(filename)) {
116 return filename;
117 }
118 filename.root = path.resolve(filename.root) + '/';
119 root.resolvePath = function(originPath, importPath, alreadyNormalized) {
120 return ProtoBuf.util.path.resolve(filename.root,
121 importPath,
122 alreadyNormalized);
123 };
124 return filename.file;
125}
126
murgatroid99cca5ffa2015-01-15 14:06:56 -0800127/**
murgatroid99654d2542016-02-17 15:36:28 -0800128 * Load a gRPC object from a .proto file. The options object can provide the
129 * following options:
murgatroid99b3068542017-03-21 11:25:01 -0700130 * - convertFieldsToCamelCase: Load this file with field names in camel case
131 * instead of their original case
murgatroid99654d2542016-02-17 15:36:28 -0800132 * - binaryAsBase64: deserialize bytes values as base64 strings instead of
133 * Buffers. Defaults to false
134 * - longsAsStrings: deserialize long values as strings instead of objects.
135 * Defaults to true
murgatroid99ac7f90d2017-03-23 18:00:09 -0700136 * - enumsAsStrings: deserialize enum values as strings instead of numbers.
137 * Defaults to true
murgatroid9927b8d902016-03-22 14:46:37 -0700138 * - deprecatedArgumentOrder: Use the beta method argument order for client
139 * methods, with optional arguments after the callback. Defaults to false.
140 * This option is only a temporary stopgap measure to smooth an API breakage.
141 * It is deprecated, and new code should not use it.
murgatroid99c02910b2016-02-17 12:59:26 -0800142 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -0700143 * @param {string=} format The file format to expect. Must be either 'proto' or
144 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -0800145 * @param {Object=} options Options to apply to the loaded file
murgatroid99cca5ffa2015-01-15 14:06:56 -0800146 * @return {Object<string, *>} The resulting gRPC object
147 */
murgatroid99c02910b2016-02-17 12:59:26 -0800148exports.load = function load(filename, format, options) {
murgatroid99b5b5f022017-03-16 16:42:10 -0700149 /* Note: format is currently unused, because the API for loading a proto
150 file or a JSON file is identical in Protobuf.js 6. In the future, there is
151 still the possibility of adding other formats that would be loaded
152 differently */
153 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b3068542017-03-21 11:25:01 -0700154 options.protobufjs_version = 6;
murgatroid99b5b5f022017-03-16 16:42:10 -0700155 var root = new ProtoBuf.Root();
156 var parse_options = {keepCase: !options.convertFieldsToCamelCase};
157 return loadObject(root.loadSync(applyProtoRoot(filename, root),
158 parse_options),
159 options);
murgatroid999cd90a62015-07-27 11:23:13 -0700160};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800161
murgatroid991d2f2892016-06-02 14:33:22 -0700162var log_template = _.template(
163 '{severity} {timestamp}\t{file}:{line}]\t{message}',
164 {interpolate: /{([\s\S]+?)}/g});
165
murgatroid99cca5ffa2015-01-15 14:06:56 -0800166/**
murgatroid999b9708a2016-06-01 11:42:20 -0700167 * 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 */
174exports.setLogger = function setLogger(logger) {
175 common.logger = logger;
murgatroid991d2f2892016-06-02 14:33:22 -0700176 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 }));
murgatroid999b9708a2016-06-01 11:42:20 -0700185 });
186};
187
188/**
189 * Sets the logger verbosity for gRPC module logging. The options are members
190 * of the grpc.logVerbosity map.
191 * @param {Number} verbosity The minimum severity to log
192 */
193exports.setLogVerbosity = function setLogVerbosity(verbosity) {
194 common.logVerbosity = verbosity;
195 grpc.setLogVerbosity(verbosity);
196};
197
198/**
murgatroid999cd90a62015-07-27 11:23:13 -0700199 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800200 */
murgatroid99366e64d2015-07-15 17:01:49 -0700201exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800202
203/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700204 * @see module:src/metadata
205 */
206exports.Metadata = Metadata;
207
208/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800209 * Status name to code number mapping
210 */
211exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700212
murgatroid99cca5ffa2015-01-15 14:06:56 -0800213/**
murgatroid990b094572015-08-14 10:48:45 -0700214 * Propagate flag name to number mapping
215 */
216exports.propagate = grpc.propagate;
217
218/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800219 * Call error name to code number mapping
220 */
221exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800222
223/**
murgatroid994a1474f2015-08-17 14:00:31 -0700224 * Write flag name to code number mapping
225 */
226exports.writeFlags = grpc.writeFlags;
227
228/**
murgatroid999b9708a2016-06-01 11:42:20 -0700229 * Log verbosity setting name to code number mapping
230 */
231exports.logVerbosity = grpc.logVerbosity;
232
233/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800234 * Credentials factories
235 */
murgatroid99153b09d2015-09-25 16:04:03 -0700236exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800237
238/**
239 * ServerCredentials factories
240 */
241exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800242
murgatroid999cd90a62015-07-27 11:23:13 -0700243/**
244 * @see module:src/client.makeClientConstructor
245 */
murgatroid99e023e982015-03-18 17:17:33 -0700246exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700247
248/**
249 * @see module:src/client.getClientChannel
250 */
251exports.getClientChannel = client.getClientChannel;
252
253/**
254 * @see module:src/client.waitForClientReady
255 */
256exports.waitForClientReady = client.waitForClientReady;
murgatroid999030c812016-09-16 13:25:08 -0700257
258exports.closeClient = function closeClient(client_obj) {
259 client.getClientChannel(client_obj).close();
260};