blob: 177628e22d49f4fbcdee2bb9268f913b06467979 [file] [log] [blame]
murgatroid99c92df4c2017-05-23 10:28:47 -07001/**
2 * @license
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
murgatroid997229f882017-05-10 16:24:05 -070058var constants = require('./src/constants.js');
59
murgatroid996f607662016-04-27 16:38:33 -070060grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
61
murgatroid99b3068542017-03-21 11:25:01 -070062/**
murgatroid99c92df4c2017-05-23 10:28:47 -070063 * @namespace grpc
64 */
65
66/**
67 * Load a ProtoBuf.js object as a gRPC object.
68 * @memberof grpc
69 * @alias grpc.loadObject
murgatroid99b3068542017-03-21 11:25:01 -070070 * @param {Object} value The ProtoBuf.js reflection object to load
71 * @param {Object=} options Options to apply to the loaded file
murgatroid99c92df4c2017-05-23 10:28:47 -070072 * @param {bool=} [options.binaryAsBase64=false] deserialize bytes values as
73 * base64 strings instead of Buffers
74 * @param {bool=} [options.longsAsStrings=true] deserialize long values as
75 * strings instead of objects
76 * @param {bool=} [options.enumsAsStrings=true] deserialize enum values as
77 * strings instead of numbers. Only works with Protobuf.js 6 values.
78 * @param {bool=} [options.deprecatedArgumentOrder=false] use the beta method
79 * argument order for client methods, with optional arguments after the
80 * callback. This option is only a temporary stopgap measure to smooth an
81 * API breakage. It is deprecated, and new code should not use it.
82 * @param {(number|string)=} [options.protobufjsVersion='detect'] 5 and 6
83 * respectively indicate that an object from the corresponding version of
84 * Protobuf.js is provided in the value argument. If the option is 'detect',
85 * gRPC wll guess what the version is based on the structure of the value.
86 * @return {Object<string, *>} The resulting gRPC object.
murgatroid99b3068542017-03-21 11:25:01 -070087 */
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 return protobuf_js_5_common.loadObject(value, options);
murgatroid99b3068542017-03-21 11:25:01 -0700109 default:
110 throw new Error('Unrecognized protobufjsVersion', protobufjsVersion);
murgatroid99cca5ffa2015-01-15 14:06:56 -0800111 }
murgatroid999cd90a62015-07-27 11:23:13 -0700112};
113
114var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800115
116/**
murgatroid99c92df4c2017-05-23 10:28:47 -0700117 * Load a gRPC object from a .proto file.
118 * @memberof grpc
119 * @alias grpc.load
murgatroid99c02910b2016-02-17 12:59:26 -0800120 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -0700121 * @param {string=} format The file format to expect. Must be either 'proto' or
122 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -0800123 * @param {Object=} options Options to apply to the loaded file
murgatroid99c92df4c2017-05-23 10:28:47 -0700124 * @param {bool=} [options.convertFieldsToCamelCase=false] Load this file with
125 * field names in camel case instead of their original case
126 * @param {bool=} [options.binaryAsBase64=false] deserialize bytes values as
127 * base64 strings instead of Buffers
128 * @param {bool=} [options.longsAsStrings=true] deserialize long values as
129 * strings instead of objects
130 * @param {bool=} [options.deprecatedArgumentOrder=false] use the beta method
131 * argument order for client methods, with optional arguments after the
132 * callback. This option is only a temporary stopgap measure to smooth an
133 * API breakage. It is deprecated, and new code should not use it.
murgatroid99cca5ffa2015-01-15 14:06:56 -0800134 * @return {Object<string, *>} The resulting gRPC object
135 */
murgatroid99c02910b2016-02-17 12:59:26 -0800136exports.load = function load(filename, format, options) {
murgatroid99b5b5f022017-03-16 16:42:10 -0700137 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b1c69e42017-05-02 14:01:35 -0700138 options.protobufjsVersion = 5;
139 if (!format) {
140 format = 'proto';
141 }
142 var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
143 if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
144 ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
145 }
146 var builder;
147 try {
148 switch(format) {
149 case 'proto':
150 builder = ProtoBuf.loadProtoFile(filename);
151 break;
152 case 'json':
153 builder = ProtoBuf.loadJsonFile(filename);
154 break;
155 default:
156 throw new Error('Unrecognized format "' + format + '"');
157 }
158 } finally {
159 ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
160 }
161 return loadObject(builder.ns, options);
murgatroid999cd90a62015-07-27 11:23:13 -0700162};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800163
murgatroid991d2f2892016-06-02 14:33:22 -0700164var log_template = _.template(
165 '{severity} {timestamp}\t{file}:{line}]\t{message}',
166 {interpolate: /{([\s\S]+?)}/g});
167
murgatroid99cca5ffa2015-01-15 14:06:56 -0800168/**
murgatroid999b9708a2016-06-01 11:42:20 -0700169 * Sets the logger function for the gRPC module. For debugging purposes, the C
170 * core will log synchronously directly to stdout unless this function is
171 * called. Note: the output format here is intended to be informational, and
172 * is not guaranteed to stay the same in the future.
173 * Logs will be directed to logger.error.
murgatroid99c92df4c2017-05-23 10:28:47 -0700174 * @memberof grpc
175 * @alias grpc.setLogger
murgatroid999b9708a2016-06-01 11:42:20 -0700176 * @param {Console} logger A Console-like object.
177 */
178exports.setLogger = function setLogger(logger) {
179 common.logger = logger;
murgatroid991d2f2892016-06-02 14:33:22 -0700180 grpc.setDefaultLoggerCallback(function(file, line, severity,
181 message, timestamp) {
182 logger.error(log_template({
183 file: path.basename(file),
184 line: line,
185 severity: severity,
186 message: message,
187 timestamp: timestamp.toISOString()
188 }));
murgatroid999b9708a2016-06-01 11:42:20 -0700189 });
190};
191
192/**
193 * Sets the logger verbosity for gRPC module logging. The options are members
194 * of the grpc.logVerbosity map.
murgatroid99c92df4c2017-05-23 10:28:47 -0700195 * @memberof grpc
196 * @alias grpc.setLogVerbosity
murgatroid999b9708a2016-06-01 11:42:20 -0700197 * @param {Number} verbosity The minimum severity to log
198 */
199exports.setLogVerbosity = function setLogVerbosity(verbosity) {
200 common.logVerbosity = verbosity;
201 grpc.setLogVerbosity(verbosity);
202};
203
murgatroid99366e64d2015-07-15 17:01:49 -0700204exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800205
murgatroid9984e3cde2015-08-20 11:27:05 -0700206exports.Metadata = Metadata;
207
murgatroid997229f882017-05-10 16:24:05 -0700208exports.status = constants.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700209
murgatroid997229f882017-05-10 16:24:05 -0700210exports.propagate = constants.propagate;
murgatroid990b094572015-08-14 10:48:45 -0700211
murgatroid997229f882017-05-10 16:24:05 -0700212exports.callError = constants.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800213
murgatroid997229f882017-05-10 16:24:05 -0700214exports.writeFlags = constants.writeFlags;
murgatroid994a1474f2015-08-17 14:00:31 -0700215
murgatroid997229f882017-05-10 16:24:05 -0700216exports.logVerbosity = constants.logVerbosity;
murgatroid999b9708a2016-06-01 11:42:20 -0700217
murgatroid99153b09d2015-09-25 16:04:03 -0700218exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800219
220/**
221 * ServerCredentials factories
murgatroid99c92df4c2017-05-23 10:28:47 -0700222 * @constructor ServerCredentials
223 * @memberof grpc
murgatroid99b6ab1b42015-01-21 10:30:36 -0800224 */
225exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800226
murgatroid999cd90a62015-07-27 11:23:13 -0700227/**
murgatroid99c92df4c2017-05-23 10:28:47 -0700228 * Create insecure server credentials
229 * @name grpc.ServerCredentials.createInsecure
230 * @kind function
231 * @return grpc.ServerCredentials
murgatroid999cd90a62015-07-27 11:23:13 -0700232 */
murgatroid99c92df4c2017-05-23 10:28:47 -0700233
234/**
235 * A private key and certificate pair
236 * @typedef {Object} grpc.ServerCredentials~keyCertPair
237 * @property {Buffer} privateKey The server's private key
238 * @property {Buffer} certChain The server's certificate chain
239 */
240
241/**
242 * Create SSL server credentials
243 * @name grpc.ServerCredentials.createInsecure
244 * @kind function
245 * @param {?Buffer} rootCerts Root CA certificates for validating client
246 * certificates
247 * @param {Array<grpc.ServerCredentials~keyCertPair>} keyCertPairs A list of
248 * private key and certificate chain pairs to be used for authenticating
249 * the server
250 * @param {boolean} [checkClientCertificate=false] Indicates that the server
251 * should request and verify the client's certificates
252 * @return grpc.ServerCredentials
253 */
254
murgatroid99e023e982015-03-18 17:17:33 -0700255exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700256
murgatroid9976ba1ff2015-08-28 14:57:04 -0700257exports.getClientChannel = client.getClientChannel;
258
murgatroid9976ba1ff2015-08-28 14:57:04 -0700259exports.waitForClientReady = client.waitForClientReady;
murgatroid999030c812016-09-16 13:25:08 -0700260
murgatroid99c92df4c2017-05-23 10:28:47 -0700261/**
262 * @memberof grpc
263 * @alias grpc.closeClient
264 * @param {grpc.Client} client_obj The client to close
265 */
murgatroid999030c812016-09-16 13:25:08 -0700266exports.closeClient = function closeClient(client_obj) {
murgatroid99ffac55d2017-05-05 13:54:03 -0700267 client.Client.prototype.close.apply(client_obj);
murgatroid999030c812016-09-16 13:25:08 -0700268};
murgatroid99ffac55d2017-05-05 13:54:03 -0700269
murgatroid99ffac55d2017-05-05 13:54:03 -0700270exports.Client = client.Client;