@weave-js/core 0.10.0 → 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 (87) hide show
  1. package/lib/ExtendableError.js +33 -0
  2. package/lib/broker/context.js +12 -0
  3. package/lib/broker/{default-options.js → defaultOptions.js} +22 -4
  4. package/lib/broker/index.js +218 -192
  5. package/lib/{build-runtime.js → buildRuntime.js} +16 -16
  6. package/lib/cache/base.js +33 -28
  7. package/lib/cache/inMemory.js +140 -0
  8. package/lib/cache/index.js +1 -1
  9. package/lib/cache/lock.js +21 -4
  10. package/lib/constants.js +7 -4
  11. package/lib/{error-handler.js → errorHandler.js} +0 -0
  12. package/lib/errors.js +16 -9
  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 +13 -9
  17. package/lib/logger/format/asHumanReadable.js +4 -3
  18. package/lib/logger/index.js +4 -1
  19. package/lib/logger/levels.js +2 -1
  20. package/lib/logger/tools.js +4 -0
  21. package/lib/logger/utils/format.js +18 -4
  22. package/lib/metrics/constants.js +1 -1
  23. package/lib/metrics/types/gauge.js +1 -1
  24. package/lib/middlewares/action-hooks/index.js +59 -11
  25. package/lib/middlewares/bulkhead/index.js +2 -1
  26. package/lib/middlewares/cache/index.js +80 -0
  27. package/lib/middlewares/circuit-breaker/index.js +38 -13
  28. package/lib/middlewares/index.js +1 -0
  29. package/lib/middlewares/metrics/index.js +1 -1
  30. package/lib/middlewares/timeout/index.js +2 -2
  31. package/lib/middlewares/tracing/index.js +24 -11
  32. package/lib/middlewares/tracing/tags.js +43 -0
  33. package/lib/middlewares/validator/index.js +1 -1
  34. package/lib/registry/{action-endpoint.js → actionEndpoint.js} +0 -0
  35. package/lib/registry/collections/{action-collection.js → actionCollection.js} +1 -1
  36. package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +2 -3
  37. package/lib/registry/collections/{event-collection.js → eventCollection.js} +4 -8
  38. package/lib/registry/collections/{node-collection.js → nodeCollection.js} +0 -0
  39. package/lib/registry/collections/{service-collection.js → serviceCollection.js} +1 -1
  40. package/lib/registry/{event-endpoint.js → eventEndpoint.js} +0 -0
  41. package/lib/registry/registry.js +9 -22
  42. package/lib/registry/service/service.js +2 -2
  43. package/lib/registry/{service-item.js → serviceItem.js} +0 -0
  44. package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +1 -1
  45. package/lib/runtime/{init-cache.js → initCache.js} +0 -0
  46. package/lib/runtime/{init-context-factory.js → initContextFactory.js} +0 -0
  47. package/lib/runtime/{init-eventbus.js → initEventbus.js} +8 -7
  48. package/lib/runtime/{init-logger.js → initLogger.js} +0 -0
  49. package/lib/runtime/{init-metrics.js → initMetrics.js} +11 -10
  50. package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +0 -0
  51. package/lib/runtime/{init-registry.js → initRegistry.js} +0 -0
  52. package/lib/runtime/{init-service-manager.js → initServiceManager.js} +0 -0
  53. package/lib/runtime/{init-tracing.js → initTracing.js} +2 -1
  54. package/lib/runtime/{init-transport.js → initTransport.js} +0 -0
  55. package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +0 -0
  56. package/lib/runtime/{init-validator.js → initValidator.js} +0 -0
  57. package/lib/tracing/collectors/base.js +11 -19
  58. package/lib/tracing/collectors/event.js +2 -2
  59. package/lib/tracing/collectors/index.js +2 -2
  60. package/lib/tracing/span.js +8 -6
  61. package/lib/transport/InboundTransformStream.js +75 -0
  62. package/lib/transport/adapters/{adapter-base.js → adapteBase.js} +15 -0
  63. package/lib/transport/adapters/adapters.js +1 -1
  64. package/lib/transport/adapters/dummy/index.js +2 -2
  65. package/lib/transport/adapters/tcp/discovery/index.js +45 -31
  66. package/lib/transport/adapters/tcp/index.js +20 -13
  67. package/lib/transport/adapters/tcp/tcpReader.js +1 -0
  68. package/lib/transport/adapters/tcp/tcpWriteStream.js +1 -1
  69. package/lib/transport/adapters/tcp/tcpWriter.js +2 -2
  70. package/lib/transport/createMessage.js +11 -0
  71. package/lib/transport/createTransport.js +214 -171
  72. package/lib/transport/errorPayloadFactory.js +18 -0
  73. package/lib/transport/{message-handlers.js → messageHandlers.js} +91 -22
  74. package/lib/transport/messageTypes.js +23 -0
  75. package/lib/types.js +49 -49
  76. package/lib/utils/options.js +4 -2
  77. package/lib/utils/{restore-error.js → restoreError.js} +1 -1
  78. package/package.json +6 -12
  79. package/CHANGELOG.md +0 -1334
  80. package/lib/cache/memory.js +0 -121
  81. package/lib/middlewares/tracing/buildTags.js +0 -17
  82. package/lib/new-errors.js +0 -25
  83. package/lib/tracing/collectors/console.js +0 -34
  84. package/lib/tracing/index.js +0 -3
  85. package/lib/tracing/tracer.js +0 -64
  86. package/lib/transport/message-types.js +0 -59
  87. package/lib/transport/message.js +0 -16
@@ -1,121 +0,0 @@
1
- /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
3
- * -----
4
- * Copyright 2021 Fachwerk
5
- */
6
-
7
- const { match } = require('@weave-js/utils')
8
- const { createCacheBase } = require('./base')
9
- const Constants = require('../metrics/constants')
10
-
11
- const makeMemoryCache = (runtime, options = {}) => {
12
- const base = createCacheBase(runtime, options)
13
- const storage = new Map()
14
- const name = 'Memory'
15
-
16
- const timer = setInterval(() => {
17
- checkTtl()
18
- }, 3000)
19
-
20
- timer.unref()
21
-
22
- // if a new broker gets connected, we need to clear the cache
23
- runtime.bus.on('$transport.connected', () => {
24
- base.log.verbose('Transport adapter connected. Cache will be cleared.')
25
- cache.clear()
26
- })
27
-
28
- const checkTtl = () => {
29
- const now = Date.now()
30
-
31
- storage.forEach((item, hashKey) => {
32
- if (item.expire && item.expire < now) {
33
- cache.log.debug(`Delete ${hashKey}`)
34
- storage.delete(hashKey)
35
- }
36
- })
37
- }
38
-
39
- const cache = Object.assign(base, {
40
- name,
41
- get (cacheKey) {
42
- base.log.debug(`Get ${cacheKey}`)
43
-
44
- if (this.metrics) {
45
- this.metrics.increment(Constants.CACHE_GET_TOTAL)
46
- }
47
-
48
- const item = storage.get(cacheKey)
49
-
50
- if (item) {
51
- cache.log.debug(`Found ${cacheKey}`)
52
-
53
- if (this.metrics) {
54
- this.metrics.increment(Constants.CACHE_FOUND_TOTAL)
55
- }
56
-
57
- // if (options.ttl) {
58
- // item.expire = Date.now()// + options_.ttl
59
- // }
60
-
61
- if (item.expire && item.expire < Date.now()) {
62
- cache.log.debug(`Delete ${cacheKey}`)
63
- storage.delete(cacheKey)
64
- if (this.metrics) {
65
- this.metrics.increment(Constants.CACHE_EXPIRED_TOTAL)
66
- }
67
- return Promise.resolve(null)
68
- }
69
-
70
- return Promise.resolve(item.data)
71
- }
72
- return Promise.resolve(null)
73
- },
74
- set (hashKey, data, ttl) {
75
- if (this.metrics) {
76
- this.metrics.increment(Constants.CACHE_SET_TOTAL)
77
- }
78
-
79
- if (ttl == null) {
80
- ttl = options.ttl
81
- }
82
-
83
- storage.set(hashKey, {
84
- data,
85
- expire: ttl ? Date.now() + ttl : null
86
- })
87
-
88
- base.log.debug(`Set ${hashKey}`)
89
-
90
- return Promise.resolve(data)
91
- },
92
- remove (hashKey) {
93
- if (this.metrics) {
94
- this.metrics.increment(Constants.CACHE_DELETED_TOTAL)
95
- }
96
- storage.delete(hashKey)
97
- base.log.debug(`Delete ${hashKey}`)
98
-
99
- return Promise.resolve()
100
- },
101
- clear (pattern = '**') {
102
- if (this.metrics) {
103
- this.metrics.increment(Constants.CACHE_DELETED_TOTAL)
104
- }
105
- storage.forEach((_, key) => {
106
- if (match(key, pattern)) {
107
- base.log.debug(`Delete ${key}`)
108
- this.remove(key)
109
- }
110
- })
111
- return Promise.resolve()
112
- },
113
- stop () {
114
- clearInterval(timer)
115
- }
116
- })
117
-
118
- return cache
119
- }
120
-
121
- 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 2021 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,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 2021 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 2021 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