blob: 452b11f6dba339346ea8e341529241d0e5db9c5e [file] [log] [blame]
murgatroid99c92df4c2017-05-23 10:28:47 -07001/**
2 * @license
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
murgatroid99cca5ffa2015-01-15 14:06:56 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
murgatroid99cca5ffa2015-01-15 14:06:56 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
murgatroid99cca5ffa2015-01-15 14:06:56 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
murgatroid99cca5ffa2015-01-15 14:06:56 -080016 *
17 */
18
murgatroid99dca966d2015-02-19 14:37:18 -080019'use strict';
20
murgatroid996d6009f2015-10-15 09:57:31 -070021var path = require('path');
murgatroid996f607662016-04-27 16:38:33 -070022var fs = require('fs');
murgatroid996d6009f2015-10-15 09:57:31 -070023
24var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem');
25
murgatroid9955739d52015-06-03 10:58:21 -070026var _ = require('lodash');
murgatroid99cca5ffa2015-01-15 14:06:56 -080027
28var ProtoBuf = require('protobufjs');
29
murgatroid99e7879552015-02-12 12:21:15 -080030var client = require('./src/client.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080031
murgatroid99e7879552015-02-12 12:21:15 -080032var server = require('./src/server.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080033
murgatroid999b9708a2016-06-01 11:42:20 -070034var common = require('./src/common.js');
35
murgatroid9984e3cde2015-08-20 11:27:05 -070036var Metadata = require('./src/metadata.js');
37
murgatroid99e190f352016-01-20 13:52:08 -080038var grpc = require('./src/grpc_extension');
murgatroid99cca5ffa2015-01-15 14:06:56 -080039
murgatroid99b5b5f022017-03-16 16:42:10 -070040var protobuf_js_5_common = require('./src/protobuf_js_5_common');
41var protobuf_js_6_common = require('./src/protobuf_js_6_common');
42
murgatroid997229f882017-05-10 16:24:05 -070043var constants = require('./src/constants.js');
44
murgatroid996f607662016-04-27 16:38:33 -070045grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
46
murgatroid99b3068542017-03-21 11:25:01 -070047/**
murgatroid99c92df4c2017-05-23 10:28:47 -070048 * @namespace grpc
49 */
50
51/**
52 * Load a ProtoBuf.js object as a gRPC object.
53 * @memberof grpc
54 * @alias grpc.loadObject
murgatroid99b3068542017-03-21 11:25:01 -070055 * @param {Object} value The ProtoBuf.js reflection object to load
56 * @param {Object=} options Options to apply to the loaded file
murgatroid99c92df4c2017-05-23 10:28:47 -070057 * @param {bool=} [options.binaryAsBase64=false] deserialize bytes values as
58 * base64 strings instead of Buffers
59 * @param {bool=} [options.longsAsStrings=true] deserialize long values as
60 * strings instead of objects
61 * @param {bool=} [options.enumsAsStrings=true] deserialize enum values as
62 * strings instead of numbers. Only works with Protobuf.js 6 values.
63 * @param {bool=} [options.deprecatedArgumentOrder=false] use the beta method
64 * argument order for client methods, with optional arguments after the
65 * callback. This option is only a temporary stopgap measure to smooth an
66 * API breakage. It is deprecated, and new code should not use it.
67 * @param {(number|string)=} [options.protobufjsVersion='detect'] 5 and 6
68 * respectively indicate that an object from the corresponding version of
69 * Protobuf.js is provided in the value argument. If the option is 'detect',
70 * gRPC wll guess what the version is based on the structure of the value.
71 * @return {Object<string, *>} The resulting gRPC object.
murgatroid99b3068542017-03-21 11:25:01 -070072 */
murgatroid99c02910b2016-02-17 12:59:26 -080073exports.loadObject = function loadObject(value, options) {
murgatroid99b1a02312017-03-17 13:45:15 -070074 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b3068542017-03-21 11:25:01 -070075 options = _.defaults(options, {'protobufjsVersion': 'detect'});
76 var protobufjsVersion;
77 if (options.protobufjsVersion === 'detect') {
78 if (protobuf_js_6_common.isProbablyProtobufJs6(value)) {
79 protobufjsVersion = 6;
80 } else if (protobuf_js_5_common.isProbablyProtobufJs5(value)) {
81 protobufjsVersion = 5;
82 } else {
83 var error_message = 'Could not detect ProtoBuf.js version. Please ' +
84 'specify the version number with the "protobufjs_version" option';
85 throw new Error(error_message);
86 }
murgatroid99b1a02312017-03-17 13:45:15 -070087 } else {
murgatroid99b3068542017-03-21 11:25:01 -070088 protobufjsVersion = options.protobufjsVersion;
89 }
90 switch (protobufjsVersion) {
91 case 6: return protobuf_js_6_common.loadObject(value, options);
92 case 5:
murgatroid99b1a02312017-03-17 13:45:15 -070093 return protobuf_js_5_common.loadObject(value, options);
murgatroid99b3068542017-03-21 11:25:01 -070094 default:
95 throw new Error('Unrecognized protobufjsVersion', protobufjsVersion);
murgatroid99cca5ffa2015-01-15 14:06:56 -080096 }
murgatroid999cd90a62015-07-27 11:23:13 -070097};
98
99var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800100
101/**
murgatroid99c92df4c2017-05-23 10:28:47 -0700102 * Load a gRPC object from a .proto file.
103 * @memberof grpc
104 * @alias grpc.load
murgatroid99c02910b2016-02-17 12:59:26 -0800105 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -0700106 * @param {string=} format The file format to expect. Must be either 'proto' or
107 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -0800108 * @param {Object=} options Options to apply to the loaded file
murgatroid99c92df4c2017-05-23 10:28:47 -0700109 * @param {bool=} [options.convertFieldsToCamelCase=false] Load this file with
110 * field names in camel case instead of their original case
111 * @param {bool=} [options.binaryAsBase64=false] deserialize bytes values as
112 * base64 strings instead of Buffers
113 * @param {bool=} [options.longsAsStrings=true] deserialize long values as
114 * strings instead of objects
115 * @param {bool=} [options.deprecatedArgumentOrder=false] use the beta method
116 * argument order for client methods, with optional arguments after the
117 * callback. This option is only a temporary stopgap measure to smooth an
118 * API breakage. It is deprecated, and new code should not use it.
murgatroid99cca5ffa2015-01-15 14:06:56 -0800119 * @return {Object<string, *>} The resulting gRPC object
120 */
murgatroid99c02910b2016-02-17 12:59:26 -0800121exports.load = function load(filename, format, options) {
murgatroid99b5b5f022017-03-16 16:42:10 -0700122 options = _.defaults(options, common.defaultGrpcOptions);
murgatroid99b1c69e42017-05-02 14:01:35 -0700123 options.protobufjsVersion = 5;
124 if (!format) {
125 format = 'proto';
126 }
127 var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
128 if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
129 ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
130 }
131 var builder;
132 try {
133 switch(format) {
134 case 'proto':
135 builder = ProtoBuf.loadProtoFile(filename);
136 break;
137 case 'json':
138 builder = ProtoBuf.loadJsonFile(filename);
139 break;
140 default:
141 throw new Error('Unrecognized format "' + format + '"');
142 }
143 } finally {
144 ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
145 }
146 return loadObject(builder.ns, options);
murgatroid999cd90a62015-07-27 11:23:13 -0700147};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800148
murgatroid991d2f2892016-06-02 14:33:22 -0700149var log_template = _.template(
150 '{severity} {timestamp}\t{file}:{line}]\t{message}',
151 {interpolate: /{([\s\S]+?)}/g});
152
murgatroid99cca5ffa2015-01-15 14:06:56 -0800153/**
murgatroid999b9708a2016-06-01 11:42:20 -0700154 * Sets the logger function for the gRPC module. For debugging purposes, the C
155 * core will log synchronously directly to stdout unless this function is
156 * called. Note: the output format here is intended to be informational, and
157 * is not guaranteed to stay the same in the future.
158 * Logs will be directed to logger.error.
murgatroid99c92df4c2017-05-23 10:28:47 -0700159 * @memberof grpc
160 * @alias grpc.setLogger
murgatroid999b9708a2016-06-01 11:42:20 -0700161 * @param {Console} logger A Console-like object.
162 */
163exports.setLogger = function setLogger(logger) {
164 common.logger = logger;
murgatroid991d2f2892016-06-02 14:33:22 -0700165 grpc.setDefaultLoggerCallback(function(file, line, severity,
166 message, timestamp) {
167 logger.error(log_template({
168 file: path.basename(file),
169 line: line,
170 severity: severity,
171 message: message,
172 timestamp: timestamp.toISOString()
173 }));
murgatroid999b9708a2016-06-01 11:42:20 -0700174 });
175};
176
177/**
178 * Sets the logger verbosity for gRPC module logging. The options are members
179 * of the grpc.logVerbosity map.
murgatroid99c92df4c2017-05-23 10:28:47 -0700180 * @memberof grpc
181 * @alias grpc.setLogVerbosity
murgatroid999b9708a2016-06-01 11:42:20 -0700182 * @param {Number} verbosity The minimum severity to log
183 */
184exports.setLogVerbosity = function setLogVerbosity(verbosity) {
185 common.logVerbosity = verbosity;
186 grpc.setLogVerbosity(verbosity);
187};
188
murgatroid99366e64d2015-07-15 17:01:49 -0700189exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800190
murgatroid9984e3cde2015-08-20 11:27:05 -0700191exports.Metadata = Metadata;
192
murgatroid997229f882017-05-10 16:24:05 -0700193exports.status = constants.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700194
murgatroid997229f882017-05-10 16:24:05 -0700195exports.propagate = constants.propagate;
murgatroid990b094572015-08-14 10:48:45 -0700196
murgatroid997229f882017-05-10 16:24:05 -0700197exports.callError = constants.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800198
murgatroid997229f882017-05-10 16:24:05 -0700199exports.writeFlags = constants.writeFlags;
murgatroid994a1474f2015-08-17 14:00:31 -0700200
murgatroid997229f882017-05-10 16:24:05 -0700201exports.logVerbosity = constants.logVerbosity;
murgatroid999b9708a2016-06-01 11:42:20 -0700202
murgatroid99153b09d2015-09-25 16:04:03 -0700203exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800204
205/**
206 * ServerCredentials factories
murgatroid99c92df4c2017-05-23 10:28:47 -0700207 * @constructor ServerCredentials
208 * @memberof grpc
murgatroid99b6ab1b42015-01-21 10:30:36 -0800209 */
210exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800211
murgatroid999cd90a62015-07-27 11:23:13 -0700212/**
murgatroid99c92df4c2017-05-23 10:28:47 -0700213 * Create insecure server credentials
214 * @name grpc.ServerCredentials.createInsecure
215 * @kind function
216 * @return grpc.ServerCredentials
murgatroid999cd90a62015-07-27 11:23:13 -0700217 */
murgatroid99c92df4c2017-05-23 10:28:47 -0700218
219/**
220 * A private key and certificate pair
221 * @typedef {Object} grpc.ServerCredentials~keyCertPair
222 * @property {Buffer} privateKey The server's private key
223 * @property {Buffer} certChain The server's certificate chain
224 */
225
226/**
227 * Create SSL server credentials
228 * @name grpc.ServerCredentials.createInsecure
229 * @kind function
230 * @param {?Buffer} rootCerts Root CA certificates for validating client
231 * certificates
232 * @param {Array<grpc.ServerCredentials~keyCertPair>} keyCertPairs A list of
233 * private key and certificate chain pairs to be used for authenticating
234 * the server
235 * @param {boolean} [checkClientCertificate=false] Indicates that the server
236 * should request and verify the client's certificates
237 * @return grpc.ServerCredentials
238 */
239
murgatroid99e023e982015-03-18 17:17:33 -0700240exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700241
murgatroid9976ba1ff2015-08-28 14:57:04 -0700242exports.getClientChannel = client.getClientChannel;
243
murgatroid9976ba1ff2015-08-28 14:57:04 -0700244exports.waitForClientReady = client.waitForClientReady;
murgatroid999030c812016-09-16 13:25:08 -0700245
murgatroid99c92df4c2017-05-23 10:28:47 -0700246/**
247 * @memberof grpc
248 * @alias grpc.closeClient
249 * @param {grpc.Client} client_obj The client to close
250 */
murgatroid999030c812016-09-16 13:25:08 -0700251exports.closeClient = function closeClient(client_obj) {
murgatroid99ffac55d2017-05-05 13:54:03 -0700252 client.Client.prototype.close.apply(client_obj);
murgatroid999030c812016-09-16 13:25:08 -0700253};
murgatroid99ffac55d2017-05-05 13:54:03 -0700254
murgatroid99ffac55d2017-05-05 13:54:03 -0700255exports.Client = client.Client;