blob: d81e780443fc44c5609ba8dc8c6d6427bd83f5c5 [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 */
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.
murgatroid9900943fc2015-05-07 13:09:42 -0700103 * @param {string} authURI The uri to authenticate to
murgatroid998c3ed002015-02-18 15:00:56 -0800104 * @param {Object} metadata Metadata object
105 * @param {function(Error, Object)} callback
106 */
murgatroid9900943fc2015-05-07 13:09:42 -0700107 return function updateMetadata(authURI, metadata, callback) {
murgatroid998c3ed002015-02-18 15:00:56 -0800108 metadata = _.clone(metadata);
109 if (metadata.Authorization) {
110 metadata.Authorization = _.clone(metadata.Authorization);
111 } else {
112 metadata.Authorization = [];
113 }
murgatroid9900943fc2015-05-07 13:09:42 -0700114 credential.getRequestMetadata(authURI, function(err, header) {
murgatroid998c3ed002015-02-18 15:00:56 -0800115 if (err) {
116 callback(err);
117 return;
118 }
murgatroid9900943fc2015-05-07 13:09:42 -0700119 metadata.Authorization.push(header.Authorization);
murgatroid998c3ed002015-02-18 15:00:56 -0800120 callback(null, metadata);
121 });
122 };
123}
124
125/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800126 * See docs for loadObject
127 */
128exports.loadObject = loadObject;
129
130/**
131 * See docs for load
132 */
133exports.load = load;
134
135/**
murgatroid99366e64d2015-07-15 17:01:49 -0700136 * See docs for Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800137 */
murgatroid99366e64d2015-07-15 17:01:49 -0700138exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800139
140/**
141 * Status name to code number mapping
142 */
143exports.status = grpc.status;
144/**
145 * Call error name to code number mapping
146 */
147exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800148
149/**
150 * Credentials factories
151 */
152exports.Credentials = grpc.Credentials;
153
154/**
155 * ServerCredentials factories
156 */
157exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800158
159exports.getGoogleAuthDelegate = getGoogleAuthDelegate;
murgatroid99e023e982015-03-18 17:17:33 -0700160
161exports.makeGenericClientConstructor = client.makeClientConstructor;