@weave-js/core 0.11.0 → 0.12.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 (118) hide show
  1. package/.eslintrc.js +1 -1
  2. package/jest.config.js +1 -1
  3. package/lib/ExtendableError.js +7 -7
  4. package/lib/broker/context.js +49 -49
  5. package/lib/broker/defaultOptions.js +9 -9
  6. package/lib/broker/index.js +161 -165
  7. package/lib/buildRuntime.js +34 -34
  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 +30 -24
  15. package/lib/constants.js +5 -5
  16. package/lib/errorHandler.js +8 -8
  17. package/lib/errors.js +35 -35
  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 -22
  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 +63 -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 +145 -148
  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 +14 -5
  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 +85 -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 +14 -17
  109. package/lib/transport/messageHandlers.js +200 -200
  110. package/lib/transport/messageTypes.js +19 -19
  111. package/lib/types.js +49 -25
  112. package/lib/utils/index.js +2 -2
  113. package/lib/utils/options.js +44 -44
  114. package/lib/utils/restoreError.js +4 -4
  115. package/package.json +4 -4
  116. package/lib/cache/base.js +0 -147
  117. package/lib/cache/inMemory.js +0 -140
  118. package/lib/cache/index.js +0 -48
@@ -1,140 +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 { createLock } = require('./lock')
10
- const Constants = require('../metrics/constants')
11
-
12
- const makeInMemoryCache = (runtime, options = {}) => {
13
- const base = createCacheBase(runtime, options)
14
- const storage = new Map()
15
- const name = 'Memory'
16
-
17
- const lock = createLock()
18
-
19
- const timer = setInterval(() => {
20
- checkTtl()
21
- }, 3000)
22
-
23
- timer.unref()
24
-
25
- // if a new broker gets connected, we need to clear the cache
26
- runtime.bus.on('$transport.connected', () => {
27
- base.log.debug('Transport adapter connected. Cache will be cleared.')
28
- cache.clear()
29
- })
30
-
31
- const checkTtl = () => {
32
- const now = Date.now()
33
-
34
- storage.forEach((item, hashKey) => {
35
- if (item.expire && item.expire < now) {
36
- cache.log.debug(`Delete ${hashKey}`)
37
- storage.delete(hashKey)
38
- }
39
- })
40
- }
41
-
42
- const cache = Object.assign(
43
- {},
44
- base,
45
- {
46
- name,
47
- init () {
48
- base.init()
49
- cache.isConnected = true
50
- },
51
- get (cacheKey) {
52
- base.log.debug(`Get ${cacheKey}`)
53
-
54
- if (base.metrics) {
55
- base.metrics.increment(Constants.CACHE_GET_TOTAL)
56
- }
57
-
58
- const item = storage.get(cacheKey)
59
-
60
- if (item) {
61
- cache.log.debug(`Found ${cacheKey}`)
62
-
63
- if (base.metrics) {
64
- base.metrics.increment(Constants.CACHE_FOUND_TOTAL)
65
- }
66
-
67
- if (item.expire && item.expire < Date.now()) {
68
- cache.log.debug(`Delete ${cacheKey}`)
69
- storage.delete(cacheKey)
70
- if (base.metrics) {
71
- base.metrics.increment(Constants.CACHE_EXPIRED_TOTAL)
72
- }
73
- return Promise.resolve(null)
74
- }
75
-
76
- return Promise.resolve(item.data)
77
- }
78
- return Promise.resolve(null)
79
- },
80
- set (hashKey, data, ttl) {
81
- if (base.metrics) {
82
- base.metrics.increment(Constants.CACHE_SET_TOTAL)
83
- }
84
-
85
- if (ttl == null) {
86
- ttl = options.ttl
87
- }
88
-
89
- storage.set(hashKey, {
90
- data,
91
- expire: ttl ? Date.now() + ttl : null
92
- })
93
-
94
- base.log.debug(`Set ${hashKey}`)
95
-
96
- return Promise.resolve(data)
97
- },
98
- remove (hashKey) {
99
- if (base.metrics) {
100
- base.metrics.increment(Constants.CACHE_DELETED_TOTAL)
101
- }
102
- storage.delete(hashKey)
103
- base.log.debug(`Delete ${hashKey}`)
104
-
105
- return Promise.resolve()
106
- },
107
- clear (pattern = '**') {
108
- if (base.metrics) {
109
- base.metrics.increment(Constants.CACHE_DELETED_TOTAL)
110
- }
111
- storage.forEach((_, key) => {
112
- if (match(key, pattern)) {
113
- base.log.debug(`Delete ${key}`)
114
- this.remove(key)
115
- }
116
- })
117
- return Promise.resolve()
118
- },
119
- lock (key, ttl) {
120
- return lock.acquire(key, ttl).then(() => {
121
- return () => lock.release(key)
122
- })
123
- },
124
- tryLock (key, ttl) {
125
- if (lock.isLocked(key)) {
126
- return Promise.reject(new Error('Locked'))
127
- }
128
-
129
- return lock.acquire(key, ttl).then(() => {
130
- return () => lock.release(key)
131
- })
132
- },
133
- stop () {
134
- clearInterval(timer)
135
- }
136
- })
137
- return cache
138
- }
139
-
140
- module.exports = makeInMemoryCache
@@ -1,48 +0,0 @@
1
- /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
3
- * -----
4
- * Copyright 2021 Fachwerk
5
- */
6
-
7
- const { isString, isFunction } = require('@weave-js/utils')
8
- const { WeaveBrokerOptionsError } = require('../errors')
9
-
10
- const adapters = {
11
- Memory: require('./inMemory')
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
- }