@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
package/.eslintrc.js CHANGED
@@ -1 +1 @@
1
- module.exports = require('../../../.eslintrc.js')
1
+ module.exports = require('../../../.eslintrc.js');
package/jest.config.js CHANGED
@@ -9,4 +9,4 @@ module.exports = {
9
9
  roots: [
10
10
  './test'
11
11
  ]
12
- }
12
+ };
@@ -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 };
@@ -1,9 +1,9 @@
1
1
  /*
2
- * Author: Kevin Ries (kevin@fachw3rk.de)
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@fachw3rk.de)
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 { loadBalancingStrategy } = require('../constants')
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: 'memory',
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: null,
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
- watchServices: false
114
- }
115
- }
113
+ }
114
+ };
115
+ };