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 | 6d6009f | 2015-10-15 09:57:31 -0700 | [diff] [blame^] | 36 | var path = require('path'); |
| 37 | |
| 38 | var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem'); |
| 39 | |
| 40 | if (!process.env.GRPC_DEFAULT_SSL_ROOTS_FILE_PATH) { |
| 41 | process.env.GRPC_DEFAULT_SSL_ROOTS_FILE_PATH = SSL_ROOTS_PATH; |
| 42 | } |
| 43 | |
murgatroid99 | 55739d5 | 2015-06-03 10:58:21 -0700 | [diff] [blame] | 44 | var _ = require('lodash'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 45 | |
| 46 | var ProtoBuf = require('protobufjs'); |
| 47 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame] | 48 | var client = require('./src/client.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 49 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame] | 50 | var server = require('./src/server.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 51 | |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 52 | var Metadata = require('./src/metadata.js'); |
| 53 | |
murgatroid99 | 2af89e4 | 2015-10-01 11:54:00 -0700 | [diff] [blame] | 54 | var grpc = require('bindings')('grpc_node'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Load a gRPC object from an existing ProtoBuf.Reflect object. |
| 58 | * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load. |
| 59 | * @return {Object<string, *>} The resulting gRPC object |
| 60 | */ |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 61 | exports.loadObject = function loadObject(value) { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 62 | var result = {}; |
| 63 | if (value.className === 'Namespace') { |
| 64 | _.each(value.children, function(child) { |
| 65 | result[child.name] = loadObject(child); |
| 66 | }); |
| 67 | return result; |
| 68 | } else if (value.className === 'Service') { |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 69 | return client.makeProtobufClientConstructor(value); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 70 | } else if (value.className === 'Message' || value.className === 'Enum') { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 71 | return value.build(); |
| 72 | } else { |
| 73 | return value; |
| 74 | } |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | var loadObject = exports.loadObject; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Load a gRPC object from a .proto file. |
| 81 | * @param {string} filename The file to load |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 82 | * @param {string=} format The file format to expect. Must be either 'proto' or |
| 83 | * 'json'. Defaults to 'proto' |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 84 | * @return {Object<string, *>} The resulting gRPC object |
| 85 | */ |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 86 | exports.load = function load(filename, format) { |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 87 | if (!format) { |
| 88 | format = 'proto'; |
| 89 | } |
| 90 | var builder; |
| 91 | switch(format) { |
| 92 | case 'proto': |
| 93 | builder = ProtoBuf.loadProtoFile(filename); |
| 94 | break; |
| 95 | case 'json': |
| 96 | builder = ProtoBuf.loadJsonFile(filename); |
| 97 | break; |
| 98 | default: |
| 99 | throw new Error('Unrecognized format "' + format + '"'); |
| 100 | } |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 101 | return loadObject(builder.ns); |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 102 | }; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 105 | * @see module:src/server.Server |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 106 | */ |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 107 | exports.Server = server.Server; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 110 | * @see module:src/metadata |
| 111 | */ |
| 112 | exports.Metadata = Metadata; |
| 113 | |
| 114 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 115 | * Status name to code number mapping |
| 116 | */ |
| 117 | exports.status = grpc.status; |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 118 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 119 | /** |
murgatroid99 | 0b09457 | 2015-08-14 10:48:45 -0700 | [diff] [blame] | 120 | * Propagate flag name to number mapping |
| 121 | */ |
| 122 | exports.propagate = grpc.propagate; |
| 123 | |
| 124 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 125 | * Call error name to code number mapping |
| 126 | */ |
| 127 | exports.callError = grpc.callError; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 128 | |
| 129 | /** |
murgatroid99 | 4a1474f | 2015-08-17 14:00:31 -0700 | [diff] [blame] | 130 | * Write flag name to code number mapping |
| 131 | */ |
| 132 | exports.writeFlags = grpc.writeFlags; |
| 133 | |
| 134 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 135 | * Credentials factories |
| 136 | */ |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 137 | exports.credentials = require('./src/credentials.js'); |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * ServerCredentials factories |
| 141 | */ |
| 142 | exports.ServerCredentials = grpc.ServerCredentials; |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 143 | |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 144 | /** |
| 145 | * @see module:src/client.makeClientConstructor |
| 146 | */ |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 147 | exports.makeGenericClientConstructor = client.makeClientConstructor; |
murgatroid99 | 76ba1ff | 2015-08-28 14:57:04 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * @see module:src/client.getClientChannel |
| 151 | */ |
| 152 | exports.getClientChannel = client.getClientChannel; |
| 153 | |
| 154 | /** |
| 155 | * @see module:src/client.waitForClientReady |
| 156 | */ |
| 157 | exports.waitForClientReady = client.waitForClientReady; |