@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.
- package/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +7 -7
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +9 -9
- package/lib/broker/index.js +161 -165
- package/lib/buildRuntime.js +34 -34
- package/lib/cache/adapters/base.js +119 -0
- package/lib/cache/adapters/inMemory.js +154 -0
- package/lib/cache/adapters/inMemoryLru.js +140 -0
- package/lib/cache/adapters/index.js +10 -0
- package/lib/cache/getCacheKeyByObject.js +31 -0
- package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
- package/lib/cache/lock.js +30 -24
- package/lib/constants.js +5 -5
- package/lib/errorHandler.js +8 -8
- package/lib/errors.js +35 -35
- package/lib/helper/defineAction.js +2 -2
- package/lib/helper/defineBrokerOptions.js +2 -2
- package/lib/helper/defineService.js +2 -2
- package/lib/index.js +21 -22
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -15
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +24 -24
- package/lib/logger/levels.js +21 -21
- package/lib/logger/tools.js +15 -15
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +75 -65
- package/lib/metrics/common.js +47 -47
- package/lib/metrics/constants.js +44 -44
- package/lib/metrics/exporter/base.js +8 -8
- package/lib/metrics/exporter/event.js +20 -20
- package/lib/metrics/exporter/index.js +15 -15
- package/lib/metrics/index.js +2 -2
- package/lib/metrics/types/base.js +20 -20
- package/lib/metrics/types/counter.js +6 -6
- package/lib/metrics/types/gauge.js +24 -24
- package/lib/metrics/types/histogram.js +26 -26
- package/lib/metrics/types/index.js +6 -6
- package/lib/metrics/types/info.js +17 -17
- package/lib/middlewares/action-hooks/index.js +38 -38
- package/lib/middlewares/bulkhead/index.js +34 -34
- package/lib/middlewares/cache/index.js +63 -37
- package/lib/middlewares/circuit-breaker/index.js +62 -62
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +47 -15
- package/lib/middlewares/index.js +2 -2
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +17 -17
- package/lib/middlewares/metrics/index.js +29 -29
- package/lib/middlewares/retry/index.js +15 -15
- package/lib/middlewares/timeout/index.js +16 -16
- package/lib/middlewares/tracing/index.js +37 -37
- package/lib/middlewares/tracing/tags.js +6 -6
- package/lib/middlewares/validator/index.js +31 -31
- package/lib/registry/actionEndpoint.js +8 -8
- package/lib/registry/collections/actionCollection.js +39 -35
- package/lib/registry/collections/endpointCollection.js +63 -63
- package/lib/registry/collections/eventCollection.js +62 -62
- package/lib/registry/collections/nodeCollection.js +45 -45
- package/lib/registry/collections/serviceCollection.js +61 -55
- package/lib/registry/eventEndpoint.js +14 -14
- package/lib/registry/load-balancer/base.js +3 -3
- package/lib/registry/load-balancer/index.js +7 -7
- package/lib/registry/node.js +27 -27
- package/lib/registry/registry.js +145 -148
- package/lib/registry/service/parseAction.js +16 -16
- package/lib/registry/service/parseEvent.js +18 -18
- package/lib/registry/service/reduceMixins.js +10 -10
- package/lib/registry/service/service.js +72 -72
- package/lib/registry/serviceItem.js +21 -21
- package/lib/runtime/initActionInvoker.js +21 -21
- package/lib/runtime/initCache.js +14 -5
- package/lib/runtime/initContextFactory.js +22 -22
- package/lib/runtime/initEventbus.js +49 -49
- package/lib/runtime/initLogger.js +11 -11
- package/lib/runtime/initMetrics.js +54 -54
- package/lib/runtime/initMiddlewareManager.js +21 -21
- package/lib/runtime/initRegistry.js +6 -6
- package/lib/runtime/initServiceManager.js +85 -68
- package/lib/runtime/initTracing.js +23 -23
- package/lib/runtime/initTransport.js +5 -5
- package/lib/runtime/initUuidFactory.js +4 -4
- package/lib/runtime/initValidator.js +5 -5
- package/lib/tracing/collectors/base.js +22 -22
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +15 -15
- package/lib/tracing/span.js +26 -26
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +26 -26
- package/lib/transport/adapters/adapteBase.js +30 -30
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +18 -18
- package/lib/transport/adapters/fromURI.js +13 -13
- package/lib/transport/adapters/getAdapterByName.js +5 -5
- package/lib/transport/adapters/index.js +12 -12
- package/lib/transport/adapters/tcp/discovery/codec.js +4 -4
- package/lib/transport/adapters/tcp/discovery/index.js +62 -62
- package/lib/transport/adapters/tcp/index.js +169 -171
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -39
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +57 -57
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +4 -4
- package/lib/transport/createTransport.js +254 -254
- package/lib/transport/errorPayloadFactory.js +14 -17
- package/lib/transport/messageHandlers.js +200 -200
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +49 -25
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +4 -4
- package/package.json +4 -4
- package/lib/cache/base.js +0 -147
- package/lib/cache/inMemory.js +0 -140
- package/lib/cache/index.js +0 -48
package/.eslintrc.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require('../../../.eslintrc.js')
|
|
1
|
+
module.exports = require('../../../.eslintrc.js');
|
package/jest.config.js
CHANGED
package/lib/ExtendableError.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
class ExtendableError extends Error {
|
|
2
2
|
constructor (message = '') {
|
|
3
|
-
super(message)
|
|
3
|
+
super(message);
|
|
4
4
|
|
|
5
5
|
Object.defineProperty(this, 'name', {
|
|
6
6
|
configurable: true,
|
|
7
7
|
enumerable: false,
|
|
8
8
|
value: this.constructor.name,
|
|
9
9
|
writable: true
|
|
10
|
-
})
|
|
10
|
+
});
|
|
11
11
|
|
|
12
12
|
Object.defineProperty(this, 'message', {
|
|
13
13
|
configurable: true,
|
|
14
14
|
enumerable: false,
|
|
15
15
|
value: message,
|
|
16
16
|
writable: true
|
|
17
|
-
})
|
|
17
|
+
});
|
|
18
18
|
|
|
19
19
|
if (Object.prototype.hasOwnProperty.call(Error, 'captureStackTrace')) {
|
|
20
|
-
Error.captureStackTrace(this, this.constructor)
|
|
21
|
-
return
|
|
20
|
+
Error.captureStackTrace(this, this.constructor);
|
|
21
|
+
return;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
Object.defineProperty(this, 'stack', {
|
|
@@ -26,8 +26,8 @@ class ExtendableError extends Error {
|
|
|
26
26
|
enumerable: false,
|
|
27
27
|
value: new Error(message).stack,
|
|
28
28
|
writable: true
|
|
29
|
-
})
|
|
29
|
+
});
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
module.exports = { ExtendableError }
|
|
33
|
+
module.exports = { ExtendableError };
|
package/lib/broker/context.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
|
-
|
|
6
|
-
'use strict'
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @typedef {import('../types').Context} Context
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* @typedef {import('../types').Endpoint} Endpoint
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
const { uuid, isFunction, isStream, isStreamObjectMode } = require('@weave-js/utils')
|
|
16
|
-
const { WeaveMaxCallLevelError, WeaveError } = require('../errors')
|
|
15
|
+
const { uuid, isFunction, isStream, isStreamObjectMode } = require('@weave-js/utils');
|
|
16
|
+
const { WeaveMaxCallLevelError, WeaveError } = require('../errors');
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Create a new context object
|
|
@@ -43,47 +43,47 @@ exports.createContext = (runtime) => {
|
|
|
43
43
|
duration: 0,
|
|
44
44
|
stopTime: 0,
|
|
45
45
|
setData (newParams) {
|
|
46
|
-
this.data = newParams || {}
|
|
46
|
+
this.data = newParams || {};
|
|
47
47
|
},
|
|
48
48
|
setStream (stream) {
|
|
49
49
|
if (isStream(stream)) {
|
|
50
50
|
if (isStreamObjectMode(context.options.stream)) {
|
|
51
|
-
this.meta.$isObjectModeStream = true
|
|
51
|
+
this.meta.$isObjectModeStream = true;
|
|
52
52
|
}
|
|
53
|
-
this.stream = stream
|
|
53
|
+
this.stream = stream;
|
|
54
54
|
} else {
|
|
55
|
-
throw new WeaveError('No valid stream.')
|
|
55
|
+
throw new WeaveError('No valid stream.');
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
setEndpoint (endpoint) {
|
|
59
|
-
this.nodeId = endpoint.node.id
|
|
60
|
-
this.endpoint = endpoint
|
|
61
|
-
this.action = endpoint.action
|
|
62
|
-
this.service = endpoint.action.service
|
|
59
|
+
this.nodeId = endpoint.node.id;
|
|
60
|
+
this.endpoint = endpoint;
|
|
61
|
+
this.action = endpoint.action;
|
|
62
|
+
this.service = endpoint.action.service;
|
|
63
63
|
},
|
|
64
64
|
emit (eventName, payload, options = {}) {
|
|
65
|
-
options.parentContext = this
|
|
66
|
-
return runtime.eventBus.emit(eventName, payload, options)
|
|
65
|
+
options.parentContext = this;
|
|
66
|
+
return runtime.eventBus.emit(eventName, payload, options);
|
|
67
67
|
},
|
|
68
68
|
broadcast (eventName, payload, options = {}) {
|
|
69
|
-
options.parentContext = this
|
|
70
|
-
return runtime.eventBus.broadcast(eventName, payload, options)
|
|
69
|
+
options.parentContext = this;
|
|
70
|
+
return runtime.eventBus.broadcast(eventName, payload, options);
|
|
71
71
|
},
|
|
72
72
|
call (actionName, params, options = {}) {
|
|
73
|
-
options.parentContext = this
|
|
73
|
+
options.parentContext = this;
|
|
74
74
|
if (runtime.options.registry.maxCallLevel > 0 && this.level >= runtime.options.registry.maxCallLevel) {
|
|
75
|
-
return Promise.reject(new WeaveMaxCallLevelError({ nodeId: runtime.nodeId, maxCallLevel: runtime.options.registry.maxCallLevel }))
|
|
75
|
+
return Promise.reject(new WeaveMaxCallLevelError({ nodeId: runtime.nodeId, maxCallLevel: runtime.options.registry.maxCallLevel }));
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/** @type {ContextPromise} */
|
|
79
|
-
const p = runtime.actionInvoker.call(actionName, params, options)
|
|
79
|
+
const p = runtime.actionInvoker.call(actionName, params, options);
|
|
80
80
|
|
|
81
81
|
return p.then(result => {
|
|
82
82
|
if (p.context) {
|
|
83
|
-
this.meta = Object.assign(this.meta, p.context.meta)
|
|
83
|
+
this.meta = Object.assign(this.meta, p.context.meta);
|
|
84
84
|
}
|
|
85
|
-
return result
|
|
86
|
-
})
|
|
85
|
+
return result;
|
|
86
|
+
});
|
|
87
87
|
},
|
|
88
88
|
startSpan (name, options) {
|
|
89
89
|
options = Object.assign({
|
|
@@ -93,19 +93,19 @@ exports.createContext = (runtime) => {
|
|
|
93
93
|
type: 'action',
|
|
94
94
|
service: this.service,
|
|
95
95
|
sampled: this.tracing
|
|
96
|
-
}, options)
|
|
96
|
+
}, options);
|
|
97
97
|
|
|
98
98
|
if (this.span) {
|
|
99
|
-
this.span = this.span.startChildSpan(name, options)
|
|
99
|
+
this.span = this.span.startChildSpan(name, options);
|
|
100
100
|
} else {
|
|
101
|
-
this.span = runtime.tracer.startSpan(name, options)
|
|
101
|
+
this.span = runtime.tracer.startSpan(name, options);
|
|
102
102
|
}
|
|
103
|
-
return this.span
|
|
103
|
+
return this.span;
|
|
104
104
|
},
|
|
105
105
|
finishSpan () {
|
|
106
106
|
if (this.span) {
|
|
107
|
-
this.span.finish()
|
|
108
|
-
return this.span
|
|
107
|
+
this.span.finish();
|
|
108
|
+
return this.span;
|
|
109
109
|
}
|
|
110
110
|
},
|
|
111
111
|
/**
|
|
@@ -113,40 +113,40 @@ exports.createContext = (runtime) => {
|
|
|
113
113
|
* @returns {Context} New copied context
|
|
114
114
|
*/
|
|
115
115
|
copy () {
|
|
116
|
-
const contextCopy = exports.createContext(runtime)
|
|
116
|
+
const contextCopy = exports.createContext(runtime);
|
|
117
117
|
|
|
118
|
-
contextCopy.nodeId = this.nodeId
|
|
119
|
-
contextCopy.options = this.options
|
|
120
|
-
contextCopy.data = this.data
|
|
121
|
-
contextCopy.meta = this.meta
|
|
122
|
-
contextCopy.parentContext = this.parentContext
|
|
123
|
-
contextCopy.callerNodeId = this.callerNodeId
|
|
124
|
-
contextCopy.requestId = this.requestId
|
|
125
|
-
contextCopy.tracing = this.tracing
|
|
126
|
-
contextCopy.level = this.level
|
|
127
|
-
contextCopy.eventName = this.eventName
|
|
128
|
-
contextCopy.eventType = this.eventType
|
|
129
|
-
contextCopy.eventGroups = this.eventGroups
|
|
118
|
+
contextCopy.nodeId = this.nodeId;
|
|
119
|
+
contextCopy.options = this.options;
|
|
120
|
+
contextCopy.data = this.data;
|
|
121
|
+
contextCopy.meta = this.meta;
|
|
122
|
+
contextCopy.parentContext = this.parentContext;
|
|
123
|
+
contextCopy.callerNodeId = this.callerNodeId;
|
|
124
|
+
contextCopy.requestId = this.requestId;
|
|
125
|
+
contextCopy.tracing = this.tracing;
|
|
126
|
+
contextCopy.level = this.level;
|
|
127
|
+
contextCopy.eventName = this.eventName;
|
|
128
|
+
contextCopy.eventType = this.eventType;
|
|
129
|
+
contextCopy.eventGroups = this.eventGroups;
|
|
130
130
|
|
|
131
|
-
return contextCopy
|
|
131
|
+
return contextCopy;
|
|
132
132
|
}
|
|
133
|
-
}
|
|
133
|
+
};
|
|
134
134
|
|
|
135
135
|
// Generate context Id
|
|
136
136
|
if (!context.id) {
|
|
137
137
|
// Use UUID factory from broker options
|
|
138
138
|
if (runtime.options.uuidFactory && isFunction(runtime.options.uuidFactory)) {
|
|
139
|
-
context.id = runtime.options.uuidFactory.call(context, runtime)
|
|
139
|
+
context.id = runtime.options.uuidFactory.call(context, runtime);
|
|
140
140
|
} else {
|
|
141
|
-
context.id = uuid()
|
|
141
|
+
context.id = uuid();
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
// Pass existing request ID
|
|
145
145
|
if (!context.requestId) {
|
|
146
|
-
context.requestId = context.id
|
|
146
|
+
context.requestId = context.id;
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
return context
|
|
151
|
-
}
|
|
150
|
+
return context;
|
|
151
|
+
};
|
|
152
152
|
|
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
|
-
* Author: Kevin Ries (kevin@
|
|
6
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
7
7
|
* -----
|
|
8
8
|
* Copyright 2021 Fachwerk
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/** @module weave */
|
|
12
|
-
const os = require('os')
|
|
13
|
-
const {
|
|
12
|
+
const os = require('os');
|
|
13
|
+
const { createInMemoryCache } = require('../cache/adapters/inMemory.js');
|
|
14
|
+
const { loadBalancingStrategy } = require('../constants');
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Returns the default options
|
|
@@ -28,7 +29,7 @@ exports.getDefaultOptions = () => {
|
|
|
28
29
|
},
|
|
29
30
|
cache: {
|
|
30
31
|
enabled: false,
|
|
31
|
-
adapter:
|
|
32
|
+
adapter: createInMemoryCache(),
|
|
32
33
|
ttl: 3000,
|
|
33
34
|
lock: {
|
|
34
35
|
enabled: false
|
|
@@ -66,7 +67,7 @@ exports.getDefaultOptions = () => {
|
|
|
66
67
|
adapters: [],
|
|
67
68
|
defaultBuckets: [1, 5, 10, 20, 25, 30, 40, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
|
|
68
69
|
},
|
|
69
|
-
middlewares:
|
|
70
|
+
middlewares: [],
|
|
70
71
|
logger: {
|
|
71
72
|
enabled: true,
|
|
72
73
|
level: 'info',
|
|
@@ -109,7 +110,6 @@ exports.getDefaultOptions = () => {
|
|
|
109
110
|
validatorOptions: {
|
|
110
111
|
strict: true,
|
|
111
112
|
strictMode: 'remove' // 'error'
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
};
|