@weave-js/core 0.11.0 → 0.12.1
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 +9 -9
- package/lib/broker/index.js +161 -165
- package/lib/buildRuntime.js +34 -34
- package/lib/cache/adapters/base.js +119 -0
- package/lib/cache/adapters/inMemory.js +154 -0
- package/lib/cache/adapters/inMemoryLru.js +140 -0
- package/lib/cache/adapters/index.js +10 -0
- 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 -22
- 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 +63 -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 +47 -15
- 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 +145 -148
- 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 +14 -5
- 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 +49 -25
- package/lib/utils/index.js +2 -2
- 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 -147
- package/lib/cache/inMemory.js +0 -140
- package/lib/cache/index.js +0 -48
|
@@ -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
|
*/
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* @typedef {import('../../types.js').Registry} Registry
|
|
11
11
|
* @typedef {import('../../types.js').ServiceActionCollection} ServiceActionCollection
|
|
12
12
|
*/
|
|
13
|
-
const { omit } = require('@weave-js/utils')
|
|
14
|
-
const { createEndpointList } = require('./endpointCollection')
|
|
13
|
+
const { omit } = require('@weave-js/utils');
|
|
14
|
+
const { createEndpointList } = require('./endpointCollection');
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Configuration object for weave service broker.
|
|
@@ -29,47 +29,51 @@ exports.createActionCollection = (registry) => {
|
|
|
29
29
|
/**
|
|
30
30
|
* @type {ServiceActionCollection}
|
|
31
31
|
*/
|
|
32
|
-
const actionCollection = Object.create(null)
|
|
33
|
-
const { runtime } = registry
|
|
34
|
-
const actions = new Map()
|
|
32
|
+
const actionCollection = Object.create(null);
|
|
33
|
+
const { runtime } = registry;
|
|
34
|
+
const actions = new Map();
|
|
35
35
|
|
|
36
36
|
actionCollection.add = (node, service, action) => {
|
|
37
|
-
let endPointList = actions.get(action.name)
|
|
37
|
+
let endPointList = actions.get(action.name);
|
|
38
38
|
if (!endPointList) {
|
|
39
|
-
endPointList = createEndpointList(runtime, action.name)
|
|
40
|
-
actions.set(action.name, endPointList)
|
|
39
|
+
endPointList = createEndpointList(runtime, action.name);
|
|
40
|
+
actions.set(action.name, endPointList);
|
|
41
41
|
}
|
|
42
|
-
return endPointList.add(node, service, action)
|
|
43
|
-
}
|
|
42
|
+
return endPointList.add(node, service, action);
|
|
43
|
+
};
|
|
44
44
|
|
|
45
45
|
actionCollection.get = (actionName) => {
|
|
46
|
-
return actions.get(actionName)
|
|
47
|
-
}
|
|
46
|
+
return actions.get(actionName);
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
actionCollection.removeByService = (service) => {
|
|
50
50
|
actions.forEach(list => {
|
|
51
|
-
list.removeByService(service)
|
|
52
|
-
})
|
|
53
|
-
}
|
|
51
|
+
list.removeByService(service);
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
54
|
|
|
55
55
|
actionCollection.remove = (actionName, node) => {
|
|
56
56
|
// todo: switch property order
|
|
57
|
-
const endpoints = actions.get(actionName)
|
|
57
|
+
const endpoints = actions.get(actionName);
|
|
58
58
|
if (endpoints) {
|
|
59
|
-
endpoints.removeByNodeId(node.id)
|
|
59
|
+
endpoints.removeByNodeId(node.id);
|
|
60
60
|
}
|
|
61
|
-
}
|
|
61
|
+
};
|
|
62
62
|
|
|
63
|
-
actionCollection.list = ({
|
|
64
|
-
|
|
63
|
+
actionCollection.list = ({
|
|
64
|
+
onlyLocals = false,
|
|
65
|
+
skipInternals = false,
|
|
66
|
+
withEndpoints = false
|
|
67
|
+
} = {}) => {
|
|
68
|
+
const result = [];
|
|
65
69
|
|
|
66
70
|
actions.forEach(action => {
|
|
67
71
|
if (skipInternals && /^\$node/.test(action.name)) {
|
|
68
|
-
return
|
|
72
|
+
return;
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
if (onlyLocals && !action.hasLocal()) {
|
|
72
|
-
return
|
|
76
|
+
return;
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
// todo: don't create an new object
|
|
@@ -78,17 +82,17 @@ exports.createActionCollection = (registry) => {
|
|
|
78
82
|
hasAvailable: action.hasAvailable(),
|
|
79
83
|
hasLocal: action.hasLocal(),
|
|
80
84
|
count: action.count()
|
|
81
|
-
}
|
|
85
|
+
};
|
|
82
86
|
|
|
83
87
|
if (item.count > 0) {
|
|
84
|
-
const endpoint = action.endpoints[0]
|
|
88
|
+
const endpoint = action.endpoints[0];
|
|
85
89
|
if (endpoint) {
|
|
86
|
-
item.action = omit(endpoint.action, ['handler', 'service'])
|
|
90
|
+
item.action = omit(endpoint.action, ['handler', 'service']);
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
if (item.action == null || item.action.protected) {
|
|
91
|
-
return
|
|
95
|
+
return;
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
if (withEndpoints) {
|
|
@@ -96,14 +100,14 @@ exports.createActionCollection = (registry) => {
|
|
|
96
100
|
return {
|
|
97
101
|
nodeId: endpoint.node.id,
|
|
98
102
|
state: endpoint.state
|
|
99
|
-
}
|
|
100
|
-
})
|
|
103
|
+
};
|
|
104
|
+
});
|
|
101
105
|
}
|
|
102
106
|
|
|
103
|
-
result.push(item)
|
|
104
|
-
})
|
|
105
|
-
return result
|
|
106
|
-
}
|
|
107
|
+
result.push(item);
|
|
108
|
+
});
|
|
109
|
+
return result;
|
|
110
|
+
};
|
|
107
111
|
|
|
108
|
-
return actionCollection
|
|
109
|
-
}
|
|
112
|
+
return actionCollection;
|
|
113
|
+
};
|
|
@@ -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,8 +14,8 @@
|
|
|
14
14
|
* @typedef {import('../../types.js').EndpointCollection} EndpointCollection
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
const { createActionEndpoint } = require('../actionEndpoint')
|
|
18
|
-
const { loadBalancingStrategy } = require('../../constants')
|
|
17
|
+
const { createActionEndpoint } = require('../actionEndpoint');
|
|
18
|
+
const { loadBalancingStrategy } = require('../../constants');
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
*
|
|
@@ -26,133 +26,133 @@ const { loadBalancingStrategy } = require('../../constants')
|
|
|
26
26
|
*/
|
|
27
27
|
exports.createEndpointList = (runtime, name, groupName) => {
|
|
28
28
|
/** @type {EndpointCollection} */
|
|
29
|
-
const endpointList = Object.create(null)
|
|
30
|
-
const options = runtime.options
|
|
29
|
+
const endpointList = Object.create(null);
|
|
30
|
+
const options = runtime.options;
|
|
31
31
|
/** @type {Array} */
|
|
32
|
-
const list = endpointList.endpoints = []
|
|
32
|
+
const list = endpointList.endpoints = [];
|
|
33
33
|
|
|
34
|
-
let counter = 0
|
|
34
|
+
let counter = 0;
|
|
35
35
|
|
|
36
|
-
endpointList.name = name
|
|
37
|
-
endpointList.groupName = groupName
|
|
38
|
-
endpointList.isInternal = name.startsWith('$')
|
|
39
|
-
endpointList.localEndpoints = []
|
|
36
|
+
endpointList.name = name;
|
|
37
|
+
endpointList.groupName = groupName;
|
|
38
|
+
endpointList.isInternal = name.startsWith('$');
|
|
39
|
+
endpointList.localEndpoints = [];
|
|
40
40
|
|
|
41
41
|
const setLocalEndpoints = () => {
|
|
42
|
-
endpointList.localEndpoints = list.filter(endpoint => endpoint.isLocal)
|
|
43
|
-
}
|
|
42
|
+
endpointList.localEndpoints = list.filter(endpoint => endpoint.isLocal);
|
|
43
|
+
};
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Select an Entpoint with the selected Load-Balancing-Strategy
|
|
47
47
|
* @param {*} endpointList List of all available Endpoints
|
|
48
48
|
* @returns {any} Endpoint
|
|
49
|
-
|
|
49
|
+
*/
|
|
50
50
|
const select = (endpointList) => {
|
|
51
|
-
//
|
|
51
|
+
// Round robin
|
|
52
52
|
if (options.registry.loadBalancingStrategy === loadBalancingStrategy.ROUND_ROBIN) {
|
|
53
53
|
if (counter >= endpointList.length) {
|
|
54
|
-
counter = 0
|
|
54
|
+
counter = 0;
|
|
55
55
|
}
|
|
56
|
-
const res = endpointList[counter++]
|
|
57
|
-
return res
|
|
56
|
+
const res = endpointList[counter++];
|
|
57
|
+
return res;
|
|
58
58
|
} else {
|
|
59
|
-
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
|
|
60
|
-
return endpointList[randomInt(0, endpointList.length - 1)]
|
|
59
|
+
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
|
|
60
|
+
return endpointList[randomInt(0, endpointList.length - 1)];
|
|
61
61
|
// todo: implement random load balancer
|
|
62
62
|
}
|
|
63
|
-
}
|
|
63
|
+
};
|
|
64
64
|
|
|
65
65
|
endpointList.add = (node, service, action) => { // todo: addaction
|
|
66
|
-
const foundEndpoint = list.find(endpoint => endpoint.node.id === node.id && endpoint.service.name === service.name)
|
|
66
|
+
const foundEndpoint = list.find(endpoint => endpoint.node.id === node.id && endpoint.service.name === service.name);
|
|
67
67
|
|
|
68
68
|
if (foundEndpoint) {
|
|
69
|
-
foundEndpoint.updateAction(action)
|
|
70
|
-
return false
|
|
69
|
+
foundEndpoint.updateAction(action);
|
|
70
|
+
return false;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
const newEndpoint = createActionEndpoint(runtime, node, service, action)
|
|
73
|
+
const newEndpoint = createActionEndpoint(runtime, node, service, action);
|
|
74
74
|
|
|
75
|
-
list.push(newEndpoint)
|
|
76
|
-
setLocalEndpoints()
|
|
77
|
-
return true
|
|
78
|
-
}
|
|
75
|
+
list.push(newEndpoint);
|
|
76
|
+
setLocalEndpoints();
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
79
79
|
|
|
80
|
-
endpointList.hasAvailable = () => list.find(endpoint => endpoint.isAvailable()) != null
|
|
80
|
+
endpointList.hasAvailable = () => list.find(endpoint => endpoint.isAvailable()) != null;
|
|
81
81
|
|
|
82
|
-
endpointList.hasLocal = () => endpointList.localEndpoints.length > 0
|
|
82
|
+
endpointList.hasLocal = () => endpointList.localEndpoints.length > 0;
|
|
83
83
|
|
|
84
84
|
endpointList.getNextAvailableEndpoint = () => {
|
|
85
85
|
if (list.length === 0) {
|
|
86
|
-
return null
|
|
86
|
+
return null;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
// If there is a local service, get a local endpoint
|
|
90
90
|
if (endpointList.isInternal && endpointList.hasLocal()) {
|
|
91
|
-
return endpointList.getNextLocalEndpoint()
|
|
91
|
+
return endpointList.getNextLocalEndpoint();
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
// If only one endpoint is available return this.
|
|
95
95
|
if (list.length === 1) {
|
|
96
|
-
const endpoint = list[0]
|
|
96
|
+
const endpoint = list[0];
|
|
97
97
|
if (endpoint.isAvailable()) {
|
|
98
|
-
return endpoint
|
|
98
|
+
return endpoint;
|
|
99
99
|
}
|
|
100
|
-
return null
|
|
100
|
+
return null;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
if (options.registry.preferLocalActions && endpointList.hasLocal()) {
|
|
104
|
-
const endpoint = endpointList.getNextLocalEndpoint()
|
|
104
|
+
const endpoint = endpointList.getNextLocalEndpoint();
|
|
105
105
|
if (endpoint && endpoint.isAvailable()) {
|
|
106
|
-
return endpoint
|
|
106
|
+
return endpoint;
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
const availableEndpoints = list.filter(endpoint => endpoint.isAvailable())
|
|
110
|
+
const availableEndpoints = list.filter(endpoint => endpoint.isAvailable());
|
|
111
111
|
if (availableEndpoints.length === 0) {
|
|
112
|
-
return null
|
|
112
|
+
return null;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
return select(availableEndpoints)
|
|
116
|
-
}
|
|
115
|
+
return select(availableEndpoints);
|
|
116
|
+
};
|
|
117
117
|
|
|
118
118
|
endpointList.getNextLocalEndpoint = () => {
|
|
119
119
|
if (endpointList.localEndpoints.length === 0) {
|
|
120
|
-
return null
|
|
120
|
+
return null;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
if (list.length === 1) {
|
|
124
|
-
const endpoint = endpointList.localEndpoints[0]
|
|
124
|
+
const endpoint = endpointList.localEndpoints[0];
|
|
125
125
|
if (endpoint.isAvailable()) {
|
|
126
|
-
return endpoint
|
|
126
|
+
return endpoint;
|
|
127
127
|
}
|
|
128
|
-
return null
|
|
128
|
+
return null;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
const availableEndpoints = endpointList.localEndpoints.filter(endpoint => endpoint.isAvailable())
|
|
131
|
+
const availableEndpoints = endpointList.localEndpoints.filter(endpoint => endpoint.isAvailable());
|
|
132
132
|
if (availableEndpoints.length === 0) {
|
|
133
|
-
return null
|
|
133
|
+
return null;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
return select(availableEndpoints)
|
|
137
|
-
}
|
|
136
|
+
return select(availableEndpoints);
|
|
137
|
+
};
|
|
138
138
|
|
|
139
|
-
endpointList.count = () => list.length
|
|
139
|
+
endpointList.count = () => list.length;
|
|
140
140
|
|
|
141
|
-
endpointList.getByNodeId = (nodeId) => list.find(endpoint => endpoint.node.id === nodeId)
|
|
141
|
+
endpointList.getByNodeId = (nodeId) => list.find(endpoint => endpoint.node.id === nodeId);
|
|
142
142
|
|
|
143
143
|
endpointList.removeByNodeId = (nodeId) => {
|
|
144
|
-
const endpointToRemove = list.find(item => item.node.id === nodeId)
|
|
145
|
-
list.splice(list.indexOf(endpointToRemove), 1)
|
|
146
|
-
setLocalEndpoints()
|
|
147
|
-
}
|
|
144
|
+
const endpointToRemove = list.find(item => item.node.id === nodeId);
|
|
145
|
+
list.splice(list.indexOf(endpointToRemove), 1);
|
|
146
|
+
setLocalEndpoints();
|
|
147
|
+
};
|
|
148
148
|
|
|
149
|
-
endpointList.removeByService = service => {
|
|
150
|
-
const endpointToRemove = list.find(endpoint => endpoint.service === service)
|
|
149
|
+
endpointList.removeByService = (service) => {
|
|
150
|
+
const endpointToRemove = list.find(endpoint => endpoint.service === service);
|
|
151
151
|
if (endpointToRemove) {
|
|
152
|
-
list.splice(list.indexOf(endpointToRemove), 1)
|
|
152
|
+
list.splice(list.indexOf(endpointToRemove), 1);
|
|
153
153
|
}
|
|
154
|
-
setLocalEndpoints()
|
|
155
|
-
}
|
|
154
|
+
setLocalEndpoints();
|
|
155
|
+
};
|
|
156
156
|
|
|
157
|
-
return endpointList
|
|
158
|
-
}
|
|
157
|
+
return endpointList;
|
|
158
|
+
};
|
|
@@ -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
|
+
};
|