murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | e190f35 | 2016-01-20 13:52:08 -0800 | [diff] [blame] | 54 | var grpc = require('./src/grpc_extension'); |
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. |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 59 | * @param {Object=} options Options to apply to the loaded object |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 60 | * @return {Object<string, *>} The resulting gRPC object |
| 61 | */ |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 62 | exports.loadObject = function loadObject(value, options) { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 63 | var result = {}; |
| 64 | if (value.className === 'Namespace') { |
| 65 | _.each(value.children, function(child) { |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 66 | result[child.name] = loadObject(child, options); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 67 | }); |
| 68 | return result; |
| 69 | } else if (value.className === 'Service') { |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 70 | return client.makeProtobufClientConstructor(value, options); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 71 | } else if (value.className === 'Message' || value.className === 'Enum') { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 72 | return value.build(); |
| 73 | } else { |
| 74 | return value; |
| 75 | } |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | var loadObject = exports.loadObject; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
murgatroid99 | 654d254 | 2016-02-17 15:36:28 -0800 | [diff] [blame] | 81 | * Load a gRPC object from a .proto file. The options object can provide the |
| 82 | * following options: |
| 83 | * - convertFieldsToCamelCase: Loads this file with that option on protobuf.js |
| 84 | * set as specified. See |
| 85 | * https://github.com/dcodeIO/protobuf.js/wiki/Advanced-options for details |
| 86 | * - binaryAsBase64: deserialize bytes values as base64 strings instead of |
| 87 | * Buffers. Defaults to false |
| 88 | * - longsAsStrings: deserialize long values as strings instead of objects. |
| 89 | * Defaults to true |
murgatroid99 | 27b8d90 | 2016-03-22 14:46:37 -0700 | [diff] [blame] | 90 | * - deprecatedArgumentOrder: Use the beta method argument order for client |
| 91 | * methods, with optional arguments after the callback. Defaults to false. |
| 92 | * This option is only a temporary stopgap measure to smooth an API breakage. |
| 93 | * It is deprecated, and new code should not use it. |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 94 | * @param {string|{root: string, file: string}} filename The file to load |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 95 | * @param {string=} format The file format to expect. Must be either 'proto' or |
| 96 | * 'json'. Defaults to 'proto' |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 97 | * @param {Object=} options Options to apply to the loaded file |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 98 | * @return {Object<string, *>} The resulting gRPC object |
| 99 | */ |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 100 | exports.load = function load(filename, format, options) { |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 101 | if (!format) { |
| 102 | format = 'proto'; |
| 103 | } |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 104 | var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase; |
| 105 | if(options && options.hasOwnProperty('convertFieldsToCamelCase')) { |
| 106 | ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase; |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 107 | } |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 108 | var builder; |
| 109 | try { |
| 110 | switch(format) { |
| 111 | case 'proto': |
| 112 | builder = ProtoBuf.loadProtoFile(filename); |
| 113 | break; |
| 114 | case 'json': |
| 115 | builder = ProtoBuf.loadJsonFile(filename); |
| 116 | break; |
| 117 | default: |
| 118 | throw new Error('Unrecognized format "' + format + '"'); |
| 119 | } |
| 120 | } finally { |
| 121 | ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal; |
| 122 | } |
| 123 | return loadObject(builder.ns, options); |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 124 | }; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 127 | * @see module:src/server.Server |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 128 | */ |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 129 | exports.Server = server.Server; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 132 | * @see module:src/metadata |
| 133 | */ |
| 134 | exports.Metadata = Metadata; |
| 135 | |
| 136 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 137 | * Status name to code number mapping |
| 138 | */ |
| 139 | exports.status = grpc.status; |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 140 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 141 | /** |
murgatroid99 | 0b09457 | 2015-08-14 10:48:45 -0700 | [diff] [blame] | 142 | * Propagate flag name to number mapping |
| 143 | */ |
| 144 | exports.propagate = grpc.propagate; |
| 145 | |
| 146 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 147 | * Call error name to code number mapping |
| 148 | */ |
| 149 | exports.callError = grpc.callError; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
murgatroid99 | 4a1474f | 2015-08-17 14:00:31 -0700 | [diff] [blame] | 152 | * Write flag name to code number mapping |
| 153 | */ |
| 154 | exports.writeFlags = grpc.writeFlags; |
| 155 | |
| 156 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 157 | * Credentials factories |
| 158 | */ |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 159 | exports.credentials = require('./src/credentials.js'); |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * ServerCredentials factories |
| 163 | */ |
| 164 | exports.ServerCredentials = grpc.ServerCredentials; |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 165 | |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 166 | /** |
| 167 | * @see module:src/client.makeClientConstructor |
| 168 | */ |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 169 | exports.makeGenericClientConstructor = client.makeClientConstructor; |
murgatroid99 | 76ba1ff | 2015-08-28 14:57:04 -0700 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * @see module:src/client.getClientChannel |
| 173 | */ |
| 174 | exports.getClientChannel = client.getClientChannel; |
| 175 | |
| 176 | /** |
| 177 | * @see module:src/client.waitForClientReady |
| 178 | */ |
| 179 | exports.waitForClientReady = client.waitForClientReady; |