@weave-js/core 0.11.1 → 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 (116) 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 +8 -9
  6. package/lib/broker/index.js +160 -164
  7. package/lib/buildRuntime.js +34 -34
  8. package/lib/cache/adapters/base.js +50 -74
  9. package/lib/cache/adapters/inMemory.js +58 -58
  10. package/lib/cache/adapters/inMemoryLru.js +57 -57
  11. package/lib/cache/adapters/index.js +3 -39
  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 -21
  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 +37 -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 +14 -14
  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 +139 -136
  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 +7 -7
  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 +28 -15
  112. package/lib/utils/index.js +1 -1
  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 -136
@@ -1,14 +1,14 @@
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
5
  */
6
6
 
7
- const { InboundTransformStream } = require('./InboundTransformStream')
8
- const { WeaveError, restoreError } = require('../errors')
9
- const { createContext } = require('../broker/context')
10
- const { createMessage } = require('./createMessage')
11
- const MessageTypes = require('./messageTypes')
7
+ const { InboundTransformStream } = require('./InboundTransformStream');
8
+ const { WeaveError, restoreError } = require('../errors');
9
+ const { createContext } = require('../broker/context');
10
+ const { createMessage } = require('./createMessage');
11
+ const MessageTypes = require('./messageTypes');
12
12
 
13
13
  /**
14
14
  * @typedef {import('../types').Transport} Transport
@@ -22,201 +22,201 @@ const MessageTypes = require('./messageTypes')
22
22
  * @returns {TransportMessageHandler} - Message handler
23
23
  */
24
24
  module.exports = (runtime, transport) => {
25
- const registry = runtime.registry
25
+ const registry = runtime.registry;
26
26
 
27
27
  const getRequestTimeout = (payload) => {
28
- return payload.timeout || runtime.options.registry.requestTimeout || 0
29
- }
28
+ return payload.timeout || runtime.options.registry.requestTimeout || 0;
29
+ };
30
30
 
31
31
  const localRequestProxy = (context) => {
32
- const actionName = context.action.name
32
+ const actionName = context.action.name;
33
33
 
34
34
  // Get available endpoints
35
- const endpointList = registry.getActionEndpoints(actionName)
35
+ const endpointList = registry.getActionEndpoints(actionName);
36
36
 
37
37
  // Reject the request if no local endpoint can be found.
38
38
  if (endpointList == null || !endpointList.hasLocal()) {
39
- transport.log.warn(`Service ${actionName} not found localy.`)
40
- return Promise.reject('Service not found')
39
+ transport.log.warn(`Service ${actionName} not found localy.`);
40
+ return Promise.reject('Service not found');
41
41
  }
42
42
 
43
43
  // From all available local endpoints - get one.
44
- const endpoint = endpointList.getNextLocalEndpoint()
44
+ const endpoint = endpointList.getNextLocalEndpoint();
45
45
 
46
46
  // if there is no endpoint, reject
47
47
  if (!endpoint) {
48
- transport.log.warn(`Service ${actionName} is not available localy.`)
49
- return Promise.reject('Service not found')
48
+ transport.log.warn(`Service ${actionName} is not available localy.`);
49
+ return Promise.reject('Service not found');
50
50
  }
51
51
 
52
52
  // Call the local action handler with context
53
- const promise = endpoint.action.handler(context)
54
- promise.context = context
53
+ const promise = endpoint.action.handler(context);
54
+ promise.context = context;
55
55
 
56
- return promise
57
- }
56
+ return promise;
57
+ };
58
58
 
59
59
  const handleIncomingRequestStream = (payload) => {
60
60
  // check for open stream.
61
- let stream = transport.pending.requestStreams.get(payload.id)
62
- let isNew = false
61
+ let stream = transport.pending.requestStreams.get(payload.id);
62
+ let isNew = false;
63
63
 
64
64
  if (!payload.isStream && !stream) {
65
- return false
65
+ return false;
66
66
  }
67
67
 
68
68
  if (!stream) {
69
- isNew = true
69
+ isNew = true;
70
70
  stream = new InboundTransformStream(
71
71
  payload.sender,
72
72
  payload.id, {
73
73
  objectMode: payload.meta && payload.meta.$isObjectModeStream
74
74
  }
75
- )
75
+ );
76
76
 
77
77
  // handle backpressure
78
78
  if (runtime.options.transport.streams.handleBackpressure) {
79
79
  stream.on('backpressure', async ({ sender, requestId }) => {
80
- const message = createMessage(MessageTypes.MESSAGE_REQUEST_STREAM_BACKPRESSURE, sender, { id: requestId })
81
- await transport.send(message)
82
- })
80
+ const message = createMessage(MessageTypes.MESSAGE_REQUEST_STREAM_BACKPRESSURE, sender, { id: requestId });
81
+ await transport.send(message);
82
+ });
83
83
 
84
84
  stream.on('resume_backpressure', async ({ sender, requestId }) => {
85
- const message = createMessage(MessageTypes.MESSAGE_REQUEST_STREAM_RESUME, sender, { id: requestId })
86
- await transport.send(message)
87
- })
85
+ const message = createMessage(MessageTypes.MESSAGE_REQUEST_STREAM_RESUME, sender, { id: requestId });
86
+ await transport.send(message);
87
+ });
88
88
  }
89
89
 
90
- stream.$prevSeq = -1
91
- stream.$pool = new Map()
90
+ stream.$prevSeq = -1;
91
+ stream.$pool = new Map();
92
92
 
93
- transport.pending.requestStreams.set(payload.id, stream)
93
+ transport.pending.requestStreams.set(payload.id, stream);
94
94
  }
95
95
 
96
96
  if (payload.sequence > (stream.$prevSeq + 1)) {
97
- stream.$pool.set(payload.sequence, payload)
98
- return isNew ? stream : null
97
+ stream.$pool.set(payload.sequence, payload);
98
+ return isNew ? stream : null;
99
99
  }
100
100
 
101
- stream.$prevSeq = payload.sequence
101
+ stream.$prevSeq = payload.sequence;
102
102
 
103
103
  if (stream.$prevSeq > 0) {
104
104
  if (!payload.isStream) {
105
- transport.log.debug('Stream ended', payload.sender)
105
+ transport.log.debug('Stream ended', payload.sender);
106
106
 
107
107
  // Todo: Handle errors
108
108
 
109
109
  // end of stream
110
- stream.end()
111
- transport.pending.requestStreams.delete(payload.id)
112
- return null
110
+ stream.end();
111
+ transport.pending.requestStreams.delete(payload.id);
112
+ return null;
113
113
  } else {
114
- transport.log.debug('Stream chunk received from ', payload.sender)
115
- stream.write(payload.chunk.type === 'Buffer' ? Buffer.from(payload.chunk.data) : payload.chunk)
114
+ transport.log.debug('Stream chunk received from ', payload.sender);
115
+ stream.write(payload.chunk.type === 'Buffer' ? Buffer.from(payload.chunk.data) : payload.chunk);
116
116
  }
117
117
  }
118
118
 
119
119
  if (stream.$pool.size > 0) {
120
- transport.log.debug(`Stream has stored packages. Size: ${stream.$pool.size}`, payload.sender)
121
- const nextSequence = stream.$prevSeq + 1
122
- const nextChunk = stream.$pool.get(nextSequence)
120
+ transport.log.debug(`Stream has stored packages. Size: ${stream.$pool.size}`, payload.sender);
121
+ const nextSequence = stream.$prevSeq + 1;
122
+ const nextChunk = stream.$pool.get(nextSequence);
123
123
  if (nextChunk) {
124
- stream.$pool.delete(nextSequence)
125
- setImmediate(() => onRequest(nextChunk))
124
+ stream.$pool.delete(nextSequence);
125
+ setImmediate(() => onRequest(nextChunk));
126
126
  }
127
127
  }
128
128
 
129
- return isNew ? stream : null
130
- }
129
+ return isNew ? stream : null;
130
+ };
131
131
 
132
132
  const handleIncomingResponseStream = (payload, request) => {
133
- let stream = transport.pending.responseStreams.get(payload.id)
133
+ let stream = transport.pending.responseStreams.get(payload.id);
134
134
 
135
135
  if (!stream && !payload.isStream) {
136
- return false
136
+ return false;
137
137
  }
138
138
 
139
139
  if (!stream) {
140
- transport.log.debug(`New stream from node ${payload.sender} received. Seq: ${payload.sequence}`)
140
+ transport.log.debug(`New stream from node ${payload.sender} received. Seq: ${payload.sequence}`);
141
141
 
142
142
  stream = new InboundTransformStream(
143
143
  payload.sender,
144
144
  payload.id, {
145
145
  objectMode: payload.meta && payload.meta.$isObjectModeStream
146
146
  }
147
- )
147
+ );
148
148
 
149
149
  // handle backpressure
150
150
  if (runtime.options.transport.streams.handleBackpressure) {
151
151
  stream.on('backpressure', async ({ sender, requestId }) => {
152
- const message = createMessage(MessageTypes.MESSAGE_RESPONSE_STREAM_BACKPRESSURE, sender, { id: requestId })
153
- await transport.send(message)
154
- })
152
+ const message = createMessage(MessageTypes.MESSAGE_RESPONSE_STREAM_BACKPRESSURE, sender, { id: requestId });
153
+ await transport.send(message);
154
+ });
155
155
 
156
156
  stream.on('resume_backpressure', async ({ sender, requestId }) => {
157
- const message = createMessage(MessageTypes.MESSAGE_RESPONSE_STREAM_RESUME, sender, { id: requestId })
158
- await transport.send(message)
159
- })
157
+ const message = createMessage(MessageTypes.MESSAGE_RESPONSE_STREAM_RESUME, sender, { id: requestId });
158
+ await transport.send(message);
159
+ });
160
160
  }
161
161
 
162
- stream.$prevSeq = -1
163
- stream.$pool = new Map()
162
+ stream.$prevSeq = -1;
163
+ stream.$pool = new Map();
164
164
 
165
- transport.pending.responseStreams.set(payload.id, stream)
166
- request.resolve(stream)
165
+ transport.pending.responseStreams.set(payload.id, stream);
166
+ request.resolve(stream);
167
167
  }
168
168
 
169
169
  if (payload.sequence > (stream.$prevSeq + 1)) {
170
- transport.log.debug(`Put the chunk into pool (size: ${stream.$pool.size}). Seq: ${payload.sequence}`)
170
+ transport.log.debug(`Put the chunk into pool (size: ${stream.$pool.size}). Seq: ${payload.sequence}`);
171
171
 
172
- stream.$pool.set(payload.sequence, payload)
173
- return true
172
+ stream.$pool.set(payload.sequence, payload);
173
+ return true;
174
174
  }
175
175
 
176
- stream.$prevSeq = payload.sequence
176
+ stream.$prevSeq = payload.sequence;
177
177
 
178
178
  if (stream.$prevSeq > 0) {
179
179
  if (!payload.isStream) {
180
- transport.log.debug('Stream ended', payload.sender)
180
+ transport.log.debug('Stream ended', payload.sender);
181
181
 
182
182
  // Todo: Handle errors
183
183
 
184
184
  // end of stream
185
- stream.end()
186
- transport.pending.responseStreams.delete(payload.id)
187
- return null
185
+ stream.end();
186
+ transport.pending.responseStreams.delete(payload.id);
187
+ return null;
188
188
  } else {
189
- transport.log.debug('Stream chunk received from ', payload.sender)
190
- stream.write(payload.chunk.type === 'Buffer' ? Buffer.from(payload.chunk.data) : payload.chunk)
189
+ transport.log.debug('Stream chunk received from ', payload.sender);
190
+ stream.write(payload.chunk.type === 'Buffer' ? Buffer.from(payload.chunk.data) : payload.chunk);
191
191
  }
192
192
  }
193
193
 
194
194
  if (stream.$pool.size > 0) {
195
- transport.log.debug(`Stream has stored packages. Size: ${stream.$pool.size}`, payload.sender)
196
- const nextSequence = stream.$prevSeq + 1
197
- const nextChunk = stream.$pool.get(nextSequence)
195
+ transport.log.debug(`Stream has stored packages. Size: ${stream.$pool.size}`, payload.sender);
196
+ const nextSequence = stream.$prevSeq + 1;
197
+ const nextChunk = stream.$pool.get(nextSequence);
198
198
  if (nextChunk) {
199
- stream.$pool.delete(nextSequence)
200
- setImmediate(() => onResponse(nextChunk))
199
+ stream.$pool.delete(nextSequence);
200
+ setImmediate(() => onResponse(nextChunk));
201
201
  }
202
202
  }
203
203
 
204
- return true
205
- }
204
+ return true;
205
+ };
206
206
 
207
207
  /**
208
208
  * Discovery handler
209
209
  * @param {any} payload - Payload
210
210
  * @returns {Promise} Promise
211
211
  */
212
- const onDiscovery = (payload) => transport.sendNodeInfo(payload.sender)
212
+ const onDiscovery = (payload) => transport.sendNodeInfo(payload.sender);
213
213
 
214
214
  /**
215
215
  * Node info handler
216
216
  * @param {any} payload - Payload
217
217
  * @returns {Promise} Promise
218
218
  */
219
- const onNodeInfos = (payload) => registry.processNodeInfo(payload)
219
+ const onNodeInfos = (payload) => registry.processNodeInfo(payload);
220
220
 
221
221
  /**
222
222
  * Request handler
@@ -224,45 +224,45 @@ module.exports = (runtime, transport) => {
224
224
  * @returns {Promise} Promise
225
225
  */
226
226
  const onRequest = (payload) => {
227
- const sender = payload.sender
227
+ const sender = payload.sender;
228
228
  try {
229
- let stream
229
+ let stream;
230
230
 
231
231
  // Handle incomming stream
232
232
  if (payload.isStream !== undefined) {
233
233
  // check for open stream.
234
- stream = handleIncomingRequestStream(payload)
234
+ stream = handleIncomingRequestStream(payload);
235
235
  if (!stream) {
236
- return Promise.resolve()
236
+ return Promise.resolve();
237
237
  }
238
238
  }
239
239
 
240
- const endpoint = registry.getLocalActionEndpoint(payload.action)
241
- const context = createContext(runtime)
240
+ const endpoint = registry.getLocalActionEndpoint(payload.action);
241
+ const context = createContext(runtime);
242
242
 
243
- context.setEndpoint(endpoint)
244
- context.id = payload.id
245
- context.setData(payload.data)
246
- context.parentId = payload.parentId
247
- context.requestId = payload.requestId
248
- context.meta = payload.meta || {}
249
- context.metrics = payload.metrics
250
- context.level = payload.level
251
- context.callerNodeId = payload.sender
252
- context.options.timeout = getRequestTimeout(payload)
243
+ context.setEndpoint(endpoint);
244
+ context.id = payload.id;
245
+ context.setData(payload.data);
246
+ context.parentId = payload.parentId;
247
+ context.requestId = payload.requestId;
248
+ context.meta = payload.meta || {};
249
+ context.metrics = payload.metrics;
250
+ context.level = payload.level;
251
+ context.callerNodeId = payload.sender;
252
+ context.options.timeout = getRequestTimeout(payload);
253
253
 
254
254
  // If payload is a stream, attach stream to context
255
255
  if (payload.isStream) {
256
- context.stream = stream
256
+ context.stream = stream;
257
257
  }
258
258
 
259
259
  return localRequestProxy(context)
260
260
  .then(data => transport.sendResponse(sender, payload.id, data, context.meta, null))
261
- .catch(error => transport.sendResponse(sender, payload.id, null, context.meta, error))
261
+ .catch(error => transport.sendResponse(sender, payload.id, null, context.meta, error));
262
262
  } catch (error) {
263
- return transport.sendResponse(sender, payload.id, null, payload.meta, error)
263
+ return transport.sendResponse(sender, payload.id, null, payload.meta, error);
264
264
  }
265
- }
265
+ };
266
266
 
267
267
  /**
268
268
  * Response handler
@@ -270,49 +270,49 @@ module.exports = (runtime, transport) => {
270
270
  * @returns {Promise} Promise
271
271
  */
272
272
  const onResponse = (payload) => {
273
- const id = payload.id
274
- const request = transport.pending.requests.get(id)
273
+ const id = payload.id;
274
+ const request = transport.pending.requests.get(id);
275
275
 
276
276
  if (!request) {
277
- return Promise.resolve()
277
+ return Promise.resolve();
278
278
  }
279
279
 
280
280
  // Merge meta data from response
281
- Object.assign(request.context.meta, payload.meta)
281
+ Object.assign(request.context.meta, payload.meta);
282
282
 
283
283
  // Handle streams
284
284
  if (payload.isStream != null) {
285
285
  if (handleIncomingResponseStream(payload, request)) {
286
- return
286
+ return;
287
287
  }
288
288
  }
289
289
 
290
- transport.pending.requests.delete(payload.id)
290
+ transport.pending.requests.delete(payload.id);
291
291
 
292
292
  // Response is an error
293
293
  if (!payload.success) {
294
- let error = restoreError(payload.error)
294
+ let error = restoreError(payload.error);
295
295
 
296
296
  if (!error) {
297
- error = new Error(payload.error.message)
298
- error.name = payload.error.name
299
- error.code = payload.error.code
300
- error.type = payload.error.type
301
- error.data = payload.error.data
297
+ error = new Error(payload.error.message);
298
+ error.name = payload.error.name;
299
+ error.code = payload.error.code;
300
+ error.type = payload.error.type;
301
+ error.data = payload.error.data;
302
302
  }
303
303
 
304
- error.retryable = payload.error.retryable
305
- error.nodeId = error.nodeId || payload.sender
304
+ error.retryable = payload.error.retryable;
305
+ error.nodeId = error.nodeId || payload.sender;
306
306
 
307
307
  if (payload.error.stack) {
308
- error.stack = payload.error.stack
308
+ error.stack = payload.error.stack;
309
309
  }
310
310
 
311
- request.reject(error)
311
+ request.reject(error);
312
312
  }
313
313
 
314
- request.resolve(payload.data)
315
- }
314
+ request.resolve(payload.data);
315
+ };
316
316
 
317
317
  /**
318
318
  * Ping handler
@@ -323,10 +323,10 @@ module.exports = (runtime, transport) => {
323
323
  const message = createMessage(MessageTypes.MESSAGE_PONG, payload.sender, {
324
324
  dispatchTime: payload.dispatchTime,
325
325
  arrivalTime: Date.now()
326
- })
326
+ });
327
327
 
328
- return transport.send(message)
329
- }
328
+ return transport.send(message);
329
+ };
330
330
 
331
331
  /**
332
332
  * Pong handler
@@ -334,16 +334,16 @@ module.exports = (runtime, transport) => {
334
334
  * @returns {void}
335
335
  */
336
336
  const onPong = (payload) => {
337
- const now = Date.now()
338
- const elapsedTime = now - payload.dispatchTime
339
- const timeDiff = Math.round(now - payload.arrivalTime - elapsedTime / 2)
337
+ const now = Date.now();
338
+ const elapsedTime = now - payload.dispatchTime;
339
+ const timeDiff = Math.round(now - payload.arrivalTime - elapsedTime / 2);
340
340
 
341
341
  runtime.eventBus.broadcastLocal('$node.pong', {
342
342
  nodeId: payload.sender,
343
343
  elapsedTime,
344
344
  timeDiff
345
- })
346
- }
345
+ });
346
+ };
347
347
 
348
348
  /**
349
349
  * Event handler
@@ -351,35 +351,35 @@ module.exports = (runtime, transport) => {
351
351
  * @returns {Promise} Promise
352
352
  */
353
353
  const onEvent = (payload) => {
354
- runtime.log.debug(`Received event "${payload.eventName}"`)
354
+ runtime.log.debug(`Received event "${payload.eventName}"`);
355
355
 
356
356
  if (!runtime.state.isStarted) {
357
- return
357
+ return;
358
358
  }
359
359
 
360
360
  // todo: reconstruct event context
361
- const context = createContext(runtime)
361
+ const context = createContext(runtime);
362
362
 
363
363
  // context.setEndpoint(endpoint)
364
- context.id = payload.id
365
- context.setData(payload.data)
366
- context.parentId = payload.parentId
367
- context.requestId = payload.requestId
368
- context.meta = payload.meta || {}
369
- context.metrics = payload.metrics
370
- context.level = payload.level
371
- context.callerNodeId = payload.sender
364
+ context.id = payload.id;
365
+ context.setData(payload.data);
366
+ context.parentId = payload.parentId;
367
+ context.requestId = payload.requestId;
368
+ context.meta = payload.meta || {};
369
+ context.metrics = payload.metrics;
370
+ context.level = payload.level;
371
+ context.callerNodeId = payload.sender;
372
372
 
373
373
  if (payload.timeout) {
374
- context.options.timeout = payload.timeout
374
+ context.options.timeout = payload.timeout;
375
375
  }
376
376
 
377
377
  // add event infos
378
- context.eventName = payload.eventName
379
- context.eventType = payload.isBroadcast ? 'broadcast' : 'emit'
378
+ context.eventName = payload.eventName;
379
+ context.eventType = payload.isBroadcast ? 'broadcast' : 'emit';
380
380
 
381
- return registry.eventCollection.emitLocal(context)
382
- }
381
+ return registry.eventCollection.emitLocal(context);
382
+ };
383
383
 
384
384
  /**
385
385
  * Disconnect handler
@@ -387,8 +387,8 @@ module.exports = (runtime, transport) => {
387
387
  * @returns {void}
388
388
  */
389
389
  const onDisconnect = (payload) => {
390
- registry.nodeDisconnected(payload.sender, false)
391
- }
390
+ registry.nodeDisconnected(payload.sender, false);
391
+ };
392
392
 
393
393
  /**
394
394
  * Heartbeat handler
@@ -396,65 +396,65 @@ module.exports = (runtime, transport) => {
396
396
  * @returns {void}
397
397
  */
398
398
  const onHeartbeat = (payload) => {
399
- transport.log.verbose(`Heartbeat from ${payload.sender}`)
400
- const node = registry.nodeCollection.get(payload.sender)
399
+ transport.log.verbose(`Heartbeat from ${payload.sender}`);
400
+ const node = registry.nodeCollection.get(payload.sender);
401
401
  // if node is unknown then request a node info message.
402
402
  if (node) {
403
403
  if (!node.isAvailable) {
404
- transport.log.debug('Known node. Propably reconnected.')
404
+ transport.log.debug('Known node. Propably reconnected.');
405
405
  // unknown node. request info message.
406
- transport.discoverNode(payload.sender)
406
+ transport.discoverNode(payload.sender);
407
407
  } else {
408
- node.heartbeat(payload)
408
+ node.heartbeat(payload);
409
409
  }
410
410
  } else {
411
411
  // unknown node. request info message.
412
- transport.discoverNode(payload.sender)
412
+ transport.discoverNode(payload.sender);
413
413
  }
414
- }
414
+ };
415
415
 
416
416
  const onResponseStreamBackpressure = (payload) => {
417
- const stream = transport.pending.outboundResponseStreams.get(payload.id)
417
+ const stream = transport.pending.outboundResponseStreams.get(payload.id);
418
418
 
419
419
  if (stream) {
420
- stream.pause()
420
+ stream.pause();
421
421
  }
422
- }
422
+ };
423
423
 
424
424
  const onResponseStreamResume = (payload) => {
425
- const stream = transport.pending.outboundResponseStreams.get(payload.id)
425
+ const stream = transport.pending.outboundResponseStreams.get(payload.id);
426
426
 
427
427
  if (stream) {
428
- stream.resume()
428
+ stream.resume();
429
429
  }
430
- }
430
+ };
431
431
 
432
432
  const onRequestStreamBackpressure = (payload) => {
433
- const stream = transport.pending.outboundRequestStreams.get(payload.id)
433
+ const stream = transport.pending.outboundRequestStreams.get(payload.id);
434
434
 
435
435
  if (stream) {
436
- stream.pause()
436
+ stream.pause();
437
437
  }
438
- }
438
+ };
439
439
 
440
440
  const onRequestStreamResume = (payload) => {
441
- const stream = transport.pending.outboundRequestStreams.get(payload.id)
441
+ const stream = transport.pending.outboundRequestStreams.get(payload.id);
442
442
 
443
443
  if (stream) {
444
- stream.resume()
444
+ stream.resume();
445
445
  }
446
- }
446
+ };
447
447
 
448
448
  return (type, data) => {
449
449
  try {
450
450
  if (data === null) {
451
- runtime.handleError(new WeaveError('Packet missing!'))
451
+ runtime.handleError(new WeaveError('Packet missing!'));
452
452
  }
453
453
 
454
- const payload = data.payload
454
+ const payload = data.payload;
455
455
 
456
456
  if (!payload) {
457
- runtime.handleError(new WeaveError('Message payload missing!'))
457
+ runtime.handleError(new WeaveError('Message payload missing!'));
458
458
  }
459
459
 
460
460
  // todo: check protocol version
@@ -468,50 +468,50 @@ module.exports = (runtime, transport) => {
468
468
 
469
469
  switch (type) {
470
470
  case MessageTypes.MESSAGE_DISCOVERY:
471
- onDiscovery(payload)
472
- break
471
+ onDiscovery(payload);
472
+ break;
473
473
  case MessageTypes.MESSAGE_INFO:
474
- onNodeInfos(payload)
475
- break
474
+ onNodeInfos(payload);
475
+ break;
476
476
  case MessageTypes.MESSAGE_REQUEST:
477
- onRequest(payload)
478
- break
477
+ onRequest(payload);
478
+ break;
479
479
  case MessageTypes.MESSAGE_RESPONSE:
480
- onResponse(payload)
481
- break
480
+ onResponse(payload);
481
+ break;
482
482
  case MessageTypes.MESSAGE_PING:
483
- onPing(payload)
484
- break
483
+ onPing(payload);
484
+ break;
485
485
  case MessageTypes.MESSAGE_PONG:
486
- onPong(payload)
487
- break
486
+ onPong(payload);
487
+ break;
488
488
  case MessageTypes.MESSAGE_DISCONNECT:
489
- onDisconnect(payload)
490
- break
489
+ onDisconnect(payload);
490
+ break;
491
491
  case MessageTypes.MESSAGE_HEARTBEAT:
492
- onHeartbeat(payload)
493
- break
492
+ onHeartbeat(payload);
493
+ break;
494
494
  case MessageTypes.MESSAGE_EVENT:
495
- onEvent(payload)
496
- break
495
+ onEvent(payload);
496
+ break;
497
497
  case MessageTypes.MESSAGE_RESPONSE_STREAM_BACKPRESSURE:
498
- onResponseStreamBackpressure(payload)
499
- break
498
+ onResponseStreamBackpressure(payload);
499
+ break;
500
500
  case MessageTypes.MESSAGE_RESPONSE_STREAM_RESUME:
501
- onResponseStreamResume(payload)
502
- break
501
+ onResponseStreamResume(payload);
502
+ break;
503
503
  case MessageTypes.MESSAGE_REQUEST_STREAM_BACKPRESSURE:
504
- onRequestStreamBackpressure(payload)
505
- break
504
+ onRequestStreamBackpressure(payload);
505
+ break;
506
506
  case MessageTypes.MESSAGE_REQUEST_STREAM_RESUME:
507
- onRequestStreamResume(payload)
508
- break
507
+ onRequestStreamResume(payload);
508
+ break;
509
509
  }
510
510
 
511
- return true
511
+ return true;
512
512
  } catch (error) {
513
- transport.log.error(error, data)
513
+ transport.log.error(error, data);
514
514
  }
515
- return false
516
- }
517
- }
515
+ return false;
516
+ };
517
+ };