@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.
- package/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +33 -0
- package/lib/broker/context.js +58 -46
- package/lib/broker/{default-options.js → defaultOptions.js} +31 -13
- package/lib/broker/index.js +273 -251
- package/lib/buildRuntime.js +70 -0
- package/lib/cache/adapters/base.js +119 -0
- package/lib/cache/adapters/inMemory.js +154 -0
- package/lib/cache/adapters/inMemoryLru.js +140 -0
- package/lib/cache/adapters/index.js +10 -0
- package/lib/cache/getCacheKeyByObject.js +31 -0
- package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
- package/lib/cache/lock.js +34 -11
- package/lib/constants.js +11 -8
- package/lib/{error-handler.js → errorHandler.js} +8 -8
- package/lib/errors.js +46 -39
- package/lib/helper/{define-action.js → defineAction.js} +5 -3
- package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +4 -2
- package/lib/helper/{define-service.js → defineService.js} +6 -3
- package/lib/index.js +25 -22
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -14
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +28 -25
- package/lib/logger/levels.js +23 -22
- package/lib/logger/tools.js +17 -13
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +89 -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 +82 -34
- package/lib/middlewares/bulkhead/index.js +34 -33
- package/lib/middlewares/cache/index.js +106 -0
- package/lib/middlewares/circuit-breaker/index.js +85 -60
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +14 -14
- package/lib/middlewares/index.js +3 -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 +18 -18
- package/lib/middlewares/tracing/index.js +50 -37
- package/lib/middlewares/tracing/tags.js +43 -0
- package/lib/middlewares/validator/index.js +32 -32
- package/lib/registry/{action-endpoint.js → actionEndpoint.js} +8 -8
- package/lib/registry/collections/{action-collection.js → actionCollection.js} +39 -35
- package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +64 -65
- package/lib/registry/collections/{event-collection.js → eventCollection.js} +65 -69
- package/lib/registry/collections/{node-collection.js → nodeCollection.js} +45 -45
- package/lib/registry/collections/{service-collection.js → serviceCollection.js} +61 -55
- package/lib/registry/eventEndpoint.js +26 -0
- 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 +147 -163
- 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/{service-item.js → serviceItem.js} +21 -21
- package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +21 -21
- package/lib/runtime/initCache.js +19 -0
- package/lib/runtime/{init-context-factory.js → initContextFactory.js} +22 -22
- package/lib/runtime/{init-eventbus.js → initEventbus.js} +54 -53
- package/lib/runtime/{init-logger.js → initLogger.js} +11 -11
- package/lib/runtime/{init-metrics.js → initMetrics.js} +55 -54
- package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +21 -21
- package/lib/runtime/{init-registry.js → initRegistry.js} +6 -6
- package/lib/runtime/initServiceManager.js +145 -0
- package/lib/runtime/{init-tracing.js → initTracing.js} +25 -24
- package/lib/runtime/{init-transport.js → initTransport.js} +5 -5
- package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +4 -4
- package/lib/runtime/{init-validator.js → initValidator.js} +5 -5
- package/lib/tracing/collectors/base.js +29 -37
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +16 -16
- package/lib/tracing/span.js +34 -32
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +75 -0
- package/lib/transport/adapters/adapteBase.js +107 -0
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +19 -19
- 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 +90 -76
- package/lib/transport/adapters/tcp/index.js +180 -175
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -38
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +58 -58
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +11 -0
- package/lib/transport/createTransport.js +377 -334
- package/lib/transport/errorPayloadFactory.js +15 -0
- package/lib/transport/messageHandlers.js +517 -0
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +95 -71
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +47 -45
- package/lib/utils/{restore-error.js → restoreError.js} +4 -4
- package/package.json +6 -12
- package/CHANGELOG.md +0 -1334
- package/lib/build-runtime.js +0 -70
- package/lib/cache/base.js +0 -142
- package/lib/cache/index.js +0 -48
- package/lib/cache/memory.js +0 -121
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- package/lib/registry/event-endpoint.js +0 -26
- package/lib/runtime/init-cache.js +0 -10
- package/lib/runtime/init-service-manager.js +0 -128
- package/lib/tracing/collectors/console.js +0 -34
- package/lib/tracing/index.js +0 -3
- package/lib/tracing/tracer.js +0 -64
- package/lib/transport/adapters/adapter-base.js +0 -92
- package/lib/transport/message-handlers.js +0 -448
- package/lib/transport/message-types.js +0 -59
- package/lib/transport/message.js +0 -16
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
|
-
* -----
|
|
4
|
-
* Copyright 2021 Fachwerk
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const EventEmitter = require('events').EventEmitter
|
|
8
|
-
|
|
9
|
-
const createTransportBase = () => {
|
|
10
|
-
let prefix = 'weave'
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
name: null,
|
|
14
|
-
bus: new EventEmitter(),
|
|
15
|
-
afterInit: null,
|
|
16
|
-
isConnected: false,
|
|
17
|
-
interruptCounter: 0,
|
|
18
|
-
repeatAttemptCounter: 0,
|
|
19
|
-
init (broker, transport, messageHandler) {
|
|
20
|
-
this.broker = broker
|
|
21
|
-
this.transport = transport
|
|
22
|
-
this.log = transport.log
|
|
23
|
-
this.messageHandler = messageHandler
|
|
24
|
-
|
|
25
|
-
if (broker.options.namespace) {
|
|
26
|
-
prefix = `${prefix}-${broker.options.namespace}`
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (this.afterInit) {
|
|
30
|
-
this.afterInit()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return Promise.resolve()
|
|
34
|
-
},
|
|
35
|
-
subscribe () {
|
|
36
|
-
return Promise.resolve()
|
|
37
|
-
},
|
|
38
|
-
/**
|
|
39
|
-
*
|
|
40
|
-
* Connection handler
|
|
41
|
-
* @instance
|
|
42
|
-
* @param {*} connectionEventParams Connection event
|
|
43
|
-
* @param {boolean} [startHeartbeatTimers=true] Start timers for this adapter
|
|
44
|
-
* @returns {void}
|
|
45
|
-
*/
|
|
46
|
-
connected (connectionEventParams = {}) {
|
|
47
|
-
this.bus.emit('$adapter.connected', connectionEventParams)
|
|
48
|
-
},
|
|
49
|
-
disconnected () {
|
|
50
|
-
this.bus.emit('$adapter.disconnected')
|
|
51
|
-
},
|
|
52
|
-
getTopic (cmd, nodeId) {
|
|
53
|
-
const topic = prefix + '.' + cmd + (nodeId ? '.' + nodeId : '')
|
|
54
|
-
return topic
|
|
55
|
-
},
|
|
56
|
-
preSend (packet) {
|
|
57
|
-
return this.send(packet)
|
|
58
|
-
},
|
|
59
|
-
send (/* message*/) {
|
|
60
|
-
this.broker.handleError(new Error('Method "send" not implemented.'))
|
|
61
|
-
},
|
|
62
|
-
incomingMessage (messageType, message) {
|
|
63
|
-
const data = this.deserialize(message)
|
|
64
|
-
this.updateStatisticReceived(message.length)
|
|
65
|
-
this.bus.emit('$adapter.message', messageType, data)
|
|
66
|
-
},
|
|
67
|
-
serialize (packet) {
|
|
68
|
-
try {
|
|
69
|
-
// Add the sender to each outgoing message
|
|
70
|
-
packet.payload.sender = this.broker.nodeId
|
|
71
|
-
return Buffer.from(JSON.stringify(packet))
|
|
72
|
-
} catch (error) {
|
|
73
|
-
this.broker.handleError(error)
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
deserialize (packet) {
|
|
77
|
-
try {
|
|
78
|
-
return JSON.parse(packet)
|
|
79
|
-
} catch (error) {
|
|
80
|
-
this.broker.handleError(error)
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
updateStatisticReceived (length) {
|
|
84
|
-
this.transport.statistics.received.packages += length
|
|
85
|
-
},
|
|
86
|
-
updateStatisticSent (length) {
|
|
87
|
-
this.transport.statistics.sent.packages += length
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
module.exports = createTransportBase
|
|
@@ -1,448 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
|
-
* -----
|
|
4
|
-
* Copyright 2021 Fachwerk
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const { Transform } = require('stream')
|
|
8
|
-
const { WeaveError, restoreError } = require('../errors')
|
|
9
|
-
const { createContext } = require('../broker/context')
|
|
10
|
-
const MessageTypes = require('./message-types')
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {import('../types').Transport} Transport
|
|
14
|
-
* @typedef {import('../types').Runtime} Runtime
|
|
15
|
-
* @typedef {import('../types').TransportMessageHandler} TransportMessageHandler
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @param {Runtime} runtime - Runtime reference
|
|
20
|
-
* @param {Transport} transport - Transport refence
|
|
21
|
-
* @returns {TransportMessageHandler} - Message handler
|
|
22
|
-
*/
|
|
23
|
-
module.exports = (runtime, transport) => {
|
|
24
|
-
const registry = runtime.registry
|
|
25
|
-
|
|
26
|
-
const getRequestTimeout = (payload) => {
|
|
27
|
-
return payload.timeout || runtime.options.registry.requestTimeout || 0
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const localRequestProxy = (context) => {
|
|
31
|
-
const actionName = context.action.name
|
|
32
|
-
|
|
33
|
-
// Get available endpoints
|
|
34
|
-
const endpointList = registry.getActionEndpoints(actionName)
|
|
35
|
-
|
|
36
|
-
// Reject the request if no local endpoint can be found.
|
|
37
|
-
if (endpointList == null || !endpointList.hasLocal()) {
|
|
38
|
-
transport.log.warn(`Service ${actionName} not found localy.`)
|
|
39
|
-
return Promise.reject('Service not found')
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// From all available local endpoints - get one.
|
|
43
|
-
const endpoint = endpointList.getNextLocalEndpoint()
|
|
44
|
-
|
|
45
|
-
// if there is no endpoint, reject
|
|
46
|
-
if (!endpoint) {
|
|
47
|
-
transport.log.warn(`Service ${actionName} is not available localy.`)
|
|
48
|
-
return Promise.reject('Service not found')
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Call the local action handler with context
|
|
52
|
-
const promise = endpoint.action.handler(context)
|
|
53
|
-
promise.context = context
|
|
54
|
-
|
|
55
|
-
return promise
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const handleIncomingRequestStream = (payload) => {
|
|
59
|
-
// check for open stream.
|
|
60
|
-
let stream = transport.pending.requestStreams.get(payload.id)
|
|
61
|
-
let isNew = false
|
|
62
|
-
|
|
63
|
-
if (!payload.isStream && !stream) {
|
|
64
|
-
return false
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (!stream) {
|
|
68
|
-
isNew = true
|
|
69
|
-
stream = new Transform({
|
|
70
|
-
objectMode: payload.meta && payload.meta.$isObjectModeStream,
|
|
71
|
-
transform: function (chunk, encoding, done) {
|
|
72
|
-
this.push(chunk)
|
|
73
|
-
return done()
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
stream.$prevSeq = -1
|
|
78
|
-
stream.$pool = new Map()
|
|
79
|
-
|
|
80
|
-
transport.pending.requestStreams.set(payload.id, stream)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (payload.sequence > (stream.$prevSeq + 1)) {
|
|
84
|
-
stream.$pool.set(payload.sequence, payload)
|
|
85
|
-
return isNew ? stream : null
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
stream.$prevSeq = payload.sequence
|
|
89
|
-
|
|
90
|
-
if (stream.$prevSeq > 0) {
|
|
91
|
-
if (!payload.isStream) {
|
|
92
|
-
transport.log.debug('Stream ended', payload.sender)
|
|
93
|
-
|
|
94
|
-
// Todo: Handle errors
|
|
95
|
-
|
|
96
|
-
// end of stream
|
|
97
|
-
stream.end()
|
|
98
|
-
transport.pending.requestStreams.delete(payload.id)
|
|
99
|
-
return null
|
|
100
|
-
} else {
|
|
101
|
-
transport.log.debug('Stream chunk received from ', payload.sender)
|
|
102
|
-
stream.write(payload.chunk.type === 'Buffer' ? Buffer.from(payload.chunk.data) : payload.chunk)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (stream.$pool.size > 0) {
|
|
107
|
-
transport.log.debug(`Stream has stored packages. Size: ${stream.$pool.size}`, payload.sender)
|
|
108
|
-
const nextSequence = stream.$prevSeq + 1
|
|
109
|
-
const nextChunk = stream.$pool.get(nextSequence)
|
|
110
|
-
if (nextChunk) {
|
|
111
|
-
stream.$pool.delete(nextSequence)
|
|
112
|
-
setImmediate(() => onRequest(nextChunk))
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return isNew ? stream : null
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const handleIncomingResponseStream = (payload, request) => {
|
|
120
|
-
let stream = transport.pending.responseStreams.get(payload.id)
|
|
121
|
-
|
|
122
|
-
if (!stream && !payload.isStream) {
|
|
123
|
-
return false
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (!stream) {
|
|
127
|
-
transport.log.debug(`New stream from node ${payload.sender} received. Seq: ${payload.sequence}`)
|
|
128
|
-
|
|
129
|
-
stream = new Transform({
|
|
130
|
-
objectMode: payload.meta && payload.meta.$isObjectModeStream,
|
|
131
|
-
transform: function (chunk, encoding, done) {
|
|
132
|
-
this.push(chunk)
|
|
133
|
-
return done()
|
|
134
|
-
}
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
stream.$prevSeq = -1
|
|
138
|
-
stream.$pool = new Map()
|
|
139
|
-
|
|
140
|
-
transport.pending.responseStreams.set(payload.id, stream)
|
|
141
|
-
request.resolve(stream)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (payload.sequence > (stream.$prevSeq + 1)) {
|
|
145
|
-
transport.log.debug(`Put the chunk into pool (size: ${stream.$pool.size}). Seq: ${payload.sequence}`)
|
|
146
|
-
|
|
147
|
-
stream.$pool.set(payload.sequence, payload)
|
|
148
|
-
return true
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
stream.$prevSeq = payload.sequence
|
|
152
|
-
|
|
153
|
-
if (stream.$prevSeq > 0) {
|
|
154
|
-
if (!payload.isStream) {
|
|
155
|
-
transport.log.debug('Stream ended', payload.sender)
|
|
156
|
-
|
|
157
|
-
// Todo: Handle errors
|
|
158
|
-
|
|
159
|
-
// end of stream
|
|
160
|
-
stream.end()
|
|
161
|
-
transport.pending.responseStreams.delete(payload.id)
|
|
162
|
-
return null
|
|
163
|
-
} else {
|
|
164
|
-
transport.log.debug('Stream chunk received from ', payload.sender)
|
|
165
|
-
stream.write(payload.chunk.type === 'Buffer' ? Buffer.from(payload.chunk.data) : payload.chunk)
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (stream.$pool.size > 0) {
|
|
170
|
-
transport.log.debug(`Stream has stored packages. Size: ${stream.$pool.size}`, payload.sender)
|
|
171
|
-
const nextSequence = stream.$prevSeq + 1
|
|
172
|
-
const nextChunk = stream.$pool.get(nextSequence)
|
|
173
|
-
if (nextChunk) {
|
|
174
|
-
stream.$pool.delete(nextSequence)
|
|
175
|
-
setImmediate(() => onResponse(nextChunk))
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return true
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Discovery handler
|
|
184
|
-
* @param {any} payload - Payload
|
|
185
|
-
* @returns {Promise} Promise
|
|
186
|
-
*/
|
|
187
|
-
const onDiscovery = (payload) => transport.sendNodeInfo(payload.sender)
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Node info handler
|
|
191
|
-
* @param {any} payload - Payload
|
|
192
|
-
* @returns {Promise} Promise
|
|
193
|
-
*/
|
|
194
|
-
const onNodeInfos = (payload) => registry.processNodeInfo(payload)
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Request handler
|
|
198
|
-
* @param {any} payload - Payload
|
|
199
|
-
* @returns {Promise} Promise
|
|
200
|
-
*/
|
|
201
|
-
const onRequest = (payload) => {
|
|
202
|
-
const sender = payload.sender
|
|
203
|
-
try {
|
|
204
|
-
let stream
|
|
205
|
-
|
|
206
|
-
// Handle incomming stream
|
|
207
|
-
if (payload.isStream !== undefined) {
|
|
208
|
-
// check for open stream.
|
|
209
|
-
stream = handleIncomingRequestStream(payload)
|
|
210
|
-
if (!stream) {
|
|
211
|
-
return Promise.resolve()
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const endpoint = registry.getLocalActionEndpoint(payload.action)
|
|
216
|
-
const context = createContext(runtime)
|
|
217
|
-
|
|
218
|
-
context.setEndpoint(endpoint)
|
|
219
|
-
context.id = payload.id
|
|
220
|
-
context.setData(payload.data)
|
|
221
|
-
context.parentId = payload.parentId
|
|
222
|
-
context.requestId = payload.requestId
|
|
223
|
-
context.meta = payload.meta || {}
|
|
224
|
-
context.metrics = payload.metrics
|
|
225
|
-
context.level = payload.level
|
|
226
|
-
context.callerNodeId = payload.sender
|
|
227
|
-
context.options.timeout = getRequestTimeout(payload)
|
|
228
|
-
|
|
229
|
-
// If payload is a stream, attach stream to context
|
|
230
|
-
if (payload.isStream) {
|
|
231
|
-
context.stream = stream
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return localRequestProxy(context)
|
|
235
|
-
.then(data => transport.response(sender, payload.id, data, context.meta, null))
|
|
236
|
-
.catch(error => transport.response(sender, payload.id, null, context.meta, error))
|
|
237
|
-
} catch (error) {
|
|
238
|
-
return transport.response(sender, payload.id, null, payload.meta, error)
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Response handler
|
|
244
|
-
* @param {any} payload - Payload
|
|
245
|
-
* @returns {Promise} Promise
|
|
246
|
-
*/
|
|
247
|
-
const onResponse = payload => {
|
|
248
|
-
const id = payload.id
|
|
249
|
-
const request = transport.pending.requests.get(id)
|
|
250
|
-
|
|
251
|
-
if (!request) {
|
|
252
|
-
return Promise.resolve()
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Merge meta data from response
|
|
256
|
-
Object.assign(request.context.meta, payload.meta)
|
|
257
|
-
|
|
258
|
-
// Handle streams
|
|
259
|
-
if (payload.isStream != null) {
|
|
260
|
-
if (handleIncomingResponseStream(payload, request)) {
|
|
261
|
-
return
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
transport.pending.requests.delete(payload.id)
|
|
266
|
-
|
|
267
|
-
// Response is an error
|
|
268
|
-
if (!payload.success) {
|
|
269
|
-
let error = restoreError(payload.error)
|
|
270
|
-
|
|
271
|
-
if (!error) {
|
|
272
|
-
error = new Error(payload.error.message)
|
|
273
|
-
error.name = payload.error.name
|
|
274
|
-
error.code = payload.error.code
|
|
275
|
-
error.type = payload.error.type
|
|
276
|
-
error.data = payload.error.data
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
error.retryable = payload.error.retryable
|
|
280
|
-
error.nodeId = error.nodeId || payload.sender
|
|
281
|
-
|
|
282
|
-
if (payload.error.stack) {
|
|
283
|
-
error.stack = payload.error.stack
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
request.reject(error)
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
request.resolve(payload.data)
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Ping handler
|
|
294
|
-
* @param {any} payload - Payload
|
|
295
|
-
* @returns {Promise} Promise
|
|
296
|
-
*/
|
|
297
|
-
const onPing = payload => {
|
|
298
|
-
const message = transport.createMessage(MessageTypes.MESSAGE_PONG, payload.sender, {
|
|
299
|
-
dispatchTime: payload.dispatchTime,
|
|
300
|
-
arrivalTime: Date.now()
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
return transport.send(message)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Pong handler
|
|
308
|
-
* @param {any} payload - Payload
|
|
309
|
-
* @returns {void}
|
|
310
|
-
*/
|
|
311
|
-
const onPong = (payload) => {
|
|
312
|
-
const now = Date.now()
|
|
313
|
-
const elapsedTime = now - payload.dispatchTime
|
|
314
|
-
const timeDiff = Math.round(now - payload.arrivalTime - elapsedTime / 2)
|
|
315
|
-
|
|
316
|
-
runtime.eventBus.broadcastLocal('$node.pong', {
|
|
317
|
-
nodeId: payload.sender,
|
|
318
|
-
elapsedTime,
|
|
319
|
-
timeDiff
|
|
320
|
-
})
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Event handler
|
|
325
|
-
* @param {any} payload - Payload
|
|
326
|
-
* @returns {Promise} Promise
|
|
327
|
-
*/
|
|
328
|
-
const onEvent = (payload) => {
|
|
329
|
-
runtime.log.debug(`Received event "${payload.eventName}"`)
|
|
330
|
-
|
|
331
|
-
if (!runtime.state.isStarted) {
|
|
332
|
-
return
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// todo: reconstruct event context
|
|
336
|
-
const context = createContext(runtime)
|
|
337
|
-
|
|
338
|
-
// context.setEndpoint(endpoint)
|
|
339
|
-
context.id = payload.id
|
|
340
|
-
context.setData(payload.data)
|
|
341
|
-
context.parentId = payload.parentId
|
|
342
|
-
context.requestId = payload.requestId
|
|
343
|
-
context.meta = payload.meta || {}
|
|
344
|
-
context.metrics = payload.metrics
|
|
345
|
-
context.level = payload.level
|
|
346
|
-
context.callerNodeId = payload.sender
|
|
347
|
-
|
|
348
|
-
if (payload.timeout) {
|
|
349
|
-
context.options.timeout = payload.timeout
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
// add event infos
|
|
353
|
-
context.eventName = payload.eventName
|
|
354
|
-
context.eventType = payload.isBroadcast ? 'broadcast' : 'emit'
|
|
355
|
-
|
|
356
|
-
return registry.eventCollection.emitLocal(context)
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* Disconnect handler
|
|
361
|
-
* @param {any} payload - Payload
|
|
362
|
-
* @returns {void}
|
|
363
|
-
*/
|
|
364
|
-
const onDisconnect = (payload) => {
|
|
365
|
-
registry.nodeDisconnected(payload.sender, false)
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Heartbeat handler
|
|
370
|
-
* @param {any} payload - Payload
|
|
371
|
-
* @returns {void}
|
|
372
|
-
*/
|
|
373
|
-
const onHeartbeat = (payload) => {
|
|
374
|
-
transport.log.verbose(`Heartbeat from ${payload.sender}`)
|
|
375
|
-
const node = registry.nodeCollection.get(payload.sender)
|
|
376
|
-
// if node is unknown then request a node info message.
|
|
377
|
-
if (node) {
|
|
378
|
-
if (!node.isAvailable) {
|
|
379
|
-
transport.log.debug('Known node. Propably reconnected.')
|
|
380
|
-
// unknown node. request info message.
|
|
381
|
-
transport.discoverNode(payload.sender)
|
|
382
|
-
} else {
|
|
383
|
-
node.heartbeat(payload)
|
|
384
|
-
}
|
|
385
|
-
} else {
|
|
386
|
-
// unknown node. request info message.
|
|
387
|
-
transport.discoverNode(payload.sender)
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
return (type, data) => {
|
|
392
|
-
try {
|
|
393
|
-
if (data === null) {
|
|
394
|
-
runtime.handleError(new WeaveError('Packet missing!'))
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
const payload = data.payload
|
|
398
|
-
|
|
399
|
-
if (!payload) {
|
|
400
|
-
runtime.handleError(new WeaveError('Message payload missing!'))
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
// todo: check protocol version
|
|
404
|
-
// todo: check node ID conflict
|
|
405
|
-
|
|
406
|
-
// if (payload.sender === broker.nodeId) {
|
|
407
|
-
// if (type === MessageTypes.MESSAGE_INFO && payload.instanceId !== broker.state.instanceId) {
|
|
408
|
-
// return broker.fatalError('Broker has detected a node ID conflict. "nodeId" of broker needs to be unique. Broker will be stopped.')
|
|
409
|
-
// }
|
|
410
|
-
// }
|
|
411
|
-
|
|
412
|
-
switch (type) {
|
|
413
|
-
case MessageTypes.MESSAGE_DISCOVERY:
|
|
414
|
-
onDiscovery(payload)
|
|
415
|
-
break
|
|
416
|
-
case MessageTypes.MESSAGE_INFO:
|
|
417
|
-
onNodeInfos(payload)
|
|
418
|
-
break
|
|
419
|
-
case MessageTypes.MESSAGE_REQUEST:
|
|
420
|
-
onRequest(payload)
|
|
421
|
-
break
|
|
422
|
-
case MessageTypes.MESSAGE_RESPONSE:
|
|
423
|
-
onResponse(payload)
|
|
424
|
-
break
|
|
425
|
-
case MessageTypes.MESSAGE_PING:
|
|
426
|
-
onPing(payload)
|
|
427
|
-
break
|
|
428
|
-
case MessageTypes.MESSAGE_PONG:
|
|
429
|
-
onPong(payload)
|
|
430
|
-
break
|
|
431
|
-
case MessageTypes.MESSAGE_DISCONNECT:
|
|
432
|
-
onDisconnect(payload)
|
|
433
|
-
break
|
|
434
|
-
case MessageTypes.MESSAGE_HEARTBEAT:
|
|
435
|
-
onHeartbeat(payload)
|
|
436
|
-
break
|
|
437
|
-
case MessageTypes.MESSAGE_EVENT:
|
|
438
|
-
onEvent(payload)
|
|
439
|
-
break
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
return true
|
|
443
|
-
} catch (error) {
|
|
444
|
-
transport.log.error(error, data)
|
|
445
|
-
}
|
|
446
|
-
return false
|
|
447
|
-
}
|
|
448
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
|
-
* -----
|
|
4
|
-
* Copyright 2021 Fachwerk
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// const MESSAGE_UNKNOWN = 'unknown'
|
|
8
|
-
// const MESSAGE_DISCOVERY = 'discovery'
|
|
9
|
-
// const MESSAGE_INFO = 'info'
|
|
10
|
-
// const MESSAGE_REQUEST = 'request'
|
|
11
|
-
// const MESSAGE_RESPONSE = 'response'
|
|
12
|
-
// const MESSAGE_RESPONSE_STREAM_CHUNK = 'response_stream_chunk'
|
|
13
|
-
// const MESSAGE_RESPONSE_STREAM_END = 'response_stream_end'
|
|
14
|
-
// const MESSAGE_PING = 'ping'
|
|
15
|
-
// const MESSAGE_PONG = 'pong'
|
|
16
|
-
// const MESSAGE_DISCONNECT = 'disconnect'
|
|
17
|
-
// const MESSAGE_HEARTBEAT = 'heartbeat'
|
|
18
|
-
// const MESSAGE_EVENT = 'event'
|
|
19
|
-
|
|
20
|
-
// // gossip messages
|
|
21
|
-
// const MESSAGE_GOSSIP_REQUEST = 'gossip_request'
|
|
22
|
-
// const MESSAGE_GOSSIP_RESPONSE = 'gossip_response'
|
|
23
|
-
// const MESSAGE_GOSSIP_HELLO = 'gossip_hello'
|
|
24
|
-
|
|
25
|
-
// const messageTypes = {
|
|
26
|
-
// MESSAGE_UNKNOWN,
|
|
27
|
-
// MESSAGE_DISCOVERY,
|
|
28
|
-
// MESSAGE_INFO,
|
|
29
|
-
// MESSAGE_REQUEST,
|
|
30
|
-
// MESSAGE_RESPONSE,
|
|
31
|
-
// MESSAGE_RESPONSE_STREAM_CHUNK,
|
|
32
|
-
// MESSAGE_RESPONSE_STREAM_END,
|
|
33
|
-
// MESSAGE_PING,
|
|
34
|
-
// MESSAGE_PONG,
|
|
35
|
-
// MESSAGE_DISCONNECT,
|
|
36
|
-
// MESSAGE_HEARTBEAT,
|
|
37
|
-
// MESSAGE_EVENT,
|
|
38
|
-
// MESSAGE_GOSSIP_HELLO,
|
|
39
|
-
// MESSAGE_GOSSIP_RESPONSE,
|
|
40
|
-
// MESSAGE_GOSSIP_REQUEST
|
|
41
|
-
// }
|
|
42
|
-
|
|
43
|
-
// module.exports = messageTypes
|
|
44
|
-
|
|
45
|
-
exports.MESSAGE_UNKNOWN = 'unknown'
|
|
46
|
-
exports.MESSAGE_DISCOVERY = 'discovery'
|
|
47
|
-
exports.MESSAGE_INFO = 'info'
|
|
48
|
-
exports.MESSAGE_REQUEST = 'request'
|
|
49
|
-
exports.MESSAGE_RESPONSE = 'response'
|
|
50
|
-
exports.MESSAGE_RESPONSE_STREAM_CHUNK = 'response_stream_chunk'
|
|
51
|
-
exports.MESSAGE_RESPONSE_STREAM_END = 'response_stream_end'
|
|
52
|
-
exports.MESSAGE_PING = 'ping'
|
|
53
|
-
exports.MESSAGE_PONG = 'pong'
|
|
54
|
-
exports.MESSAGE_DISCONNECT = 'disconnect'
|
|
55
|
-
exports.MESSAGE_HEARTBEAT = 'heartbeat'
|
|
56
|
-
exports.MESSAGE_EVENT = 'event'
|
|
57
|
-
exports.MESSAGE_GOSSIP_HELLO = 'gossip_hello'
|
|
58
|
-
exports.MESSAGE_GOSSIP_RESPONSE = 'gossip_response'
|
|
59
|
-
exports.MESSAGE_GOSSIP_REQUEST = 'gossip_request'
|
package/lib/transport/message.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
|
-
* -----
|
|
4
|
-
* Copyright 2021 Fachwerk
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const makeMessage = (MessageTypes) =>
|
|
8
|
-
(type, targetNodeId, payload) => {
|
|
9
|
-
return {
|
|
10
|
-
type: type || MessageTypes.MESSAGE_UNKNOWN,
|
|
11
|
-
targetNodeId,
|
|
12
|
-
payload: Buffer.from(JSON.stringify(payload || {}))
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports = makeMessage
|