@weave-js/redis-transport 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/lib/index.js +51 -51
- package/package.json +4 -4
- package/test/adapter.test.js +25 -25
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.12.0](https://github.com/weave-microservices/weave/compare/@weave-js/redis-transport@0.11.1...@weave-js/redis-transport@0.12.0) (2022-03-30)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @weave-js/redis-transport
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## [0.11.1](https://github.com/weave-microservices/weave/compare/@weave-js/redis-transport@0.11.0...@weave-js/redis-transport@0.11.1) (2022-03-07)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @weave-js/redis-transport
|
package/lib/index.js
CHANGED
|
@@ -1,104 +1,104 @@
|
|
|
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
6
|
|
|
7
|
-
const redis = require('redis')
|
|
8
|
-
const { defaultsDeep, promiseDelay } = require('@weave-js/utils')
|
|
9
|
-
const { TransportAdapters } = require('@weave-js/core')
|
|
7
|
+
const redis = require('redis');
|
|
8
|
+
const { defaultsDeep, promiseDelay } = require('@weave-js/utils');
|
|
9
|
+
const { TransportAdapters } = require('@weave-js/core');
|
|
10
10
|
|
|
11
11
|
const defaultOptions = {
|
|
12
12
|
port: 6379,
|
|
13
13
|
host: '127.0.0.1'
|
|
14
|
-
}
|
|
14
|
+
};
|
|
15
15
|
|
|
16
16
|
const RedisTransportAdapter = (adapterOptions) => {
|
|
17
|
-
let clientSub
|
|
18
|
-
let clientPub
|
|
17
|
+
let clientSub;
|
|
18
|
+
let clientPub;
|
|
19
19
|
|
|
20
20
|
// Merge options with default options.
|
|
21
|
-
adapterOptions = defaultsDeep(adapterOptions, defaultOptions)
|
|
21
|
+
adapterOptions = defaultsDeep(adapterOptions, defaultOptions);
|
|
22
22
|
|
|
23
23
|
return Object.assign(TransportAdapters.BaseAdapter(adapterOptions), {
|
|
24
24
|
name: 'REDIS',
|
|
25
25
|
connect () {
|
|
26
26
|
return new Promise((resolve, reject) => {
|
|
27
|
-
clientSub = redis.createClient(adapterOptions)
|
|
27
|
+
clientSub = redis.createClient(adapterOptions);
|
|
28
28
|
|
|
29
29
|
clientSub.on('connect', () => {
|
|
30
|
-
clientPub = redis.createClient(adapterOptions)
|
|
30
|
+
clientPub = redis.createClient(adapterOptions);
|
|
31
31
|
|
|
32
|
-
this.log.info('Redis SUB client connected.')
|
|
32
|
+
this.log.info('Redis SUB client connected.');
|
|
33
33
|
|
|
34
34
|
clientPub.on('connect', () => {
|
|
35
35
|
if (this.interruptionCount > 0 && !this.isConnected) {
|
|
36
|
-
this.bus.emit('adapter.connected', true)
|
|
36
|
+
this.bus.emit('adapter.connected', true);
|
|
37
37
|
}
|
|
38
|
-
this.log.info('Redis PUB client connected.')
|
|
39
|
-
this.isConnected = true
|
|
40
|
-
resolve()
|
|
41
|
-
})
|
|
38
|
+
this.log.info('Redis PUB client connected.');
|
|
39
|
+
this.isConnected = true;
|
|
40
|
+
resolve();
|
|
41
|
+
});
|
|
42
42
|
|
|
43
43
|
clientPub.on('error', error => {
|
|
44
|
-
this.log.error('Redis PUB error:', error.message)
|
|
45
|
-
this.isConnected = false
|
|
46
|
-
reject(error)
|
|
47
|
-
})
|
|
44
|
+
this.log.error('Redis PUB error:', error.message);
|
|
45
|
+
this.isConnected = false;
|
|
46
|
+
reject(error);
|
|
47
|
+
});
|
|
48
48
|
|
|
49
49
|
clientPub.on('close', () => {
|
|
50
50
|
if (this.isConnected) {
|
|
51
|
-
this.isConnected = false
|
|
52
|
-
this.interruptionCount
|
|
53
|
-
this.log.warn('Redis PUB disconnected.')
|
|
54
|
-
this.disconnected()
|
|
51
|
+
this.isConnected = false;
|
|
52
|
+
this.interruptionCount++;
|
|
53
|
+
this.log.warn('Redis PUB disconnected.');
|
|
54
|
+
this.disconnected();
|
|
55
55
|
}
|
|
56
|
-
})
|
|
57
|
-
})
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
58
|
|
|
59
59
|
clientSub.on('error', error => {
|
|
60
|
-
this.log.error('Redis PUB error:', error.message)
|
|
61
|
-
reject(error)
|
|
62
|
-
})
|
|
60
|
+
this.log.error('Redis PUB error:', error.message);
|
|
61
|
+
reject(error);
|
|
62
|
+
});
|
|
63
63
|
|
|
64
64
|
clientSub.on('message', (topic, message) => {
|
|
65
|
-
const type = topic.split('.')[1]
|
|
66
|
-
this.incomingMessage(type, message)
|
|
67
|
-
})
|
|
65
|
+
const type = topic.split('.')[1];
|
|
66
|
+
this.incomingMessage(type, message);
|
|
67
|
+
});
|
|
68
68
|
|
|
69
69
|
clientSub.on('close', () => {
|
|
70
|
-
this.log.warn('Redis SUB disconnected.')
|
|
71
|
-
})
|
|
70
|
+
this.log.warn('Redis SUB disconnected.');
|
|
71
|
+
});
|
|
72
72
|
})
|
|
73
73
|
.then(() => {
|
|
74
|
-
this.connected()
|
|
75
|
-
})
|
|
74
|
+
this.connected();
|
|
75
|
+
});
|
|
76
76
|
},
|
|
77
77
|
subscribe (type, nodeId) {
|
|
78
78
|
return new Promise(resolve => {
|
|
79
|
-
const topic = this.getTopic(type, nodeId)
|
|
79
|
+
const topic = this.getTopic(type, nodeId);
|
|
80
80
|
clientSub.subscribe(topic, () => {
|
|
81
|
-
return resolve()
|
|
82
|
-
})
|
|
83
|
-
})
|
|
81
|
+
return resolve();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
84
|
},
|
|
85
85
|
send (message) {
|
|
86
|
-
const data = this.serialize(message)
|
|
86
|
+
const data = this.serialize(message);
|
|
87
87
|
if (this.isConnected) {
|
|
88
|
-
this.updateStatisticSent(data.length)
|
|
89
|
-
const topic = this.getTopic(message.type, message.targetNodeId)
|
|
90
|
-
clientPub.publish(topic, data)
|
|
88
|
+
this.updateStatisticSent(data.length);
|
|
89
|
+
const topic = this.getTopic(message.type, message.targetNodeId);
|
|
90
|
+
clientPub.publish(topic, data);
|
|
91
91
|
}
|
|
92
|
-
return Promise.resolve()
|
|
92
|
+
return Promise.resolve();
|
|
93
93
|
},
|
|
94
94
|
close () {
|
|
95
95
|
if (clientPub && clientSub) {
|
|
96
|
-
clientPub.quit()
|
|
97
|
-
clientSub.quit()
|
|
96
|
+
clientPub.quit();
|
|
97
|
+
clientSub.quit();
|
|
98
98
|
}
|
|
99
|
-
return promiseDelay(Promise.resolve(), 500)
|
|
99
|
+
return promiseDelay(Promise.resolve(), 500);
|
|
100
100
|
}
|
|
101
|
-
})
|
|
102
|
-
}
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
103
|
|
|
104
|
-
module.exports = RedisTransportAdapter
|
|
104
|
+
module.exports = RedisTransportAdapter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weave-js/redis-transport",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "weave redis transport adapter module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@weave-js/core": ">=0.9.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@weave-js/core": "^0.
|
|
26
|
+
"@weave-js/core": "^0.12.0"
|
|
27
27
|
},
|
|
28
28
|
"jest": {
|
|
29
29
|
"testEnvironment": "node",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
]
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@weave-js/utils": "^0.
|
|
36
|
+
"@weave-js/utils": "^0.11.0",
|
|
37
37
|
"redis": "^3.1.2"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "406e96b1f3017cc1d22de6693f61a2265506c239"
|
|
40
40
|
}
|
package/test/adapter.test.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
const { Weave } = require('@weave-js/core')
|
|
2
|
-
const REDISTransport = require('../lib/index')
|
|
1
|
+
const { Weave } = require('@weave-js/core');
|
|
2
|
+
const REDISTransport = require('../lib/index');
|
|
3
3
|
|
|
4
4
|
describe('REDIS transport adapter', () => {
|
|
5
|
-
let broker1
|
|
6
|
-
let broker2
|
|
7
|
-
const startedHook1 = jest.fn()
|
|
8
|
-
const startedHook2 = jest.fn()
|
|
5
|
+
let broker1;
|
|
6
|
+
let broker2;
|
|
7
|
+
const startedHook1 = jest.fn();
|
|
8
|
+
const startedHook2 = jest.fn();
|
|
9
9
|
|
|
10
10
|
beforeEach(() => {
|
|
11
11
|
broker1 = Weave({
|
|
@@ -18,16 +18,16 @@ describe('REDIS transport adapter', () => {
|
|
|
18
18
|
adapter: REDISTransport()
|
|
19
19
|
},
|
|
20
20
|
started: startedHook1
|
|
21
|
-
})
|
|
21
|
+
});
|
|
22
22
|
|
|
23
23
|
broker1.createService({
|
|
24
24
|
name: 'testService1',
|
|
25
25
|
actions: {
|
|
26
26
|
hello (context) {
|
|
27
|
-
return 'Hello from ' + context.nodeId
|
|
27
|
+
return 'Hello from ' + context.nodeId;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
})
|
|
30
|
+
});
|
|
31
31
|
|
|
32
32
|
broker2 = Weave({
|
|
33
33
|
nodeId: 'node2',
|
|
@@ -39,41 +39,41 @@ describe('REDIS transport adapter', () => {
|
|
|
39
39
|
adapter: REDISTransport()
|
|
40
40
|
},
|
|
41
41
|
started: startedHook2
|
|
42
|
-
})
|
|
42
|
+
});
|
|
43
43
|
|
|
44
44
|
broker2.createService({
|
|
45
45
|
name: 'testService2',
|
|
46
46
|
actions: {
|
|
47
47
|
hello () {
|
|
48
|
-
return 'Hello from ' + this.broker.nodeId
|
|
48
|
+
return 'Hello from ' + this.broker.nodeId;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
-
})
|
|
51
|
+
});
|
|
52
52
|
|
|
53
53
|
return Promise.all([
|
|
54
54
|
broker1.start(),
|
|
55
55
|
broker2.start()
|
|
56
|
-
])
|
|
57
|
-
})
|
|
56
|
+
]);
|
|
57
|
+
});
|
|
58
58
|
|
|
59
59
|
afterEach(() => {
|
|
60
|
-
return Promise.all([broker1.stop(), broker2.stop()])
|
|
61
|
-
})
|
|
60
|
+
return Promise.all([broker1.stop(), broker2.stop()]);
|
|
61
|
+
});
|
|
62
62
|
|
|
63
63
|
it('should connect', () => {
|
|
64
|
-
expect(startedHook1).toBeCalledTimes(1)
|
|
65
|
-
expect(startedHook2).toBeCalledTimes(1)
|
|
66
|
-
})
|
|
64
|
+
expect(startedHook1).toBeCalledTimes(1);
|
|
65
|
+
expect(startedHook2).toBeCalledTimes(1);
|
|
66
|
+
});
|
|
67
67
|
|
|
68
68
|
it('should get node info', (done) => {
|
|
69
69
|
broker1
|
|
70
70
|
.waitForServices(['testService2'])
|
|
71
71
|
.then(() => {
|
|
72
|
-
return broker1.call('testService2.hello')
|
|
72
|
+
return broker1.call('testService2.hello');
|
|
73
73
|
})
|
|
74
74
|
.then(result => {
|
|
75
|
-
expect(result).toBe('Hello from node2')
|
|
76
|
-
done()
|
|
77
|
-
})
|
|
78
|
-
})
|
|
79
|
-
})
|
|
75
|
+
expect(result).toBe('Hello from node2');
|
|
76
|
+
done();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|