@weave-js/core 0.10.0 → 0.12.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.
Files changed (132) hide show
  1. package/.eslintrc.js +1 -1
  2. package/jest.config.js +1 -1
  3. package/lib/ExtendableError.js +33 -0
  4. package/lib/broker/context.js +58 -46
  5. package/lib/broker/{default-options.js → defaultOptions.js} +31 -13
  6. package/lib/broker/index.js +273 -251
  7. package/lib/buildRuntime.js +70 -0
  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 +34 -11
  15. package/lib/constants.js +11 -8
  16. package/lib/{error-handler.js → errorHandler.js} +8 -8
  17. package/lib/errors.js +46 -39
  18. package/lib/helper/{define-action.js → defineAction.js} +5 -3
  19. package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +4 -2
  20. package/lib/helper/{define-service.js → defineService.js} +6 -3
  21. package/lib/index.js +25 -22
  22. package/lib/logger/base.js +38 -29
  23. package/lib/logger/format/asHumanReadable.js +15 -14
  24. package/lib/logger/format/asJson.js +10 -10
  25. package/lib/logger/format/index.js +3 -3
  26. package/lib/logger/index.js +28 -25
  27. package/lib/logger/levels.js +23 -22
  28. package/lib/logger/tools.js +17 -13
  29. package/lib/logger/utils/colorize.js +23 -23
  30. package/lib/logger/utils/format.js +89 -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 +82 -34
  44. package/lib/middlewares/bulkhead/index.js +34 -33
  45. package/lib/middlewares/cache/index.js +106 -0
  46. package/lib/middlewares/circuit-breaker/index.js +85 -60
  47. package/lib/middlewares/context-tracker/index.js +39 -39
  48. package/lib/middlewares/error-handler/index.js +14 -14
  49. package/lib/middlewares/index.js +3 -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 +18 -18
  54. package/lib/middlewares/tracing/index.js +50 -37
  55. package/lib/middlewares/tracing/tags.js +43 -0
  56. package/lib/middlewares/validator/index.js +32 -32
  57. package/lib/registry/{action-endpoint.js → actionEndpoint.js} +8 -8
  58. package/lib/registry/collections/{action-collection.js → actionCollection.js} +39 -35
  59. package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +64 -65
  60. package/lib/registry/collections/{event-collection.js → eventCollection.js} +65 -69
  61. package/lib/registry/collections/{node-collection.js → nodeCollection.js} +45 -45
  62. package/lib/registry/collections/{service-collection.js → serviceCollection.js} +61 -55
  63. package/lib/registry/eventEndpoint.js +26 -0
  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 +147 -163
  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/{service-item.js → serviceItem.js} +21 -21
  73. package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +21 -21
  74. package/lib/runtime/initCache.js +19 -0
  75. package/lib/runtime/{init-context-factory.js → initContextFactory.js} +22 -22
  76. package/lib/runtime/{init-eventbus.js → initEventbus.js} +54 -53
  77. package/lib/runtime/{init-logger.js → initLogger.js} +11 -11
  78. package/lib/runtime/{init-metrics.js → initMetrics.js} +55 -54
  79. package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +21 -21
  80. package/lib/runtime/{init-registry.js → initRegistry.js} +6 -6
  81. package/lib/runtime/initServiceManager.js +145 -0
  82. package/lib/runtime/{init-tracing.js → initTracing.js} +25 -24
  83. package/lib/runtime/{init-transport.js → initTransport.js} +5 -5
  84. package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +4 -4
  85. package/lib/runtime/{init-validator.js → initValidator.js} +5 -5
  86. package/lib/tracing/collectors/base.js +29 -37
  87. package/lib/tracing/collectors/event.js +33 -33
  88. package/lib/tracing/collectors/index.js +16 -16
  89. package/lib/tracing/span.js +34 -32
  90. package/lib/tracing/time.js +8 -8
  91. package/lib/transport/InboundTransformStream.js +75 -0
  92. package/lib/transport/adapters/adapteBase.js +107 -0
  93. package/lib/transport/adapters/adapters.js +4 -4
  94. package/lib/transport/adapters/dummy/index.js +19 -19
  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 +90 -76
  100. package/lib/transport/adapters/tcp/index.js +180 -175
  101. package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
  102. package/lib/transport/adapters/tcp/tcpReader.js +39 -38
  103. package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
  104. package/lib/transport/adapters/tcp/tcpWriter.js +58 -58
  105. package/lib/transport/adapters/tcp/utils.js +12 -12
  106. package/lib/transport/createMessage.js +11 -0
  107. package/lib/transport/createTransport.js +377 -334
  108. package/lib/transport/errorPayloadFactory.js +15 -0
  109. package/lib/transport/messageHandlers.js +517 -0
  110. package/lib/transport/messageTypes.js +23 -0
  111. package/lib/types.js +95 -71
  112. package/lib/utils/index.js +2 -2
  113. package/lib/utils/options.js +47 -45
  114. package/lib/utils/{restore-error.js → restoreError.js} +4 -4
  115. package/package.json +6 -12
  116. package/CHANGELOG.md +0 -1334
  117. package/lib/build-runtime.js +0 -70
  118. package/lib/cache/base.js +0 -142
  119. package/lib/cache/index.js +0 -48
  120. package/lib/cache/memory.js +0 -121
  121. package/lib/middlewares/tracing/buildTags.js +0 -17
  122. package/lib/new-errors.js +0 -25
  123. package/lib/registry/event-endpoint.js +0 -26
  124. package/lib/runtime/init-cache.js +0 -10
  125. package/lib/runtime/init-service-manager.js +0 -128
  126. package/lib/tracing/collectors/console.js +0 -34
  127. package/lib/tracing/index.js +0 -3
  128. package/lib/tracing/tracer.js +0 -64
  129. package/lib/transport/adapters/adapter-base.js +0 -92
  130. package/lib/transport/message-handlers.js +0 -448
  131. package/lib/transport/message-types.js +0 -59
  132. package/lib/transport/message.js +0 -16
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
+ };
@@ -0,0 +1,33 @@
1
+ class ExtendableError extends Error {
2
+ constructor (message = '') {
3
+ super(message);
4
+
5
+ Object.defineProperty(this, 'name', {
6
+ configurable: true,
7
+ enumerable: false,
8
+ value: this.constructor.name,
9
+ writable: true
10
+ });
11
+
12
+ Object.defineProperty(this, 'message', {
13
+ configurable: true,
14
+ enumerable: false,
15
+ value: message,
16
+ writable: true
17
+ });
18
+
19
+ if (Object.prototype.hasOwnProperty.call(Error, 'captureStackTrace')) {
20
+ Error.captureStackTrace(this, this.constructor);
21
+ return;
22
+ }
23
+
24
+ Object.defineProperty(this, 'stack', {
25
+ configurable: true,
26
+ enumerable: false,
27
+ value: new Error(message).stack,
28
+ writable: true
29
+ });
30
+ }
31
+ }
32
+
33
+ module.exports = { ExtendableError };
@@ -1,18 +1,19 @@
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
10
10
  * @typedef {import('../types').Runtime} Runtime
11
11
  * @typedef {import('../types').ContextPromise} ContextPromise
12
+ * @typedef {import('../types').Endpoint} Endpoint
12
13
  */
13
14
 
14
- const { uuid, isFunction, isStream, isStreamObjectMode } = require('@weave-js/utils')
15
- const { WeaveMaxCallLevelError, WeaveError } = require('../errors')
15
+ const { uuid, isFunction, isStream, isStreamObjectMode } = require('@weave-js/utils');
16
+ const { WeaveMaxCallLevelError, WeaveError } = require('../errors');
16
17
 
17
18
  /**
18
19
  * Create a new context object
@@ -42,60 +43,69 @@ exports.createContext = (runtime) => {
42
43
  duration: 0,
43
44
  stopTime: 0,
44
45
  setData (newParams) {
45
- this.data = newParams || {}
46
+ this.data = newParams || {};
46
47
  },
47
48
  setStream (stream) {
48
49
  if (isStream(stream)) {
49
50
  if (isStreamObjectMode(context.options.stream)) {
50
- this.meta.$isObjectModeStream = true
51
+ this.meta.$isObjectModeStream = true;
51
52
  }
52
- this.stream = stream
53
+ this.stream = stream;
53
54
  } else {
54
- throw new WeaveError('No valid stream.')
55
+ throw new WeaveError('No valid stream.');
55
56
  }
56
57
  },
57
58
  setEndpoint (endpoint) {
58
- this.nodeId = endpoint.node.id
59
- this.endpoint = endpoint
60
- this.action = endpoint.action
61
- 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;
62
63
  },
63
64
  emit (eventName, payload, options = {}) {
64
- options.parentContext = this
65
- return runtime.eventBus.emit(eventName, payload, options)
65
+ options.parentContext = this;
66
+ return runtime.eventBus.emit(eventName, payload, options);
66
67
  },
67
68
  broadcast (eventName, payload, options = {}) {
68
- options.parentContext = this
69
- return runtime.eventBus.broadcast(eventName, payload, options)
69
+ options.parentContext = this;
70
+ return runtime.eventBus.broadcast(eventName, payload, options);
70
71
  },
71
72
  call (actionName, params, options = {}) {
72
- options.parentContext = this
73
+ options.parentContext = this;
73
74
  if (runtime.options.registry.maxCallLevel > 0 && this.level >= runtime.options.registry.maxCallLevel) {
74
- 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 }));
75
76
  }
76
77
 
77
78
  /** @type {ContextPromise} */
78
- const p = runtime.actionInvoker.call(actionName, params, options)
79
+ const p = runtime.actionInvoker.call(actionName, params, options);
79
80
 
80
81
  return p.then(result => {
81
82
  if (p.context) {
82
- this.meta = Object.assign(this.meta, p.context.meta)
83
+ this.meta = Object.assign(this.meta, p.context.meta);
83
84
  }
84
- return result
85
- })
85
+ return result;
86
+ });
86
87
  },
87
88
  startSpan (name, options) {
89
+ options = Object.assign({
90
+ id: this.id,
91
+ traceId: this.requestId,
92
+ parentId: this.parentId,
93
+ type: 'action',
94
+ service: this.service,
95
+ sampled: this.tracing
96
+ }, options);
97
+
88
98
  if (this.span) {
89
- this.span = this.span.startChildSpan(name, options)
99
+ this.span = this.span.startChildSpan(name, options);
90
100
  } else {
91
- this.span = runtime.tracer.startSpan(name, options)
101
+ this.span = runtime.tracer.startSpan(name, options);
92
102
  }
93
- return this.span
103
+ return this.span;
94
104
  },
95
105
  finishSpan () {
96
106
  if (this.span) {
97
- this.span.finish()
98
- return this.span
107
+ this.span.finish();
108
+ return this.span;
99
109
  }
100
110
  },
101
111
  /**
@@ -103,38 +113,40 @@ exports.createContext = (runtime) => {
103
113
  * @returns {Context} New copied context
104
114
  */
105
115
  copy () {
106
- const contextCopy = exports.createContext(runtime)
116
+ const contextCopy = exports.createContext(runtime);
107
117
 
108
- contextCopy.nodeId = this.nodeId
109
- contextCopy.options = this.options
110
- contextCopy.data = this.data
111
- contextCopy.meta = this.meta
112
- contextCopy.parentContext = this.parentContext
113
- contextCopy.callerNodeId = this.callerNodeId
114
- contextCopy.level = this.level
115
- contextCopy.eventName = this.eventName
116
- contextCopy.eventType = this.eventType
117
- 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;
118
130
 
119
- return contextCopy
131
+ return contextCopy;
120
132
  }
121
- }
133
+ };
122
134
 
123
135
  // Generate context Id
124
136
  if (!context.id) {
125
137
  // Use UUID factory from broker options
126
138
  if (runtime.options.uuidFactory && isFunction(runtime.options.uuidFactory)) {
127
- context.id = runtime.options.uuidFactory.call(context, runtime)
139
+ context.id = runtime.options.uuidFactory.call(context, runtime);
128
140
  } else {
129
- context.id = uuid()
141
+ context.id = uuid();
130
142
  }
131
143
 
132
144
  // Pass existing request ID
133
145
  if (!context.requestId) {
134
- context.requestId = context.id
146
+ context.requestId = context.id;
135
147
  }
136
148
  }
137
149
 
138
- return context
139
- }
150
+ return context;
151
+ };
140
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,8 +29,11 @@ exports.getDefaultOptions = () => {
28
29
  },
29
30
  cache: {
30
31
  enabled: false,
31
- adapter: 'memory',
32
- ttl: 3000
32
+ adapter: createInMemoryCache(),
33
+ ttl: 3000,
34
+ lock: {
35
+ enabled: false
36
+ }
33
37
  },
34
38
  circuitBreaker: {
35
39
  enabled: false,
@@ -49,7 +53,10 @@ exports.getDefaultOptions = () => {
49
53
  heartbeatTimeout: 10 * 1000,
50
54
  offlineNodeCheckInterval: 30 * 1000,
51
55
  maxOfflineTime: 1000 * 60 * 10,
52
- maxChunkSize: 256 * 1024
56
+ maxChunkSize: 256 * 1024,
57
+ streams: {
58
+ handleBackpressure: true
59
+ }
53
60
  },
54
61
  errorHandler: null,
55
62
  loadInternalMiddlewares: true,
@@ -58,9 +65,9 @@ exports.getDefaultOptions = () => {
58
65
  collectCommonMetrics: true,
59
66
  collectInterval: 5000,
60
67
  adapters: [],
61
- defaultBuckets: [1, 5, 10, 20, 25, 30, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
68
+ defaultBuckets: [1, 5, 10, 20, 25, 30, 40, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
62
69
  },
63
- middlewares: null,
70
+ middlewares: [],
64
71
  logger: {
65
72
  enabled: true,
66
73
  level: 'info',
@@ -72,7 +79,19 @@ exports.getDefaultOptions = () => {
72
79
  tracing: {
73
80
  enabled: false,
74
81
  samplingRate: 1.0,
75
- collectors: []
82
+ collectors: [],
83
+ actions: {
84
+ data: false,
85
+ tags: {}
86
+ },
87
+ events: {
88
+ data: false,
89
+ tags: {}
90
+ },
91
+ errors: {
92
+ fields: ['name', 'message', 'code', 'type', 'data'],
93
+ stackTrace: false
94
+ }
76
95
  },
77
96
  namespace: '',
78
97
  registry: {
@@ -91,7 +110,6 @@ exports.getDefaultOptions = () => {
91
110
  validatorOptions: {
92
111
  strict: true,
93
112
  strictMode: 'remove' // 'error'
94
- },
95
- watchServices: false
96
- }
97
- }
113
+ }
114
+ };
115
+ };