/
service.send.to.all.ts
46 lines (39 loc) · 1.55 KB
/
service.send.to.all.ts
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
import { logger } from './../../Log/Logger';
/** @module service-middlewares */
import { IServDiscMwParam } from './../service.interfaces';
import XYZ from './../../xyz'
import * as http from 'http'
/**
* Will resolve the service path to an array of nodes that can responde to
* the target path given. It will then send the message to all of the
* node in the array.
* @method _sendToAll
* @param {Array} params [description]
* @param {Function} next used to call the next middleware
* @param {Function} done used to end the middleware stack
* @param {Object} xyz reference to the caller's xyz instance
*/
function _sendToAll (params: IServDiscMwParam, next, done, xyz:XYZ) {
let servicePath = params.opt.servicePath
let userPayload = params.opt.payload
let responseCallback = params.responseCallback
let route = params.opt.route
let redirect = params.opt.redirect
let foreignNodes = xyz.serviceRepository.foreignNodes
let logger = xyz.logger
let Path = xyz.path
let wrapper = xyz.Util.wrapper
// let serviceTokens = servicePath.split('/')
let matches
for (let node in foreignNodes) {
matches = Path.match(servicePath, foreignNodes[node])
if (matches.length) {
for (let match of matches) {
params.targets.push({ service: match, node: node })
}
}
}
logger.verbose(`${wrapper('bold', 'SEND TO ALL')} :: determined node for service ${servicePath} by first find strategy ${params.targets.map((o) => o.node + ':' + o.service)}, `)
if (next) next()
}
module.exports = _sendToAll