murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 4 | * 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 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 34 | 'use strict'; |
| 35 | |
murgatroid99 | 55739d5 | 2015-06-03 10:58:21 -0700 | [diff] [blame] | 36 | var _ = require('lodash'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 37 | |
| 38 | var ProtoBuf = require('protobufjs'); |
| 39 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame] | 40 | var client = require('./src/client.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 41 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame] | 42 | var server = require('./src/server.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 43 | |
| 44 | var 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 | */ |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 51 | exports.loadObject = function loadObject(value) { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 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') { |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 59 | return client.makeProtobufClientConstructor(value); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 60 | } else if (value.className === 'Message' || value.className === 'Enum') { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 61 | return value.build(); |
| 62 | } else { |
| 63 | return value; |
| 64 | } |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | var loadObject = exports.loadObject; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Load a gRPC object from a .proto file. |
| 71 | * @param {string} filename The file to load |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 72 | * @param {string=} format The file format to expect. Must be either 'proto' or |
| 73 | * 'json'. Defaults to 'proto' |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 74 | * @return {Object<string, *>} The resulting gRPC object |
| 75 | */ |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 76 | exports.load = function load(filename, format) { |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 77 | 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 | } |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 91 | |
| 92 | return loadObject(builder.ns); |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 93 | }; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 94 | |
| 95 | /** |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 96 | * Get a function that a client can use to update metadata with authentication |
murgatroid99 | bdc8baf | 2015-02-18 17:06:34 -0800 | [diff] [blame] | 97 | * information from a Google Auth credential object, which comes from the |
murgatroid99 | 042e63c | 2015-02-24 17:02:09 -0800 | [diff] [blame] | 98 | * google-auth-library. |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 99 | * @param {Object} credential The credential object to use |
| 100 | * @return {function(Object, callback)} Metadata updater function |
| 101 | */ |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 102 | exports.getGoogleAuthDelegate = function getGoogleAuthDelegate(credential) { |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Update a metadata object with authentication information. |
murgatroid99 | 00943fc | 2015-05-07 13:09:42 -0700 | [diff] [blame] | 105 | * @param {string} authURI The uri to authenticate to |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 106 | * @param {Object} metadata Metadata object |
| 107 | * @param {function(Error, Object)} callback |
| 108 | */ |
murgatroid99 | 00943fc | 2015-05-07 13:09:42 -0700 | [diff] [blame] | 109 | return function updateMetadata(authURI, metadata, callback) { |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 110 | metadata = _.clone(metadata); |
| 111 | if (metadata.Authorization) { |
| 112 | metadata.Authorization = _.clone(metadata.Authorization); |
| 113 | } else { |
| 114 | metadata.Authorization = []; |
| 115 | } |
murgatroid99 | 00943fc | 2015-05-07 13:09:42 -0700 | [diff] [blame] | 116 | credential.getRequestMetadata(authURI, function(err, header) { |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 117 | if (err) { |
| 118 | callback(err); |
| 119 | return; |
| 120 | } |
murgatroid99 | 00943fc | 2015-05-07 13:09:42 -0700 | [diff] [blame] | 121 | metadata.Authorization.push(header.Authorization); |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 122 | callback(null, metadata); |
| 123 | }); |
| 124 | }; |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 125 | }; |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 128 | * @see module:src/server.Server |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 129 | */ |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 130 | exports.Server = server.Server; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Status name to code number mapping |
| 134 | */ |
| 135 | exports.status = grpc.status; |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 136 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 137 | /** |
murgatroid99 | 0b09457 | 2015-08-14 10:48:45 -0700 | [diff] [blame] | 138 | * Propagate flag name to number mapping |
| 139 | */ |
| 140 | exports.propagate = grpc.propagate; |
| 141 | |
| 142 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 143 | * Call error name to code number mapping |
| 144 | */ |
| 145 | exports.callError = grpc.callError; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 146 | |
| 147 | /** |
murgatroid99 | 4a1474f | 2015-08-17 14:00:31 -0700 | [diff] [blame^] | 148 | * Write flag name to code number mapping |
| 149 | */ |
| 150 | exports.writeFlags = grpc.writeFlags; |
| 151 | |
| 152 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 153 | * Credentials factories |
| 154 | */ |
| 155 | exports.Credentials = grpc.Credentials; |
| 156 | |
| 157 | /** |
| 158 | * ServerCredentials factories |
| 159 | */ |
| 160 | exports.ServerCredentials = grpc.ServerCredentials; |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 161 | |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 162 | /** |
| 163 | * @see module:src/client.makeClientConstructor |
| 164 | */ |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 165 | exports.makeGenericClientConstructor = client.makeClientConstructor; |