blob: 875756328d7aca2d2c12c383e9fb3e70e6577256 [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
murgatroid99cca5ffa2015-01-15 14:06:56 -080036var _ = require('underscore');
37
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 */
51function loadObject(value) {
52 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 }
65}
66
67/**
68 * Load a gRPC object from a .proto file.
69 * @param {string} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070070 * @param {string=} format The file format to expect. Must be either 'proto' or
71 * 'json'. Defaults to 'proto'
murgatroid99cca5ffa2015-01-15 14:06:56 -080072 * @return {Object<string, *>} The resulting gRPC object
73 */
murgatroid9971dbb862015-04-20 11:22:51 -070074function load(filename, format) {
75 if (!format) {
76 format = 'proto';
77 }
78 var builder;
79 switch(format) {
80 case 'proto':
81 builder = ProtoBuf.loadProtoFile(filename);
82 break;
83 case 'json':
84 builder = ProtoBuf.loadJsonFile(filename);
85 break;
86 default:
87 throw new Error('Unrecognized format "' + format + '"');
88 }
murgatroid99cca5ffa2015-01-15 14:06:56 -080089
90 return loadObject(builder.ns);
91}
92
93/**
murgatroid998c3ed002015-02-18 15:00:56 -080094 * Get a function that a client can use to update metadata with authentication
murgatroid99bdc8baf2015-02-18 17:06:34 -080095 * information from a Google Auth credential object, which comes from the
murgatroid99042e63c2015-02-24 17:02:09 -080096 * google-auth-library.
murgatroid998c3ed002015-02-18 15:00:56 -080097 * @param {Object} credential The credential object to use
98 * @return {function(Object, callback)} Metadata updater function
99 */
100function getGoogleAuthDelegate(credential) {
101 /**
102 * Update a metadata object with authentication information.
103 * @param {Object} metadata Metadata object
104 * @param {function(Error, Object)} callback
105 */
106 return function updateMetadata(metadata, callback) {
107 metadata = _.clone(metadata);
108 if (metadata.Authorization) {
109 metadata.Authorization = _.clone(metadata.Authorization);
110 } else {
111 metadata.Authorization = [];
112 }
113 credential.getAccessToken(function(err, token) {
114 if (err) {
115 callback(err);
116 return;
117 }
118 metadata.Authorization.push('Bearer ' + token);
119 callback(null, metadata);
120 });
121 };
122}
123
124/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800125 * See docs for loadObject
126 */
127exports.loadObject = loadObject;
128
129/**
130 * See docs for load
131 */
132exports.load = load;
133
134/**
murgatroid99e7879552015-02-12 12:21:15 -0800135 * See docs for server.makeServerConstructor
murgatroid99cca5ffa2015-01-15 14:06:56 -0800136 */
murgatroid99e023e982015-03-18 17:17:33 -0700137exports.buildServer = server.makeProtobufServerConstructor;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800138
139/**
140 * Status name to code number mapping
141 */
142exports.status = grpc.status;
143/**
144 * Call error name to code number mapping
145 */
146exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800147
148/**
149 * Credentials factories
150 */
151exports.Credentials = grpc.Credentials;
152
153/**
154 * ServerCredentials factories
155 */
156exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800157
158exports.getGoogleAuthDelegate = getGoogleAuthDelegate;
murgatroid99e023e982015-03-18 17:17:33 -0700159
160exports.makeGenericClientConstructor = client.makeClientConstructor;
161
162exports.makeGenericServerConstructor = server.makeServerConstructor;