@weave-js/core 0.11.0 → 0.12.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/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +7 -7
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +9 -9
- package/lib/broker/index.js +161 -165
- package/lib/buildRuntime.js +34 -34
- 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 +30 -24
- package/lib/constants.js +5 -5
- package/lib/errorHandler.js +8 -8
- package/lib/errors.js +35 -35
- 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 -22
- 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 +63 -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 +145 -148
- 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 +14 -5
- 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 +85 -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 +14 -17
- package/lib/transport/messageHandlers.js +200 -200
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +49 -25
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +4 -4
- package/package.json +4 -4
- package/lib/cache/base.js +0 -147
- package/lib/cache/inMemory.js +0 -140
- package/lib/cache/index.js +0 -48
|
@@ -1,23 +1,23 @@
|
|
|
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
|
-
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'
|
|
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: EventEmitter } = 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: EventEmitter } = 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
|
*/
|
|
@@ -127,7 +127,7 @@ const { EventEmitter2: EventEmitter } = 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
|
|
130
|
+
* @property {function(Array<string>|string):Promise<any>} waitForServices waitForServices
|
|
131
131
|
* @property {function(string, number=):Promise<PingResult>} ping ping
|
|
132
132
|
* @property {function(Error):void} handleError handleError
|
|
133
133
|
* @property {function():void} fatalError fatalError
|
|
@@ -169,7 +169,13 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
169
169
|
* @property {Boolean} enabled - Enable tracing middleware. (default = false)
|
|
170
170
|
* @property {Number} samplingRate - Rate of traced actions. (default = 1.0)
|
|
171
171
|
* @property {Array<String|Object>} collectors - Array of tracing collectors.
|
|
172
|
-
* @property {TracingErrorOptions} errors - Settings for tracing errors.
|
|
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)
|
|
173
179
|
*/
|
|
174
180
|
|
|
175
181
|
/**
|
|
@@ -178,6 +184,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
178
184
|
* @property {Boolean} enabled Enable cache middleware. (default = false)
|
|
179
185
|
* @property {String | Object} adapter - Cache adapter. (default = memory (In Memory))
|
|
180
186
|
* @property {number} [ttl=3000] - Cache item TTL.
|
|
187
|
+
* @property {CacheLockOptions} [lock] Cache lock options.
|
|
181
188
|
*/
|
|
182
189
|
|
|
183
190
|
/**
|
|
@@ -261,7 +268,6 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
261
268
|
* @property {RetryPolicyOptions} [retryPolicy] - Retry policy
|
|
262
269
|
* @property {ValidatorOptions} [validatorOptions] Validator options.
|
|
263
270
|
* @property {boolean} [validateActionParams] validateActionParams.
|
|
264
|
-
* @property {boolean} [watchServices] watchServices.
|
|
265
271
|
* @property {number} [waitForServiceInterval] waitForServiceInterval.
|
|
266
272
|
* @property {function():string} [beforeRegisterMiddlewares] beforeRegisterMiddlewares.
|
|
267
273
|
* @property {function(Runtime):string} [uuidFactory] uuidFactory.
|
|
@@ -328,7 +334,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
328
334
|
* @property {number} stopTime stopTime
|
|
329
335
|
* @property {any} [metrics] metrics
|
|
330
336
|
* @property {function(any):void} setData - Set data object.
|
|
331
|
-
* @property {
|
|
337
|
+
* @property {CallActionFunctionDef} call - Call a service action
|
|
332
338
|
* @property {function(string, object):Promise<any>} emit emit
|
|
333
339
|
* @property {Stream} [stream] - Stream
|
|
334
340
|
* @property {function(string, any):Promise<any>} broadcast Broadcast an event to all listener.
|
|
@@ -350,7 +356,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
350
356
|
* @property {Promise<any>} get get
|
|
351
357
|
* @property {Promise<any>} remove remove
|
|
352
358
|
* @property {Promise<any>} clear clear
|
|
353
|
-
* @property {string}
|
|
359
|
+
* @property {string} getCachingKey getCachingKey
|
|
354
360
|
* @property {function():Middleware} createMiddleware createMiddleware
|
|
355
361
|
* @property {function():Promise<any>} stop stop
|
|
356
362
|
*/
|
|
@@ -482,7 +488,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
482
488
|
* @property {function(string):Endpoint} getLocalActionEndpoint getLocalActionEndpoint
|
|
483
489
|
* @property {function():NodeInfo} getNodeInfo getNodeInfo
|
|
484
490
|
* @property {function():NodeInfo} getLocalNodeInfo getLocalNodeInfo
|
|
485
|
-
* @property {function():NodeInfo} generateLocalNodeInfo generateLocalNodeInfo
|
|
491
|
+
* @property {function(Boolean=):NodeInfo} generateLocalNodeInfo generateLocalNodeInfo
|
|
486
492
|
* @property {*} processNodeInfo processNodeInfo
|
|
487
493
|
* @property {function(string, boolean):void} nodeDisconnected nodeDisconnected
|
|
488
494
|
* @property {function(string):void} removeNode removeNode
|
|
@@ -507,6 +513,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
507
513
|
* @property {function():number} count count
|
|
508
514
|
* @property {function(string):Endpoint} getByNodeId getByNodeId
|
|
509
515
|
* @property {function(string):void} removeByNodeId removeByNodeId
|
|
516
|
+
* @property {function(string):Endpoint} endpointByNodeId Get endpoint by node ID.
|
|
510
517
|
* @property {function():void} removeByService removeByService
|
|
511
518
|
*/
|
|
512
519
|
|
|
@@ -522,6 +529,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
522
529
|
* @property {number} lastHeartbeatTime lastHeartbeatTime
|
|
523
530
|
* @property {number} offlineTime offlineTime
|
|
524
531
|
* @property {boolean} isAvailable isAvailable
|
|
532
|
+
* @property {boolean} wasDisconnectedUnexpectedly Node was disconnected unexpectedly
|
|
525
533
|
* @property {Array<ServiceItem>} services services
|
|
526
534
|
* @property {number} sequence sequence
|
|
527
535
|
* @property {Array<string>} [events] events
|
|
@@ -561,7 +569,8 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
561
569
|
|
|
562
570
|
/**
|
|
563
571
|
* @typedef {object} NodeCollectionListFilterOptions
|
|
564
|
-
* @property {boolean} [
|
|
572
|
+
* @property {boolean} [availableOnly=false] Git only available nodes.
|
|
573
|
+
* @property {boolean} [withServices=false] Output all services on an node.
|
|
565
574
|
*/
|
|
566
575
|
|
|
567
576
|
/**
|
|
@@ -575,16 +584,16 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
575
584
|
* @property {boolean} has has
|
|
576
585
|
* @property {function(string):Node} get get
|
|
577
586
|
* @property {function(string):boolean} remove remove
|
|
578
|
-
* @property {function(NodeCollectionListFilterOptions):Array<Node>} list list
|
|
587
|
+
* @property {function(NodeCollectionListFilterOptions=):Array<Node>} list list
|
|
579
588
|
* @property {void} disconnected disconnected
|
|
580
589
|
* @property {Array<Node>} toArray toArray
|
|
581
590
|
*/
|
|
582
591
|
|
|
583
592
|
/**
|
|
584
593
|
* @typedef {Object} ServiceActionCollectionListFilterParams
|
|
585
|
-
* @property {boolean} [onlyLocals] Shows only local service actions
|
|
586
|
-
* @property {boolean} [skipInternals] Shows only local service actions
|
|
587
|
-
* @property {boolean} [withEndpoints] Shows only local service actions
|
|
594
|
+
* @property {boolean} [onlyLocals=false] Shows only local service actions
|
|
595
|
+
* @property {boolean} [skipInternals=false] Shows only local service actions
|
|
596
|
+
* @property {boolean} [withEndpoints=false] Shows only local service actions
|
|
588
597
|
*/
|
|
589
598
|
|
|
590
599
|
/**
|
|
@@ -617,6 +626,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
617
626
|
/**
|
|
618
627
|
* @typedef {Object} ServiceCollectionListFilterParams
|
|
619
628
|
* @property {boolean} [localOnly=false] Show only local services.
|
|
629
|
+
* @property {boolean} [availableOnly=false] Show only available services.
|
|
620
630
|
* @property {boolean} [withActions=false] Include actions in result.
|
|
621
631
|
* @property {boolean} [withEvents=false] Include events in result.
|
|
622
632
|
* @property {boolean} [withNodeService=false] Include node service.
|
|
@@ -630,8 +640,8 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
630
640
|
* @property {function(Node,string, number, any):ServiceItem} add Add a new service to service collection.
|
|
631
641
|
* @property {*} get get
|
|
632
642
|
* @property {function(string, number, string):boolean} has has
|
|
633
|
-
* @property {function(string, string, number):void} remove
|
|
634
|
-
* @property {function(string):void} removeAllByNodeId
|
|
643
|
+
* @property {function(string, string, number=):void} remove Remove endpoint by node ID, service name and version*.
|
|
644
|
+
* @property {function(string):void} removeAllByNodeId Remove all endpoints by node ID.
|
|
635
645
|
* @property {*} registerAction registerAction
|
|
636
646
|
* @property {function(string):EndpointCollection} tryFindActionsByActionName tryFindActionsByActionName
|
|
637
647
|
* @property {function():Array<any>} getActionsList getActionsList
|
|
@@ -652,7 +662,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
652
662
|
* @property {function(Action):void} addAction addAction
|
|
653
663
|
* @property {function(Event):void} addEvent addEvent
|
|
654
664
|
* @property {function(string, number, string=): boolean} equals equals
|
|
655
|
-
* @property {function(any)} update update
|
|
665
|
+
* @property {function(any):void} update update
|
|
656
666
|
*/
|
|
657
667
|
|
|
658
668
|
/**
|
|
@@ -746,13 +756,27 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
746
756
|
* @typedef Middleware
|
|
747
757
|
* @property {() => any} [created] created
|
|
748
758
|
* @property {function(Broker):void} [started] started
|
|
749
|
-
* @property {function(
|
|
750
|
-
* @property {(
|
|
751
|
-
* @property {function(
|
|
752
|
-
* @property {(
|
|
753
|
-
* @property {(
|
|
754
|
-
* @property {(
|
|
755
|
-
* @property {()
|
|
759
|
+
* @property {function(Broker):void} [stopping] stopped
|
|
760
|
+
* @property {function(Broker):void} [createService] stopped
|
|
761
|
+
* @property {function(Broker):void} [loadService] stopped
|
|
762
|
+
* @property {function(Broker):void} [loadServices] stopped
|
|
763
|
+
* @property {function(Broker):void} [stopping] stopped
|
|
764
|
+
* @property {function(Broker):void} [ping] stopped
|
|
765
|
+
* @property {function(Broker):void} [call] stopped
|
|
766
|
+
* @property {function(Broker):void} [multiCall] stopped
|
|
767
|
+
* @property {function(Broker):void} [emit] stopped
|
|
768
|
+
* @property {function(Broker):void} [broadcast] stopped
|
|
769
|
+
* @property {function(Broker):void} [broadcastLocal] stopped
|
|
770
|
+
* @property {function(Broker):void} [serviceCreating] stopped
|
|
771
|
+
* @property {function(Broker):void} [serviceCreated] stopped
|
|
772
|
+
* @property {function(Broker):void} [serviceStopping] stopped
|
|
773
|
+
* @property {function(Broker):void} [serviceStopped] stopped
|
|
774
|
+
* @property {function(Broker):void} [localAction] stopped
|
|
775
|
+
* @property {function(Broker):void} [localEvent] stopped
|
|
776
|
+
* @property {function(Broker):void} [remoteAction] stopped
|
|
777
|
+
* @property {function(Broker):void} [remoteEvent] stopped
|
|
778
|
+
* @property {function(Broker):void} [transportSend] stopped
|
|
779
|
+
* @property {function(Broker):void} [transportMessageHandler] stopped
|
|
756
780
|
*/
|
|
757
781
|
|
|
758
782
|
// Validator
|
|
@@ -768,7 +792,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
768
792
|
* Tracer instance
|
|
769
793
|
* @typedef Tracer
|
|
770
794
|
* @property {void} init init
|
|
771
|
-
* @property {Promise<any>} stop stop
|
|
795
|
+
* @property {function():Promise<any>} stop stop
|
|
772
796
|
* @property {boolean} shouldSample shouldSample
|
|
773
797
|
* @property {function():void} invokeCollectorMethod invokeCollectorMethod
|
|
774
798
|
* @property {function(string, object):Span} startSpan startSpan
|
|
@@ -823,4 +847,4 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2')
|
|
|
823
847
|
* @param {object} data - Payload
|
|
824
848
|
*/
|
|
825
849
|
|
|
826
|
-
module.exports = {}
|
|
850
|
+
module.exports = {};
|
package/lib/utils/index.js
CHANGED
package/lib/utils/options.js
CHANGED
|
@@ -6,111 +6,111 @@ const {
|
|
|
6
6
|
flatten,
|
|
7
7
|
wrapHandler,
|
|
8
8
|
wrapInArray
|
|
9
|
-
} = require('@weave-js/utils')
|
|
9
|
+
} = require('@weave-js/utils');
|
|
10
10
|
|
|
11
11
|
function mergeSettings (source, targetSchema) {
|
|
12
|
-
return defaultsDeep(source, targetSchema)
|
|
12
|
+
return defaultsDeep(source, targetSchema);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function mergeMeta (source, targetSchema) {
|
|
16
|
-
return defaultsDeep(source, targetSchema)
|
|
16
|
+
return defaultsDeep(source, targetSchema);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
function mergeUniqueArrays (source, targetSchema) {
|
|
20
|
-
return compact(flatten([targetSchema, source]))
|
|
20
|
+
return compact(flatten([targetSchema, source]));
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function mergeActions (source, targetSchema) {
|
|
24
24
|
Object.keys(source).map(key => {
|
|
25
25
|
// prevent action merge
|
|
26
26
|
if (source[key] === false && targetSchema[key]) {
|
|
27
|
-
delete targetSchema[key]
|
|
28
|
-
return
|
|
27
|
+
delete targetSchema[key];
|
|
28
|
+
return;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
const sourceAction = wrapHandler(source[key])
|
|
32
|
-
const targetSchemaAction = wrapHandler(targetSchema[key])
|
|
31
|
+
const sourceAction = wrapHandler(source[key]);
|
|
32
|
+
const targetSchemaAction = wrapHandler(targetSchema[key]);
|
|
33
33
|
|
|
34
|
-
targetSchema[key] = deepMerge(sourceAction, targetSchemaAction)
|
|
35
|
-
})
|
|
34
|
+
targetSchema[key] = deepMerge(sourceAction, targetSchemaAction);
|
|
35
|
+
});
|
|
36
36
|
|
|
37
|
-
return targetSchema
|
|
37
|
+
return targetSchema;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// Merge events
|
|
41
41
|
function mergeEvents (source, targetSchema) {
|
|
42
42
|
Object.keys(source).map(key => {
|
|
43
|
-
const sourceEvent = wrapHandler(source[key])
|
|
44
|
-
const targetEvent = wrapHandler(targetSchema[key])
|
|
43
|
+
const sourceEvent = wrapHandler(source[key]);
|
|
44
|
+
const targetEvent = wrapHandler(targetSchema[key]);
|
|
45
45
|
|
|
46
|
-
let handler = compact(flatten([sourceEvent ? sourceEvent.handler : null, targetEvent ? targetEvent.handler : null]))
|
|
46
|
+
let handler = compact(flatten([sourceEvent ? sourceEvent.handler : null, targetEvent ? targetEvent.handler : null]));
|
|
47
47
|
if (handler.length === 1) {
|
|
48
|
-
handler = handler[0]
|
|
48
|
+
handler = handler[0];
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
targetSchema[key] = deepMerge(sourceEvent, targetEvent)
|
|
52
|
-
targetSchema[key].handler = handler
|
|
53
|
-
})
|
|
54
|
-
return targetSchema
|
|
51
|
+
targetSchema[key] = deepMerge(sourceEvent, targetEvent);
|
|
52
|
+
targetSchema[key].handler = handler;
|
|
53
|
+
});
|
|
54
|
+
return targetSchema;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
function mergeMethods (source, targetSchema) {
|
|
58
|
-
return Object.assign(source, targetSchema)
|
|
58
|
+
return Object.assign(source, targetSchema);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
function mergeActionHooks (source, target) {
|
|
62
62
|
Object.keys(source).map(hookName => {
|
|
63
63
|
if (!target[hookName]) {
|
|
64
|
-
target[hookName] = {}
|
|
64
|
+
target[hookName] = {};
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
Object.keys(source[hookName]).map(actionName => {
|
|
68
|
-
const sourceHookAction = wrapInArray(source[hookName][actionName])
|
|
69
|
-
const targetHookAction = wrapInArray(target[hookName][actionName])
|
|
70
|
-
target[hookName][actionName] = compact(flatten([sourceHookAction, targetHookAction]))
|
|
71
|
-
})
|
|
72
|
-
})
|
|
68
|
+
const sourceHookAction = wrapInArray(source[hookName][actionName]);
|
|
69
|
+
const targetHookAction = wrapInArray(target[hookName][actionName]);
|
|
70
|
+
target[hookName][actionName] = compact(flatten([sourceHookAction, targetHookAction]));
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
73
|
|
|
74
|
-
return target
|
|
74
|
+
return target;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
function mergeLifecicleHooks (source, targetSchema) {
|
|
78
|
-
return compact(flatten([targetSchema, source]))
|
|
78
|
+
return compact(flatten([targetSchema, source]));
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
function mergeSchemas (mixin, targetSchema) {
|
|
82
|
-
const mixinSchema = clone(mixin)
|
|
83
|
-
const resultSchema = clone(targetSchema)
|
|
82
|
+
const mixinSchema = clone(mixin);
|
|
83
|
+
const resultSchema = clone(targetSchema);
|
|
84
84
|
|
|
85
85
|
Object.keys(resultSchema).forEach(key => {
|
|
86
86
|
if (['name', 'version'].includes(key)) {
|
|
87
87
|
// override value
|
|
88
|
-
mixinSchema[key] = resultSchema[key]
|
|
88
|
+
mixinSchema[key] = resultSchema[key];
|
|
89
89
|
} else if (key === 'dependencies') {
|
|
90
|
-
mixinSchema[key] = mergeUniqueArrays(resultSchema[key], mixinSchema[key])
|
|
90
|
+
mixinSchema[key] = mergeUniqueArrays(resultSchema[key], mixinSchema[key]);
|
|
91
91
|
} else if (key === 'mixins') {
|
|
92
|
-
mixinSchema[key] = mergeUniqueArrays(resultSchema[key], mixinSchema[key] || {})
|
|
92
|
+
mixinSchema[key] = mergeUniqueArrays(resultSchema[key], mixinSchema[key] || {});
|
|
93
93
|
} else if (key === 'settings') {
|
|
94
|
-
mixinSchema[key] = mergeSettings(resultSchema[key], mixinSchema[key])
|
|
94
|
+
mixinSchema[key] = mergeSettings(resultSchema[key], mixinSchema[key]);
|
|
95
95
|
} else if (key === 'meta') {
|
|
96
|
-
mixinSchema[key] = mergeMeta(resultSchema[key], mixinSchema[key])
|
|
96
|
+
mixinSchema[key] = mergeMeta(resultSchema[key], mixinSchema[key]);
|
|
97
97
|
} else if (key === 'actions') {
|
|
98
|
-
mixinSchema[key] = mergeActions(resultSchema[key], mixinSchema[key] || {})
|
|
98
|
+
mixinSchema[key] = mergeActions(resultSchema[key], mixinSchema[key] || {});
|
|
99
99
|
} else if (key === 'hooks') {
|
|
100
|
-
mixinSchema[key] = mergeActionHooks(resultSchema[key], mixinSchema[key] || {})
|
|
100
|
+
mixinSchema[key] = mergeActionHooks(resultSchema[key], mixinSchema[key] || {});
|
|
101
101
|
} else if (key === 'events') {
|
|
102
|
-
mixinSchema[key] = mergeEvents(resultSchema[key], mixinSchema[key] || {})
|
|
102
|
+
mixinSchema[key] = mergeEvents(resultSchema[key], mixinSchema[key] || {});
|
|
103
103
|
} else if (key === 'methods') {
|
|
104
|
-
mixinSchema[key] = mergeMethods(resultSchema[key], mixinSchema[key] || {})
|
|
104
|
+
mixinSchema[key] = mergeMethods(resultSchema[key], mixinSchema[key] || {});
|
|
105
105
|
} else if (['afterSchemasMerged', 'created', 'started', 'stopped'].includes(key)) {
|
|
106
|
-
mixinSchema[key] = mergeLifecicleHooks(resultSchema[key], mixinSchema[key])
|
|
106
|
+
mixinSchema[key] = mergeLifecicleHooks(resultSchema[key], mixinSchema[key]);
|
|
107
107
|
} else {
|
|
108
108
|
// default action for properties
|
|
109
|
-
mixinSchema[key] = resultSchema[key]
|
|
109
|
+
mixinSchema[key] = resultSchema[key];
|
|
110
110
|
}
|
|
111
|
-
})
|
|
111
|
+
});
|
|
112
112
|
|
|
113
|
-
return mixinSchema
|
|
113
|
+
return mixinSchema;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
module.exports = { mergeSchemas }
|
|
116
|
+
module.exports = { mergeSchemas };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
const errors = require('../errors')
|
|
1
|
+
const errors = require('../errors');
|
|
2
2
|
|
|
3
3
|
exports.restoreError = (error) => {
|
|
4
|
-
const ErrorClass = errors[error.name]
|
|
4
|
+
const ErrorClass = errors[error.name];
|
|
5
5
|
|
|
6
6
|
if (ErrorClass) {
|
|
7
7
|
switch (error.name) {
|
|
8
8
|
case 'WeaveError':
|
|
9
|
-
return new ErrorClass(error.message, error.code, error.type, error.data)
|
|
9
|
+
return new ErrorClass(error.message, error.code, error.type, error.data);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
}
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weave-js/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "The core package of weave",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Weave",
|
|
@@ -40,8 +40,8 @@
|
|
|
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.
|
|
43
|
+
"@weave-js/utils": "^0.11.1",
|
|
44
|
+
"@weave-js/validator": "^0.12.0",
|
|
45
45
|
"eventemitter2": "^6.4.5",
|
|
46
46
|
"glob": "^7.2.0"
|
|
47
47
|
},
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"lib": "lib",
|
|
50
50
|
"test": "test"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "3f9662a98caa08d32ba5a181cc5871318aaece23"
|
|
53
53
|
}
|
package/lib/cache/base.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
|
-
* -----
|
|
4
|
-
* Copyright 2021 Fachwerk
|
|
5
|
-
*/
|
|
6
|
-
const crypto = require('crypto')
|
|
7
|
-
const { isObject } = require('@weave-js/utils')
|
|
8
|
-
const Constants = require('../metrics/constants')
|
|
9
|
-
|
|
10
|
-
function getCacheKeyByObject (val) {
|
|
11
|
-
if (Array.isArray(val)) {
|
|
12
|
-
return val.map(object => getCacheKeyByObject(object)).join('/')
|
|
13
|
-
} else if (isObject(val)) {
|
|
14
|
-
return Object.keys(val).map(key => {
|
|
15
|
-
return [key, getCacheKeyByObject(val[key])].join('/')
|
|
16
|
-
}).join('/')
|
|
17
|
-
} else if (val !== null) {
|
|
18
|
-
return val.toString()
|
|
19
|
-
} else {
|
|
20
|
-
return 'null'
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function generateHash (key) {
|
|
25
|
-
return crypto
|
|
26
|
-
.createHash('sha1')
|
|
27
|
-
.update(key)
|
|
28
|
-
.digest('base64')
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function registerCacheMetrics (metrics) {
|
|
32
|
-
metrics.register({ type: 'counter', name: Constants.CACHE_GET_TOTAL })
|
|
33
|
-
metrics.register({ type: 'counter', name: Constants.CACHE_SET_TOTAL })
|
|
34
|
-
metrics.register({ type: 'counter', name: Constants.CACHE_FOUND_TOTAL })
|
|
35
|
-
metrics.register({ type: 'counter', name: Constants.CACHE_EXPIRED_TOTAL })
|
|
36
|
-
metrics.register({ type: 'counter', name: Constants.CACHE_DELETED_TOTAL })
|
|
37
|
-
metrics.register({ type: 'counter', name: Constants.CACHE_CLEANED_TOTAL })
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
exports.createCacheBase = (runtime, options) => {
|
|
41
|
-
const cache = {
|
|
42
|
-
isConnected: false,
|
|
43
|
-
runtime,
|
|
44
|
-
options: Object.assign({
|
|
45
|
-
ttl: null
|
|
46
|
-
}, options),
|
|
47
|
-
init () {
|
|
48
|
-
// register metrics
|
|
49
|
-
if (runtime.metrics) {
|
|
50
|
-
this.metrics = runtime.metrics
|
|
51
|
-
registerCacheMetrics(runtime.metrics)
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
log: runtime.createLogger('CACHER'),
|
|
55
|
-
set (/* hashKey, result, ttl */) {
|
|
56
|
-
/* istanbul ignore next */
|
|
57
|
-
runtime.handleError(new Error('Method not implemented.'))
|
|
58
|
-
},
|
|
59
|
-
get (/* hashKey */) {
|
|
60
|
-
/* istanbul ignore next */
|
|
61
|
-
runtime.handleError(new Error('Method not implemented.'))
|
|
62
|
-
},
|
|
63
|
-
remove () {
|
|
64
|
-
/* istanbul ignore next */
|
|
65
|
-
runtime.handleError(new Error('Method not implemented.'))
|
|
66
|
-
},
|
|
67
|
-
clear () {
|
|
68
|
-
/* istanbul ignore next */
|
|
69
|
-
runtime.handleError(new Error('Method not implemented.'))
|
|
70
|
-
},
|
|
71
|
-
stop () {
|
|
72
|
-
/* istanbul ignore next */
|
|
73
|
-
return Promise.resolve()
|
|
74
|
-
},
|
|
75
|
-
getCachingHash (actionName, params, meta, keys) {
|
|
76
|
-
if (params || meta) {
|
|
77
|
-
const prefix = `${actionName}:`
|
|
78
|
-
|
|
79
|
-
if (keys) {
|
|
80
|
-
if (keys.length === 1) {
|
|
81
|
-
const value = params[keys[0]]
|
|
82
|
-
const key = getCacheKeyByObject(value)
|
|
83
|
-
return prefix + (isObject(value) ? key : value)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (keys.length > 0) {
|
|
87
|
-
const res = keys.reduce((pre, property, i) => {
|
|
88
|
-
const value = params[property]
|
|
89
|
-
let hash
|
|
90
|
-
if (isObject(value)) {
|
|
91
|
-
const key = getCacheKeyByObject(value)
|
|
92
|
-
hash = generateHash(key)
|
|
93
|
-
} else {
|
|
94
|
-
hash = value
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return pre + (i > 0 ? '|' : '') + hash
|
|
98
|
-
}, prefix)
|
|
99
|
-
return res
|
|
100
|
-
}
|
|
101
|
-
} else {
|
|
102
|
-
return prefix + generateHash(getCacheKeyByObject(params))
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return actionName
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// cache.middleware = (runtime) => {
|
|
111
|
-
// return {
|
|
112
|
-
// localAction: (handler, action) => {
|
|
113
|
-
// const cacheOptions = Object.assign({ enabled: true }, isObject(action.cache) ? action.cache : { enabled: !!action.cache })
|
|
114
|
-
// if (cacheOptions.enabled) {
|
|
115
|
-
// return function cacheMiddleware (context, serviceInjections) {
|
|
116
|
-
// const cacheHashKey = runtime.cache.getCachingHash(action.name, context.data, context.meta, action.cache.keys)
|
|
117
|
-
// context.isCachedResult = false
|
|
118
|
-
|
|
119
|
-
// if (context.meta.$noCache === true) {
|
|
120
|
-
// return handler(context, serviceInjections)
|
|
121
|
-
// }
|
|
122
|
-
|
|
123
|
-
// if (runtime.cache.isConnected === false) {
|
|
124
|
-
// runtime.cache.log('Cache adapter is not connected yet. Call handler...')
|
|
125
|
-
// return handler(context, serviceInjections)
|
|
126
|
-
// }
|
|
127
|
-
|
|
128
|
-
// return runtime.cache.get(cacheHashKey).then((cachedResult) => {
|
|
129
|
-
// if (cachedResult !== null) {
|
|
130
|
-
// context.isCachedResult = true
|
|
131
|
-
// return cachedResult
|
|
132
|
-
// }
|
|
133
|
-
|
|
134
|
-
// return handler(context, serviceInjections).then((result) => {
|
|
135
|
-
// runtime.cache.set(cacheHashKey, result, action.cache.ttl)
|
|
136
|
-
// return result
|
|
137
|
-
// })
|
|
138
|
-
// })
|
|
139
|
-
// }
|
|
140
|
-
// }
|
|
141
|
-
// return handler
|
|
142
|
-
// }
|
|
143
|
-
// }
|
|
144
|
-
// }
|
|
145
|
-
|
|
146
|
-
return cache
|
|
147
|
-
}
|