@weave-js/core 0.11.1 → 0.13.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.
Files changed (116) hide show
  1. package/.eslintrc.js +1 -1
  2. package/jest.config.js +1 -1
  3. package/lib/ExtendableError.js +8 -8
  4. package/lib/broker/context.js +49 -49
  5. package/lib/broker/defaultOptions.js +8 -9
  6. package/lib/broker/index.js +160 -164
  7. package/lib/buildRuntime.js +34 -34
  8. package/lib/cache/adapters/base.js +50 -74
  9. package/lib/cache/adapters/inMemory.js +58 -58
  10. package/lib/cache/adapters/inMemoryLru.js +57 -57
  11. package/lib/cache/adapters/index.js +3 -39
  12. package/lib/cache/getCacheKeyByObject.js +31 -0
  13. package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
  14. package/lib/cache/lock.js +30 -24
  15. package/lib/constants.js +5 -5
  16. package/lib/errorHandler.js +8 -8
  17. package/lib/errors.js +64 -54
  18. package/lib/helper/defineAction.js +2 -2
  19. package/lib/helper/defineBrokerOptions.js +2 -2
  20. package/lib/helper/defineService.js +2 -2
  21. package/lib/index.js +21 -21
  22. package/lib/logger/base.js +38 -29
  23. package/lib/logger/format/asHumanReadable.js +15 -15
  24. package/lib/logger/format/asJson.js +10 -10
  25. package/lib/logger/format/index.js +3 -3
  26. package/lib/logger/index.js +24 -24
  27. package/lib/logger/levels.js +21 -21
  28. package/lib/logger/tools.js +15 -15
  29. package/lib/logger/utils/colorize.js +23 -23
  30. package/lib/logger/utils/format.js +75 -65
  31. package/lib/metrics/common.js +47 -47
  32. package/lib/metrics/constants.js +44 -44
  33. package/lib/metrics/exporter/base.js +8 -8
  34. package/lib/metrics/exporter/event.js +20 -20
  35. package/lib/metrics/exporter/index.js +15 -15
  36. package/lib/metrics/index.js +2 -2
  37. package/lib/metrics/types/base.js +20 -20
  38. package/lib/metrics/types/counter.js +6 -6
  39. package/lib/metrics/types/gauge.js +24 -24
  40. package/lib/metrics/types/histogram.js +26 -26
  41. package/lib/metrics/types/index.js +6 -6
  42. package/lib/metrics/types/info.js +17 -17
  43. package/lib/middlewares/action-hooks/index.js +38 -38
  44. package/lib/middlewares/bulkhead/index.js +34 -34
  45. package/lib/middlewares/cache/index.js +37 -37
  46. package/lib/middlewares/circuit-breaker/index.js +62 -62
  47. package/lib/middlewares/context-tracker/index.js +39 -39
  48. package/lib/middlewares/error-handler/index.js +47 -15
  49. package/lib/middlewares/index.js +2 -2
  50. package/lib/middlewares/metrics/getMiddlewareWrapper.js +17 -17
  51. package/lib/middlewares/metrics/index.js +29 -29
  52. package/lib/middlewares/retry/index.js +15 -15
  53. package/lib/middlewares/timeout/index.js +16 -16
  54. package/lib/middlewares/tracing/index.js +37 -37
  55. package/lib/middlewares/tracing/tags.js +6 -6
  56. package/lib/middlewares/validator/index.js +31 -31
  57. package/lib/registry/actionEndpoint.js +8 -8
  58. package/lib/registry/collections/actionCollection.js +39 -35
  59. package/lib/registry/collections/endpointCollection.js +63 -63
  60. package/lib/registry/collections/eventCollection.js +62 -62
  61. package/lib/registry/collections/nodeCollection.js +45 -45
  62. package/lib/registry/collections/serviceCollection.js +61 -55
  63. package/lib/registry/eventEndpoint.js +14 -14
  64. package/lib/registry/load-balancer/base.js +3 -3
  65. package/lib/registry/load-balancer/index.js +7 -7
  66. package/lib/registry/node.js +27 -27
  67. package/lib/registry/registry.js +139 -136
  68. package/lib/registry/service/parseAction.js +16 -16
  69. package/lib/registry/service/parseEvent.js +18 -18
  70. package/lib/registry/service/reduceMixins.js +10 -10
  71. package/lib/registry/service/service.js +72 -72
  72. package/lib/registry/serviceItem.js +21 -21
  73. package/lib/runtime/initActionInvoker.js +21 -21
  74. package/lib/runtime/initCache.js +7 -7
  75. package/lib/runtime/initContextFactory.js +22 -22
  76. package/lib/runtime/initEventbus.js +49 -49
  77. package/lib/runtime/initLogger.js +11 -11
  78. package/lib/runtime/initMetrics.js +54 -54
  79. package/lib/runtime/initMiddlewareManager.js +21 -21
  80. package/lib/runtime/initRegistry.js +6 -6
  81. package/lib/runtime/initServiceManager.js +90 -68
  82. package/lib/runtime/initTracing.js +23 -23
  83. package/lib/runtime/initTransport.js +5 -5
  84. package/lib/runtime/initUuidFactory.js +4 -4
  85. package/lib/runtime/initValidator.js +5 -5
  86. package/lib/tracing/collectors/base.js +22 -22
  87. package/lib/tracing/collectors/event.js +33 -33
  88. package/lib/tracing/collectors/index.js +15 -15
  89. package/lib/tracing/span.js +26 -26
  90. package/lib/tracing/time.js +8 -8
  91. package/lib/transport/InboundTransformStream.js +26 -26
  92. package/lib/transport/adapters/adapteBase.js +30 -30
  93. package/lib/transport/adapters/adapters.js +4 -4
  94. package/lib/transport/adapters/dummy/index.js +18 -18
  95. package/lib/transport/adapters/fromURI.js +13 -13
  96. package/lib/transport/adapters/getAdapterByName.js +5 -5
  97. package/lib/transport/adapters/index.js +12 -12
  98. package/lib/transport/adapters/tcp/discovery/codec.js +4 -4
  99. package/lib/transport/adapters/tcp/discovery/index.js +62 -62
  100. package/lib/transport/adapters/tcp/index.js +169 -171
  101. package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
  102. package/lib/transport/adapters/tcp/tcpReader.js +39 -39
  103. package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
  104. package/lib/transport/adapters/tcp/tcpWriter.js +57 -57
  105. package/lib/transport/adapters/tcp/utils.js +12 -12
  106. package/lib/transport/createMessage.js +4 -4
  107. package/lib/transport/createTransport.js +254 -254
  108. package/lib/transport/errorPayloadFactory.js +13 -17
  109. package/lib/transport/messageHandlers.js +199 -206
  110. package/lib/transport/messageTypes.js +19 -19
  111. package/lib/types.js +28 -15
  112. package/lib/utils/index.js +1 -1
  113. package/lib/utils/options.js +44 -44
  114. package/lib/utils/restoreError.js +58 -6
  115. package/package.json +4 -4
  116. package/lib/cache/base.js +0 -136
@@ -1,107 +1,107 @@
1
- const { WeaveGracefulStopTimeoutError } = require('../../errors')
1
+ const { WeaveGracefulStopTimeoutError } = require('../../errors');
2
2
 
3
3
  module.exports = (runtime) => {
4
4
  function addContext (context) {
5
5
  if (context.service) {
6
6
  // local actions
7
- context.service._trackedContexts.push(context)
7
+ context.service._trackedContexts.push(context);
8
8
  } else {
9
9
  // remote actions
10
- context.service._trackedContexts.push(context)
10
+ context.service._trackedContexts.push(context);
11
11
  }
12
12
  }
13
13
 
14
14
  function removeContext (context) {
15
15
  if (context.service) {
16
16
  // local actions
17
- const index = context.service._trackedContexts.indexOf(context)
17
+ const index = context.service._trackedContexts.indexOf(context);
18
18
  if (index !== -1) {
19
- context.service._trackedContexts.splice(index, 1)
19
+ context.service._trackedContexts.splice(index, 1);
20
20
  }
21
21
  } else {
22
- const index = context.service._trackedContexts.indexOf(context)
22
+ const index = context.service._trackedContexts.indexOf(context);
23
23
  if (index !== -1) {
24
- context.service._trackedContexts.splice(index, 1)
24
+ context.service._trackedContexts.splice(index, 1);
25
25
  }
26
26
  }
27
27
  }
28
28
 
29
29
  function wrapContextTrackerMiddleware (actionHandler) {
30
30
  return function ContextTrackerMiddleware (context, serviceInjections) {
31
- const isTracked = context.options.track === true ? context.options.track : runtime.options.contextTracking.enabled
31
+ const isTracked = context.options.track === true ? context.options.track : runtime.options.contextTracking.enabled;
32
32
 
33
33
  if (!isTracked) {
34
- return actionHandler(context, serviceInjections)
34
+ return actionHandler(context, serviceInjections);
35
35
  }
36
36
 
37
- addContext(context)
37
+ addContext(context);
38
38
 
39
39
  return actionHandler(context, serviceInjections)
40
40
  .then(result => {
41
- removeContext(context)
42
- return result
41
+ removeContext(context);
42
+ return result;
43
43
  })
44
44
  .catch(error => {
45
- removeContext(context)
46
- throw error
47
- })
48
- }
45
+ removeContext(context);
46
+ throw error;
47
+ });
48
+ };
49
49
  }
50
50
 
51
51
  const waitingForActiveContexts = (contextList, log, shutdownTimeout, service) => {
52
52
  return new Promise((resolve) => {
53
53
  if (contextList.length === 0) {
54
- resolve()
54
+ resolve();
55
55
  }
56
56
 
57
- let isTimedOut = false
57
+ let isTimedOut = false;
58
58
  const timeout = setTimeout(() => {
59
- isTimedOut = true
60
- log.error(new WeaveGracefulStopTimeoutError(service))
61
- contextList = []
62
- resolve()
63
- }, shutdownTimeout)
59
+ isTimedOut = true;
60
+ log.error(new WeaveGracefulStopTimeoutError(service));
61
+ contextList = [];
62
+ resolve();
63
+ }, shutdownTimeout);
64
64
 
65
- let isFirstCheck = true
65
+ let isFirstCheck = true;
66
66
  const checkContexts = () => {
67
67
  if (contextList.length === 0) {
68
- clearTimeout(timeout)
69
- resolve()
68
+ clearTimeout(timeout);
69
+ resolve();
70
70
  } else {
71
71
  if (isFirstCheck) {
72
- log.info(`Waiting for ${contextList.length} open Contexts...`)
73
- isFirstCheck = false
72
+ log.info(`Waiting for ${contextList.length} open Contexts...`);
73
+ isFirstCheck = false;
74
74
  }
75
75
 
76
76
  if (!isTimedOut) {
77
- setTimeout(checkContexts, 100)
77
+ setTimeout(checkContexts, 100);
78
78
  }
79
79
  }
80
- }
81
- setImmediate(checkContexts)
82
- })
83
- }
80
+ };
81
+ setImmediate(checkContexts);
82
+ });
83
+ };
84
84
 
85
85
  return {
86
86
  created () {
87
87
  // init context-store
88
- runtime.state.trackedContexts = []
88
+ runtime.state.trackedContexts = [];
89
89
  },
90
90
  serviceStarting (service) {
91
- service._trackedContexts = []
91
+ service._trackedContexts = [];
92
92
  },
93
93
 
94
94
  // Before a local service stopping
95
95
  serviceStopping (service) {
96
- return waitingForActiveContexts(service._trackedContexts, service.log, service.settings.$shutdownTimeout || service.broker.options.contextTracking.shutdownTimeout, service)
96
+ return waitingForActiveContexts(service._trackedContexts, service.log, service.settings.$shutdownTimeout || service.broker.options.contextTracking.shutdownTimeout, service);
97
97
  },
98
98
 
99
99
  // Before broker stopping
100
100
  stopping () {
101
- return waitingForActiveContexts(runtime.state.trackedContexts, runtime.log, runtime.options.contextTracking.shutdownTimeout)
101
+ return waitingForActiveContexts(runtime.state.trackedContexts, runtime.log, runtime.options.contextTracking.shutdownTimeout);
102
102
  },
103
103
  localAction: wrapContextTrackerMiddleware,
104
104
  remoteAction: wrapContextTrackerMiddleware,
105
105
  localEvent: wrapContextTrackerMiddleware
106
- }
107
- }
106
+ };
107
+ };
@@ -1,35 +1,67 @@
1
1
  /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
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);
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
- runtime.log.debug(`The action ${context.action.name} is rejected`, { requestId: context.id }, error)
23
- return runtime.handleError(error)
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.message, { cause: error });
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
- // .catch(error => {
26
- // runtime.log.error(error)
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
+ };
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
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@fachw3rk.de)
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@fachw3rk.de)
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@fachw3rk.de)
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@fachw3rk.de)
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
+ };