blob: 1c197729d776fc352ffbc38b9ff85506bf5f4a13 [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/**
murgatroid99654d2542016-02-17 15:36:28 -080081 * Load a gRPC object from a .proto file. The options object can provide the
82 * following options:
83 * - convertFieldsToCamelCase: Loads this file with that option on protobuf.js
84 * set as specified. See
85 * https://github.com/dcodeIO/protobuf.js/wiki/Advanced-options for details
86 * - binaryAsBase64: deserialize bytes values as base64 strings instead of
87 * Buffers. Defaults to false
88 * - longsAsStrings: deserialize long values as strings instead of objects.
89 * Defaults to true
murgatroid99c02910b2016-02-17 12:59:26 -080090 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070091 * @param {string=} format The file format to expect. Must be either 'proto' or
92 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -080093 * @param {Object=} options Options to apply to the loaded file
murgatroid99cca5ffa2015-01-15 14:06:56 -080094 * @return {Object<string, *>} The resulting gRPC object
95 */
murgatroid99c02910b2016-02-17 12:59:26 -080096exports.load = function load(filename, format, options) {
murgatroid9971dbb862015-04-20 11:22:51 -070097 if (!format) {
98 format = 'proto';
99 }
murgatroid99c02910b2016-02-17 12:59:26 -0800100 var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
101 if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
102 ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
murgatroid9971dbb862015-04-20 11:22:51 -0700103 }
murgatroid99c02910b2016-02-17 12:59:26 -0800104 var builder;
105 try {
106 switch(format) {
107 case 'proto':
108 builder = ProtoBuf.loadProtoFile(filename);
109 break;
110 case 'json':
111 builder = ProtoBuf.loadJsonFile(filename);
112 break;
113 default:
114 throw new Error('Unrecognized format "' + format + '"');
115 }
116 } finally {
117 ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
118 }
119 return loadObject(builder.ns, options);
murgatroid999cd90a62015-07-27 11:23:13 -0700120};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800121
122/**
murgatroid999cd90a62015-07-27 11:23:13 -0700123 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800124 */
murgatroid99366e64d2015-07-15 17:01:49 -0700125exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800126
127/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700128 * @see module:src/metadata
129 */
130exports.Metadata = Metadata;
131
132/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800133 * Status name to code number mapping
134 */
135exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700136
murgatroid99cca5ffa2015-01-15 14:06:56 -0800137/**
murgatroid990b094572015-08-14 10:48:45 -0700138 * Propagate flag name to number mapping
139 */
140exports.propagate = grpc.propagate;
141
142/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800143 * Call error name to code number mapping
144 */
145exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800146
147/**
murgatroid994a1474f2015-08-17 14:00:31 -0700148 * Write flag name to code number mapping
149 */
150exports.writeFlags = grpc.writeFlags;
151
152/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800153 * Credentials factories
154 */
murgatroid99153b09d2015-09-25 16:04:03 -0700155exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800156
157/**
158 * ServerCredentials factories
159 */
160exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800161
murgatroid999cd90a62015-07-27 11:23:13 -0700162/**
163 * @see module:src/client.makeClientConstructor
164 */
murgatroid99e023e982015-03-18 17:17:33 -0700165exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700166
167/**
168 * @see module:src/client.getClientChannel
169 */
170exports.getClientChannel = client.getClientChannel;
171
172/**
173 * @see module:src/client.waitForClientReady
174 */
175exports.waitForClientReady = client.waitForClientReady;