@weave-js/core 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +7 -7
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +8 -9
- package/lib/broker/index.js +160 -164
- package/lib/buildRuntime.js +34 -34
- package/lib/cache/adapters/base.js +50 -74
- package/lib/cache/adapters/inMemory.js +58 -58
- package/lib/cache/adapters/inMemoryLru.js +57 -57
- package/lib/cache/adapters/index.js +3 -39
- package/lib/cache/getCacheKeyByObject.js +31 -0
- package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
- package/lib/cache/lock.js +30 -24
- package/lib/constants.js +5 -5
- package/lib/errorHandler.js +8 -8
- package/lib/errors.js +35 -35
- package/lib/helper/defineAction.js +2 -2
- package/lib/helper/defineBrokerOptions.js +2 -2
- package/lib/helper/defineService.js +2 -2
- package/lib/index.js +21 -21
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -15
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +24 -24
- package/lib/logger/levels.js +21 -21
- package/lib/logger/tools.js +15 -15
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +75 -65
- package/lib/metrics/common.js +47 -47
- package/lib/metrics/constants.js +44 -44
- package/lib/metrics/exporter/base.js +8 -8
- package/lib/metrics/exporter/event.js +20 -20
- package/lib/metrics/exporter/index.js +15 -15
- package/lib/metrics/index.js +2 -2
- package/lib/metrics/types/base.js +20 -20
- package/lib/metrics/types/counter.js +6 -6
- package/lib/metrics/types/gauge.js +24 -24
- package/lib/metrics/types/histogram.js +26 -26
- package/lib/metrics/types/index.js +6 -6
- package/lib/metrics/types/info.js +17 -17
- package/lib/middlewares/action-hooks/index.js +38 -38
- package/lib/middlewares/bulkhead/index.js +34 -34
- package/lib/middlewares/cache/index.js +37 -37
- package/lib/middlewares/circuit-breaker/index.js +62 -62
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +14 -14
- package/lib/middlewares/index.js +2 -2
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +17 -17
- package/lib/middlewares/metrics/index.js +29 -29
- package/lib/middlewares/retry/index.js +15 -15
- package/lib/middlewares/timeout/index.js +16 -16
- package/lib/middlewares/tracing/index.js +37 -37
- package/lib/middlewares/tracing/tags.js +6 -6
- package/lib/middlewares/validator/index.js +31 -31
- package/lib/registry/actionEndpoint.js +8 -8
- package/lib/registry/collections/actionCollection.js +39 -35
- package/lib/registry/collections/endpointCollection.js +63 -63
- package/lib/registry/collections/eventCollection.js +62 -62
- package/lib/registry/collections/nodeCollection.js +45 -45
- package/lib/registry/collections/serviceCollection.js +61 -55
- package/lib/registry/eventEndpoint.js +14 -14
- package/lib/registry/load-balancer/base.js +3 -3
- package/lib/registry/load-balancer/index.js +7 -7
- package/lib/registry/node.js +27 -27
- package/lib/registry/registry.js +139 -136
- package/lib/registry/service/parseAction.js +16 -16
- package/lib/registry/service/parseEvent.js +18 -18
- package/lib/registry/service/reduceMixins.js +10 -10
- package/lib/registry/service/service.js +72 -72
- package/lib/registry/serviceItem.js +21 -21
- package/lib/runtime/initActionInvoker.js +21 -21
- package/lib/runtime/initCache.js +7 -7
- package/lib/runtime/initContextFactory.js +22 -22
- package/lib/runtime/initEventbus.js +49 -49
- package/lib/runtime/initLogger.js +11 -11
- package/lib/runtime/initMetrics.js +54 -54
- package/lib/runtime/initMiddlewareManager.js +21 -21
- package/lib/runtime/initRegistry.js +6 -6
- package/lib/runtime/initServiceManager.js +85 -68
- package/lib/runtime/initTracing.js +23 -23
- package/lib/runtime/initTransport.js +5 -5
- package/lib/runtime/initUuidFactory.js +4 -4
- package/lib/runtime/initValidator.js +5 -5
- package/lib/tracing/collectors/base.js +22 -22
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +15 -15
- package/lib/tracing/span.js +26 -26
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +26 -26
- package/lib/transport/adapters/adapteBase.js +30 -30
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +18 -18
- package/lib/transport/adapters/fromURI.js +13 -13
- package/lib/transport/adapters/getAdapterByName.js +5 -5
- package/lib/transport/adapters/index.js +12 -12
- package/lib/transport/adapters/tcp/discovery/codec.js +4 -4
- package/lib/transport/adapters/tcp/discovery/index.js +62 -62
- package/lib/transport/adapters/tcp/index.js +169 -171
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -39
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +57 -57
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +4 -4
- package/lib/transport/createTransport.js +254 -254
- package/lib/transport/errorPayloadFactory.js +14 -17
- package/lib/transport/messageHandlers.js +200 -200
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +28 -15
- package/lib/utils/index.js +1 -1
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +4 -4
- package/package.json +4 -4
- package/lib/cache/base.js +0 -136
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
|
-
* Author: Kevin Ries (kevin@
|
|
4
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
5
5
|
* -----
|
|
6
6
|
* Copyright 2021 Fachwerk
|
|
7
7
|
*/
|
|
@@ -14,20 +14,20 @@
|
|
|
14
14
|
* @typedef {import('../../types.js').EndpointCollection} EndpointCollection
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
const { omit, match } = require('@weave-js/utils')
|
|
18
|
-
const { createEndpointList } = require('./endpointCollection')
|
|
17
|
+
const { omit, match } = require('@weave-js/utils');
|
|
18
|
+
const { createEndpointList } = require('./endpointCollection');
|
|
19
19
|
|
|
20
|
-
const broadcastEvents = ['broadcast', 'localBroadcast']
|
|
20
|
+
const broadcastEvents = ['broadcast', 'localBroadcast'];
|
|
21
21
|
/**
|
|
22
22
|
* Create event collection
|
|
23
23
|
* @param {Registry} registry Registy reference
|
|
24
24
|
* @return {EventCollection} Event collection
|
|
25
25
|
*/
|
|
26
26
|
exports.createEventCollection = (registry) => {
|
|
27
|
-
const eventCollection = Object.create(null)
|
|
28
|
-
const { runtime } = registry
|
|
29
|
-
const events = [] // todo: refactor to js Map
|
|
30
|
-
const getAllEventsByEventName = (eventName) => events.filter(list => match(eventName, list.name))
|
|
27
|
+
const eventCollection = Object.create(null);
|
|
28
|
+
const { runtime } = registry;
|
|
29
|
+
const events = []; // todo: refactor to js Map
|
|
30
|
+
const getAllEventsByEventName = (eventName) => events.filter(list => match(eventName, list.name));
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Add node to collection
|
|
@@ -37,80 +37,80 @@ exports.createEventCollection = (registry) => {
|
|
|
37
37
|
* @return {EndpointCollection} Endpoint collection
|
|
38
38
|
*/
|
|
39
39
|
eventCollection.add = (node, service, event) => {
|
|
40
|
-
const groupName = event.group || service.name
|
|
41
|
-
let endpointList = eventCollection.get(event.name, groupName)
|
|
40
|
+
const groupName = event.group || service.name;
|
|
41
|
+
let endpointList = eventCollection.get(event.name, groupName);
|
|
42
42
|
if (!endpointList) {
|
|
43
|
-
endpointList = createEndpointList(runtime, event.name, groupName)
|
|
44
|
-
events.push(endpointList)
|
|
43
|
+
endpointList = createEndpointList(runtime, event.name, groupName);
|
|
44
|
+
events.push(endpointList);
|
|
45
45
|
}
|
|
46
|
-
return endpointList.add(node, service, event)
|
|
47
|
-
}
|
|
46
|
+
return endpointList.add(node, service, event);
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
eventCollection.get = (eventName, groupName) => {
|
|
50
|
-
return events.find(endpointList => endpointList.name === eventName && endpointList.groupName === groupName)
|
|
51
|
-
}
|
|
50
|
+
return events.find(endpointList => endpointList.name === eventName && endpointList.groupName === groupName);
|
|
51
|
+
};
|
|
52
52
|
|
|
53
53
|
eventCollection.remove = (node, eventName) => {
|
|
54
54
|
events.map((list) => {
|
|
55
55
|
if (list.name === eventName) {
|
|
56
|
-
list.removeByNodeId(node.id)
|
|
56
|
+
list.removeByNodeId(node.id);
|
|
57
57
|
}
|
|
58
|
-
})
|
|
59
|
-
}
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
60
|
|
|
61
61
|
eventCollection.removeByService = (service) => {
|
|
62
62
|
events.map((list) => {
|
|
63
|
-
list.removeByService(service)
|
|
64
|
-
})
|
|
65
|
-
}
|
|
63
|
+
list.removeByService(service);
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
66
|
|
|
67
67
|
eventCollection.getBalancedEndpoints = (eventName, groups) => {
|
|
68
|
-
const result = []
|
|
68
|
+
const result = [];
|
|
69
69
|
getAllEventsByEventName(eventName)
|
|
70
70
|
.forEach((endpointList) => {
|
|
71
71
|
if (groups == null || groups.length === 0 || groups.indexOf(endpointList.groupName) !== -1) {
|
|
72
|
-
const endpoint = endpointList.getNextAvailableEndpoint()
|
|
72
|
+
const endpoint = endpointList.getNextAvailableEndpoint();
|
|
73
73
|
if (endpoint && endpoint.isAvailable()) {
|
|
74
|
-
result.push([endpoint, endpointList.groupName])
|
|
74
|
+
result.push([endpoint, endpointList.groupName]);
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
})
|
|
78
|
-
return result
|
|
79
|
-
}
|
|
77
|
+
});
|
|
78
|
+
return result;
|
|
79
|
+
};
|
|
80
80
|
|
|
81
81
|
eventCollection.getAllEndpoints = (eventName) => {
|
|
82
82
|
return getAllEventsByEventName(eventName)
|
|
83
83
|
.map(list => list.endpoints)
|
|
84
84
|
.map(endpoints => endpoints.filter(endpoint => endpoint.isAvailable()))
|
|
85
|
-
.reduce((prev, curr) => prev.concat(curr))
|
|
86
|
-
}
|
|
85
|
+
.reduce((prev, curr) => prev.concat(curr));
|
|
86
|
+
};
|
|
87
87
|
|
|
88
88
|
eventCollection.getAllEndpointsUniqueNodes = (eventName, groups) => {
|
|
89
89
|
let endpoints = getAllEventsByEventName(eventName)
|
|
90
90
|
.filter(endpointList => (groups == null || groups.length === 0 || groups.includes(endpointList.groupName)))
|
|
91
|
-
.map(endpointList => endpointList.endpoints)
|
|
91
|
+
.map(endpointList => endpointList.endpoints);
|
|
92
92
|
|
|
93
93
|
if (endpoints.length > 0) {
|
|
94
|
-
endpoints = endpoints.reduce((prev, curr) => prev.concat(curr))
|
|
94
|
+
endpoints = endpoints.reduce((prev, curr) => prev.concat(curr));
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
const unique = {}
|
|
98
|
-
const distinct = []
|
|
97
|
+
const unique = {};
|
|
98
|
+
const distinct = [];
|
|
99
99
|
|
|
100
100
|
for (const i in endpoints) {
|
|
101
101
|
if (typeof (unique[endpoints[i].node.id]) === 'undefined') {
|
|
102
|
-
distinct.push(endpoints[i])
|
|
102
|
+
distinct.push(endpoints[i]);
|
|
103
103
|
}
|
|
104
|
-
unique[endpoints[i].node.id] = endpoints[i].node.id
|
|
104
|
+
unique[endpoints[i].node.id] = endpoints[i].node.id;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
return distinct
|
|
108
|
-
}
|
|
107
|
+
return distinct;
|
|
108
|
+
};
|
|
109
109
|
|
|
110
110
|
eventCollection.emitLocal = (context) => {
|
|
111
|
-
const promises = []
|
|
112
|
-
const groups = context.eventGroups
|
|
113
|
-
const isBroadcast = broadcastEvents.includes(context.eventType)
|
|
111
|
+
const promises = [];
|
|
112
|
+
const groups = context.eventGroups;
|
|
113
|
+
const isBroadcast = broadcastEvents.includes(context.eventType);
|
|
114
114
|
|
|
115
115
|
getAllEventsByEventName(context.eventName)
|
|
116
116
|
.filter(endpointList => (groups == null || groups.length === 0 || groups.includes(endpointList.groupName)))
|
|
@@ -118,30 +118,30 @@ exports.createEventCollection = (registry) => {
|
|
|
118
118
|
if (isBroadcast) {
|
|
119
119
|
list.endpoints.map(endpoint => {
|
|
120
120
|
if (endpoint.isLocal && endpoint.action.handler) {
|
|
121
|
-
promises.push(endpoint.action.handler(context))
|
|
121
|
+
promises.push(endpoint.action.handler(context));
|
|
122
122
|
}
|
|
123
|
-
})
|
|
123
|
+
});
|
|
124
124
|
} else {
|
|
125
|
-
const endpoint = list.getNextLocalEndpoint()
|
|
125
|
+
const endpoint = list.getNextLocalEndpoint();
|
|
126
126
|
if (endpoint && endpoint.isLocal && endpoint.action.handler) {
|
|
127
|
-
promises.push(endpoint.action.handler(context))
|
|
127
|
+
promises.push(endpoint.action.handler(context));
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
-
})
|
|
130
|
+
});
|
|
131
131
|
|
|
132
|
-
return Promise.all(promises)
|
|
133
|
-
}
|
|
132
|
+
return Promise.all(promises);
|
|
133
|
+
};
|
|
134
134
|
|
|
135
135
|
eventCollection.list = ({ onlyLocals = false, skipInternals = false, withEndpoints = false } = {}) => {
|
|
136
|
-
const result = []
|
|
136
|
+
const result = [];
|
|
137
137
|
|
|
138
138
|
events.forEach(list => {
|
|
139
139
|
if (skipInternals && /^\$node/.test(list.name)) {
|
|
140
|
-
return
|
|
140
|
+
return;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
if (onlyLocals && !list.hasLocal()) {
|
|
144
|
-
return
|
|
144
|
+
return;
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
const item = {
|
|
@@ -150,12 +150,12 @@ exports.createEventCollection = (registry) => {
|
|
|
150
150
|
groupName: list.groupName,
|
|
151
151
|
hasLocal: list.hasLocal(),
|
|
152
152
|
count: list.count()
|
|
153
|
-
}
|
|
153
|
+
};
|
|
154
154
|
|
|
155
155
|
if (item.count > 0) {
|
|
156
|
-
const endpoint = list.endpoints[0]
|
|
156
|
+
const endpoint = list.endpoints[0];
|
|
157
157
|
if (endpoint) {
|
|
158
|
-
item.event = omit(endpoint.action, ['handler', 'service'])
|
|
158
|
+
item.event = omit(endpoint.action, ['handler', 'service']);
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
|
|
@@ -164,13 +164,13 @@ exports.createEventCollection = (registry) => {
|
|
|
164
164
|
return {
|
|
165
165
|
nodeId: endpoint.node.id,
|
|
166
166
|
state: endpoint.state
|
|
167
|
-
}
|
|
168
|
-
})
|
|
167
|
+
};
|
|
168
|
+
});
|
|
169
169
|
}
|
|
170
|
-
result.push(item)
|
|
171
|
-
})
|
|
172
|
-
return result
|
|
173
|
-
}
|
|
170
|
+
result.push(item);
|
|
171
|
+
});
|
|
172
|
+
return result;
|
|
173
|
+
};
|
|
174
174
|
|
|
175
|
-
return eventCollection
|
|
176
|
-
}
|
|
175
|
+
return eventCollection;
|
|
176
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
|
-
* Author: Kevin Ries (kevin@
|
|
4
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
5
5
|
* -----
|
|
6
6
|
* Copyright 2021 Fachwerk
|
|
7
7
|
*/
|
|
@@ -11,88 +11,88 @@
|
|
|
11
11
|
* @typedef {import('../../types.js').NodeCollection} NodeCollection
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
const { getIpList, omit } = require('@weave-js/utils')
|
|
15
|
-
const { createNode } = require('../node')
|
|
14
|
+
const { getIpList, omit } = require('@weave-js/utils');
|
|
15
|
+
const { createNode } = require('../node');
|
|
16
16
|
/**
|
|
17
17
|
* Create node collection
|
|
18
18
|
* @param {Registry} registry Registry reference
|
|
19
19
|
* @return {NodeCollection} Node collection
|
|
20
20
|
*/
|
|
21
21
|
exports.createNodeCollection = (registry) => {
|
|
22
|
-
const nodeCollection = Object.create(null)
|
|
23
|
-
const { runtime } = registry
|
|
24
|
-
const nodes = new Map()
|
|
22
|
+
const nodeCollection = Object.create(null);
|
|
23
|
+
const { runtime } = registry;
|
|
24
|
+
const nodes = new Map();
|
|
25
25
|
|
|
26
|
-
nodeCollection.localNode = null
|
|
26
|
+
nodeCollection.localNode = null;
|
|
27
27
|
|
|
28
28
|
nodeCollection.createNode = (nodeId) => {
|
|
29
|
-
return createNode(nodeId)
|
|
30
|
-
}
|
|
29
|
+
return createNode(nodeId);
|
|
30
|
+
};
|
|
31
31
|
|
|
32
32
|
nodeCollection.add = (id, node) => {
|
|
33
|
-
nodes.set(id, node)
|
|
34
|
-
}
|
|
33
|
+
nodes.set(id, node);
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
nodeCollection.has = (id) => {
|
|
37
|
-
return nodes.has(id)
|
|
38
|
-
}
|
|
37
|
+
return nodes.has(id);
|
|
38
|
+
};
|
|
39
39
|
|
|
40
40
|
nodeCollection.get = (id) => {
|
|
41
|
-
return nodes.get(id)
|
|
42
|
-
}
|
|
41
|
+
return nodes.get(id);
|
|
42
|
+
};
|
|
43
43
|
|
|
44
44
|
nodeCollection.remove = (id) => {
|
|
45
|
-
return nodes.delete(id)
|
|
46
|
-
}
|
|
45
|
+
return nodes.delete(id);
|
|
46
|
+
};
|
|
47
47
|
|
|
48
48
|
nodeCollection.list = ({ withServices = true } = {}) => {
|
|
49
|
-
const result = []
|
|
49
|
+
const result = [];
|
|
50
50
|
nodes.forEach(node => {
|
|
51
51
|
if (withServices) {
|
|
52
|
-
result.push(omit(node, ['info']))
|
|
52
|
+
result.push(omit(node, ['info']));
|
|
53
53
|
} else {
|
|
54
|
-
result.push(omit(node, ['info', 'services']))
|
|
54
|
+
result.push(omit(node, ['info', 'services']));
|
|
55
55
|
}
|
|
56
|
-
})
|
|
57
|
-
return result
|
|
58
|
-
}
|
|
56
|
+
});
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
59
|
|
|
60
60
|
nodeCollection.disconnected = (nodeId, isUnexpected) => {
|
|
61
|
-
const node = nodes.get(nodeId)
|
|
61
|
+
const node = nodes.get(nodeId);
|
|
62
62
|
if (node && node.isAvailable) {
|
|
63
|
-
registry.deregisterServiceByNodeId(node.id)
|
|
64
|
-
node.disconnected(isUnexpected)
|
|
65
|
-
runtime.eventBus.broadcastLocal('$node.disconnected', nodeId, isUnexpected)
|
|
66
|
-
registry.log.warn(`Node '${node.id}'${isUnexpected ? ' unexpectedly' : ''} disconnected.`)
|
|
63
|
+
registry.deregisterServiceByNodeId(node.id);
|
|
64
|
+
node.disconnected(isUnexpected);
|
|
65
|
+
runtime.eventBus.broadcastLocal('$node.disconnected', nodeId, isUnexpected);
|
|
66
|
+
registry.log.warn(`Node '${node.id}'${isUnexpected ? ' unexpectedly' : ''} disconnected.`);
|
|
67
67
|
}
|
|
68
|
-
}
|
|
68
|
+
};
|
|
69
69
|
|
|
70
70
|
nodeCollection.toArray = () => {
|
|
71
|
-
const result = []
|
|
72
|
-
nodes.forEach(node => result.push(node))
|
|
73
|
-
return result
|
|
74
|
-
}
|
|
71
|
+
const result = [];
|
|
72
|
+
nodes.forEach(node => result.push(node));
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
75
75
|
|
|
76
76
|
// get Local node information and add it to the collection by
|
|
77
77
|
const addLocalNode = () => {
|
|
78
|
-
const node = createNode(runtime.options.nodeId)
|
|
78
|
+
const node = createNode(runtime.options.nodeId);
|
|
79
79
|
|
|
80
|
-
node.isLocal = true
|
|
81
|
-
node.IPList = getIpList()
|
|
80
|
+
node.isLocal = true;
|
|
81
|
+
node.IPList = getIpList();
|
|
82
82
|
node.client = {
|
|
83
83
|
type: 'nodejs',
|
|
84
84
|
version: runtime.version,
|
|
85
85
|
langVersion: process.version
|
|
86
|
-
}
|
|
86
|
+
};
|
|
87
87
|
|
|
88
|
-
node.sequence = 1
|
|
89
|
-
nodeCollection.add(node.id, node)
|
|
90
|
-
nodeCollection.localNode = node
|
|
88
|
+
node.sequence = 1;
|
|
89
|
+
nodeCollection.add(node.id, node);
|
|
90
|
+
nodeCollection.localNode = node;
|
|
91
91
|
|
|
92
|
-
return node
|
|
93
|
-
}
|
|
92
|
+
return node;
|
|
93
|
+
};
|
|
94
94
|
|
|
95
|
-
addLocalNode()
|
|
95
|
+
addLocalNode();
|
|
96
96
|
|
|
97
|
-
return nodeCollection
|
|
98
|
-
}
|
|
97
|
+
return nodeCollection;
|
|
98
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
|
-
* Author: Kevin Ries (kevin@
|
|
4
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
5
5
|
* -----
|
|
6
6
|
* Copyright 2021 Fachwerk
|
|
7
7
|
*/
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
// const { createEndpointCollection } = require('./endpoint-collection')
|
|
15
|
-
const { omit, remove } = require('@weave-js/utils')
|
|
16
|
-
const { createServiceItem } = require('../serviceItem')
|
|
15
|
+
const { omit, remove } = require('@weave-js/utils');
|
|
16
|
+
const { createServiceItem } = require('../serviceItem');
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Service collection factory
|
|
@@ -22,10 +22,10 @@ const { createServiceItem } = require('../serviceItem')
|
|
|
22
22
|
*/
|
|
23
23
|
exports.createServiceCollection = (registry) => {
|
|
24
24
|
/** @type {ServiceCollection} */
|
|
25
|
-
const serviceCollection = Object.create(null)
|
|
26
|
-
const { runtime } = registry
|
|
27
|
-
const services = serviceCollection.services = []
|
|
28
|
-
const actions = new Map()
|
|
25
|
+
const serviceCollection = Object.create(null);
|
|
26
|
+
const { runtime } = registry;
|
|
27
|
+
const services = serviceCollection.services = [];
|
|
28
|
+
const actions = new Map();
|
|
29
29
|
// const options = broker.options
|
|
30
30
|
|
|
31
31
|
// const findServiceByNode = (nodeId, name) => {
|
|
@@ -33,65 +33,71 @@ exports.createServiceCollection = (registry) => {
|
|
|
33
33
|
// }
|
|
34
34
|
|
|
35
35
|
serviceCollection.add = (node, name, version, settings) => {
|
|
36
|
-
const item = createServiceItem(node, name, version, settings, node.id === runtime.nodeId)
|
|
37
|
-
services.push(item)
|
|
38
|
-
return item
|
|
39
|
-
}
|
|
36
|
+
const item = createServiceItem(node, name, version, settings, node.id === runtime.nodeId);
|
|
37
|
+
services.push(item);
|
|
38
|
+
return item;
|
|
39
|
+
};
|
|
40
40
|
|
|
41
|
-
serviceCollection.get = (nodeId, name, version) => services.find(svc => svc.equals(name, version, nodeId))
|
|
41
|
+
serviceCollection.get = (nodeId, name, version) => services.find(svc => svc.equals(name, version, nodeId));
|
|
42
42
|
|
|
43
43
|
serviceCollection.has = (name, version, nodeId) => {
|
|
44
|
-
return !!services.find(svc => svc.equals(name, version, nodeId))
|
|
45
|
-
}
|
|
44
|
+
return !!services.find(svc => svc.equals(name, version, nodeId));
|
|
45
|
+
};
|
|
46
46
|
|
|
47
47
|
serviceCollection.remove = (nodeId, name, version) => {
|
|
48
|
-
const service = serviceCollection.get(nodeId, name, version)
|
|
48
|
+
const service = serviceCollection.get(nodeId, name, version);
|
|
49
49
|
|
|
50
50
|
if (service) {
|
|
51
|
-
registry.actionCollection.removeByService(service)
|
|
52
|
-
registry.eventCollection.removeByService(service)
|
|
53
|
-
remove(services, svc => svc === service)
|
|
51
|
+
registry.actionCollection.removeByService(service);
|
|
52
|
+
registry.eventCollection.removeByService(service);
|
|
53
|
+
remove(services, svc => svc === service);
|
|
54
54
|
}
|
|
55
|
-
}
|
|
55
|
+
};
|
|
56
56
|
|
|
57
57
|
serviceCollection.removeAllByNodeId = (nodeId) => {
|
|
58
58
|
remove(services, service => {
|
|
59
59
|
if (service.node.id === nodeId) {
|
|
60
|
-
registry.actionCollection.removeByService(service)
|
|
61
|
-
registry.eventCollection.removeByService(service)
|
|
62
|
-
return true
|
|
60
|
+
registry.actionCollection.removeByService(service);
|
|
61
|
+
registry.eventCollection.removeByService(service);
|
|
62
|
+
return true;
|
|
63
63
|
}
|
|
64
|
-
return false
|
|
65
|
-
})
|
|
66
|
-
}
|
|
64
|
+
return false;
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
67
|
|
|
68
|
-
serviceCollection.tryFindActionsByActionName = (actionName) => actions.get(actionName)
|
|
68
|
+
serviceCollection.tryFindActionsByActionName = (actionName) => actions.get(actionName);
|
|
69
69
|
|
|
70
70
|
serviceCollection.getActionsList = () => {
|
|
71
|
-
const result = []
|
|
71
|
+
const result = [];
|
|
72
72
|
actions.forEach((action, key) => {
|
|
73
73
|
const item = {
|
|
74
74
|
name: key,
|
|
75
75
|
count: action.count(),
|
|
76
76
|
hasLocal: action.hasLocal()
|
|
77
|
-
}
|
|
78
|
-
result.push(item)
|
|
79
|
-
})
|
|
80
|
-
return result
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
serviceCollection.list = ({
|
|
84
|
-
|
|
77
|
+
};
|
|
78
|
+
result.push(item);
|
|
79
|
+
});
|
|
80
|
+
return result;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
serviceCollection.list = ({
|
|
84
|
+
localOnly = false,
|
|
85
|
+
withActions = false,
|
|
86
|
+
withEvents = false,
|
|
87
|
+
withNodeService = false,
|
|
88
|
+
withSettings = false
|
|
89
|
+
} = {}) => {
|
|
90
|
+
const result = [];
|
|
85
91
|
services.forEach((service) => {
|
|
86
92
|
if (/^\$node/.test(service.name) && !withNodeService) {
|
|
87
|
-
return
|
|
93
|
+
return;
|
|
88
94
|
}
|
|
89
95
|
if (service.settings && service.settings.$private) {
|
|
90
|
-
return
|
|
96
|
+
return;
|
|
91
97
|
}
|
|
92
98
|
|
|
93
99
|
if (localOnly && !service.isLocal) {
|
|
94
|
-
return
|
|
100
|
+
return;
|
|
95
101
|
}
|
|
96
102
|
|
|
97
103
|
const item = {
|
|
@@ -99,39 +105,39 @@ exports.createServiceCollection = (registry) => {
|
|
|
99
105
|
nodeId: service.node.id,
|
|
100
106
|
version: service.version,
|
|
101
107
|
isAvailable: service.node.isAvailable
|
|
102
|
-
}
|
|
108
|
+
};
|
|
103
109
|
|
|
104
110
|
if (withSettings) {
|
|
105
|
-
item.settings = service.settings
|
|
111
|
+
item.settings = service.settings;
|
|
106
112
|
}
|
|
107
113
|
|
|
108
114
|
if (withActions) {
|
|
109
|
-
item.actions = {}
|
|
115
|
+
item.actions = {};
|
|
110
116
|
Object.values(service.actions)
|
|
111
117
|
.forEach(action => {
|
|
112
|
-
item.actions[action.name] = omit(action, ['handler', 'service'])
|
|
113
|
-
})
|
|
118
|
+
item.actions[action.name] = omit(action, ['handler', 'service']);
|
|
119
|
+
});
|
|
114
120
|
}
|
|
115
121
|
|
|
116
122
|
if (withEvents) {
|
|
117
|
-
item.events = {}
|
|
123
|
+
item.events = {};
|
|
118
124
|
Object.values(service.events)
|
|
119
125
|
.forEach(event => {
|
|
120
|
-
item.events[event.name] = omit(event, ['service', 'handler'])
|
|
121
|
-
})
|
|
126
|
+
item.events[event.name] = omit(event, ['service', 'handler']);
|
|
127
|
+
});
|
|
122
128
|
}
|
|
123
129
|
|
|
124
|
-
result.push(item)
|
|
125
|
-
})
|
|
126
|
-
return result
|
|
127
|
-
}
|
|
130
|
+
result.push(item);
|
|
131
|
+
});
|
|
132
|
+
return result;
|
|
133
|
+
};
|
|
128
134
|
|
|
129
135
|
serviceCollection.findEndpointByNodeId = (actionName, nodeId) => {
|
|
130
|
-
const endpointListItem = serviceCollection.tryFindActionsByActionName(actionName)
|
|
136
|
+
const endpointListItem = serviceCollection.tryFindActionsByActionName(actionName);
|
|
131
137
|
if (endpointListItem) {
|
|
132
|
-
return endpointListItem.endpointByNodeId(nodeId)
|
|
138
|
+
return endpointListItem.endpointByNodeId(nodeId);
|
|
133
139
|
}
|
|
134
|
-
}
|
|
140
|
+
};
|
|
135
141
|
|
|
136
|
-
return serviceCollection
|
|
137
|
-
}
|
|
142
|
+
return serviceCollection;
|
|
143
|
+
};
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
exports.createEventEndpoint = (broker, node, service, event) => {
|
|
8
|
-
const eventEndpoint = Object.create(null)
|
|
8
|
+
const eventEndpoint = Object.create(null);
|
|
9
9
|
|
|
10
|
-
eventEndpoint.node = node
|
|
11
|
-
eventEndpoint.service = service
|
|
12
|
-
eventEndpoint.action = event
|
|
13
|
-
eventEndpoint.isLocal = eventEndpoint.node.id === broker.nodeId
|
|
14
|
-
eventEndpoint.state = true
|
|
15
|
-
eventEndpoint.name = `${node.id}:${event.name}
|
|
10
|
+
eventEndpoint.node = node;
|
|
11
|
+
eventEndpoint.service = service;
|
|
12
|
+
eventEndpoint.action = event;
|
|
13
|
+
eventEndpoint.isLocal = eventEndpoint.node.id === broker.nodeId;
|
|
14
|
+
eventEndpoint.state = true;
|
|
15
|
+
eventEndpoint.name = `${node.id}:${event.name}`;
|
|
16
16
|
|
|
17
17
|
eventEndpoint.updateEvent = (newEvent) => {
|
|
18
|
-
eventEndpoint.action = newEvent
|
|
19
|
-
}
|
|
18
|
+
eventEndpoint.action = newEvent;
|
|
19
|
+
};
|
|
20
20
|
|
|
21
21
|
eventEndpoint.isAvailable = () => {
|
|
22
|
-
return eventEndpoint.state
|
|
23
|
-
}
|
|
22
|
+
return eventEndpoint.state;
|
|
23
|
+
};
|
|
24
24
|
|
|
25
|
-
return eventEndpoint
|
|
26
|
-
}
|
|
25
|
+
return eventEndpoint;
|
|
26
|
+
};
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
const Strategies = {
|
|
2
2
|
Random: require('./random'),
|
|
3
3
|
RoundRobin: require('./round-robin')
|
|
4
|
-
}
|
|
4
|
+
};
|
|
5
5
|
const getByName = name => {
|
|
6
6
|
if (!name) {
|
|
7
|
-
return null
|
|
7
|
+
return null;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
const n = Object.keys(Strategies).find(n => n.toLowerCase() === name.toLowerCase())
|
|
10
|
+
const n = Object.keys(Strategies).find(n => n.toLowerCase() === name.toLowerCase());
|
|
11
11
|
if (n) {
|
|
12
|
-
return this.Cache[n]
|
|
12
|
+
return this.Cache[n];
|
|
13
13
|
}
|
|
14
|
-
}
|
|
14
|
+
};
|
|
15
15
|
|
|
16
16
|
module.exports = {
|
|
17
17
|
resolve (option) {
|
|
18
18
|
if (typeof option === 'string') {
|
|
19
|
-
return getByName(option)
|
|
19
|
+
return getByName(option);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
}
|
|
22
|
+
};
|