-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.js
309 lines (265 loc) · 6.96 KB
/
client.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'use strict'
const util = require('util')
const events = require('events')
const async = require('async')
const bluebird = require('bluebird')
const utils = require('ht-utils')
const Service = require('./service')
const LocalTransport = require('./transports').Local
let Client = function Client (services) {
if (!(this instanceof Client)) {
return new Client(services)
}
this.services = {}
this.connections = {}
this.schemas = {}
this.middleware = {
before: [],
after: []
}
for (let service in services) {
if (!services.hasOwnProperty(service)) {
continue
}
this.add(service, services[service])
}
}
util.inherits(Client, events.EventEmitter)
Client.prototype.add = function (name, transport) {
if (this.services[name]) {
throw new Error('Tried adding a service with duplicate name')
}
if (transport instanceof Service) {
let service = transport
transport = new LocalTransport()
service.addTransport(transport)
}
this.services[name] = transport
let client = new transport.Client()
this.connections[name] = client
this.emit('added', name)
}
Client.prototype.addSchema = function (service, method, schema) {
if (!this.schemas[service]) {
this.schemas[service] = {}
}
if (typeof schema.validate !== 'function') {
throw new Error('Schema for ' + method + ' does not have a validate function.')
}
this.schemas[service][method] = schema
}
Client.prototype.connect = function (done) {
async.each(Object.keys(this.services), (name, cb) => {
this.connections[name].connect((err) => {
if (err) {
return cb(err)
}
this.emit('connected', name)
cb()
})
}, done)
}
Client.prototype.disconnect = function (done) {
async.each(Object.keys(this.connections), (name, cb) => {
this.connections[name].disconnect((err) => {
if (err) {
return cb(err)
}
delete this.connections[name]
this.emit('disconnected', name)
cb()
})
}, done)
}
Client.prototype.call = function (service, method, data, callback) {
let context = {
service,
method
}
let returnPromise = false
let promise, resolve, reject
// this can be cleaned up
if (!data && !callback) {
data = undefined
returnPromise = true
} else if (data && typeof data !== 'function' && !callback) {
returnPromise = true
} else if (typeof data === 'function') {
callback = data
data = undefined
}
if (returnPromise) {
// :(
promise = new bluebird.Promise(function (_resolve, _reject) {
resolve = _resolve
reject = _reject
})
}
let _beforeMiddleware = this.middleware.before.filter((m) => {
if (m.service && m.service !== context.service) return false
if (m.method && m.method !== context.method) return false
return true
})
function returnResult (err, data) {
if (returnPromise) {
if (err) {
return reject(err)
} else {
return resolve(data)
}
}
return callback(err, data)
}
async.eachSeries(_beforeMiddleware, function (middleware, done) {
middleware.fn.call(context, data, function (err, result) {
if (err) {
return done(err)
}
data = result
done()
})
}, (err) => {
if (err) {
return returnResult(err)
}
let conn = this.connections[context.service]
if (!conn) {
return returnResult({ error: 'unknown-service' })
}
conn.call(context.method, data, (err, data) => {
if (err) {
return returnResult(err)
}
let _afterMiddleware = this.middleware.after.filter((m) => {
if (m.service && m.service !== context.service) return false
if (m.method && m.method !== context.method) return false
return true
})
async.eachSeries(_afterMiddleware, function (middleware, done) {
middleware.fn.call(context, data, function (err, result) {
if (err) {
return done(err)
}
data = result
done()
})
}, (err) => {
if (err) {
return returnResult(err)
}
let finish = (data) => {
this.emit('called', context.service, context.method)
return returnResult(null, data)
}
if (this.schemas[context.service] && this.schemas[context.service][context.method]) {
let schema = this.schemas[context.service][context.method]
schema.validate(data, function (err, data) {
if (err) {
return returnResult({
$htValidationError: true,
error: err.message
})
}
return finish(data)
})
} else {
return finish(data)
}
})
})
})
if (returnPromise) {
return promise
}
}
Client.prototype.before = function (fn, opts = {}) {
let { service, method } = opts
this.middleware.before.push({
service,
method,
fn
})
}
Client.prototype.after = function (fn, opts = {}) {
let { service, method } = opts
this.middleware.after.push({
service,
method,
fn
})
}
Client.prototype.prepare = function (service, method, data) {
return (_data, callback) => {
if (typeof _data === 'function') {
callback = _data
_data = undefined
}
if (typeof _data === 'undefined') {
_data = data
}
return this.call(service, method, _data, callback)
}
}
Client.prototype.chain = function (service, method, data) {
let client = this
if (!client.isChain) {
// return new instance of the client
// so we can set values on it
client = new Client()
for (let k in this) {
if (client.hasOwnProperty(k)) {
client[k] = this[k]
}
}
client.isChain = true
client.chainedMethods = []
}
client.chainedMethods.push({
service,
method,
data
})
return client
}
Client.prototype.end = function (callback) {
if (!this.isChain) {
return callback(new Error('Client.end called on a non-chained HT client.'))
}
let tmp = this.chainedMethods.reduce(function (previous, method) {
let last = previous[previous.length - 1]
if (!last || last.service !== method.service) {
previous.push({
service: method.service,
calls: []
})
}
let call = {
method: method.method
}
if (method.data) {
call.data = method.data
}
previous[previous.length - 1].calls.push(call)
return previous
}, [])
let methods = tmp.map(function (serviceCall) {
let call = {
service: serviceCall.service,
method: '$htMultiCall',
data: serviceCall.calls
}
if (serviceCall.calls.length === 1) {
// If we're only calling 1 method on the
// service, call it directly and don't
// use $htMultiCall
call = {
service: serviceCall.service,
method: serviceCall.calls[0].method,
data: serviceCall.calls[0].data
}
}
return call
})
utils.getLastResult.call(this, methods, callback)
}
export default Client