ScanStream.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 scaning 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. this.opt.redis[this.opt.command](args, (err, res) => {
  33. if (err) {
  34. this.emit("error", err);
  35. return;
  36. }
  37. this._redisCursor = res[0] instanceof Buffer ? res[0].toString() : res[0];
  38. if (this._redisCursor === "0") {
  39. this._redisDrained = true;
  40. }
  41. this.push(res[1]);
  42. });
  43. }
  44. close() {
  45. this._redisDrained = true;
  46. }
  47. }
  48. exports.default = ScanStream;