@weave-js/core 0.9.2 → 0.11.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 (98) hide show
  1. package/lib/ExtendableError.js +33 -0
  2. package/lib/broker/context.js +25 -13
  3. package/lib/broker/{default-options.js → defaultOptions.js} +23 -5
  4. package/lib/broker/index.js +224 -194
  5. package/lib/buildRuntime.js +70 -0
  6. package/lib/cache/base.js +47 -31
  7. package/lib/cache/inMemory.js +140 -0
  8. package/lib/cache/index.js +2 -2
  9. package/lib/cache/lock.js +21 -4
  10. package/lib/constants.js +8 -5
  11. package/lib/errorHandler.js +19 -0
  12. package/lib/errors.js +17 -10
  13. package/lib/helper/{define-action.js → defineAction.js} +3 -1
  14. package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +2 -0
  15. package/lib/helper/{define-service.js → defineService.js} +4 -1
  16. package/lib/index.js +15 -89
  17. package/lib/logger/base.js +3 -3
  18. package/lib/logger/format/asHumanReadable.js +42 -0
  19. package/lib/logger/format/asJson.js +25 -0
  20. package/lib/logger/format/index.js +4 -0
  21. package/lib/logger/index.js +12 -5
  22. package/lib/logger/levels.js +9 -8
  23. package/lib/logger/tools.js +4 -25
  24. package/lib/logger/utils/colorize.js +32 -0
  25. package/lib/logger/utils/format.js +18 -4
  26. package/lib/metrics/constants.js +8 -6
  27. package/lib/metrics/types/gauge.js +1 -1
  28. package/lib/middlewares/action-hooks/index.js +62 -14
  29. package/lib/middlewares/bulkhead/index.js +5 -4
  30. package/lib/middlewares/cache/index.js +80 -0
  31. package/lib/middlewares/circuit-breaker/index.js +41 -16
  32. package/lib/middlewares/context-tracker/index.js +3 -3
  33. package/lib/middlewares/error-handler/index.js +3 -3
  34. package/lib/middlewares/index.js +2 -1
  35. package/lib/middlewares/metrics/getMiddlewareWrapper.js +2 -2
  36. package/lib/middlewares/metrics/index.js +2 -2
  37. package/lib/middlewares/retry/index.js +11 -10
  38. package/lib/middlewares/timeout/index.js +5 -5
  39. package/lib/middlewares/tracing/index.js +27 -14
  40. package/lib/middlewares/tracing/tags.js +43 -0
  41. package/lib/middlewares/validator/index.js +6 -6
  42. package/lib/registry/{action-endpoint.js → actionEndpoint.js} +0 -0
  43. package/lib/registry/collections/{action-collection.js → actionCollection.js} +2 -2
  44. package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +19 -3
  45. package/lib/registry/collections/{event-collection.js → eventCollection.js} +15 -9
  46. package/lib/registry/collections/{node-collection.js → nodeCollection.js} +1 -1
  47. package/lib/registry/collections/{service-collection.js → serviceCollection.js} +2 -2
  48. package/lib/registry/{event-endpoint.js → eventEndpoint.js} +1 -1
  49. package/lib/registry/node.js +1 -1
  50. package/lib/registry/registry.js +10 -23
  51. package/lib/registry/service/service.js +2 -2
  52. package/lib/registry/{service-item.js → serviceItem.js} +1 -1
  53. package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +3 -3
  54. package/lib/runtime/{init-cache.js → initCache.js} +0 -0
  55. package/lib/runtime/{init-context-factory.js → initContextFactory.js} +2 -2
  56. package/lib/runtime/{init-eventbus.js → initEventbus.js} +11 -10
  57. package/lib/runtime/{init-logger.js → initLogger.js} +0 -0
  58. package/lib/runtime/{init-metrics.js → initMetrics.js} +11 -10
  59. package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +1 -1
  60. package/lib/runtime/{init-registry.js → initRegistry.js} +1 -1
  61. package/lib/runtime/{init-service-manager.js → initServiceManager.js} +0 -0
  62. package/lib/runtime/{init-tracing.js → initTracing.js} +2 -1
  63. package/lib/runtime/{init-transport.js → initTransport.js} +1 -1
  64. package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +0 -0
  65. package/lib/runtime/{init-validator.js → initValidator.js} +1 -1
  66. package/lib/tracing/collectors/base.js +11 -19
  67. package/lib/tracing/collectors/event.js +2 -2
  68. package/lib/tracing/collectors/index.js +2 -2
  69. package/lib/tracing/span.js +8 -6
  70. package/lib/transport/InboundTransformStream.js +75 -0
  71. package/lib/transport/adapters/{adapter-base.js → adapteBase.js} +16 -1
  72. package/lib/transport/adapters/adapters.js +1 -1
  73. package/lib/transport/adapters/dummy/index.js +3 -3
  74. package/lib/transport/adapters/index.js +1 -1
  75. package/lib/transport/adapters/tcp/discovery/index.js +45 -31
  76. package/lib/transport/adapters/tcp/index.js +20 -13
  77. package/lib/transport/adapters/tcp/tcpReader.js +1 -0
  78. package/lib/transport/adapters/tcp/tcpWriteStream.js +1 -1
  79. package/lib/transport/adapters/tcp/tcpWriter.js +2 -2
  80. package/lib/transport/createMessage.js +11 -0
  81. package/lib/transport/{transport-factory.js → createTransport.js} +234 -173
  82. package/lib/transport/errorPayloadFactory.js +18 -0
  83. package/lib/transport/{message-handlers.js → messageHandlers.js} +93 -24
  84. package/lib/transport/messageTypes.js +23 -0
  85. package/lib/types.js +66 -66
  86. package/lib/utils/index.js +0 -1
  87. package/lib/utils/options.js +4 -2
  88. package/lib/utils/{restore-error.js → restoreError.js} +1 -1
  89. package/package.json +6 -12
  90. package/CHANGELOG.md +0 -1293
  91. package/lib/cache/memory.js +0 -91
  92. package/lib/middlewares/tracing/buildTags.js +0 -17
  93. package/lib/new-errors.js +0 -25
  94. package/lib/tracing/collectors/console.js +0 -34
  95. package/lib/tracing/index.js +0 -3
  96. package/lib/tracing/tracer.js +0 -64
  97. package/lib/transport/message-types.js +0 -59
  98. package/lib/transport/message.js +0 -16
@@ -2,7 +2,7 @@
2
2
  /*
3
3
  * Author: Kevin Ries (kevin@fachw3rk.de)
4
4
  * -----
5
- * Copyright 2020 Fachwerk
5
+ * Copyright 2021 Fachwerk
6
6
  */
7
7
 
8
8
  /**
@@ -21,11 +21,11 @@
21
21
  const { safeCopy } = require('@weave-js/utils')
22
22
 
23
23
  // own packages
24
- const { createNodeCollection } = require('./collections/node-collection')
25
- const { createServiceCollection } = require('./collections/service-collection')
26
- const { createActionCollection } = require('./collections/action-collection')
27
- const { createEventCollection } = require('./collections/event-collection')
28
- const { createActionEndpoint } = require('./action-endpoint')
24
+ const { createNodeCollection } = require('./collections/nodeCollection')
25
+ const { createServiceCollection } = require('./collections/serviceCollection')
26
+ const { createActionCollection } = require('./collections/actionCollection')
27
+ const { createEventCollection } = require('./collections/eventCollection')
28
+ const { createActionEndpoint } = require('./actionEndpoint')
29
29
  const { createNode } = require('./node')
30
30
  const { WeaveServiceNotFoundError, WeaveServiceNotAvailableError } = require('../errors')
31
31
 
@@ -47,24 +47,12 @@ exports.createRegistry = (runtime) => {
47
47
  log: runtime.createLogger('REGISTRY'),
48
48
  /**
49
49
  * Initialize the registry
50
- * @param {Broker} runtime Broker instance
50
+ * @param {Runtime} runtime Runtime
51
51
  * @param {MiddlewareHandler} middlewareHandler Middleware handler
52
52
  * @param {ServiceChangedDelegate} serviceChanged Service changed delegate
53
53
  * @returns {void}
54
54
  */
55
55
  init (runtime) {
56
- /**
57
- * @type {Broker}
58
- */
59
-
60
- /**
61
- * @type {MiddlewareHandler}
62
- */
63
-
64
- /**
65
- * @type {ServiceChangedDelegate}
66
- */
67
-
68
56
  // init collections
69
57
  this.nodeCollection = createNodeCollection(this)
70
58
  this.serviceCollection = createServiceCollection(this)
@@ -141,7 +129,6 @@ exports.createRegistry = (runtime) => {
141
129
  }
142
130
 
143
131
  if (oldActions) {
144
- // this.deregisterAction()
145
132
  Object.keys(oldActions).forEach(actionName => {
146
133
  if (!service.actions[actionName]) {
147
134
  this.actionCollection.remove(actionName, node)
@@ -201,7 +188,7 @@ exports.createRegistry = (runtime) => {
201
188
  if (node.isLocal) {
202
189
  action.handler = middlewareHandler.wrapHandler('localAction', action.handler, action)// this.onRegisterLocalAction(action)
203
190
  } else {
204
- action.handler = middlewareHandler.wrapHandler('remoteAction', runtime.transport.request.bind(runtime.transport), action)// this.onRegisterRemoteAction(action)
191
+ action.handler = middlewareHandler.wrapHandler('remoteAction', runtime.transport.sendRequest.bind(runtime.transport), action)// this.onRegisterRemoteAction(action)
205
192
  }
206
193
 
207
194
  this.actionCollection.add(node, service, action)
@@ -369,9 +356,9 @@ exports.createRegistry = (runtime) => {
369
356
  }
370
357
 
371
358
  // todo: Handle multiple nodes with the same ID.
372
- const updateNesesary = node.update(payload, isReconnected)
359
+ const updateNecessary = node.update(payload, isReconnected)
373
360
 
374
- if (updateNesesary && node.services) {
361
+ if (updateNecessary && node.services) {
375
362
  this.registerRemoteServices(node, node.services)
376
363
  }
377
364
 
@@ -9,7 +9,7 @@ const { WeaveError } = require('../../errors')
9
9
  const { parseAction } = require('./parseAction')
10
10
  const { parseEvent } = require('./parseEvent')
11
11
  const { reduceMixins } = require('./reduceMixins')
12
- const { createEventEndpoint } = require('../event-endpoint')
12
+ const { createEventEndpoint } = require('../eventEndpoint')
13
13
  /**
14
14
  * Service factory
15
15
  * @param {Runtime} runtime Broker instance
@@ -133,7 +133,7 @@ exports.createServiceFromSchema = (runtime, schema) => {
133
133
  context = runtime.contextFactory.create(endpoint, data, options || {})
134
134
  }
135
135
 
136
- return wrappedAction(context)
136
+ return wrappedAction(context, { service, runtime, errors: {}})
137
137
  }
138
138
  })
139
139
  }
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Author: Kevin Ries (kevin@fachw3rk.de)
3
3
  * -----
4
- * Copyright 2020 Fachwerk
4
+ * Copyright 2021 Fachwerk
5
5
  */
6
6
  'use strict'
7
7
 
@@ -1,4 +1,4 @@
1
- const { WeaveError } = require('../errors')
1
+ const errors = require('../errors')
2
2
 
3
3
  exports.initActionInvoker = (runtime) => {
4
4
  const { registry, contextFactory, log, handleError } = runtime
@@ -36,7 +36,7 @@ exports.initActionInvoker = (runtime) => {
36
36
  log.debug({ action: context.action.name, nodeId, requestId: context.requestId }, 'Call action on remote node.')
37
37
  }
38
38
 
39
- const p = action.handler(context, endpoint.service, runtime)
39
+ const p = action.handler(context, { service: context.action.service, runtime, errors })
40
40
  p.context = context
41
41
 
42
42
  return p
@@ -51,7 +51,7 @@ exports.initActionInvoker = (runtime) => {
51
51
  if (Array.isArray(actions)) {
52
52
  return Promise.all(actions.map(item => call(item.actionName, item.params, item.options)))
53
53
  } else {
54
- return Promise.reject(new WeaveError('Actions need to be an Array'))
54
+ return Promise.reject(new errors.WeaveError('Actions need to be an Array'))
55
55
  }
56
56
  }
57
57
 
File without changes
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Author: Kevin Ries (kevin@fachw3rk.de)
3
3
  * -----
4
- * Copyright 2020 Fachwerk
4
+ * Copyright 2021 Fachwerk
5
5
  */
6
6
  'use strict'
7
7
 
@@ -12,7 +12,7 @@ const { createContext } = require('../broker/context')
12
12
  * @typedef {import('../types').Context} Context
13
13
  * @typedef {import('../types').Endpoint} Endpoint
14
14
  * @typedef {import('../types').ActionOptions} ActionOptions
15
- */
15
+ */
16
16
 
17
17
  /**
18
18
  * Init context factory.
@@ -15,7 +15,11 @@ exports.initEventbus = (runtime) => {
15
15
  options = {}
16
16
  }
17
17
 
18
- const promises = []
18
+ // Emit local events
19
+ if (/^\$/.test(eventName)) {
20
+ bus.emit(eventName, payload)
21
+ }
22
+
19
23
  // todo: create an event context object
20
24
  const context = contextFactory.create(null, payload, options)
21
25
 
@@ -23,23 +27,20 @@ exports.initEventbus = (runtime) => {
23
27
  context.eventName = eventName
24
28
  context.eventGroups = options.groups
25
29
 
26
- // Emit system events
27
- if (/^\$/.test(eventName)) {
28
- bus.emit(eventName, payload)
29
- }
30
-
31
30
  const endpoints = registry.eventCollection.getBalancedEndpoints(eventName, options.groups)
32
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
+ context.setEndpoint(endpoint)
38
39
  promises.push(endpoint.action.handler(context))
39
40
  } else {
40
41
  const e = groupedEndpoints[endpoint.node.id]
41
42
  if (e) {
42
- e.push(groupName)
43
+ e.groups.push(groupName)
43
44
  } else {
44
45
  groupedEndpoints[endpoint.node.id] = {
45
46
  endpoint,
@@ -50,7 +51,7 @@ exports.initEventbus = (runtime) => {
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 => {
@@ -69,7 +70,7 @@ exports.initEventbus = (runtime) => {
69
70
  * @param {String} eventName Name of the event
70
71
  * @param {any} payload Payload
71
72
  * @param {*} [options=null] Options
72
- * @returns {void}
73
+ * @returns {Promise<any>} Promise
73
74
  */
74
75
  const broadcastLocal = (eventName, payload, options) => {
75
76
  // If the given group is no array - wrap it.
@@ -96,7 +97,7 @@ exports.initEventbus = (runtime) => {
96
97
  * @param {String} eventName Name of the event
97
98
  * @param {any} payload Payload
98
99
  * @param {*} [options=null] Groups
99
- * @returns {void}
100
+ * @returns {Promise<any>} Promise
100
101
  */
101
102
  const broadcast = (eventName, payload, options) => {
102
103
  if (Array.isArray(options)) {
File without changes
@@ -1,4 +1,5 @@
1
1
  const { isPlainObject, isFunction } = require('@weave-js/utils')
2
+ const { WeaveError } = require('../errors')
2
3
  const { registerCommonMetrics, updateCommonMetrics } = require('../metrics/common')
3
4
  const MetricTypes = require('../metrics/types')
4
5
 
@@ -32,7 +33,7 @@ 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 => {
@@ -50,21 +51,21 @@ exports.initMetrics = (runtime) => {
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
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
71
  const type = createMetricType(this, obj)
@@ -81,7 +82,7 @@ exports.initMetrics = (runtime) => {
81
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
88
  item.increment(labels, value, timestamp)
@@ -94,7 +95,7 @@ exports.initMetrics = (runtime) => {
94
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
101
  item.decrement(labels, value, timestamp)
@@ -107,7 +108,7 @@ exports.initMetrics = (runtime) => {
107
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
114
  item.set(value, labels, timestamp)
@@ -137,12 +138,12 @@ exports.initMetrics = (runtime) => {
137
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
144
  return item
144
145
  },
145
- list (options = {}) {
146
+ list (/* options = {} */) {
146
147
  const results = []
147
148
 
148
149
  this.storage.forEach(metric => {
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Author: Kevin Ries (kevin@fachw3rk.de)
3
3
  * -----
4
- * Copyright 2020 Fachwerk
4
+ * Copyright 2021 Fachwerk
5
5
  */
6
6
 
7
7
  exports.initMiddlewareHandler = (runtime) => {
@@ -2,7 +2,7 @@
2
2
  /*
3
3
  * Author: Kevin Ries (kevin@fachw3rk.de)
4
4
  * -----
5
- * Copyright 2020 Fachwerk
5
+ * Copyright 2021 Fachwerk
6
6
  */
7
7
  /**
8
8
  * @typedef {import('../types').Runtime} Runtime
@@ -9,6 +9,8 @@ exports.initTracer = (runtime) => {
9
9
 
10
10
  Object.defineProperty(runtime, 'tracer', {
11
11
  value: {
12
+ runtime,
13
+ options,
12
14
  log,
13
15
  async stop () {
14
16
  if (collectors.length > 0) {
@@ -55,7 +57,6 @@ exports.initTracer = (runtime) => {
55
57
  collectors = options.collectors
56
58
  .map(entry => {
57
59
  const initCollector = resolveCollector(runtime, entry, this)
58
- // collector.init(this)
59
60
  initCollector.init(runtime)
60
61
  return initCollector
61
62
  })
@@ -2,7 +2,7 @@
2
2
  * @typedef {import('../types').TransportAdapter} TransportAdapter
3
3
  **/
4
4
 
5
- const { createTransport } = require('../transport/transport-factory')
5
+ const { createTransport } = require('../transport/createTransport')
6
6
  const TransportAdapters = require('../transport/adapters')
7
7
 
8
8
  exports.initTransport = (runtime) => {
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Author: Kevin Ries (kevin@fachw3rk.de)
3
3
  * -----
4
- * Copyright 2020 Fachwerk
4
+ * Copyright 2021 Fachwerk
5
5
  */
6
6
 
7
7
  /**
@@ -1,26 +1,25 @@
1
- const { isObject } = require('@weave-js/utils')
2
- const { WeaveError } = require('../../errors')
1
+ const { isObject, pick } = require('@weave-js/utils')
3
2
 
4
- exports.createBaseTracingCollector = (options) => {
3
+ exports.createBaseTracingCollector = (runtime) => {
5
4
  const baseTracingCollector = Object.create(null)
6
5
 
7
- baseTracingCollector.options = options
6
+ baseTracingCollector.options = runtime.tracer.options
8
7
 
9
- baseTracingCollector.init = (runtime, tracer) => {
8
+ baseTracingCollector.init = (runtime) => {
10
9
  baseTracingCollector.runtime = runtime
11
- baseTracingCollector.tracer = tracer
10
+ baseTracingCollector.tracer = runtime.tracer
12
11
  }
13
12
 
14
13
  baseTracingCollector.startedSpan = () => {
15
- throw new WeaveError('not implemented.')
14
+ // throw new WeaveError('not implemented.')
16
15
  }
17
16
 
18
17
  baseTracingCollector.finishedSpan = () => {
19
- throw new WeaveError('not implemented.')
18
+ // throw new WeaveError('not implemented.')
20
19
  }
21
20
 
22
21
  baseTracingCollector.stop = () => {
23
- throw new WeaveError('not implemented.')
22
+ // throw new WeaveError('not implemented.')
24
23
  }
25
24
 
26
25
  baseTracingCollector.flattenTags = (obj, convertToString = false, path = '') => {
@@ -42,18 +41,11 @@ exports.createBaseTracingCollector = (options) => {
42
41
  }, {})
43
42
  }
44
43
 
45
- baseTracingCollector.getErrorFields = (error, ...fields) => {
46
- if (!error) {
44
+ baseTracingCollector.getErrorFields = (err, fields) => {
45
+ if (!err) {
47
46
  return null
48
47
  }
49
-
50
- const picked = {}
51
-
52
- for (const field of fields) {
53
- picked[field] = error[field]
54
- }
55
-
56
- return picked
48
+ return pick(err, fields)
57
49
  }
58
50
 
59
51
  return baseTracingCollector
@@ -13,7 +13,7 @@ const mergeDefaultOptions = (options) => {
13
13
  module.exports = (options) => (runtime, tracer) => {
14
14
  options = mergeDefaultOptions(options)
15
15
 
16
- const exporter = createBaseTracingCollector(options)
16
+ const exporter = createBaseTracingCollector(runtime)
17
17
 
18
18
  exporter.init(runtime, tracer)
19
19
 
@@ -28,7 +28,7 @@ module.exports = (options) => (runtime, tracer) => {
28
28
  const newSpan = Object.assign({}, span)
29
29
 
30
30
  if (newSpan.error) {
31
- newSpan.error = exporter.getErrorFields(newSpan.error)
31
+ newSpan.error = exporter.getErrorFields(newSpan.error, exporter.options.errors.fields)
32
32
  }
33
33
 
34
34
  return newSpan
@@ -11,14 +11,14 @@ const getByName = name => {
11
11
  return collectors[n]
12
12
  }
13
13
 
14
- exports.resolveCollector = (runtime, collector, tracer) => {
14
+ exports.resolveCollector = (runtime, collector) => {
15
15
  let CollectorClass
16
16
  if (typeof collector === 'string') {
17
17
  CollectorClass = getByName(collector)
18
18
  }
19
19
 
20
20
  if (isFunction(collector)) {
21
- return collector(runtime, tracer)
21
+ return collector(runtime)
22
22
  }
23
23
 
24
24
  if (typeof collector === 'object') {
@@ -3,8 +3,8 @@ const hrTime = require('./time')
3
3
  exports.createSpan = (tracer, name, options) => {
4
4
  const span = Object.assign({}, {
5
5
  name,
6
- id: options.id || tracer.broker.getUUID(),
7
- traceId: options.traceId || tracer.broker.getUUID(),
6
+ id: options.id || tracer.runtime.generateUUID(),
7
+ traceId: options.traceId || tracer.runtime.generateUUID(),
8
8
  parentId: options.parentId,
9
9
  type: options.type,
10
10
  sampled: options.sampled || tracer.shouldSample(),
@@ -12,10 +12,12 @@ exports.createSpan = (tracer, name, options) => {
12
12
  tags: {}
13
13
  })
14
14
 
15
- span.service = {
16
- name: options.service.name,
17
- version: options.service.version,
18
- fullyQualifiedName: options.service.fullyQualifiedName
15
+ if (options.service) {
16
+ span.service = {
17
+ name: options.service.name,
18
+ version: options.service.version,
19
+ fullyQualifiedName: options.service.fullyQualifiedName
20
+ }
19
21
  }
20
22
 
21
23
  span.addTags = (tags) => {
@@ -0,0 +1,75 @@
1
+ const { Transform } = require('stream')
2
+
3
+ const pushWithBackpressure = (stream, chunks, encoding, callback = null, $index = 0) => {
4
+ if (!(stream instanceof Transform)) {
5
+ throw new TypeError('Argument "stream" must be an instance of Duplex')
6
+ }
7
+
8
+ chunks = [].concat(chunks).filter(x => x !== undefined)
9
+
10
+ if (typeof encoding === 'function') {
11
+ callback = encoding
12
+ encoding = undefined
13
+ }
14
+
15
+ if ($index >= chunks.length) {
16
+ if (typeof callback === 'function') {
17
+ callback()
18
+ }
19
+ return stream
20
+ } else if (!stream.push(chunks[$index], ...([encoding].filter(Boolean)))) {
21
+ stream.emit('backpressure', {
22
+ sender: stream.sender,
23
+ requestId: stream.requestId
24
+ })
25
+
26
+ const pipedStreams = [].concat(
27
+ (stream._readableState || {}).pipes || stream
28
+ ).filter(Boolean)
29
+
30
+ let listenerCalled = false
31
+
32
+ const drainListener = () => {
33
+ stream.emit('resume_backpressure', {
34
+ sender: stream.sender,
35
+ requestId: stream.requestId
36
+ })
37
+
38
+ if (listenerCalled) {
39
+ return
40
+ }
41
+
42
+ listenerCalled = true
43
+
44
+ for (const stream of pipedStreams) {
45
+ stream.removeListener('drain', drainListener)
46
+ }
47
+
48
+ pushWithBackpressure(stream, chunks, encoding, callback, $index + 1)
49
+ }
50
+
51
+ for (const stream of pipedStreams) {
52
+ stream.once('drain', drainListener)
53
+ }
54
+
55
+ return stream
56
+ }
57
+ return pushWithBackpressure(stream, chunks, encoding, callback, $index + 1)
58
+ }
59
+ class InboundTransformStream extends Transform {
60
+ constructor (sender, requestId, options) {
61
+ super(options)
62
+ this.sender = sender
63
+ this.requestId = requestId
64
+ }
65
+
66
+ _transform (chunk, encoding, callback) {
67
+ pushWithBackpressure(this, chunk, encoding, callback)
68
+ }
69
+
70
+ _flush (callback) {
71
+ return callback()
72
+ }
73
+ }
74
+
75
+ module.exports = { InboundTransformStream }
@@ -1,11 +1,26 @@
1
1
  /*
2
2
  * Author: Kevin Ries (kevin@fachw3rk.de)
3
3
  * -----
4
- * Copyright 2020 Fachwerk
4
+ * Copyright 2021 Fachwerk
5
5
  */
6
6
 
7
7
  const EventEmitter = require('events').EventEmitter
8
8
 
9
+ /**
10
+ * @typedef {Object} AdapterBase
11
+ * @property {string} name Name
12
+ * @property {EventEmitter} bus Event bus.
13
+ * @property {function():Promise<any>} afterInit After init callback.
14
+ * @property {boolean} isConnected Is connected state.
15
+ * @property {number} interruptCounter Interruption counter.
16
+ * @property {number} repeatAttemptCounter Repeat attempt counter
17
+ * @property {function(Object, Object, Object):Promise<any>} init Repeat attempt counter
18
+ */
19
+
20
+ /**
21
+ * Create a adapter base object.
22
+ * @returns {AdapterBase} Adapter base object
23
+ */
9
24
  const createTransportBase = () => {
10
25
  let prefix = 'weave'
11
26
 
@@ -4,6 +4,6 @@
4
4
  * Copyright 2019 Fachwerk
5
5
  */
6
6
 
7
- exports.BaseAdapter = require('./adapter-base')
7
+ exports.BaseAdapter = require('./adapteBase')
8
8
  exports.Dummy = require('./dummy')
9
9
  exports.TCP = require('./tcp')
@@ -1,10 +1,10 @@
1
1
  /*
2
2
  * Author: Kevin Ries (kevin@fachw3rk.de)
3
3
  * -----
4
- * Copyright 2020 Fachwerk
4
+ * Copyright 2021 Fachwerk
5
5
  */
6
6
 
7
- const TransportBase = require('../adapter-base')
7
+ const TransportBase = require('../adapteBase')
8
8
  const EventEmitter = require('eventemitter2').EventEmitter2
9
9
 
10
10
  // create a global eventbus to pass messages between weave service brokers.
@@ -18,7 +18,7 @@ const DummyTransportAdapter = (adapterOptions) => {
18
18
 
19
19
  return Object.assign(TransportBase(adapterOptions), {
20
20
  name: 'Dummy',
21
- connect (isTryReconnect = false) {
21
+ connect () {
22
22
  this.bus.emit('$adapter.connected', false)
23
23
  this.log.info('Dummy transport client connected.')
24
24
  return Promise.resolve()
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Author: Kevin Ries (kevin@fachw3rk.de)
3
3
  * -----
4
- * Copyright 2020 Fachwerk
4
+ * Copyright 2021 Fachwerk
5
5
  */
6
6
 
7
7
  const { WeaveBrokerOptionsError } = require('../../errors')