blob: 9fb6faa5d7ce77b1f0df37b9d2c5336ac5acf88f [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
murgatroid996f607662016-04-27 16:38:33 -070055grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
56
murgatroid99cca5ffa2015-01-15 14:06:56 -080057/**
58 * Load a gRPC object from an existing ProtoBuf.Reflect object.
59 * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
murgatroid99c02910b2016-02-17 12:59:26 -080060 * @param {Object=} options Options to apply to the loaded object
murgatroid99cca5ffa2015-01-15 14:06:56 -080061 * @return {Object<string, *>} The resulting gRPC object
62 */
murgatroid99c02910b2016-02-17 12:59:26 -080063exports.loadObject = function loadObject(value, options) {
murgatroid99cca5ffa2015-01-15 14:06:56 -080064 var result = {};
65 if (value.className === 'Namespace') {
66 _.each(value.children, function(child) {
murgatroid99c02910b2016-02-17 12:59:26 -080067 result[child.name] = loadObject(child, options);
murgatroid99cca5ffa2015-01-15 14:06:56 -080068 });
69 return result;
70 } else if (value.className === 'Service') {
murgatroid99c02910b2016-02-17 12:59:26 -080071 return client.makeProtobufClientConstructor(value, options);
murgatroid9997d61302015-01-20 18:06:43 -080072 } else if (value.className === 'Message' || value.className === 'Enum') {
murgatroid99cca5ffa2015-01-15 14:06:56 -080073 return value.build();
74 } else {
75 return value;
76 }
murgatroid999cd90a62015-07-27 11:23:13 -070077};
78
79var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -080080
81/**
murgatroid99654d2542016-02-17 15:36:28 -080082 * Load a gRPC object from a .proto file. The options object can provide the
83 * following options:
84 * - convertFieldsToCamelCase: Loads this file with that option on protobuf.js
85 * set as specified. See
86 * https://github.com/dcodeIO/protobuf.js/wiki/Advanced-options for details
87 * - binaryAsBase64: deserialize bytes values as base64 strings instead of
88 * Buffers. Defaults to false
89 * - longsAsStrings: deserialize long values as strings instead of objects.
90 * Defaults to true
murgatroid9927b8d902016-03-22 14:46:37 -070091 * - deprecatedArgumentOrder: Use the beta method argument order for client
92 * methods, with optional arguments after the callback. Defaults to false.
93 * This option is only a temporary stopgap measure to smooth an API breakage.
94 * It is deprecated, and new code should not use it.
murgatroid99c02910b2016-02-17 12:59:26 -080095 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070096 * @param {string=} format The file format to expect. Must be either 'proto' or
97 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -080098 * @param {Object=} options Options to apply to the loaded file
murgatroid99cca5ffa2015-01-15 14:06:56 -080099 * @return {Object<string, *>} The resulting gRPC object
100 */
murgatroid99c02910b2016-02-17 12:59:26 -0800101exports.load = function load(filename, format, options) {
murgatroid9971dbb862015-04-20 11:22:51 -0700102 if (!format) {
103 format = 'proto';
104 }
murgatroid99c02910b2016-02-17 12:59:26 -0800105 var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
106 if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
107 ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
murgatroid9971dbb862015-04-20 11:22:51 -0700108 }
murgatroid99c02910b2016-02-17 12:59:26 -0800109 var builder;
110 try {
111 switch(format) {
112 case 'proto':
113 builder = ProtoBuf.loadProtoFile(filename);
114 break;
115 case 'json':
116 builder = ProtoBuf.loadJsonFile(filename);
117 break;
118 default:
119 throw new Error('Unrecognized format "' + format + '"');
120 }
121 } finally {
122 ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
123 }
124 return loadObject(builder.ns, options);
murgatroid999cd90a62015-07-27 11:23:13 -0700125};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800126
murgatroid991d2f2892016-06-02 14:33:22 -0700127var log_template = _.template(
128 '{severity} {timestamp}\t{file}:{line}]\t{message}',
129 {interpolate: /{([\s\S]+?)}/g});
130
murgatroid99cca5ffa2015-01-15 14:06:56 -0800131/**
murgatroid999b9708a2016-06-01 11:42:20 -0700132 * Sets the logger function for the gRPC module. For debugging purposes, the C
133 * core will log synchronously directly to stdout unless this function is
134 * called. Note: the output format here is intended to be informational, and
135 * is not guaranteed to stay the same in the future.
136 * Logs will be directed to logger.error.
137 * @param {Console} logger A Console-like object.
138 */
139exports.setLogger = function setLogger(logger) {
140 common.logger = logger;
murgatroid991d2f2892016-06-02 14:33:22 -0700141 grpc.setDefaultLoggerCallback(function(file, line, severity,
142 message, timestamp) {
143 logger.error(log_template({
144 file: path.basename(file),
145 line: line,
146 severity: severity,
147 message: message,
148 timestamp: timestamp.toISOString()
149 }));
murgatroid999b9708a2016-06-01 11:42:20 -0700150 });
151};
152
153/**
154 * Sets the logger verbosity for gRPC module logging. The options are members
155 * of the grpc.logVerbosity map.
156 * @param {Number} verbosity The minimum severity to log
157 */
158exports.setLogVerbosity = function setLogVerbosity(verbosity) {
159 common.logVerbosity = verbosity;
160 grpc.setLogVerbosity(verbosity);
161};
162
163/**
murgatroid999cd90a62015-07-27 11:23:13 -0700164 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800165 */
murgatroid99366e64d2015-07-15 17:01:49 -0700166exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800167
168/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700169 * @see module:src/metadata
170 */
171exports.Metadata = Metadata;
172
173/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800174 * Status name to code number mapping
175 */
176exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700177
murgatroid99cca5ffa2015-01-15 14:06:56 -0800178/**
murgatroid990b094572015-08-14 10:48:45 -0700179 * Propagate flag name to number mapping
180 */
181exports.propagate = grpc.propagate;
182
183/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800184 * Call error name to code number mapping
185 */
186exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800187
188/**
murgatroid994a1474f2015-08-17 14:00:31 -0700189 * Write flag name to code number mapping
190 */
191exports.writeFlags = grpc.writeFlags;
192
193/**
murgatroid999b9708a2016-06-01 11:42:20 -0700194 * Log verbosity setting name to code number mapping
195 */
196exports.logVerbosity = grpc.logVerbosity;
197
198/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800199 * Credentials factories
200 */
murgatroid99153b09d2015-09-25 16:04:03 -0700201exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800202
203/**
204 * ServerCredentials factories
205 */
206exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800207
murgatroid999cd90a62015-07-27 11:23:13 -0700208/**
209 * @see module:src/client.makeClientConstructor
210 */
murgatroid99e023e982015-03-18 17:17:33 -0700211exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700212
213/**
214 * @see module:src/client.getClientChannel
215 */
216exports.getClientChannel = client.getClientChannel;
217
218/**
219 * @see module:src/client.waitForClientReady
220 */
221exports.waitForClientReady = client.waitForClientReady;