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