eip-cloud-services 1.2.1 → 1.2.3
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/package.json +4 -1
- package/src/redis.js +16 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eip-cloud-services",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Houses a collection of helpers for connecting with Cloud services.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -24,5 +24,8 @@
|
|
|
24
24
|
"mysql": "^2.18.1",
|
|
25
25
|
"mysql2": "^3.14.4",
|
|
26
26
|
"redis": "^4.6.7"
|
|
27
|
+
},
|
|
28
|
+
"overrides": {
|
|
29
|
+
"jws": "4.0.1"
|
|
27
30
|
}
|
|
28
31
|
}
|
package/src/redis.js
CHANGED
|
@@ -8,6 +8,11 @@ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirector
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
const clients = {};
|
|
11
|
+
const applyPrefix = (value) => {
|
|
12
|
+
const prefix = config?.redis?.prefix;
|
|
13
|
+
if (!prefix || typeof value !== 'string') return value;
|
|
14
|
+
return value.startsWith(prefix) ? value : `${prefix}${value}`;
|
|
15
|
+
};
|
|
11
16
|
|
|
12
17
|
/**
|
|
13
18
|
* Creates or retrieves a Redis client instance based on a given client identifier.
|
|
@@ -725,9 +730,10 @@ exports.setExpiry = async ( key, seconds = 0 ) => {
|
|
|
725
730
|
exports.publish = async ( channel, message ) => {
|
|
726
731
|
try {
|
|
727
732
|
if ( typeof channel === 'string' && typeof message === 'string' ) {
|
|
728
|
-
const
|
|
733
|
+
const fullChannel = applyPrefix ( channel );
|
|
734
|
+
const client = await getClient ( `${fullChannel}_pub` );
|
|
729
735
|
|
|
730
|
-
return client.publish (
|
|
736
|
+
return client.publish ( fullChannel, message );
|
|
731
737
|
}
|
|
732
738
|
|
|
733
739
|
throw new Error ( 'redis.publish expects string types for both channel and message' );
|
|
@@ -749,15 +755,16 @@ exports.publish = async ( channel, message ) => {
|
|
|
749
755
|
exports.subscribe = async ( channel, messageHandler ) => {
|
|
750
756
|
try {
|
|
751
757
|
if ( typeof channel === 'string' && typeof messageHandler === 'function' ) {
|
|
752
|
-
const
|
|
758
|
+
const fullChannel = applyPrefix ( channel );
|
|
759
|
+
const client = await getClient ( `${fullChannel}_sub` );
|
|
753
760
|
|
|
754
761
|
client.on ( 'message', ( receivedChannel, message ) => {
|
|
755
|
-
if ( receivedChannel ===
|
|
762
|
+
if ( receivedChannel === fullChannel ) {
|
|
756
763
|
messageHandler ( message );
|
|
757
764
|
}
|
|
758
765
|
} );
|
|
759
766
|
|
|
760
|
-
await client.subscribe (
|
|
767
|
+
await client.subscribe ( fullChannel );
|
|
761
768
|
|
|
762
769
|
return;
|
|
763
770
|
}
|
|
@@ -781,12 +788,13 @@ exports.subscribe = async ( channel, messageHandler ) => {
|
|
|
781
788
|
exports.unsubscribe = async ( channel ) => {
|
|
782
789
|
try {
|
|
783
790
|
if ( typeof channel === 'string' ) {
|
|
784
|
-
const
|
|
791
|
+
const fullChannel = applyPrefix ( channel );
|
|
792
|
+
const client = await getClient ( `${fullChannel}_sub` );
|
|
785
793
|
|
|
786
794
|
client.removeAllListeners ( 'message' );
|
|
787
795
|
|
|
788
|
-
await client.unsubscribe (
|
|
789
|
-
await deleteClient ( `${
|
|
796
|
+
await client.unsubscribe ( fullChannel );
|
|
797
|
+
await deleteClient ( `${fullChannel}_sub` );
|
|
790
798
|
|
|
791
799
|
return;
|
|
792
800
|
}
|