@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,35 +1,67 @@
|
|
|
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
|
-
const { WeaveError } = require('../../errors')
|
|
7
|
+
const { WeaveError } = require('../../errors');
|
|
8
8
|
|
|
9
9
|
module.exports = (runtime) => {
|
|
10
10
|
const wrapErrorHandlerMiddleware = function (handler) {
|
|
11
11
|
return function errorHandlerMiddleware (context, serviceInjections) {
|
|
12
12
|
return handler(context, serviceInjections)
|
|
13
|
-
.catch(error => {
|
|
13
|
+
.catch((error) => {
|
|
14
14
|
if (!(error instanceof Error)) {
|
|
15
|
-
error = new WeaveError(error, 500)
|
|
15
|
+
error = new WeaveError(error, 500);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
if (runtime.nodeId !== context.nodeId) {
|
|
19
|
-
runtime.transport.removePendingRequestsById(context.id)
|
|
19
|
+
runtime.transport.removePendingRequestsById(context.id);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
Object.defineProperty(error, 'context', {
|
|
23
|
+
value: context,
|
|
24
|
+
writable: true,
|
|
25
|
+
enumerable: false
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
runtime.log.debug(`The action "${context.action.name}" was rejected`, { requestId: context.requestId }, error);
|
|
29
|
+
return runtime.handleError(error);
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const wrapEventErrorHandlerMiddleware = function (handler) {
|
|
35
|
+
return function errorHandlerMiddleware (context, serviceInjections) {
|
|
36
|
+
return handler(context, serviceInjections)
|
|
37
|
+
.catch((error) => {
|
|
38
|
+
if (!(error instanceof Error)) {
|
|
39
|
+
error = new WeaveError(error, 500);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (runtime.nodeId !== context.nodeId) {
|
|
43
|
+
runtime.transport.removePendingRequestsById(context.id);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
Object.defineProperty(error, 'context', {
|
|
47
|
+
value: context,
|
|
48
|
+
writable: true,
|
|
49
|
+
enumerable: false
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
runtime.log.debug(`The event "${context.eventName}" was rejected`, { requestId: context.requestId }, error);
|
|
53
|
+
return runtime.handleError(error);
|
|
24
54
|
})
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
55
|
+
.catch((error) => {
|
|
56
|
+
// we just log the error because we don't want to crash the event loop
|
|
57
|
+
runtime.log.error(error);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
};
|
|
30
61
|
|
|
31
62
|
return {
|
|
32
63
|
localAction: wrapErrorHandlerMiddleware,
|
|
33
|
-
remoteAction: wrapErrorHandlerMiddleware
|
|
34
|
-
|
|
35
|
-
}
|
|
64
|
+
remoteAction: wrapErrorHandlerMiddleware,
|
|
65
|
+
localEvent: wrapEventErrorHandlerMiddleware
|
|
66
|
+
};
|
|
67
|
+
};
|
package/lib/middlewares/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
@@ -16,4 +16,4 @@ module.exports = {
|
|
|
16
16
|
Timeout: require('./timeout'),
|
|
17
17
|
ContextTracker: require('./context-tracker'),
|
|
18
18
|
Validator: require('./validator')
|
|
19
|
-
}
|
|
19
|
+
};
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
const { Constants } = require('../../metrics')
|
|
1
|
+
const { Constants } = require('../../metrics');
|
|
2
2
|
|
|
3
3
|
module.exports.getMiddlewareWrapper = (runtime) => function (type, action, handler) {
|
|
4
|
-
const serviceName = action.service ? action.service.fullyQualifiedName : null
|
|
5
|
-
const actionName = action.name
|
|
4
|
+
const serviceName = action.service ? action.service.fullyQualifiedName : null;
|
|
5
|
+
const actionName = action.name;
|
|
6
6
|
|
|
7
7
|
return function metricMiddleware (context, serviceInjections) {
|
|
8
|
-
const callerNodeId = context.callerNodeId
|
|
8
|
+
const callerNodeId = context.callerNodeId;
|
|
9
9
|
|
|
10
|
-
runtime.metrics.increment(Constants.REQUESTS_TOTAL, { type, serviceName, actionName, callerNodeId })
|
|
11
|
-
runtime.metrics.increment(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId })
|
|
12
|
-
const requestEnd = runtime.metrics.timer(Constants.REQUESTS_TIME, { type, serviceName, actionName, callerNodeId })
|
|
10
|
+
runtime.metrics.increment(Constants.REQUESTS_TOTAL, { type, serviceName, actionName, callerNodeId });
|
|
11
|
+
runtime.metrics.increment(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId });
|
|
12
|
+
const requestEnd = runtime.metrics.timer(Constants.REQUESTS_TIME, { type, serviceName, actionName, callerNodeId });
|
|
13
13
|
|
|
14
14
|
return handler(context, serviceInjections)
|
|
15
15
|
.then(result => {
|
|
16
|
-
requestEnd()
|
|
17
|
-
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId })
|
|
18
|
-
return result
|
|
16
|
+
requestEnd();
|
|
17
|
+
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId });
|
|
18
|
+
return result;
|
|
19
19
|
})
|
|
20
20
|
.catch(error => {
|
|
21
|
-
requestEnd()
|
|
22
|
-
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId })
|
|
23
|
-
runtime.metrics.increment(Constants.REQUESTS_ERRORS_TOTAL)
|
|
24
|
-
runtime.handleError(error)
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
}
|
|
21
|
+
requestEnd();
|
|
22
|
+
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId });
|
|
23
|
+
runtime.metrics.increment(Constants.REQUESTS_ERRORS_TOTAL);
|
|
24
|
+
runtime.handleError(error);
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -1,57 +1,57 @@
|
|
|
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
|
-
const { Constants } = require('../../metrics')
|
|
7
|
-
const { getMiddlewareWrapper } = require('./getMiddlewareWrapper')
|
|
6
|
+
const { Constants } = require('../../metrics');
|
|
7
|
+
const { getMiddlewareWrapper } = require('./getMiddlewareWrapper');
|
|
8
8
|
|
|
9
9
|
module.exports = (runtime) => {
|
|
10
|
-
const wrapMetricMiddleware = getMiddlewareWrapper(runtime)
|
|
10
|
+
const wrapMetricMiddleware = getMiddlewareWrapper(runtime);
|
|
11
11
|
|
|
12
12
|
return {
|
|
13
13
|
created () {
|
|
14
14
|
// Request metrics
|
|
15
|
-
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_TOTAL, description: 'Number of total requests.' })
|
|
16
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_IN_FLIGHT, description: 'Number of running requests.' })
|
|
17
|
-
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_ERRORS_TOTAL, description: 'Number of failed requests.' })
|
|
18
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_TIME, description: 'Request times in milliseconds' })
|
|
15
|
+
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_TOTAL, description: 'Number of total requests.' });
|
|
16
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_IN_FLIGHT, description: 'Number of running requests.' });
|
|
17
|
+
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_ERRORS_TOTAL, description: 'Number of failed requests.' });
|
|
18
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_TIME, description: 'Request times in milliseconds' });
|
|
19
19
|
|
|
20
20
|
// Event metrics
|
|
21
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_EMITS, description: 'Number of total emitted events.' })
|
|
22
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS, description: 'Number of total broadcasted events.' })
|
|
23
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS_LOCAL, description: 'Number of total local broadcasted events.' })
|
|
24
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_RECEIVED, description: 'Number of total received events.' })
|
|
21
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_EMITS, description: 'Number of total emitted events.' });
|
|
22
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS, description: 'Number of total broadcasted events.' });
|
|
23
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS_LOCAL, description: 'Number of total local broadcasted events.' });
|
|
24
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_RECEIVED, description: 'Number of total received events.' });
|
|
25
25
|
|
|
26
26
|
// Transport metrics
|
|
27
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' })
|
|
28
|
-
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_SENT, description: 'Number of in flight streams.' })
|
|
29
|
-
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_RECEIVED, description: 'Number of in flight streams.' })
|
|
30
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' })
|
|
27
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' });
|
|
28
|
+
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_SENT, description: 'Number of in flight streams.' });
|
|
29
|
+
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_RECEIVED, description: 'Number of in flight streams.' });
|
|
30
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' });
|
|
31
31
|
},
|
|
32
32
|
localAction (next, action) {
|
|
33
|
-
return wrapMetricMiddleware('local', action, next)
|
|
33
|
+
return wrapMetricMiddleware('local', action, next);
|
|
34
34
|
},
|
|
35
35
|
remoteAction (next, action) {
|
|
36
|
-
return wrapMetricMiddleware('remote', action, next)
|
|
36
|
+
return wrapMetricMiddleware('remote', action, next);
|
|
37
37
|
},
|
|
38
38
|
emit (next) {
|
|
39
39
|
return (event, payload) => {
|
|
40
|
-
runtime.metrics.increment(Constants.EVENT_TOTAL_EMITS)
|
|
41
|
-
return next(event, payload)
|
|
42
|
-
}
|
|
40
|
+
runtime.metrics.increment(Constants.EVENT_TOTAL_EMITS);
|
|
41
|
+
return next(event, payload);
|
|
42
|
+
};
|
|
43
43
|
},
|
|
44
44
|
broadcast (next) {
|
|
45
45
|
return (event, payload) => {
|
|
46
|
-
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS)
|
|
47
|
-
return next(event, payload)
|
|
48
|
-
}
|
|
46
|
+
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS);
|
|
47
|
+
return next(event, payload);
|
|
48
|
+
};
|
|
49
49
|
},
|
|
50
50
|
broadcastLocal (next) {
|
|
51
51
|
return (event, payload) => {
|
|
52
|
-
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS_LOCAL)
|
|
53
|
-
return next(event, payload)
|
|
54
|
-
}
|
|
52
|
+
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS_LOCAL);
|
|
53
|
+
return next(event, payload);
|
|
54
|
+
};
|
|
55
55
|
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
56
|
+
};
|
|
57
|
+
};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
|
|
2
2
|
/*
|
|
3
|
-
* Author: Kevin Ries (kevin@
|
|
3
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
4
4
|
* -----
|
|
5
5
|
* Copyright 2021 Fachwerk
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const { delay } = require('@weave-js/utils')
|
|
8
|
+
const { delay } = require('@weave-js/utils');
|
|
9
9
|
|
|
10
10
|
const wrapRetryMiddleware = function (handler, action) {
|
|
11
|
-
const self = this
|
|
12
|
-
const options = Object.assign({}, self.options.retryPolicy, action.retryPolicy || {})
|
|
11
|
+
const self = this;
|
|
12
|
+
const options = Object.assign({}, self.options.retryPolicy, action.retryPolicy || {});
|
|
13
13
|
|
|
14
14
|
// middleware is enabled
|
|
15
15
|
if (options.enabled) {
|
|
@@ -17,28 +17,28 @@ const wrapRetryMiddleware = function (handler, action) {
|
|
|
17
17
|
return function retryMiddleware (context, serviceInjections) {
|
|
18
18
|
// if the context has no repeat count, set it to zero.
|
|
19
19
|
if (context.retryCount === undefined) {
|
|
20
|
-
context.retryCount = 0
|
|
20
|
+
context.retryCount = 0;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
const attempts = typeof context.options.retries === 'number' ? context.options.retries : options.retries
|
|
23
|
+
const attempts = typeof context.options.retries === 'number' ? context.options.retries : options.retries;
|
|
24
24
|
|
|
25
25
|
return handler(context, serviceInjections)
|
|
26
26
|
.catch(error => {
|
|
27
27
|
if (context.retryCount++ < attempts && error.retryable === true) {
|
|
28
|
-
self.log.warn(`Retry to recall action '${context.action.name}' after ${options.delay}.`)
|
|
28
|
+
self.log.warn(`Retry to recall action '${context.action.name}' after ${options.delay}.`);
|
|
29
29
|
return delay(options.delay)
|
|
30
|
-
.then(() => self.call(context.action.name, context.data, { context }))
|
|
30
|
+
.then(() => self.call(context.action.name, context.data, { context }));
|
|
31
31
|
}
|
|
32
|
-
return Promise.reject(error)
|
|
33
|
-
})
|
|
34
|
-
}
|
|
32
|
+
return Promise.reject(error);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
35
|
}
|
|
36
|
-
return handler
|
|
37
|
-
}
|
|
36
|
+
return handler;
|
|
37
|
+
};
|
|
38
38
|
|
|
39
39
|
module.exports = () => {
|
|
40
40
|
return {
|
|
41
41
|
localAction: wrapRetryMiddleware,
|
|
42
42
|
remoteAction: wrapRetryMiddleware
|
|
43
|
-
}
|
|
44
|
-
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -1,44 +1,44 @@
|
|
|
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
|
-
const { promiseTimeout } = require('@weave-js/utils')
|
|
8
|
-
const { WeaveRequestTimeoutError } = require('../../errors')
|
|
7
|
+
const { promiseTimeout } = require('@weave-js/utils');
|
|
8
|
+
const { WeaveRequestTimeoutError } = require('../../errors');
|
|
9
9
|
|
|
10
10
|
const wrapTimeoutMiddleware = function (handler) {
|
|
11
|
-
const self = this
|
|
12
|
-
const registryOptions = self.options.registry || {}
|
|
11
|
+
const self = this;
|
|
12
|
+
const registryOptions = self.options.registry || {};
|
|
13
13
|
|
|
14
14
|
return function timeoutMiddleware (context, serviceInjections) {
|
|
15
15
|
if (typeof context.options.timeout === 'undefined' || registryOptions.requestTimeout) {
|
|
16
|
-
context.options.timeout = registryOptions.requestTimeout || 0
|
|
16
|
+
context.options.timeout = registryOptions.requestTimeout || 0;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
if (context.options.timeout > 0 && !context.startHighResolutionTime) {
|
|
20
|
-
context.startHighResolutionTime = process.hrtime()
|
|
20
|
+
context.startHighResolutionTime = process.hrtime();
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
let promise = handler(context, serviceInjections)
|
|
23
|
+
let promise = handler(context, serviceInjections);
|
|
24
24
|
|
|
25
25
|
if (context.options.timeout > 0) {
|
|
26
26
|
promise = promiseTimeout(context.options.timeout, promise, new WeaveRequestTimeoutError(context.action.name, context.nodeId, context.options.timeout))
|
|
27
27
|
.catch(error => {
|
|
28
28
|
if (error instanceof WeaveRequestTimeoutError) {
|
|
29
|
-
self.log.warn(`Request '${context.action.name}' timed out.`)
|
|
29
|
+
self.log.warn(`Request '${context.action.name}' timed out.`);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
return Promise.reject(error)
|
|
33
|
-
})
|
|
32
|
+
return Promise.reject(error);
|
|
33
|
+
});
|
|
34
34
|
}
|
|
35
|
-
return promise
|
|
36
|
-
}
|
|
37
|
-
}
|
|
35
|
+
return promise;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
38
|
|
|
39
39
|
module.exports = () => {
|
|
40
40
|
return {
|
|
41
41
|
localAction: wrapTimeoutMiddleware,
|
|
42
42
|
remoteAction: wrapTimeoutMiddleware
|
|
43
|
-
}
|
|
44
|
-
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -1,25 +1,25 @@
|
|
|
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
|
-
const { isPlainObject } = require('@weave-js/utils')
|
|
8
|
-
const { buildActionTags, buildEventTags } = require('./tags')
|
|
7
|
+
const { isPlainObject } = require('@weave-js/utils');
|
|
8
|
+
const { buildActionTags, buildEventTags } = require('./tags');
|
|
9
9
|
|
|
10
10
|
const wrapTracingLocalActionMiddleware = function (handler) {
|
|
11
|
-
const broker = this
|
|
12
|
-
const tracingOptions = broker.options.tracing || {}
|
|
11
|
+
const broker = this;
|
|
12
|
+
const tracingOptions = broker.options.tracing || {};
|
|
13
13
|
|
|
14
14
|
if (tracingOptions.enabled) {
|
|
15
15
|
return function metricsLocalMiddleware (context, serviceInjections) {
|
|
16
|
-
const tags = buildActionTags(context, tracingOptions)
|
|
16
|
+
const tags = buildActionTags(context, tracingOptions);
|
|
17
17
|
|
|
18
18
|
if (tracingOptions) {
|
|
19
|
-
tags.data = context.data !== null && isPlainObject(context.data) ? Object.assign({}, context.data) : context.data
|
|
19
|
+
tags.data = context.data !== null && isPlainObject(context.data) ? Object.assign({}, context.data) : context.data;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
const spanName = `action "${context.action.name}"
|
|
22
|
+
const spanName = `action "${context.action.name}"`;
|
|
23
23
|
|
|
24
24
|
const span = context.startSpan(spanName, {
|
|
25
25
|
id: context.id,
|
|
@@ -29,43 +29,43 @@ const wrapTracingLocalActionMiddleware = function (handler) {
|
|
|
29
29
|
service: context.service,
|
|
30
30
|
tags,
|
|
31
31
|
sampled: context.tracing
|
|
32
|
-
})
|
|
32
|
+
});
|
|
33
33
|
|
|
34
|
-
context.span = span
|
|
34
|
+
context.span = span;
|
|
35
35
|
|
|
36
36
|
return handler(context, serviceInjections)
|
|
37
37
|
.then(result => {
|
|
38
38
|
const tags = {
|
|
39
39
|
isCachedResult: context.isCachedResult
|
|
40
|
-
}
|
|
40
|
+
};
|
|
41
41
|
|
|
42
42
|
if (tracingOptions) {
|
|
43
|
-
tags.response = result !== null && isPlainObject(result) ? Object.assign({}, result) : result
|
|
43
|
+
tags.response = result !== null && isPlainObject(result) ? Object.assign({}, result) : result;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
span.addTags(tags)
|
|
47
|
-
span.finish()
|
|
48
|
-
return result
|
|
46
|
+
span.addTags(tags);
|
|
47
|
+
span.finish();
|
|
48
|
+
return result;
|
|
49
49
|
})
|
|
50
50
|
.catch(error => {
|
|
51
51
|
span
|
|
52
52
|
.setError(error)
|
|
53
|
-
.finish()
|
|
54
|
-
return Promise.reject(error)
|
|
55
|
-
})
|
|
56
|
-
}
|
|
53
|
+
.finish();
|
|
54
|
+
return Promise.reject(error);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
57
|
}
|
|
58
|
-
return handler
|
|
59
|
-
}
|
|
58
|
+
return handler;
|
|
59
|
+
};
|
|
60
60
|
|
|
61
61
|
const wrapTracingLocalEventMiddleware = function (handler, event) {
|
|
62
|
-
const broker = this
|
|
63
|
-
const service = event.service
|
|
64
|
-
const tracingOptions = broker.options.tracing || {}
|
|
62
|
+
const broker = this;
|
|
63
|
+
const service = event.service;
|
|
64
|
+
const tracingOptions = broker.options.tracing || {};
|
|
65
65
|
|
|
66
66
|
if (tracingOptions.enabled) {
|
|
67
67
|
return function metricsLocalMiddleware (context) {
|
|
68
|
-
const tags = buildEventTags(context)
|
|
68
|
+
const tags = buildEventTags(context);
|
|
69
69
|
|
|
70
70
|
const span = context.startSpan(`event "${context.eventName}"`, {
|
|
71
71
|
id: context.id,
|
|
@@ -75,29 +75,29 @@ const wrapTracingLocalEventMiddleware = function (handler, event) {
|
|
|
75
75
|
service,
|
|
76
76
|
tags,
|
|
77
77
|
sampled: context.tracing
|
|
78
|
-
})
|
|
78
|
+
});
|
|
79
79
|
|
|
80
|
-
context.span = span
|
|
80
|
+
context.span = span;
|
|
81
81
|
|
|
82
82
|
return handler(context)
|
|
83
83
|
.then(result => {
|
|
84
|
-
span.finish()
|
|
85
|
-
return result
|
|
84
|
+
span.finish();
|
|
85
|
+
return result;
|
|
86
86
|
})
|
|
87
87
|
.catch(error => {
|
|
88
88
|
span
|
|
89
89
|
.setError(error)
|
|
90
|
-
.finish()
|
|
91
|
-
return Promise.reject(error)
|
|
92
|
-
})
|
|
93
|
-
}
|
|
90
|
+
.finish();
|
|
91
|
+
return Promise.reject(error);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
94
|
}
|
|
95
|
-
return handler
|
|
96
|
-
}
|
|
95
|
+
return handler;
|
|
96
|
+
};
|
|
97
97
|
|
|
98
98
|
module.exports = () => {
|
|
99
99
|
return {
|
|
100
100
|
localAction: wrapTracingLocalActionMiddleware,
|
|
101
101
|
localEvent: wrapTracingLocalEventMiddleware
|
|
102
|
-
}
|
|
103
|
-
}
|
|
102
|
+
};
|
|
103
|
+
};
|
|
@@ -17,10 +17,10 @@ module.exports.buildActionTags = (context, brokerTracingOptions, actionTracingOp
|
|
|
17
17
|
isRemoteCall: !!context.callerNodeId,
|
|
18
18
|
nodeId: context.nodeId,
|
|
19
19
|
requestId: context.requestId
|
|
20
|
-
}
|
|
20
|
+
};
|
|
21
21
|
|
|
22
|
-
return tags
|
|
23
|
-
}
|
|
22
|
+
return tags;
|
|
23
|
+
};
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Build span tags object
|
|
@@ -37,7 +37,7 @@ module.exports.buildEventTags = (context, brokerTracingOptions, actionTracingOpt
|
|
|
37
37
|
isRemoteCall: !!context.callerNodeId,
|
|
38
38
|
nodeId: context.nodeId,
|
|
39
39
|
requestId: context.requestId
|
|
40
|
-
}
|
|
40
|
+
};
|
|
41
41
|
|
|
42
|
-
return tags
|
|
43
|
-
}
|
|
42
|
+
return tags;
|
|
43
|
+
};
|
|
@@ -1,11 +1,11 @@
|
|
|
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
|
-
const { WeaveParameterValidationError } = require('../../errors')
|
|
8
|
-
const { capitalize } = require('@weave-js/utils')
|
|
7
|
+
const { WeaveParameterValidationError } = require('../../errors');
|
|
8
|
+
const { capitalize } = require('@weave-js/utils');
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* @typedef {import('../../types').Runtime} Runtime
|
|
@@ -18,77 +18,77 @@ const { capitalize } = require('@weave-js/utils')
|
|
|
18
18
|
* @returns {Middleware} - Validator middleware
|
|
19
19
|
*/
|
|
20
20
|
module.exports = (runtime) => {
|
|
21
|
-
const validator = runtime.validator
|
|
21
|
+
const validator = runtime.validator;
|
|
22
22
|
|
|
23
23
|
const processErrors = (context, type, results) => {
|
|
24
|
-
const errors = results.map(data => Object.assign(data, { nodeId: context.nodeId, action: context.action.name }))
|
|
25
|
-
return Promise.reject(new WeaveParameterValidationError(`${capitalize(type)} parameter validation error`, errors))
|
|
26
|
-
}
|
|
24
|
+
const errors = results.map(data => Object.assign(data, { nodeId: context.nodeId, action: context.action.name }));
|
|
25
|
+
return Promise.reject(new WeaveParameterValidationError(`${capitalize(type)} parameter validation error`, errors));
|
|
26
|
+
};
|
|
27
27
|
|
|
28
28
|
return {
|
|
29
29
|
localAction (handler, action) {
|
|
30
30
|
const parameterOptions = Object.assign(
|
|
31
31
|
runtime.options.validatorOptions,
|
|
32
32
|
action.validatorOptions
|
|
33
|
-
)
|
|
33
|
+
);
|
|
34
34
|
|
|
35
35
|
// validate request schema
|
|
36
|
-
let validateRequestSchema
|
|
37
|
-
let validateResponseSchema
|
|
36
|
+
let validateRequestSchema;
|
|
37
|
+
let validateResponseSchema;
|
|
38
38
|
if (action.params && typeof action.params === 'object') {
|
|
39
|
-
validateRequestSchema = validator.compile(action.params, parameterOptions)
|
|
39
|
+
validateRequestSchema = validator.compile(action.params, parameterOptions);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
if (action.responseSchema && typeof action.responseSchema === 'object') {
|
|
43
|
-
validateResponseSchema = validator.compile(action.responseSchema, parameterOptions)
|
|
43
|
+
validateResponseSchema = validator.compile(action.responseSchema, parameterOptions);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
if (!validateRequestSchema && !validateRequestSchema) {
|
|
47
|
-
return handler
|
|
47
|
+
return handler;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
return (context, serviceInjections) => {
|
|
51
|
-
const requestSchemaResult = validateRequestSchema ? validateRequestSchema(context.data) : true
|
|
51
|
+
const requestSchemaResult = validateRequestSchema ? validateRequestSchema(context.data) : true;
|
|
52
52
|
|
|
53
53
|
if (requestSchemaResult === true) {
|
|
54
54
|
return handler(context, serviceInjections)
|
|
55
55
|
.then((result) => {
|
|
56
56
|
if (validateResponseSchema) {
|
|
57
|
-
const responseSchemaResult = validateResponseSchema(result)
|
|
57
|
+
const responseSchemaResult = validateResponseSchema(result);
|
|
58
58
|
if (responseSchemaResult === true) {
|
|
59
|
-
return result
|
|
59
|
+
return result;
|
|
60
60
|
}
|
|
61
|
-
return processErrors(context, 'response', responseSchemaResult)
|
|
61
|
+
return processErrors(context, 'response', responseSchemaResult);
|
|
62
62
|
}
|
|
63
|
-
return result
|
|
64
|
-
})
|
|
63
|
+
return result;
|
|
64
|
+
});
|
|
65
65
|
} else {
|
|
66
66
|
// Enriching the validator errors with some useful information
|
|
67
|
-
return processErrors(context, 'request', requestSchemaResult)
|
|
67
|
+
return processErrors(context, 'request', requestSchemaResult);
|
|
68
68
|
}
|
|
69
|
-
}
|
|
69
|
+
};
|
|
70
70
|
},
|
|
71
71
|
localEvent (handler, event) {
|
|
72
72
|
if (event.params && typeof event.params === 'object') {
|
|
73
73
|
const parameterOptions = Object.assign(
|
|
74
74
|
runtime.options.validatorOptions,
|
|
75
75
|
event.validatorOptions
|
|
76
|
-
)
|
|
76
|
+
);
|
|
77
77
|
|
|
78
|
-
const validate = validator.compile(event.params, parameterOptions)
|
|
78
|
+
const validate = validator.compile(event.params, parameterOptions);
|
|
79
79
|
|
|
80
80
|
return (context, serviceInjections) => {
|
|
81
|
-
let result = validate(context.data)
|
|
81
|
+
let result = validate(context.data);
|
|
82
82
|
|
|
83
83
|
if (result === true) {
|
|
84
|
-
return handler(context, serviceInjections)
|
|
84
|
+
return handler(context, serviceInjections);
|
|
85
85
|
} else {
|
|
86
|
-
result = result.map(data => Object.assign(data, { nodeId: context.nodeId, event: context.eventName }))
|
|
87
|
-
return Promise.reject(new WeaveParameterValidationError('Parameter validation error', result))
|
|
86
|
+
result = result.map(data => Object.assign(data, { nodeId: context.nodeId, event: context.eventName }));
|
|
87
|
+
return Promise.reject(new WeaveParameterValidationError('Parameter validation error', result));
|
|
88
88
|
}
|
|
89
|
-
}
|
|
89
|
+
};
|
|
90
90
|
}
|
|
91
|
-
return handler
|
|
91
|
+
return handler;
|
|
92
92
|
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/*
|
|
10
|
-
* Author: Kevin Ries (kevin@
|
|
10
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
11
11
|
* -----
|
|
12
12
|
* Copyright 2021 Fachwerk
|
|
13
13
|
*/
|
|
@@ -31,15 +31,15 @@ exports.createActionEndpoint = (runtime, node, service, action) => {
|
|
|
31
31
|
isLocal: node.id === runtime.nodeId,
|
|
32
32
|
state: true,
|
|
33
33
|
name: `${node.id}:${action.name}`
|
|
34
|
-
}
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
endpoint.updateAction = (newAction) => {
|
|
37
|
-
endpoint.action = newAction
|
|
38
|
-
}
|
|
37
|
+
endpoint.action = newAction;
|
|
38
|
+
};
|
|
39
39
|
|
|
40
40
|
endpoint.isAvailable = () => {
|
|
41
|
-
return endpoint.state === true
|
|
42
|
-
}
|
|
41
|
+
return endpoint.state === true;
|
|
42
|
+
};
|
|
43
43
|
|
|
44
|
-
return endpoint
|
|
45
|
-
}
|
|
44
|
+
return endpoint;
|
|
45
|
+
};
|