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