@weave-js/core 0.11.1 → 0.13.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/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +8 -8
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +8 -9
- package/lib/broker/index.js +160 -164
- package/lib/buildRuntime.js +34 -34
- package/lib/cache/adapters/base.js +50 -74
- package/lib/cache/adapters/inMemory.js +58 -58
- package/lib/cache/adapters/inMemoryLru.js +57 -57
- package/lib/cache/adapters/index.js +3 -39
- 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 +64 -54
- 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 -21
- 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 +37 -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 +139 -136
- 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 +7 -7
- 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 +90 -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 +13 -17
- package/lib/transport/messageHandlers.js +199 -206
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +28 -15
- package/lib/utils/index.js +1 -1
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +58 -6
- package/package.json +4 -4
- package/lib/cache/base.js +0 -136
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const { dotGet } = require('@weave-js/utils');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get property from data or metadata object.
|
|
5
|
+
* @param {any} data data object
|
|
6
|
+
* @param {object} metadata metadata object
|
|
7
|
+
* @param {string} key key
|
|
8
|
+
* @returns {any} Result
|
|
9
|
+
*/
|
|
10
|
+
const getPropertyFromDataOrMetadata = (data, metadata, key) => {
|
|
11
|
+
// if a key starts with ":", the property is picked from metadata
|
|
12
|
+
if (key.startsWith(':')) {
|
|
13
|
+
// remove ':' from key.
|
|
14
|
+
key = key.replace(':', '');
|
|
15
|
+
return dotGet(metadata, key);
|
|
16
|
+
}
|
|
17
|
+
return dotGet(data, key);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = { getPropertyFromDataOrMetadata };
|
package/lib/cache/lock.js
CHANGED
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
const createLock = () => {
|
|
2
|
-
const locked = new Map()
|
|
2
|
+
const locked = new Map();
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
function acquire (key, ttl) {
|
|
5
|
+
const lockedItems = locked.get(key);
|
|
6
|
+
if (!lockedItems) {
|
|
7
|
+
locked.set(key, []);
|
|
8
|
+
return Promise.resolve();
|
|
9
|
+
} else {
|
|
10
|
+
return new Promise((resolve) => lockedItems.push(resolve));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isLocked (key) {
|
|
15
|
+
return !!locked.has(key);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function release (key) {
|
|
19
|
+
const lockedItems = locked.get(key);
|
|
20
|
+
if (lockedItems) {
|
|
21
|
+
if (lockedItems.length > 0) {
|
|
22
|
+
lockedItems.shift()();
|
|
10
23
|
} else {
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
isLocked (key) {
|
|
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
|
-
}
|
|
24
|
+
locked.delete(key);
|
|
25
25
|
}
|
|
26
|
-
return Promise.resolve()
|
|
27
26
|
}
|
|
27
|
+
return Promise.resolve();
|
|
28
28
|
}
|
|
29
|
-
}
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
return Object.freeze({
|
|
31
|
+
acquire,
|
|
32
|
+
isLocked,
|
|
33
|
+
release
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
module.exports = { createLock };
|
package/lib/constants.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
exports.PROTOCOL_VERSION = 1
|
|
7
|
+
exports.PROTOCOL_VERSION = 1;
|
|
8
8
|
|
|
9
9
|
exports.loadBalancingStrategy = {
|
|
10
10
|
ROUND_ROBIN: 'round_robin',
|
|
11
11
|
RANDOM: 'random'
|
|
12
|
-
}
|
|
12
|
+
};
|
|
13
13
|
|
|
14
14
|
exports.circuitBreakerStates = {
|
|
15
15
|
CIRCUIT_CLOSED: 'closed',
|
|
16
16
|
CIRCUIT_HALF_OPENED: 'half_opened',
|
|
17
17
|
CIRCUIT_HALF_OPEN_WAITING: 'half_open_waiting',
|
|
18
18
|
CIRCUIT_OPENED: 'opened'
|
|
19
|
-
}
|
|
19
|
+
};
|
|
20
20
|
|
|
21
21
|
exports.level = {
|
|
22
22
|
trace: 'trace',
|
|
@@ -25,4 +25,4 @@ exports.level = {
|
|
|
25
25
|
warn: 'warn',
|
|
26
26
|
error: 'error',
|
|
27
27
|
fatal: 'fatal'
|
|
28
|
-
}
|
|
28
|
+
};
|
package/lib/errorHandler.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
exports.errorHandler = ({ options }, error) => {
|
|
2
2
|
if (options.errorHandler) {
|
|
3
|
-
return options.errorHandler.call(null, error)
|
|
3
|
+
return options.errorHandler.call(null, error);
|
|
4
4
|
}
|
|
5
|
-
throw error
|
|
6
|
-
}
|
|
5
|
+
throw error;
|
|
6
|
+
};
|
|
7
7
|
|
|
8
8
|
exports.fatalErrorHandler = (runtime, message, error, killProcess = true) => {
|
|
9
|
-
const { options, log } = runtime
|
|
9
|
+
const { options, log } = runtime;
|
|
10
10
|
if (options.logger.enabled) {
|
|
11
|
-
log.fatal(error, message)
|
|
11
|
+
log.fatal(error, message);
|
|
12
12
|
} else {
|
|
13
|
-
console.error(message, error)
|
|
13
|
+
console.error(message, error);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
if (killProcess) {
|
|
17
|
-
process.exit(1)
|
|
17
|
+
process.exit(1);
|
|
18
18
|
}
|
|
19
|
-
}
|
|
19
|
+
};
|
package/lib/errors.js
CHANGED
|
@@ -1,118 +1,129 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const {
|
|
7
|
+
const { defaultsDeep } = require('@weave-js/utils');
|
|
8
|
+
const { ExtendableError } = require('./ExtendableError');
|
|
8
9
|
|
|
9
10
|
class WeaveError extends ExtendableError {
|
|
10
|
-
constructor (message, code, type, data
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
constructor (message, options) { // code, type, data
|
|
12
|
+
options = defaultsDeep(
|
|
13
|
+
options,
|
|
14
|
+
{
|
|
15
|
+
code: 'WEAVE_ERROR',
|
|
16
|
+
retryable: false
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
super(message, options);
|
|
21
|
+
this.name = this.constructor.name;
|
|
22
|
+
this.code = options.code || 'WEAVE_ERROR';
|
|
23
|
+
this.data = options.data;
|
|
24
|
+
this.retryable = false;
|
|
25
|
+
|
|
26
|
+
if (options.statusCode) {
|
|
27
|
+
this.statusCode = options.statusCode;
|
|
28
|
+
}
|
|
17
29
|
}
|
|
18
30
|
}
|
|
19
31
|
|
|
20
32
|
class WeaveRetryableError extends WeaveError {
|
|
21
|
-
constructor (message, code
|
|
22
|
-
super(message)
|
|
23
|
-
this.
|
|
24
|
-
this.type = type
|
|
25
|
-
this.data = data
|
|
26
|
-
this.retryable = true
|
|
33
|
+
constructor (message, options = { code: 'WEAVE_RETRYABLE_ERROR', retryable: true }) {
|
|
34
|
+
super(message, options);
|
|
35
|
+
this.retryable = true;
|
|
27
36
|
}
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
class WeaveServiceNotFoundError extends WeaveRetryableError {
|
|
31
40
|
constructor (data = {}) {
|
|
32
|
-
let message
|
|
41
|
+
let message;
|
|
33
42
|
|
|
34
43
|
if (data.actionName && data.nodeId) {
|
|
35
|
-
message = `Service "${data.actionName}" not found on node "${data.nodeId}"
|
|
44
|
+
message = `Service "${data.actionName}" not found on node "${data.nodeId}".`;
|
|
36
45
|
} else if (data.actionName) {
|
|
37
|
-
message = `Service "${data.actionName}" not found
|
|
46
|
+
message = `Service "${data.actionName}" not found.`;
|
|
38
47
|
} else {
|
|
39
|
-
message = 'Service not found.'
|
|
48
|
+
message = 'Service not found.';
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
super(message,
|
|
51
|
+
super(message, { code: 'WEAVE_SERVICE_NOT_FOUND_ERROR', data });
|
|
43
52
|
}
|
|
44
53
|
}
|
|
45
54
|
|
|
46
|
-
class WeaveServiceNotAvailableError extends WeaveRetryableError {
|
|
55
|
+
class WeaveServiceNotAvailableError extends WeaveRetryableError { // 503
|
|
47
56
|
constructor (data = {}) {
|
|
48
|
-
let message
|
|
57
|
+
let message;
|
|
49
58
|
if (data.nodeId) {
|
|
50
|
-
message = `Service "${data.actionName}" not available on node "${data.nodeId}"
|
|
59
|
+
message = `Service "${data.actionName}" not available on node "${data.nodeId}".`;
|
|
51
60
|
} else if (data.actionName) {
|
|
52
|
-
message = `Service "${data.actionName}" not available
|
|
61
|
+
message = `Service "${data.actionName}" not available.`;
|
|
53
62
|
} else {
|
|
54
|
-
message = 'Service not available.'
|
|
63
|
+
message = 'Service not available.';
|
|
55
64
|
}
|
|
56
65
|
|
|
57
|
-
super(message,
|
|
66
|
+
super(message, { code: 'WEAVE_SERVICE_NOT_AVAILABLE_ERROR', data });
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
69
|
|
|
61
|
-
class WeaveRequestTimeoutError extends WeaveRetryableError {
|
|
70
|
+
class WeaveRequestTimeoutError extends WeaveRetryableError { // 504
|
|
62
71
|
constructor (actionName, nodeId, timeout) {
|
|
63
|
-
const
|
|
64
|
-
super(message, 504, 'WEAVE_REQUEST_TIMEOUT_ERROR', {
|
|
72
|
+
const data = {
|
|
65
73
|
actionName,
|
|
66
74
|
nodeId,
|
|
67
75
|
timeout
|
|
68
|
-
}
|
|
69
|
-
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const message = `Action ${actionName} timed out node ${nodeId || '<local>'}.`;
|
|
79
|
+
super(message, { code: 'WEAVE_REQUEST_TIMEOUT_ERROR', data });
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
|
|
73
|
-
class WeaveParameterValidationError extends WeaveError {
|
|
83
|
+
class WeaveParameterValidationError extends WeaveError { // 422
|
|
74
84
|
constructor (message, data) {
|
|
75
|
-
super(message,
|
|
85
|
+
super(message, { code: 'WEAVE_PARAMETER_VALIDATION_ERROR', data });
|
|
76
86
|
}
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
class WeaveBrokerOptionsError extends WeaveError {
|
|
80
90
|
constructor (message, data) {
|
|
81
|
-
super(
|
|
91
|
+
super(
|
|
92
|
+
message,
|
|
93
|
+
{ code: 'WEAVE_BROKER_OPTIONS_ERROR', data }
|
|
94
|
+
);
|
|
82
95
|
}
|
|
83
96
|
}
|
|
84
97
|
|
|
85
|
-
class WeaveQueueSizeExceededError extends WeaveError {
|
|
98
|
+
class WeaveQueueSizeExceededError extends WeaveError { // 429
|
|
86
99
|
constructor (data) {
|
|
87
|
-
super(
|
|
100
|
+
super(
|
|
101
|
+
'Queue size limit was exceeded. Request rejected.',
|
|
102
|
+
{ code: 'WEAVE_QUEUE_SIZE_EXCEEDED_ERROR', data }
|
|
103
|
+
);
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
106
|
|
|
91
107
|
class WeaveMaxCallLevelError extends WeaveError {
|
|
92
108
|
constructor (data) {
|
|
93
|
-
super(
|
|
109
|
+
super(
|
|
110
|
+
`Request level has reached the limit ${data.maxCallLevel} on node "${data.nodeId}".`,
|
|
111
|
+
{ code: 'WEAVE_MAX_CALL_LEVEL_ERROR', data }
|
|
112
|
+
);
|
|
94
113
|
}
|
|
95
114
|
}
|
|
96
115
|
|
|
97
116
|
class WeaveGracefulStopTimeoutError extends WeaveError {
|
|
98
117
|
constructor (service) {
|
|
99
|
-
|
|
118
|
+
const data = {
|
|
100
119
|
name: service.name,
|
|
101
120
|
version: service.version
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
121
|
+
};
|
|
105
122
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
switch (error.name) {
|
|
111
|
-
case 'WeaveError':
|
|
112
|
-
return new ErrorClass(error.message, error.code, error.type, error.data)
|
|
113
|
-
// case 'WeaveParameterValidationError':
|
|
114
|
-
// return new ErrorClass(error.message, error.data)
|
|
115
|
-
}
|
|
123
|
+
super(
|
|
124
|
+
`Unable to stop service "${service.name}"`,
|
|
125
|
+
{ code: 'WEAVE_GRACEFUL_STOP_TIMEOUT', data }
|
|
126
|
+
);
|
|
116
127
|
}
|
|
117
128
|
}
|
|
118
129
|
|
|
@@ -126,6 +137,5 @@ module.exports = {
|
|
|
126
137
|
WeaveRetryableError,
|
|
127
138
|
WeaveServiceNotAvailableError,
|
|
128
139
|
WeaveServiceNotFoundError,
|
|
129
|
-
WeaveGracefulStopTimeoutError
|
|
130
|
-
|
|
131
|
-
}
|
|
140
|
+
WeaveGracefulStopTimeoutError
|
|
141
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
* @typedef {import('./types.js').Broker} Broker
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { getDefaultOptions } = require('./broker/defaultOptions')
|
|
8
|
-
const { defaultsDeep } = require('@weave-js/utils')
|
|
9
|
-
const { initRuntime } = require('./buildRuntime')
|
|
10
|
-
const { createBrokerInstance } = require('./broker')
|
|
7
|
+
const { getDefaultOptions } = require('./broker/defaultOptions');
|
|
8
|
+
const { defaultsDeep } = require('@weave-js/utils');
|
|
9
|
+
const { initRuntime } = require('./buildRuntime');
|
|
10
|
+
const { createBrokerInstance } = require('./broker');
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @type {BrokerOptions} options Broker options
|
|
14
14
|
*/
|
|
15
|
-
exports.defaultOptions = getDefaultOptions()
|
|
15
|
+
exports.defaultOptions = getDefaultOptions();
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Build runtime object
|
|
@@ -21,42 +21,42 @@ exports.defaultOptions = getDefaultOptions()
|
|
|
21
21
|
*/
|
|
22
22
|
exports.createBroker = (options) => {
|
|
23
23
|
// get default options
|
|
24
|
-
const defaultOptions = getDefaultOptions()
|
|
24
|
+
const defaultOptions = getDefaultOptions();
|
|
25
25
|
|
|
26
26
|
// merge options with default options
|
|
27
|
-
options = defaultsDeep(options, defaultOptions)
|
|
27
|
+
options = defaultsDeep(options, defaultOptions);
|
|
28
28
|
|
|
29
29
|
// Init runtime
|
|
30
|
-
const runtime = initRuntime(options)
|
|
30
|
+
const runtime = initRuntime(options);
|
|
31
31
|
|
|
32
32
|
// Create broker instance
|
|
33
|
-
return createBrokerInstance(runtime)
|
|
34
|
-
}
|
|
33
|
+
return createBrokerInstance(runtime);
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* @deprecated since version 0.9.0
|
|
38
38
|
* @param {import('./types.js').BrokerOptions} options Broker options.
|
|
39
39
|
* @returns {import('./types.js').Broker} Broker instance
|
|
40
40
|
*/
|
|
41
|
-
exports.Weave = exports.createBroker
|
|
41
|
+
exports.Weave = exports.createBroker;
|
|
42
42
|
|
|
43
43
|
// Errors
|
|
44
|
-
exports.Errors = require('./errors')
|
|
44
|
+
exports.Errors = require('./errors');
|
|
45
45
|
|
|
46
|
-
exports.Constants = require('./constants')
|
|
46
|
+
exports.Constants = require('./constants');
|
|
47
47
|
|
|
48
48
|
// Caching
|
|
49
|
-
exports.Cache = require('./cache/adapters')
|
|
49
|
+
exports.Cache = require('./cache/adapters');
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* @deprecated since version 0.10.0
|
|
53
53
|
*/
|
|
54
|
-
exports.createBaseTracingCollector = require('./tracing/collectors/base').createBaseTracingCollector
|
|
55
|
-
exports.TransportAdapters = require('./transport/adapters')
|
|
56
|
-
exports.TracingAdapters = require('./tracing/collectors')
|
|
57
|
-
exports.CacheAdapters = require('./cache/adapters')
|
|
54
|
+
exports.createBaseTracingCollector = require('./tracing/collectors/base').createBaseTracingCollector;
|
|
55
|
+
exports.TransportAdapters = require('./transport/adapters');
|
|
56
|
+
exports.TracingAdapters = require('./tracing/collectors');
|
|
57
|
+
exports.CacheAdapters = require('./cache/adapters');
|
|
58
58
|
|
|
59
59
|
// Helper
|
|
60
|
-
exports.defineBrokerOptions = require('./helper/defineBrokerOptions')
|
|
61
|
-
exports.defineService = require('./helper/defineService')
|
|
62
|
-
exports.defineAction = require('./helper/defineAction')
|
|
60
|
+
exports.defineBrokerOptions = require('./helper/defineBrokerOptions');
|
|
61
|
+
exports.defineService = require('./helper/defineService');
|
|
62
|
+
exports.defineAction = require('./helper/defineAction');
|
package/lib/logger/base.js
CHANGED
|
@@ -1,54 +1,63 @@
|
|
|
1
|
-
const {
|
|
2
|
-
const {
|
|
1
|
+
const { WeaveError } = require('../errors');
|
|
2
|
+
const { isStandardLevel, levelMethods } = require('./levels');
|
|
3
|
+
const { noop, generateLogMethod } = require('./tools');
|
|
3
4
|
|
|
4
5
|
exports.initBase = (runtime) => {
|
|
5
6
|
runtime.setLevel = (level) => {
|
|
6
|
-
const { labels, values } = runtime.levels
|
|
7
|
+
const { labels, values } = runtime.levels;
|
|
8
|
+
|
|
9
|
+
// Handle number values for level
|
|
7
10
|
if (typeof level === 'number') {
|
|
8
|
-
if (labels[level] === undefined)
|
|
9
|
-
|
|
11
|
+
if (labels[level] === undefined) {
|
|
12
|
+
throw new WeaveError(`Unknown level value: "${level}"`);
|
|
13
|
+
}
|
|
14
|
+
level = labels[level];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Handle unknown log levels
|
|
18
|
+
if (values[level] === undefined) {
|
|
19
|
+
throw new WeaveError(`Unknown level: "${level}"`);
|
|
10
20
|
}
|
|
11
21
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const useOnlyCustomLevelsVal = runtime.options.useOnlyCustomLevelsSym
|
|
16
|
-
const hook = runtime.options.hooks.logMethod
|
|
22
|
+
const levelVal = runtime.levelValue = values[level];
|
|
23
|
+
const useOnlyCustomLevelsVal = runtime.options.useOnlyCustomLevelsSym;
|
|
24
|
+
const hook = runtime.options.hooks.logMethod;
|
|
17
25
|
|
|
18
26
|
for (const key in values) {
|
|
19
27
|
if (levelVal < values[key]) {
|
|
20
|
-
runtime.logMethods[key] = noop
|
|
21
|
-
continue
|
|
28
|
+
runtime.logMethods[key] = noop;
|
|
29
|
+
continue;
|
|
22
30
|
}
|
|
23
|
-
runtime.logMethods[key] = isStandardLevel(key, useOnlyCustomLevelsVal) ? levelMethods[key](runtime, hook) : generateLogMethod(runtime, values[key], hook)
|
|
31
|
+
runtime.logMethods[key] = isStandardLevel(key, useOnlyCustomLevelsVal) ? levelMethods[key](runtime, hook) : generateLogMethod(runtime, values[key], hook);
|
|
24
32
|
}
|
|
25
|
-
}
|
|
33
|
+
};
|
|
26
34
|
|
|
27
|
-
runtime.write = (
|
|
28
|
-
const isErrorObject =
|
|
29
|
-
const mixin = runtime.mixin
|
|
30
|
-
const time = Date.now()
|
|
31
|
-
let object
|
|
35
|
+
runtime.write = (originObject, message, number) => {
|
|
36
|
+
const isErrorObject = originObject instanceof Error;
|
|
37
|
+
const mixin = runtime.mixin;
|
|
38
|
+
const time = Date.now();
|
|
39
|
+
let object;
|
|
32
40
|
|
|
33
|
-
if (
|
|
34
|
-
object = mixin ? mixin({}) : {}
|
|
41
|
+
if (originObject === undefined || originObject === null) {
|
|
42
|
+
object = mixin ? mixin({}) : {};
|
|
35
43
|
} else {
|
|
36
|
-
object = Object.assign(mixin ? mixin(
|
|
44
|
+
object = Object.assign(mixin ? mixin(originObject) : {}, originObject);
|
|
37
45
|
|
|
46
|
+
// If the object is an error object, we set the message to the error message.
|
|
38
47
|
if (!message && isErrorObject) {
|
|
39
|
-
message =
|
|
48
|
+
message = originObject.message;
|
|
40
49
|
}
|
|
41
50
|
|
|
42
51
|
if (isErrorObject) {
|
|
43
|
-
object.stack =
|
|
52
|
+
object.stack = originObject.stack;
|
|
44
53
|
if (!object.type) {
|
|
45
|
-
object.type = 'Error'
|
|
54
|
+
object.type = 'Error';
|
|
46
55
|
}
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
const logString = runtime.formatter(runtime, object, message, number, time)
|
|
59
|
+
const logString = runtime.formatter(runtime, object, message, number, time);
|
|
51
60
|
|
|
52
|
-
runtime.destination.write(logString)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
61
|
+
runtime.destination.write(logString);
|
|
62
|
+
};
|
|
63
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const { green, magenta, red, yellow, gray, cyan } = require('../utils/colorize')
|
|
2
|
-
const os = require('os')
|
|
1
|
+
const { green, magenta, red, yellow, gray, cyan } = require('../utils/colorize');
|
|
2
|
+
const os = require('os');
|
|
3
3
|
|
|
4
4
|
exports.asHumanReadable = ({ levels, options }, originObj, message, number, time) => {
|
|
5
|
-
let logResult = ''
|
|
5
|
+
let logResult = '';
|
|
6
6
|
|
|
7
7
|
const logLevelColors = {
|
|
8
8
|
fatal: magenta,
|
|
@@ -11,32 +11,32 @@ exports.asHumanReadable = ({ levels, options }, originObj, message, number, time
|
|
|
11
11
|
info: green,
|
|
12
12
|
debug: cyan,
|
|
13
13
|
verbose: gray
|
|
14
|
-
}
|
|
14
|
+
};
|
|
15
15
|
|
|
16
|
-
const currentLabel = levels.labels[number]
|
|
16
|
+
const currentLabel = levels.labels[number];
|
|
17
17
|
|
|
18
|
-
const color = logLevelColors[currentLabel] || yellow
|
|
18
|
+
const color = logLevelColors[currentLabel] || yellow;
|
|
19
19
|
// Log level label
|
|
20
|
-
logResult += color(currentLabel.toUpperCase())
|
|
20
|
+
logResult += color(currentLabel.toUpperCase());
|
|
21
21
|
|
|
22
22
|
// date time
|
|
23
|
-
logResult += ' [' + new Date(time).toISOString() + '] '
|
|
23
|
+
logResult += ' [' + new Date(time).toISOString() + '] ';
|
|
24
24
|
|
|
25
25
|
if (options.base.pid && options.base.hostname) {
|
|
26
|
-
logResult += ` (${options.base.pid} on ${options.base.hostname})
|
|
26
|
+
logResult += ` (${options.base.pid} on ${options.base.hostname})`;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
if (message) {
|
|
30
|
-
logResult += ' ' + color(message)
|
|
30
|
+
logResult += ' ' + color(message);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
if (Object.keys(originObj).length > 0) {
|
|
34
34
|
// logResult += gray(' Json:')
|
|
35
|
-
logResult += os.EOL
|
|
36
|
-
logResult += gray(JSON.stringify(originObj, null, 2))
|
|
35
|
+
logResult += os.EOL;
|
|
36
|
+
logResult += gray(JSON.stringify(originObj, null, 2));
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
logResult += os.EOL
|
|
39
|
+
logResult += os.EOL;
|
|
40
40
|
|
|
41
|
-
return logResult
|
|
42
|
-
}
|
|
41
|
+
return logResult;
|
|
42
|
+
};
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
const os = require('os')
|
|
1
|
+
const os = require('os');
|
|
2
2
|
|
|
3
3
|
exports.asJson = (runtime, originObj, message, number, time) => {
|
|
4
4
|
const data = {
|
|
5
5
|
level: number,
|
|
6
6
|
time,
|
|
7
7
|
...runtime.fixtures
|
|
8
|
-
}
|
|
8
|
+
};
|
|
9
9
|
|
|
10
10
|
if (message !== undefined) {
|
|
11
|
-
data[runtime.options.messageKey] = message
|
|
11
|
+
data[runtime.options.messageKey] = message;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const doesNotHaveOwnProperty = originObj.hasOwnProperty === undefined;
|
|
15
15
|
|
|
16
|
-
let value
|
|
16
|
+
let value;
|
|
17
17
|
for (const key in originObj) {
|
|
18
|
-
value = originObj[key]
|
|
19
|
-
if ((
|
|
20
|
-
data[key] = value
|
|
18
|
+
value = originObj[key];
|
|
19
|
+
if ((doesNotHaveOwnProperty || originObj.hasOwnProperty(key)) && value !== undefined) {
|
|
20
|
+
data[key] = value;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
return JSON.stringify(data) + os.EOL
|
|
25
|
-
}
|
|
24
|
+
return JSON.stringify(data) + os.EOL;
|
|
25
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { asJson } = require('./asJson')
|
|
2
|
-
const { asHumanReadable } = require('./asHumanReadable')
|
|
1
|
+
const { asJson } = require('./asJson');
|
|
2
|
+
const { asHumanReadable } = require('./asHumanReadable');
|
|
3
3
|
|
|
4
|
-
module.exports = { asJson, asHumanReadable }
|
|
4
|
+
module.exports = { asJson, asHumanReadable };
|