ScanStream.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const stream_1 = require("stream");
  4. /**
  5. * Convenient class to convert the process of scanning keys to a readable stream.
  6. */
  7. class ScanStream extends stream_1.Readable {
  8. constructor(opt) {
  9. super(opt);
  10. this.opt = opt;
  11. this._redisCursor = "0";
  12. this._redisDrained = false;
  13. }
  14. _read() {
  15. if (this._redisDrained) {
  16. this.push(null);
  17. return;
  18. }
  19. const args = [this._redisCursor];
  20. if (this.opt.key) {
  21. args.unshift(this.opt.key);
  22. }
  23. if (this.opt.match) {
  24. args.push("MATCH", this.opt.match);
  25. }
  26. if (this.opt.type) {
  27. args.push("TYPE", this.opt.type);
  28. }
  29. if (this.opt.count) {
  30. args.push("COUNT", String(this.opt.count));
  31. }
  32. if (this.opt.noValues) {
  33. args.push("NOVALUES");
  34. }
  35. this.opt.redis[this.opt.command](args, (err, res) => {
  36. if (err) {
  37. this.emit("error", err);
  38. return;
  39. }
  40. this._redisCursor = res[0] instanceof Buffer ? res[0].toString() : res[0];
  41. if (this._redisCursor === "0") {
  42. this._redisDrained = true;
  43. }
  44. this.push(res[1]);
  45. });
  46. }
  47. close() {
  48. this._redisDrained = true;
  49. }
  50. }
  51. exports.default = ScanStream;