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 | ffac55d | 2017-05-05 13:54:03 -0700 | [diff] [blame^] | 34 | /** |
| 35 | * @module |
| 36 | */ |
| 37 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 38 | 'use strict'; |
| 39 | |
murgatroid99 | 6d6009f | 2015-10-15 09:57:31 -0700 | [diff] [blame] | 40 | var path = require('path'); |
murgatroid99 | 6f60766 | 2016-04-27 16:38:33 -0700 | [diff] [blame] | 41 | var fs = require('fs'); |
murgatroid99 | 6d6009f | 2015-10-15 09:57:31 -0700 | [diff] [blame] | 42 | |
| 43 | var SSL_ROOTS_PATH = path.resolve(__dirname, '..', '..', 'etc', 'roots.pem'); |
| 44 | |
murgatroid99 | 55739d5 | 2015-06-03 10:58:21 -0700 | [diff] [blame] | 45 | var _ = require('lodash'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 46 | |
| 47 | var ProtoBuf = require('protobufjs'); |
| 48 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame] | 49 | var client = require('./src/client.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 50 | |
murgatroid99 | e787955 | 2015-02-12 12:21:15 -0800 | [diff] [blame] | 51 | var server = require('./src/server.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 52 | |
murgatroid99 | 9b9708a | 2016-06-01 11:42:20 -0700 | [diff] [blame] | 53 | var common = require('./src/common.js'); |
| 54 | |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 55 | var Metadata = require('./src/metadata.js'); |
| 56 | |
murgatroid99 | e190f35 | 2016-01-20 13:52:08 -0800 | [diff] [blame] | 57 | var grpc = require('./src/grpc_extension'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 58 | |
murgatroid99 | b5b5f02 | 2017-03-16 16:42:10 -0700 | [diff] [blame] | 59 | var protobuf_js_5_common = require('./src/protobuf_js_5_common'); |
| 60 | var protobuf_js_6_common = require('./src/protobuf_js_6_common'); |
| 61 | |
murgatroid99 | 6f60766 | 2016-04-27 16:38:33 -0700 | [diff] [blame] | 62 | grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii')); |
| 63 | |
murgatroid99 | b306854 | 2017-03-21 11:25:01 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Load a ProtoBuf.js object as a gRPC object. The options object can provide |
| 66 | * the following options: |
| 67 | * - binaryAsBase64: deserialize bytes values as base64 strings instead of |
| 68 | * Buffers. Defaults to false |
| 69 | * - longsAsStrings: deserialize long values as strings instead of objects. |
| 70 | * Defaults to true |
murgatroid99 | ac7f90d | 2017-03-23 18:00:09 -0700 | [diff] [blame] | 71 | * - enumsAsStrings: deserialize enum values as strings instead of numbers. |
| 72 | * Defaults to true |
murgatroid99 | b306854 | 2017-03-21 11:25:01 -0700 | [diff] [blame] | 73 | * - deprecatedArgumentOrder: Use the beta method argument order for client |
| 74 | * methods, with optional arguments after the callback. Defaults to false. |
| 75 | * This option is only a temporary stopgap measure to smooth an API breakage. |
| 76 | * It is deprecated, and new code should not use it. |
| 77 | * - protobufjsVersion: Available values are 5, 6, and 'detect'. 5 and 6 |
| 78 | * respectively indicate that an object from the corresponding version of |
| 79 | * ProtoBuf.js is provided in the value argument. If the option is 'detect', |
| 80 | * gRPC will guess what the version is based on the structure of the value. |
| 81 | * Defaults to 'detect'. |
| 82 | * @param {Object} value The ProtoBuf.js reflection object to load |
| 83 | * @param {Object=} options Options to apply to the loaded file |
| 84 | * @return {Object<string, *>} The resulting gRPC object |
| 85 | */ |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 86 | exports.loadObject = function loadObject(value, options) { |
murgatroid99 | b1a0231 | 2017-03-17 13:45:15 -0700 | [diff] [blame] | 87 | options = _.defaults(options, common.defaultGrpcOptions); |
murgatroid99 | b306854 | 2017-03-21 11:25:01 -0700 | [diff] [blame] | 88 | options = _.defaults(options, {'protobufjsVersion': 'detect'}); |
| 89 | var protobufjsVersion; |
| 90 | if (options.protobufjsVersion === 'detect') { |
| 91 | if (protobuf_js_6_common.isProbablyProtobufJs6(value)) { |
| 92 | protobufjsVersion = 6; |
| 93 | } else if (protobuf_js_5_common.isProbablyProtobufJs5(value)) { |
| 94 | protobufjsVersion = 5; |
| 95 | } else { |
| 96 | var error_message = 'Could not detect ProtoBuf.js version. Please ' + |
| 97 | 'specify the version number with the "protobufjs_version" option'; |
| 98 | throw new Error(error_message); |
| 99 | } |
murgatroid99 | b1a0231 | 2017-03-17 13:45:15 -0700 | [diff] [blame] | 100 | } else { |
murgatroid99 | b306854 | 2017-03-21 11:25:01 -0700 | [diff] [blame] | 101 | protobufjsVersion = options.protobufjsVersion; |
| 102 | } |
| 103 | switch (protobufjsVersion) { |
| 104 | case 6: return protobuf_js_6_common.loadObject(value, options); |
| 105 | case 5: |
murgatroid99 | b1a0231 | 2017-03-17 13:45:15 -0700 | [diff] [blame] | 106 | var deprecation_message = 'Calling grpc.loadObject with an object ' + |
| 107 | 'generated by ProtoBuf.js 5 is deprecated. Please upgrade to ' + |
| 108 | 'ProtoBuf.js 6.'; |
| 109 | common.log(grpc.logVerbosity.INFO, deprecation_message); |
| 110 | return protobuf_js_5_common.loadObject(value, options); |
murgatroid99 | b306854 | 2017-03-21 11:25:01 -0700 | [diff] [blame] | 111 | default: |
| 112 | throw new Error('Unrecognized protobufjsVersion', protobufjsVersion); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 113 | } |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | var loadObject = exports.loadObject; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 117 | |
murgatroid99 | b5b5f02 | 2017-03-16 16:42:10 -0700 | [diff] [blame] | 118 | function applyProtoRoot(filename, root) { |
| 119 | if (_.isString(filename)) { |
| 120 | return filename; |
| 121 | } |
| 122 | filename.root = path.resolve(filename.root) + '/'; |
| 123 | root.resolvePath = function(originPath, importPath, alreadyNormalized) { |
| 124 | return ProtoBuf.util.path.resolve(filename.root, |
| 125 | importPath, |
| 126 | alreadyNormalized); |
| 127 | }; |
| 128 | return filename.file; |
| 129 | } |
| 130 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 131 | /** |
murgatroid99 | 654d254 | 2016-02-17 15:36:28 -0800 | [diff] [blame] | 132 | * Load a gRPC object from a .proto file. The options object can provide the |
| 133 | * following options: |
murgatroid99 | b306854 | 2017-03-21 11:25:01 -0700 | [diff] [blame] | 134 | * - convertFieldsToCamelCase: Load this file with field names in camel case |
| 135 | * instead of their original case |
murgatroid99 | 654d254 | 2016-02-17 15:36:28 -0800 | [diff] [blame] | 136 | * - binaryAsBase64: deserialize bytes values as base64 strings instead of |
| 137 | * Buffers. Defaults to false |
| 138 | * - longsAsStrings: deserialize long values as strings instead of objects. |
| 139 | * Defaults to true |
murgatroid99 | ac7f90d | 2017-03-23 18:00:09 -0700 | [diff] [blame] | 140 | * - enumsAsStrings: deserialize enum values as strings instead of numbers. |
| 141 | * Defaults to true |
murgatroid99 | 27b8d90 | 2016-03-22 14:46:37 -0700 | [diff] [blame] | 142 | * - deprecatedArgumentOrder: Use the beta method argument order for client |
| 143 | * methods, with optional arguments after the callback. Defaults to false. |
| 144 | * This option is only a temporary stopgap measure to smooth an API breakage. |
| 145 | * It is deprecated, and new code should not use it. |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 146 | * @param {string|{root: string, file: string}} filename The file to load |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 147 | * @param {string=} format The file format to expect. Must be either 'proto' or |
| 148 | * 'json'. Defaults to 'proto' |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 149 | * @param {Object=} options Options to apply to the loaded file |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 150 | * @return {Object<string, *>} The resulting gRPC object |
| 151 | */ |
murgatroid99 | c02910b | 2016-02-17 12:59:26 -0800 | [diff] [blame] | 152 | exports.load = function load(filename, format, options) { |
murgatroid99 | b5b5f02 | 2017-03-16 16:42:10 -0700 | [diff] [blame] | 153 | /* Note: format is currently unused, because the API for loading a proto |
| 154 | file or a JSON file is identical in Protobuf.js 6. In the future, there is |
| 155 | still the possibility of adding other formats that would be loaded |
| 156 | differently */ |
| 157 | options = _.defaults(options, common.defaultGrpcOptions); |
murgatroid99 | b306854 | 2017-03-21 11:25:01 -0700 | [diff] [blame] | 158 | options.protobufjs_version = 6; |
murgatroid99 | b5b5f02 | 2017-03-16 16:42:10 -0700 | [diff] [blame] | 159 | var root = new ProtoBuf.Root(); |
| 160 | var parse_options = {keepCase: !options.convertFieldsToCamelCase}; |
| 161 | return loadObject(root.loadSync(applyProtoRoot(filename, root), |
| 162 | parse_options), |
| 163 | options); |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 164 | }; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 165 | |
murgatroid99 | 1d2f289 | 2016-06-02 14:33:22 -0700 | [diff] [blame] | 166 | var log_template = _.template( |
| 167 | '{severity} {timestamp}\t{file}:{line}]\t{message}', |
| 168 | {interpolate: /{([\s\S]+?)}/g}); |
| 169 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 170 | /** |
murgatroid99 | 9b9708a | 2016-06-01 11:42:20 -0700 | [diff] [blame] | 171 | * Sets the logger function for the gRPC module. For debugging purposes, the C |
| 172 | * core will log synchronously directly to stdout unless this function is |
| 173 | * called. Note: the output format here is intended to be informational, and |
| 174 | * is not guaranteed to stay the same in the future. |
| 175 | * Logs will be directed to logger.error. |
| 176 | * @param {Console} logger A Console-like object. |
| 177 | */ |
| 178 | exports.setLogger = function setLogger(logger) { |
| 179 | common.logger = logger; |
murgatroid99 | 1d2f289 | 2016-06-02 14:33:22 -0700 | [diff] [blame] | 180 | grpc.setDefaultLoggerCallback(function(file, line, severity, |
| 181 | message, timestamp) { |
| 182 | logger.error(log_template({ |
| 183 | file: path.basename(file), |
| 184 | line: line, |
| 185 | severity: severity, |
| 186 | message: message, |
| 187 | timestamp: timestamp.toISOString() |
| 188 | })); |
murgatroid99 | 9b9708a | 2016-06-01 11:42:20 -0700 | [diff] [blame] | 189 | }); |
| 190 | }; |
| 191 | |
| 192 | /** |
| 193 | * Sets the logger verbosity for gRPC module logging. The options are members |
| 194 | * of the grpc.logVerbosity map. |
| 195 | * @param {Number} verbosity The minimum severity to log |
| 196 | */ |
| 197 | exports.setLogVerbosity = function setLogVerbosity(verbosity) { |
| 198 | common.logVerbosity = verbosity; |
| 199 | grpc.setLogVerbosity(verbosity); |
| 200 | }; |
| 201 | |
| 202 | /** |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 203 | * @see module:src/server.Server |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 204 | */ |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 205 | exports.Server = server.Server; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 206 | |
| 207 | /** |
murgatroid99 | 84e3cde | 2015-08-20 11:27:05 -0700 | [diff] [blame] | 208 | * @see module:src/metadata |
| 209 | */ |
| 210 | exports.Metadata = Metadata; |
| 211 | |
| 212 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 213 | * Status name to code number mapping |
| 214 | */ |
| 215 | exports.status = grpc.status; |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 216 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 217 | /** |
murgatroid99 | 0b09457 | 2015-08-14 10:48:45 -0700 | [diff] [blame] | 218 | * Propagate flag name to number mapping |
| 219 | */ |
| 220 | exports.propagate = grpc.propagate; |
| 221 | |
| 222 | /** |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 223 | * Call error name to code number mapping |
| 224 | */ |
| 225 | exports.callError = grpc.callError; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 226 | |
| 227 | /** |
murgatroid99 | 4a1474f | 2015-08-17 14:00:31 -0700 | [diff] [blame] | 228 | * Write flag name to code number mapping |
| 229 | */ |
| 230 | exports.writeFlags = grpc.writeFlags; |
| 231 | |
| 232 | /** |
murgatroid99 | 9b9708a | 2016-06-01 11:42:20 -0700 | [diff] [blame] | 233 | * Log verbosity setting name to code number mapping |
| 234 | */ |
| 235 | exports.logVerbosity = grpc.logVerbosity; |
| 236 | |
| 237 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 238 | * Credentials factories |
| 239 | */ |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 240 | exports.credentials = require('./src/credentials.js'); |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * ServerCredentials factories |
| 244 | */ |
| 245 | exports.ServerCredentials = grpc.ServerCredentials; |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 246 | |
murgatroid99 | 9cd90a6 | 2015-07-27 11:23:13 -0700 | [diff] [blame] | 247 | /** |
| 248 | * @see module:src/client.makeClientConstructor |
| 249 | */ |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 250 | exports.makeGenericClientConstructor = client.makeClientConstructor; |
murgatroid99 | 76ba1ff | 2015-08-28 14:57:04 -0700 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * @see module:src/client.getClientChannel |
| 254 | */ |
| 255 | exports.getClientChannel = client.getClientChannel; |
| 256 | |
| 257 | /** |
| 258 | * @see module:src/client.waitForClientReady |
| 259 | */ |
| 260 | exports.waitForClientReady = client.waitForClientReady; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 261 | |
| 262 | exports.closeClient = function closeClient(client_obj) { |
murgatroid99 | ffac55d | 2017-05-05 13:54:03 -0700 | [diff] [blame^] | 263 | client.Client.prototype.close.apply(client_obj); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 264 | }; |
murgatroid99 | ffac55d | 2017-05-05 13:54:03 -0700 | [diff] [blame^] | 265 | |
| 266 | /** |
| 267 | * @see module:src/client.Client |
| 268 | */ |
| 269 | exports.Client = client.Client; |