@weave-js/core 0.9.4 → 0.11.1
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} +25 -6
- package/lib/broker/index.js +224 -194
- package/lib/buildRuntime.js +70 -0
- package/lib/cache/adapters/base.js +143 -0
- package/lib/cache/adapters/inMemory.js +154 -0
- package/lib/cache/adapters/inMemoryLru.js +140 -0
- package/lib/cache/adapters/index.js +46 -0
- package/lib/cache/base.js +49 -44
- 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 +17 -92
- package/lib/logger/format/asHumanReadable.js +4 -3
- package/lib/logger/index.js +7 -1
- package/lib/logger/levels.js +8 -7
- package/lib/logger/tools.js +4 -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 +106 -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 +19 -38
- 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/initCache.js +19 -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} +0 -0
- 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/createTransport.js +215 -172
- package/lib/transport/errorPayloadFactory.js +18 -0
- package/lib/transport/{message-handlers.js → messageHandlers.js} +92 -23
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +86 -75
- package/lib/utils/index.js +1 -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 -1317
- package/lib/cache/index.js +0 -48
- package/lib/cache/memory.js +0 -91
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- package/lib/runtime/init-cache.js +0 -10
- 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
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
3
|
* -----
|
|
4
|
-
* Copyright
|
|
4
|
+
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const {
|
|
7
|
+
const { InboundTransformStream } = require('./InboundTransformStream')
|
|
8
8
|
const { WeaveError, restoreError } = require('../errors')
|
|
9
9
|
const { createContext } = require('../broker/context')
|
|
10
|
-
const
|
|
10
|
+
const { createMessage } = require('./createMessage')
|
|
11
|
+
const MessageTypes = require('./messageTypes')
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* @typedef {import('../types').Transport} Transport
|
|
@@ -66,13 +67,25 @@ module.exports = (runtime, transport) => {
|
|
|
66
67
|
|
|
67
68
|
if (!stream) {
|
|
68
69
|
isNew = true
|
|
69
|
-
stream = new
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return done()
|
|
70
|
+
stream = new InboundTransformStream(
|
|
71
|
+
payload.sender,
|
|
72
|
+
payload.id, {
|
|
73
|
+
objectMode: payload.meta && payload.meta.$isObjectModeStream
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
// handle backpressure
|
|
78
|
+
if (runtime.options.transport.streams.handleBackpressure) {
|
|
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
|
+
})
|
|
83
|
+
|
|
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
|
+
})
|
|
88
|
+
}
|
|
76
89
|
|
|
77
90
|
stream.$prevSeq = -1
|
|
78
91
|
stream.$pool = new Map()
|
|
@@ -126,13 +139,25 @@ module.exports = (runtime, transport) => {
|
|
|
126
139
|
if (!stream) {
|
|
127
140
|
transport.log.debug(`New stream from node ${payload.sender} received. Seq: ${payload.sequence}`)
|
|
128
141
|
|
|
129
|
-
stream = new
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return done()
|
|
142
|
+
stream = new InboundTransformStream(
|
|
143
|
+
payload.sender,
|
|
144
|
+
payload.id, {
|
|
145
|
+
objectMode: payload.meta && payload.meta.$isObjectModeStream
|
|
134
146
|
}
|
|
135
|
-
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
// handle backpressure
|
|
150
|
+
if (runtime.options.transport.streams.handleBackpressure) {
|
|
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
|
+
})
|
|
155
|
+
|
|
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
|
+
})
|
|
160
|
+
}
|
|
136
161
|
|
|
137
162
|
stream.$prevSeq = -1
|
|
138
163
|
stream.$pool = new Map()
|
|
@@ -232,10 +257,10 @@ module.exports = (runtime, transport) => {
|
|
|
232
257
|
}
|
|
233
258
|
|
|
234
259
|
return localRequestProxy(context)
|
|
235
|
-
.then(data => transport.
|
|
236
|
-
.catch(error => transport.
|
|
260
|
+
.then(data => transport.sendResponse(sender, payload.id, data, context.meta, null))
|
|
261
|
+
.catch(error => transport.sendResponse(sender, payload.id, null, context.meta, error))
|
|
237
262
|
} catch (error) {
|
|
238
|
-
return transport.
|
|
263
|
+
return transport.sendResponse(sender, payload.id, null, payload.meta, error)
|
|
239
264
|
}
|
|
240
265
|
}
|
|
241
266
|
|
|
@@ -244,7 +269,7 @@ module.exports = (runtime, transport) => {
|
|
|
244
269
|
* @param {any} payload - Payload
|
|
245
270
|
* @returns {Promise} Promise
|
|
246
271
|
*/
|
|
247
|
-
const onResponse = payload => {
|
|
272
|
+
const onResponse = (payload) => {
|
|
248
273
|
const id = payload.id
|
|
249
274
|
const request = transport.pending.requests.get(id)
|
|
250
275
|
|
|
@@ -295,7 +320,7 @@ module.exports = (runtime, transport) => {
|
|
|
295
320
|
* @returns {Promise} Promise
|
|
296
321
|
*/
|
|
297
322
|
const onPing = payload => {
|
|
298
|
-
const message =
|
|
323
|
+
const message = createMessage(MessageTypes.MESSAGE_PONG, payload.sender, {
|
|
299
324
|
dispatchTime: payload.dispatchTime,
|
|
300
325
|
arrivalTime: Date.now()
|
|
301
326
|
})
|
|
@@ -388,6 +413,38 @@ module.exports = (runtime, transport) => {
|
|
|
388
413
|
}
|
|
389
414
|
}
|
|
390
415
|
|
|
416
|
+
const onResponseStreamBackpressure = (payload) => {
|
|
417
|
+
const stream = transport.pending.outboundResponseStreams.get(payload.id)
|
|
418
|
+
|
|
419
|
+
if (stream) {
|
|
420
|
+
stream.pause()
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const onResponseStreamResume = (payload) => {
|
|
425
|
+
const stream = transport.pending.outboundResponseStreams.get(payload.id)
|
|
426
|
+
|
|
427
|
+
if (stream) {
|
|
428
|
+
stream.resume()
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const onRequestStreamBackpressure = (payload) => {
|
|
433
|
+
const stream = transport.pending.outboundRequestStreams.get(payload.id)
|
|
434
|
+
|
|
435
|
+
if (stream) {
|
|
436
|
+
stream.pause()
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const onRequestStreamResume = (payload) => {
|
|
441
|
+
const stream = transport.pending.outboundRequestStreams.get(payload.id)
|
|
442
|
+
|
|
443
|
+
if (stream) {
|
|
444
|
+
stream.resume()
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
391
448
|
return (type, data) => {
|
|
392
449
|
try {
|
|
393
450
|
if (data === null) {
|
|
@@ -403,9 +460,9 @@ module.exports = (runtime, transport) => {
|
|
|
403
460
|
// todo: check protocol version
|
|
404
461
|
// todo: check node ID conflict
|
|
405
462
|
|
|
406
|
-
// if (payload.sender ===
|
|
407
|
-
// if (type === MessageTypes.MESSAGE_INFO && payload.instanceId !==
|
|
408
|
-
// return
|
|
463
|
+
// if (payload.sender === runtime.nodeId) {
|
|
464
|
+
// if (type === MessageTypes.MESSAGE_INFO && payload.instanceId !== runtime.state.instanceId) {
|
|
465
|
+
// return runtime.fatalError('Weave broker has detected a node ID conflict. "nodeId" of broker needs to be unique. Broker will be stopped.')
|
|
409
466
|
// }
|
|
410
467
|
// }
|
|
411
468
|
|
|
@@ -437,6 +494,18 @@ module.exports = (runtime, transport) => {
|
|
|
437
494
|
case MessageTypes.MESSAGE_EVENT:
|
|
438
495
|
onEvent(payload)
|
|
439
496
|
break
|
|
497
|
+
case MessageTypes.MESSAGE_RESPONSE_STREAM_BACKPRESSURE:
|
|
498
|
+
onResponseStreamBackpressure(payload)
|
|
499
|
+
break
|
|
500
|
+
case MessageTypes.MESSAGE_RESPONSE_STREAM_RESUME:
|
|
501
|
+
onResponseStreamResume(payload)
|
|
502
|
+
break
|
|
503
|
+
case MessageTypes.MESSAGE_REQUEST_STREAM_BACKPRESSURE:
|
|
504
|
+
onRequestStreamBackpressure(payload)
|
|
505
|
+
break
|
|
506
|
+
case MessageTypes.MESSAGE_REQUEST_STREAM_RESUME:
|
|
507
|
+
onRequestStreamResume(payload)
|
|
508
|
+
break
|
|
440
509
|
}
|
|
441
510
|
|
|
442
511
|
return true
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
|
+
* -----
|
|
4
|
+
* Copyright 2021 Fachwerk
|
|
5
|
+
*/
|
|
6
|
+
exports.MESSAGE_UNKNOWN = 'unknown'
|
|
7
|
+
exports.MESSAGE_DISCOVERY = 'discovery'
|
|
8
|
+
exports.MESSAGE_INFO = 'info'
|
|
9
|
+
exports.MESSAGE_REQUEST = 'request'
|
|
10
|
+
exports.MESSAGE_RESPONSE = 'response'
|
|
11
|
+
exports.MESSAGE_PING = 'ping'
|
|
12
|
+
exports.MESSAGE_PONG = 'pong'
|
|
13
|
+
exports.MESSAGE_DISCONNECT = 'disconnect'
|
|
14
|
+
exports.MESSAGE_HEARTBEAT = 'heartbeat'
|
|
15
|
+
exports.MESSAGE_EVENT = 'event'
|
|
16
|
+
exports.MESSAGE_GOSSIP_HELLO = 'gossip_hello'
|
|
17
|
+
exports.MESSAGE_GOSSIP_RESPONSE = 'gossip_response'
|
|
18
|
+
exports.MESSAGE_GOSSIP_REQUEST = 'gossip_request'
|
|
19
|
+
exports.MESSAGE_EVENT_ACK = 'event_ack'
|
|
20
|
+
exports.MESSAGE_RESPONSE_STREAM_BACKPRESSURE = 'response_stream_backpressure'
|
|
21
|
+
exports.MESSAGE_RESPONSE_STREAM_RESUME = 'response_stream_resume'
|
|
22
|
+
exports.MESSAGE_REQUEST_STREAM_BACKPRESSURE = 'request_stream_backpressure'
|
|
23
|
+
exports.MESSAGE_REQUEST_STREAM_RESUME = 'request_stream_resume'
|
package/lib/types.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// eslint-disable-next-line no-unused-vars
|
|
2
|
-
const { EventEmitter2 } = require('eventemitter2')
|
|
2
|
+
const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {import('stream').Stream} Stream
|
|
@@ -22,7 +22,7 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
22
22
|
/**
|
|
23
23
|
* @callback CallActionFunctionDef - Action function definition
|
|
24
24
|
* @param {string} actionName - Name of the action
|
|
25
|
-
* @param {object} data - Payload
|
|
25
|
+
* @param {object} [data] - Payload
|
|
26
26
|
* @param {ActionOptions} [options] - Action options
|
|
27
27
|
* @return {Promise<any>} Promise - Result
|
|
28
28
|
*/
|
|
@@ -58,8 +58,8 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
58
58
|
/**
|
|
59
59
|
* ActionInvoker
|
|
60
60
|
* @typedef {object} ActionInvoker
|
|
61
|
-
* @property {function(string, object, ActionOptions):Promise<any>} call -
|
|
62
|
-
* @property {function(string, object, ActionOptions):Promise<any>}
|
|
61
|
+
* @property {function(string, object, ActionOptions):Promise<any>} call - Call a
|
|
62
|
+
* @property {function(string, object, ActionOptions):Promise<any>} multiCall - Service changed delegate
|
|
63
63
|
*/
|
|
64
64
|
|
|
65
65
|
/**
|
|
@@ -76,7 +76,7 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
76
76
|
* @property {string} version - Weave version of the broker.
|
|
77
77
|
* @property {ActionInvoker} actionInvoker - Action invoker
|
|
78
78
|
* @property {EventBus} [eventBus] - Service event bus
|
|
79
|
-
* @property {
|
|
79
|
+
* @property {EventEmitter} bus - Instance event bus
|
|
80
80
|
* @property {Broker} [broker] - Broker reference.
|
|
81
81
|
* @property {BrokerOptions} options - options
|
|
82
82
|
* @property {RuntimeInstanceState} state - Instance state
|
|
@@ -111,11 +111,11 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
111
111
|
* @property {function():Promise<any>} start - Start the broker.
|
|
112
112
|
* @property {function():Promise<any>} stop - Stop the broker
|
|
113
113
|
* @property {function(ServiceSchema):Service} createService - Create a new service with a service schema object.
|
|
114
|
-
* @property {function():void} loadService - Load a service from a given path.
|
|
115
|
-
* @property {function():void} loadServices - Load all services from a given directory.
|
|
114
|
+
* @property {function(string):void} loadService - Load a service from a given path.
|
|
115
|
+
* @property {function(string=, string=):void} loadServices - Load all services from a given directory.
|
|
116
116
|
* @property {ContextFactory} contextFactory - contextFactory
|
|
117
117
|
* @property {Logger} log - Logger instance
|
|
118
|
-
* @property {function():Logger} createLogger - Create a new logger instace.
|
|
118
|
+
* @property {function(string, any):Logger} createLogger - Create a new logger instace.
|
|
119
119
|
* @property {Cache} [cache] - Cache
|
|
120
120
|
* @property {function():string} getUUID - Create a new from the UUID factory.
|
|
121
121
|
* @property {Registry} registry - Service registry reference
|
|
@@ -127,12 +127,17 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
127
127
|
* @property {function(string, any, any=):Promise<any>} emit - Emit
|
|
128
128
|
* @property {function(string, any, any=):Promise<any>} broadcast broadcast
|
|
129
129
|
* @property {function(string, any, any=):Promise<any>} broadcastLocal broadcastLocal
|
|
130
|
-
* @property {function(string):Promise<any>} waitForServices waitForServices
|
|
131
|
-
* @property {Promise<PingResult>} ping ping
|
|
132
|
-
* @property {function(Error)} handleError handleError
|
|
133
|
-
* @property {void} fatalError fatalError
|
|
130
|
+
* @property {function(Array<string>|string):Promise<any>} waitForServices waitForServices
|
|
131
|
+
* @property {function(string, number=):Promise<PingResult>} ping ping
|
|
132
|
+
* @property {function(Error):void} handleError handleError
|
|
133
|
+
* @property {function():void} fatalError fatalError
|
|
134
134
|
*/
|
|
135
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Ping result
|
|
138
|
+
* @typedef {Object<string, number>} PingResult
|
|
139
|
+
*/
|
|
140
|
+
|
|
136
141
|
/**
|
|
137
142
|
* Configuration object for weave service broker.
|
|
138
143
|
* @typedef {Object} BulkheadOptions
|
|
@@ -151,12 +156,26 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
151
156
|
* @property {Array<Number>} defaultBuckets Default buckets for histograms.
|
|
152
157
|
*/
|
|
153
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Configuration object for weave service broker.
|
|
161
|
+
* @typedef {Object} TracingErrorOptions
|
|
162
|
+
* @property {Array<string>} fields - Fields of errors, that should be included in spans.
|
|
163
|
+
* @property {Boolean} stackTrace - Include stacktrace in spans
|
|
164
|
+
*/
|
|
165
|
+
|
|
154
166
|
/**
|
|
155
167
|
* Configuration object for weave service broker.
|
|
156
168
|
* @typedef {Object} TracingOptions
|
|
157
169
|
* @property {Boolean} enabled - Enable tracing middleware. (default = false)
|
|
158
170
|
* @property {Number} samplingRate - Rate of traced actions. (default = 1.0)
|
|
159
171
|
* @property {Array<String|Object>} collectors - Array of tracing collectors.
|
|
172
|
+
* @property {TracingErrorOptions} [errors] - Settings for tracing errors.
|
|
173
|
+
*/
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Configuration object for weave service broker.
|
|
177
|
+
* @typedef {Object} CacheLockOptions
|
|
178
|
+
* @property {Boolean} [enabled=false] Enable cache lock. (default = false)
|
|
160
179
|
*/
|
|
161
180
|
|
|
162
181
|
/**
|
|
@@ -165,6 +184,7 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
165
184
|
* @property {Boolean} enabled Enable cache middleware. (default = false)
|
|
166
185
|
* @property {String | Object} adapter - Cache adapter. (default = memory (In Memory))
|
|
167
186
|
* @property {number} [ttl=3000] - Cache item TTL.
|
|
187
|
+
* @property {CacheLockOptions} [lock] Cache lock options.
|
|
168
188
|
*/
|
|
169
189
|
|
|
170
190
|
/**
|
|
@@ -184,6 +204,12 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
184
204
|
* @property {Number} retries Number of attempts before the action call gets rejected. (default = 5)
|
|
185
205
|
*/
|
|
186
206
|
|
|
207
|
+
/**
|
|
208
|
+
* Configuration object for weave service broker.
|
|
209
|
+
* @typedef {Object} TransportStreamOptions
|
|
210
|
+
* @property {Boolean} handleBackpressure Handle stream backpressure. (default = true)
|
|
211
|
+
*/
|
|
212
|
+
|
|
187
213
|
/**
|
|
188
214
|
* Configuration object for weave service broker.
|
|
189
215
|
* @typedef {Object} TransportOptions
|
|
@@ -195,6 +221,7 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
195
221
|
* @property {Number} offlineNodeCheckInterval - Interval in milliseconds to check and remove not offline nodes. (default = 30000ms)
|
|
196
222
|
* @property {Number} maxOfflineTime - Maximum time a node can be offline before it is removed from the registry. (default = 600000ms)
|
|
197
223
|
* @property {Number} maxChunkSize - Maximum chunk size for streams. (default = 256 * 1024 Bits)
|
|
224
|
+
* @property {TransportStreamOptions} streams - Transport stream options.
|
|
198
225
|
*/
|
|
199
226
|
|
|
200
227
|
/**
|
|
@@ -264,26 +291,6 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
264
291
|
* @typedef {Object.<string, *>}
|
|
265
292
|
*/
|
|
266
293
|
|
|
267
|
-
// Cache
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Cache interface
|
|
271
|
-
* @typedef Cache
|
|
272
|
-
* @property {string} [name] name
|
|
273
|
-
* @property {LoggerOptions} options options
|
|
274
|
-
* @property {void} init init
|
|
275
|
-
* @property {Logger} log log
|
|
276
|
-
* @property {Promise<any>} set set
|
|
277
|
-
* @property {Promise<any>} get get
|
|
278
|
-
* @property {Promise<any>} remove remove
|
|
279
|
-
* @property {Promise<any>} clear clear
|
|
280
|
-
* @property {string} getCachingHash getCachingHash
|
|
281
|
-
* @property {Middleware} createMiddleware createMiddleware
|
|
282
|
-
* @property {Promise<any>} stop stop
|
|
283
|
-
*/
|
|
284
|
-
|
|
285
|
-
// Context
|
|
286
|
-
|
|
287
294
|
/**
|
|
288
295
|
* @export
|
|
289
296
|
* @typedef ContextFactory
|
|
@@ -328,12 +335,12 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
328
335
|
* @property {number} stopTime stopTime
|
|
329
336
|
* @property {any} [metrics] metrics
|
|
330
337
|
* @property {function(any):void} setData - Set data object.
|
|
331
|
-
* @property {
|
|
332
|
-
* @property {
|
|
338
|
+
* @property {CallActionFunctionDef} call - Call a service action
|
|
339
|
+
* @property {function(string, object):Promise<any>} emit emit
|
|
333
340
|
* @property {Stream} [stream] - Stream
|
|
334
|
-
* @property {
|
|
335
|
-
* @property {
|
|
336
|
-
* @property {
|
|
341
|
+
* @property {function(string, any):Promise<any>} broadcast Broadcast an event to all listener.
|
|
342
|
+
* @property {function():void} startSpan startSpan
|
|
343
|
+
* @property {function():void} finishSpan finishSpan
|
|
337
344
|
* @property {function():Context} copy - Copy the current context.
|
|
338
345
|
* @property {function(Stream):void} setStream - Set the context data stream.
|
|
339
346
|
* @property {function(Endpoint):void} setEndpoint - Set data object.
|
|
@@ -350,9 +357,9 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
350
357
|
* @property {Promise<any>} get get
|
|
351
358
|
* @property {Promise<any>} remove remove
|
|
352
359
|
* @property {Promise<any>} clear clear
|
|
353
|
-
* @property {string}
|
|
354
|
-
* @property {Middleware} createMiddleware createMiddleware
|
|
355
|
-
* @property {Promise<any>} stop stop
|
|
360
|
+
* @property {string} getCachingKey getCachingKey
|
|
361
|
+
* @property {function():Middleware} createMiddleware createMiddleware
|
|
362
|
+
* @property {function():Promise<any>} stop stop
|
|
356
363
|
*/
|
|
357
364
|
|
|
358
365
|
// Transport
|
|
@@ -368,9 +375,9 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
368
375
|
* @property {PendingStore} pending pending
|
|
369
376
|
* @property {Function} resolveConnect resolveConnect
|
|
370
377
|
* @property {string} adapterName adapterName
|
|
371
|
-
* @property {Promise<any>} connect connect
|
|
372
|
-
* @property {Promise<any>} disconnect disconnect
|
|
373
|
-
* @property {
|
|
378
|
+
* @property {function():Promise<any>} connect connect
|
|
379
|
+
* @property {function():Promise<any>} disconnect disconnect
|
|
380
|
+
* @property {function():Promise<any>} setReady setReady
|
|
374
381
|
* @property {function(TransportMessage):Promise<any>} send send
|
|
375
382
|
* @property {any} sendNodeInfo sendNodeInfo
|
|
376
383
|
* @property {function():Promise<any>} sendPing sendPing
|
|
@@ -475,14 +482,14 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
475
482
|
* @property {Endpoint | WeaveError} getNextAvailableActionEndpoint getNextAvailableActionEndpoint
|
|
476
483
|
* @property {function(ServiceActionCollectionListFilterParams=):Array<any>} getActionList getActionList
|
|
477
484
|
* @property {function(string): void} deregisterServiceByNodeId deregisterServiceByNodeId
|
|
478
|
-
* @property {function(string, number,
|
|
485
|
+
* @property {function(string, number, string):boolean} hasService hasService
|
|
479
486
|
* @property {function(string, string):Endpoint} getActionEndpointByNodeId getActionEndpointByNodeId
|
|
480
487
|
* @property {function(string):EndpointCollection} getActionEndpoints getActionEndpoints
|
|
481
488
|
* @property {function(ServiceAction):Endpoint} createPrivateActionEndpoint createPrivateActionEndpoint
|
|
482
489
|
* @property {function(string):Endpoint} getLocalActionEndpoint getLocalActionEndpoint
|
|
483
|
-
* @property {NodeInfo} getNodeInfo getNodeInfo
|
|
484
|
-
* @property {NodeInfo} getLocalNodeInfo getLocalNodeInfo
|
|
485
|
-
* @property {NodeInfo} generateLocalNodeInfo generateLocalNodeInfo
|
|
490
|
+
* @property {function():NodeInfo} getNodeInfo getNodeInfo
|
|
491
|
+
* @property {function():NodeInfo} getLocalNodeInfo getLocalNodeInfo
|
|
492
|
+
* @property {function(Boolean=):NodeInfo} generateLocalNodeInfo generateLocalNodeInfo
|
|
486
493
|
* @property {*} processNodeInfo processNodeInfo
|
|
487
494
|
* @property {function(string, boolean):void} nodeDisconnected nodeDisconnected
|
|
488
495
|
* @property {function(string):void} removeNode removeNode
|
|
@@ -506,8 +513,9 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
506
513
|
* @property {function():Endpoint} getNextLocalEndpoint getNextLocalEndpoint
|
|
507
514
|
* @property {function():number} count count
|
|
508
515
|
* @property {function(string):Endpoint} getByNodeId getByNodeId
|
|
509
|
-
* @property {void} removeByNodeId removeByNodeId
|
|
510
|
-
* @property {
|
|
516
|
+
* @property {function(string):void} removeByNodeId removeByNodeId
|
|
517
|
+
* @property {function(string):Endpoint} endpointByNodeId Get endpoint by node ID.
|
|
518
|
+
* @property {function():void} removeByService removeByService
|
|
511
519
|
*/
|
|
512
520
|
|
|
513
521
|
/**
|
|
@@ -522,6 +530,7 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
522
530
|
* @property {number} lastHeartbeatTime lastHeartbeatTime
|
|
523
531
|
* @property {number} offlineTime offlineTime
|
|
524
532
|
* @property {boolean} isAvailable isAvailable
|
|
533
|
+
* @property {boolean} wasDisconnectedUnexpectedly Node was disconnected unexpectedly
|
|
525
534
|
* @property {Array<ServiceItem>} services services
|
|
526
535
|
* @property {number} sequence sequence
|
|
527
536
|
* @property {Array<string>} [events] events
|
|
@@ -534,7 +543,6 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
534
543
|
|
|
535
544
|
/**
|
|
536
545
|
* Endpoint
|
|
537
|
-
*
|
|
538
546
|
* @typedef Endpoint
|
|
539
547
|
* @property {Node} node node
|
|
540
548
|
* @property {Service} service service
|
|
@@ -543,13 +551,13 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
543
551
|
* @property {boolean} state state
|
|
544
552
|
* @property {string} name name
|
|
545
553
|
* @property {void} updateAction updateAction
|
|
546
|
-
* @property {boolean} isAvailable isAvailable
|
|
554
|
+
* @property {function():boolean} isAvailable isAvailable
|
|
547
555
|
*/
|
|
548
556
|
|
|
549
557
|
/**
|
|
550
558
|
* Event colleciton
|
|
551
559
|
* @typedef EventCollection
|
|
552
|
-
* @property {function(Node, Service,
|
|
560
|
+
* @property {function(Node, Service, EventCollection):Endpoint} add add
|
|
553
561
|
* @property {Endpoint} get get
|
|
554
562
|
* @property {function(string, Node):void} remove remove
|
|
555
563
|
* @property {function(Service):void} removeByService removeByService
|
|
@@ -562,7 +570,8 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
562
570
|
|
|
563
571
|
/**
|
|
564
572
|
* @typedef {object} NodeCollectionListFilterOptions
|
|
565
|
-
* @property {boolean} [
|
|
573
|
+
* @property {boolean} [availableOnly=false] Git only available nodes.
|
|
574
|
+
* @property {boolean} [withServices=false] Output all services on an node.
|
|
566
575
|
*/
|
|
567
576
|
|
|
568
577
|
/**
|
|
@@ -576,14 +585,14 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
576
585
|
* @property {boolean} has has
|
|
577
586
|
* @property {function(string):Node} get get
|
|
578
587
|
* @property {function(string):boolean} remove remove
|
|
579
|
-
* @property {function(NodeCollectionListFilterOptions):Array<Node>} list list
|
|
588
|
+
* @property {function(NodeCollectionListFilterOptions=):Array<Node>} list list
|
|
580
589
|
* @property {void} disconnected disconnected
|
|
581
590
|
* @property {Array<Node>} toArray toArray
|
|
582
591
|
*/
|
|
583
592
|
|
|
584
593
|
/**
|
|
585
594
|
* @typedef {Object} ServiceActionCollectionListFilterParams
|
|
586
|
-
* @property {boolean} [
|
|
595
|
+
* @property {boolean} [localOnly] Shows only local service actions
|
|
587
596
|
* @property {boolean} [skipInternals] Shows only local service actions
|
|
588
597
|
* @property {boolean} [withEndpoints] Shows only local service actions
|
|
589
598
|
*/
|
|
@@ -593,8 +602,8 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
593
602
|
* @typedef ServiceActionCollection
|
|
594
603
|
* @property {*} add add
|
|
595
604
|
* @property {function(string):EndpointCollection} get get
|
|
596
|
-
* @property {function(
|
|
597
|
-
* @property {function(string, Node)} remove remove
|
|
605
|
+
* @property {function(Service):void} removeByService removeByService
|
|
606
|
+
* @property {function(string, Node):void} remove remove
|
|
598
607
|
* @property {function(ServiceActionCollectionListFilterParams):Array<any>} list list
|
|
599
608
|
*/
|
|
600
609
|
|
|
@@ -612,17 +621,18 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
612
621
|
* Service action definition
|
|
613
622
|
* @typedef ServiceActionDefinition
|
|
614
623
|
* @property {Object} [params] params
|
|
615
|
-
* @property {function(this: Service, Context):Promise<any>} handler handler
|
|
624
|
+
* @property {function(this: Service, Context):Promise<any> | any} handler handler
|
|
616
625
|
*/
|
|
617
626
|
|
|
618
627
|
/**
|
|
619
628
|
* @typedef {Object} ServiceCollectionListFilterParams
|
|
620
629
|
* @property {boolean} [localOnly=false] Show only local services.
|
|
630
|
+
* @property {boolean} [availableOnly=false] Show only available services.
|
|
621
631
|
* @property {boolean} [withActions=false] Include actions in result.
|
|
622
632
|
* @property {boolean} [withEvents=false] Include events in result.
|
|
623
633
|
* @property {boolean} [withNodeService=false] Include node service.
|
|
624
634
|
* @property {boolean} [withSettings=false] Include service settings.
|
|
625
|
-
|
|
635
|
+
*/
|
|
626
636
|
|
|
627
637
|
/**
|
|
628
638
|
* Service collection
|
|
@@ -631,8 +641,8 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
631
641
|
* @property {function(Node,string, number, any):ServiceItem} add Add a new service to service collection.
|
|
632
642
|
* @property {*} get get
|
|
633
643
|
* @property {function(string, number, string):boolean} has has
|
|
634
|
-
* @property {function(string, string, number):void} remove
|
|
635
|
-
* @property {function(string):void} removeAllByNodeId
|
|
644
|
+
* @property {function(string, string, number=):void} remove Remove endpoint by node ID, service name and version*.
|
|
645
|
+
* @property {function(string):void} removeAllByNodeId Remove all endpoints by node ID.
|
|
636
646
|
* @property {*} registerAction registerAction
|
|
637
647
|
* @property {function(string):EndpointCollection} tryFindActionsByActionName tryFindActionsByActionName
|
|
638
648
|
* @property {function():Array<any>} getActionsList getActionsList
|
|
@@ -660,19 +670,19 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
660
670
|
* Service schema
|
|
661
671
|
* @typedef ServiceSchema
|
|
662
672
|
* @property {string} name name
|
|
663
|
-
* @property {Array<string
|
|
664
|
-
* @property {number} [version] version
|
|
665
|
-
* @property {Array<ServiceSchema> | ServiceSchema} mixins mixins
|
|
666
|
-
* @property {ServiceSettings} settings settings
|
|
667
|
-
* @property {Object} [meta] meta
|
|
668
|
-
* @property {{[key: string]: Function }} hooks hooks
|
|
669
|
-
* @property {Object.<string, ServiceActionDefinition | ServiceActionHandler | boolean
|
|
670
|
-
* @property {{[key: string]: ServiceEvent }} [events] events
|
|
671
|
-
* @property {Object.<string, ServiceMethodDefinition
|
|
672
|
-
* @property {
|
|
673
|
-
* @property {
|
|
674
|
-
* @property {
|
|
675
|
-
* @property {function():void |Array<function():void
|
|
673
|
+
* @property {Array<string>=} dependencies - Service dependencies
|
|
674
|
+
* @property {number=} [version] version
|
|
675
|
+
* @property {Array<ServiceSchema> | ServiceSchema=} mixins mixins
|
|
676
|
+
* @property {ServiceSettings=} settings settings
|
|
677
|
+
* @property {Object=} [meta] meta
|
|
678
|
+
* @property {{[key: string]: Function }=} hooks hooks
|
|
679
|
+
* @property {Object.<string, ServiceActionDefinition | ServiceActionHandler | boolean>=} [actions] actions
|
|
680
|
+
* @property {{[key: string]: ServiceEvent }=} [events] events
|
|
681
|
+
* @property {Object.<string, ServiceMethodDefinition>=} [methods] methods
|
|
682
|
+
* @property {*=} [created] - Created hook
|
|
683
|
+
* @property {*=} [started] - Started hook
|
|
684
|
+
* @property {*=} [stopped] - Stopped hook
|
|
685
|
+
* @property {function():void |Array<function():void>=} afterSchemasMerged - After schemas merged hook
|
|
676
686
|
*/
|
|
677
687
|
|
|
678
688
|
/**
|
|
@@ -725,6 +735,7 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
725
735
|
* @property {void} timer timer
|
|
726
736
|
* @property {Metric} getMetric getMetric
|
|
727
737
|
* @property {any} list list
|
|
738
|
+
* @property {function():Promise<any>} stop list
|
|
728
739
|
*/
|
|
729
740
|
|
|
730
741
|
// Midlewares
|
|
@@ -734,7 +745,7 @@ const { EventEmitter2 } = require('eventemitter2')
|
|
|
734
745
|
* @export
|
|
735
746
|
* @typedef MiddlewareHandler
|
|
736
747
|
* @property {void} init init
|
|
737
|
-
* @property {
|
|
748
|
+
* @property {function(Middleware)} add add
|
|
738
749
|
* @property {any} wrapMethod wrapMethod
|
|
739
750
|
* @property {any} wrapHandler wrapHandler
|
|
740
751
|
* @property {any} callHandlersAsync callHandlersAsync
|
package/lib/utils/index.js
CHANGED
package/lib/utils/options.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
const {
|
|
3
2
|
clone,
|
|
4
3
|
compact,
|
|
@@ -38,6 +37,7 @@ function mergeActions (source, targetSchema) {
|
|
|
38
37
|
return targetSchema
|
|
39
38
|
}
|
|
40
39
|
|
|
40
|
+
// Merge events
|
|
41
41
|
function mergeEvents (source, targetSchema) {
|
|
42
42
|
Object.keys(source).map(key => {
|
|
43
43
|
const sourceEvent = wrapHandler(source[key])
|
|
@@ -78,7 +78,7 @@ function mergeLifecicleHooks (source, targetSchema) {
|
|
|
78
78
|
return compact(flatten([targetSchema, source]))
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
function mergeSchemas (mixin, targetSchema) {
|
|
82
82
|
const mixinSchema = clone(mixin)
|
|
83
83
|
const resultSchema = clone(targetSchema)
|
|
84
84
|
|
|
@@ -112,3 +112,5 @@ exports.mergeSchemas = (mixin, targetSchema) => {
|
|
|
112
112
|
|
|
113
113
|
return mixinSchema
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
module.exports = { mergeSchemas }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weave-js/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"description": "The core package of weave",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Weave",
|
|
@@ -40,20 +40,14 @@
|
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@weave-js/errors": "^0.9.1",
|
|
43
|
-
"@weave-js/utils": "^0.
|
|
44
|
-
"@weave-js/validator": "^0.
|
|
45
|
-
"eventemitter2": "^6.4.
|
|
46
|
-
"glob": "^7.
|
|
47
|
-
"node-fetch": "^2.6.1"
|
|
48
|
-
},
|
|
49
|
-
"devDependencies": {
|
|
50
|
-
"eslint": "^7.30.0",
|
|
51
|
-
"jest": "^27.0.6",
|
|
52
|
-
"jest-cli": "^27.0.6"
|
|
43
|
+
"@weave-js/utils": "^0.10.1",
|
|
44
|
+
"@weave-js/validator": "^0.11.1",
|
|
45
|
+
"eventemitter2": "^6.4.5",
|
|
46
|
+
"glob": "^7.2.0"
|
|
53
47
|
},
|
|
54
48
|
"directories": {
|
|
55
49
|
"lib": "lib",
|
|
56
50
|
"test": "test"
|
|
57
51
|
},
|
|
58
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "bc467dd08fe0626c40e7eb19b344c102386baf15"
|
|
59
53
|
}
|