blob: 0d1a7fd887bd5724eee705d2fa919c85c3c27f4f [file] [log] [blame]
murgatroid99cca5ffa2015-01-15 14:06:56 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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');
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
murgatroid992af89e42015-10-01 11:54:00 -070054var grpc = require('bindings')('grpc_node');
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.
59 * @return {Object<string, *>} The resulting gRPC object
60 */
murgatroid999cd90a62015-07-27 11:23:13 -070061exports.loadObject = function loadObject(value) {
murgatroid99cca5ffa2015-01-15 14:06:56 -080062 var result = {};
63 if (value.className === 'Namespace') {
64 _.each(value.children, function(child) {
65 result[child.name] = loadObject(child);
66 });
67 return result;
68 } else if (value.className === 'Service') {
murgatroid99e023e982015-03-18 17:17:33 -070069 return client.makeProtobufClientConstructor(value);
murgatroid9997d61302015-01-20 18:06:43 -080070 } else if (value.className === 'Message' || value.className === 'Enum') {
murgatroid99cca5ffa2015-01-15 14:06:56 -080071 return value.build();
72 } else {
73 return value;
74 }
murgatroid999cd90a62015-07-27 11:23:13 -070075};
76
77var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -080078
79/**
80 * Load a gRPC object from a .proto file.
81 * @param {string} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070082 * @param {string=} format The file format to expect. Must be either 'proto' or
83 * 'json'. Defaults to 'proto'
murgatroid99cca5ffa2015-01-15 14:06:56 -080084 * @return {Object<string, *>} The resulting gRPC object
85 */
murgatroid999cd90a62015-07-27 11:23:13 -070086exports.load = function load(filename, format) {
murgatroid9971dbb862015-04-20 11:22:51 -070087 if (!format) {
88 format = 'proto';
89 }
90 var builder;
91 switch(format) {
92 case 'proto':
93 builder = ProtoBuf.loadProtoFile(filename);
94 break;
95 case 'json':
96 builder = ProtoBuf.loadJsonFile(filename);
97 break;
98 default:
99 throw new Error('Unrecognized format "' + format + '"');
100 }
murgatroid99cca5ffa2015-01-15 14:06:56 -0800101 return loadObject(builder.ns);
murgatroid999cd90a62015-07-27 11:23:13 -0700102};
murgatroid99cca5ffa2015-01-15 14:06:56 -0800103
104/**
murgatroid999cd90a62015-07-27 11:23:13 -0700105 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800106 */
murgatroid99366e64d2015-07-15 17:01:49 -0700107exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800108
109/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700110 * @see module:src/metadata
111 */
112exports.Metadata = Metadata;
113
114/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800115 * Status name to code number mapping
116 */
117exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700118
murgatroid99cca5ffa2015-01-15 14:06:56 -0800119/**
murgatroid990b094572015-08-14 10:48:45 -0700120 * Propagate flag name to number mapping
121 */
122exports.propagate = grpc.propagate;
123
124/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800125 * Call error name to code number mapping
126 */
127exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800128
129/**
murgatroid994a1474f2015-08-17 14:00:31 -0700130 * Write flag name to code number mapping
131 */
132exports.writeFlags = grpc.writeFlags;
133
134/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800135 * Credentials factories
136 */
murgatroid99153b09d2015-09-25 16:04:03 -0700137exports.credentials = require('./src/credentials.js');
murgatroid99b6ab1b42015-01-21 10:30:36 -0800138
139/**
140 * ServerCredentials factories
141 */
142exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800143
murgatroid999cd90a62015-07-27 11:23:13 -0700144/**
145 * @see module:src/client.makeClientConstructor
146 */
murgatroid99e023e982015-03-18 17:17:33 -0700147exports.makeGenericClientConstructor = client.makeClientConstructor;
murgatroid9976ba1ff2015-08-28 14:57:04 -0700148
149/**
150 * @see module:src/client.getClientChannel
151 */
152exports.getClientChannel = client.getClientChannel;
153
154/**
155 * @see module:src/client.waitForClientReady
156 */
157exports.waitForClientReady = client.waitForClientReady;