blob: 0b768edc6bc716e0180fd47d05c1f2ff85aa831e [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
70 * @return {Object<string, *>} The resulting gRPC object
71 */
72function load(filename) {
73 var builder = ProtoBuf.loadProtoFile(filename);
74
75 return loadObject(builder.ns);
76}
77
78/**
murgatroid998c3ed002015-02-18 15:00:56 -080079 * Get a function that a client can use to update metadata with authentication
murgatroid99bdc8baf2015-02-18 17:06:34 -080080 * information from a Google Auth credential object, which comes from the
murgatroid99042e63c2015-02-24 17:02:09 -080081 * google-auth-library.
murgatroid998c3ed002015-02-18 15:00:56 -080082 * @param {Object} credential The credential object to use
83 * @return {function(Object, callback)} Metadata updater function
84 */
85function getGoogleAuthDelegate(credential) {
86 /**
87 * Update a metadata object with authentication information.
88 * @param {Object} metadata Metadata object
89 * @param {function(Error, Object)} callback
90 */
91 return function updateMetadata(metadata, callback) {
92 metadata = _.clone(metadata);
93 if (metadata.Authorization) {
94 metadata.Authorization = _.clone(metadata.Authorization);
95 } else {
96 metadata.Authorization = [];
97 }
98 credential.getAccessToken(function(err, token) {
99 if (err) {
100 callback(err);
101 return;
102 }
103 metadata.Authorization.push('Bearer ' + token);
104 callback(null, metadata);
105 });
106 };
107}
108
109/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800110 * See docs for loadObject
111 */
112exports.loadObject = loadObject;
113
114/**
115 * See docs for load
116 */
117exports.load = load;
118
119/**
murgatroid99e7879552015-02-12 12:21:15 -0800120 * See docs for server.makeServerConstructor
murgatroid99cca5ffa2015-01-15 14:06:56 -0800121 */
murgatroid99e023e982015-03-18 17:17:33 -0700122exports.buildServer = server.makeProtobufServerConstructor;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800123
124/**
125 * Status name to code number mapping
126 */
127exports.status = grpc.status;
128/**
129 * Call error name to code number mapping
130 */
131exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800132
133/**
134 * Credentials factories
135 */
136exports.Credentials = grpc.Credentials;
137
138/**
139 * ServerCredentials factories
140 */
141exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800142
143exports.getGoogleAuthDelegate = getGoogleAuthDelegate;
murgatroid99e023e982015-03-18 17:17:33 -0700144
145exports.makeGenericClientConstructor = client.makeClientConstructor;
146
147exports.makeGenericServerConstructor = server.makeServerConstructor;