blob: 6567d56260c82507a89d7ae86de7eeed74d6cf33 [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
murgatroid9927b8d902016-03-22 14:46:37 -070090 * - deprecatedArgumentOrder: Use the beta method argument order for client
91 * methods, with optional arguments after the callback. Defaults to false.
92 * This option is only a temporary stopgap measure to smooth an API breakage.
93 * It is deprecated, and new code should not use it.
murgatroid99c02910b2016-02-17 12:59:26 -080094 * @param {string|{root: string, file: string}} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070095 * @param {string=} format The file format to expect. Must be either 'proto' or
96 * 'json'. Defaults to 'proto'
murgatroid99c02910b2016-02-17 12:59:26 -080097 * @param {Object=} options Options to apply to the loaded file
murgatroid99cca5ffa2015-01-15 14:06:56 -080098 * @return {Object<string, *>} The resulting gRPC object
99 */
murgatroid99c02910b2016-02-17 12:59:26 -0800100exports.load = function load(filename, format, options) {
murgatroid9971dbb862015-04-20 11:22:51 -0700101 if (!format) {
102 format = 'proto';
103 }
murgatroid99c02910b2016-02-17 12:59:26 -0800104 var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
105 if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
106 ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
murgatroid9971dbb862015-04-20 11:22:51 -0700107 }
murgatroid99c02910b2016-02-17 12:59:26 -0800108 var builder;
109 try {
110 switch(format) {
111 case 'proto':
112 builder = ProtoBuf.loadProtoFile(filename);
113 break;
114 case 'json':
115 builder = ProtoBuf.loadJsonFile(filename);
116 break;
117 default:
118 throw new Error('Unrecognized format "' + format + '"');
119 }
120 } finally {
121 ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
122 }
123 return loadObject(builder.ns, options);
murgatroid999cd90a62015-07-27 11:23:13 -0700124};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800125
126/**
murgatroid999cd90a62015-07-27 11:23:13 -0700127 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800128 */
murgatroid99366e64d2015-07-15 17:01:49 -0700129exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800130
131/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700132 * @see module:src/metadata
133 */
134exports.Metadata = Metadata;
135
136/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800137 * Status name to code number mapping
138 */
139exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700140
murgatroid99cca5ffa2015-01-15 14:06:56 -0800141/**
murgatroid990b094572015-08-14 10:48:45 -0700142 * Propagate flag name to number mapping
143 */
144exports.propagate = grpc.propagate;
145
146/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800147 * Call error name to code number mapping
148 */
149exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800150
151/**
murgatroid994a1474f2015-08-17 14:00:31 -0700152 * Write flag name to code number mapping
153 */
154exports.writeFlags = grpc.writeFlags;
155
156/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800157 * Credentials factories
158 */
murgatroid99153b09d2015-09-25 16:04:03 -0700159exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800160
161/**
162 * ServerCredentials factories
163 */
164exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800165
murgatroid999cd90a62015-07-27 11:23:13 -0700166/**
167 * @see module:src/client.makeClientConstructor
168 */
murgatroid99e023e982015-03-18 17:17:33 -0700169exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700170
171/**
172 * @see module:src/client.getClientChannel
173 */
174exports.getClientChannel = client.getClientChannel;
175
176/**
177 * @see module:src/client.waitForClientReady
178 */
179exports.waitForClientReady = client.waitForClientReady;