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AAG,KACjC,KACA,EACA,EAAG,KAAK,sBACR,EAEJ,EACA,qBAAqB,UAAU,eAAiB,SAAU,EAAO,EAAM,GACrE,MAAM,EAAI,KAAK,cAAgB,EACzB,EAAI,EAAK,KAAK,sBACpB,OAAO,eAAe,UAAU,eAAe,KAAK,KAAM,EAAG,EAAG,EAClE,EACA,qBAAqB,UAAU,KAAO,SAAU,EAAQ,GACtD,MAAM,EAAI,KAAK,cAAgB,EAAO,MAChC,EAAI,IAAK,EAAQ,MAAO,GAC9B,OAAO,eAAe,UAAU,KAAK,KAAK,KAAM,EAAG,EACrD,EAEA,OAAO,QAAQ,qBAAuB","file":"/npm/@cool-midway/mqemitter-redis@7.0.0/mqemitter-redis.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@cool-midway/mqemitter-redis@7.0.0/mqemitter-redis.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","\"use strict\";\n\nconst Redis = require(\"ioredis\");\nconst MQEmitter = require(\"mqemitter\");\nconst hyperid = require(\"hyperid\")();\nconst inherits = require(\"inherits\");\nconst LRU = require(\"lru-cache\");\nconst msgpack = require(\"msgpack-lite\");\nconst EE = require(\"events\").EventEmitter;\nconst Pipeline = require(\"ioredis-auto-pipeline\");\n\nfunction MQEmitterRedis(opts) {\n  if (!(this instanceof MQEmitterRedis)) {\n    return new MQEmitterRedis(opts);\n  }\n\n  opts = opts || {};\n\n  this._opts = opts;\n\n  if (opts instanceof Array) {\n    this.subConn = new Redis.Cluster(opts);\n    this.pubConn = new Redis.Cluster(opts);\n  } else {\n    this.subConn = new Redis(opts.connectionString || opts);\n    this.pubConn = new Redis(opts.connectionString || opts);\n  }\n\n  this._pipeline = Pipeline(this.pubConn);\n\n  this._topics = {};\n\n  this._cache = new LRU({\n    max: 10000,\n    ttl: 60 * 1000, // one minute\n  });\n\n  this.state = new EE();\n\n  const that = this;\n\n  function onError(err) {\n    if (err && !that.closing) {\n      that.state.emit(\"error\", err);\n    }\n  }\n\n  this._onError = onError;\n\n  function handler(sub, topic, payload) {\n    const packet = msgpack.decode(payload);\n    if (!that._cache.get(packet.id)) {\n      that._emit(packet.msg);\n    }\n    that._cache.set(packet.id, true);\n  }\n\n  this.subConn.on(\"messageBuffer\", function (topic, message) {\n    handler(topic, topic, message);\n  });\n\n  this.subConn.on(\"pmessageBuffer\", function (sub, topic, message) {\n    handler(sub, topic, message);\n  });\n\n  this.subConn.on(\"connect\", function () {\n    that.state.emit(\"subConnect\");\n  });\n\n  this.subConn.on(\"error\", function (err) {\n    that._onError(err);\n  });\n\n  this.pubConn.on(\"connect\", function () {\n    that.state.emit(\"pubConnect\");\n  });\n\n  this.pubConn.on(\"error\", function (err) {\n    that._onError(err);\n  });\n\n  MQEmitter.call(this, opts);\n\n  this._opts.regexWildcardOne = new RegExp(\n    this._opts.wildcardOne.replace(/([/,!\\\\^${}[\\]().*+?|<>\\-&])/g, \"\\\\$&\"),\n    \"g\"\n  );\n  this._opts.regexWildcardSome = new RegExp(\n    (this._opts.matchEmptyLevels\n      ? this._opts.separator.replace(/([/,!\\\\^${}[\\]().*+?|<>\\-&])/g, \"\\\\$&\") +\n        \"?\"\n      : \"\") +\n      this._opts.wildcardSome.replace(/([/,!\\\\^${}[\\]().*+?|<>\\-&])/g, \"\\\\$&\"),\n    \"g\"\n  );\n}\n\ninherits(MQEmitterRedis, MQEmitter);\n[\"emit\", \"on\", \"removeListener\", \"close\"].forEach(function (name) {\n  MQEmitterRedis.prototype[\"_\" + name] = MQEmitterRedis.prototype[name];\n});\n\nMQEmitterRedis.prototype.close = function (cb) {\n  cb = cb || noop;\n\n  if (this.closed || this.closing) {\n    return cb();\n  }\n\n  this.closing = true;\n\n  let count = 2;\n  const that = this;\n\n  function onEnd() {\n    if (--count === 0) {\n      that._close(cb);\n    }\n  }\n\n  this.subConn.on(\"end\", onEnd);\n  this.subConn.quit();\n\n  this.pubConn.on(\"end\", onEnd);\n  this.pubConn.quit();\n\n  return this;\n};\n\nMQEmitterRedis.prototype._subTopic = function (topic) {\n  return topic\n    .replace(this._opts.regexWildcardOne, \"*\")\n    .replace(this._opts.regexWildcardSome, \"*\");\n};\n\nMQEmitterRedis.prototype.on = function on(topic, cb, done) {\n  const subTopic = this._subTopic(topic);\n  const onFinish = function () {\n    if (done) {\n      setImmediate(done);\n    }\n  };\n\n  this._on(topic, cb);\n\n  if (this._topics[subTopic]) {\n    this._topics[subTopic]++;\n    onFinish.call(this);\n    return this;\n  }\n\n  this._topics[subTopic] = 1;\n\n  if (this._containsWildcard(topic)) {\n    this.subConn.psubscribe(subTopic, onFinish.bind(this));\n  } else {\n    this.subConn.subscribe(subTopic, onFinish.bind(this));\n  }\n\n  return this;\n};\n\nMQEmitterRedis.prototype.emit = function (msg, done) {\n  done = done || this._onError;\n\n  if (this.closed) {\n    const err = new Error(\"mqemitter-redis is closed\");\n    return done(err);\n  }\n\n  const packet = {\n    id: hyperid(),\n    msg,\n  };\n\n  this._pipeline\n    .publish(msg.topic, msgpack.encode(packet))\n    .then(() => done())\n    .catch(done);\n};\n\nMQEmitterRedis.prototype.removeListener = function (topic, cb, done) {\n  const subTopic = this._subTopic(topic);\n  const onFinish = function () {\n    if (done) {\n      setImmediate(done);\n    }\n  };\n\n  this._removeListener(topic, cb);\n\n  if (--this._topics[subTopic] > 0) {\n    onFinish();\n    return this;\n  }\n\n  delete this._topics[subTopic];\n\n  if (this._containsWildcard(topic)) {\n    this.subConn.punsubscribe(subTopic, onFinish);\n  } else if (this._matcher.match(topic)) {\n    this.subConn.unsubscribe(subTopic, onFinish);\n  }\n\n  return this;\n};\n\nMQEmitterRedis.prototype._containsWildcard = function (topic) {\n  return (\n    topic.indexOf(this._opts.wildcardOne) >= 0 ||\n    topic.indexOf(this._opts.wildcardSome) >= 0\n  );\n};\n\nfunction noop() {}\n\nmodule.exports = MQEmitterRedis;\n\nfunction MQEmitterRedisPrefix(pubSubPrefix, options) {\n  MQEmitterRedis.call(this, options);\n  this._pubSubPrefix = pubSubPrefix;\n  this._sym_proxiedCallback = Symbol(\"proxiedCallback\");\n}\ninherits(MQEmitterRedisPrefix, MQEmitterRedis);\nMQEmitterRedisPrefix.prototype.on = function (topic, cb, done) {\n  const t = this._pubSubPrefix + topic;\n  cb[this._sym_proxiedCallback] = function (packet, cbcb) {\n    const t = packet.topic.slice(this._pubSubPrefix.length);\n    const p = { ...packet, topic: t };\n    return cb.call(this, p, cbcb);\n  }.bind(this);\n  return MQEmitterRedis.prototype.on.call(\n    this,\n    t,\n    cb[this._sym_proxiedCallback],\n    done\n  );\n};\nMQEmitterRedisPrefix.prototype.removeListener = function (topic, func, done) {\n  const t = this._pubSubPrefix + topic;\n  const f = func[this._sym_proxiedCallback];\n  return MQEmitterRedis.prototype.removeListener.call(this, t, f, done);\n};\nMQEmitterRedisPrefix.prototype.emit = function (packet, done) {\n  const t = this._pubSubPrefix + packet.topic;\n  const p = { ...packet, topic: t };\n  return MQEmitterRedis.prototype.emit.call(this, p, done);\n};\n\nmodule.exports.MQEmitterRedisPrefix = MQEmitterRedisPrefix;\n"]}