@weave-js/core 0.9.2 → 0.11.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/lib/ExtendableError.js +33 -0
- package/lib/broker/context.js +25 -13
- package/lib/broker/{default-options.js → defaultOptions.js} +23 -5
- package/lib/broker/index.js +224 -194
- package/lib/buildRuntime.js +70 -0
- package/lib/cache/base.js +47 -31
- package/lib/cache/inMemory.js +140 -0
- package/lib/cache/index.js +2 -2
- package/lib/cache/lock.js +21 -4
- package/lib/constants.js +8 -5
- package/lib/errorHandler.js +19 -0
- package/lib/errors.js +17 -10
- package/lib/helper/{define-action.js → defineAction.js} +3 -1
- package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +2 -0
- package/lib/helper/{define-service.js → defineService.js} +4 -1
- package/lib/index.js +15 -89
- package/lib/logger/base.js +3 -3
- package/lib/logger/format/asHumanReadable.js +42 -0
- package/lib/logger/format/asJson.js +25 -0
- package/lib/logger/format/index.js +4 -0
- package/lib/logger/index.js +12 -5
- package/lib/logger/levels.js +9 -8
- package/lib/logger/tools.js +4 -25
- package/lib/logger/utils/colorize.js +32 -0
- package/lib/logger/utils/format.js +18 -4
- package/lib/metrics/constants.js +8 -6
- package/lib/metrics/types/gauge.js +1 -1
- package/lib/middlewares/action-hooks/index.js +62 -14
- package/lib/middlewares/bulkhead/index.js +5 -4
- package/lib/middlewares/cache/index.js +80 -0
- package/lib/middlewares/circuit-breaker/index.js +41 -16
- package/lib/middlewares/context-tracker/index.js +3 -3
- package/lib/middlewares/error-handler/index.js +3 -3
- package/lib/middlewares/index.js +2 -1
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +2 -2
- package/lib/middlewares/metrics/index.js +2 -2
- package/lib/middlewares/retry/index.js +11 -10
- package/lib/middlewares/timeout/index.js +5 -5
- package/lib/middlewares/tracing/index.js +27 -14
- package/lib/middlewares/tracing/tags.js +43 -0
- package/lib/middlewares/validator/index.js +6 -6
- package/lib/registry/{action-endpoint.js → actionEndpoint.js} +0 -0
- package/lib/registry/collections/{action-collection.js → actionCollection.js} +2 -2
- package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +19 -3
- package/lib/registry/collections/{event-collection.js → eventCollection.js} +15 -9
- package/lib/registry/collections/{node-collection.js → nodeCollection.js} +1 -1
- package/lib/registry/collections/{service-collection.js → serviceCollection.js} +2 -2
- package/lib/registry/{event-endpoint.js → eventEndpoint.js} +1 -1
- package/lib/registry/node.js +1 -1
- package/lib/registry/registry.js +10 -23
- package/lib/registry/service/service.js +2 -2
- package/lib/registry/{service-item.js → serviceItem.js} +1 -1
- package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +3 -3
- package/lib/runtime/{init-cache.js → initCache.js} +0 -0
- package/lib/runtime/{init-context-factory.js → initContextFactory.js} +2 -2
- package/lib/runtime/{init-eventbus.js → initEventbus.js} +11 -10
- package/lib/runtime/{init-logger.js → initLogger.js} +0 -0
- package/lib/runtime/{init-metrics.js → initMetrics.js} +11 -10
- package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +1 -1
- package/lib/runtime/{init-registry.js → initRegistry.js} +1 -1
- package/lib/runtime/{init-service-manager.js → initServiceManager.js} +0 -0
- package/lib/runtime/{init-tracing.js → initTracing.js} +2 -1
- package/lib/runtime/{init-transport.js → initTransport.js} +1 -1
- package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +0 -0
- package/lib/runtime/{init-validator.js → initValidator.js} +1 -1
- package/lib/tracing/collectors/base.js +11 -19
- package/lib/tracing/collectors/event.js +2 -2
- package/lib/tracing/collectors/index.js +2 -2
- package/lib/tracing/span.js +8 -6
- package/lib/transport/InboundTransformStream.js +75 -0
- package/lib/transport/adapters/{adapter-base.js → adapteBase.js} +16 -1
- package/lib/transport/adapters/adapters.js +1 -1
- package/lib/transport/adapters/dummy/index.js +3 -3
- package/lib/transport/adapters/index.js +1 -1
- package/lib/transport/adapters/tcp/discovery/index.js +45 -31
- package/lib/transport/adapters/tcp/index.js +20 -13
- package/lib/transport/adapters/tcp/tcpReader.js +1 -0
- package/lib/transport/adapters/tcp/tcpWriteStream.js +1 -1
- package/lib/transport/adapters/tcp/tcpWriter.js +2 -2
- package/lib/transport/createMessage.js +11 -0
- package/lib/transport/{transport-factory.js → createTransport.js} +234 -173
- package/lib/transport/errorPayloadFactory.js +18 -0
- package/lib/transport/{message-handlers.js → messageHandlers.js} +93 -24
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +66 -66
- package/lib/utils/index.js +0 -1
- package/lib/utils/options.js +4 -2
- package/lib/utils/{restore-error.js → restoreError.js} +1 -1
- package/package.json +6 -12
- package/CHANGELOG.md +0 -1293
- package/lib/cache/memory.js +0 -91
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- 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/message-types.js +0 -59
- package/lib/transport/message.js +0 -16
|
@@ -12,7 +12,7 @@ const createDiscoveryService = (adapter, options) => {
|
|
|
12
12
|
const codec = Codec(options)
|
|
13
13
|
const bus = new EventEmitter()
|
|
14
14
|
const ips = getIpList(false)
|
|
15
|
-
const
|
|
15
|
+
const MESSAGE_TYPE_LENGTH = 1
|
|
16
16
|
|
|
17
17
|
let servers = []
|
|
18
18
|
let discoverTimer
|
|
@@ -20,32 +20,29 @@ const createDiscoveryService = (adapter, options) => {
|
|
|
20
20
|
const startServer = (host, port, multicastAddress) => {
|
|
21
21
|
return new Promise((resolve, reject) => {
|
|
22
22
|
try {
|
|
23
|
-
const socket = dgram.createSocket({
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const messageType = Buffer.prototype.readUInt8.call(message, 0)
|
|
27
|
-
const payload = message.slice(MESSAGE_TYPE_LENGHT)
|
|
28
|
-
|
|
29
|
-
switch (messageType) {
|
|
30
|
-
case messageTypes.HELLO:
|
|
31
|
-
onMessage(payload, info)
|
|
32
|
-
break
|
|
33
|
-
default:
|
|
34
|
-
onUnknown(payload, info)
|
|
35
|
-
}
|
|
23
|
+
const socket = dgram.createSocket({
|
|
24
|
+
type: options.discovery.type,
|
|
25
|
+
reuseAddr: options.discovery.udpReuseAddress
|
|
36
26
|
})
|
|
37
27
|
|
|
38
|
-
socket.
|
|
28
|
+
socket.on('message', onMessage)
|
|
29
|
+
|
|
30
|
+
socket.bind({ port, exclusive: true }, () => {
|
|
39
31
|
try {
|
|
40
32
|
if (multicastAddress) {
|
|
41
33
|
socket.setMulticastInterface(host)
|
|
42
34
|
socket.addMembership(multicastAddress, host)
|
|
43
35
|
socket.setMulticastTTL(1)
|
|
44
|
-
socket
|
|
36
|
+
// Add destination to socket object
|
|
37
|
+
Object.defineProperty(socket, 'destinations', {
|
|
38
|
+
value: [multicastAddress]
|
|
39
|
+
})
|
|
45
40
|
adapter.log.info(`UDP Server is listening on ${host}:${port}. Membership: ${multicastAddress}`)
|
|
46
41
|
} else {
|
|
47
42
|
socket.setBroadcast(true)
|
|
48
|
-
socket
|
|
43
|
+
Object.defineProperty(socket, 'destinations', {
|
|
44
|
+
value: getBroadcastAddresses()
|
|
45
|
+
})
|
|
49
46
|
}
|
|
50
47
|
} catch (error) {
|
|
51
48
|
adapter.log.info(`UDP Multicast membership error: ${error.message}`)
|
|
@@ -62,20 +59,25 @@ const createDiscoveryService = (adapter, options) => {
|
|
|
62
59
|
}
|
|
63
60
|
|
|
64
61
|
const onMessage = (buffer, info) => {
|
|
65
|
-
const
|
|
66
|
-
|
|
62
|
+
const messageType = Buffer.prototype.readUInt8.call(buffer, 0)
|
|
63
|
+
const payload = buffer.slice(MESSAGE_TYPE_LENGTH)
|
|
67
64
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
65
|
+
switch (messageType) {
|
|
66
|
+
case messageTypes.HELLO:
|
|
67
|
+
const message = codec.decode(payload)
|
|
68
|
+
message.host = info.address
|
|
72
69
|
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
if (message.namespace === namespace) {
|
|
71
|
+
bus.emit('message', message)
|
|
72
|
+
}
|
|
73
|
+
break
|
|
74
|
+
default:
|
|
75
|
+
adapter.log.debug(`Received an unknown data package from host "${info.address}"`)
|
|
76
|
+
}
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
function sendMessage (payload) {
|
|
78
|
-
const header = Buffer.alloc(
|
|
80
|
+
const header = Buffer.alloc(MESSAGE_TYPE_LENGTH)
|
|
79
81
|
Buffer.prototype.writeUInt8.call(header, messageTypes.HELLO, 0)
|
|
80
82
|
|
|
81
83
|
const message = Buffer.concat([header, codec.encode(payload)])
|
|
@@ -101,7 +103,6 @@ const createDiscoveryService = (adapter, options) => {
|
|
|
101
103
|
|
|
102
104
|
function startDiscovering (port) {
|
|
103
105
|
discoverTimer = setInterval(() => sendDiscoveryPackage(port), 2000)
|
|
104
|
-
|
|
105
106
|
discoverTimer.unref()
|
|
106
107
|
}
|
|
107
108
|
|
|
@@ -115,14 +116,27 @@ const createDiscoveryService = (adapter, options) => {
|
|
|
115
116
|
|
|
116
117
|
return {
|
|
117
118
|
bus,
|
|
118
|
-
|
|
119
|
+
async start (port) {
|
|
119
120
|
if (!options.discovery.enabled) {
|
|
120
121
|
return Promise.resolve()
|
|
121
122
|
}
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
.
|
|
124
|
+
// UDP Multicast
|
|
125
|
+
if (options.discovery.udpMulticast) {
|
|
126
|
+
if (options.discovery.multicastAddress) {
|
|
127
|
+
await Promise.all(ips.map(ip => startServer(ip, options.discovery.port, options.discovery.multicastAddress)))
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// UDP Broadcast
|
|
132
|
+
if (options.discovery.udpBroadcast) {
|
|
133
|
+
await startServer(options.discovery.udpBindAddress, options.discovery.port, options.discovery.multicastAddress)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// send the first dis
|
|
137
|
+
const timeout = Math.floor(Math.random() * 500) + 500
|
|
138
|
+
setTimeout(() => sendDiscoveryPackage(port), timeout)
|
|
139
|
+
startDiscovering(port)
|
|
126
140
|
},
|
|
127
141
|
close () {
|
|
128
142
|
stopDiscovery()
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
const { defaultsDeep } = require('@weave-js/utils')
|
|
3
|
-
const TransportBase = require('../
|
|
3
|
+
const TransportBase = require('../adapteBase')
|
|
4
4
|
const Swim = require('./discovery/index')
|
|
5
|
-
const MessageTypes = require('../../
|
|
5
|
+
const MessageTypes = require('../../messageTypes')
|
|
6
6
|
const TCPReader = require('./tcpReader')
|
|
7
7
|
const TCPWriter = require('./tcpWriter')
|
|
8
|
+
const { createMessage } = require('../../createMessage')
|
|
9
|
+
|
|
8
10
|
// const TCPMessageTypeHelper = require('./tcp-messagetypes')
|
|
9
11
|
|
|
10
12
|
const defaultOptions = {
|
|
@@ -12,6 +14,7 @@ const defaultOptions = {
|
|
|
12
14
|
discovery: {
|
|
13
15
|
enabled: true,
|
|
14
16
|
type: 'udp4',
|
|
17
|
+
udpMulticast: true,
|
|
15
18
|
multicastAddress: '239.0.0.0',
|
|
16
19
|
port: 54355,
|
|
17
20
|
udpReuseAddress: true
|
|
@@ -59,7 +62,11 @@ module.exports = function SwimTransport (adapterOptions) {
|
|
|
59
62
|
MessageTypes.MESSAGE_RESPONSE,
|
|
60
63
|
MessageTypes.MESSAGE_GOSSIP_HELLO,
|
|
61
64
|
MessageTypes.MESSAGE_GOSSIP_REQUEST,
|
|
62
|
-
MessageTypes.MESSAGE_GOSSIP_RESPONSE
|
|
65
|
+
MessageTypes.MESSAGE_GOSSIP_RESPONSE,
|
|
66
|
+
MessageTypes.MESSAGE_RESPONSE_STREAM_BACKPRESSURE,
|
|
67
|
+
MessageTypes.MESSAGE_RESPONSE_STREAM_RESUME,
|
|
68
|
+
MessageTypes.MESSAGE_REQUEST_STREAM_BACKPRESSURE,
|
|
69
|
+
MessageTypes.MESSAGE_REQUEST_STREAM_RESUME
|
|
63
70
|
].includes(message.type)) {
|
|
64
71
|
if (message.type === MessageTypes.MESSAGE_DISCONNECT) {
|
|
65
72
|
// send a disconnect message to all connected nodes
|
|
@@ -81,7 +88,7 @@ module.exports = function SwimTransport (adapterOptions) {
|
|
|
81
88
|
|
|
82
89
|
const localNode = self.broker.registry.nodeCollection.localNode
|
|
83
90
|
|
|
84
|
-
const message =
|
|
91
|
+
const message = createMessage(MessageTypes.MESSAGE_GOSSIP_HELLO, nodeId, {
|
|
85
92
|
host: localNode.IPList[0],
|
|
86
93
|
port: localNode.port
|
|
87
94
|
})
|
|
@@ -165,7 +172,7 @@ module.exports = function SwimTransport (adapterOptions) {
|
|
|
165
172
|
}
|
|
166
173
|
})
|
|
167
174
|
|
|
168
|
-
self.swim.
|
|
175
|
+
self.swim.start(port)
|
|
169
176
|
}
|
|
170
177
|
|
|
171
178
|
function startTCPServer () {
|
|
@@ -174,19 +181,19 @@ module.exports = function SwimTransport (adapterOptions) {
|
|
|
174
181
|
|
|
175
182
|
tcpReader.on('message', onMessage)
|
|
176
183
|
|
|
177
|
-
tcpWriter.on('error', (error,
|
|
184
|
+
tcpWriter.on('error', (error, nodeId) => {
|
|
178
185
|
self.log.debug('TCP client error on ', error)
|
|
179
|
-
self.broker.registry.nodeDisconnected(
|
|
186
|
+
self.broker.registry.nodeDisconnected(nodeId, false)
|
|
180
187
|
})
|
|
181
188
|
|
|
182
|
-
tcpWriter.on('end',
|
|
189
|
+
tcpWriter.on('end', nodeId => {
|
|
183
190
|
self.log.debug('TCP connection ended with')
|
|
184
|
-
self.broker.registry.nodeDisconnected(
|
|
191
|
+
self.broker.registry.nodeDisconnected(nodeId, false)
|
|
185
192
|
})
|
|
186
193
|
|
|
187
|
-
// tcpWriter.on('error', (_,
|
|
194
|
+
// tcpWriter.on('error', (_, nodeId) => {
|
|
188
195
|
// self.log.debug('TCP server error on ')
|
|
189
|
-
// // this.nodes.disconnected(
|
|
196
|
+
// // this.nodes.disconnected(nodeId, false)
|
|
190
197
|
// })
|
|
191
198
|
|
|
192
199
|
return tcpReader.listen()
|
|
@@ -250,7 +257,7 @@ module.exports = function SwimTransport (adapterOptions) {
|
|
|
250
257
|
|
|
251
258
|
const destinationNode = nodes[Math.floor(Math.random() * nodes.length)]
|
|
252
259
|
if (destinationNode) {
|
|
253
|
-
const message =
|
|
260
|
+
const message = createMessage(MessageTypes.MESSAGE_GOSSIP_REQUEST, destinationNode.id, payload)
|
|
254
261
|
|
|
255
262
|
self.send(message).catch(() => {
|
|
256
263
|
self.log.debug(`Unable to send gossip response to ${destinationNode.id}`)
|
|
@@ -357,7 +364,7 @@ module.exports = function SwimTransport (adapterOptions) {
|
|
|
357
364
|
|
|
358
365
|
if (response.online || response.offline) {
|
|
359
366
|
const destinationNode = self.broker.registry.nodeCollection.get(payload.sender)
|
|
360
|
-
const message =
|
|
367
|
+
const message = createMessage(MessageTypes.MESSAGE_GOSSIP_RESPONSE, destinationNode.id, response)
|
|
361
368
|
self.send(message).catch(() => {})
|
|
362
369
|
}
|
|
363
370
|
} catch (error) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
const { Writable } = require('stream')
|
|
3
|
-
const MessageTypes = require('../../
|
|
3
|
+
const MessageTypes = require('../../messageTypes')
|
|
4
4
|
const TCPMessageTypeHelper = require('./tcp-messagetypes')
|
|
5
5
|
|
|
6
6
|
module.exports = class TCPWriteStream extends Writable {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
const net = require('net')
|
|
2
2
|
const { EventEmitter } = require('events')
|
|
3
|
-
const MessageTypes = require('../../
|
|
3
|
+
const MessageTypes = require('../../messageTypes')
|
|
4
4
|
const TCPMessageTypeHelper = require('./tcp-messagetypes')
|
|
5
5
|
|
|
6
|
-
module.exports = (adapter
|
|
6
|
+
module.exports = (adapter) => {
|
|
7
7
|
const self = Object.assign({}, EventEmitter.prototype)
|
|
8
8
|
const sockets = new Map()
|
|
9
9
|
const messageTypeHelper = TCPMessageTypeHelper(MessageTypes)
|