blob: 51d3fa590cd3130a60d00daf346f00a1de46f08f [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
murgatroid9984e3cde2015-08-20 11:27:05 -070044var Metadata = require('./src/metadata.js');
45
murgatroid99cca5ffa2015-01-15 14:06:56 -080046var grpc = require('bindings')('grpc');
47
48/**
49 * Load a gRPC object from an existing ProtoBuf.Reflect object.
50 * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
51 * @return {Object<string, *>} The resulting gRPC object
52 */
murgatroid999cd90a62015-07-27 11:23:13 -070053exports.loadObject = function loadObject(value) {
murgatroid99cca5ffa2015-01-15 14:06:56 -080054 var result = {};
55 if (value.className === 'Namespace') {
56 _.each(value.children, function(child) {
57 result[child.name] = loadObject(child);
58 });
59 return result;
60 } else if (value.className === 'Service') {
murgatroid99e023e982015-03-18 17:17:33 -070061 return client.makeProtobufClientConstructor(value);
murgatroid9997d61302015-01-20 18:06:43 -080062 } else if (value.className === 'Message' || value.className === 'Enum') {
murgatroid99cca5ffa2015-01-15 14:06:56 -080063 return value.build();
64 } else {
65 return value;
66 }
murgatroid999cd90a62015-07-27 11:23:13 -070067};
68
69var loadObject = exports.loadObject;
murgatroid99cca5ffa2015-01-15 14:06:56 -080070
71/**
72 * Load a gRPC object from a .proto file.
73 * @param {string} filename The file to load
murgatroid9971dbb862015-04-20 11:22:51 -070074 * @param {string=} format The file format to expect. Must be either 'proto' or
75 * 'json'. Defaults to 'proto'
murgatroid99cca5ffa2015-01-15 14:06:56 -080076 * @return {Object<string, *>} The resulting gRPC object
77 */
murgatroid999cd90a62015-07-27 11:23:13 -070078exports.load = function load(filename, format) {
murgatroid9971dbb862015-04-20 11:22:51 -070079 if (!format) {
80 format = 'proto';
81 }
82 var builder;
83 switch(format) {
84 case 'proto':
85 builder = ProtoBuf.loadProtoFile(filename);
86 break;
87 case 'json':
88 builder = ProtoBuf.loadJsonFile(filename);
89 break;
90 default:
91 throw new Error('Unrecognized format "' + format + '"');
92 }
murgatroid99cca5ffa2015-01-15 14:06:56 -080093
94 return loadObject(builder.ns);
murgatroid999cd90a62015-07-27 11:23:13 -070095};
murgatroid99cca5ffa2015-01-15 14:06:56 -080096
97/**
murgatroid998c3ed002015-02-18 15:00:56 -080098 * Get a function that a client can use to update metadata with authentication
murgatroid99bdc8baf2015-02-18 17:06:34 -080099 * information from a Google Auth credential object, which comes from the
murgatroid99042e63c2015-02-24 17:02:09 -0800100 * google-auth-library.
murgatroid998c3ed002015-02-18 15:00:56 -0800101 * @param {Object} credential The credential object to use
102 * @return {function(Object, callback)} Metadata updater function
103 */
murgatroid999cd90a62015-07-27 11:23:13 -0700104exports.getGoogleAuthDelegate = function getGoogleAuthDelegate(credential) {
murgatroid998c3ed002015-02-18 15:00:56 -0800105 /**
106 * Update a metadata object with authentication information.
murgatroid9900943fc2015-05-07 13:09:42 -0700107 * @param {string} authURI The uri to authenticate to
murgatroid998c3ed002015-02-18 15:00:56 -0800108 * @param {Object} metadata Metadata object
109 * @param {function(Error, Object)} callback
110 */
murgatroid9900943fc2015-05-07 13:09:42 -0700111 return function updateMetadata(authURI, metadata, callback) {
murgatroid9900943fc2015-05-07 13:09:42 -0700112 credential.getRequestMetadata(authURI, function(err, header) {
murgatroid998c3ed002015-02-18 15:00:56 -0800113 if (err) {
114 callback(err);
115 return;
116 }
murgatroid9984e3cde2015-08-20 11:27:05 -0700117 metadata.add('authorization', header.Authorization);
murgatroid998c3ed002015-02-18 15:00:56 -0800118 callback(null, metadata);
119 });
120 };
murgatroid999cd90a62015-07-27 11:23:13 -0700121};
murgatroid998c3ed002015-02-18 15:00:56 -0800122
123/**
murgatroid999cd90a62015-07-27 11:23:13 -0700124 * @see module:src/server.Server
murgatroid99cca5ffa2015-01-15 14:06:56 -0800125 */
murgatroid99366e64d2015-07-15 17:01:49 -0700126exports.Server = server.Server;
murgatroid99cca5ffa2015-01-15 14:06:56 -0800127
128/**
murgatroid9984e3cde2015-08-20 11:27:05 -0700129 * @see module:src/metadata
130 */
131exports.Metadata = Metadata;
132
133/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800134 * Status name to code number mapping
135 */
136exports.status = grpc.status;
murgatroid999cd90a62015-07-27 11:23:13 -0700137
murgatroid99cca5ffa2015-01-15 14:06:56 -0800138/**
murgatroid990b094572015-08-14 10:48:45 -0700139 * Propagate flag name to number mapping
140 */
141exports.propagate = grpc.propagate;
142
143/**
murgatroid99cca5ffa2015-01-15 14:06:56 -0800144 * Call error name to code number mapping
145 */
146exports.callError = grpc.callError;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800147
148/**
murgatroid994a1474f2015-08-17 14:00:31 -0700149 * Write flag name to code number mapping
150 */
151exports.writeFlags = grpc.writeFlags;
152
153/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800154 * Credentials factories
155 */
156exports.Credentials = grpc.Credentials;
157
158/**
159 * ServerCredentials factories
160 */
161exports.ServerCredentials = grpc.ServerCredentials;
murgatroid998c3ed002015-02-18 15:00:56 -0800162
murgatroid999cd90a62015-07-27 11:23:13 -0700163/**
164 * @see module:src/client.makeClientConstructor
165 */
murgatroid99e023e982015-03-18 17:17:33 -0700166exports.makeGenericClientConstructor = client.makeClientConstructor;