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)
}
}
}
|