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