-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
service.js
167 lines (148 loc) · 5 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";
module.exports = Service;
// extends Namespace
var Namespace = require("./namespace");
((Service.prototype = Object.create(Namespace.prototype)).constructor = Service).className = "Service";
var Method = require("./method"),
util = require("./util"),
rpc = require("./rpc");
/**
* Constructs a new service instance.
* @classdesc Reflected service.
* @extends NamespaceBase
* @constructor
* @param {string} name Service name
* @param {Object.<string,*>} [options] Service options
* @throws {TypeError} If arguments are invalid
*/
function Service(name, options) {
Namespace.call(this, name, options);
/**
* Service methods.
* @type {Object.<string,Method>}
*/
this.methods = {}; // toJSON, marker
/**
* Cached methods as an array.
* @type {Method[]|null}
* @private
*/
this._methodsArray = null;
}
/**
* Service descriptor.
* @interface IService
* @extends INamespace
* @property {Object.<string,IMethod>} methods Method descriptors
*/
/**
* Constructs a service from a service descriptor.
* @param {string} name Service name
* @param {IService} json Service descriptor
* @returns {Service} Created service
* @throws {TypeError} If arguments are invalid
*/
Service.fromJSON = function fromJSON(name, json) {
var service = new Service(name, json.options);
/* istanbul ignore else */
if (json.methods)
for (var names = Object.keys(json.methods), i = 0; i < names.length; ++i)
service.add(Method.fromJSON(names[i], json.methods[names[i]]));
if (json.nested)
service.addJSON(json.nested);
service.comment = json.comment;
return service;
};
/**
* Converts this service to a service descriptor.
* @param {IToJSONOptions} [toJSONOptions] JSON conversion options
* @returns {IService} Service descriptor
*/
Service.prototype.toJSON = function toJSON(toJSONOptions) {
var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);
var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
return util.toObject([
"options" , inherited && inherited.options || undefined,
"methods" , Namespace.arrayToJSON(this.methodsArray, toJSONOptions) || /* istanbul ignore next */ {},
"nested" , inherited && inherited.nested || undefined,
"comment" , keepComments ? this.comment : undefined
]);
};
/**
* Methods of this service as an array for iteration.
* @name Service#methodsArray
* @type {Method[]}
* @readonly
*/
Object.defineProperty(Service.prototype, "methodsArray", {
get: function() {
return this._methodsArray || (this._methodsArray = util.toArray(this.methods));
}
});
function clearCache(service) {
service._methodsArray = null;
return service;
}
/**
* @override
*/
Service.prototype.get = function get(name) {
return this.methods[name]
|| Namespace.prototype.get.call(this, name);
};
/**
* @override
*/
Service.prototype.resolveAll = function resolveAll() {
var methods = this.methodsArray;
for (var i = 0; i < methods.length; ++i)
methods[i].resolve();
return Namespace.prototype.resolve.call(this);
};
/**
* @override
*/
Service.prototype.add = function add(object) {
/* istanbul ignore if */
if (this.get(object.name))
throw Error("duplicate name '" + object.name + "' in " + this);
if (object instanceof Method) {
this.methods[object.name] = object;
object.parent = this;
return clearCache(this);
}
return Namespace.prototype.add.call(this, object);
};
/**
* @override
*/
Service.prototype.remove = function remove(object) {
if (object instanceof Method) {
/* istanbul ignore if */
if (this.methods[object.name] !== object)
throw Error(object + " is not a member of " + this);
delete this.methods[object.name];
object.parent = null;
return clearCache(this);
}
return Namespace.prototype.remove.call(this, object);
};
/**
* Creates a runtime service using the specified rpc implementation.
* @param {RPCImpl} rpcImpl RPC implementation
* @param {boolean} [requestDelimited=false] Whether requests are length-delimited
* @param {boolean} [responseDelimited=false] Whether responses are length-delimited
* @returns {rpc.Service} RPC service. Useful where requests and/or responses are streamed.
*/
Service.prototype.create = function create(rpcImpl, requestDelimited, responseDelimited) {
var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");
rpcService[methodName] = util.codegen(["r","c"], util.isReserved(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
m: method,
q: method.resolvedRequestType.ctor,
s: method.resolvedResponseType.ctor
});
}
return rpcService;
};