blob: a8dfa200245d056b72dbced8bf86771395865ba1 [file] [log] [blame]
murgatroid99cca5ffa2015-01-15 14:06:56 -08001/*
2 *
3 * Copyright 2014, 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
34var _ = require('underscore');
35
36var ProtoBuf = require('protobufjs');
37
38var surface_client = require('./surface_client.js');
39
40var surface_server = require('./surface_server.js');
41
42var grpc = require('bindings')('grpc');
43
44/**
45 * Load a gRPC object from an existing ProtoBuf.Reflect object.
46 * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
47 * @return {Object<string, *>} The resulting gRPC object
48 */
49function loadObject(value) {
50 var result = {};
51 if (value.className === 'Namespace') {
52 _.each(value.children, function(child) {
53 result[child.name] = loadObject(child);
54 });
55 return result;
56 } else if (value.className === 'Service') {
57 return surface_client.makeClientConstructor(value);
58 } else if (value.className === 'Service.Message') {
59 return value.build();
60 } else {
61 return value;
62 }
63}
64
65/**
66 * Load a gRPC object from a .proto file.
67 * @param {string} filename The file to load
68 * @return {Object<string, *>} The resulting gRPC object
69 */
70function load(filename) {
71 var builder = ProtoBuf.loadProtoFile(filename);
72
73 return loadObject(builder.ns);
74}
75
76/**
77 * See docs for loadObject
78 */
79exports.loadObject = loadObject;
80
81/**
82 * See docs for load
83 */
84exports.load = load;
85
86/**
87 * See docs for surface_server.makeServerConstructor
88 */
89exports.buildServer = surface_server.makeServerConstructor;
90
91/**
92 * Status name to code number mapping
93 */
94exports.status = grpc.status;
95/**
96 * Call error name to code number mapping
97 */
98exports.callError = grpc.callError;