Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / node_modules / protobufjs / src / ProtoBuf / Builder / Service.js
1 /*?\r
2  // --- Scope ------------------\r
3  // T : Reflect.Service instance\r
4  */\r
5 /**\r
6  * Constructs a new runtime Service.\r
7  * @name ProtoBuf.Builder.Service\r
8  * @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message\r
9  * @class Barebone of all runtime services.\r
10  * @constructor\r
11  * @throws {Error} If the service cannot be created\r
12  */\r
13 var Service = function(rpcImpl) {\r
14     ProtoBuf.Builder.Service.call(this);\r
15 \r
16     /**\r
17      * Service implementation.\r
18      * @name ProtoBuf.Builder.Service#rpcImpl\r
19      * @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))}\r
20      * @expose\r
21      */\r
22     this.rpcImpl = rpcImpl || function(name, msg, callback) {\r
23         // This is what a user has to implement: A function receiving the method name, the actual message to\r
24         // send (type checked) and the callback that's either provided with the error as its first\r
25         // argument or null and the actual response message.\r
26         setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async!\r
27     };\r
28 };\r
29 \r
30 /**\r
31  * @alias ProtoBuf.Builder.Service.prototype\r
32  * @inner\r
33  */\r
34 var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype);\r
35 \r
36 /**\r
37  * Asynchronously performs an RPC call using the given RPC implementation.\r
38  * @name ProtoBuf.Builder.Service.[Method]\r
39  * @function\r
40  * @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation\r
41  * @param {ProtoBuf.Builder.Message} req Request\r
42  * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving\r
43  *  the error if any and the response either as a pre-parsed message or as its raw bytes\r
44  * @abstract\r
45  */\r
46 \r
47 /**\r
48  * Asynchronously performs an RPC call using the instance's RPC implementation.\r
49  * @name ProtoBuf.Builder.Service#[Method]\r
50  * @function\r
51  * @param {ProtoBuf.Builder.Message} req Request\r
52  * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving\r
53  *  the error if any and the response either as a pre-parsed message or as its raw bytes\r
54  * @abstract\r
55  */\r
56 \r
57 var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod);\r
58 for (var i=0; i<rpc.length; i++) {\r
59     (function(method) {\r
60 \r
61         // service#Method(message, callback)\r
62         ServicePrototype[method.name] = function(req, callback) {\r
63             try {\r
64                 try {\r
65                     // If given as a buffer, decode the request. Will throw a TypeError if not a valid buffer.\r
66                     req = method.resolvedRequestType.clazz.decode(ByteBuffer.wrap(req));\r
67                 } catch (err) {\r
68                     if (!(err instanceof TypeError))\r
69                         throw err;\r
70                 }\r
71                 if (req === null || typeof req !== 'object')\r
72                     throw Error("Illegal arguments");\r
73                 if (!(req instanceof method.resolvedRequestType.clazz))\r
74                     req = new method.resolvedRequestType.clazz(req);\r
75                 this.rpcImpl(method.fqn(), req, function(err, res) { // Assumes that this is properly async\r
76                     if (err) {\r
77                         callback(err);\r
78                         return;\r
79                     }\r
80                     // Coalesce to empty string when service response has empty content\r
81                     if (res === null)\r
82                         res = ''\r
83                     try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}\r
84                     if (!res || !(res instanceof method.resolvedResponseType.clazz)) {\r
85                         callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));\r
86                         return;\r
87                     }\r
88                     callback(null, res);\r
89                 });\r
90             } catch (err) {\r
91                 setTimeout(callback.bind(this, err), 0);\r
92             }\r
93         };\r
94 \r
95         // Service.Method(rpcImpl, message, callback)\r
96         Service[method.name] = function(rpcImpl, req, callback) {\r
97             new Service(rpcImpl)[method.name](req, callback);\r
98         };\r
99 \r
100         if (Object.defineProperty)\r
101             Object.defineProperty(Service[method.name], "$options", { "value": method.buildOpt() }),\r
102             Object.defineProperty(ServicePrototype[method.name], "$options", { "value": Service[method.name]["$options"] });\r
103     })(rpc[i]);\r
104 }\r
105 \r
106 // Properties\r
107 \r
108 /**\r
109  * Service options.\r
110  * @name ProtoBuf.Builder.Service.$options\r
111  * @type {Object.<string,*>}\r
112  * @expose\r
113  */\r
114 var $optionsS; // cc needs this\r
115 \r
116 /**\r
117  * Service options.\r
118  * @name ProtoBuf.Builder.Service#$options\r
119  * @type {Object.<string,*>}\r
120  * @expose\r
121  */\r
122 var $options;\r
123 \r
124 /**\r
125  * Reflection type.\r
126  * @name ProtoBuf.Builder.Service.$type\r
127  * @type {!ProtoBuf.Reflect.Service}\r
128  * @expose\r
129  */\r
130 var $typeS;\r
131 \r
132 /**\r
133  * Reflection type.\r
134  * @name ProtoBuf.Builder.Service#$type\r
135  * @type {!ProtoBuf.Reflect.Service}\r
136  * @expose\r
137  */\r
138 var $type;\r
139 \r
140 if (Object.defineProperty)\r
141     Object.defineProperty(Service, "$options", { "value": T.buildOpt() }),\r
142     Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] }),\r
143     Object.defineProperty(Service, "$type", { "value": T }),\r
144     Object.defineProperty(ServicePrototype, "$type", { "value": T });\r