converting to MP, just config in accessRights left
This commit is contained in:
+35
-47
@@ -4,16 +4,17 @@ export class RedisConnexion {
|
||||
constructor(options) {
|
||||
this.config = options.config;
|
||||
this.debug = options.debug;
|
||||
this.redisId = options.redisId;
|
||||
this.redisConfig = this.config.redis[this.redisId]
|
||||
|
||||
this.subscriptions = {}; // Externally fed
|
||||
this.wssConnections = {}; // Externally fed
|
||||
this.redPillsUuids = []; // Externally fed
|
||||
|
||||
this.redisClient = redis.createClient({
|
||||
socket: {
|
||||
tls: this.config.redis.tls,
|
||||
host: this.config.redis.host,
|
||||
port: this.config.redis.port
|
||||
tls: this.redisConfig.tls,
|
||||
host: this.redisConfig.host,
|
||||
port: this.redisConfig.port
|
||||
}
|
||||
});
|
||||
|
||||
@@ -42,11 +43,11 @@ export class RedisConnexion {
|
||||
}
|
||||
|
||||
async redisLogin(){
|
||||
if(this.debug) console.log(`Connecting to Redis (${this.config.redis.host}:${this.config.redis.port}, tls:${this.config.redis.tls?'yes':'no'})...`);
|
||||
if(this.debug) console.log(`Connecting to Redis (${this.redisConfig.host}:${this.redisConfig.port}, tls:${this.redisConfig.tls?'yes':'no'})...`);
|
||||
await this.redisClient.connect();
|
||||
if(this.debug) console.log('Connected to Redis !');
|
||||
if(this.config.redis.user) {
|
||||
await this.redisClient.sendCommand(['AUTH', this.config.redis.user, this.config.redis.pass]);
|
||||
if(this.redisConfig.user) {
|
||||
await this.redisClient.sendCommand(['AUTH', this.redisConfig.user, this.redisConfig.pass]);
|
||||
if(this.debug) console.log('Logged into Redis !');
|
||||
} else {
|
||||
if(this.debug) console.log('Connected (anon) to Redis...');
|
||||
@@ -60,32 +61,38 @@ export class RedisConnexion {
|
||||
async redisChansStart(){
|
||||
this.redisSubscriber = this.redisClient.duplicate();
|
||||
await this.redisSubscriber.connect();
|
||||
if(this.config.redis.user) {
|
||||
await this.redisSubscriber.sendCommand(['AUTH', this.config.redis.user, this.config.redis.pass]);
|
||||
if(this.redisConfig.user) {
|
||||
await this.redisSubscriber.sendCommand(['AUTH', this.redisConfig.user, this.redisConfig.pass]);
|
||||
}
|
||||
this.redisSubscriber.pSubscribe(this.config.redis.basePrefix + '*', this.redisReceive.bind(this));
|
||||
if(this.debug) console.log('PSubscription OK ', this.config.redis.basePrefix + '*');
|
||||
const allChans = this.redisConfig.basePrefix + this.redisConfig.ChansFilter+'*'
|
||||
this.redisSubscriber.pSubscribe(allChans, this.redisReceive.bind(this));
|
||||
if(this.debug) console.log('PSubscription OK ', allChans);
|
||||
}
|
||||
|
||||
async redisSubscribe(chanName, callBack){
|
||||
if(!chanName.startsWith(this.config.redis.basePrefix)) chanName = this.config.redis.basePrefix + chanName
|
||||
if(!chanName.startsWith(this.redisConfig.basePrefix)) chanName = this.redisConfig.basePrefix + chanName
|
||||
if(!chanName.startsWith(this.redisConfig.basePrefix+this.ChansFilter)) {
|
||||
console.warn(`redisSubscribe : forbidden channel range on this redis !`)
|
||||
return
|
||||
}
|
||||
await this.redisSubscriber.subscribe(chanName, callBack);
|
||||
}
|
||||
|
||||
async redisPublish(chanName, msg){
|
||||
if(typeof (msg) != 'string') msg = JSON.stringify(msg);
|
||||
if(!chanName.startsWith(this.config.redis.basePrefix)) chanName = this.config.redis.basePrefix + chanName
|
||||
await this.redisClient.publish(chanName, msg);
|
||||
}
|
||||
if(!chanName.startsWith(this.redisConfig.basePrefix)) chanName = this.redisConfig.basePrefix + chanName
|
||||
if(!chanName.startsWith(this.redisConfig.basePrefix+this.ChansFilter)) {
|
||||
console.warn(`redisPublish : forbidden channel range on this redis !`)
|
||||
return
|
||||
}
|
||||
|
||||
async redisRefreshSession(k){
|
||||
await this.redisClient.expire(k, this.config.server.sessionExpiration);
|
||||
await this.redisClient.publish(chanName, msg);
|
||||
}
|
||||
|
||||
async redisSet(k, v, exp = 0, customPrefix=null){
|
||||
if(typeof(v) != 'string') v = JSON.stringify(v);
|
||||
if(customPrefix!==null) k = customPrefix + k
|
||||
else if(!k.startsWith(this.config.redis.basePrefix)) k = this.config.redis.basePrefix + k
|
||||
else if(!k.startsWith(this.redisConfig.basePrefix)) k = this.redisConfig.basePrefix + k
|
||||
if(this.debug) console.log('Redis SET ', k);
|
||||
try { await this.redisClient.set(k, v) }
|
||||
catch(err) { console.error('Redis crash doing Redis set: ', k, v) }
|
||||
@@ -97,7 +104,7 @@ export class RedisConnexion {
|
||||
|
||||
async redisGet(k, customPrefix=null){
|
||||
if(customPrefix!==null) k = customPrefix + k
|
||||
else if(!k.startsWith(this.config.redis.basePrefix)) k = this.config.redis.basePrefix + k
|
||||
else if(!k.startsWith(this.redisConfig.basePrefix)) k = this.redisConfig.basePrefix + k
|
||||
if(this.debug) console.log('Redis GET ', k)
|
||||
let v=null
|
||||
try { v = await this.redisClient.get(k) }
|
||||
@@ -107,7 +114,7 @@ export class RedisConnexion {
|
||||
|
||||
async redisDel(k, customPrefix=null){
|
||||
if(customPrefix!==null) k = customPrefix + k
|
||||
else if(!k.startsWith(this.config.redis.basePrefix)) k = this.config.redis.basePrefix + k
|
||||
else if(!k.startsWith(this.redisConfig.basePrefix)) k = this.redisConfig.basePrefix + k
|
||||
if(this.debug) console.log('Deleting ', k);
|
||||
await this.redisClient.del(k);
|
||||
}
|
||||
@@ -115,7 +122,7 @@ export class RedisConnexion {
|
||||
|
||||
async redisGetTtl(k, customPrefix=null){
|
||||
if(customPrefix!==null) k = customPrefix + k
|
||||
else if(!k.startsWith(this.config.redis.basePrefix)) k = this.config.redis.basePrefix + k
|
||||
else if(!k.startsWith(this.redisConfig.basePrefix)) k = this.redisConfig.basePrefix + k
|
||||
if(this.debug) console.log('Redis Get TTL ', k)
|
||||
let v=null
|
||||
try { v = await this.redisClient.ttl(k) }
|
||||
@@ -125,14 +132,14 @@ export class RedisConnexion {
|
||||
|
||||
async redisSetTtl(k, ttl, customPrefix=null){
|
||||
if(customPrefix!==null) k = customPrefix + k
|
||||
else if(!k.startsWith(this.config.redis.basePrefix)) k = this.config.redis.basePrefix + k
|
||||
else if(!k.startsWith(this.redisConfig.basePrefix)) k = this.redisConfig.basePrefix + k
|
||||
if(this.debug) console.log('Redis Set TTL ', k);
|
||||
try { await this.redisClient.expire(k, ttl) }
|
||||
catch(err) { console.error('Redis crash doing Redis expire: ', k, ttl) }
|
||||
}
|
||||
|
||||
async redisXadd(streamName, kvObj, max = ''){
|
||||
if(!streamName.startsWith(this.config.redis.basePrefix)) streamName = this.config.redis.basePrefix + streamName
|
||||
if(!streamName.startsWith(this.redisConfig.basePrefix)) streamName = this.redisConfig.basePrefix + streamName
|
||||
if(this.debug) console.log('Redis XADD ', streamName, kvObj);
|
||||
let arr = ['XADD', streamName]
|
||||
if(max != '') arr = [...arr, ...['MAXLEN', '~', (1*max).toString()]]
|
||||
@@ -149,7 +156,7 @@ export class RedisConnexion {
|
||||
}
|
||||
|
||||
async redisXrange(streamName, start = '-', end = '+', withPayload = true){
|
||||
if(!streamName.startsWith(this.config.redis.basePrefix)) streamName = this.config.redis.basePrefix + streamName
|
||||
if(!streamName.startsWith(this.redisConfig.basePrefix)) streamName = this.redisConfig.basePrefix + streamName
|
||||
if(this.debug) console.log('Redis XRANGE ', streamName);
|
||||
if(typeof(start)!='string') start = start.toString()
|
||||
if(typeof(end)!='string') end = end.toString()
|
||||
@@ -173,9 +180,9 @@ export class RedisConnexion {
|
||||
}
|
||||
|
||||
isHistorizedChan(chan){
|
||||
if(!chan.startsWith(this.config.redis.basePrefix)) chan = this.config.redis.basePrefix + chan
|
||||
var matches = this.config.redis.historizeChannels.filter((e) => {
|
||||
if(!e.startsWith(this.config.redis.basePrefix)) e = this.config.redis.basePrefix + e
|
||||
if(!chan.startsWith(this.redisConfig.basePrefix)) chan = this.redisConfig.basePrefix + chan
|
||||
var matches = this.redisConfig.historizeChannels.filter((e) => {
|
||||
if(!e.startsWith(this.redisConfig.basePrefix)) e = this.redisConfig.basePrefix + e
|
||||
if(e.indexOf('*') > -1) {
|
||||
let r = new RegExp('^'+e.replace(/\*/g,'(.+)')+'$','g')
|
||||
return(chan.match(r) != null);
|
||||
@@ -206,30 +213,11 @@ export class RedisConnexion {
|
||||
return;
|
||||
}
|
||||
|
||||
if(this.redPillsUuids.length>0) { // Any dev bus console in RedPills (promiscuous) mode ?
|
||||
if(this.debug) console.log(`Will send to ${this.redPillsUuids.length} REDPILLS`);
|
||||
let shortChan = chan.startsWith(this.config.redis.basePrefix) ? chan.substr(this.config.redis.basePrefix.length) : chan
|
||||
let payload ={
|
||||
'event': 'REDISMSG',
|
||||
'payload': {
|
||||
'bmsg':{ // Extra encapsulation to avoid triggering normal listeners on FE
|
||||
'msg': msg,
|
||||
'chan': shortChan,
|
||||
}
|
||||
}
|
||||
}
|
||||
for(var uuid of this.redPillsUuids) {
|
||||
if(uuid in this.wssConnections) {
|
||||
this.wssConnections[uuid].send(JSON.stringify(payload));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(this.debug) console.log('will now fanout...', chan, msg);
|
||||
const uuids = this.getSubscribedUuids(chan)
|
||||
if(uuids.length>0) { // Anyone interested at all about this chan ?
|
||||
if(this.debug) console.log(`Will broadcast to ${uuids.length} web clients`);
|
||||
let shortChan = chan.startsWith(this.config.redis.basePrefix) ? chan.substr(this.config.redis.basePrefix.length) : chan
|
||||
let shortChan = chan.startsWith(this.redisConfig.basePrefix) ? chan.substr(this.redisConfig.basePrefix.length) : chan
|
||||
let payload ={
|
||||
'event': 'REDISMSG',
|
||||
'payload': {
|
||||
|
||||
Reference in New Issue
Block a user