@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.
Files changed (132) hide show
  1. package/.eslintrc.js +1 -1
  2. package/jest.config.js +1 -1
  3. package/lib/ExtendableError.js +33 -0
  4. package/lib/broker/context.js +58 -46
  5. package/lib/broker/{default-options.js → defaultOptions.js} +31 -13
  6. package/lib/broker/index.js +273 -251
  7. package/lib/buildRuntime.js +70 -0
  8. package/lib/cache/adapters/base.js +119 -0
  9. package/lib/cache/adapters/inMemory.js +154 -0
  10. package/lib/cache/adapters/inMemoryLru.js +140 -0
  11. package/lib/cache/adapters/index.js +10 -0
  12. package/lib/cache/getCacheKeyByObject.js +31 -0
  13. package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
  14. package/lib/cache/lock.js +34 -11
  15. package/lib/constants.js +11 -8
  16. package/lib/{error-handler.js → errorHandler.js} +8 -8
  17. package/lib/errors.js +46 -39
  18. package/lib/helper/{define-action.js → defineAction.js} +5 -3
  19. package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +4 -2
  20. package/lib/helper/{define-service.js → defineService.js} +6 -3
  21. package/lib/index.js +25 -22
  22. package/lib/logger/base.js +38 -29
  23. package/lib/logger/format/asHumanReadable.js +15 -14
  24. package/lib/logger/format/asJson.js +10 -10
  25. package/lib/logger/format/index.js +3 -3
  26. package/lib/logger/index.js +28 -25
  27. package/lib/logger/levels.js +23 -22
  28. package/lib/logger/tools.js +17 -13
  29. package/lib/logger/utils/colorize.js +23 -23
  30. package/lib/logger/utils/format.js +89 -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 +82 -34
  44. package/lib/middlewares/bulkhead/index.js +34 -33
  45. package/lib/middlewares/cache/index.js +106 -0
  46. package/lib/middlewares/circuit-breaker/index.js +85 -60
  47. package/lib/middlewares/context-tracker/index.js +39 -39
  48. package/lib/middlewares/error-handler/index.js +14 -14
  49. package/lib/middlewares/index.js +3 -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 +18 -18
  54. package/lib/middlewares/tracing/index.js +50 -37
  55. package/lib/middlewares/tracing/tags.js +43 -0
  56. package/lib/middlewares/validator/index.js +32 -32
  57. package/lib/registry/{action-endpoint.js → actionEndpoint.js} +8 -8
  58. package/lib/registry/collections/{action-collection.js → actionCollection.js} +39 -35
  59. package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +64 -65
  60. package/lib/registry/collections/{event-collection.js → eventCollection.js} +65 -69
  61. package/lib/registry/collections/{node-collection.js → nodeCollection.js} +45 -45
  62. package/lib/registry/collections/{service-collection.js → serviceCollection.js} +61 -55
  63. package/lib/registry/eventEndpoint.js +26 -0
  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 +147 -163
  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/{service-item.js → serviceItem.js} +21 -21
  73. package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +21 -21
  74. package/lib/runtime/initCache.js +19 -0
  75. package/lib/runtime/{init-context-factory.js → initContextFactory.js} +22 -22
  76. package/lib/runtime/{init-eventbus.js → initEventbus.js} +54 -53
  77. package/lib/runtime/{init-logger.js → initLogger.js} +11 -11
  78. package/lib/runtime/{init-metrics.js → initMetrics.js} +55 -54
  79. package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +21 -21
  80. package/lib/runtime/{init-registry.js → initRegistry.js} +6 -6
  81. package/lib/runtime/initServiceManager.js +145 -0
  82. package/lib/runtime/{init-tracing.js → initTracing.js} +25 -24
  83. package/lib/runtime/{init-transport.js → initTransport.js} +5 -5
  84. package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +4 -4
  85. package/lib/runtime/{init-validator.js → initValidator.js} +5 -5
  86. package/lib/tracing/collectors/base.js +29 -37
  87. package/lib/tracing/collectors/event.js +33 -33
  88. package/lib/tracing/collectors/index.js +16 -16
  89. package/lib/tracing/span.js +34 -32
  90. package/lib/tracing/time.js +8 -8
  91. package/lib/transport/InboundTransformStream.js +75 -0
  92. package/lib/transport/adapters/adapteBase.js +107 -0
  93. package/lib/transport/adapters/adapters.js +4 -4
  94. package/lib/transport/adapters/dummy/index.js +19 -19
  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 +90 -76
  100. package/lib/transport/adapters/tcp/index.js +180 -175
  101. package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
  102. package/lib/transport/adapters/tcp/tcpReader.js +39 -38
  103. package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
  104. package/lib/transport/adapters/tcp/tcpWriter.js +58 -58
  105. package/lib/transport/adapters/tcp/utils.js +12 -12
  106. package/lib/transport/createMessage.js +11 -0
  107. package/lib/transport/createTransport.js +377 -334
  108. package/lib/transport/errorPayloadFactory.js +15 -0
  109. package/lib/transport/messageHandlers.js +517 -0
  110. package/lib/transport/messageTypes.js +23 -0
  111. package/lib/types.js +95 -71
  112. package/lib/utils/index.js +2 -2
  113. package/lib/utils/options.js +47 -45
  114. package/lib/utils/{restore-error.js → restoreError.js} +4 -4
  115. package/package.json +6 -12
  116. package/CHANGELOG.md +0 -1334
  117. package/lib/build-runtime.js +0 -70
  118. package/lib/cache/base.js +0 -142
  119. package/lib/cache/index.js +0 -48
  120. package/lib/cache/memory.js +0 -121
  121. package/lib/middlewares/tracing/buildTags.js +0 -17
  122. package/lib/new-errors.js +0 -25
  123. package/lib/registry/event-endpoint.js +0 -26
  124. package/lib/runtime/init-cache.js +0 -10
  125. package/lib/runtime/init-service-manager.js +0 -128
  126. package/lib/tracing/collectors/console.js +0 -34
  127. package/lib/tracing/index.js +0 -3
  128. package/lib/tracing/tracer.js +0 -64
  129. package/lib/transport/adapters/adapter-base.js +0 -92
  130. package/lib/transport/message-handlers.js +0 -448
  131. package/lib/transport/message-types.js +0 -59
  132. package/lib/transport/message.js +0 -16
@@ -1,5 +1,5 @@
1
1
  exports.initEventbus = (runtime) => {
2
- const { options: brokerOptions, bus, registry, contextFactory } = runtime
2
+ const { options: brokerOptions, bus, registry, contextFactory } = runtime;
3
3
 
4
4
  /**
5
5
  * Emit a event on all services (grouped and load balanced).
@@ -10,59 +10,60 @@ exports.initEventbus = (runtime) => {
10
10
  */
11
11
  const emit = (eventName, payload, options) => {
12
12
  if (Array.isArray(options)) {
13
- options = { groups: options }
13
+ options = { groups: options };
14
14
  } else if (options == null) {
15
- options = {}
15
+ options = {};
16
16
  }
17
17
 
18
- const promises = []
19
- // todo: create an event context object
20
- const context = contextFactory.create(null, payload, options)
21
-
22
- context.eventType = 'emit'
23
- context.eventName = eventName
24
- context.eventGroups = options.groups
25
-
26
- // Emit system events
18
+ // Emit local events
27
19
  if (/^\$/.test(eventName)) {
28
- bus.emit(eventName, payload)
20
+ bus.emit(eventName, payload);
29
21
  }
30
22
 
31
- const endpoints = registry.eventCollection.getBalancedEndpoints(eventName, options.groups)
32
- const groupedEndpoints = {}
23
+ // todo: create an event context object
24
+ const context = contextFactory.create(null, payload, options);
25
+
26
+ context.eventType = 'emit';
27
+ context.eventName = eventName;
28
+ context.eventGroups = options.groups;
29
+
30
+ const endpoints = registry.eventCollection.getBalancedEndpoints(eventName, options.groups);
31
+ const groupedEndpoints = {};
32
+ const promises = [];
33
33
 
34
34
  endpoints.map(([endpoint, groupName]) => {
35
35
  if (endpoint) {
36
36
  if (endpoint.node.id === brokerOptions.nodeId) {
37
37
  // Local event. Call handler
38
- promises.push(endpoint.action.handler(context))
38
+ context.setEndpoint(endpoint);
39
+ promises.push(endpoint.action.handler(context));
39
40
  } else {
40
- const e = groupedEndpoints[endpoint.node.id]
41
+ const e = groupedEndpoints[endpoint.node.id];
41
42
  if (e) {
42
- e.groups.push(groupName)
43
+ e.groups.push(groupName);
43
44
  } else {
44
45
  groupedEndpoints[endpoint.node.id] = {
45
46
  endpoint,
46
47
  groups: [groupName]
47
- }
48
+ };
48
49
  }
49
50
  }
50
51
  }
51
- })
52
+ });
52
53
 
53
- // send remote events
54
+ // Send remote events
54
55
  if (runtime.transport) {
55
56
  Object.values(groupedEndpoints)
56
57
  .forEach(groupedEndpoint => {
57
- const newContext = context.copy()
58
- newContext.setEndpoint(groupedEndpoint.endpoint)
59
- newContext.eventGroups = groupedEndpoint.groups
60
- promises.push(runtime.transport.sendEvent(newContext))
61
- })
58
+ const newContext = context.copy();
59
+ newContext.setEndpoint(groupedEndpoint.endpoint);
60
+ newContext.eventGroups = groupedEndpoint.groups;
61
+ promises.push(runtime.transport.sendEvent(newContext));
62
+ });
62
63
  }
63
64
 
64
- return Promise.all(promises)
65
- }
65
+ return Promise.all(promises);
66
+ };
66
67
 
67
68
  /**
68
69
  * Send a broadcasted event to all local services.
@@ -74,22 +75,22 @@ exports.initEventbus = (runtime) => {
74
75
  const broadcastLocal = (eventName, payload, options) => {
75
76
  // If the given group is no array - wrap it.
76
77
  if (Array.isArray(options)) {
77
- options = { groups: options }
78
+ options = { groups: options };
78
79
  } else if (options == null) {
79
- options = {}
80
+ options = {};
80
81
  }
81
82
 
82
- const context = contextFactory.create(null, payload, options)
83
- context.eventType = 'broadcastLocal'
84
- context.eventName = eventName
83
+ const context = contextFactory.create(null, payload, options);
84
+ context.eventType = 'broadcastLocal';
85
+ context.eventName = eventName;
85
86
 
86
87
  // Emit the event on the internal event bus
87
88
  if (/^\$/.test(eventName)) {
88
- bus.emit(eventName, payload)
89
+ bus.emit(eventName, payload);
89
90
  }
90
91
 
91
- return registry.eventCollection.emitLocal(context)
92
- }
92
+ return registry.eventCollection.emitLocal(context);
93
+ };
93
94
 
94
95
  /**
95
96
  * Send a broadcasted event to all services.
@@ -100,38 +101,38 @@ exports.initEventbus = (runtime) => {
100
101
  */
101
102
  const broadcast = (eventName, payload, options) => {
102
103
  if (Array.isArray(options)) {
103
- options = { groups: options }
104
+ options = { groups: options };
104
105
  } else if (options == null) {
105
- options = {}
106
+ options = {};
106
107
  }
107
108
 
108
- const promises = []
109
+ const promises = [];
109
110
 
110
111
  if (runtime.transport) {
111
112
  // create context
112
113
  // todo: create an event context object
113
- const context = contextFactory.create(null, payload, options)
114
- context.eventType = 'broadcast'
115
- context.eventName = eventName
116
- context.eventGroups = options.groups
114
+ const context = contextFactory.create(null, payload, options);
115
+ context.eventType = 'broadcast';
116
+ context.eventName = eventName;
117
+ context.eventGroups = options.groups;
117
118
 
118
119
  // Avoid to broadcast internal events.
119
120
  if (!/^\$/.test(eventName)) {
120
- const endpoints = registry.eventCollection.getAllEndpointsUniqueNodes(eventName, options.groups)
121
+ const endpoints = registry.eventCollection.getAllEndpointsUniqueNodes(eventName, options.groups);
121
122
 
122
123
  endpoints.map(endpoint => {
123
124
  if (endpoint.node.id !== brokerOptions.nodeId) {
124
- const newContext = context.copy()
125
- newContext.setEndpoint(endpoint)
126
- promises.push(runtime.transport.sendEvent(newContext))
125
+ const newContext = context.copy();
126
+ newContext.setEndpoint(endpoint);
127
+ promises.push(runtime.transport.sendEvent(newContext));
127
128
  }
128
- })
129
+ });
129
130
  }
130
131
  }
131
132
 
132
- promises.push(broadcastLocal(eventName, payload, options))
133
- return Promise.all(promises)
134
- }
133
+ promises.push(broadcastLocal(eventName, payload, options));
134
+ return Promise.all(promises);
135
+ };
135
136
 
136
137
  Object.defineProperty(runtime, 'eventBus', {
137
138
  value: {
@@ -139,5 +140,5 @@ exports.initEventbus = (runtime) => {
139
140
  broadcast,
140
141
  broadcastLocal
141
142
  }
142
- })
143
- }
143
+ });
144
+ };
@@ -4,8 +4,8 @@
4
4
  * @typedef {import('../types.js').Transport} Transport
5
5
  */
6
6
 
7
- const { defaultsDeep } = require('@weave-js/utils')
8
- const { createLogger: createDefaultLogger } = require('../logger/index')
7
+ const { defaultsDeep } = require('@weave-js/utils');
8
+ const { createLogger: createDefaultLogger } = require('../logger/index');
9
9
 
10
10
  /**
11
11
  * Init logger
@@ -18,11 +18,11 @@ exports.initLogger = (runtime) => {
18
18
  nodeId: runtime.options.nodeId,
19
19
  moduleName,
20
20
  ...additional
21
- }
21
+ };
22
22
 
23
23
  // custom logger generator function.
24
24
  if (typeof runtime.options.logger === 'function') {
25
- return runtime.options.logger(bindings, runtime.options.logger.level)
25
+ return runtime.options.logger(bindings, runtime.options.logger.level);
26
26
  }
27
27
 
28
28
  // merge log options
@@ -30,18 +30,18 @@ exports.initLogger = (runtime) => {
30
30
  base: {
31
31
  ...bindings
32
32
  }
33
- }, runtime.options.logger)
33
+ }, runtime.options.logger);
34
34
 
35
- return createDefaultLogger(loggerOptions)
36
- }
35
+ return createDefaultLogger(loggerOptions);
36
+ };
37
37
 
38
- const createLogger = (moduleName, service) => loggerFactory(runtime, moduleName, service)
38
+ const createLogger = (moduleName, service) => loggerFactory(runtime, moduleName, service);
39
39
 
40
40
  // create weave default logger
41
- const log = createLogger('WEAVE')
41
+ const log = createLogger('WEAVE');
42
42
 
43
43
  Object.assign(runtime, {
44
44
  createLogger,
45
45
  log
46
- })
47
- }
46
+ });
47
+ };
@@ -1,6 +1,7 @@
1
- const { isPlainObject, isFunction } = require('@weave-js/utils')
2
- const { registerCommonMetrics, updateCommonMetrics } = require('../metrics/common')
3
- const MetricTypes = require('../metrics/types')
1
+ const { isPlainObject, isFunction } = require('@weave-js/utils');
2
+ const { WeaveError } = require('../errors');
3
+ const { registerCommonMetrics, updateCommonMetrics } = require('../metrics/common');
4
+ const MetricTypes = require('../metrics/types');
4
5
 
5
6
  /**
6
7
  * @typedef {import('../types.js').Runtime} Runtime
@@ -12,15 +13,15 @@ const MetricTypes = require('../metrics/types')
12
13
  * @returns {void}
13
14
  */
14
15
  exports.initMetrics = (runtime) => {
15
- const metricOptions = runtime.options.metrics
16
+ const metricOptions = runtime.options.metrics;
16
17
 
17
18
  if (metricOptions.enabled) {
18
- const storage = new Map()
19
+ const storage = new Map();
19
20
 
20
- const log = runtime.createLogger('METRICS')
21
+ const log = runtime.createLogger('METRICS');
21
22
 
22
23
  /** @type {NodeJS.Timeout} */
23
- let commonUpdateTimer
24
+ let commonUpdateTimer;
24
25
 
25
26
  Object.defineProperty(runtime, 'metrics', {
26
27
  value: {
@@ -32,101 +33,101 @@ exports.initMetrics = (runtime) => {
32
33
  // Load adapters
33
34
  if (metricOptions.adapters) {
34
35
  if (!Array.isArray(metricOptions.adapters)) {
35
- runtime.handleError(new Error('Metic adapter needs to be an Array'))
36
+ runtime.handleError(new WeaveError('Metic adapter needs to be an Array.'));
36
37
  }
37
38
 
38
39
  this.adapters = metricOptions.adapters.map(adapter => {
39
- adapter.init(this)
40
- return adapter
41
- })
40
+ adapter.init(this);
41
+ return adapter;
42
+ });
42
43
  }
43
44
  },
44
45
  stop () {
45
46
  if (commonUpdateTimer) {
46
- clearInterval(commonUpdateTimer)
47
+ clearInterval(commonUpdateTimer);
47
48
  }
48
49
 
49
- return Promise.all(this.adapters.map(adapter => adapter.stop()))
50
+ return Promise.all(this.adapters.map(adapter => adapter.stop()));
50
51
  },
51
52
  register (obj) {
52
53
  if (!isPlainObject(obj)) {
53
- runtime.handleError(new Error('Param needs to be an object.'))
54
+ runtime.handleError(new WeaveError('Param needs to be an object.'));
54
55
  }
55
56
 
56
57
  if (!obj.type) {
57
- runtime.handleError(new Error('Type is missing.'))
58
+ runtime.handleError(new WeaveError('Type is missing.'));
58
59
  }
59
60
 
60
61
  if (!obj.name) {
61
- runtime.handleError(new Error('Name is missing.'))
62
+ runtime.handleError(new WeaveError('Name is missing.'));
62
63
  }
63
64
 
64
- const createMetricType = MetricTypes.resolve(obj.type)
65
+ const createMetricType = MetricTypes.resolve(obj.type);
65
66
 
66
67
  if (!createMetricType) {
67
- runtime.handleError(new Error('Unknown metric type.'))
68
+ runtime.handleError(new WeaveError('Unknown metric type.'));
68
69
  }
69
70
 
70
- const type = createMetricType(this, obj)
71
+ const type = createMetricType(this, obj);
71
72
 
72
- this.storage.set(obj.name, type)
73
+ this.storage.set(obj.name, type);
73
74
 
74
- return type
75
+ return type;
75
76
  },
76
77
  increment (name, labels, value = 1, timestamp) {
77
78
  if (!metricOptions.enabled) {
78
- return null
79
+ return null;
79
80
  }
80
81
 
81
- const item = this.storage.get(name)
82
+ const item = this.storage.get(name);
82
83
 
83
84
  if (!item) {
84
- runtime.handleError(new Error('Item not found.'))
85
+ runtime.handleError(new WeaveError('Item not found.'));
85
86
  }
86
87
 
87
- item.increment(labels, value, timestamp)
88
+ item.increment(labels, value, timestamp);
88
89
  },
89
90
  decrement (name, labels, value = 1, timestamp) {
90
91
  if (!metricOptions.enabled) {
91
- return null
92
+ return null;
92
93
  }
93
94
 
94
- const item = this.storage.get(name)
95
+ const item = this.storage.get(name);
95
96
 
96
97
  if (!item) {
97
- runtime.handleError(new Error('Item not found.'))
98
+ runtime.handleError(new WeaveError('Item not found.'));
98
99
  }
99
100
 
100
- item.decrement(labels, value, timestamp)
101
+ item.decrement(labels, value, timestamp);
101
102
  },
102
103
  set (name, value, labels, timestamp) {
103
104
  if (!metricOptions.enabled) {
104
- return null
105
+ return null;
105
106
  }
106
107
 
107
- const item = this.storage.get(name)
108
+ const item = this.storage.get(name);
108
109
 
109
110
  if (!isFunction(item.set)) {
110
- runtime.handleError(new Error('Invalid metric type'))
111
+ runtime.handleError(new WeaveError('Invalid metric type'));
111
112
  }
112
113
 
113
- item.set(value, labels, timestamp)
114
+ item.set(value, labels, timestamp);
114
115
  },
115
116
  timer (name, labels, timestamp) {
116
- let item
117
+ let item;
117
118
  if (name) {
118
- item = this.storage.get(name)
119
+ item = this.storage.get(name);
119
120
  }
120
- const start = process.hrtime()
121
+ const start = process.hrtime();
121
122
 
122
123
  return () => {
123
- const delta = process.hrtime(start)
124
- const duration = (delta[0] + delta[1] / 1e9) * 1000
124
+ const delta = process.hrtime(start);
125
+ const duration = (delta[0] + delta[1] / 1e9) * 1000;
125
126
  if (item) {
126
- item.set(duration, labels, timestamp)
127
+ item.set(duration, labels, timestamp);
127
128
  }
128
- return duration
129
- }
129
+ return duration;
130
+ };
130
131
  // const item = this.storage.get(name)
131
132
  // if (item) {
132
133
 
@@ -134,30 +135,30 @@ exports.initMetrics = (runtime) => {
134
135
  // item.observe(labels, value, timestamp)
135
136
  },
136
137
  getMetric (name) {
137
- const item = this.storage.get(name)
138
+ const item = this.storage.get(name);
138
139
 
139
140
  if (!item) {
140
- runtime.handleError(new Error('Item not found.'))
141
+ runtime.handleError(new WeaveError('Item not found.'));
141
142
  }
142
143
 
143
- return item
144
+ return item;
144
145
  },
145
- list (options = {}) {
146
- const results = []
146
+ list (/* options = {} */) {
147
+ const results = [];
147
148
 
148
149
  this.storage.forEach(metric => {
149
- results.push(metric.toObject())
150
- })
150
+ results.push(metric.toObject());
151
+ });
151
152
 
152
- return results
153
+ return results;
153
154
  }
154
155
  }
155
- })
156
+ });
156
157
 
157
158
  if (metricOptions.enabled && metricOptions.collectCommonMetrics) {
158
- registerCommonMetrics(runtime)
159
- commonUpdateTimer = setInterval(() => updateCommonMetrics(runtime), metricOptions.collectInterval)
160
- commonUpdateTimer.unref()
159
+ registerCommonMetrics(runtime);
160
+ commonUpdateTimer = setInterval(() => updateCommonMetrics(runtime), metricOptions.collectInterval);
161
+ commonUpdateTimer.unref();
161
162
  }
162
163
  }
163
- }
164
+ };
@@ -1,71 +1,71 @@
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
7
  exports.initMiddlewareHandler = (runtime) => {
8
- const list = []
8
+ const list = [];
9
9
 
10
10
  Object.defineProperty(runtime, 'middlewareHandler', {
11
11
  value: {
12
12
  count () {
13
- return list.length
13
+ return list.length;
14
14
  },
15
15
  add (middleware) {
16
16
  if (!middleware) {
17
- return
17
+ return;
18
18
  }
19
19
 
20
20
  if (typeof middleware === 'function') {
21
- middleware = middleware.call(runtime, runtime)
21
+ middleware = middleware.call(runtime, runtime);
22
22
  }
23
23
 
24
- list.push(middleware)
24
+ list.push(middleware);
25
25
  },
26
26
  wrapMethod (methodName, handler, bindTo = runtime) {
27
27
  if (list.length) {
28
- const middlewareList = list.filter(middleware => !!middleware[methodName])
28
+ const middlewareList = list.filter(middleware => !!middleware[methodName]);
29
29
  if (middlewareList.length) {
30
- handler = middlewareList.reduce((next, middleware) => middleware[methodName].call(runtime, next), handler.bind(bindTo))
30
+ handler = middlewareList.reduce((next, middleware) => middleware[methodName].call(runtime, next), handler.bind(bindTo));
31
31
  }
32
32
  }
33
- return handler
33
+ return handler;
34
34
  },
35
35
  wrapHandler (methodName, handler, definition) {
36
36
  if (list.length) {
37
37
  handler = list.reduce((handler, middleware) => {
38
38
  if (typeof middleware[methodName] === 'function') {
39
- return middleware[methodName].call(runtime, handler, definition)
39
+ return middleware[methodName].call(runtime, handler, definition);
40
40
  } else {
41
- return handler
41
+ return handler;
42
42
  }
43
- }, handler)
43
+ }, handler);
44
44
  }
45
- return handler
45
+ return handler;
46
46
  },
47
47
  callHandlersAsync (methodName, args, reverse = false) {
48
- const middlewareList = reverse ? Array.from(list).reverse() : list
48
+ const middlewareList = reverse ? Array.from(list).reverse() : list;
49
49
  const momentousHandlers = middlewareList
50
50
  .filter(middleware => typeof middleware[methodName] === 'function')
51
- .map(middleware => middleware[methodName])
51
+ .map(middleware => middleware[methodName]);
52
52
 
53
53
  if (momentousHandlers.length) {
54
- return momentousHandlers.reduce((p, func) => p.then(() => func.apply(runtime, args)), Promise.resolve())
54
+ return momentousHandlers.reduce((p, func) => p.then(() => func.apply(runtime, args)), Promise.resolve());
55
55
  }
56
56
 
57
- return Promise.resolve()
57
+ return Promise.resolve();
58
58
  },
59
59
  callHandlersSync (methodName, args, reverse = false) {
60
60
  if (list.length) {
61
- const middlewareList = reverse ? Array.from(list).reverse() : list
61
+ const middlewareList = reverse ? Array.from(list).reverse() : list;
62
62
 
63
63
  middlewareList
64
64
  .filter(middleware => typeof middleware[methodName] === 'function')
65
65
  .map(middleware => middleware[methodName])
66
- .forEach(handler => handler.apply(runtime, args))
66
+ .forEach(handler => handler.apply(runtime, args));
67
67
  }
68
68
  }
69
69
  }
70
- })
71
- }
70
+ });
71
+ };
@@ -1,13 +1,13 @@
1
1
  // @ts-check
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
8
  * @typedef {import('../types').Runtime} Runtime
9
9
  */
10
- const { createRegistry } = require('../registry/registry.js')
10
+ const { createRegistry } = require('../registry/registry.js');
11
11
 
12
12
  /**
13
13
  * Injects registy in runtime
@@ -15,11 +15,11 @@ const { createRegistry } = require('../registry/registry.js')
15
15
  * @returns {void}
16
16
  */
17
17
  exports.initRegistry = (runtime) => {
18
- const registry = createRegistry(runtime)
18
+ const registry = createRegistry(runtime);
19
19
 
20
- registry.init(runtime)
20
+ registry.init(runtime);
21
21
 
22
22
  Object.defineProperty(runtime, 'registry', {
23
23
  value: registry
24
- })
25
- }
24
+ });
25
+ };