@weave-js/core 0.9.4 → 0.11.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.
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} +25 -6
  4. package/lib/broker/index.js +224 -194
  5. package/lib/buildRuntime.js +70 -0
  6. package/lib/cache/adapters/base.js +143 -0
  7. package/lib/cache/adapters/inMemory.js +154 -0
  8. package/lib/cache/adapters/inMemoryLru.js +140 -0
  9. package/lib/cache/adapters/index.js +46 -0
  10. package/lib/cache/base.js +49 -44
  11. package/lib/cache/lock.js +21 -4
  12. package/lib/constants.js +8 -5
  13. package/lib/errorHandler.js +19 -0
  14. package/lib/errors.js +17 -10
  15. package/lib/helper/{define-action.js → defineAction.js} +3 -1
  16. package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +2 -0
  17. package/lib/helper/{define-service.js → defineService.js} +4 -1
  18. package/lib/index.js +17 -92
  19. package/lib/logger/format/asHumanReadable.js +4 -3
  20. package/lib/logger/index.js +7 -1
  21. package/lib/logger/levels.js +8 -7
  22. package/lib/logger/tools.js +4 -0
  23. package/lib/logger/utils/format.js +18 -4
  24. package/lib/metrics/constants.js +8 -6
  25. package/lib/metrics/types/gauge.js +1 -1
  26. package/lib/middlewares/action-hooks/index.js +62 -14
  27. package/lib/middlewares/bulkhead/index.js +5 -4
  28. package/lib/middlewares/cache/index.js +106 -0
  29. package/lib/middlewares/circuit-breaker/index.js +41 -16
  30. package/lib/middlewares/context-tracker/index.js +3 -3
  31. package/lib/middlewares/error-handler/index.js +3 -3
  32. package/lib/middlewares/index.js +2 -1
  33. package/lib/middlewares/metrics/getMiddlewareWrapper.js +2 -2
  34. package/lib/middlewares/metrics/index.js +2 -2
  35. package/lib/middlewares/retry/index.js +11 -10
  36. package/lib/middlewares/timeout/index.js +5 -5
  37. package/lib/middlewares/tracing/index.js +27 -14
  38. package/lib/middlewares/tracing/tags.js +43 -0
  39. package/lib/middlewares/validator/index.js +6 -6
  40. package/lib/registry/{action-endpoint.js → actionEndpoint.js} +0 -0
  41. package/lib/registry/collections/{action-collection.js → actionCollection.js} +2 -2
  42. package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +19 -3
  43. package/lib/registry/collections/{event-collection.js → eventCollection.js} +15 -9
  44. package/lib/registry/collections/{node-collection.js → nodeCollection.js} +1 -1
  45. package/lib/registry/collections/{service-collection.js → serviceCollection.js} +2 -2
  46. package/lib/registry/{event-endpoint.js → eventEndpoint.js} +1 -1
  47. package/lib/registry/node.js +1 -1
  48. package/lib/registry/registry.js +19 -38
  49. package/lib/registry/service/service.js +2 -2
  50. package/lib/registry/{service-item.js → serviceItem.js} +1 -1
  51. package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +3 -3
  52. package/lib/runtime/initCache.js +19 -0
  53. package/lib/runtime/{init-context-factory.js → initContextFactory.js} +2 -2
  54. package/lib/runtime/{init-eventbus.js → initEventbus.js} +11 -10
  55. package/lib/runtime/{init-logger.js → initLogger.js} +0 -0
  56. package/lib/runtime/{init-metrics.js → initMetrics.js} +11 -10
  57. package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +1 -1
  58. package/lib/runtime/{init-registry.js → initRegistry.js} +1 -1
  59. package/lib/runtime/{init-service-manager.js → initServiceManager.js} +0 -0
  60. package/lib/runtime/{init-tracing.js → initTracing.js} +2 -1
  61. package/lib/runtime/{init-transport.js → initTransport.js} +0 -0
  62. package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +0 -0
  63. package/lib/runtime/{init-validator.js → initValidator.js} +1 -1
  64. package/lib/tracing/collectors/base.js +11 -19
  65. package/lib/tracing/collectors/event.js +2 -2
  66. package/lib/tracing/collectors/index.js +2 -2
  67. package/lib/tracing/span.js +8 -6
  68. package/lib/transport/InboundTransformStream.js +75 -0
  69. package/lib/transport/adapters/{adapter-base.js → adapteBase.js} +16 -1
  70. package/lib/transport/adapters/adapters.js +1 -1
  71. package/lib/transport/adapters/dummy/index.js +3 -3
  72. package/lib/transport/adapters/index.js +1 -1
  73. package/lib/transport/adapters/tcp/discovery/index.js +45 -31
  74. package/lib/transport/adapters/tcp/index.js +20 -13
  75. package/lib/transport/adapters/tcp/tcpReader.js +1 -0
  76. package/lib/transport/adapters/tcp/tcpWriteStream.js +1 -1
  77. package/lib/transport/adapters/tcp/tcpWriter.js +2 -2
  78. package/lib/transport/createMessage.js +11 -0
  79. package/lib/transport/createTransport.js +215 -172
  80. package/lib/transport/errorPayloadFactory.js +18 -0
  81. package/lib/transport/{message-handlers.js → messageHandlers.js} +92 -23
  82. package/lib/transport/messageTypes.js +23 -0
  83. package/lib/types.js +86 -75
  84. package/lib/utils/index.js +1 -1
  85. package/lib/utils/options.js +4 -2
  86. package/lib/utils/{restore-error.js → restoreError.js} +1 -1
  87. package/package.json +6 -12
  88. package/CHANGELOG.md +0 -1317
  89. package/lib/cache/index.js +0 -48
  90. package/lib/cache/memory.js +0 -91
  91. package/lib/middlewares/tracing/buildTags.js +0 -17
  92. package/lib/new-errors.js +0 -25
  93. package/lib/runtime/init-cache.js +0 -10
  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
@@ -1,48 +0,0 @@
1
- /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
3
- * -----
4
- * Copyright 2020 Fachwerk
5
- */
6
-
7
- const { isString, isFunction } = require('@weave-js/utils')
8
- const { WeaveBrokerOptionsError } = require('../errors')
9
-
10
- const adapters = {
11
- Memory: require('./memory')
12
- }
13
-
14
- exports.adapters = adapters
15
-
16
- exports.createCacheBase = require('./base').createCacheBase
17
-
18
- exports.resolve = (cacheOptions) => {
19
- const getByName = name => {
20
- if (!name) {
21
- return null
22
- }
23
-
24
- const n = Object.keys(adapters).find(n => n.toLowerCase() === name.toLowerCase())
25
- if (n) {
26
- return adapters[n]
27
- }
28
- }
29
-
30
- let cacheFactory
31
-
32
- if (cacheOptions === true) {
33
- cacheFactory = this.adapters.Memory
34
- } else if (isString(cacheOptions)) {
35
- const cache = getByName(cacheOptions)
36
-
37
- if (cache) {
38
- cacheFactory = cache
39
- } else {
40
- throw new WeaveBrokerOptionsError(`Unknown cache type "${cacheOptions}"`)
41
- }
42
- } else if (isFunction(cacheOptions)) {
43
- cacheFactory = cacheOptions
44
- }
45
- if (cacheFactory) {
46
- return cacheFactory
47
- }
48
- }
@@ -1,91 +0,0 @@
1
- /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
3
- * -----
4
- * Copyright 2020 Fachwerk
5
- */
6
-
7
- const { match } = require('@weave-js/utils')
8
- const { createCacheBase } = require('./base')
9
-
10
- const makeMemoryCache = (runtime, options = {}) => {
11
- const base = createCacheBase(runtime, options)
12
- const storage = new Map()
13
- const name = 'Memory'
14
-
15
- const timer = setInterval(() => {
16
- checkTtl()
17
- }, 3000)
18
-
19
- timer.unref()
20
-
21
- // if a new broker gets connected, we need to clear the cache
22
- runtime.bus.on('$transport.connected', () => {
23
- base.log.verbose('Transpot adapter connected. Cache is cleared.')
24
- cache.clear()
25
- })
26
-
27
- const checkTtl = () => {
28
- const now = Date.now()
29
-
30
- storage.forEach((item, hashKey) => {
31
- if (item.expire && item.expire < now) {
32
- cache.log.debug(`Delete ${hashKey}`)
33
- storage.delete(hashKey)
34
- }
35
- })
36
- }
37
-
38
- const cache = Object.assign(base, {
39
- name,
40
- get (cacheKey) {
41
- const item = storage.get(cacheKey)
42
-
43
- if (item) {
44
- if (options.ttl) {
45
- item.expire = Date.now()// + options_.ttl
46
- }
47
-
48
- this.log.debug(`Get ${cacheKey}`)
49
-
50
- return Promise.resolve(item.data)
51
- }
52
- return Promise.resolve(null)
53
- },
54
- set (hashKey, data, ttl) {
55
- if (ttl == null) {
56
- ttl = options.ttl
57
- }
58
-
59
- storage.set(hashKey, {
60
- data,
61
- expire: ttl ? Date.now() + ttl : null
62
- })
63
-
64
- this.log.debug(`Set ${hashKey}`)
65
-
66
- return Promise.resolve(data)
67
- },
68
- remove (hashKey) {
69
- storage.delete(hashKey)
70
- this.log.debug(`Delete ${hashKey}`)
71
-
72
- return Promise.resolve()
73
- },
74
- clear (pattern = '**') {
75
- storage.forEach((_, key) => {
76
- if (match(key, pattern)) {
77
- this.log.debug(`Delete ${key}`)
78
- this.remove(key)
79
- }
80
- })
81
- return Promise.resolve()
82
- },
83
- stop () {
84
- clearInterval(timer)
85
- }
86
- })
87
-
88
- return cache
89
- }
90
-
91
- module.exports = makeMemoryCache
@@ -1,17 +0,0 @@
1
- /**
2
- * @typedef {import("../../types.js").Context} Context
3
- */
4
-
5
- /**
6
- * Build span tags object
7
- * @param {Context} context - Context
8
- * @returns {object} Tags
9
- */
10
- module.exports.buildTags = (context) => {
11
- return {
12
- requestLevel: context.level,
13
- action: context.action ? { name: context.action.name, shortName: context.action.shortName } : null,
14
- isRemoteCall: !!context.callerNodeId,
15
- nodeId: context.nodeId
16
- }
17
- }
package/lib/new-errors.js DELETED
@@ -1,25 +0,0 @@
1
- /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
3
- * -----
4
- * Copyright 2020 Fachwerk
5
- */
6
-
7
- const errors = require('@weave-js/errors')
8
-
9
- class RetrieableError extends Error {
10
- constructor () {
11
- super(...arguments)
12
- this.retryable = true
13
- }
14
- }
15
-
16
- module.exports = errors({
17
- WeaveError: { code: 500 },
18
- WeaveRetrieableError: { baseClass: RetrieableError },
19
- WeaveServiceNotFoundError: {}, // retrieable
20
- WeaveServiceNotAvailableError: {},
21
- WeaveRequestTimeoutError: {},
22
- WeaveParameterValidationError: {},
23
- WeaveBrokerOptionsError: {},
24
- WeaveQueueSizeExceededError: {}
25
- })
@@ -1,10 +0,0 @@
1
- const Cache = require('../cache')
2
-
3
- exports.initCache = (runtime) => {
4
- if (runtime.options.cache && runtime.options.cache.enabled) {
5
- const createCache = Cache.resolve(runtime.options.cache.adapter)
6
- Object.defineProperty(runtime, 'cache', {
7
- value: createCache(runtime, runtime.options.cache)
8
- })
9
- }
10
- }
@@ -1,34 +0,0 @@
1
- /* istanbul ignore file */
2
-
3
- const BaseCollector = require('./base')
4
-
5
- class ConsoleCollector extends BaseCollector {
6
- constructor (options) {
7
- super(options)
8
-
9
- this.options = Object.assign({
10
- events: {
11
- started: '$tracing.trace.span.started',
12
- finished: '$tracing.trace.span.finished'
13
- },
14
- broadcast: false
15
- })
16
- }
17
-
18
- init (broker, tracer) {
19
- super.init(tracer)
20
- this.broker = broker
21
- }
22
-
23
- startedSpan (span) {
24
- // const data = this.generateTracePayload(span)
25
- this.broker.emit(this.options.events.started, span)
26
- }
27
-
28
- finishedSpan (span) {
29
- // const data = this.generateTracePayload(span)
30
- this.broker.emit(this.options.events.finished, span)
31
- }
32
- }
33
-
34
- module.exports = ConsoleCollector
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- Tracer: require('./tracer')
3
- }
@@ -1,64 +0,0 @@
1
- const { resolveCollector } = require('./collectors')
2
- const { createSpan } = require('./span')
3
-
4
- module.exports = () => {
5
- let collectors = []
6
- let samplingCounter = 0
7
-
8
- return {
9
- init (broker, options) {
10
- this.options = options
11
- this.broker = broker
12
- this.log = broker.createLogger('Tracer')
13
- // this.storage = asyncStore()
14
- // this.storage.enable()
15
-
16
- if (options.enabled) {
17
- this.log.info('Tracer initialized.')
18
-
19
- if (options.collectors) {
20
- collectors = options.collectors
21
- .map(entry => {
22
- const initCollector = resolveCollector(broker, entry, this)
23
- // collector.init(this)
24
- return initCollector()
25
- })
26
- }
27
- }
28
- },
29
- async stop () {
30
- if (collectors) {
31
- return await Promise.all(collectors.map(collector => collector.stop()))
32
- }
33
- },
34
- shouldSample (span) {
35
- // check span priority
36
- if (this.options.samplingRate === 0) {
37
- return false
38
- }
39
-
40
- if (this.options.samplingRate === 1) {
41
- return true
42
- }
43
-
44
- if (++samplingCounter * this.options.samplingRate >= 1) {
45
- samplingCounter = 0
46
- return true
47
- }
48
-
49
- return false
50
- },
51
- invokeCollectorMethod (method, args) {
52
- collectors.map(collector => collector[method].apply(collector, args))
53
- },
54
- startSpan (name, options) {
55
- const span = createSpan(this, name, Object.assign({
56
- type: 'custom'
57
- }, options))
58
-
59
- span.start()
60
-
61
- return span
62
- }
63
- }
64
- }
@@ -1,59 +0,0 @@
1
- /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
3
- * -----
4
- * Copyright 2020 Fachwerk
5
- */
6
-
7
- // const MESSAGE_UNKNOWN = 'unknown'
8
- // const MESSAGE_DISCOVERY = 'discovery'
9
- // const MESSAGE_INFO = 'info'
10
- // const MESSAGE_REQUEST = 'request'
11
- // const MESSAGE_RESPONSE = 'response'
12
- // const MESSAGE_RESPONSE_STREAM_CHUNK = 'response_stream_chunk'
13
- // const MESSAGE_RESPONSE_STREAM_END = 'response_stream_end'
14
- // const MESSAGE_PING = 'ping'
15
- // const MESSAGE_PONG = 'pong'
16
- // const MESSAGE_DISCONNECT = 'disconnect'
17
- // const MESSAGE_HEARTBEAT = 'heartbeat'
18
- // const MESSAGE_EVENT = 'event'
19
-
20
- // // gossip messages
21
- // const MESSAGE_GOSSIP_REQUEST = 'gossip_request'
22
- // const MESSAGE_GOSSIP_RESPONSE = 'gossip_response'
23
- // const MESSAGE_GOSSIP_HELLO = 'gossip_hello'
24
-
25
- // const messageTypes = {
26
- // MESSAGE_UNKNOWN,
27
- // MESSAGE_DISCOVERY,
28
- // MESSAGE_INFO,
29
- // MESSAGE_REQUEST,
30
- // MESSAGE_RESPONSE,
31
- // MESSAGE_RESPONSE_STREAM_CHUNK,
32
- // MESSAGE_RESPONSE_STREAM_END,
33
- // MESSAGE_PING,
34
- // MESSAGE_PONG,
35
- // MESSAGE_DISCONNECT,
36
- // MESSAGE_HEARTBEAT,
37
- // MESSAGE_EVENT,
38
- // MESSAGE_GOSSIP_HELLO,
39
- // MESSAGE_GOSSIP_RESPONSE,
40
- // MESSAGE_GOSSIP_REQUEST
41
- // }
42
-
43
- // module.exports = messageTypes
44
-
45
- exports.MESSAGE_UNKNOWN = 'unknown'
46
- exports.MESSAGE_DISCOVERY = 'discovery'
47
- exports.MESSAGE_INFO = 'info'
48
- exports.MESSAGE_REQUEST = 'request'
49
- exports.MESSAGE_RESPONSE = 'response'
50
- exports.MESSAGE_RESPONSE_STREAM_CHUNK = 'response_stream_chunk'
51
- exports.MESSAGE_RESPONSE_STREAM_END = 'response_stream_end'
52
- exports.MESSAGE_PING = 'ping'
53
- exports.MESSAGE_PONG = 'pong'
54
- exports.MESSAGE_DISCONNECT = 'disconnect'
55
- exports.MESSAGE_HEARTBEAT = 'heartbeat'
56
- exports.MESSAGE_EVENT = 'event'
57
- exports.MESSAGE_GOSSIP_HELLO = 'gossip_hello'
58
- exports.MESSAGE_GOSSIP_RESPONSE = 'gossip_response'
59
- exports.MESSAGE_GOSSIP_REQUEST = 'gossip_request'
@@ -1,16 +0,0 @@
1
- /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
3
- * -----
4
- * Copyright 2020 Fachwerk
5
- */
6
-
7
- const makeMessage = (MessageTypes) =>
8
- (type, targetNodeId, payload) => {
9
- return {
10
- type: type || MessageTypes.MESSAGE_UNKNOWN,
11
- targetNodeId,
12
- payload: Buffer.from(JSON.stringify(payload || {}))
13
- }
14
- }
15
-
16
- module.exports = makeMessage