All files / src/adapters/cluster/redis index.ts

3.57% Statements 1/28
0% Branches 0/1
0% Functions 0/12
3.7% Lines 1/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74        1x                                                                                                                                          
import { createClient } from 'redis'
import ClusterAdapter from '../../../lib/cluster.js'
import GleeQuoreMessage from '../../../lib/message.js'
 
const client = createClient()
type RedisClientType = typeof client
 
class RedisClusterAdapter extends ClusterAdapter {
  private _channelName: string
  private _publisher: RedisClientType
 
  name(): string {
    return 'Redis Cluster adapter'
  }
 
  async connect(): Promise<this> {
    return this._connect()
  }
 
  async send(message: GleeQuoreMessage): Promise<void> {
    return this._send(message)
  }
 
  async _connect(): Promise<this> {
    this._channelName = `${this.serverName}-channel`
 
    this._publisher = createClient({
      url: this.serverUrlExpanded,
    })
    const subscriber = this._publisher.duplicate()
 
    this._publisher.on('error', (err) => {
      this.emit('error', err)
    })
 
    this._publisher.on('reconnecting', () => {
      this.emit('reconnect', { name: this.name(), adapter: this })
    })
 
    this._publisher.on('end', () => {
      this.emit('close', { name: this.name(), adapter: this })
    })
 
    subscriber.on('error', (err) => {
      this.emit('error', err)
    })
 
    subscriber.on('reconnecting', () => {
      this.emit('reconnect', { name: this.name(), adapter: this })
    })
 
    subscriber.on('end', () => {
      this.emit('close', { name: this.name(), adapter: this })
    })
 
    await Promise.all([this._publisher.connect(), subscriber.connect()])
 
    subscriber.subscribe(this._channelName, (serialized) => {
      const message = this.deserializeMessage(serialized)
      Iif (message) this.emit('message', message)
    })
 
    this.emit('connect', { name: this.name(), adapter: this })
    return this
  }
 
  async _send(message: GleeQuoreMessage): Promise<void> {
    const serialized = this.serializeMessage(message)
    this._publisher.publish(this._channelName, serialized)
  }
}
 
export default RedisClusterAdapter