blob: 889b0ac0e92eee151670d8b11495638535cd6f90 [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
murgatroid9955739d52015-06-03 10:58:21 -070036var _ = require('lodash');
murgatroid99cca5ffa2015-01-15 14:06:56 -080037
38var ProtoBuf = require('protobufjs');
39
murgatroid99e7879552015-02-12 12:21:15 -080040var client = require('./src/client.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080041
murgatroid99e7879552015-02-12 12:21:15 -080042var server = require('./src/server.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080043
44var grpc = require('bindings')('grpc');
45
46/**
47 * Load a gRPC object from an existing ProtoBuf.Reflect object.
48 * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
49 * @return {Object<string, *>} The resulting gRPC object
50 */
murgatroid999cd90a62015-07-27 11:23:13 -070051exports.loadObject = function loadObject(value) {
murgatroid99cca5ffa2015-01-15 14:06:56 -080052 var result = {};
53 if (value.className === 'Namespace') {
54 _.each(value.children, function(child) {
55 result[child.name] = loadObject(child);
56 });
57 return result;
58 } else if (value.className === 'Service') {
murgatroid99e023e982015-03-18 17:17:33 -070059 return client.makeProtobufClientConstructor(value);
murgatroid9997d61302015-01-20 18:06:43 -080060 } else if (value.className === 'Message' || value.className === 'Enum') {
murgatroid99cca5ffa2015-01-15 14:06:56 -080061 return value.build();
62 } else {
63 return value;
64 }
murgatroid999cd90a62015-07-27 11:23:13 -070065};
66
67var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -080068
69/**
70 * Load a gRPC object from a .proto file.
71 * @param {string} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070072 * @param {string=} format The file format to expect. Must be either 'proto' or
73 * 'json'. Defaults to 'proto'
murgatroid99cca5ffa2015-01-15 14:06:56 -080074 * @return {Object<string, *>} The resulting gRPC object
75 */
murgatroid999cd90a62015-07-27 11:23:13 -070076exports.load = function load(filename, format) {
murgatroid9971dbb862015-04-20 11:22:51 -070077 if (!format) {
78 format = 'proto';
79 }
80 var builder;
81 switch(format) {
82 case 'proto':
83 builder = ProtoBuf.loadProtoFile(filename);
84 break;
85 case 'json':
86 builder = ProtoBuf.loadJsonFile(filename);
87 break;
88 default:
89 throw new Error('Unrecognized format "' + format + '"');
90 }
murgatroid99cca5ffa2015-01-15 14:06:56 -080091
92 return loadObject(builder.ns);
murgatroid999cd90a62015-07-27 11:23:13 -070093};
murgatroid99cca5ffa2015-01-15 14:06:56 -080094
95/**
murgatroid998c3ed002015-02-18 15:00:56 -080096 * Get a function that a client can use to update metadata with authentication
murgatroid99bdc8baf2015-02-18 17:06:34 -080097 * information from a Google Auth credential object, which comes from the
murgatroid99042e63c2015-02-24 17:02:09 -080098 * google-auth-library.
murgatroid998c3ed002015-02-18 15:00:56 -080099 * @param {Object} credential The credential object to use
100 * @return {function(Object, callback)} Metadata updater function
101 */
murgatroid999cd90a62015-07-27 11:23:13 -0700102exports.getGoogleAuthDelegate = function getGoogleAuthDelegate(credential) {
murgatroid998c3ed002015-02-18 15:00:56 -0800103 /**
104 * Update a metadata object with authentication information.
murgatroid9900943fc2015-05-07 13:09:42 -0700105 * @param {string} authURI The uri to authenticate to
murgatroid998c3ed002015-02-18 15:00:56 -0800106 * @param {Object} metadata Metadata object
107 * @param {function(Error, Object)} callback
108 */
murgatroid9900943fc2015-05-07 13:09:42 -0700109 return function updateMetadata(authURI, metadata, callback) {
murgatroid998c3ed002015-02-18 15:00:56 -0800110 metadata = _.clone(metadata);
111 if (metadata.Authorization) {
112 metadata.Authorization = _.clone(metadata.Authorization);
113 } else {
114 metadata.Authorization = [];
115 }
murgatroid9900943fc2015-05-07 13:09:42 -0700116 credential.getRequestMetadata(authURI, function(err, header) {
murgatroid998c3ed002015-02-18 15:00:56 -0800117 if (err) {
118 callback(err);
119 return;
120 }
murgatroid9900943fc2015-05-07 13:09:42 -0700121 metadata.Authorization.push(header.Authorization);
murgatroid998c3ed002015-02-18 15:00:56 -0800122 callback(null, metadata);
123 });
124 };
murgatroid999cd90a62015-07-27 11:23:13 -0700125};
murgatroid998c3ed002015-02-18 15:00:56 -0800126
127/**
murgatroid999cd90a62015-07-27 11:23:13 -0700128 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800129 */
murgatroid99366e64d2015-07-15 17:01:49 -0700130exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800131
132/**
133 * 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 */
155exports.Credentials = grpc.Credentials;
156
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;