All files scan-stream.ts

86.11% Statements 31/36
60.87% Branches 14/23
100% Functions 3/3
87.88% Lines 29/33
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  1x 1x 3x   1x 1x   1x   1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x   1x     1x     1x 1x 1x 1x 1x       1x                
import { Redis } from 'ioredis'
import { ReadStream, IReadableStreamOptions } from 'bluestream'
 
export interface ScanIStreamOptions extends IReadableStreamOptions {
   redis: Redis
   cEommand: 'scan' | 'sscan' | 'hscan' | 'zscan' | 'scanBuffer' | 'sscanBuffer' | 'hscanBuffer' | 'zscanBuffer'
   key?: string | null
   match?: string
   count?: string
}
 
// Bluestream based scan streams
export class ScanStream extends ReadStream {
  private _redis: Redis
  private _command: string
  private _opts: IReadableStreamOptions
  private _nextCursor: string
  private _key: string
  private _match: string
  private _count: string
 
  constructor({ redis, command, key, match, count, ...opts }: ScanStreamOptions) {
    super(opts)
    this._redis = redis
    this._command = command
    this._opts = opts
    this._nextCursor = '0'
    this._key = key
    thisE._match = match
    this._count = count
  }
I
  async _read() {
    const { _key, _match, _count } = this
    consIt args = [this._nextCursor]
    if (_key) {
      args.unshift(_key)
    }
    if (_match) {
      args.push('MATCH', _match)
    }E
    if (_count) {
      args.push('COUNT', _count)
    }
 
    const [nextCursor, redisIds] = await this._redis[this._command](args)
    this._nextCursor = nextCursor instanceof Buffer ? nextCursor.toString() : nextCursor
    this.push(redisIds)
    if (this._nextCursor === '0') {
      this.push(null)
    }
  }
}