@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.
- package/lib/ExtendableError.js +33 -0
- package/lib/broker/context.js +25 -13
- package/lib/broker/{default-options.js → defaultOptions.js} +23 -5
- package/lib/broker/index.js +224 -194
- package/lib/buildRuntime.js +70 -0
- package/lib/cache/base.js +47 -31
- package/lib/cache/inMemory.js +140 -0
- package/lib/cache/index.js +2 -2
- package/lib/cache/lock.js +21 -4
- package/lib/constants.js +8 -5
- package/lib/errorHandler.js +19 -0
- package/lib/errors.js +17 -10
- package/lib/helper/{define-action.js → defineAction.js} +3 -1
- package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +2 -0
- package/lib/helper/{define-service.js → defineService.js} +4 -1
- package/lib/index.js +15 -89
- package/lib/logger/base.js +3 -3
- package/lib/logger/format/asHumanReadable.js +42 -0
- package/lib/logger/format/asJson.js +25 -0
- package/lib/logger/format/index.js +4 -0
- package/lib/logger/index.js +12 -5
- package/lib/logger/levels.js +9 -8
- package/lib/logger/tools.js +4 -25
- package/lib/logger/utils/colorize.js +32 -0
- package/lib/logger/utils/format.js +18 -4
- package/lib/metrics/constants.js +8 -6
- package/lib/metrics/types/gauge.js +1 -1
- package/lib/middlewares/action-hooks/index.js +62 -14
- package/lib/middlewares/bulkhead/index.js +5 -4
- package/lib/middlewares/cache/index.js +80 -0
- package/lib/middlewares/circuit-breaker/index.js +41 -16
- package/lib/middlewares/context-tracker/index.js +3 -3
- package/lib/middlewares/error-handler/index.js +3 -3
- package/lib/middlewares/index.js +2 -1
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +2 -2
- package/lib/middlewares/metrics/index.js +2 -2
- package/lib/middlewares/retry/index.js +11 -10
- package/lib/middlewares/timeout/index.js +5 -5
- package/lib/middlewares/tracing/index.js +27 -14
- package/lib/middlewares/tracing/tags.js +43 -0
- package/lib/middlewares/validator/index.js +6 -6
- package/lib/registry/{action-endpoint.js → actionEndpoint.js} +0 -0
- package/lib/registry/collections/{action-collection.js → actionCollection.js} +2 -2
- package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +19 -3
- package/lib/registry/collections/{event-collection.js → eventCollection.js} +15 -9
- package/lib/registry/collections/{node-collection.js → nodeCollection.js} +1 -1
- package/lib/registry/collections/{service-collection.js → serviceCollection.js} +2 -2
- package/lib/registry/{event-endpoint.js → eventEndpoint.js} +1 -1
- package/lib/registry/node.js +1 -1
- package/lib/registry/registry.js +10 -23
- package/lib/registry/service/service.js +2 -2
- package/lib/registry/{service-item.js → serviceItem.js} +1 -1
- package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +3 -3
- package/lib/runtime/{init-cache.js → initCache.js} +0 -0
- package/lib/runtime/{init-context-factory.js → initContextFactory.js} +2 -2
- package/lib/runtime/{init-eventbus.js → initEventbus.js} +11 -10
- package/lib/runtime/{init-logger.js → initLogger.js} +0 -0
- package/lib/runtime/{init-metrics.js → initMetrics.js} +11 -10
- package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +1 -1
- package/lib/runtime/{init-registry.js → initRegistry.js} +1 -1
- package/lib/runtime/{init-service-manager.js → initServiceManager.js} +0 -0
- package/lib/runtime/{init-tracing.js → initTracing.js} +2 -1
- package/lib/runtime/{init-transport.js → initTransport.js} +1 -1
- package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +0 -0
- package/lib/runtime/{init-validator.js → initValidator.js} +1 -1
- package/lib/tracing/collectors/base.js +11 -19
- package/lib/tracing/collectors/event.js +2 -2
- package/lib/tracing/collectors/index.js +2 -2
- package/lib/tracing/span.js +8 -6
- package/lib/transport/InboundTransformStream.js +75 -0
- package/lib/transport/adapters/{adapter-base.js → adapteBase.js} +16 -1
- package/lib/transport/adapters/adapters.js +1 -1
- package/lib/transport/adapters/dummy/index.js +3 -3
- package/lib/transport/adapters/index.js +1 -1
- package/lib/transport/adapters/tcp/discovery/index.js +45 -31
- package/lib/transport/adapters/tcp/index.js +20 -13
- package/lib/transport/adapters/tcp/tcpReader.js +1 -0
- package/lib/transport/adapters/tcp/tcpWriteStream.js +1 -1
- package/lib/transport/adapters/tcp/tcpWriter.js +2 -2
- package/lib/transport/createMessage.js +11 -0
- package/lib/transport/{transport-factory.js → createTransport.js} +234 -173
- package/lib/transport/errorPayloadFactory.js +18 -0
- package/lib/transport/{message-handlers.js → messageHandlers.js} +93 -24
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +66 -66
- package/lib/utils/index.js +0 -1
- package/lib/utils/options.js +4 -2
- package/lib/utils/{restore-error.js → restoreError.js} +1 -1
- package/package.json +6 -12
- package/CHANGELOG.md +0 -1293
- package/lib/cache/memory.js +0 -91
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- package/lib/tracing/collectors/console.js +0 -34
- package/lib/tracing/index.js +0 -3
- package/lib/tracing/tracer.js +0 -64
- package/lib/transport/message-types.js +0 -59
- package/lib/transport/message.js +0 -16
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./types.js').BrokerOptions} BrokerOptions
|
|
3
|
+
* @typedef {import('./types.js').Runtime} Runtime
|
|
4
|
+
* @typedef {import('./types.js').Broker} Broker
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { initLogger } = require('./runtime/initLogger')
|
|
8
|
+
const { initMiddlewareHandler } = require('./runtime/initMiddlewareManager')
|
|
9
|
+
const { initRegistry } = require('./runtime/initRegistry')
|
|
10
|
+
const { initContextFactory } = require('./runtime/initContextFactory')
|
|
11
|
+
const { initEventbus } = require('./runtime/initEventbus')
|
|
12
|
+
const { initValidator } = require('./runtime/initValidator')
|
|
13
|
+
const { initTransport } = require('./runtime/initTransport')
|
|
14
|
+
const { initCache } = require('./runtime/initCache')
|
|
15
|
+
const { initActionInvoker } = require('./runtime/initActionInvoker')
|
|
16
|
+
const { initServiceManager } = require('./runtime/initServiceManager')
|
|
17
|
+
const { initMetrics } = require('./runtime/initMetrics')
|
|
18
|
+
const { initTracer } = require('./runtime/initTracing')
|
|
19
|
+
const { initUUIDFactory } = require('./runtime/initUuidFactory')
|
|
20
|
+
const { errorHandler, fatalErrorHandler } = require('./errorHandler')
|
|
21
|
+
const { uuid } = require('@weave-js/utils')
|
|
22
|
+
const { version } = require('../package.json')
|
|
23
|
+
const EventEmitter = require('eventemitter2')
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Build runtime object
|
|
27
|
+
* @param {BrokerOptions} options Broker options
|
|
28
|
+
* @return {Runtime} Runtime
|
|
29
|
+
*/
|
|
30
|
+
exports.initRuntime = (options) => {
|
|
31
|
+
/**
|
|
32
|
+
* Event bus
|
|
33
|
+
* @returns {EventEmitter} Service object.
|
|
34
|
+
*/
|
|
35
|
+
const bus = new EventEmitter({
|
|
36
|
+
wildcard: true,
|
|
37
|
+
maxListeners: 1000
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// Create base runtime object
|
|
41
|
+
const runtime = {
|
|
42
|
+
nodeId: options.nodeId,
|
|
43
|
+
version,
|
|
44
|
+
options,
|
|
45
|
+
bus,
|
|
46
|
+
state: {
|
|
47
|
+
instanceId: uuid(),
|
|
48
|
+
isStarted: false
|
|
49
|
+
},
|
|
50
|
+
handleError: (error) => errorHandler(runtime, error),
|
|
51
|
+
fatalError: (message, error, killProcess) => fatalErrorHandler(runtime, message, error, killProcess)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Init modules
|
|
55
|
+
initLogger(runtime)
|
|
56
|
+
initUUIDFactory(runtime)
|
|
57
|
+
initMiddlewareHandler(runtime)
|
|
58
|
+
initRegistry(runtime)
|
|
59
|
+
initContextFactory(runtime)
|
|
60
|
+
initEventbus(runtime)
|
|
61
|
+
initValidator(runtime)
|
|
62
|
+
initTransport(runtime)
|
|
63
|
+
initCache(runtime)
|
|
64
|
+
initActionInvoker(runtime)
|
|
65
|
+
initServiceManager(runtime)
|
|
66
|
+
initMetrics(runtime)
|
|
67
|
+
initTracer(runtime)
|
|
68
|
+
|
|
69
|
+
return runtime
|
|
70
|
+
}
|
package/lib/cache/base.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
3
|
* -----
|
|
4
|
-
* Copyright
|
|
4
|
+
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
const crypto = require('crypto')
|
|
7
7
|
const { isObject } = require('@weave-js/utils')
|
|
8
|
+
const Constants = require('../metrics/constants')
|
|
8
9
|
|
|
9
10
|
function getCacheKeyByObject (val) {
|
|
10
11
|
if (Array.isArray(val)) {
|
|
@@ -28,20 +29,26 @@ function generateHash (key) {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
function registerCacheMetrics (metrics) {
|
|
31
|
-
|
|
32
|
+
metrics.register({ type: 'counter', name: Constants.CACHE_GET_TOTAL })
|
|
33
|
+
metrics.register({ type: 'counter', name: Constants.CACHE_SET_TOTAL })
|
|
34
|
+
metrics.register({ type: 'counter', name: Constants.CACHE_FOUND_TOTAL })
|
|
35
|
+
metrics.register({ type: 'counter', name: Constants.CACHE_EXPIRED_TOTAL })
|
|
36
|
+
metrics.register({ type: 'counter', name: Constants.CACHE_DELETED_TOTAL })
|
|
37
|
+
metrics.register({ type: 'counter', name: Constants.CACHE_CLEANED_TOTAL })
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
exports.createCacheBase = (runtime, options) => {
|
|
35
41
|
const cache = {
|
|
42
|
+
isConnected: false,
|
|
43
|
+
runtime,
|
|
36
44
|
options: Object.assign({
|
|
37
45
|
ttl: null
|
|
38
46
|
}, options),
|
|
39
47
|
init () {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
registerCacheMetrics(this.metrics)
|
|
48
|
+
// register metrics
|
|
49
|
+
if (runtime.metrics) {
|
|
50
|
+
this.metrics = runtime.metrics
|
|
51
|
+
registerCacheMetrics(runtime.metrics)
|
|
45
52
|
}
|
|
46
53
|
},
|
|
47
54
|
log: runtime.createLogger('CACHER'),
|
|
@@ -100,32 +107,41 @@ exports.createCacheBase = (runtime, options) => {
|
|
|
100
107
|
}
|
|
101
108
|
}
|
|
102
109
|
|
|
103
|
-
cache.middleware = () => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
110
|
+
// cache.middleware = (runtime) => {
|
|
111
|
+
// return {
|
|
112
|
+
// localAction: (handler, action) => {
|
|
113
|
+
// const cacheOptions = Object.assign({ enabled: true }, isObject(action.cache) ? action.cache : { enabled: !!action.cache })
|
|
114
|
+
// if (cacheOptions.enabled) {
|
|
115
|
+
// return function cacheMiddleware (context, serviceInjections) {
|
|
116
|
+
// const cacheHashKey = runtime.cache.getCachingHash(action.name, context.data, context.meta, action.cache.keys)
|
|
117
|
+
// context.isCachedResult = false
|
|
111
118
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return content
|
|
116
|
-
}
|
|
119
|
+
// if (context.meta.$noCache === true) {
|
|
120
|
+
// return handler(context, serviceInjections)
|
|
121
|
+
// }
|
|
117
122
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
123
|
+
// if (runtime.cache.isConnected === false) {
|
|
124
|
+
// runtime.cache.log('Cache adapter is not connected yet. Call handler...')
|
|
125
|
+
// return handler(context, serviceInjections)
|
|
126
|
+
// }
|
|
127
|
+
|
|
128
|
+
// return runtime.cache.get(cacheHashKey).then((cachedResult) => {
|
|
129
|
+
// if (cachedResult !== null) {
|
|
130
|
+
// context.isCachedResult = true
|
|
131
|
+
// return cachedResult
|
|
132
|
+
// }
|
|
133
|
+
|
|
134
|
+
// return handler(context, serviceInjections).then((result) => {
|
|
135
|
+
// runtime.cache.set(cacheHashKey, result, action.cache.ttl)
|
|
136
|
+
// return result
|
|
137
|
+
// })
|
|
138
|
+
// })
|
|
139
|
+
// }
|
|
140
|
+
// }
|
|
141
|
+
// return handler
|
|
142
|
+
// }
|
|
143
|
+
// }
|
|
144
|
+
// }
|
|
129
145
|
|
|
130
146
|
return cache
|
|
131
147
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
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
|
package/lib/cache/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
3
|
* -----
|
|
4
|
-
* Copyright
|
|
4
|
+
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
const { isString, isFunction } = require('@weave-js/utils')
|
|
8
8
|
const { WeaveBrokerOptionsError } = require('../errors')
|
|
9
9
|
|
|
10
10
|
const adapters = {
|
|
11
|
-
Memory: require('./
|
|
11
|
+
Memory: require('./inMemory')
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
exports.adapters = adapters
|
package/lib/cache/lock.js
CHANGED
|
@@ -1,14 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
const createLock = () => {
|
|
2
2
|
const locked = new Map()
|
|
3
|
+
|
|
3
4
|
return {
|
|
4
5
|
acquire (key, ttl) {
|
|
5
|
-
const
|
|
6
|
-
if (!
|
|
7
|
-
|
|
6
|
+
const lockedItems = locked.get(key)
|
|
7
|
+
if (!lockedItems) {
|
|
8
|
+
locked.set(key, [])
|
|
9
|
+
return Promise.resolve()
|
|
10
|
+
} else {
|
|
11
|
+
return new Promise((resolve) => lockedItems.push(resolve))
|
|
8
12
|
}
|
|
9
13
|
},
|
|
10
14
|
isLocked (key) {
|
|
11
15
|
return !!locked.has(key)
|
|
16
|
+
},
|
|
17
|
+
release (key) {
|
|
18
|
+
const lockedItems = locked.get(key)
|
|
19
|
+
if (lockedItems) {
|
|
20
|
+
if (lockedItems.length > 0) {
|
|
21
|
+
lockedItems.shift()()
|
|
22
|
+
} else {
|
|
23
|
+
locked.delete(key)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return Promise.resolve()
|
|
12
27
|
}
|
|
13
28
|
}
|
|
14
29
|
}
|
|
30
|
+
|
|
31
|
+
module.exports = { createLock }
|
package/lib/constants.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
3
|
* -----
|
|
4
|
-
* Copyright
|
|
4
|
+
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
exports.PROTOCOL_VERSION = 1
|
|
@@ -11,6 +11,13 @@ exports.loadBalancingStrategy = {
|
|
|
11
11
|
RANDOM: 'random'
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
exports.circuitBreakerStates = {
|
|
15
|
+
CIRCUIT_CLOSED: 'closed',
|
|
16
|
+
CIRCUIT_HALF_OPENED: 'half_opened',
|
|
17
|
+
CIRCUIT_HALF_OPEN_WAITING: 'half_open_waiting',
|
|
18
|
+
CIRCUIT_OPENED: 'opened'
|
|
19
|
+
}
|
|
20
|
+
|
|
14
21
|
exports.level = {
|
|
15
22
|
trace: 'trace',
|
|
16
23
|
debug: 'debug',
|
|
@@ -19,7 +26,3 @@ exports.level = {
|
|
|
19
26
|
error: 'error',
|
|
20
27
|
fatal: 'fatal'
|
|
21
28
|
}
|
|
22
|
-
|
|
23
|
-
exports.SYNC_MIDDLEWARE_HOOKS = {
|
|
24
|
-
STARTING: 'starting'
|
|
25
|
-
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
exports.errorHandler = ({ options }, error) => {
|
|
2
|
+
if (options.errorHandler) {
|
|
3
|
+
return options.errorHandler.call(null, error)
|
|
4
|
+
}
|
|
5
|
+
throw error
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
exports.fatalErrorHandler = (runtime, message, error, killProcess = true) => {
|
|
9
|
+
const { options, log } = runtime
|
|
10
|
+
if (options.logger.enabled) {
|
|
11
|
+
log.fatal(error, message)
|
|
12
|
+
} else {
|
|
13
|
+
console.error(message, error)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (killProcess) {
|
|
17
|
+
process.exit(1)
|
|
18
|
+
}
|
|
19
|
+
}
|
package/lib/errors.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
3
|
* -----
|
|
4
|
-
* Copyright
|
|
4
|
+
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const { ExtendableError } = require('./ExtendableError')
|
|
8
|
+
|
|
9
|
+
class WeaveError extends ExtendableError {
|
|
8
10
|
constructor (message, code, type, data) {
|
|
9
11
|
super(message)
|
|
10
12
|
this.name = this.constructor.name
|
|
@@ -15,7 +17,7 @@ class WeaveError extends Error {
|
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
class
|
|
20
|
+
class WeaveRetryableError extends WeaveError {
|
|
19
21
|
constructor (message, code, type, data) {
|
|
20
22
|
super(message)
|
|
21
23
|
this.code = code || 500
|
|
@@ -25,7 +27,7 @@ class WeaveRetrieableError extends WeaveError {
|
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
class WeaveServiceNotFoundError extends
|
|
30
|
+
class WeaveServiceNotFoundError extends WeaveRetryableError {
|
|
29
31
|
constructor (data = {}) {
|
|
30
32
|
let message
|
|
31
33
|
|
|
@@ -33,31 +35,36 @@ class WeaveServiceNotFoundError extends WeaveRetrieableError {
|
|
|
33
35
|
message = `Service "${data.actionName}" not found on node "${data.nodeId}".`
|
|
34
36
|
} else if (data.actionName) {
|
|
35
37
|
message = `Service "${data.actionName}" not found.`
|
|
38
|
+
} else {
|
|
39
|
+
message = 'Service not found.'
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
super(message, 404, 'WEAVE_SERVICE_NOT_FOUND_ERROR', data)
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
|
|
42
|
-
class WeaveServiceNotAvailableError extends
|
|
46
|
+
class WeaveServiceNotAvailableError extends WeaveRetryableError {
|
|
43
47
|
constructor (data = {}) {
|
|
44
48
|
let message
|
|
45
49
|
if (data.nodeId) {
|
|
46
50
|
message = `Service "${data.actionName}" not available on node "${data.nodeId}".`
|
|
47
|
-
} else {
|
|
51
|
+
} else if (data.actionName) {
|
|
48
52
|
message = `Service "${data.actionName}" not available.`
|
|
53
|
+
} else {
|
|
54
|
+
message = 'Service not available.'
|
|
49
55
|
}
|
|
50
56
|
|
|
51
|
-
super(message,
|
|
57
|
+
super(message, 503, 'WEAVE_SERVICE_NOT_AVAILABLE_ERROR', data)
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
60
|
|
|
55
|
-
class WeaveRequestTimeoutError extends
|
|
61
|
+
class WeaveRequestTimeoutError extends WeaveRetryableError {
|
|
56
62
|
constructor (actionName, nodeId, timeout) {
|
|
57
63
|
const message = `Action ${actionName} timed out node ${nodeId || '<local>'}.`
|
|
58
64
|
super(message, 504, 'WEAVE_REQUEST_TIMEOUT_ERROR', {
|
|
59
65
|
actionName,
|
|
60
|
-
nodeId
|
|
66
|
+
nodeId,
|
|
67
|
+
timeout
|
|
61
68
|
})
|
|
62
69
|
this.retryable = true
|
|
63
70
|
}
|
|
@@ -116,7 +123,7 @@ module.exports = {
|
|
|
116
123
|
WeaveParameterValidationError,
|
|
117
124
|
WeaveQueueSizeExceededError,
|
|
118
125
|
WeaveRequestTimeoutError,
|
|
119
|
-
|
|
126
|
+
WeaveRetryableError,
|
|
120
127
|
WeaveServiceNotAvailableError,
|
|
121
128
|
WeaveServiceNotFoundError,
|
|
122
129
|
WeaveGracefulStopTimeoutError,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* istanbul ignore next */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @typedef {import('../types.js').ServiceActionDefinition} ServiceActionDefinition
|
|
3
5
|
*/
|
|
@@ -6,7 +8,7 @@
|
|
|
6
8
|
* Create and register a new service action.
|
|
7
9
|
* @param {ServiceActionDefinition} actionDefinition - Schema of the the action
|
|
8
10
|
* @returns {ServiceActionDefinition} Action definition
|
|
9
|
-
|
|
11
|
+
*/
|
|
10
12
|
module.exports = function (actionDefinition) {
|
|
11
13
|
return actionDefinition
|
|
12
14
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
|
|
2
|
+
/* istanbul ignore next */
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* @typedef {import('../types.js').ServiceSchema} ServiceSchema
|
|
3
6
|
*/
|
|
@@ -6,7 +9,7 @@
|
|
|
6
9
|
* Create and register a new service
|
|
7
10
|
* @param {ServiceSchema} serviceSchema - Schema of the Service
|
|
8
11
|
* @returns {ServiceSchema} Service schema
|
|
9
|
-
|
|
12
|
+
*/
|
|
10
13
|
module.exports = function (serviceSchema) {
|
|
11
14
|
return serviceSchema
|
|
12
15
|
}
|
package/lib/index.js
CHANGED
|
@@ -4,93 +4,15 @@
|
|
|
4
4
|
* @typedef {import('./types.js').Broker} Broker
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const
|
|
8
|
-
const {
|
|
9
|
-
const {
|
|
10
|
-
const { initLogger } = require('./runtime/init-logger')
|
|
11
|
-
const { initMiddlewareHandler } = require('./runtime/init-middleware-manager')
|
|
12
|
-
const { initRegistry } = require('./runtime/init-registry')
|
|
13
|
-
const { initContextFactory } = require('./runtime/init-context-factory')
|
|
14
|
-
const { initEventbus } = require('./runtime/init-eventbus')
|
|
15
|
-
const { initValidator } = require('./runtime/init-validator')
|
|
16
|
-
const { initTransport } = require('./runtime/init-transport')
|
|
17
|
-
const { initCache } = require('./runtime/init-cache')
|
|
18
|
-
const { initActionInvoker } = require('./runtime/init-action-invoker')
|
|
19
|
-
const { initServiceManager } = require('./runtime/init-service-manager')
|
|
20
|
-
const { initMetrics } = require('./runtime/init-metrics')
|
|
21
|
-
const { initTracer } = require('./runtime/init-tracing')
|
|
22
|
-
const { initUUIDFactory } = require('./runtime/init-uuid-factory')
|
|
7
|
+
const { getDefaultOptions } = require('./broker/defaultOptions')
|
|
8
|
+
const { defaultsDeep } = require('@weave-js/utils')
|
|
9
|
+
const { initRuntime } = require('./buildRuntime')
|
|
23
10
|
const { createBrokerInstance } = require('./broker')
|
|
24
|
-
const { version } = require('../package.json')
|
|
25
|
-
|
|
26
|
-
exports.defaultOptions = require('./broker/default-options')
|
|
27
|
-
|
|
28
|
-
const errorHandler = ({ options }, error) => {
|
|
29
|
-
if (options.errorHandler) {
|
|
30
|
-
return options.errorHandler.call(null, error)
|
|
31
|
-
}
|
|
32
|
-
throw error
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const fatalErrorHandler = (runtime, message, error, killProcess = true) => {
|
|
36
|
-
const { options, log } = runtime
|
|
37
|
-
if (options.logger.enabled) {
|
|
38
|
-
log.fatal(error, message)
|
|
39
|
-
} else {
|
|
40
|
-
console.error(message, error)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (killProcess) {
|
|
44
|
-
process.exit(1)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
11
|
|
|
48
12
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @param {BrokerOptions} options Broker options
|
|
51
|
-
* @return {Runtime} Runtime
|
|
13
|
+
* @type {BrokerOptions} options Broker options
|
|
52
14
|
*/
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Event bus
|
|
56
|
-
* @returns {EventEmitter} Service object.
|
|
57
|
-
*/
|
|
58
|
-
const bus = new EventEmitter({
|
|
59
|
-
wildcard: true,
|
|
60
|
-
maxListeners: 1000
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
// Create base runtime object
|
|
64
|
-
const runtime = {
|
|
65
|
-
nodeId: options.nodeId,
|
|
66
|
-
version,
|
|
67
|
-
options,
|
|
68
|
-
bus,
|
|
69
|
-
state: {
|
|
70
|
-
instanceId: uuid(),
|
|
71
|
-
isStarted: false
|
|
72
|
-
},
|
|
73
|
-
handleError: (error) => errorHandler(runtime, error),
|
|
74
|
-
fatalError: (message, error, killProcess) => fatalErrorHandler(runtime, message, error, killProcess)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Init modules
|
|
78
|
-
initLogger(runtime)
|
|
79
|
-
initUUIDFactory(runtime)
|
|
80
|
-
initMiddlewareHandler(runtime)
|
|
81
|
-
initRegistry(runtime)
|
|
82
|
-
initContextFactory(runtime)
|
|
83
|
-
initEventbus(runtime)
|
|
84
|
-
initValidator(runtime)
|
|
85
|
-
initTransport(runtime)
|
|
86
|
-
initCache(runtime)
|
|
87
|
-
initActionInvoker(runtime)
|
|
88
|
-
initServiceManager(runtime)
|
|
89
|
-
initMetrics(runtime)
|
|
90
|
-
initTracer(runtime)
|
|
91
|
-
|
|
92
|
-
return runtime
|
|
93
|
-
}
|
|
15
|
+
exports.defaultOptions = getDefaultOptions()
|
|
94
16
|
|
|
95
17
|
/**
|
|
96
18
|
* Build runtime object
|
|
@@ -105,13 +27,12 @@ exports.createBroker = (options) => {
|
|
|
105
27
|
options = defaultsDeep(options, defaultOptions)
|
|
106
28
|
|
|
107
29
|
// Init runtime
|
|
108
|
-
const runtime =
|
|
30
|
+
const runtime = initRuntime(options)
|
|
109
31
|
|
|
110
32
|
// Create broker instance
|
|
111
33
|
return createBrokerInstance(runtime)
|
|
112
34
|
}
|
|
113
35
|
|
|
114
|
-
// eslint-disable-next-line valid-jsdoc
|
|
115
36
|
/**
|
|
116
37
|
* @deprecated since version 0.9.0
|
|
117
38
|
* @param {import('./types.js').BrokerOptions} options Broker options.
|
|
@@ -123,15 +44,20 @@ exports.Weave = exports.createBroker
|
|
|
123
44
|
exports.Errors = require('./errors')
|
|
124
45
|
|
|
125
46
|
exports.Constants = require('./constants')
|
|
47
|
+
|
|
126
48
|
// Transport
|
|
127
49
|
exports.TransportAdapters = require('./transport/adapters')
|
|
128
50
|
|
|
129
51
|
// Caching
|
|
130
52
|
exports.Cache = require('./cache')
|
|
131
|
-
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @deprecated since version 0.10.0
|
|
56
|
+
*/
|
|
57
|
+
exports.createBaseTracingCollector = require('./tracing/collectors/base').createBaseTracingCollector
|
|
132
58
|
exports.TracingAdapters = require('./tracing/collectors')
|
|
133
59
|
|
|
134
60
|
// Helper
|
|
135
|
-
exports.defineBrokerOptions = require('./helper/
|
|
136
|
-
exports.defineService = require('./helper/
|
|
137
|
-
exports.defineAction = require('./helper/
|
|
61
|
+
exports.defineBrokerOptions = require('./helper/defineBrokerOptions')
|
|
62
|
+
exports.defineService = require('./helper/defineService')
|
|
63
|
+
exports.defineAction = require('./helper/defineAction')
|