@weave-js/core 0.10.0 → 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 +33 -0
- package/lib/broker/context.js +58 -46
- package/lib/broker/{default-options.js → defaultOptions.js} +31 -13
- package/lib/broker/index.js +273 -251
- package/lib/buildRuntime.js +70 -0
- 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 +34 -11
- package/lib/constants.js +11 -8
- package/lib/{error-handler.js → errorHandler.js} +8 -8
- package/lib/errors.js +46 -39
- package/lib/helper/{define-action.js → defineAction.js} +5 -3
- package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +4 -2
- package/lib/helper/{define-service.js → defineService.js} +6 -3
- package/lib/index.js +25 -22
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -14
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +28 -25
- package/lib/logger/levels.js +23 -22
- package/lib/logger/tools.js +17 -13
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +89 -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 +82 -34
- package/lib/middlewares/bulkhead/index.js +34 -33
- package/lib/middlewares/cache/index.js +106 -0
- package/lib/middlewares/circuit-breaker/index.js +85 -60
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +14 -14
- package/lib/middlewares/index.js +3 -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 +18 -18
- package/lib/middlewares/tracing/index.js +50 -37
- package/lib/middlewares/tracing/tags.js +43 -0
- package/lib/middlewares/validator/index.js +32 -32
- package/lib/registry/{action-endpoint.js → actionEndpoint.js} +8 -8
- package/lib/registry/collections/{action-collection.js → actionCollection.js} +39 -35
- package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +64 -65
- package/lib/registry/collections/{event-collection.js → eventCollection.js} +65 -69
- package/lib/registry/collections/{node-collection.js → nodeCollection.js} +45 -45
- package/lib/registry/collections/{service-collection.js → serviceCollection.js} +61 -55
- package/lib/registry/eventEndpoint.js +26 -0
- 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 +147 -163
- 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/{service-item.js → serviceItem.js} +21 -21
- package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +21 -21
- package/lib/runtime/initCache.js +19 -0
- package/lib/runtime/{init-context-factory.js → initContextFactory.js} +22 -22
- package/lib/runtime/{init-eventbus.js → initEventbus.js} +54 -53
- package/lib/runtime/{init-logger.js → initLogger.js} +11 -11
- package/lib/runtime/{init-metrics.js → initMetrics.js} +55 -54
- package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +21 -21
- package/lib/runtime/{init-registry.js → initRegistry.js} +6 -6
- package/lib/runtime/initServiceManager.js +145 -0
- package/lib/runtime/{init-tracing.js → initTracing.js} +25 -24
- package/lib/runtime/{init-transport.js → initTransport.js} +5 -5
- package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +4 -4
- package/lib/runtime/{init-validator.js → initValidator.js} +5 -5
- package/lib/tracing/collectors/base.js +29 -37
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +16 -16
- package/lib/tracing/span.js +34 -32
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +75 -0
- package/lib/transport/adapters/adapteBase.js +107 -0
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +19 -19
- 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 +90 -76
- package/lib/transport/adapters/tcp/index.js +180 -175
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -38
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +58 -58
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +11 -0
- package/lib/transport/createTransport.js +377 -334
- package/lib/transport/errorPayloadFactory.js +15 -0
- package/lib/transport/messageHandlers.js +517 -0
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +95 -71
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +47 -45
- package/lib/utils/{restore-error.js → restoreError.js} +4 -4
- package/package.json +6 -12
- package/CHANGELOG.md +0 -1334
- package/lib/build-runtime.js +0 -70
- package/lib/cache/base.js +0 -142
- package/lib/cache/index.js +0 -48
- package/lib/cache/memory.js +0 -121
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- package/lib/registry/event-endpoint.js +0 -26
- package/lib/runtime/init-cache.js +0 -10
- package/lib/runtime/init-service-manager.js +0 -128
- package/lib/tracing/collectors/console.js +0 -34
- package/lib/tracing/index.js +0 -3
- package/lib/tracing/tracer.js +0 -64
- package/lib/transport/adapters/adapter-base.js +0 -92
- package/lib/transport/message-handlers.js +0 -448
- package/lib/transport/message-types.js +0 -59
- package/lib/transport/message.js +0 -16
|
@@ -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('./
|
|
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,146 +14,145 @@
|
|
|
14
14
|
* @typedef {import('../../types.js').EndpointCollection} EndpointCollection
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
const { createActionEndpoint } = require('../
|
|
18
|
-
const { loadBalancingStrategy } = require('../../constants')
|
|
17
|
+
const { createActionEndpoint } = require('../actionEndpoint');
|
|
18
|
+
const { loadBalancingStrategy } = require('../../constants');
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
*
|
|
22
22
|
* @param {Runtime} runtime Runtime instance
|
|
23
23
|
* @param {string} name name
|
|
24
|
-
* @param {string} groupName Group name
|
|
24
|
+
* @param {string=} groupName Group name
|
|
25
25
|
* @returns {EndpointCollection} EndpointCollection
|
|
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.
|
|
37
|
-
endpointList.
|
|
38
|
-
endpointList.
|
|
39
|
-
endpointList.
|
|
40
|
-
endpointList.localEndpoints = []
|
|
36
|
+
endpointList.name = name;
|
|
37
|
+
endpointList.groupName = groupName;
|
|
38
|
+
endpointList.isInternal = name.startsWith('$');
|
|
39
|
+
endpointList.localEndpoints = [];
|
|
41
40
|
|
|
42
41
|
const setLocalEndpoints = () => {
|
|
43
|
-
endpointList.localEndpoints = list.filter(endpoint => endpoint.isLocal)
|
|
44
|
-
}
|
|
42
|
+
endpointList.localEndpoints = list.filter(endpoint => endpoint.isLocal);
|
|
43
|
+
};
|
|
45
44
|
|
|
46
45
|
/**
|
|
47
46
|
* Select an Entpoint with the selected Load-Balancing-Strategy
|
|
48
47
|
* @param {*} endpointList List of all available Endpoints
|
|
49
48
|
* @returns {any} Endpoint
|
|
50
|
-
|
|
49
|
+
*/
|
|
51
50
|
const select = (endpointList) => {
|
|
52
|
-
//
|
|
51
|
+
// Round robin
|
|
53
52
|
if (options.registry.loadBalancingStrategy === loadBalancingStrategy.ROUND_ROBIN) {
|
|
54
53
|
if (counter >= endpointList.length) {
|
|
55
|
-
counter = 0
|
|
54
|
+
counter = 0;
|
|
56
55
|
}
|
|
57
|
-
const res = endpointList[counter++]
|
|
58
|
-
return res
|
|
56
|
+
const res = endpointList[counter++];
|
|
57
|
+
return res;
|
|
59
58
|
} else {
|
|
60
|
-
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
|
|
61
|
-
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)];
|
|
62
61
|
// todo: implement random load balancer
|
|
63
62
|
}
|
|
64
|
-
}
|
|
63
|
+
};
|
|
65
64
|
|
|
66
65
|
endpointList.add = (node, service, action) => { // todo: addaction
|
|
67
|
-
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);
|
|
68
67
|
|
|
69
68
|
if (foundEndpoint) {
|
|
70
|
-
foundEndpoint.updateAction(action)
|
|
71
|
-
return false
|
|
69
|
+
foundEndpoint.updateAction(action);
|
|
70
|
+
return false;
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
const newEndpoint = createActionEndpoint(runtime, node, service, action)
|
|
73
|
+
const newEndpoint = createActionEndpoint(runtime, node, service, action);
|
|
75
74
|
|
|
76
|
-
list.push(newEndpoint)
|
|
77
|
-
setLocalEndpoints()
|
|
78
|
-
return true
|
|
79
|
-
}
|
|
75
|
+
list.push(newEndpoint);
|
|
76
|
+
setLocalEndpoints();
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
80
79
|
|
|
81
|
-
endpointList.hasAvailable = () => list.find(endpoint => endpoint.isAvailable()) != null
|
|
80
|
+
endpointList.hasAvailable = () => list.find(endpoint => endpoint.isAvailable()) != null;
|
|
82
81
|
|
|
83
|
-
endpointList.hasLocal = () => endpointList.localEndpoints.length > 0
|
|
82
|
+
endpointList.hasLocal = () => endpointList.localEndpoints.length > 0;
|
|
84
83
|
|
|
85
84
|
endpointList.getNextAvailableEndpoint = () => {
|
|
86
85
|
if (list.length === 0) {
|
|
87
|
-
return null
|
|
86
|
+
return null;
|
|
88
87
|
}
|
|
89
88
|
|
|
90
89
|
// If there is a local service, get a local endpoint
|
|
91
90
|
if (endpointList.isInternal && endpointList.hasLocal()) {
|
|
92
|
-
return endpointList.getNextLocalEndpoint()
|
|
91
|
+
return endpointList.getNextLocalEndpoint();
|
|
93
92
|
}
|
|
94
93
|
|
|
95
94
|
// If only one endpoint is available return this.
|
|
96
95
|
if (list.length === 1) {
|
|
97
|
-
const endpoint = list[0]
|
|
96
|
+
const endpoint = list[0];
|
|
98
97
|
if (endpoint.isAvailable()) {
|
|
99
|
-
return endpoint
|
|
98
|
+
return endpoint;
|
|
100
99
|
}
|
|
101
|
-
return null
|
|
100
|
+
return null;
|
|
102
101
|
}
|
|
103
102
|
|
|
104
103
|
if (options.registry.preferLocalActions && endpointList.hasLocal()) {
|
|
105
|
-
const endpoint = endpointList.getNextLocalEndpoint()
|
|
104
|
+
const endpoint = endpointList.getNextLocalEndpoint();
|
|
106
105
|
if (endpoint && endpoint.isAvailable()) {
|
|
107
|
-
return endpoint
|
|
106
|
+
return endpoint;
|
|
108
107
|
}
|
|
109
108
|
}
|
|
110
109
|
|
|
111
|
-
const availableEndpoints = list.filter(endpoint => endpoint.isAvailable())
|
|
110
|
+
const availableEndpoints = list.filter(endpoint => endpoint.isAvailable());
|
|
112
111
|
if (availableEndpoints.length === 0) {
|
|
113
|
-
return null
|
|
112
|
+
return null;
|
|
114
113
|
}
|
|
115
114
|
|
|
116
|
-
return select(availableEndpoints)
|
|
117
|
-
}
|
|
115
|
+
return select(availableEndpoints);
|
|
116
|
+
};
|
|
118
117
|
|
|
119
118
|
endpointList.getNextLocalEndpoint = () => {
|
|
120
119
|
if (endpointList.localEndpoints.length === 0) {
|
|
121
|
-
return null
|
|
120
|
+
return null;
|
|
122
121
|
}
|
|
123
122
|
|
|
124
123
|
if (list.length === 1) {
|
|
125
|
-
const endpoint = endpointList.localEndpoints[0]
|
|
124
|
+
const endpoint = endpointList.localEndpoints[0];
|
|
126
125
|
if (endpoint.isAvailable()) {
|
|
127
|
-
return endpoint
|
|
126
|
+
return endpoint;
|
|
128
127
|
}
|
|
129
|
-
return null
|
|
128
|
+
return null;
|
|
130
129
|
}
|
|
131
130
|
|
|
132
|
-
const availableEndpoints = endpointList.localEndpoints.filter(endpoint => endpoint.isAvailable())
|
|
131
|
+
const availableEndpoints = endpointList.localEndpoints.filter(endpoint => endpoint.isAvailable());
|
|
133
132
|
if (availableEndpoints.length === 0) {
|
|
134
|
-
return null
|
|
133
|
+
return null;
|
|
135
134
|
}
|
|
136
135
|
|
|
137
|
-
return select(availableEndpoints)
|
|
138
|
-
}
|
|
136
|
+
return select(availableEndpoints);
|
|
137
|
+
};
|
|
139
138
|
|
|
140
|
-
endpointList.count = () => list.length
|
|
139
|
+
endpointList.count = () => list.length;
|
|
141
140
|
|
|
142
|
-
endpointList.getByNodeId = (nodeId) => list.find(endpoint => endpoint.node.id === nodeId)
|
|
141
|
+
endpointList.getByNodeId = (nodeId) => list.find(endpoint => endpoint.node.id === nodeId);
|
|
143
142
|
|
|
144
143
|
endpointList.removeByNodeId = (nodeId) => {
|
|
145
|
-
const endpointToRemove = list.find(item => item.node.id === nodeId)
|
|
146
|
-
list.splice(list.indexOf(endpointToRemove), 1)
|
|
147
|
-
setLocalEndpoints()
|
|
148
|
-
}
|
|
144
|
+
const endpointToRemove = list.find(item => item.node.id === nodeId);
|
|
145
|
+
list.splice(list.indexOf(endpointToRemove), 1);
|
|
146
|
+
setLocalEndpoints();
|
|
147
|
+
};
|
|
149
148
|
|
|
150
|
-
endpointList.removeByService = service => {
|
|
151
|
-
const endpointToRemove = list.find(endpoint => endpoint.service === service)
|
|
149
|
+
endpointList.removeByService = (service) => {
|
|
150
|
+
const endpointToRemove = list.find(endpoint => endpoint.service === service);
|
|
152
151
|
if (endpointToRemove) {
|
|
153
|
-
list.splice(list.indexOf(endpointToRemove), 1)
|
|
152
|
+
list.splice(list.indexOf(endpointToRemove), 1);
|
|
154
153
|
}
|
|
155
|
-
setLocalEndpoints()
|
|
156
|
-
}
|
|
154
|
+
setLocalEndpoints();
|
|
155
|
+
};
|
|
157
156
|
|
|
158
|
-
return endpointList
|
|
159
|
-
}
|
|
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('./
|
|
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,84 +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
|
-
events.map(list => {
|
|
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
|
-
events.map(list => {
|
|
63
|
-
list.removeByService(service)
|
|
64
|
-
})
|
|
65
|
-
}
|
|
62
|
+
events.map((list) => {
|
|
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
|
-
.forEach(endpointList => {
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
// .filter(({ endpoint }) => endpoint && endpoint.isAvailable())
|
|
81
|
-
// .map(({ endpoint, endpointList }) => [endpoint, endpointList.groupName])
|
|
82
|
-
return result
|
|
83
|
-
}
|
|
77
|
+
});
|
|
78
|
+
return result;
|
|
79
|
+
};
|
|
84
80
|
|
|
85
81
|
eventCollection.getAllEndpoints = (eventName) => {
|
|
86
82
|
return getAllEventsByEventName(eventName)
|
|
87
83
|
.map(list => list.endpoints)
|
|
88
84
|
.map(endpoints => endpoints.filter(endpoint => endpoint.isAvailable()))
|
|
89
|
-
.reduce((prev, curr) => prev.concat(curr))
|
|
90
|
-
}
|
|
85
|
+
.reduce((prev, curr) => prev.concat(curr));
|
|
86
|
+
};
|
|
91
87
|
|
|
92
88
|
eventCollection.getAllEndpointsUniqueNodes = (eventName, groups) => {
|
|
93
89
|
let endpoints = getAllEventsByEventName(eventName)
|
|
94
90
|
.filter(endpointList => (groups == null || groups.length === 0 || groups.includes(endpointList.groupName)))
|
|
95
|
-
.map(endpointList => endpointList.endpoints)
|
|
91
|
+
.map(endpointList => endpointList.endpoints);
|
|
96
92
|
|
|
97
93
|
if (endpoints.length > 0) {
|
|
98
|
-
endpoints = endpoints.reduce((prev, curr) => prev.concat(curr))
|
|
94
|
+
endpoints = endpoints.reduce((prev, curr) => prev.concat(curr));
|
|
99
95
|
}
|
|
100
96
|
|
|
101
|
-
const unique = {}
|
|
102
|
-
const distinct = []
|
|
97
|
+
const unique = {};
|
|
98
|
+
const distinct = [];
|
|
103
99
|
|
|
104
100
|
for (const i in endpoints) {
|
|
105
101
|
if (typeof (unique[endpoints[i].node.id]) === 'undefined') {
|
|
106
|
-
distinct.push(endpoints[i])
|
|
102
|
+
distinct.push(endpoints[i]);
|
|
107
103
|
}
|
|
108
|
-
unique[endpoints[i].node.id] = endpoints[i].node.id
|
|
104
|
+
unique[endpoints[i].node.id] = endpoints[i].node.id;
|
|
109
105
|
}
|
|
110
106
|
|
|
111
|
-
return distinct
|
|
112
|
-
}
|
|
107
|
+
return distinct;
|
|
108
|
+
};
|
|
113
109
|
|
|
114
110
|
eventCollection.emitLocal = (context) => {
|
|
115
|
-
const promises = []
|
|
116
|
-
const groups = context.eventGroups
|
|
117
|
-
const isBroadcast = broadcastEvents.includes(context.eventType)
|
|
111
|
+
const promises = [];
|
|
112
|
+
const groups = context.eventGroups;
|
|
113
|
+
const isBroadcast = broadcastEvents.includes(context.eventType);
|
|
118
114
|
|
|
119
115
|
getAllEventsByEventName(context.eventName)
|
|
120
116
|
.filter(endpointList => (groups == null || groups.length === 0 || groups.includes(endpointList.groupName)))
|
|
@@ -122,30 +118,30 @@ exports.createEventCollection = (registry) => {
|
|
|
122
118
|
if (isBroadcast) {
|
|
123
119
|
list.endpoints.map(endpoint => {
|
|
124
120
|
if (endpoint.isLocal && endpoint.action.handler) {
|
|
125
|
-
promises.push(endpoint.action.handler(context))
|
|
121
|
+
promises.push(endpoint.action.handler(context));
|
|
126
122
|
}
|
|
127
|
-
})
|
|
123
|
+
});
|
|
128
124
|
} else {
|
|
129
|
-
const endpoint = list.getNextLocalEndpoint()
|
|
125
|
+
const endpoint = list.getNextLocalEndpoint();
|
|
130
126
|
if (endpoint && endpoint.isLocal && endpoint.action.handler) {
|
|
131
|
-
promises.push(endpoint.action.handler(context))
|
|
127
|
+
promises.push(endpoint.action.handler(context));
|
|
132
128
|
}
|
|
133
129
|
}
|
|
134
|
-
})
|
|
130
|
+
});
|
|
135
131
|
|
|
136
|
-
return Promise.all(promises)
|
|
137
|
-
}
|
|
132
|
+
return Promise.all(promises);
|
|
133
|
+
};
|
|
138
134
|
|
|
139
135
|
eventCollection.list = ({ onlyLocals = false, skipInternals = false, withEndpoints = false } = {}) => {
|
|
140
|
-
const result = []
|
|
136
|
+
const result = [];
|
|
141
137
|
|
|
142
138
|
events.forEach(list => {
|
|
143
139
|
if (skipInternals && /^\$node/.test(list.name)) {
|
|
144
|
-
return
|
|
140
|
+
return;
|
|
145
141
|
}
|
|
146
142
|
|
|
147
143
|
if (onlyLocals && !list.hasLocal()) {
|
|
148
|
-
return
|
|
144
|
+
return;
|
|
149
145
|
}
|
|
150
146
|
|
|
151
147
|
const item = {
|
|
@@ -154,12 +150,12 @@ exports.createEventCollection = (registry) => {
|
|
|
154
150
|
groupName: list.groupName,
|
|
155
151
|
hasLocal: list.hasLocal(),
|
|
156
152
|
count: list.count()
|
|
157
|
-
}
|
|
153
|
+
};
|
|
158
154
|
|
|
159
155
|
if (item.count > 0) {
|
|
160
|
-
const endpoint = list.endpoints[0]
|
|
156
|
+
const endpoint = list.endpoints[0];
|
|
161
157
|
if (endpoint) {
|
|
162
|
-
item.event = omit(endpoint.action, ['handler', 'service'])
|
|
158
|
+
item.event = omit(endpoint.action, ['handler', 'service']);
|
|
163
159
|
}
|
|
164
160
|
}
|
|
165
161
|
|
|
@@ -168,13 +164,13 @@ exports.createEventCollection = (registry) => {
|
|
|
168
164
|
return {
|
|
169
165
|
nodeId: endpoint.node.id,
|
|
170
166
|
state: endpoint.state
|
|
171
|
-
}
|
|
172
|
-
})
|
|
167
|
+
};
|
|
168
|
+
});
|
|
173
169
|
}
|
|
174
|
-
result.push(item)
|
|
175
|
-
})
|
|
176
|
-
return result
|
|
177
|
-
}
|
|
170
|
+
result.push(item);
|
|
171
|
+
});
|
|
172
|
+
return result;
|
|
173
|
+
};
|
|
178
174
|
|
|
179
|
-
return eventCollection
|
|
180
|
-
}
|
|
175
|
+
return eventCollection;
|
|
176
|
+
};
|