murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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 | |
| 34 | var _ = require('underscore'); |
| 35 | |
| 36 | var ProtoBuf = require('protobufjs'); |
| 37 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame^] | 38 | var client = require('./src/client.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 39 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame^] | 40 | var server = require('./src/server.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 41 | |
| 42 | var grpc = require('bindings')('grpc'); |
| 43 | |
| 44 | /** |
| 45 | * Load a gRPC object from an existing ProtoBuf.Reflect object. |
| 46 | * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load. |
| 47 | * @return {Object<string, *>} The resulting gRPC object |
| 48 | */ |
| 49 | function loadObject(value) { |
| 50 | var result = {}; |
| 51 | if (value.className === 'Namespace') { |
| 52 | _.each(value.children, function(child) { |
| 53 | result[child.name] = loadObject(child); |
| 54 | }); |
| 55 | return result; |
| 56 | } else if (value.className === 'Service') { |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame^] | 57 | return client.makeClientConstructor(value); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 58 | } else if (value.className === 'Message' || value.className === 'Enum') { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 59 | return value.build(); |
| 60 | } else { |
| 61 | return value; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Load a gRPC object from a .proto file. |
| 67 | * @param {string} filename The file to load |
| 68 | * @return {Object<string, *>} The resulting gRPC object |
| 69 | */ |
| 70 | function load(filename) { |
| 71 | var builder = ProtoBuf.loadProtoFile(filename); |
| 72 | |
| 73 | return loadObject(builder.ns); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * See docs for loadObject |
| 78 | */ |
| 79 | exports.loadObject = loadObject; |
| 80 | |
| 81 | /** |
| 82 | * See docs for load |
| 83 | */ |
| 84 | exports.load = load; |
| 85 | |
| 86 | /** |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame^] | 87 | * See docs for server.makeServerConstructor |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 88 | */ |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame^] | 89 | exports.buildServer = server.makeServerConstructor; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Status name to code number mapping |
| 93 | */ |
| 94 | exports.status = grpc.status; |
| 95 | /** |
| 96 | * Call error name to code number mapping |
| 97 | */ |
| 98 | exports.callError = grpc.callError; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Credentials factories |
| 102 | */ |
| 103 | exports.Credentials = grpc.Credentials; |
| 104 | |
| 105 | /** |
| 106 | * ServerCredentials factories |
| 107 | */ |
| 108 | exports.ServerCredentials = grpc.ServerCredentials; |