blob: 4e4d12d9e45f14327c1e7c8e5aab5b8bd88abad3 [file] [log] [blame]
murgatroid99cca5ffa2015-01-15 14:06:56 -08001/*
2 *
murgatroid99e190f352016-01-20 13:52:08 -08003 * Copyright 2015-2016, 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');
37
38var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem');
39
40if (!process.env.GRPC_DEFAULT_SSL_ROOTS_FILE_PATH) {
41 process.env.GRPC_DEFAULT_SSL_ROOTS_FILE_PATH = SSL_ROOTS_PATH;
42}
43
murgatroid9955739d52015-06-03 10:58:21 -070044var _ = require('lodash');
murgatroid99cca5ffa2015-01-15 14:06:56 -080045
46var ProtoBuf = require('protobufjs');
47
murgatroid99e7879552015-02-12 12:21:15 -080048var client = require('./src/client.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080049
murgatroid99e7879552015-02-12 12:21:15 -080050var server = require('./src/server.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080051
murgatroid9984e3cde2015-08-20 11:27:05 -070052var Metadata = require('./src/metadata.js');
53
murgatroid99e190f352016-01-20 13:52:08 -080054var grpc = require('./src/grpc_extension');
murgatroid99cca5ffa2015-01-15 14:06:56 -080055
56/**
57 * Load a gRPC object from an existing ProtoBuf.Reflect object.
58 * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
murgatroid99c02910b2016-02-17 12:59:26 -080059 * @param {Object=} options Options to apply to the loaded object
murgatroid99cca5ffa2015-01-15 14:06:56 -080060 * @return {Object<string, *>} The resulting gRPC object
61 */
murgatroid99c02910b2016-02-17 12:59:26 -080062exports.loadObject = function loadObject(value, options) {
murgatroid99cca5ffa2015-01-15 14:06:56 -080063 var result = {};
64 if (value.className === 'Namespace') {
65 _.each(value.children, function(child) {
murgatroid99c02910b2016-02-17 12:59:26 -080066 result[child.name] = loadObject(child, options);
murgatroid99cca5ffa2015-01-15 14:06:56 -080067 });
68 return result;
69 } else if (value.className === 'Service') {
murgatroid99c02910b2016-02-17 12:59:26 -080070 return client.makeProtobufClientConstructor(value, options);
murgatroid9997d61302015-01-20 18:06:43 -080071 } else if (value.className === 'Message' || value.className === 'Enum') {
murgatroid99cca5ffa2015-01-15 14:06:56 -080072 return value.build();
73 } else {
74 return value;
75 }
murgatroid999cd90a62015-07-27 11:23:13 -070076};
77
78var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -080079
80/**
81 * Load a gRPC object from a .proto file.
murgatroid99c02910b2016-02-17 12:59:26 -080082 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070083 * @param {string=} format The file format to expect. Must be either 'proto' or
84 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -080085 * @param {Object=} options Options to apply to the loaded file
murgatroid99cca5ffa2015-01-15 14:06:56 -080086 * @return {Object<string, *>} The resulting gRPC object
87 */
murgatroid99c02910b2016-02-17 12:59:26 -080088exports.load = function load(filename, format, options) {
murgatroid9971dbb862015-04-20 11:22:51 -070089 if (!format) {
90 format = 'proto';
91 }
murgatroid99c02910b2016-02-17 12:59:26 -080092 var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
93 if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
94 ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
murgatroid9971dbb862015-04-20 11:22:51 -070095 }
murgatroid99c02910b2016-02-17 12:59:26 -080096 var builder;
97 try {
98 switch(format) {
99 case 'proto':
100 builder = ProtoBuf.loadProtoFile(filename);
101 break;
102 case 'json':
103 builder = ProtoBuf.loadJsonFile(filename);
104 break;
105 default:
106 throw new Error('Unrecognized format "' + format + '"');
107 }
108 } finally {
109 ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
110 }
111 return loadObject(builder.ns, options);
murgatroid999cd90a62015-07-27 11:23:13 -0700112};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800113
114/**
murgatroid999cd90a62015-07-27 11:23:13 -0700115 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800116 */
murgatroid99366e64d2015-07-15 17:01:49 -0700117exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800118
119/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700120 * @see module:src/metadata
121 */
122exports.Metadata = Metadata;
123
124/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800125 * Status name to code number mapping
126 */
127exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700128
murgatroid99cca5ffa2015-01-15 14:06:56 -0800129/**
murgatroid990b094572015-08-14 10:48:45 -0700130 * Propagate flag name to number mapping
131 */
132exports.propagate = grpc.propagate;
133
134/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800135 * Call error name to code number mapping
136 */
137exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800138
139/**
murgatroid994a1474f2015-08-17 14:00:31 -0700140 * Write flag name to code number mapping
141 */
142exports.writeFlags = grpc.writeFlags;
143
144/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800145 * Credentials factories
146 */
murgatroid99153b09d2015-09-25 16:04:03 -0700147exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800148
149/**
150 * ServerCredentials factories
151 */
152exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800153
murgatroid999cd90a62015-07-27 11:23:13 -0700154/**
155 * @see module:src/client.makeClientConstructor
156 */
murgatroid99e023e982015-03-18 17:17:33 -0700157exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700158
159/**
160 * @see module:src/client.getClientChannel
161 */
162exports.getClientChannel = client.getClientChannel;
163
164/**
165 * @see module:src/client.waitForClientReady
166 */
167exports.waitForClientReady = client.waitForClientReady;