blob: e41664ebf38259dac1655fb87cc2699448b64138 [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
murgatroid99c02910b2016-02-17 12:59:26 -080060exports.loadObject = function loadObject(value, options) {
murgatroid99b1a02312017-03-17 13:45:15 -070061 options = _.defaults(options, common.defaultGrpcOptions);
62 if (value instanceof ProtoBuf.ReflectionObject) {
63 return protobuf_js_6_common.loadObject(value, options);
64 } else {
65 /* If value is not a ProtoBuf.js 6 reflection object, we assume that it is
66 a ProtoBuf.js 5 reflection object, for backwards compatibility */
67 var deprecation_message = 'Calling grpc.loadObject with an object ' +
68 'generated by ProtoBuf.js 5 is deprecated. Please upgrade to ' +
69 'ProtoBuf.js 6.';
70 common.log(grpc.logVerbosity.INFO, deprecation_message);
71 return protobuf_js_5_common.loadObject(value, options);
murgatroid99cca5ffa2015-01-15 14:06:56 -080072 }
murgatroid999cd90a62015-07-27 11:23:13 -070073};
74
75var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -080076
murgatroid99b5b5f022017-03-16 16:42:10 -070077function applyProtoRoot(filename, root) {
78 if (_.isString(filename)) {
79 return filename;
80 }
81 filename.root = path.resolve(filename.root) + '/';
82 root.resolvePath = function(originPath, importPath, alreadyNormalized) {
83 return ProtoBuf.util.path.resolve(filename.root,
84 importPath,
85 alreadyNormalized);
86 };
87 return filename.file;
88}
89
murgatroid99cca5ffa2015-01-15 14:06:56 -080090/**
murgatroid99654d2542016-02-17 15:36:28 -080091 * Load a gRPC object from a .proto file. The options object can provide the
92 * following options:
93 * - convertFieldsToCamelCase: Loads this file with that option on protobuf.js
94 * set as specified. See
95 * https://github.com/dcodeIO/protobuf.js/wiki/Advanced-options for details
96 * - binaryAsBase64: deserialize bytes values as base64 strings instead of
97 * Buffers. Defaults to false
98 * - longsAsStrings: deserialize long values as strings instead of objects.
99 * Defaults to true
murgatroid9927b8d902016-03-22 14:46:37 -0700100 * - deprecatedArgumentOrder: Use the beta method argument order for client
101 * methods, with optional arguments after the callback. Defaults to false.
102 * This option is only a temporary stopgap measure to smooth an API breakage.
103 * It is deprecated, and new code should not use it.
murgatroid99c02910b2016-02-17 12:59:26 -0800104 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -0700105 * @param {string=} format The file format to expect. Must be either 'proto' or
106 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -0800107 * @param {Object=} options Options to apply to the loaded file
murgatroid99cca5ffa2015-01-15 14:06:56 -0800108 * @return {Object<string, *>} The resulting gRPC object
109 */
murgatroid99c02910b2016-02-17 12:59:26 -0800110exports.load = function load(filename, format, options) {
murgatroid99b5b5f022017-03-16 16:42:10 -0700111 /* Note: format is currently unused, because the API for loading a proto
112 file or a JSON file is identical in Protobuf.js 6. In the future, there is
113 still the possibility of adding other formats that would be loaded
114 differently */
115 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b5b5f022017-03-16 16:42:10 -0700116 var root = new ProtoBuf.Root();
117 var parse_options = {keepCase: !options.convertFieldsToCamelCase};
118 return loadObject(root.loadSync(applyProtoRoot(filename, root),
119 parse_options),
120 options);
murgatroid999cd90a62015-07-27 11:23:13 -0700121};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800122
murgatroid991d2f2892016-06-02 14:33:22 -0700123var log_template = _.template(
124 '{severity} {timestamp}\t{file}:{line}]\t{message}',
125 {interpolate: /{([\s\S]+?)}/g});
126
murgatroid99cca5ffa2015-01-15 14:06:56 -0800127/**
murgatroid999b9708a2016-06-01 11:42:20 -0700128 * Sets the logger function for the gRPC module. For debugging purposes, the C
129 * core will log synchronously directly to stdout unless this function is
130 * called. Note: the output format here is intended to be informational, and
131 * is not guaranteed to stay the same in the future.
132 * Logs will be directed to logger.error.
133 * @param {Console} logger A Console-like object.
134 */
135exports.setLogger = function setLogger(logger) {
136 common.logger = logger;
murgatroid991d2f2892016-06-02 14:33:22 -0700137 grpc.setDefaultLoggerCallback(function(file, line, severity,
138 message, timestamp) {
139 logger.error(log_template({
140 file: path.basename(file),
141 line: line,
142 severity: severity,
143 message: message,
144 timestamp: timestamp.toISOString()
145 }));
murgatroid999b9708a2016-06-01 11:42:20 -0700146 });
147};
148
149/**
150 * Sets the logger verbosity for gRPC module logging. The options are members
151 * of the grpc.logVerbosity map.
152 * @param {Number} verbosity The minimum severity to log
153 */
154exports.setLogVerbosity = function setLogVerbosity(verbosity) {
155 common.logVerbosity = verbosity;
156 grpc.setLogVerbosity(verbosity);
157};
158
159/**
murgatroid999cd90a62015-07-27 11:23:13 -0700160 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800161 */
murgatroid99366e64d2015-07-15 17:01:49 -0700162exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800163
164/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700165 * @see module:src/metadata
166 */
167exports.Metadata = Metadata;
168
169/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800170 * Status name to code number mapping
171 */
172exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700173
murgatroid99cca5ffa2015-01-15 14:06:56 -0800174/**
murgatroid990b094572015-08-14 10:48:45 -0700175 * Propagate flag name to number mapping
176 */
177exports.propagate = grpc.propagate;
178
179/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800180 * Call error name to code number mapping
181 */
182exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800183
184/**
murgatroid994a1474f2015-08-17 14:00:31 -0700185 * Write flag name to code number mapping
186 */
187exports.writeFlags = grpc.writeFlags;
188
189/**
murgatroid999b9708a2016-06-01 11:42:20 -0700190 * Log verbosity setting name to code number mapping
191 */
192exports.logVerbosity = grpc.logVerbosity;
193
194/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800195 * Credentials factories
196 */
murgatroid99153b09d2015-09-25 16:04:03 -0700197exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800198
199/**
200 * ServerCredentials factories
201 */
202exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800203
murgatroid999cd90a62015-07-27 11:23:13 -0700204/**
205 * @see module:src/client.makeClientConstructor
206 */
murgatroid99e023e982015-03-18 17:17:33 -0700207exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700208
209/**
210 * @see module:src/client.getClientChannel
211 */
212exports.getClientChannel = client.getClientChannel;
213
214/**
215 * @see module:src/client.waitForClientReady
216 */
217exports.waitForClientReady = client.waitForClientReady;
murgatroid999030c812016-09-16 13:25:08 -0700218
219exports.closeClient = function closeClient(client_obj) {
220 client.getClientChannel(client_obj).close();
221};