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 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 36 | var _ = require('underscore'); |
| 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 | */ |
| 51 | function 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') { |
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 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Load a gRPC object from a .proto file. |
| 69 | * @param {string} filename The file to load |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame^] | 70 | * @param {string=} format The file format to expect. Must be either 'proto' or |
| 71 | * 'json'. Defaults to 'proto' |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 72 | * @return {Object<string, *>} The resulting gRPC object |
| 73 | */ |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame^] | 74 | function 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 | } |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 89 | |
| 90 | return loadObject(builder.ns); |
| 91 | } |
| 92 | |
| 93 | /** |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 94 | * Get a function that a client can use to update metadata with authentication |
murgatroid99 | bdc8baf | 2015-02-18 17:06:34 -0800 | [diff] [blame] | 95 | * information from a Google Auth credential object, which comes from the |
murgatroid99 | 042e63c | 2015-02-24 17:02:09 -0800 | [diff] [blame] | 96 | * google-auth-library. |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 97 | * @param {Object} credential The credential object to use |
| 98 | * @return {function(Object, callback)} Metadata updater function |
| 99 | */ |
| 100 | function getGoogleAuthDelegate(credential) { |
| 101 | /** |
| 102 | * Update a metadata object with authentication information. |
| 103 | * @param {Object} metadata Metadata object |
| 104 | * @param {function(Error, Object)} callback |
| 105 | */ |
| 106 | return function updateMetadata(metadata, callback) { |
| 107 | metadata = _.clone(metadata); |
| 108 | if (metadata.Authorization) { |
| 109 | metadata.Authorization = _.clone(metadata.Authorization); |
| 110 | } else { |
| 111 | metadata.Authorization = []; |
| 112 | } |
| 113 | credential.getAccessToken(function(err, token) { |
| 114 | if (err) { |
| 115 | callback(err); |
| 116 | return; |
| 117 | } |
| 118 | metadata.Authorization.push('Bearer ' + token); |
| 119 | callback(null, metadata); |
| 120 | }); |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 125 | * See docs for loadObject |
| 126 | */ |
| 127 | exports.loadObject = loadObject; |
| 128 | |
| 129 | /** |
| 130 | * See docs for load |
| 131 | */ |
| 132 | exports.load = load; |
| 133 | |
| 134 | /** |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame] | 135 | * See docs for server.makeServerConstructor |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 136 | */ |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 137 | exports.buildServer = server.makeProtobufServerConstructor; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Status name to code number mapping |
| 141 | */ |
| 142 | exports.status = grpc.status; |
| 143 | /** |
| 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 | /** |
| 149 | * Credentials factories |
| 150 | */ |
| 151 | exports.Credentials = grpc.Credentials; |
| 152 | |
| 153 | /** |
| 154 | * ServerCredentials factories |
| 155 | */ |
| 156 | exports.ServerCredentials = grpc.ServerCredentials; |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 157 | |
| 158 | exports.getGoogleAuthDelegate = getGoogleAuthDelegate; |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 159 | |
| 160 | exports.makeGenericClientConstructor = client.makeClientConstructor; |
| 161 | |
| 162 | exports.makeGenericServerConstructor = server.makeServerConstructor; |