@weave-js/core 0.11.1 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +8 -8
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +8 -9
- package/lib/broker/index.js +160 -164
- package/lib/buildRuntime.js +34 -34
- package/lib/cache/adapters/base.js +50 -74
- package/lib/cache/adapters/inMemory.js +58 -58
- package/lib/cache/adapters/inMemoryLru.js +57 -57
- package/lib/cache/adapters/index.js +3 -39
- package/lib/cache/getCacheKeyByObject.js +31 -0
- package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
- package/lib/cache/lock.js +30 -24
- package/lib/constants.js +5 -5
- package/lib/errorHandler.js +8 -8
- package/lib/errors.js +64 -54
- package/lib/helper/defineAction.js +2 -2
- package/lib/helper/defineBrokerOptions.js +2 -2
- package/lib/helper/defineService.js +2 -2
- package/lib/index.js +21 -21
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -15
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +24 -24
- package/lib/logger/levels.js +21 -21
- package/lib/logger/tools.js +15 -15
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +75 -65
- package/lib/metrics/common.js +47 -47
- package/lib/metrics/constants.js +44 -44
- package/lib/metrics/exporter/base.js +8 -8
- package/lib/metrics/exporter/event.js +20 -20
- package/lib/metrics/exporter/index.js +15 -15
- package/lib/metrics/index.js +2 -2
- package/lib/metrics/types/base.js +20 -20
- package/lib/metrics/types/counter.js +6 -6
- package/lib/metrics/types/gauge.js +24 -24
- package/lib/metrics/types/histogram.js +26 -26
- package/lib/metrics/types/index.js +6 -6
- package/lib/metrics/types/info.js +17 -17
- package/lib/middlewares/action-hooks/index.js +38 -38
- package/lib/middlewares/bulkhead/index.js +34 -34
- package/lib/middlewares/cache/index.js +37 -37
- package/lib/middlewares/circuit-breaker/index.js +62 -62
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +47 -15
- package/lib/middlewares/index.js +2 -2
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +17 -17
- package/lib/middlewares/metrics/index.js +29 -29
- package/lib/middlewares/retry/index.js +15 -15
- package/lib/middlewares/timeout/index.js +16 -16
- package/lib/middlewares/tracing/index.js +37 -37
- package/lib/middlewares/tracing/tags.js +6 -6
- package/lib/middlewares/validator/index.js +31 -31
- package/lib/registry/actionEndpoint.js +8 -8
- package/lib/registry/collections/actionCollection.js +39 -35
- package/lib/registry/collections/endpointCollection.js +63 -63
- package/lib/registry/collections/eventCollection.js +62 -62
- package/lib/registry/collections/nodeCollection.js +45 -45
- package/lib/registry/collections/serviceCollection.js +61 -55
- package/lib/registry/eventEndpoint.js +14 -14
- package/lib/registry/load-balancer/base.js +3 -3
- package/lib/registry/load-balancer/index.js +7 -7
- package/lib/registry/node.js +27 -27
- package/lib/registry/registry.js +139 -136
- package/lib/registry/service/parseAction.js +16 -16
- package/lib/registry/service/parseEvent.js +18 -18
- package/lib/registry/service/reduceMixins.js +10 -10
- package/lib/registry/service/service.js +72 -72
- package/lib/registry/serviceItem.js +21 -21
- package/lib/runtime/initActionInvoker.js +21 -21
- package/lib/runtime/initCache.js +7 -7
- package/lib/runtime/initContextFactory.js +22 -22
- package/lib/runtime/initEventbus.js +49 -49
- package/lib/runtime/initLogger.js +11 -11
- package/lib/runtime/initMetrics.js +54 -54
- package/lib/runtime/initMiddlewareManager.js +21 -21
- package/lib/runtime/initRegistry.js +6 -6
- package/lib/runtime/initServiceManager.js +90 -68
- package/lib/runtime/initTracing.js +23 -23
- package/lib/runtime/initTransport.js +5 -5
- package/lib/runtime/initUuidFactory.js +4 -4
- package/lib/runtime/initValidator.js +5 -5
- package/lib/tracing/collectors/base.js +22 -22
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +15 -15
- package/lib/tracing/span.js +26 -26
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +26 -26
- package/lib/transport/adapters/adapteBase.js +30 -30
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +18 -18
- package/lib/transport/adapters/fromURI.js +13 -13
- package/lib/transport/adapters/getAdapterByName.js +5 -5
- package/lib/transport/adapters/index.js +12 -12
- package/lib/transport/adapters/tcp/discovery/codec.js +4 -4
- package/lib/transport/adapters/tcp/discovery/index.js +62 -62
- package/lib/transport/adapters/tcp/index.js +169 -171
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -39
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +57 -57
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +4 -4
- package/lib/transport/createTransport.js +254 -254
- package/lib/transport/errorPayloadFactory.js +13 -17
- package/lib/transport/messageHandlers.js +199 -206
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +28 -15
- package/lib/utils/index.js +1 -1
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +58 -6
- package/package.json +4 -4
- package/lib/cache/base.js +0 -136
package/lib/registry/node.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* @typedef {import('../types.js').Node} Node
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
const { cpuUsage } = require('@weave-js/utils')
|
|
11
|
+
const { cpuUsage } = require('@weave-js/utils');
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Node factory
|
|
@@ -38,53 +38,53 @@ exports.createNode = (nodeId) => {
|
|
|
38
38
|
events: null,
|
|
39
39
|
IPList: [],
|
|
40
40
|
update (payload, isReconnected) {
|
|
41
|
-
const newSequence = payload.sequence || 1
|
|
41
|
+
const newSequence = payload.sequence || 1;
|
|
42
42
|
|
|
43
|
-
this.services = payload.services
|
|
44
|
-
this.events = payload.events
|
|
45
|
-
this.client = payload.client || {}
|
|
46
|
-
this.IPList = payload.IPList || []
|
|
47
|
-
this.info = payload
|
|
43
|
+
this.services = payload.services;
|
|
44
|
+
this.events = payload.events;
|
|
45
|
+
this.client = payload.client || {};
|
|
46
|
+
this.IPList = payload.IPList || [];
|
|
47
|
+
this.info = payload;
|
|
48
48
|
|
|
49
49
|
if ((newSequence > this.sequence) || isReconnected === true) {
|
|
50
|
-
this.sequence = newSequence
|
|
51
|
-
this.offlineTime = null
|
|
50
|
+
this.sequence = newSequence;
|
|
51
|
+
this.offlineTime = null;
|
|
52
52
|
|
|
53
|
-
return true
|
|
53
|
+
return true;
|
|
54
54
|
}
|
|
55
|
-
return false
|
|
55
|
+
return false;
|
|
56
56
|
},
|
|
57
57
|
updateLocalInfo () {
|
|
58
58
|
cpuUsage().then(result => {
|
|
59
|
-
const newVal = Math.round(result.avg)
|
|
59
|
+
const newVal = Math.round(result.avg);
|
|
60
60
|
|
|
61
61
|
if (this.cpu !== newVal) {
|
|
62
|
-
this.cpu = Math.round(result.avg)
|
|
63
|
-
this.cpuSequence
|
|
62
|
+
this.cpu = Math.round(result.avg);
|
|
63
|
+
this.cpuSequence++;
|
|
64
64
|
}
|
|
65
|
-
})
|
|
65
|
+
});
|
|
66
66
|
},
|
|
67
67
|
heartbeat (payload) {
|
|
68
68
|
if (!this.isAvailable) {
|
|
69
|
-
this.isAvailable = true
|
|
70
|
-
this.offlineTime = null
|
|
69
|
+
this.isAvailable = true;
|
|
70
|
+
this.offlineTime = null;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
if (payload.cpu !== null) {
|
|
74
|
-
this.cpu = payload.cpu
|
|
75
|
-
this.cpuSequence = payload.cpuSequence || 1
|
|
74
|
+
this.cpu = payload.cpu;
|
|
75
|
+
this.cpuSequence = payload.cpuSequence || 1;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
this.lastHeartbeatTime = Date.now()
|
|
78
|
+
this.lastHeartbeatTime = Date.now();
|
|
79
79
|
},
|
|
80
80
|
disconnected (isUnexpected = false) {
|
|
81
81
|
if (this.isAvailable) {
|
|
82
|
-
this.offlineTime = Date.now()
|
|
83
|
-
this.sequence
|
|
84
|
-
this.isUnexpectedDisconnected = isUnexpected
|
|
82
|
+
this.offlineTime = Date.now();
|
|
83
|
+
this.sequence++;
|
|
84
|
+
this.isUnexpectedDisconnected = isUnexpected;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
this.isAvailable = false
|
|
87
|
+
this.isAvailable = false;
|
|
88
88
|
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
89
|
+
};
|
|
90
|
+
};
|
package/lib/registry/registry.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
/*
|
|
3
|
-
* Author: Kevin Ries (kevin@
|
|
3
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
4
4
|
* -----
|
|
5
5
|
* Copyright 2021 Fachwerk
|
|
6
6
|
*/
|
|
@@ -18,18 +18,18 @@
|
|
|
18
18
|
* @typedef {import('../types.js').ServiceChangedDelegate} ServiceChangedDelegate
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
const { safeCopy } = require('@weave-js/utils')
|
|
21
|
+
const { safeCopy } = require('@weave-js/utils');
|
|
22
22
|
|
|
23
23
|
// own packages
|
|
24
|
-
const { createNodeCollection } = require('./collections/nodeCollection')
|
|
25
|
-
const { createServiceCollection } = require('./collections/serviceCollection')
|
|
26
|
-
const { createActionCollection } = require('./collections/actionCollection')
|
|
27
|
-
const { createEventCollection } = require('./collections/eventCollection')
|
|
28
|
-
const { createActionEndpoint } = require('./actionEndpoint')
|
|
29
|
-
const { createNode } = require('./node')
|
|
30
|
-
const { WeaveServiceNotFoundError, WeaveServiceNotAvailableError } = require('../errors')
|
|
24
|
+
const { createNodeCollection } = require('./collections/nodeCollection');
|
|
25
|
+
const { createServiceCollection } = require('./collections/serviceCollection');
|
|
26
|
+
const { createActionCollection } = require('./collections/actionCollection');
|
|
27
|
+
const { createEventCollection } = require('./collections/eventCollection');
|
|
28
|
+
const { createActionEndpoint } = require('./actionEndpoint');
|
|
29
|
+
const { createNode } = require('./node');
|
|
30
|
+
const { WeaveServiceNotFoundError, WeaveServiceNotAvailableError } = require('../errors');
|
|
31
31
|
|
|
32
|
-
const noop = () => {}
|
|
32
|
+
const noop = () => {};
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Registry factory
|
|
@@ -37,7 +37,7 @@ const noop = () => {}
|
|
|
37
37
|
* @returns {Registry} Registry
|
|
38
38
|
*/
|
|
39
39
|
exports.createRegistry = (runtime) => {
|
|
40
|
-
const { middlewareHandler } = runtime
|
|
40
|
+
const { middlewareHandler } = runtime;
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* @type {Registry}
|
|
@@ -48,263 +48,266 @@ exports.createRegistry = (runtime) => {
|
|
|
48
48
|
/**
|
|
49
49
|
* Initialize the registry
|
|
50
50
|
* @param {Runtime} runtime Runtime
|
|
51
|
-
* @param {MiddlewareHandler} middlewareHandler Middleware handler
|
|
52
|
-
* @param {ServiceChangedDelegate} serviceChanged Service changed delegate
|
|
53
51
|
* @returns {void}
|
|
54
52
|
*/
|
|
55
53
|
init (runtime) {
|
|
56
54
|
// init collections
|
|
57
|
-
this.nodeCollection = createNodeCollection(this)
|
|
58
|
-
this.serviceCollection = createServiceCollection(this)
|
|
59
|
-
this.actionCollection = createActionCollection(this)
|
|
60
|
-
this.eventCollection = createEventCollection(this)
|
|
55
|
+
this.nodeCollection = createNodeCollection(this);
|
|
56
|
+
this.serviceCollection = createServiceCollection(this);
|
|
57
|
+
this.actionCollection = createActionCollection(this);
|
|
58
|
+
this.eventCollection = createEventCollection(this);
|
|
61
59
|
|
|
62
60
|
// register an event handler for "$broker.started".
|
|
63
61
|
runtime.bus.on('$broker.started', () => {
|
|
64
62
|
if (this.nodeCollection.localNode) {
|
|
65
|
-
this.generateLocalNodeInfo(true)
|
|
63
|
+
this.generateLocalNodeInfo(true);
|
|
66
64
|
}
|
|
67
|
-
})
|
|
65
|
+
});
|
|
68
66
|
},
|
|
69
67
|
onRegisterLocalAction: noop,
|
|
70
68
|
onRegisterRemoteAction: noop,
|
|
71
69
|
checkActionVisibility (action, node) {
|
|
72
|
-
if (
|
|
73
|
-
|
|
70
|
+
if (
|
|
71
|
+
typeof action.visibility === 'undefined' ||
|
|
72
|
+
action.visibility === 'public' ||
|
|
73
|
+
action.visibility === 'published'
|
|
74
|
+
) {
|
|
75
|
+
return true;
|
|
74
76
|
}
|
|
75
77
|
|
|
78
|
+
// Only callable from local services.
|
|
76
79
|
if (action.visibility === 'protected' && node.isLocal) {
|
|
77
|
-
return true
|
|
80
|
+
return true;
|
|
78
81
|
}
|
|
79
82
|
|
|
80
|
-
return false
|
|
83
|
+
return false;
|
|
81
84
|
},
|
|
82
85
|
registerLocalService (serviceSpecification) {
|
|
83
86
|
if (!this.serviceCollection.has(serviceSpecification.name, serviceSpecification.version, runtime.nodeId)) {
|
|
84
|
-
const service = this.serviceCollection.add(this.nodeCollection.localNode, serviceSpecification.name, serviceSpecification.version, serviceSpecification.settings)
|
|
87
|
+
const service = this.serviceCollection.add(this.nodeCollection.localNode, serviceSpecification.name, serviceSpecification.version, serviceSpecification.settings);
|
|
85
88
|
|
|
86
89
|
if (serviceSpecification.actions) {
|
|
87
|
-
this.registerActions(this.nodeCollection.localNode, service, serviceSpecification.actions)
|
|
90
|
+
this.registerActions(this.nodeCollection.localNode, service, serviceSpecification.actions);
|
|
88
91
|
}
|
|
89
92
|
|
|
90
93
|
if (serviceSpecification.events) {
|
|
91
|
-
this.registerEvents(this.nodeCollection.localNode, service, serviceSpecification.events)
|
|
94
|
+
this.registerEvents(this.nodeCollection.localNode, service, serviceSpecification.events);
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
this.nodeCollection.localNode.services.push(service)
|
|
97
|
+
this.nodeCollection.localNode.services.push(service);
|
|
95
98
|
|
|
96
|
-
this.generateLocalNodeInfo(runtime.state.isStarted)
|
|
99
|
+
this.generateLocalNodeInfo(runtime.state.isStarted);
|
|
97
100
|
|
|
98
101
|
if (serviceSpecification.version) {
|
|
99
|
-
this.log.info(`Service '${service.name}' (v${serviceSpecification.version}) registered.`)
|
|
102
|
+
this.log.info(`Service '${service.name}' (v${serviceSpecification.version}) registered.`);
|
|
100
103
|
} else {
|
|
101
|
-
this.log.info(`Service '${service.name}' registered.`)
|
|
104
|
+
this.log.info(`Service '${service.name}' registered.`);
|
|
102
105
|
}
|
|
103
106
|
|
|
104
|
-
runtime.services.serviceChanged(true)
|
|
107
|
+
runtime.services.serviceChanged(true);
|
|
105
108
|
}
|
|
106
109
|
},
|
|
107
110
|
registerRemoteServices (node, services) {
|
|
108
111
|
services.forEach((service) => {
|
|
109
112
|
// todo: handle events
|
|
110
|
-
let oldActions
|
|
111
|
-
let oldEvents
|
|
112
|
-
let svc = this.serviceCollection.get(node.id, service.name, service.version)
|
|
113
|
+
let oldActions;
|
|
114
|
+
let oldEvents;
|
|
115
|
+
let svc = this.serviceCollection.get(node.id, service.name, service.version);
|
|
113
116
|
|
|
114
117
|
if (!svc) {
|
|
115
|
-
svc = this.serviceCollection.add(node, service.name, service.version, service.settings)
|
|
118
|
+
svc = this.serviceCollection.add(node, service.name, service.version, service.settings);
|
|
116
119
|
} else {
|
|
117
120
|
// Update existing service with new actions
|
|
118
|
-
oldActions = Object.assign({}, svc.actions)
|
|
119
|
-
oldEvents = Object.assign({}, svc.events)
|
|
120
|
-
svc.update(service)
|
|
121
|
+
oldActions = Object.assign({}, svc.actions);
|
|
122
|
+
oldEvents = Object.assign({}, svc.events);
|
|
123
|
+
svc.update(service);
|
|
121
124
|
}
|
|
122
125
|
|
|
123
126
|
if (service.actions) {
|
|
124
|
-
this.registerActions(node, svc, service.actions)
|
|
127
|
+
this.registerActions(node, svc, service.actions);
|
|
125
128
|
}
|
|
126
129
|
|
|
127
130
|
if (service.events) {
|
|
128
|
-
this.registerEvents(node, svc, service.events)
|
|
131
|
+
this.registerEvents(node, svc, service.events);
|
|
129
132
|
}
|
|
130
133
|
|
|
131
134
|
if (oldActions) {
|
|
132
135
|
Object.keys(oldActions).forEach(actionName => {
|
|
133
136
|
if (!service.actions[actionName]) {
|
|
134
|
-
this.actionCollection.remove(actionName, node)
|
|
137
|
+
this.actionCollection.remove(actionName, node);
|
|
135
138
|
}
|
|
136
|
-
})
|
|
139
|
+
});
|
|
137
140
|
}
|
|
138
141
|
|
|
139
142
|
if (oldEvents) {
|
|
140
143
|
Object.keys(oldEvents).forEach(eventName => {
|
|
141
144
|
if (!service.actions[eventName]) {
|
|
142
|
-
this.eventCollection.remove(eventName, node)
|
|
145
|
+
this.eventCollection.remove(eventName, node);
|
|
143
146
|
}
|
|
144
|
-
})
|
|
147
|
+
});
|
|
145
148
|
}
|
|
146
|
-
})
|
|
149
|
+
});
|
|
147
150
|
|
|
148
151
|
// remove old services
|
|
149
|
-
const oldServices = Array.from(this.serviceCollection.services)
|
|
152
|
+
const oldServices = Array.from(this.serviceCollection.services);
|
|
150
153
|
oldServices.forEach((oldService) => {
|
|
151
154
|
if (oldService.node.id !== node.id) {
|
|
152
|
-
return
|
|
155
|
+
return;
|
|
153
156
|
}
|
|
154
157
|
|
|
155
|
-
let isExisting = false
|
|
158
|
+
let isExisting = false;
|
|
156
159
|
|
|
157
160
|
// check if the old service exists in the new services.
|
|
158
161
|
services.forEach((svc) => {
|
|
159
162
|
if (oldService.equals(svc.name, svc.version)) {
|
|
160
|
-
isExisting = true
|
|
163
|
+
isExisting = true;
|
|
161
164
|
}
|
|
162
|
-
})
|
|
165
|
+
});
|
|
163
166
|
|
|
164
167
|
if (!isExisting) {
|
|
165
|
-
this.deregisterService(oldService.name, oldService.version, node.id)
|
|
168
|
+
this.deregisterService(oldService.name, oldService.version, node.id);
|
|
166
169
|
}
|
|
167
|
-
})
|
|
170
|
+
});
|
|
168
171
|
|
|
169
|
-
runtime.services.serviceChanged(false)
|
|
172
|
+
runtime.services.serviceChanged(false);
|
|
170
173
|
},
|
|
171
174
|
registerEvents (node, service, events) {
|
|
172
175
|
Object.keys(events).forEach((key) => {
|
|
173
|
-
const event = events[key]
|
|
176
|
+
const event = events[key];
|
|
174
177
|
|
|
175
178
|
if (node.isLocal) {
|
|
176
|
-
event.handler = middlewareHandler.wrapHandler('localEvent', event.handler, event) // this.onRegisterLocalEvent(event)
|
|
179
|
+
event.handler = middlewareHandler.wrapHandler('localEvent', event.handler, event); // this.onRegisterLocalEvent(event)
|
|
177
180
|
}
|
|
178
181
|
|
|
179
|
-
this.eventCollection.add(node, service, event)
|
|
180
|
-
service.addEvent(event)
|
|
181
|
-
})
|
|
182
|
+
this.eventCollection.add(node, service, event);
|
|
183
|
+
service.addEvent(event);
|
|
184
|
+
});
|
|
182
185
|
},
|
|
183
186
|
registerActions (node, service, actions) {
|
|
184
187
|
Object.keys(actions).forEach((key) => {
|
|
185
|
-
const action = actions[key]
|
|
188
|
+
const action = actions[key];
|
|
186
189
|
|
|
187
190
|
if (!this.checkActionVisibility(action, node)) {
|
|
188
|
-
return
|
|
191
|
+
return;
|
|
189
192
|
}
|
|
190
193
|
|
|
191
194
|
if (node.isLocal) {
|
|
192
|
-
action.handler = middlewareHandler.wrapHandler('localAction', action.handler, action)
|
|
195
|
+
action.handler = middlewareHandler.wrapHandler('localAction', action.handler, action);
|
|
193
196
|
} else {
|
|
194
|
-
action.handler = middlewareHandler.wrapHandler('remoteAction', runtime.transport.sendRequest.bind(runtime.transport), action)
|
|
197
|
+
action.handler = middlewareHandler.wrapHandler('remoteAction', runtime.transport.sendRequest.bind(runtime.transport), action);
|
|
195
198
|
}
|
|
196
199
|
|
|
197
|
-
this.actionCollection.add(node, service, action)
|
|
200
|
+
this.actionCollection.add(node, service, action);
|
|
198
201
|
|
|
199
|
-
service.addAction(action)
|
|
200
|
-
})
|
|
202
|
+
service.addAction(action);
|
|
203
|
+
});
|
|
201
204
|
},
|
|
202
205
|
deregisterService (name, version, nodeId) {
|
|
203
|
-
this.serviceCollection.remove(nodeId || runtime.nodeId, name, version)
|
|
206
|
+
this.serviceCollection.remove(nodeId || runtime.nodeId, name, version);
|
|
204
207
|
|
|
205
208
|
// It must be a local service if there is no node ID.
|
|
206
209
|
if (!nodeId) {
|
|
207
|
-
const serviceToRemove = this.nodeCollection.localNode.services.find(service => service.name === name)
|
|
208
|
-
this.nodeCollection.localNode.services.splice(this.nodeCollection.localNode.services.indexOf(serviceToRemove), 1)
|
|
210
|
+
const serviceToRemove = this.nodeCollection.localNode.services.find(service => service.name === name);
|
|
211
|
+
this.nodeCollection.localNode.services.splice(this.nodeCollection.localNode.services.indexOf(serviceToRemove), 1);
|
|
209
212
|
}
|
|
210
213
|
|
|
211
214
|
if (!nodeId || nodeId === runtime.nodeId) {
|
|
212
|
-
this.generateLocalNodeInfo(true)
|
|
215
|
+
this.generateLocalNodeInfo(true);
|
|
213
216
|
}
|
|
214
217
|
},
|
|
215
218
|
deregisterServiceByNodeId (nodeId) {
|
|
216
|
-
return this.serviceCollection.removeAllByNodeId(nodeId)
|
|
219
|
+
return this.serviceCollection.removeAllByNodeId(nodeId);
|
|
217
220
|
},
|
|
218
221
|
hasService (serviceName, version, nodeId) {
|
|
219
|
-
return this.serviceCollection.has(serviceName, version, nodeId)
|
|
222
|
+
return this.serviceCollection.has(serviceName, version, nodeId);
|
|
220
223
|
},
|
|
221
224
|
getNextAvailableActionEndpoint (actionName, opts = {}) {
|
|
222
225
|
// Handle direct endpoint call.
|
|
223
226
|
if (typeof actionName !== 'string') {
|
|
224
|
-
return actionName
|
|
227
|
+
return actionName;
|
|
225
228
|
} else {
|
|
226
229
|
// check if the action call is intended for a specific remote node
|
|
227
230
|
if (opts.nodeId) {
|
|
228
|
-
const endpoint = this.getActionEndpointByNodeId(actionName, opts.nodeId)
|
|
231
|
+
const endpoint = this.getActionEndpointByNodeId(actionName, opts.nodeId);
|
|
229
232
|
|
|
230
233
|
// no endpoint for this action & node ID found
|
|
231
234
|
if (!endpoint) {
|
|
232
|
-
this.log.warn(`Service "${actionName}" is not registered on node ${opts.nodeId}.`)
|
|
233
|
-
return new WeaveServiceNotFoundError({ actionName, nodeId: opts.nodeId })
|
|
235
|
+
this.log.warn(`Service "${actionName}" is not registered on node ${opts.nodeId}.`);
|
|
236
|
+
return new WeaveServiceNotFoundError({ actionName, nodeId: opts.nodeId });
|
|
234
237
|
}
|
|
235
238
|
|
|
236
|
-
return endpoint
|
|
239
|
+
return endpoint;
|
|
237
240
|
} else {
|
|
238
|
-
const endpointList = this.getActionEndpoints(actionName)
|
|
241
|
+
const endpointList = this.getActionEndpoints(actionName);
|
|
239
242
|
|
|
240
243
|
if (!endpointList) {
|
|
241
|
-
this.log.warn(`Service "${actionName}" is not registered.`)
|
|
242
|
-
return new WeaveServiceNotFoundError({ actionName })
|
|
244
|
+
this.log.warn(`Service "${actionName}" is not registered.`);
|
|
245
|
+
return new WeaveServiceNotFoundError({ actionName });
|
|
243
246
|
}
|
|
244
247
|
|
|
245
|
-
const endpoint = endpointList.getNextAvailableEndpoint()
|
|
248
|
+
const endpoint = endpointList.getNextAvailableEndpoint();
|
|
246
249
|
|
|
247
250
|
if (!endpoint) {
|
|
248
|
-
this.log.warn(`Service "${actionName}" is not available.`)
|
|
249
|
-
return new WeaveServiceNotAvailableError({ actionName })
|
|
251
|
+
this.log.warn(`Service "${actionName}" is not available.`);
|
|
252
|
+
return new WeaveServiceNotAvailableError({ actionName });
|
|
250
253
|
}
|
|
251
254
|
|
|
252
|
-
return endpoint
|
|
255
|
+
return endpoint;
|
|
253
256
|
}
|
|
254
257
|
}
|
|
255
258
|
},
|
|
256
259
|
getActionEndpointByNodeId (actionName, nodeId) {
|
|
257
|
-
const endpointList = this.getActionEndpoints(actionName)
|
|
260
|
+
const endpointList = this.getActionEndpoints(actionName);
|
|
258
261
|
if (endpointList) {
|
|
259
|
-
return endpointList.getByNodeId(nodeId)
|
|
262
|
+
return endpointList.getByNodeId(nodeId);
|
|
260
263
|
}
|
|
261
|
-
return null
|
|
264
|
+
return null;
|
|
262
265
|
},
|
|
263
266
|
getActionEndpoints (actionName) {
|
|
264
|
-
return this.actionCollection.get(actionName)
|
|
267
|
+
return this.actionCollection.get(actionName);
|
|
265
268
|
},
|
|
266
269
|
createPrivateActionEndpoint (action) {
|
|
267
|
-
return createActionEndpoint(runtime, this.nodeCollection.localNode, action.service, action)
|
|
270
|
+
return createActionEndpoint(runtime, this.nodeCollection.localNode, action.service, action);
|
|
268
271
|
},
|
|
269
272
|
getLocalActionEndpoint (actionName) {
|
|
270
|
-
const endpointList = this.getActionEndpoints(actionName)
|
|
273
|
+
const endpointList = this.getActionEndpoints(actionName);
|
|
271
274
|
|
|
272
275
|
if (!endpointList) {
|
|
273
|
-
this.log.warn(`Service "${actionName}" is not registered localy.`)
|
|
274
|
-
runtime.handleError(new WeaveServiceNotFoundError({ actionName }))
|
|
276
|
+
this.log.warn(`Service "${actionName}" is not registered localy.`);
|
|
277
|
+
runtime.handleError(new WeaveServiceNotFoundError({ actionName }));
|
|
275
278
|
}
|
|
276
279
|
|
|
277
|
-
const endpoint = endpointList.getNextLocalEndpoint()
|
|
280
|
+
const endpoint = endpointList.getNextLocalEndpoint();
|
|
278
281
|
|
|
279
282
|
if (!endpoint) {
|
|
280
|
-
this.log.warn(`Service "${actionName}" is not available localy.`)
|
|
281
|
-
runtime.handleError(new WeaveServiceNotAvailableError({ actionName }))
|
|
283
|
+
this.log.warn(`Service "${actionName}" is not available localy.`);
|
|
284
|
+
runtime.handleError(new WeaveServiceNotAvailableError({ actionName }));
|
|
282
285
|
}
|
|
283
286
|
|
|
284
|
-
return endpoint
|
|
287
|
+
return endpoint;
|
|
285
288
|
},
|
|
286
289
|
getNodeInfo (nodeId) {
|
|
287
|
-
const node = this.nodeCollection.get(nodeId)
|
|
290
|
+
const node = this.nodeCollection.get(nodeId);
|
|
288
291
|
|
|
289
292
|
if (!node) {
|
|
290
|
-
return null
|
|
293
|
+
return null;
|
|
291
294
|
}
|
|
292
295
|
|
|
293
|
-
return node.info
|
|
296
|
+
return node.info;
|
|
294
297
|
},
|
|
295
298
|
getLocalNodeInfo (forceGenerateInfo) {
|
|
296
299
|
if (forceGenerateInfo || !this.nodeCollection.localNode.info) {
|
|
297
|
-
return this.generateLocalNodeInfo()
|
|
300
|
+
return this.generateLocalNodeInfo();
|
|
298
301
|
}
|
|
299
302
|
|
|
300
|
-
return this.nodeCollection.localNode.info
|
|
303
|
+
return this.nodeCollection.localNode.info;
|
|
301
304
|
},
|
|
302
305
|
generateLocalNodeInfo (incrementSequence = false) {
|
|
303
|
-
const { client, IPList, sequence } = this.nodeCollection.localNode
|
|
304
|
-
const nodeInfo = { client, IPList, sequence }
|
|
306
|
+
const { client, IPList, sequence } = this.nodeCollection.localNode;
|
|
307
|
+
const nodeInfo = { client, IPList, sequence };
|
|
305
308
|
|
|
306
309
|
if (incrementSequence) {
|
|
307
|
-
this.nodeCollection.localNode.sequence
|
|
310
|
+
this.nodeCollection.localNode.sequence++;
|
|
308
311
|
}
|
|
309
312
|
|
|
310
313
|
if (runtime.state.isStarted) {
|
|
@@ -314,80 +317,80 @@ exports.createRegistry = (runtime) => {
|
|
|
314
317
|
withEvents: true,
|
|
315
318
|
withNodeService: runtime.options.registry.publishNodeService,
|
|
316
319
|
withSettings: true
|
|
317
|
-
})
|
|
320
|
+
});
|
|
318
321
|
} else {
|
|
319
|
-
nodeInfo.services = []
|
|
322
|
+
nodeInfo.services = [];
|
|
320
323
|
}
|
|
321
324
|
|
|
322
|
-
this.nodeCollection.localNode.info = safeCopy(nodeInfo)
|
|
323
|
-
return this.nodeCollection.localNode.info
|
|
325
|
+
this.nodeCollection.localNode.info = safeCopy(nodeInfo);
|
|
326
|
+
return this.nodeCollection.localNode.info;
|
|
324
327
|
},
|
|
325
328
|
processNodeInfo (payload) {
|
|
326
329
|
/**
|
|
327
330
|
* @type {string}
|
|
328
331
|
*/
|
|
329
|
-
const nodeId = payload.sender
|
|
332
|
+
const nodeId = payload.sender;
|
|
330
333
|
|
|
331
334
|
/**
|
|
332
335
|
* @type {Node}
|
|
333
336
|
*/
|
|
334
|
-
let node = this.nodeCollection.get(nodeId)
|
|
337
|
+
let node = this.nodeCollection.get(nodeId);
|
|
335
338
|
|
|
336
339
|
/**
|
|
337
340
|
* @type {boolean}
|
|
338
341
|
*/
|
|
339
|
-
let isNew = false
|
|
342
|
+
let isNew = false;
|
|
340
343
|
|
|
341
344
|
/**
|
|
342
345
|
* @type {boolean}
|
|
343
346
|
*/
|
|
344
|
-
let isReconnected = false
|
|
347
|
+
let isReconnected = false;
|
|
345
348
|
|
|
346
349
|
// There is no node with the specified ID. It must therefore be a new node.
|
|
347
350
|
if (!node) {
|
|
348
|
-
isNew = true
|
|
349
|
-
node = createNode(nodeId)
|
|
350
|
-
this.nodeCollection.add(nodeId, node)
|
|
351
|
+
isNew = true;
|
|
352
|
+
node = createNode(nodeId);
|
|
353
|
+
this.nodeCollection.add(nodeId, node);
|
|
351
354
|
} else if (!node.isAvailable) {
|
|
352
355
|
// Node exists, but is marked as unavailable. It must therefore be a reconnected node.
|
|
353
|
-
isReconnected = true
|
|
354
|
-
node.isAvailable = true
|
|
355
|
-
node.lastHeartbeatTime = Date.now()
|
|
356
|
+
isReconnected = true;
|
|
357
|
+
node.isAvailable = true;
|
|
358
|
+
node.lastHeartbeatTime = Date.now();
|
|
356
359
|
}
|
|
357
360
|
|
|
358
361
|
// todo: Handle multiple nodes with the same ID.
|
|
359
|
-
const updateNecessary = node.update(payload, isReconnected)
|
|
362
|
+
const updateNecessary = node.update(payload, isReconnected);
|
|
360
363
|
|
|
361
364
|
if (updateNecessary && node.services) {
|
|
362
|
-
this.registerRemoteServices(node, node.services)
|
|
365
|
+
this.registerRemoteServices(node, node.services);
|
|
363
366
|
}
|
|
364
367
|
|
|
365
368
|
if (isNew) {
|
|
366
|
-
runtime.eventBus.broadcastLocal('$node.connected', { node, isReconnected })
|
|
367
|
-
this.log.info(`Node "${node.id}" connected!`)
|
|
369
|
+
runtime.eventBus.broadcastLocal('$node.connected', { node, isReconnected });
|
|
370
|
+
this.log.info(`Node "${node.id}" connected!`);
|
|
368
371
|
} else if (isReconnected) {
|
|
369
|
-
runtime.eventBus.broadcastLocal('$node.connected', { node, isReconnected })
|
|
370
|
-
this.log.info(`Node "${node.id}" reconnected!`)
|
|
372
|
+
runtime.eventBus.broadcastLocal('$node.connected', { node, isReconnected });
|
|
373
|
+
this.log.info(`Node "${node.id}" reconnected!`);
|
|
371
374
|
} else {
|
|
372
|
-
runtime.eventBus.broadcastLocal('$node.updated', { node, isReconnected })
|
|
373
|
-
this.log.info(`Node "${node.id}" updated!`)
|
|
375
|
+
runtime.eventBus.broadcastLocal('$node.updated', { node, isReconnected });
|
|
376
|
+
this.log.info(`Node "${node.id}" updated!`);
|
|
374
377
|
}
|
|
375
378
|
},
|
|
376
379
|
nodeDisconnected (nodeId, isUnexpected) {
|
|
377
|
-
const node = this.nodeCollection.get(nodeId)
|
|
380
|
+
const node = this.nodeCollection.get(nodeId);
|
|
378
381
|
if (node && node.isAvailable) {
|
|
379
|
-
this.deregisterServiceByNodeId(node.id)
|
|
380
|
-
node.disconnected(isUnexpected)
|
|
381
|
-
runtime.eventBus.broadcastLocal('$node.disconnected', { nodeId, isUnexpected })
|
|
382
|
-
this.log.warn(`Nodes "${node.id}"${isUnexpected ? ' unexpectedly' : ''} disconnected.`)
|
|
382
|
+
this.deregisterServiceByNodeId(node.id);
|
|
383
|
+
node.disconnected(isUnexpected);
|
|
384
|
+
runtime.eventBus.broadcastLocal('$node.disconnected', { nodeId, isUnexpected });
|
|
385
|
+
this.log.warn(`Nodes "${node.id}"${isUnexpected ? ' unexpectedly' : ''} disconnected.`);
|
|
383
386
|
}
|
|
384
387
|
},
|
|
385
388
|
removeNode (nodeId) {
|
|
386
|
-
this.nodeCollection.remove(nodeId)
|
|
387
|
-
runtime.eventBus.broadcastLocal('$node.removed', { nodeId })
|
|
388
|
-
this.log.warn(`Node "${nodeId}" removed.`)
|
|
389
|
+
this.nodeCollection.remove(nodeId);
|
|
390
|
+
runtime.eventBus.broadcastLocal('$node.removed', { nodeId });
|
|
391
|
+
this.log.warn(`Node "${nodeId}" removed.`);
|
|
389
392
|
}
|
|
390
|
-
}
|
|
393
|
+
};
|
|
391
394
|
|
|
392
|
-
return registry
|
|
393
|
-
}
|
|
395
|
+
return registry;
|
|
396
|
+
};
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
const { isFunction, clone, wrapHandler, isObject, promisify } = require('@weave-js/utils')
|
|
2
|
-
const { WeaveError } = require('../../errors')
|
|
1
|
+
const { isFunction, clone, wrapHandler, isObject, promisify } = require('@weave-js/utils');
|
|
2
|
+
const { WeaveError } = require('../../errors');
|
|
3
3
|
|
|
4
4
|
module.exports.parseAction = (runtime, service, actionDefinition, name) => {
|
|
5
|
-
let action = actionDefinition
|
|
5
|
+
let action = actionDefinition;
|
|
6
6
|
|
|
7
7
|
// if the handler is a method (short form), we wrap the method in our handler object.
|
|
8
8
|
if (isFunction(actionDefinition)) {
|
|
9
|
-
action = wrapHandler(actionDefinition)
|
|
9
|
+
action = wrapHandler(actionDefinition);
|
|
10
10
|
} else if (isObject(actionDefinition)) {
|
|
11
|
-
action = clone(actionDefinition)
|
|
11
|
+
action = clone(actionDefinition);
|
|
12
12
|
} else {
|
|
13
|
-
runtime.handleError(new WeaveError(`Invalid action definition in "${name}" on service "${service.name}".`))
|
|
13
|
+
runtime.handleError(new WeaveError(`Invalid action definition in "${name}" on service "${service.name}".`));
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
const handler = action.handler
|
|
16
|
+
const handler = action.handler;
|
|
17
17
|
|
|
18
18
|
// Action handler has to be a function
|
|
19
19
|
if (!isFunction(handler)) {
|
|
20
|
-
runtime.handleError(new WeaveError(`Missing action handler in "${name}" on service "${service.name}".`))
|
|
20
|
+
runtime.handleError(new WeaveError(`Missing action handler in "${name}" on service "${service.name}".`));
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
action.name = service.name + '.' + (action.name || name)
|
|
24
|
-
action.shortName = name
|
|
23
|
+
action.name = service.name + '.' + (action.name || name);
|
|
24
|
+
action.shortName = name;
|
|
25
25
|
|
|
26
26
|
// if this is a versioned service. The action name is prefixed with the version number.
|
|
27
27
|
if (service.version) {
|
|
28
|
-
action.name = `v${service.version}.${action.name}
|
|
28
|
+
action.name = `v${service.version}.${action.name}`;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
action.service = service
|
|
32
|
-
action.version = service.version
|
|
33
|
-
action.handler = promisify(handler.bind(service))
|
|
31
|
+
action.service = service;
|
|
32
|
+
action.version = service.version;
|
|
33
|
+
action.handler = promisify(handler.bind(service));
|
|
34
34
|
|
|
35
|
-
return action
|
|
36
|
-
}
|
|
35
|
+
return action;
|
|
36
|
+
};
|