OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 'use strict'; |
| 35 |
| 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 |
| 44 var _ = require('lodash'); |
| 45 |
| 46 var ProtoBuf = require('protobufjs'); |
| 47 |
| 48 var client = require('./src/client.js'); |
| 49 |
| 50 var server = require('./src/server.js'); |
| 51 |
| 52 var Metadata = require('./src/metadata.js'); |
| 53 |
| 54 var grpc = require('./src/grpc_extension'); |
| 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 * @param {Object=} options Options to apply to the loaded object |
| 60 * @return {Object<string, *>} The resulting gRPC object |
| 61 */ |
| 62 exports.loadObject = function loadObject(value, options) { |
| 63 var result = {}; |
| 64 if (value.className === 'Namespace') { |
| 65 _.each(value.children, function(child) { |
| 66 result[child.name] = loadObject(child, options); |
| 67 }); |
| 68 return result; |
| 69 } else if (value.className === 'Service') { |
| 70 return client.makeProtobufClientConstructor(value, options); |
| 71 } else if (value.className === 'Message' || value.className === 'Enum') { |
| 72 return value.build(); |
| 73 } else { |
| 74 return value; |
| 75 } |
| 76 }; |
| 77 |
| 78 var loadObject = exports.loadObject; |
| 79 |
| 80 /** |
| 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 |
| 90 * @param {string|{root: string, file: string}} filename The file to load |
| 91 * @param {string=} format The file format to expect. Must be either 'proto' or |
| 92 * 'json'. Defaults to 'proto' |
| 93 * @param {Object=} options Options to apply to the loaded file |
| 94 * @return {Object<string, *>} The resulting gRPC object |
| 95 */ |
| 96 exports.load = function load(filename, format, options) { |
| 97 if (!format) { |
| 98 format = 'proto'; |
| 99 } |
| 100 var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase; |
| 101 if(options && options.hasOwnProperty('convertFieldsToCamelCase')) { |
| 102 ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase; |
| 103 } |
| 104 var builder; |
| 105 try { |
| 106 switch(format) { |
| 107 case 'proto': |
| 108 builder = ProtoBuf.loadProtoFile(filename); |
| 109 break; |
| 110 case 'json': |
| 111 builder = ProtoBuf.loadJsonFile(filename); |
| 112 break; |
| 113 default: |
| 114 throw new Error('Unrecognized format "' + format + '"'); |
| 115 } |
| 116 } finally { |
| 117 ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal; |
| 118 } |
| 119 return loadObject(builder.ns, options); |
| 120 }; |
| 121 |
| 122 /** |
| 123 * @see module:src/server.Server |
| 124 */ |
| 125 exports.Server = server.Server; |
| 126 |
| 127 /** |
| 128 * @see module:src/metadata |
| 129 */ |
| 130 exports.Metadata = Metadata; |
| 131 |
| 132 /** |
| 133 * Status name to code number mapping |
| 134 */ |
| 135 exports.status = grpc.status; |
| 136 |
| 137 /** |
| 138 * Propagate flag name to number mapping |
| 139 */ |
| 140 exports.propagate = grpc.propagate; |
| 141 |
| 142 /** |
| 143 * Call error name to code number mapping |
| 144 */ |
| 145 exports.callError = grpc.callError; |
| 146 |
| 147 /** |
| 148 * Write flag name to code number mapping |
| 149 */ |
| 150 exports.writeFlags = grpc.writeFlags; |
| 151 |
| 152 /** |
| 153 * Credentials factories |
| 154 */ |
| 155 exports.credentials = require('./src/credentials.js'); |
| 156 |
| 157 /** |
| 158 * ServerCredentials factories |
| 159 */ |
| 160 exports.ServerCredentials = grpc.ServerCredentials; |
| 161 |
| 162 /** |
| 163 * @see module:src/client.makeClientConstructor |
| 164 */ |
| 165 exports.makeGenericClientConstructor = client.makeClientConstructor; |
| 166 |
| 167 /** |
| 168 * @see module:src/client.getClientChannel |
| 169 */ |
| 170 exports.getClientChannel = client.getClientChannel; |
| 171 |
| 172 /** |
| 173 * @see module:src/client.waitForClientReady |
| 174 */ |
| 175 exports.waitForClientReady = client.waitForClientReady; |
OLD | NEW |