redis-smq-common 3.0.0-rc.7 → 3.0.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/dist/index.cjs +2 -3
- package/dist/index.d.cts +5 -20
- package/dist/index.d.ts +5 -20
- package/dist/index.js +2 -3
- package/package.json +1 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
+
## [3.0.0-rc.9](https://github.com/weyoss/redis-smq-common/compare/v3.0.0-rc.8...v3.0.0-rc.9) (2024-01-25)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Misc
|
|
7
|
+
|
|
8
|
+
* update package-lock.json ([d001ba1](https://github.com/weyoss/redis-smq-common/commit/d001ba1938ed34e4a76f5da1814de56dde72ca99))
|
|
9
|
+
|
|
10
|
+
## [3.0.0-rc.8](https://github.com/weyoss/redis-smq-common/compare/v3.0.0-rc.7...v3.0.0-rc.8) (2024-01-25)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### ⚠ BREAKING CHANGES
|
|
14
|
+
|
|
15
|
+
* drop support for node-redis v3
|
|
16
|
+
|
|
17
|
+
### Documentation
|
|
18
|
+
|
|
19
|
+
* drop support for node-redis v3 ([3481d53](https://github.com/weyoss/redis-smq-common/commit/3481d53de1df805e9a4fc9ec244c6f6b7e131947))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Codebase Refactoring
|
|
23
|
+
|
|
24
|
+
* drop support for node-redis v3 ([0b89843](https://github.com/weyoss/redis-smq-common/commit/0b8984396ec0ccb57eb1e183e9b7b0a6f1935e9b))
|
|
25
|
+
|
|
3
26
|
## [3.0.0-rc.7](https://github.com/weyoss/redis-smq-common/compare/v3.0.0-rc.6...v3.0.0-rc.7) (2023-11-26)
|
|
4
27
|
|
|
5
28
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`)[1].split(":")[1].split(".").map(n=>Number(n)),t()):t(new b)})}loadScripts(t){U.getInstance().loadScripts(this,t)}runScript(t,i,e,n){let s=U.getInstance().getScriptId(t);this.evalsha(s,[i.length,...i,...e],(l,a)=>{l?n(l):n(null,a)})}static addScript(t,i){if(!U.getInstance().addScript(t,i))throw new u(`A script with name [${t}] already exists`)}};v.redisServerVersion=null;var p=v;p.addScript("RELEASE_LOCK",et.default.readFileSync((0,rt.resolve)(__dirname,"./lua/release-lock.lua")).toString());p.addScript("EXTEND_LOCK",et.default.readFileSync((0,rt.resolve)(__dirname,"./lua/extend-lock.lua")).toString());var F=class{constructor(r,t,i,e=!1,n=!1,s=!0){this.status=0;this.lockingTimer=null;this.autoExtendTimer=null;this.throwExceptions=!0;this.lockKey=t,this.ttl=i,this.retryOnFail=e,this.lockId=(0,ut.v4)(),this.redisClient=r,this.autoExtend=n,this.throwExceptions=s}resetTimers(){this.lockingTimer&&(clearTimeout(this.lockingTimer),this.lockingTimer=null),this.autoExtendTimer&&(clearTimeout(this.autoExtendTimer),this.autoExtendTimer=null)}setUnlocked(){this.status=0}setLocked(){this.status=2}setExtended(){this.status=5}extend(r){this.isLocked()?(this.status=4,this.redisClient.runScript("EXTEND_LOCK",[this.lockKey],[this.lockId,this.ttl],(t,i)=>{t?r(t):this.status===4?i?(this.setExtended(),r()):(this.setUnlocked(),r(new M)):r(new x)})):r(new O)}runAutoExtendTimer(){let r=Math.ceil(this.ttl/2);this.autoExtendTimer=setTimeout(()=>this.extend(t=>{if(!t)this.runAutoExtendTimer();else if(this.throwExceptions&&!(t instanceof x))throw t}),r)}acquireLock(r){if(!this.isReleased())r(new H);else{this.status=1;let t=()=>{this.status===1?this.redisClient.set(this.lockKey,this.lockId,{expire:{mode:"PX",value:this.ttl},exists:"NX"},(i,e)=>{i?r(i):this.status===1?e?(this.setLocked(),this.autoExtend&&this.runAutoExtendTimer(),r()):this.retryOnFail?this.lockingTimer=setTimeout(t,1e3):(this.setUnlocked(),r(new R)):r(new x)}):r(new x)};t()}}extendLock(r){this.autoExtend?r(new N):this.extend(r)}releaseLock(r){this.status===0?r():this.isLocked()?(this.resetTimers(),this.status=3,this.redisClient.runScript("RELEASE_LOCK",[this.lockKey],[this.lockId],i=>{i?r(i):(this.setUnlocked(),r())})):r(new O)}acquireOrExtend(r){if(this.autoExtend)r(new N);else{let t=()=>{this.acquireLock(i=>{i?r(i):r(null,2)})};this.isLocked()?this.extend(i=>{i?i instanceof M?t():r(i):r(null,5)}):t()}}isLocked(){return this.status===2||this.status===5}isReleased(){return this.status===0}getId(){return this.lockId}};var f=class{constructor(r=!0){this.isPowered=!1;this.pendingState=null;this.throwExceptionOnError=r}switch(r){if(this.pendingState!==null){if(this.throwExceptionOnError)throw new w("Can not switch state while another state transition is in progress.");return!1}if(r===this.isPowered){if(this.throwExceptionOnError)throw new w("Can not switch to the same current state.");return!1}return this.pendingState=r,!0}isUp(){return this.isPowered}isDown(){return!this.isPowered}isGoingUp(){return this.pendingState===!0}isGoingDown(){return this.pendingState===!1}isRunning(){return this.isUp()&&this.pendingState===null}goingUp(){return this.switch(!0)}goingDown(){return this.switch(!1)}commit(){if(this.pendingState===null)throw new w("Expected a pending state");this.isPowered=this.pendingState,this.pendingState=null}rollback(){if(this.pendingState===null)throw new w("Expected a pending state");this.pendingState=null}};var ht=require("events"),z=class extends ht.EventEmitter{};var y=class extends z{constructor(t=()=>{},i=1e3){super();this.powerManager=new f;this.onNextTickFn=null;this.timeout=null;this.interval=null;this.shutdownTimeout=null;this.aborted=!1;this.onTickFn=t,this.time=i,this.powerManager.goingUp()}shutdown(){this.shutdownTimeout&&clearTimeout(this.shutdownTimeout),this.powerManager.commit(),this.emit("down")}onTick(){var t;if(this.powerManager.isGoingDown())this.shutdown();else if(this.powerManager.isRunning()){let i=(t=this.onNextTickFn)!=null?t:this.onTickFn;this.onNextTickFn=null,i()}else this.emit("error",new T("Unexpected call"))}abort(){this.aborted||(this.aborted=!0,this.powerManager.isGoingDown()?this.shutdown():this.quit())}quit(){this.powerManager.isGoingUp()?(this.powerManager.rollback(),this.emit("down")):this.aborted&&this.powerManager.isDown()?this.emit("down"):(this.powerManager.goingDown(),this.timeout?(clearTimeout(this.timeout),this.shutdown()):this.interval?(clearInterval(this.interval),this.shutdown()):this.aborted?this.shutdown():this.shutdownTimeout=setTimeout(()=>{this.powerManager.isGoingDown()&&this.shutdown()},6e4))}isTicking(){return!!(this.timeout||this.interval)}nextTick(){if(this.isTicking())throw new L("A timer is already running");this.powerManager.isGoingDown()?this.shutdown():(this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.timeout=setTimeout(()=>{this.timeout=null,this.onTick()},this.time)))}nextTickFn(t){this.onNextTickFn=t,this.nextTick()}runTimer(){if(this.isTicking())throw new L("A timer is already running");this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.interval=setInterval(()=>this.onTick(),this.time))}};var q=class{constructor(r,t=1e3){this.ticker=null;this.powerManager=null;this.getTicker=()=>{if(!this.ticker)throw new E("Expected an instance of Ticker");return this.ticker};this.onTick=()=>{this.work(r=>{if(r)throw r;this.getTicker().nextTick()})};this.run=()=>{if(this.managed)throw new E("You can not run a managed worker");let r=this.getPowerManager();r.goingUp(),this.getTicker().nextTick(),r.commit()};this.quit=r=>{if(this.managed)r();else{let t=this.getPowerManager();t.goingDown();let i=this.getTicker();i.on("down",()=>{t.commit(),r()}),i.quit()}};this.managed=r,r||(this.ticker=new y(this.onTick,t),this.powerManager=new f)}getPowerManager(){if(!this.powerManager)throw new E("Expected an instance of PowerSwitch");return this.powerManager}};var G=class extends z{constructor(t,i,e,n){super();this.onTick=()=>{k.waterfall([t=>{this.lock.acquireOrExtend((i,e)=>{e===2&&this.logger.info(`Workers are exclusively running from this instance (Lock ID ${this.lock.getId()}).`),t(i)})},t=>{this.workerPool.work(t)}],t=>{!t||t instanceof R?this.ticker.nextTick():this.emit("error",t)})};this.clearWorkerPool=t=>{this.workerPool.clear(t)};this.stopTicker=t=>{this.ticker.once("down",t),this.ticker.quit()};this.releaseLock=t=>{this.lock.releaseLock(t)};this.run=()=>{this.emit("up"),this.ticker.nextTick()};this.quit=t=>{k.waterfall([this.stopTicker,this.clearWorkerPool,this.releaseLock],()=>{this.emit("down"),t()})};this.powerManager=new f,this.redisClient=t,this.logger=n,this.lock=new F(t,i,6e4),this.ticker=new y(this.onTick),this.workerPool=e}addWorker(t){this.workerPool.add(t)}};var _=class{constructor(){this.pool=[];this.work=r=>{if(this.pool.length){let t=this.pool.map(i=>e=>i.work(e));k.waterfall(t,r)}else r()};this.add=r=>(this.pool.push(r),this.pool.length);this.clear=r=>{k.each(this.pool,(t,i,e)=>{t.quit(e)},()=>{this.pool=[],r()})}}};var mt=require("bunyan");var W=()=>{},zt={debug:W,warn:W,info:W,error:W},I=null;function Pt(){I=null}function Ht(o){if(I)throw new P("Logger has been already initialized.");I=o}function Ut(o,r){var t;if(!o.enabled)return zt;if(I||(I=(0,mt.createLogger)(lt(D({},(t=o.options)!=null?t:{}),{name:"redis-smq"}))),r){let i=(e,n)=>(s,...l)=>{let a=typeof s=="string"?`${r} | ${s}`:s;return n[e](a,...l)};return{info:i("info",I),warn:i("warn",I),debug:i("debug",I),error:i("error",I)}}return I}var bt={getLogger:Ut,setLogger:Ht,destroy:Pt};var pt=require("redis");var K=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lrem(r,t,i),this}lpop(r){return this.multi.lpop(r),this}lpush(r,t){return this.multi.lpush(r,t),this}ltrim(r,t,i){return this.multi.ltrim(r,t,i),this}rpop(r){return this.multi.rpop(r),this}rpush(r,t){return this.multi.rpush(r,t),this}zadd(r,t,i){return this.multi.zadd(r,t,i),this}zrem(r,t){return this.multi.zrem(r,t),this}sadd(r,t){return this.multi.sadd(r,t),this}srem(r,t){return this.multi.srem(r,t),this}hset(r,t,i){return this.multi.hset(r,t,String(i)),this}hdel(r,t){return this.multi.hdel(r,t),this}hincrby(r,t,i){return this.multi.hincrby(r,t,i),this}pexpire(r,t){return this.multi.pexpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rpoplpush(r,t),this}del(r){return this.multi.del(r),this}exec(r){this.multi.exec((t,i)=>{t?r(t):i?r(null,i):r(new C)})}};var Ft=function(o){return o?this.flush_and_error({message:"Connection forcefully ended and command aborted.",code:"NR_CLOSED"}):arguments.length===0&&this.warn(`Using .end() without the flush parameter is deprecated and throws from v.3.0.0 on.
|
|
3
|
-
Please check the doku (https://github.com/NodeRedis/node_redis) and explictly use flush.`),this.retry_timer&&(clearTimeout(this.retry_timer),this.retry_timer=null),this.stream.removeAllListeners(),this.stream.once("close",()=>{this.emit("end"),this.emitted_end=!0}),this.stream.on("error",()=>{}),this.connected=!1,this.ready=!1,this.closing=!0,this.stream.destroySoon()},Q=class extends p{constructor(t={}){super();this.client=(0,pt.createClient)(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")}),this.client.end=Ft}set(t,i,e,n){e.exists&&e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,e.exists,n):e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,n):e.exists?this.client.set(t,i,e.exists,n):this.client.set(t,i,n)}zadd(t,i,e,n){this.client.zadd(t,i,e,n)}multi(){return new K(this.client)}watch(t,i){this.client.watch(t,i)}unwatch(t){this.client.unwatch(t)}sismember(t,i,e){this.client.sismember(t,i,e)}sscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.sscan(...s,(l,[a,c])=>{l?n(l):n(null,{cursor:a,items:c})})}zscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.zscan(...s,(l,a)=>{if(l)n(l);else if(!a)n(new b);else{let c=new Set,[d,m]=a;for(;m.length;){let S=String(m.shift()),It=String(m.shift());c.add(S)}n(null,{cursor:d,items:[...c]})}})}zcard(t,i){this.client.zcard(t,i)}zrange(t,i,e,n){this.client.zrange(t,i,e,n)}zrevrange(t,i,e,n){this.client.zrevrange(t,i,e,n)}psubscribe(t){this.client.psubscribe(t)}punsubscribe(t){this.client.punsubscribe(t)}subscribe(t){this.client.subscribe(t)}unsubscribe(t){this.client.unsubscribe(t)}zrangebyscore(t,i,e,n,s,l){this.client.zrangebyscore(t,i,e,"LIMIT",n,s,l)}zrem(t,i,e){this.client.zrem(t,i,e)}smembers(t,i){this.client.smembers(t,i)}sadd(t,i,e){this.client.sadd(t,i,e)}srem(t,i,e){this.client.srem(t,i,e)}hgetall(t,i){this.client.hgetall(t,(e,n)=>{e?i(e):i(null,n!=null?n:{})})}hscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.hscan(...s,(l,[a,c])=>{if(l)n(l);else{let d={};for(;c.length;){let m=String(c.shift());d[m]=String(c.shift())}n(null,{cursor:a,result:d})}})}hget(t,i,e){this.client.hget(t,i,e)}hset(t,i,e,n){this.client.hset(t,i,String(e),n)}hdel(t,i,e){this.client.hdel(t,...typeof i=="string"?[i]:i,e)}lrange(t,i,e,n){this.client.lrange(t,i,e,n)}hkeys(t,i){this.client.hkeys(t,i)}hlen(t,i){this.client.hlen(t,i)}brpoplpush(t,i,e,n){this.client.brpoplpush(t,i,e,n)}rpoplpush(t,i,e){this.client.rpoplpush(t,i,e)}zrangebyscorewithscores(t,i,e,n){this.client.zrangebyscore(t,i,e,"WITHSCORES",(s,l)=>{if(s)n(s);else{let a=l!=null?l:[],c={};for(let d=a.splice(0,2);d.length>0;d=a.splice(0,2)){let[m,S]=d;c[S]=m}n(null,c)}})}rpop(t,i){this.client.rpop(t,i)}lrem(t,i,e,n){this.client.lrem(t,i,e,n)}publish(t,i,e){this.client.publish(t,i,e)}flushall(t){this.client.flushall(t)}loadScript(t,i){this.client.script("load",t,i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.evalsha(n.concat(s),(l,a)=>e(l,a))}get(t,i){this.client.get(t,i)}del(t,i){this.client.del(...typeof t=="string"?[t]:t,i)}llen(t,i){this.client.llen(t,i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lmove(t,i,e,n,s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.zremrangebyscore(t,i,e,n)}hmget(t,i,e){this.client.hmget(t,i,e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end(!0))}end(t){this.connectionClosed||this.client.end(t)}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info(t)}on(t,i){return this.client.on(t,i),this}};var vt=require("@redis/client");var kt=require("@redis/client");var B=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lRem(r,t,i),this}lpop(r){return this.multi.lPop(r),this}lpush(r,t){return this.multi.lPush(r,t),this}ltrim(r,t,i){return this.multi.lTrim(r,t,i),this}rpop(r){return this.multi.rPop(r),this}rpush(r,t){return this.multi.rPush(r,t),this}zadd(r,t,i){return this.multi.zAdd(r,{score:t,value:i}),this}zrem(r,t){return this.multi.zRem(r,t),this}sadd(r,t){return this.multi.sAdd(r,t),this}srem(r,t){return this.multi.sRem(r,t),this}hset(r,t,i){return this.multi.hSet(r,t,i),this}hdel(r,t){return this.multi.hDel(r,t),this}hincrby(r,t,i){return this.multi.hIncrBy(r,t,i),this}pexpire(r,t){return this.multi.pExpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rPopLPush(r,t),this}del(r){return this.multi.del(r),this}exec(r){this.multi.exec().then(t=>r(null,t)).catch(t=>{t instanceof kt.WatchError?r(new C):r(t)})}};var $=class extends p{constructor(t={}){super();this.client=(0,vt.createClient)(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")}),this.client.connect()}set(t,i,e,n){this.client.set(t,i,D(D({},e.expire?{[e.expire.mode]:e.expire.value}:{}),e.exists?{[e.exists]:!0}:{})).then(s=>n(null,s)).catch(n)}zadd(t,i,e,n){this.client.zAdd(t,{score:i,value:e}).then(s=>n(null,s)).catch(n)}multi(){return new B(this.client)}watch(t,i){this.client.watch(t).then(e=>i(null,e)).catch(i)}unwatch(t){this.client.unwatch().then(i=>t(null,i)).catch(t)}sismember(t,i,e){this.client.sIsMember(t,i).then(n=>e(null,Number(n))).catch(e)}zcard(t,i){this.client.zCard(t).then(e=>i(null,e)).catch(i)}zrange(t,i,e,n){this.client.zRange(t,i,e).then(s=>n(null,(Array.isArray(s)?s:[]).map(l=>String(l)))).catch(n)}zrevrange(t,i,e,n){this.client.sendCommand(["ZREVRANGE",t,String(i),String(e)]).then(s=>n(null,(Array.isArray(s)?s:[]).map(l=>String(l)))).catch(n)}zrem(t,i,e){this.client.zRem(t,i).then(n=>e(null,n)).catch(e)}psubscribe(t){this.client.pSubscribe(t,(i,e)=>{this.client.emit("pmessage",t,e,i)})}punsubscribe(t){this.client.pUnsubscribe(t).catch(()=>{})}subscribe(t){this.client.subscribe(t,(i,e)=>{this.client.emit("message",e,i)})}unsubscribe(t){this.client.unsubscribe(t).catch(()=>{})}zrangebyscore(t,i,e,n,s,l){this.client.zRangeByScore(t,i,e,{LIMIT:{offset:n,count:s}}).then(a=>l(null,a)).catch(l)}smembers(t,i){this.client.sMembers(t).then(e=>i(null,e)).catch(i)}sscan(t,i,e,n){let s=[t,Number(i),e];this.client.sScan(...s).then(({cursor:l,members:a})=>{n(null,{cursor:String(l),items:a})}).catch(n)}zscan(t,i,e,n){let s=[t,Number(i),e];this.client.zScan(...s).then(({cursor:l,members:a})=>{let c=new Set;for(let d of a)c.add(d.value);n(null,{cursor:String(l),items:[...c]})}).catch(n)}sadd(t,i,e){this.client.sAdd(t,i).then(n=>e(null,n)).catch(e)}srem(t,i,e){this.client.sRem(t,i).then(n=>e(null,n)).catch(e)}hgetall(t,i){this.client.hGetAll(t).then(e=>i(null,e)).catch(i)}hscan(t,i,e,n){let s=[t,Number(i),e];this.client.hScan(...s).then(({cursor:l,tuples:a})=>{let c={};for(;a.length;){let d=a.shift();d&&(c[d.field]=d.value)}n(null,{cursor:String(l),result:c})}).catch(n)}hget(t,i,e){this.client.hGet(t,i).then(n=>e(null,n!=null?n:null)).catch(e)}hset(t,i,e,n){this.client.hSet(t,i,e).then(s=>n(null,s)).catch(n)}hdel(t,i,e){this.client.hDel(t,i).then(n=>e(null,Number(n))).catch(e)}lrange(t,i,e,n){this.client.lRange(t,i,e).then(s=>n(null,s)).catch(n)}hkeys(t,i){this.client.hKeys(t).then(e=>i(null,e)).catch(i)}hlen(t,i){this.client.hLen(t).then(e=>i(null,e)).catch(i)}brpoplpush(t,i,e,n){this.client.brPopLPush(t,i,e).then(s=>n(null,s)).catch(n)}rpoplpush(t,i,e){this.client.rPopLPush(t,i).then(n=>e(null,n)).catch(e)}zrangebyscorewithscores(t,i,e,n){this.client.zRangeByScoreWithScores(t,i,e).then(s=>{let l={};for(let{score:a,value:c}of s)l[a]=c;n(null,l)}).catch(n)}rpop(t,i){this.client.rPop(t).then(e=>i(null,e)).catch(i)}lrem(t,i,e,n){this.client.lRem(t,i,e).then(s=>n(null,s)).catch(n)}publish(t,i,e){this.client.publish(t,i).then(n=>e(null,n)).catch(e)}flushall(t){this.client.flushAll().then(i=>t(null,i)).catch(t)}loadScript(t,i){this.client.sendCommand(["SCRIPT","LOAD",t]).then(e=>i(null,typeof e=="string"?e:String(e))).catch(i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.sendCommand(["evalsha",...n.concat(s).map(l=>String(l))]).then(l=>{Array.isArray(l)?e(null,l.map(a=>a instanceof Buffer?a.toString():a)):l instanceof Buffer?e(null,l.toString()):e(null,l)}).catch(e)}get(t,i){this.client.sendCommand(["get",t]).then(e=>i(null,typeof e=="string"?e:null)).catch(i)}del(t,i){this.client.del(t).then(e=>i(null,e)).catch(i)}llen(t,i){this.client.sendCommand(["llen",t]).then(e=>i(null,Number(e))).catch(i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lMove(t,i,e,n).then(l=>s(null,l)).catch(s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.sendCommand(["zremrangebyscore",t,`${i}`,`${e}`]).then(s=>n(null,Number(s))).catch(n)}hmget(t,i,e){this.client.hmGet(t,i).then(n=>e(null,n)).catch(e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect().catch(()=>{})}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info().then(i=>t(null,i)).catch(t)}on(t,i){return this.client.on(t,i),this}};var Ct=j(require("ioredis"),1);var J=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lrem(r,t,i),this}lpop(r){return this.multi.lpop(r),this}lpush(r,t){return this.multi.lpush(r,t),this}ltrim(r,t,i){return this.multi.ltrim(r,t,i),this}rpop(r){return this.multi.rpop(r),this}rpush(r,t){return this.multi.rpush(r,t),this}zadd(r,t,i){return this.multi.zadd(r,t,i),this}zrem(r,t){return this.multi.zrem(r,...typeof t=="string"?[t]:t),this}sadd(r,t){return this.multi.sadd(r,t),this}srem(r,t){return this.multi.srem(r,...typeof t=="string"?[t]:t),this}hset(r,t,i){return this.multi.hset(r,t,i),this}hdel(r,t){return this.multi.hdel(r,...typeof t=="string"?[t]:t),this}hincrby(r,t,i){return this.multi.hincrby(r,t,i),this}pexpire(r,t){return this.multi.pexpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rpoplpush(r,t),this}del(r){return this.multi.del(...typeof r=="string"?[r]:r),this}exec(r){this.multi.exec((t,i)=>{if(t)r(t);else if(!i)r(new C);else{let e=[],n=null;for(let s of i){if(!Array.isArray(s)){n=new u("Expected an array reply from multi.exec()");break}let[l,a]=s;if(l instanceof Error){n=l;break}e.push(a)}n?r(n):r(null,e)}})}};var Z=class extends p{constructor(t={}){super();this.client=new Ct.default(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")})}set(t,i,e,n){e.exists&&e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,e.exists,n):e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,n):e.exists?this.client.set(t,i,e.exists,n):this.client.set(t,i,n)}zadd(t,i,e,n){this.client.zadd(t,i,e,n)}multi(){return new J(this.client)}watch(t,i){this.client.watch(t,i)}unwatch(t){this.client.unwatch(t)}sismember(t,i,e){this.client.sismember(t,i,e)}sscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.sscan(...s,(l,[a,c])=>{l?n(l):n(null,{cursor:a,items:c})})}zcard(t,i){this.client.zcard(t,i)}zrange(t,i,e,n){this.client.zrange(t,i,e,n)}zscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.zscan(...s,(l,a)=>{if(l)n(l);else if(!a)n(new b);else{let c=new Set,[d,m]=a;for(;m.length;){let S=String(m.shift()),It=String(m.shift());c.add(S)}n(null,{cursor:d,items:[...c]})}})}zrevrange(t,i,e,n){this.client.zrevrange(t,i,e,n)}zrem(t,i,e){this.client.zrem(t,i,e)}psubscribe(t){this.client.psubscribe(t)}punsubscribe(t){this.client.punsubscribe(t)}subscribe(t){this.client.subscribe(t)}unsubscribe(t){this.client.unsubscribe(t)}zrangebyscore(t,i,e,n,s,l){this.client.zrangebyscore(t,i,e,"LIMIT",n,s,l)}smembers(t,i){this.client.smembers(t,i)}sadd(t,i,e){this.client.sadd(t,i,e)}srem(t,i,e){this.client.srem(t,i,e)}hgetall(t,i){this.client.hgetall(t,i)}hscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.hscan(...s,(l,[a,c])=>{if(l)n(l);else{let d={};for(;c.length;){let m=String(c.shift());d[m]=String(c.shift())}n(null,{cursor:a,result:d})}})}hget(t,i,e){this.client.hget(t,i,e)}hset(t,i,e,n){this.client.hset(t,i,e,n)}hdel(t,i,e){this.client.hdel(t,i,e)}lrange(t,i,e,n){this.client.lrange(t,i,e,n)}hkeys(t,i){this.client.hkeys(t,i)}hlen(t,i){this.client.hlen(t,i)}brpoplpush(t,i,e,n){this.client.brpoplpush(t,i,e,n)}rpoplpush(t,i,e){this.client.rpoplpush(t,i,e)}zrangebyscorewithscores(t,i,e,n){this.client.zrangebyscore(t,i,e,"WITHSCORES",(s,l)=>{if(s)n(s);else{let a=l!=null?l:[],c={};for(let d=a.splice(0,2);d.length>0;d=a.splice(0,2)){let[m,S]=d;c[S]=m}n(null,c)}})}rpop(t,i){this.client.rpop(t,i)}lrem(t,i,e,n){this.client.lrem(t,i,e,n)}publish(t,i,e){this.client.publish(t,i,e)}flushall(t){this.client.flushall(t)}loadScript(t,i){this.client.script("load",t,i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.evalsha(n.concat(s),e)}get(t,i){this.client.get(t,i)}del(t,i){this.client.del(t,i)}llen(t,i){this.client.llen(t,i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lmove(t,i,e,n,s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.zremrangebyscore(t,i,e,n)}hmget(t,i,e){this.client.hmget(t,i,e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect(!1)}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info(t)}on(t,i){return this.client.on(t,i),this}};function Vt(o){return o.client==="redis"?new Q(o.options):o.client==="redis_v4"?new $(o.options):new Z(o.options)}function Dt(o,r){let t=Vt(o);t.once("ready",()=>{k.waterfall([i=>t.validateRedisServerSupport(i),i=>t.loadScripts(i)],i=>{if(i)throw i;r(null,t)})})}var ft={createInstance:Dt};0&&(module.exports={CallbackEmptyReplyError,CallbackInvalidReplyError,ERedisConfigClient,EventEmitter,Lock,LockAbortError,LockAcquireError,LockError,LockExtendError,LockMethodNotAllowedError,LockNotAcquiredError,LockNotReleasedError,LoggerError,PanicError,PowerSwitch,PowerSwitchError,RedisClient,RedisClientError,RedisSMQError,Ticker,TickerError,WatchedKeysChangedError,Worker,WorkerError,WorkerPool,WorkerRunner,async,logger,redis});
|
|
1
|
+
"use strict";var vt=Object.create;var F=Object.defineProperty,ft=Object.defineProperties,Ct=Object.getOwnPropertyDescriptor,It=Object.getOwnPropertyDescriptors,xt=Object.getOwnPropertyNames,et=Object.getOwnPropertySymbols,wt=Object.getPrototypeOf,nt=Object.prototype.hasOwnProperty,Tt=Object.prototype.propertyIsEnumerable;var rt=(o,r,t)=>r in o?F(o,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[r]=t,D=(o,r)=>{for(var t in r||(r={}))nt.call(r,t)&&rt(o,t,r[t]);if(et)for(var t of et(r))Tt.call(r,t)&&rt(o,t,r[t]);return o},st=(o,r)=>ft(o,It(r));var Et=(o,r)=>{for(var t in r)F(o,t,{get:r[t],enumerable:!0})},ot=(o,r,t,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of xt(r))!nt.call(o,e)&&e!==t&&F(o,e,{get:()=>r[e],enumerable:!(i=Ct(r,e))||i.enumerable});return o};var J=(o,r,t)=>(t=o!=null?vt(wt(o)):{},ot(r||!o||!o.__esModule?F(t,"default",{value:o,enumerable:!0}):t,o)),yt=o=>ot(F({},"__esModule",{value:!0}),o);var Ut={};Et(Ut,{CallbackEmptyReplyError:()=>m,CallbackInvalidReplyError:()=>q,ERedisConfigClient:()=>Z,EventEmitter:()=>N,Lock:()=>H,LockAbortError:()=>C,LockAcquireError:()=>w,LockError:()=>h,LockExtendError:()=>L,LockMethodNotAllowedError:()=>M,LockNotAcquiredError:()=>O,LockNotReleasedError:()=>z,LoggerError:()=>P,PanicError:()=>x,PowerSwitch:()=>v,PowerSwitchError:()=>I,RedisClient:()=>k,RedisClientError:()=>u,RedisSMQError:()=>d,Ticker:()=>y,TickerError:()=>S,WatchedKeysChangedError:()=>T,Worker:()=>X,WorkerError:()=>E,WorkerPool:()=>V,WorkerRunner:()=>G,async:()=>b,logger:()=>ht,redis:()=>kt});module.exports=yt(Ut);var Z=(t=>(t.IOREDIS="ioredis",t.REDIS="redis",t))(Z||{});var d=class extends Error{constructor(r){super(r)}get name(){return this.constructor.name}};var m=class extends d{constructor(){super("Expected a non-empty reply")}};var q=class extends d{constructor(){super("Invalid reply type")}};var x=class extends d{};var P=class extends d{};var S=class extends d{};var h=class extends d{};var C=class extends h{constructor(r="releaseLock() may have been called. Abandoning."){super(r)}};var w=class extends h{constructor(r="Could not acquire a lock"){super(r)}};var L=class extends h{constructor(r="Acquired lock could not be extended"){super(r)}};var M=class extends h{constructor(r="This method can not be used when autoExtend is enabled"){super(r)}};var O=class extends h{constructor(r="Can not extend a lock which has not been yet acquired. Maybe a pending operation is in progress."){super(r)}};var z=class extends h{constructor(r="A lock has been already obtained but not yet released or maybe a pending operation is in progress."){super(r)}};var u=class extends d{};var T=class extends u{constructor(r="One (or more) of the watched keys has been changed"){super(r)}};var E=class extends x{};var I=class extends d{};var dt=require("uuid");var tt=J(require("fs"),1),it=require("path");var ct=require("events");var lt=(o,r,t)=>{if(o.length){let i=0,e=()=>{r(o[i],i,n=>{i+=1,n||i>=o.length?t(n):setTimeout(()=>e(),0)})};e()}else t()},at=(o,r,t)=>{let i=Object.keys(o);if(i.length){let e=0,n=()=>{let s=i[e];r(o[s],s,l=>{e+=1,l||e>=i.length?t(l):setTimeout(()=>n(),0)})};n()}else t()},Rt=(o,r,t)=>{Array.isArray(o)?lt(o,r,t):at(o,r,t)},St=(o,r)=>{if(o.length){let t=0,i=(e,...n)=>{t+=1,e?r(e):t<o.length?setTimeout(()=>{n.length?o[t](...n,i):o[t](i)},0):n.length?r(null,n[0]):r()};o[t](i)}else r()},b={each:Rt,eachIn:at,eachOf:lt,waterfall:St};var j=J(require("fs"),1),Y=require("path");var A=class A{constructor(){this.isLoaded=!1;this.scripts=new Map,this.addScript("ZPOPRPUSH",j.default.readFileSync((0,Y.resolve)(__dirname,"./lua/zpoprpush.lua")).toString()),this.addScript("LPOPRPUSH",j.default.readFileSync((0,Y.resolve)(__dirname,"./lua/lpoprpush.lua")).toString())}hasScript(r){return this.scripts.has(r)}addScript(r,t){return this.hasScript(r)?!1:(this.scripts.set(r,{content:t}),this.isLoaded=!1,!0)}loadScripts(r,t){if(this.isLoaded)t();else{let i=[];this.scripts.forEach(e=>i.push(n=>{e.id?n():r.loadScript(e.content,(s,l)=>{s?n(s):l?(e.id=l,n()):n(new m)})})),b.waterfall(i,e=>{e?t(e):(this.isLoaded=!0,t())})}}getScriptId(r){var i;let{id:t}=(i=this.scripts.get(r))!=null?i:{};if(!t)throw new u(`ID of script [${r}] is missing`);return t}static getInstance(){return A.instance||(A.instance=new A),A.instance}};A.instance=null;var U=A;var Lt=[4,0,0],p=class p extends ct.EventEmitter{constructor(){super(...arguments);this.connectionClosed=!0}validateRedisVersion(t,i=0,e=0){if(!p.redisServerVersion)throw new u("UNKNOWN_REDIS_SERVER_VERSION");return p.redisServerVersion[0]>t||p.redisServerVersion[0]===t&&p.redisServerVersion[1]>=i&&p.redisServerVersion[2]>=e}validateRedisServerSupport(t){let i=e=>{let[n,s,l]=Lt;this.validateRedisVersion(n,s,l)?e():e(new u("UNSUPPORTED_REDIS_SERVER_VERSION"))};p.redisServerVersion?i(t):this.updateServerVersion(e=>{e?t(e):i(t)})}sscanAll(t,i,e){let n=new Set,s=l=>{this.sscan(t,l,i,(a,c)=>{a?e(a):c?(c.items.forEach(g=>n.add(g)),c.cursor==="0"?e(null,[...n]):s(c.cursor)):e(new m)})};s("0")}hscanAll(t,i,e){let n={},s=l=>{this.hscan(t,l,i,(a,c)=>{a?e(a):c?(Object.assign(n,c.result),c.cursor==="0"?e(null,n):s(c.cursor)):e(new m)})};s("0")}zpoprpush(t,i,e){this.runScript("ZPOPRPUSH",[t,i],[],(n,s)=>{n?e(n):e(null,typeof s=="string"?s:null)})}lpoprpush(t,i,e){this.validateRedisVersion(6,2)?this.lmove(t,i,"LEFT","RIGHT",e):this.runScript("LPOPRPUSH",[t,i],[],(n,s)=>{n?e(n):e(null,typeof s=="string"?s:null)})}updateServerVersion(t){p.redisServerVersion?t():this.getInfo((i,e)=>{i?t(i):e?(p.redisServerVersion=e.split(`\r
|
|
2
|
+
`)[1].split(":")[1].split(".").map(n=>Number(n)),t()):t(new m)})}loadScripts(t){U.getInstance().loadScripts(this,t)}runScript(t,i,e,n){let s=U.getInstance().getScriptId(t);this.evalsha(s,[i.length,...i,...e],(l,a)=>{l?n(l):n(null,a)})}static addScript(t,i){if(!U.getInstance().addScript(t,i))throw new u(`A script with name [${t}] already exists`)}};p.redisServerVersion=null;var k=p;k.addScript("RELEASE_LOCK",tt.default.readFileSync((0,it.resolve)(__dirname,"./lua/release-lock.lua")).toString());k.addScript("EXTEND_LOCK",tt.default.readFileSync((0,it.resolve)(__dirname,"./lua/extend-lock.lua")).toString());var H=class{constructor(r,t,i,e=!1,n=!1,s=!0){this.status=0;this.lockingTimer=null;this.autoExtendTimer=null;this.throwExceptions=!0;this.lockKey=t,this.ttl=i,this.retryOnFail=e,this.lockId=(0,dt.v4)(),this.redisClient=r,this.autoExtend=n,this.throwExceptions=s}resetTimers(){this.lockingTimer&&(clearTimeout(this.lockingTimer),this.lockingTimer=null),this.autoExtendTimer&&(clearTimeout(this.autoExtendTimer),this.autoExtendTimer=null)}setUnlocked(){this.status=0}setLocked(){this.status=2}setExtended(){this.status=5}extend(r){this.isLocked()?(this.status=4,this.redisClient.runScript("EXTEND_LOCK",[this.lockKey],[this.lockId,this.ttl],(t,i)=>{t?r(t):this.status===4?i?(this.setExtended(),r()):(this.setUnlocked(),r(new L)):r(new C)})):r(new O)}runAutoExtendTimer(){let r=Math.ceil(this.ttl/2);this.autoExtendTimer=setTimeout(()=>this.extend(t=>{if(!t)this.runAutoExtendTimer();else if(this.throwExceptions&&!(t instanceof C))throw t}),r)}acquireLock(r){if(!this.isReleased())r(new z);else{this.status=1;let t=()=>{this.status===1?this.redisClient.set(this.lockKey,this.lockId,{expire:{mode:"PX",value:this.ttl},exists:"NX"},(i,e)=>{i?r(i):this.status===1?e?(this.setLocked(),this.autoExtend&&this.runAutoExtendTimer(),r()):this.retryOnFail?this.lockingTimer=setTimeout(t,1e3):(this.setUnlocked(),r(new w)):r(new C)}):r(new C)};t()}}extendLock(r){this.autoExtend?r(new M):this.extend(r)}releaseLock(r){this.status===0?r():this.isLocked()?(this.resetTimers(),this.status=3,this.redisClient.runScript("RELEASE_LOCK",[this.lockKey],[this.lockId],i=>{i?r(i):(this.setUnlocked(),r())})):r(new O)}acquireOrExtend(r){if(this.autoExtend)r(new M);else{let t=()=>{this.acquireLock(i=>{i?r(i):r(null,2)})};this.isLocked()?this.extend(i=>{i?i instanceof L?t():r(i):r(null,5)}):t()}}isLocked(){return this.status===2||this.status===5}isReleased(){return this.status===0}getId(){return this.lockId}};var v=class{constructor(r=!0){this.isPowered=!1;this.pendingState=null;this.throwExceptionOnError=r}switch(r){if(this.pendingState!==null){if(this.throwExceptionOnError)throw new I("Can not switch state while another state transition is in progress.");return!1}if(r===this.isPowered){if(this.throwExceptionOnError)throw new I("Can not switch to the same current state.");return!1}return this.pendingState=r,!0}isUp(){return this.isPowered}isDown(){return!this.isPowered}isGoingUp(){return this.pendingState===!0}isGoingDown(){return this.pendingState===!1}isRunning(){return this.isUp()&&this.pendingState===null}goingUp(){return this.switch(!0)}goingDown(){return this.switch(!1)}commit(){if(this.pendingState===null)throw new I("Expected a pending state");this.isPowered=this.pendingState,this.pendingState=null}rollback(){if(this.pendingState===null)throw new I("Expected a pending state");this.pendingState=null}};var ut=require("events"),N=class extends ut.EventEmitter{};var y=class extends N{constructor(t=()=>{},i=1e3){super();this.powerManager=new v;this.onNextTickFn=null;this.timeout=null;this.interval=null;this.shutdownTimeout=null;this.aborted=!1;this.onTickFn=t,this.time=i,this.powerManager.goingUp()}shutdown(){this.shutdownTimeout&&clearTimeout(this.shutdownTimeout),this.powerManager.commit(),this.emit("down")}onTick(){var t;if(this.powerManager.isGoingDown())this.shutdown();else if(this.powerManager.isRunning()){let i=(t=this.onNextTickFn)!=null?t:this.onTickFn;this.onNextTickFn=null,i()}else this.emit("error",new x("Unexpected call"))}abort(){this.aborted||(this.aborted=!0,this.powerManager.isGoingDown()?this.shutdown():this.quit())}quit(){this.powerManager.isGoingUp()?(this.powerManager.rollback(),this.emit("down")):this.aborted&&this.powerManager.isDown()?this.emit("down"):(this.powerManager.goingDown(),this.timeout?(clearTimeout(this.timeout),this.shutdown()):this.interval?(clearInterval(this.interval),this.shutdown()):this.aborted?this.shutdown():this.shutdownTimeout=setTimeout(()=>{this.powerManager.isGoingDown()&&this.shutdown()},6e4))}isTicking(){return!!(this.timeout||this.interval)}nextTick(){if(this.isTicking())throw new S("A timer is already running");this.powerManager.isGoingDown()?this.shutdown():(this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.timeout=setTimeout(()=>{this.timeout=null,this.onTick()},this.time)))}nextTickFn(t){this.onNextTickFn=t,this.nextTick()}runTimer(){if(this.isTicking())throw new S("A timer is already running");this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.interval=setInterval(()=>this.onTick(),this.time))}};var X=class{constructor(r,t=1e3){this.ticker=null;this.powerManager=null;this.getTicker=()=>{if(!this.ticker)throw new E("Expected an instance of Ticker");return this.ticker};this.onTick=()=>{this.work(r=>{if(r)throw r;this.getTicker().nextTick()})};this.run=()=>{if(this.managed)throw new E("You can not run a managed worker");let r=this.getPowerManager();r.goingUp(),this.getTicker().nextTick(),r.commit()};this.quit=r=>{if(this.managed)r();else{let t=this.getPowerManager();t.goingDown();let i=this.getTicker();i.on("down",()=>{t.commit(),r()}),i.quit()}};this.managed=r,r||(this.ticker=new y(this.onTick,t),this.powerManager=new v)}getPowerManager(){if(!this.powerManager)throw new E("Expected an instance of PowerSwitch");return this.powerManager}};var G=class extends N{constructor(t,i,e,n){super();this.onTick=()=>{b.waterfall([t=>{this.lock.acquireOrExtend((i,e)=>{e===2&&this.logger.info(`Workers are exclusively running from this instance (Lock ID ${this.lock.getId()}).`),t(i)})},t=>{this.workerPool.work(t)}],t=>{!t||t instanceof w?this.ticker.nextTick():this.emit("error",t)})};this.clearWorkerPool=t=>{this.workerPool.clear(t)};this.stopTicker=t=>{this.ticker.once("down",t),this.ticker.quit()};this.releaseLock=t=>{this.lock.releaseLock(t)};this.run=()=>{this.emit("up"),this.ticker.nextTick()};this.quit=t=>{b.waterfall([this.stopTicker,this.clearWorkerPool,this.releaseLock],()=>{this.emit("down"),t()})};this.powerManager=new v,this.redisClient=t,this.logger=n,this.lock=new H(t,i,6e4),this.ticker=new y(this.onTick),this.workerPool=e}addWorker(t){this.workerPool.add(t)}};var V=class{constructor(){this.pool=[];this.work=r=>{if(this.pool.length){let t=this.pool.map(i=>e=>i.work(e));b.waterfall(t,r)}else r()};this.add=r=>(this.pool.push(r),this.pool.length);this.clear=r=>{b.each(this.pool,(t,i,e)=>{t.quit(e)},()=>{this.pool=[],r()})}}};var gt=require("bunyan");var W=()=>{},Mt={debug:W,warn:W,info:W,error:W},f=null;function Ot(){f=null}function At(o){if(f)throw new P("Logger has been already initialized.");f=o}function Nt(o,r){var t;if(!o.enabled)return Mt;if(f||(f=(0,gt.createLogger)(st(D({},(t=o.options)!=null?t:{}),{name:"redis-smq"}))),r){let i=(e,n)=>(s,...l)=>{let a=typeof s=="string"?`${r} | ${s}`:s;return n[e](a,...l)};return{info:i("info",f),warn:i("warn",f),debug:i("debug",f),error:i("error",f)}}return f}var ht={getLogger:Nt,setLogger:At,destroy:Ot};var bt=require("@redis/client");var mt=require("@redis/client");var K=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lRem(r,t,i),this}lpop(r){return this.multi.lPop(r),this}lpush(r,t){return this.multi.lPush(r,t),this}ltrim(r,t,i){return this.multi.lTrim(r,t,i),this}rpop(r){return this.multi.rPop(r),this}rpush(r,t){return this.multi.rPush(r,t),this}zadd(r,t,i){return this.multi.zAdd(r,{score:t,value:i}),this}zrem(r,t){return this.multi.zRem(r,t),this}sadd(r,t){return this.multi.sAdd(r,t),this}srem(r,t){return this.multi.sRem(r,t),this}hset(r,t,i){return this.multi.hSet(r,t,i),this}hdel(r,t){return this.multi.hDel(r,t),this}hincrby(r,t,i){return this.multi.hIncrBy(r,t,i),this}pexpire(r,t){return this.multi.pExpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rPopLPush(r,t),this}del(r){return this.multi.del(r),this}exec(r){this.multi.exec().then(t=>r(null,t)).catch(t=>{t instanceof mt.WatchError?r(new T):r(t)})}};var _=class extends k{constructor(t={}){super();this.client=(0,bt.createClient)(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")}),this.client.connect()}set(t,i,e,n){this.client.set(t,i,D(D({},e.expire?{[e.expire.mode]:e.expire.value}:{}),e.exists?{[e.exists]:!0}:{})).then(s=>n(null,s)).catch(n)}zadd(t,i,e,n){this.client.zAdd(t,{score:i,value:e}).then(s=>n(null,s)).catch(n)}multi(){return new K(this.client)}watch(t,i){this.client.watch(t).then(e=>i(null,e)).catch(i)}unwatch(t){this.client.unwatch().then(i=>t(null,i)).catch(t)}sismember(t,i,e){this.client.sIsMember(t,i).then(n=>e(null,Number(n))).catch(e)}zcard(t,i){this.client.zCard(t).then(e=>i(null,e)).catch(i)}zrange(t,i,e,n){this.client.zRange(t,i,e).then(s=>n(null,(Array.isArray(s)?s:[]).map(l=>String(l)))).catch(n)}zrevrange(t,i,e,n){this.client.sendCommand(["ZREVRANGE",t,String(i),String(e)]).then(s=>n(null,(Array.isArray(s)?s:[]).map(l=>String(l)))).catch(n)}zrem(t,i,e){this.client.zRem(t,i).then(n=>e(null,n)).catch(e)}psubscribe(t){this.client.pSubscribe(t,(i,e)=>{this.client.emit("pmessage",t,e,i)})}punsubscribe(t){this.client.pUnsubscribe(t).catch(()=>{})}subscribe(t){this.client.subscribe(t,(i,e)=>{this.client.emit("message",e,i)})}unsubscribe(t){this.client.unsubscribe(t).catch(()=>{})}zrangebyscore(t,i,e,n,s,l){this.client.zRangeByScore(t,i,e,{LIMIT:{offset:n,count:s}}).then(a=>l(null,a)).catch(l)}smembers(t,i){this.client.sMembers(t).then(e=>i(null,e)).catch(i)}sscan(t,i,e,n){let s=[t,Number(i),e];this.client.sScan(...s).then(({cursor:l,members:a})=>{n(null,{cursor:String(l),items:a})}).catch(n)}zscan(t,i,e,n){let s=[t,Number(i),e];this.client.zScan(...s).then(({cursor:l,members:a})=>{let c=new Set;for(let g of a)c.add(g.value);n(null,{cursor:String(l),items:[...c]})}).catch(n)}sadd(t,i,e){this.client.sAdd(t,i).then(n=>e(null,n)).catch(e)}srem(t,i,e){this.client.sRem(t,i).then(n=>e(null,n)).catch(e)}hgetall(t,i){this.client.hGetAll(t).then(e=>i(null,e)).catch(i)}hscan(t,i,e,n){let s=[t,Number(i),e];this.client.hScan(...s).then(({cursor:l,tuples:a})=>{let c={};for(;a.length;){let g=a.shift();g&&(c[g.field]=g.value)}n(null,{cursor:String(l),result:c})}).catch(n)}hget(t,i,e){this.client.hGet(t,i).then(n=>e(null,n!=null?n:null)).catch(e)}hset(t,i,e,n){this.client.hSet(t,i,e).then(s=>n(null,s)).catch(n)}hdel(t,i,e){this.client.hDel(t,i).then(n=>e(null,Number(n))).catch(e)}lrange(t,i,e,n){this.client.lRange(t,i,e).then(s=>n(null,s)).catch(n)}hkeys(t,i){this.client.hKeys(t).then(e=>i(null,e)).catch(i)}hlen(t,i){this.client.hLen(t).then(e=>i(null,e)).catch(i)}brpoplpush(t,i,e,n){this.client.brPopLPush(t,i,e).then(s=>n(null,s)).catch(n)}rpoplpush(t,i,e){this.client.rPopLPush(t,i).then(n=>e(null,n)).catch(e)}zrangebyscorewithscores(t,i,e,n){this.client.zRangeByScoreWithScores(t,i,e).then(s=>{let l={};for(let{score:a,value:c}of s)l[a]=c;n(null,l)}).catch(n)}rpop(t,i){this.client.rPop(t).then(e=>i(null,e)).catch(i)}lrem(t,i,e,n){this.client.lRem(t,i,e).then(s=>n(null,s)).catch(n)}publish(t,i,e){this.client.publish(t,i).then(n=>e(null,n)).catch(e)}flushall(t){this.client.flushAll().then(i=>t(null,i)).catch(t)}loadScript(t,i){this.client.sendCommand(["SCRIPT","LOAD",t]).then(e=>i(null,typeof e=="string"?e:String(e))).catch(i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.sendCommand(["evalsha",...n.concat(s).map(l=>String(l))]).then(l=>{Array.isArray(l)?e(null,l.map(a=>a instanceof Buffer?a.toString():a)):l instanceof Buffer?e(null,l.toString()):e(null,l)}).catch(e)}get(t,i){this.client.sendCommand(["get",t]).then(e=>i(null,typeof e=="string"?e:null)).catch(i)}del(t,i){this.client.del(t).then(e=>i(null,e)).catch(i)}llen(t,i){this.client.sendCommand(["llen",t]).then(e=>i(null,Number(e))).catch(i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lMove(t,i,e,n).then(l=>s(null,l)).catch(s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.sendCommand(["zremrangebyscore",t,`${i}`,`${e}`]).then(s=>n(null,Number(s))).catch(n)}hmget(t,i,e){this.client.hmGet(t,i).then(n=>e(null,n)).catch(e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect().catch(()=>{})}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info().then(i=>t(null,i)).catch(t)}on(t,i){return this.client.on(t,i),this}};var pt=J(require("ioredis"),1);var Q=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lrem(r,t,i),this}lpop(r){return this.multi.lpop(r),this}lpush(r,t){return this.multi.lpush(r,t),this}ltrim(r,t,i){return this.multi.ltrim(r,t,i),this}rpop(r){return this.multi.rpop(r),this}rpush(r,t){return this.multi.rpush(r,t),this}zadd(r,t,i){return this.multi.zadd(r,t,i),this}zrem(r,t){return this.multi.zrem(r,...typeof t=="string"?[t]:t),this}sadd(r,t){return this.multi.sadd(r,t),this}srem(r,t){return this.multi.srem(r,...typeof t=="string"?[t]:t),this}hset(r,t,i){return this.multi.hset(r,t,i),this}hdel(r,t){return this.multi.hdel(r,...typeof t=="string"?[t]:t),this}hincrby(r,t,i){return this.multi.hincrby(r,t,i),this}pexpire(r,t){return this.multi.pexpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rpoplpush(r,t),this}del(r){return this.multi.del(...typeof r=="string"?[r]:r),this}exec(r){this.multi.exec((t,i)=>{if(t)r(t);else if(!i)r(new T);else{let e=[],n=null;for(let s of i){if(!Array.isArray(s)){n=new u("Expected an array reply from multi.exec()");break}let[l,a]=s;if(l instanceof Error){n=l;break}e.push(a)}n?r(n):r(null,e)}})}};var B=class extends k{constructor(t={}){super();this.client=new pt.default(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")})}set(t,i,e,n){e.exists&&e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,e.exists,n):e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,n):e.exists?this.client.set(t,i,e.exists,n):this.client.set(t,i,n)}zadd(t,i,e,n){this.client.zadd(t,i,e,n)}multi(){return new Q(this.client)}watch(t,i){this.client.watch(t,i)}unwatch(t){this.client.unwatch(t)}sismember(t,i,e){this.client.sismember(t,i,e)}sscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.sscan(...s,(l,[a,c])=>{l?n(l):n(null,{cursor:a,items:c})})}zcard(t,i){this.client.zcard(t,i)}zrange(t,i,e,n){this.client.zrange(t,i,e,n)}zscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.zscan(...s,(l,a)=>{if(l)n(l);else if(!a)n(new m);else{let c=new Set,[g,R]=a;for(;R.length;){let $=String(R.shift()),Ht=String(R.shift());c.add($)}n(null,{cursor:g,items:[...c]})}})}zrevrange(t,i,e,n){this.client.zrevrange(t,i,e,n)}zrem(t,i,e){this.client.zrem(t,i,e)}psubscribe(t){this.client.psubscribe(t)}punsubscribe(t){this.client.punsubscribe(t)}subscribe(t){this.client.subscribe(t)}unsubscribe(t){this.client.unsubscribe(t)}zrangebyscore(t,i,e,n,s,l){this.client.zrangebyscore(t,i,e,"LIMIT",n,s,l)}smembers(t,i){this.client.smembers(t,i)}sadd(t,i,e){this.client.sadd(t,i,e)}srem(t,i,e){this.client.srem(t,i,e)}hgetall(t,i){this.client.hgetall(t,i)}hscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.hscan(...s,(l,[a,c])=>{if(l)n(l);else{let g={};for(;c.length;){let R=String(c.shift());g[R]=String(c.shift())}n(null,{cursor:a,result:g})}})}hget(t,i,e){this.client.hget(t,i,e)}hset(t,i,e,n){this.client.hset(t,i,e,n)}hdel(t,i,e){this.client.hdel(t,i,e)}lrange(t,i,e,n){this.client.lrange(t,i,e,n)}hkeys(t,i){this.client.hkeys(t,i)}hlen(t,i){this.client.hlen(t,i)}brpoplpush(t,i,e,n){this.client.brpoplpush(t,i,e,n)}rpoplpush(t,i,e){this.client.rpoplpush(t,i,e)}zrangebyscorewithscores(t,i,e,n){this.client.zrangebyscore(t,i,e,"WITHSCORES",(s,l)=>{if(s)n(s);else{let a=l!=null?l:[],c={};for(let g=a.splice(0,2);g.length>0;g=a.splice(0,2)){let[R,$]=g;c[$]=R}n(null,c)}})}rpop(t,i){this.client.rpop(t,i)}lrem(t,i,e,n){this.client.lrem(t,i,e,n)}publish(t,i,e){this.client.publish(t,i,e)}flushall(t){this.client.flushall(t)}loadScript(t,i){this.client.script("load",t,i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.evalsha(n.concat(s),e)}get(t,i){this.client.get(t,i)}del(t,i){this.client.del(t,i)}llen(t,i){this.client.llen(t,i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lmove(t,i,e,n,s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.zremrangebyscore(t,i,e,n)}hmget(t,i,e){this.client.hmget(t,i,e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect(!1)}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info(t)}on(t,i){return this.client.on(t,i),this}};function Pt(o){return o.client==="redis"?new _(o.options):new B(o.options)}function zt(o,r){let t=Pt(o);t.once("ready",()=>{b.waterfall([i=>t.validateRedisServerSupport(i),i=>t.loadScripts(i)],i=>{if(i)throw i;r(null,t)})})}var kt={createInstance:zt};0&&(module.exports={CallbackEmptyReplyError,CallbackInvalidReplyError,ERedisConfigClient,EventEmitter,Lock,LockAbortError,LockAcquireError,LockError,LockExtendError,LockMethodNotAllowedError,LockNotAcquiredError,LockNotReleasedError,LoggerError,PanicError,PowerSwitch,PowerSwitchError,RedisClient,RedisClientError,RedisSMQError,Ticker,TickerError,WatchedKeysChangedError,Worker,WorkerError,WorkerPool,WorkerRunner,async,logger,redis});
|
|
4
3
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.d.cts
CHANGED
|
@@ -3,7 +3,6 @@ import { RedisClientOptions, RedisModules, RedisFunctions, RedisScripts, RedisCl
|
|
|
3
3
|
import { EventEmitter as EventEmitter$1 } from 'events';
|
|
4
4
|
import * as Logger from 'bunyan';
|
|
5
5
|
import { RedisOptions } from 'ioredis';
|
|
6
|
-
import { ClientOpts } from 'redis';
|
|
7
6
|
|
|
8
7
|
interface ICallback<T> {
|
|
9
8
|
(err?: Error | null, reply?: T): void;
|
|
@@ -17,22 +16,17 @@ interface IRedisConfigIORedis {
|
|
|
17
16
|
options?: RedisOptions;
|
|
18
17
|
}
|
|
19
18
|
declare enum ERedisConfigClient {
|
|
20
|
-
REDIS = "redis",
|
|
21
19
|
IOREDIS = "ioredis",
|
|
22
|
-
|
|
20
|
+
REDIS = "redis"
|
|
23
21
|
}
|
|
24
22
|
interface IRedisConfigNodeRedis {
|
|
25
23
|
client: ERedisConfigClient.REDIS;
|
|
26
|
-
options?: ClientOpts;
|
|
27
|
-
}
|
|
28
|
-
interface IRedisConfigNodeRedisV4 {
|
|
29
|
-
client: ERedisConfigClient.REDIS_V4;
|
|
30
24
|
options?: RedisClientOptions;
|
|
31
25
|
}
|
|
32
|
-
type IRedisConfig = IRedisConfigIORedis | IRedisConfigNodeRedis
|
|
26
|
+
type IRedisConfig = IRedisConfigIORedis | IRedisConfigNodeRedis;
|
|
33
27
|
|
|
34
|
-
type
|
|
35
|
-
type
|
|
28
|
+
type TRedisTransactionNodeRedis = RedisClientMultiCommandType<RedisModules, RedisFunctions, RedisScripts>;
|
|
29
|
+
type TRedisClientNodeRedis = RedisClientType<RedisModules, RedisFunctions, RedisScripts>;
|
|
36
30
|
interface IRedisClient {
|
|
37
31
|
validateRedisVersion(major: number, feature?: number, minor?: number): boolean;
|
|
38
32
|
validateRedisServerSupport(cb: ICallback<void>): void;
|
|
@@ -146,15 +140,6 @@ declare module 'ioredis' {
|
|
|
146
140
|
lmove(source: string, destination: string, from: 'LEFT' | 'RIGHT', to: 'LEFT' | 'RIGHT', cb: ICallback<string>): void;
|
|
147
141
|
}
|
|
148
142
|
}
|
|
149
|
-
declare module 'redis' {
|
|
150
|
-
interface Commands<R> {
|
|
151
|
-
info(cb?: Callback<string>): R;
|
|
152
|
-
info(section?: string | string[], cb?: Callback<string>): R;
|
|
153
|
-
INFO(cb?: Callback<string>): R;
|
|
154
|
-
INFO(section?: string | string[], cb?: Callback<string>): R;
|
|
155
|
-
lmove(source: string, destination: string, from: 'LEFT' | 'RIGHT', to: 'LEFT' | 'RIGHT', cb: ICallback<string>): void;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
143
|
|
|
159
144
|
interface ILoggerConfig {
|
|
160
145
|
enabled: boolean;
|
|
@@ -472,4 +457,4 @@ declare const redis: {
|
|
|
472
457
|
createInstance: typeof createInstance;
|
|
473
458
|
};
|
|
474
459
|
|
|
475
|
-
export { CallbackEmptyReplyError, CallbackInvalidReplyError, ERedisConfigClient, EventEmitter, type ICallback, type ILogger, type ILoggerConfig, type IRedisClient, type IRedisConfig, type IRedisConfigIORedis, type IRedisConfigNodeRedis, type
|
|
460
|
+
export { CallbackEmptyReplyError, CallbackInvalidReplyError, ERedisConfigClient, EventEmitter, type ICallback, type ILogger, type ILoggerConfig, type IRedisClient, type IRedisConfig, type IRedisConfigIORedis, type IRedisConfigNodeRedis, type IRedisTransaction, Lock, LockAbortError, LockAcquireError, LockError, LockExtendError, LockMethodNotAllowedError, LockNotAcquiredError, LockNotReleasedError, LoggerError, PanicError, PowerSwitch, PowerSwitchError, RedisClient, RedisClientError, RedisSMQError, type TEvent, type TEventEmitterEvent, type TFunction, type TRedisClientNodeRedis, type TRedisTransactionNodeRedis, type TUnaryFunction, Ticker, TickerError, WatchedKeysChangedError, Worker, WorkerError, WorkerPool, WorkerRunner, async, logger, redis };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { RedisClientOptions, RedisModules, RedisFunctions, RedisScripts, RedisCl
|
|
|
3
3
|
import { EventEmitter as EventEmitter$1 } from 'events';
|
|
4
4
|
import * as Logger from 'bunyan';
|
|
5
5
|
import { RedisOptions } from 'ioredis';
|
|
6
|
-
import { ClientOpts } from 'redis';
|
|
7
6
|
|
|
8
7
|
interface ICallback<T> {
|
|
9
8
|
(err?: Error | null, reply?: T): void;
|
|
@@ -17,22 +16,17 @@ interface IRedisConfigIORedis {
|
|
|
17
16
|
options?: RedisOptions;
|
|
18
17
|
}
|
|
19
18
|
declare enum ERedisConfigClient {
|
|
20
|
-
REDIS = "redis",
|
|
21
19
|
IOREDIS = "ioredis",
|
|
22
|
-
|
|
20
|
+
REDIS = "redis"
|
|
23
21
|
}
|
|
24
22
|
interface IRedisConfigNodeRedis {
|
|
25
23
|
client: ERedisConfigClient.REDIS;
|
|
26
|
-
options?: ClientOpts;
|
|
27
|
-
}
|
|
28
|
-
interface IRedisConfigNodeRedisV4 {
|
|
29
|
-
client: ERedisConfigClient.REDIS_V4;
|
|
30
24
|
options?: RedisClientOptions;
|
|
31
25
|
}
|
|
32
|
-
type IRedisConfig = IRedisConfigIORedis | IRedisConfigNodeRedis
|
|
26
|
+
type IRedisConfig = IRedisConfigIORedis | IRedisConfigNodeRedis;
|
|
33
27
|
|
|
34
|
-
type
|
|
35
|
-
type
|
|
28
|
+
type TRedisTransactionNodeRedis = RedisClientMultiCommandType<RedisModules, RedisFunctions, RedisScripts>;
|
|
29
|
+
type TRedisClientNodeRedis = RedisClientType<RedisModules, RedisFunctions, RedisScripts>;
|
|
36
30
|
interface IRedisClient {
|
|
37
31
|
validateRedisVersion(major: number, feature?: number, minor?: number): boolean;
|
|
38
32
|
validateRedisServerSupport(cb: ICallback<void>): void;
|
|
@@ -146,15 +140,6 @@ declare module 'ioredis' {
|
|
|
146
140
|
lmove(source: string, destination: string, from: 'LEFT' | 'RIGHT', to: 'LEFT' | 'RIGHT', cb: ICallback<string>): void;
|
|
147
141
|
}
|
|
148
142
|
}
|
|
149
|
-
declare module 'redis' {
|
|
150
|
-
interface Commands<R> {
|
|
151
|
-
info(cb?: Callback<string>): R;
|
|
152
|
-
info(section?: string | string[], cb?: Callback<string>): R;
|
|
153
|
-
INFO(cb?: Callback<string>): R;
|
|
154
|
-
INFO(section?: string | string[], cb?: Callback<string>): R;
|
|
155
|
-
lmove(source: string, destination: string, from: 'LEFT' | 'RIGHT', to: 'LEFT' | 'RIGHT', cb: ICallback<string>): void;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
143
|
|
|
159
144
|
interface ILoggerConfig {
|
|
160
145
|
enabled: boolean;
|
|
@@ -472,4 +457,4 @@ declare const redis: {
|
|
|
472
457
|
createInstance: typeof createInstance;
|
|
473
458
|
};
|
|
474
459
|
|
|
475
|
-
export { CallbackEmptyReplyError, CallbackInvalidReplyError, ERedisConfigClient, EventEmitter, type ICallback, type ILogger, type ILoggerConfig, type IRedisClient, type IRedisConfig, type IRedisConfigIORedis, type IRedisConfigNodeRedis, type
|
|
460
|
+
export { CallbackEmptyReplyError, CallbackInvalidReplyError, ERedisConfigClient, EventEmitter, type ICallback, type ILogger, type ILoggerConfig, type IRedisClient, type IRedisConfig, type IRedisConfigIORedis, type IRedisConfigNodeRedis, type IRedisTransaction, Lock, LockAbortError, LockAcquireError, LockError, LockExtendError, LockMethodNotAllowedError, LockNotAcquiredError, LockNotReleasedError, LoggerError, PanicError, PowerSwitch, PowerSwitchError, RedisClient, RedisClientError, RedisSMQError, type TEvent, type TEventEmitterEvent, type TFunction, type TRedisClientNodeRedis, type TRedisTransactionNodeRedis, type TUnaryFunction, Ticker, TickerError, WatchedKeysChangedError, Worker, WorkerError, WorkerPool, WorkerRunner, async, logger, redis };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
`)[1].split(":")[1].split(".").map(n=>Number(n)),t()):t(new b)})}loadScripts(t){z.getInstance().loadScripts(this,t)}runScript(t,i,e,n){let s=z.getInstance().getScriptId(t);this.evalsha(s,[i.length,...i,...e],(o,a)=>{o?n(o):n(null,a)})}static addScript(t,i){if(!z.getInstance().addScript(t,i))throw new u(`A script with name [${t}] already exists`)}};v.redisServerVersion=null;var p=v;p.addScript("RELEASE_LOCK",st.readFileSync(ot(__dirname,"./lua/release-lock.lua")).toString());p.addScript("EXTEND_LOCK",st.readFileSync(ot(__dirname,"./lua/extend-lock.lua")).toString());var V=class{constructor(r,t,i,e=!1,n=!1,s=!0){this.status=0;this.lockingTimer=null;this.autoExtendTimer=null;this.throwExceptions=!0;this.lockKey=t,this.ttl=i,this.retryOnFail=e,this.lockId=kt(),this.redisClient=r,this.autoExtend=n,this.throwExceptions=s}resetTimers(){this.lockingTimer&&(clearTimeout(this.lockingTimer),this.lockingTimer=null),this.autoExtendTimer&&(clearTimeout(this.autoExtendTimer),this.autoExtendTimer=null)}setUnlocked(){this.status=0}setLocked(){this.status=2}setExtended(){this.status=5}extend(r){this.isLocked()?(this.status=4,this.redisClient.runScript("EXTEND_LOCK",[this.lockKey],[this.lockId,this.ttl],(t,i)=>{t?r(t):this.status===4?i?(this.setExtended(),r()):(this.setUnlocked(),r(new N)):r(new x)})):r(new A)}runAutoExtendTimer(){let r=Math.ceil(this.ttl/2);this.autoExtendTimer=setTimeout(()=>this.extend(t=>{if(!t)this.runAutoExtendTimer();else if(this.throwExceptions&&!(t instanceof x))throw t}),r)}acquireLock(r){if(!this.isReleased())r(new F);else{this.status=1;let t=()=>{this.status===1?this.redisClient.set(this.lockKey,this.lockId,{expire:{mode:"PX",value:this.ttl},exists:"NX"},(i,e)=>{i?r(i):this.status===1?e?(this.setLocked(),this.autoExtend&&this.runAutoExtendTimer(),r()):this.retryOnFail?this.lockingTimer=setTimeout(t,1e3):(this.setUnlocked(),r(new E)):r(new x)}):r(new x)};t()}}extendLock(r){this.autoExtend?r(new O):this.extend(r)}releaseLock(r){this.status===0?r():this.isLocked()?(this.resetTimers(),this.status=3,this.redisClient.runScript("RELEASE_LOCK",[this.lockKey],[this.lockId],i=>{i?r(i):(this.setUnlocked(),r())})):r(new A)}acquireOrExtend(r){if(this.autoExtend)r(new O);else{let t=()=>{this.acquireLock(i=>{i?r(i):r(null,2)})};this.isLocked()?this.extend(i=>{i?i instanceof N?t():r(i):r(null,5)}):t()}}isLocked(){return this.status===2||this.status===5}isReleased(){return this.status===0}getId(){return this.lockId}};var I=class{constructor(r=!0){this.isPowered=!1;this.pendingState=null;this.throwExceptionOnError=r}switch(r){if(this.pendingState!==null){if(this.throwExceptionOnError)throw new w("Can not switch state while another state transition is in progress.");return!1}if(r===this.isPowered){if(this.throwExceptionOnError)throw new w("Can not switch to the same current state.");return!1}return this.pendingState=r,!0}isUp(){return this.isPowered}isDown(){return!this.isPowered}isGoingUp(){return this.pendingState===!0}isGoingDown(){return this.pendingState===!1}isRunning(){return this.isUp()&&this.pendingState===null}goingUp(){return this.switch(!0)}goingDown(){return this.switch(!1)}commit(){if(this.pendingState===null)throw new w("Expected a pending state");this.isPowered=this.pendingState,this.pendingState=null}rollback(){if(this.pendingState===null)throw new w("Expected a pending state");this.pendingState=null}};import{EventEmitter as vt}from"events";var P=class extends vt{};var L=class extends P{constructor(t=()=>{},i=1e3){super();this.powerManager=new I;this.onNextTickFn=null;this.timeout=null;this.interval=null;this.shutdownTimeout=null;this.aborted=!1;this.onTickFn=t,this.time=i,this.powerManager.goingUp()}shutdown(){this.shutdownTimeout&&clearTimeout(this.shutdownTimeout),this.powerManager.commit(),this.emit("down")}onTick(){var t;if(this.powerManager.isGoingDown())this.shutdown();else if(this.powerManager.isRunning()){let i=(t=this.onNextTickFn)!=null?t:this.onTickFn;this.onNextTickFn=null,i()}else this.emit("error",new R("Unexpected call"))}abort(){this.aborted||(this.aborted=!0,this.powerManager.isGoingDown()?this.shutdown():this.quit())}quit(){this.powerManager.isGoingUp()?(this.powerManager.rollback(),this.emit("down")):this.aborted&&this.powerManager.isDown()?this.emit("down"):(this.powerManager.goingDown(),this.timeout?(clearTimeout(this.timeout),this.shutdown()):this.interval?(clearInterval(this.interval),this.shutdown()):this.aborted?this.shutdown():this.shutdownTimeout=setTimeout(()=>{this.powerManager.isGoingDown()&&this.shutdown()},6e4))}isTicking(){return!!(this.timeout||this.interval)}nextTick(){if(this.isTicking())throw new M("A timer is already running");this.powerManager.isGoingDown()?this.shutdown():(this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.timeout=setTimeout(()=>{this.timeout=null,this.onTick()},this.time)))}nextTickFn(t){this.onNextTickFn=t,this.nextTick()}runTimer(){if(this.isTicking())throw new M("A timer is already running");this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.interval=setInterval(()=>this.onTick(),this.time))}};var B=class{constructor(r,t=1e3){this.ticker=null;this.powerManager=null;this.getTicker=()=>{if(!this.ticker)throw new y("Expected an instance of Ticker");return this.ticker};this.onTick=()=>{this.work(r=>{if(r)throw r;this.getTicker().nextTick()})};this.run=()=>{if(this.managed)throw new y("You can not run a managed worker");let r=this.getPowerManager();r.goingUp(),this.getTicker().nextTick(),r.commit()};this.quit=r=>{if(this.managed)r();else{let t=this.getPowerManager();t.goingDown();let i=this.getTicker();i.on("down",()=>{t.commit(),r()}),i.quit()}};this.managed=r,r||(this.ticker=new L(this.onTick,t),this.powerManager=new I)}getPowerManager(){if(!this.powerManager)throw new y("Expected an instance of PowerSwitch");return this.powerManager}};var $=class extends P{constructor(t,i,e,n){super();this.onTick=()=>{k.waterfall([t=>{this.lock.acquireOrExtend((i,e)=>{e===2&&this.logger.info(`Workers are exclusively running from this instance (Lock ID ${this.lock.getId()}).`),t(i)})},t=>{this.workerPool.work(t)}],t=>{!t||t instanceof E?this.ticker.nextTick():this.emit("error",t)})};this.clearWorkerPool=t=>{this.workerPool.clear(t)};this.stopTicker=t=>{this.ticker.once("down",t),this.ticker.quit()};this.releaseLock=t=>{this.lock.releaseLock(t)};this.run=()=>{this.emit("up"),this.ticker.nextTick()};this.quit=t=>{k.waterfall([this.stopTicker,this.clearWorkerPool,this.releaseLock],()=>{this.emit("down"),t()})};this.powerManager=new I,this.redisClient=t,this.logger=n,this.lock=new V(t,i,6e4),this.ticker=new L(this.onTick),this.workerPool=e}addWorker(t){this.workerPool.add(t)}};var J=class{constructor(){this.pool=[];this.work=r=>{if(this.pool.length){let t=this.pool.map(i=>e=>i.work(e));k.waterfall(t,r)}else r()};this.add=r=>(this.pool.push(r),this.pool.length);this.clear=r=>{k.each(this.pool,(t,i,e)=>{t.quit(e)},()=>{this.pool=[],r()})}}};import{createLogger as Ct}from"bunyan";var D=()=>{},ft={debug:D,warn:D,info:D,error:D},C=null;function It(){C=null}function xt(l){if(C)throw new U("Logger has been already initialized.");C=l}function wt(l,r){var t;if(!l.enabled)return ft;if(C||(C=Ct(Y(H({},(t=l.options)!=null?t:{}),{name:"redis-smq"}))),r){let i=(e,n)=>(s,...o)=>{let a=typeof s=="string"?`${r} | ${s}`:s;return n[e](a,...o)};return{info:i("info",C),warn:i("warn",C),debug:i("debug",C),error:i("error",C)}}return C}var Tt={getLogger:wt,setLogger:xt,destroy:It};import{createClient as Rt}from"redis";var X=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lrem(r,t,i),this}lpop(r){return this.multi.lpop(r),this}lpush(r,t){return this.multi.lpush(r,t),this}ltrim(r,t,i){return this.multi.ltrim(r,t,i),this}rpop(r){return this.multi.rpop(r),this}rpush(r,t){return this.multi.rpush(r,t),this}zadd(r,t,i){return this.multi.zadd(r,t,i),this}zrem(r,t){return this.multi.zrem(r,t),this}sadd(r,t){return this.multi.sadd(r,t),this}srem(r,t){return this.multi.srem(r,t),this}hset(r,t,i){return this.multi.hset(r,t,String(i)),this}hdel(r,t){return this.multi.hdel(r,t),this}hincrby(r,t,i){return this.multi.hincrby(r,t,i),this}pexpire(r,t){return this.multi.pexpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rpoplpush(r,t),this}del(r){return this.multi.del(r),this}exec(r){this.multi.exec((t,i)=>{t?r(t):i?r(null,i):r(new f)})}};var Et=function(l){return l?this.flush_and_error({message:"Connection forcefully ended and command aborted.",code:"NR_CLOSED"}):arguments.length===0&&this.warn(`Using .end() without the flush parameter is deprecated and throws from v.3.0.0 on.
|
|
3
|
-
Please check the doku (https://github.com/NodeRedis/node_redis) and explictly use flush.`),this.retry_timer&&(clearTimeout(this.retry_timer),this.retry_timer=null),this.stream.removeAllListeners(),this.stream.once("close",()=>{this.emit("end"),this.emitted_end=!0}),this.stream.on("error",()=>{}),this.connected=!1,this.ready=!1,this.closing=!0,this.stream.destroySoon()},q=class extends p{constructor(t={}){super();this.client=Rt(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")}),this.client.end=Et}set(t,i,e,n){e.exists&&e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,e.exists,n):e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,n):e.exists?this.client.set(t,i,e.exists,n):this.client.set(t,i,n)}zadd(t,i,e,n){this.client.zadd(t,i,e,n)}multi(){return new X(this.client)}watch(t,i){this.client.watch(t,i)}unwatch(t){this.client.unwatch(t)}sismember(t,i,e){this.client.sismember(t,i,e)}sscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.sscan(...s,(o,[a,c])=>{o?n(o):n(null,{cursor:a,items:c})})}zscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.zscan(...s,(o,a)=>{if(o)n(o);else if(!a)n(new b);else{let c=new Set,[d,h]=a;for(;h.length;){let T=String(h.shift()),lt=String(h.shift());c.add(T)}n(null,{cursor:d,items:[...c]})}})}zcard(t,i){this.client.zcard(t,i)}zrange(t,i,e,n){this.client.zrange(t,i,e,n)}zrevrange(t,i,e,n){this.client.zrevrange(t,i,e,n)}psubscribe(t){this.client.psubscribe(t)}punsubscribe(t){this.client.punsubscribe(t)}subscribe(t){this.client.subscribe(t)}unsubscribe(t){this.client.unsubscribe(t)}zrangebyscore(t,i,e,n,s,o){this.client.zrangebyscore(t,i,e,"LIMIT",n,s,o)}zrem(t,i,e){this.client.zrem(t,i,e)}smembers(t,i){this.client.smembers(t,i)}sadd(t,i,e){this.client.sadd(t,i,e)}srem(t,i,e){this.client.srem(t,i,e)}hgetall(t,i){this.client.hgetall(t,(e,n)=>{e?i(e):i(null,n!=null?n:{})})}hscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.hscan(...s,(o,[a,c])=>{if(o)n(o);else{let d={};for(;c.length;){let h=String(c.shift());d[h]=String(c.shift())}n(null,{cursor:a,result:d})}})}hget(t,i,e){this.client.hget(t,i,e)}hset(t,i,e,n){this.client.hset(t,i,String(e),n)}hdel(t,i,e){this.client.hdel(t,...typeof i=="string"?[i]:i,e)}lrange(t,i,e,n){this.client.lrange(t,i,e,n)}hkeys(t,i){this.client.hkeys(t,i)}hlen(t,i){this.client.hlen(t,i)}brpoplpush(t,i,e,n){this.client.brpoplpush(t,i,e,n)}rpoplpush(t,i,e){this.client.rpoplpush(t,i,e)}zrangebyscorewithscores(t,i,e,n){this.client.zrangebyscore(t,i,e,"WITHSCORES",(s,o)=>{if(s)n(s);else{let a=o!=null?o:[],c={};for(let d=a.splice(0,2);d.length>0;d=a.splice(0,2)){let[h,T]=d;c[T]=h}n(null,c)}})}rpop(t,i){this.client.rpop(t,i)}lrem(t,i,e,n){this.client.lrem(t,i,e,n)}publish(t,i,e){this.client.publish(t,i,e)}flushall(t){this.client.flushall(t)}loadScript(t,i){this.client.script("load",t,i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.evalsha(n.concat(s),(o,a)=>e(o,a))}get(t,i){this.client.get(t,i)}del(t,i){this.client.del(...typeof t=="string"?[t]:t,i)}llen(t,i){this.client.llen(t,i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lmove(t,i,e,n,s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.zremrangebyscore(t,i,e,n)}hmget(t,i,e){this.client.hmget(t,i,e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end(!0))}end(t){this.connectionClosed||this.client.end(t)}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info(t)}on(t,i){return this.client.on(t,i),this}};import{createClient as St}from"@redis/client";import{WatchError as yt}from"@redis/client";var G=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lRem(r,t,i),this}lpop(r){return this.multi.lPop(r),this}lpush(r,t){return this.multi.lPush(r,t),this}ltrim(r,t,i){return this.multi.lTrim(r,t,i),this}rpop(r){return this.multi.rPop(r),this}rpush(r,t){return this.multi.rPush(r,t),this}zadd(r,t,i){return this.multi.zAdd(r,{score:t,value:i}),this}zrem(r,t){return this.multi.zRem(r,t),this}sadd(r,t){return this.multi.sAdd(r,t),this}srem(r,t){return this.multi.sRem(r,t),this}hset(r,t,i){return this.multi.hSet(r,t,i),this}hdel(r,t){return this.multi.hDel(r,t),this}hincrby(r,t,i){return this.multi.hIncrBy(r,t,i),this}pexpire(r,t){return this.multi.pExpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rPopLPush(r,t),this}del(r){return this.multi.del(r),this}exec(r){this.multi.exec().then(t=>r(null,t)).catch(t=>{t instanceof yt?r(new f):r(t)})}};var _=class extends p{constructor(t={}){super();this.client=St(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")}),this.client.connect()}set(t,i,e,n){this.client.set(t,i,H(H({},e.expire?{[e.expire.mode]:e.expire.value}:{}),e.exists?{[e.exists]:!0}:{})).then(s=>n(null,s)).catch(n)}zadd(t,i,e,n){this.client.zAdd(t,{score:i,value:e}).then(s=>n(null,s)).catch(n)}multi(){return new G(this.client)}watch(t,i){this.client.watch(t).then(e=>i(null,e)).catch(i)}unwatch(t){this.client.unwatch().then(i=>t(null,i)).catch(t)}sismember(t,i,e){this.client.sIsMember(t,i).then(n=>e(null,Number(n))).catch(e)}zcard(t,i){this.client.zCard(t).then(e=>i(null,e)).catch(i)}zrange(t,i,e,n){this.client.zRange(t,i,e).then(s=>n(null,(Array.isArray(s)?s:[]).map(o=>String(o)))).catch(n)}zrevrange(t,i,e,n){this.client.sendCommand(["ZREVRANGE",t,String(i),String(e)]).then(s=>n(null,(Array.isArray(s)?s:[]).map(o=>String(o)))).catch(n)}zrem(t,i,e){this.client.zRem(t,i).then(n=>e(null,n)).catch(e)}psubscribe(t){this.client.pSubscribe(t,(i,e)=>{this.client.emit("pmessage",t,e,i)})}punsubscribe(t){this.client.pUnsubscribe(t).catch(()=>{})}subscribe(t){this.client.subscribe(t,(i,e)=>{this.client.emit("message",e,i)})}unsubscribe(t){this.client.unsubscribe(t).catch(()=>{})}zrangebyscore(t,i,e,n,s,o){this.client.zRangeByScore(t,i,e,{LIMIT:{offset:n,count:s}}).then(a=>o(null,a)).catch(o)}smembers(t,i){this.client.sMembers(t).then(e=>i(null,e)).catch(i)}sscan(t,i,e,n){let s=[t,Number(i),e];this.client.sScan(...s).then(({cursor:o,members:a})=>{n(null,{cursor:String(o),items:a})}).catch(n)}zscan(t,i,e,n){let s=[t,Number(i),e];this.client.zScan(...s).then(({cursor:o,members:a})=>{let c=new Set;for(let d of a)c.add(d.value);n(null,{cursor:String(o),items:[...c]})}).catch(n)}sadd(t,i,e){this.client.sAdd(t,i).then(n=>e(null,n)).catch(e)}srem(t,i,e){this.client.sRem(t,i).then(n=>e(null,n)).catch(e)}hgetall(t,i){this.client.hGetAll(t).then(e=>i(null,e)).catch(i)}hscan(t,i,e,n){let s=[t,Number(i),e];this.client.hScan(...s).then(({cursor:o,tuples:a})=>{let c={};for(;a.length;){let d=a.shift();d&&(c[d.field]=d.value)}n(null,{cursor:String(o),result:c})}).catch(n)}hget(t,i,e){this.client.hGet(t,i).then(n=>e(null,n!=null?n:null)).catch(e)}hset(t,i,e,n){this.client.hSet(t,i,e).then(s=>n(null,s)).catch(n)}hdel(t,i,e){this.client.hDel(t,i).then(n=>e(null,Number(n))).catch(e)}lrange(t,i,e,n){this.client.lRange(t,i,e).then(s=>n(null,s)).catch(n)}hkeys(t,i){this.client.hKeys(t).then(e=>i(null,e)).catch(i)}hlen(t,i){this.client.hLen(t).then(e=>i(null,e)).catch(i)}brpoplpush(t,i,e,n){this.client.brPopLPush(t,i,e).then(s=>n(null,s)).catch(n)}rpoplpush(t,i,e){this.client.rPopLPush(t,i).then(n=>e(null,n)).catch(e)}zrangebyscorewithscores(t,i,e,n){this.client.zRangeByScoreWithScores(t,i,e).then(s=>{let o={};for(let{score:a,value:c}of s)o[a]=c;n(null,o)}).catch(n)}rpop(t,i){this.client.rPop(t).then(e=>i(null,e)).catch(i)}lrem(t,i,e,n){this.client.lRem(t,i,e).then(s=>n(null,s)).catch(n)}publish(t,i,e){this.client.publish(t,i).then(n=>e(null,n)).catch(e)}flushall(t){this.client.flushAll().then(i=>t(null,i)).catch(t)}loadScript(t,i){this.client.sendCommand(["SCRIPT","LOAD",t]).then(e=>i(null,typeof e=="string"?e:String(e))).catch(i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.sendCommand(["evalsha",...n.concat(s).map(o=>String(o))]).then(o=>{Array.isArray(o)?e(null,o.map(a=>a instanceof Buffer?a.toString():a)):o instanceof Buffer?e(null,o.toString()):e(null,o)}).catch(e)}get(t,i){this.client.sendCommand(["get",t]).then(e=>i(null,typeof e=="string"?e:null)).catch(i)}del(t,i){this.client.del(t).then(e=>i(null,e)).catch(i)}llen(t,i){this.client.sendCommand(["llen",t]).then(e=>i(null,Number(e))).catch(i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lMove(t,i,e,n).then(o=>s(null,o)).catch(s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.sendCommand(["zremrangebyscore",t,`${i}`,`${e}`]).then(s=>n(null,Number(s))).catch(n)}hmget(t,i,e){this.client.hmGet(t,i).then(n=>e(null,n)).catch(e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect().catch(()=>{})}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info().then(i=>t(null,i)).catch(t)}on(t,i){return this.client.on(t,i),this}};import Lt from"ioredis";var W=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lrem(r,t,i),this}lpop(r){return this.multi.lpop(r),this}lpush(r,t){return this.multi.lpush(r,t),this}ltrim(r,t,i){return this.multi.ltrim(r,t,i),this}rpop(r){return this.multi.rpop(r),this}rpush(r,t){return this.multi.rpush(r,t),this}zadd(r,t,i){return this.multi.zadd(r,t,i),this}zrem(r,t){return this.multi.zrem(r,...typeof t=="string"?[t]:t),this}sadd(r,t){return this.multi.sadd(r,t),this}srem(r,t){return this.multi.srem(r,...typeof t=="string"?[t]:t),this}hset(r,t,i){return this.multi.hset(r,t,i),this}hdel(r,t){return this.multi.hdel(r,...typeof t=="string"?[t]:t),this}hincrby(r,t,i){return this.multi.hincrby(r,t,i),this}pexpire(r,t){return this.multi.pexpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rpoplpush(r,t),this}del(r){return this.multi.del(...typeof r=="string"?[r]:r),this}exec(r){this.multi.exec((t,i)=>{if(t)r(t);else if(!i)r(new f);else{let e=[],n=null;for(let s of i){if(!Array.isArray(s)){n=new u("Expected an array reply from multi.exec()");break}let[o,a]=s;if(o instanceof Error){n=o;break}e.push(a)}n?r(n):r(null,e)}})}};var K=class extends p{constructor(t={}){super();this.client=new Lt(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")})}set(t,i,e,n){e.exists&&e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,e.exists,n):e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,n):e.exists?this.client.set(t,i,e.exists,n):this.client.set(t,i,n)}zadd(t,i,e,n){this.client.zadd(t,i,e,n)}multi(){return new W(this.client)}watch(t,i){this.client.watch(t,i)}unwatch(t){this.client.unwatch(t)}sismember(t,i,e){this.client.sismember(t,i,e)}sscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.sscan(...s,(o,[a,c])=>{o?n(o):n(null,{cursor:a,items:c})})}zcard(t,i){this.client.zcard(t,i)}zrange(t,i,e,n){this.client.zrange(t,i,e,n)}zscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.zscan(...s,(o,a)=>{if(o)n(o);else if(!a)n(new b);else{let c=new Set,[d,h]=a;for(;h.length;){let T=String(h.shift()),lt=String(h.shift());c.add(T)}n(null,{cursor:d,items:[...c]})}})}zrevrange(t,i,e,n){this.client.zrevrange(t,i,e,n)}zrem(t,i,e){this.client.zrem(t,i,e)}psubscribe(t){this.client.psubscribe(t)}punsubscribe(t){this.client.punsubscribe(t)}subscribe(t){this.client.subscribe(t)}unsubscribe(t){this.client.unsubscribe(t)}zrangebyscore(t,i,e,n,s,o){this.client.zrangebyscore(t,i,e,"LIMIT",n,s,o)}smembers(t,i){this.client.smembers(t,i)}sadd(t,i,e){this.client.sadd(t,i,e)}srem(t,i,e){this.client.srem(t,i,e)}hgetall(t,i){this.client.hgetall(t,i)}hscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.hscan(...s,(o,[a,c])=>{if(o)n(o);else{let d={};for(;c.length;){let h=String(c.shift());d[h]=String(c.shift())}n(null,{cursor:a,result:d})}})}hget(t,i,e){this.client.hget(t,i,e)}hset(t,i,e,n){this.client.hset(t,i,e,n)}hdel(t,i,e){this.client.hdel(t,i,e)}lrange(t,i,e,n){this.client.lrange(t,i,e,n)}hkeys(t,i){this.client.hkeys(t,i)}hlen(t,i){this.client.hlen(t,i)}brpoplpush(t,i,e,n){this.client.brpoplpush(t,i,e,n)}rpoplpush(t,i,e){this.client.rpoplpush(t,i,e)}zrangebyscorewithscores(t,i,e,n){this.client.zrangebyscore(t,i,e,"WITHSCORES",(s,o)=>{if(s)n(s);else{let a=o!=null?o:[],c={};for(let d=a.splice(0,2);d.length>0;d=a.splice(0,2)){let[h,T]=d;c[T]=h}n(null,c)}})}rpop(t,i){this.client.rpop(t,i)}lrem(t,i,e,n){this.client.lrem(t,i,e,n)}publish(t,i,e){this.client.publish(t,i,e)}flushall(t){this.client.flushall(t)}loadScript(t,i){this.client.script("load",t,i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.evalsha(n.concat(s),e)}get(t,i){this.client.get(t,i)}del(t,i){this.client.del(t,i)}llen(t,i){this.client.llen(t,i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lmove(t,i,e,n,s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.zremrangebyscore(t,i,e,n)}hmget(t,i,e){this.client.hmget(t,i,e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect(!1)}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info(t)}on(t,i){return this.client.on(t,i),this}};function Mt(l){return l.client==="redis"?new q(l.options):l.client==="redis_v4"?new _(l.options):new K(l.options)}function Nt(l,r){let t=Mt(l);t.once("ready",()=>{k.waterfall([i=>t.validateRedisServerSupport(i),i=>t.loadScripts(i)],i=>{if(i)throw i;r(null,t)})})}var Ot={createInstance:Nt};export{b as CallbackEmptyReplyError,Q as CallbackInvalidReplyError,tt as ERedisConfigClient,P as EventEmitter,V as Lock,x as LockAbortError,E as LockAcquireError,m as LockError,N as LockExtendError,O as LockMethodNotAllowedError,A as LockNotAcquiredError,F as LockNotReleasedError,U as LoggerError,R as PanicError,I as PowerSwitch,w as PowerSwitchError,p as RedisClient,u as RedisClientError,g as RedisSMQError,L as Ticker,M as TickerError,f as WatchedKeysChangedError,B as Worker,y as WorkerError,J as WorkerPool,$ as WorkerRunner,k as async,Tt as logger,Ot as redis};
|
|
1
|
+
var st=Object.defineProperty,ot=Object.defineProperties;var lt=Object.getOwnPropertyDescriptors;var $=Object.getOwnPropertySymbols;var at=Object.prototype.hasOwnProperty,ct=Object.prototype.propertyIsEnumerable;var J=(o,r,t)=>r in o?st(o,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[r]=t,z=(o,r)=>{for(var t in r||(r={}))at.call(r,t)&&J(o,t,r[t]);if($)for(var t of $(r))ct.call(r,t)&&J(o,t,r[t]);return o},Z=(o,r)=>ot(o,lt(r));var j=(t=>(t.IOREDIS="ioredis",t.REDIS="redis",t))(j||{});var d=class extends Error{constructor(r){super(r)}get name(){return this.constructor.name}};var m=class extends d{constructor(){super("Expected a non-empty reply")}};var K=class extends d{constructor(){super("Invalid reply type")}};var w=class extends d{};var U=class extends d{};var L=class extends d{};var h=class extends d{};var C=class extends h{constructor(r="releaseLock() may have been called. Abandoning."){super(r)}};var T=class extends h{constructor(r="Could not acquire a lock"){super(r)}};var M=class extends h{constructor(r="Acquired lock could not be extended"){super(r)}};var O=class extends h{constructor(r="This method can not be used when autoExtend is enabled"){super(r)}};var A=class extends h{constructor(r="Can not extend a lock which has not been yet acquired. Maybe a pending operation is in progress."){super(r)}};var H=class extends h{constructor(r="A lock has been already obtained but not yet released or maybe a pending operation is in progress."){super(r)}};var u=class extends d{};var E=class extends u{constructor(r="One (or more) of the watched keys has been changed"){super(r)}};var y=class extends w{};var I=class extends d{};import{v4 as mt}from"uuid";import rt from"fs";import{resolve as nt}from"path";import{EventEmitter as gt}from"events";var Y=(o,r,t)=>{if(o.length){let i=0,e=()=>{r(o[i],i,n=>{i+=1,n||i>=o.length?t(n):setTimeout(()=>e(),0)})};e()}else t()},tt=(o,r,t)=>{let i=Object.keys(o);if(i.length){let e=0,n=()=>{let s=i[e];r(o[s],s,l=>{e+=1,l||e>=i.length?t(l):setTimeout(()=>n(),0)})};n()}else t()},dt=(o,r,t)=>{Array.isArray(o)?Y(o,r,t):tt(o,r,t)},ut=(o,r)=>{if(o.length){let t=0,i=(e,...n)=>{t+=1,e?r(e):t<o.length?setTimeout(()=>{n.length?o[t](...n,i):o[t](i)},0):n.length?r(null,n[0]):r()};o[t](i)}else r()},b={each:dt,eachIn:tt,eachOf:Y,waterfall:ut};import it from"fs";import{resolve as et}from"path";var R=class R{constructor(){this.isLoaded=!1;this.scripts=new Map,this.addScript("ZPOPRPUSH",it.readFileSync(et(__dirname,"./lua/zpoprpush.lua")).toString()),this.addScript("LPOPRPUSH",it.readFileSync(et(__dirname,"./lua/lpoprpush.lua")).toString())}hasScript(r){return this.scripts.has(r)}addScript(r,t){return this.hasScript(r)?!1:(this.scripts.set(r,{content:t}),this.isLoaded=!1,!0)}loadScripts(r,t){if(this.isLoaded)t();else{let i=[];this.scripts.forEach(e=>i.push(n=>{e.id?n():r.loadScript(e.content,(s,l)=>{s?n(s):l?(e.id=l,n()):n(new m)})})),b.waterfall(i,e=>{e?t(e):(this.isLoaded=!0,t())})}}getScriptId(r){var i;let{id:t}=(i=this.scripts.get(r))!=null?i:{};if(!t)throw new u(`ID of script [${r}] is missing`);return t}static getInstance(){return R.instance||(R.instance=new R),R.instance}};R.instance=null;var N=R;var ht=[4,0,0],p=class p extends gt{constructor(){super(...arguments);this.connectionClosed=!0}validateRedisVersion(t,i=0,e=0){if(!p.redisServerVersion)throw new u("UNKNOWN_REDIS_SERVER_VERSION");return p.redisServerVersion[0]>t||p.redisServerVersion[0]===t&&p.redisServerVersion[1]>=i&&p.redisServerVersion[2]>=e}validateRedisServerSupport(t){let i=e=>{let[n,s,l]=ht;this.validateRedisVersion(n,s,l)?e():e(new u("UNSUPPORTED_REDIS_SERVER_VERSION"))};p.redisServerVersion?i(t):this.updateServerVersion(e=>{e?t(e):i(t)})}sscanAll(t,i,e){let n=new Set,s=l=>{this.sscan(t,l,i,(a,c)=>{a?e(a):c?(c.items.forEach(g=>n.add(g)),c.cursor==="0"?e(null,[...n]):s(c.cursor)):e(new m)})};s("0")}hscanAll(t,i,e){let n={},s=l=>{this.hscan(t,l,i,(a,c)=>{a?e(a):c?(Object.assign(n,c.result),c.cursor==="0"?e(null,n):s(c.cursor)):e(new m)})};s("0")}zpoprpush(t,i,e){this.runScript("ZPOPRPUSH",[t,i],[],(n,s)=>{n?e(n):e(null,typeof s=="string"?s:null)})}lpoprpush(t,i,e){this.validateRedisVersion(6,2)?this.lmove(t,i,"LEFT","RIGHT",e):this.runScript("LPOPRPUSH",[t,i],[],(n,s)=>{n?e(n):e(null,typeof s=="string"?s:null)})}updateServerVersion(t){p.redisServerVersion?t():this.getInfo((i,e)=>{i?t(i):e?(p.redisServerVersion=e.split(`\r
|
|
2
|
+
`)[1].split(":")[1].split(".").map(n=>Number(n)),t()):t(new m)})}loadScripts(t){N.getInstance().loadScripts(this,t)}runScript(t,i,e,n){let s=N.getInstance().getScriptId(t);this.evalsha(s,[i.length,...i,...e],(l,a)=>{l?n(l):n(null,a)})}static addScript(t,i){if(!N.getInstance().addScript(t,i))throw new u(`A script with name [${t}] already exists`)}};p.redisServerVersion=null;var k=p;k.addScript("RELEASE_LOCK",rt.readFileSync(nt(__dirname,"./lua/release-lock.lua")).toString());k.addScript("EXTEND_LOCK",rt.readFileSync(nt(__dirname,"./lua/extend-lock.lua")).toString());var F=class{constructor(r,t,i,e=!1,n=!1,s=!0){this.status=0;this.lockingTimer=null;this.autoExtendTimer=null;this.throwExceptions=!0;this.lockKey=t,this.ttl=i,this.retryOnFail=e,this.lockId=mt(),this.redisClient=r,this.autoExtend=n,this.throwExceptions=s}resetTimers(){this.lockingTimer&&(clearTimeout(this.lockingTimer),this.lockingTimer=null),this.autoExtendTimer&&(clearTimeout(this.autoExtendTimer),this.autoExtendTimer=null)}setUnlocked(){this.status=0}setLocked(){this.status=2}setExtended(){this.status=5}extend(r){this.isLocked()?(this.status=4,this.redisClient.runScript("EXTEND_LOCK",[this.lockKey],[this.lockId,this.ttl],(t,i)=>{t?r(t):this.status===4?i?(this.setExtended(),r()):(this.setUnlocked(),r(new M)):r(new C)})):r(new A)}runAutoExtendTimer(){let r=Math.ceil(this.ttl/2);this.autoExtendTimer=setTimeout(()=>this.extend(t=>{if(!t)this.runAutoExtendTimer();else if(this.throwExceptions&&!(t instanceof C))throw t}),r)}acquireLock(r){if(!this.isReleased())r(new H);else{this.status=1;let t=()=>{this.status===1?this.redisClient.set(this.lockKey,this.lockId,{expire:{mode:"PX",value:this.ttl},exists:"NX"},(i,e)=>{i?r(i):this.status===1?e?(this.setLocked(),this.autoExtend&&this.runAutoExtendTimer(),r()):this.retryOnFail?this.lockingTimer=setTimeout(t,1e3):(this.setUnlocked(),r(new T)):r(new C)}):r(new C)};t()}}extendLock(r){this.autoExtend?r(new O):this.extend(r)}releaseLock(r){this.status===0?r():this.isLocked()?(this.resetTimers(),this.status=3,this.redisClient.runScript("RELEASE_LOCK",[this.lockKey],[this.lockId],i=>{i?r(i):(this.setUnlocked(),r())})):r(new A)}acquireOrExtend(r){if(this.autoExtend)r(new O);else{let t=()=>{this.acquireLock(i=>{i?r(i):r(null,2)})};this.isLocked()?this.extend(i=>{i?i instanceof M?t():r(i):r(null,5)}):t()}}isLocked(){return this.status===2||this.status===5}isReleased(){return this.status===0}getId(){return this.lockId}};var f=class{constructor(r=!0){this.isPowered=!1;this.pendingState=null;this.throwExceptionOnError=r}switch(r){if(this.pendingState!==null){if(this.throwExceptionOnError)throw new I("Can not switch state while another state transition is in progress.");return!1}if(r===this.isPowered){if(this.throwExceptionOnError)throw new I("Can not switch to the same current state.");return!1}return this.pendingState=r,!0}isUp(){return this.isPowered}isDown(){return!this.isPowered}isGoingUp(){return this.pendingState===!0}isGoingDown(){return this.pendingState===!1}isRunning(){return this.isUp()&&this.pendingState===null}goingUp(){return this.switch(!0)}goingDown(){return this.switch(!1)}commit(){if(this.pendingState===null)throw new I("Expected a pending state");this.isPowered=this.pendingState,this.pendingState=null}rollback(){if(this.pendingState===null)throw new I("Expected a pending state");this.pendingState=null}};import{EventEmitter as bt}from"events";var P=class extends bt{};var S=class extends P{constructor(t=()=>{},i=1e3){super();this.powerManager=new f;this.onNextTickFn=null;this.timeout=null;this.interval=null;this.shutdownTimeout=null;this.aborted=!1;this.onTickFn=t,this.time=i,this.powerManager.goingUp()}shutdown(){this.shutdownTimeout&&clearTimeout(this.shutdownTimeout),this.powerManager.commit(),this.emit("down")}onTick(){var t;if(this.powerManager.isGoingDown())this.shutdown();else if(this.powerManager.isRunning()){let i=(t=this.onNextTickFn)!=null?t:this.onTickFn;this.onNextTickFn=null,i()}else this.emit("error",new w("Unexpected call"))}abort(){this.aborted||(this.aborted=!0,this.powerManager.isGoingDown()?this.shutdown():this.quit())}quit(){this.powerManager.isGoingUp()?(this.powerManager.rollback(),this.emit("down")):this.aborted&&this.powerManager.isDown()?this.emit("down"):(this.powerManager.goingDown(),this.timeout?(clearTimeout(this.timeout),this.shutdown()):this.interval?(clearInterval(this.interval),this.shutdown()):this.aborted?this.shutdown():this.shutdownTimeout=setTimeout(()=>{this.powerManager.isGoingDown()&&this.shutdown()},6e4))}isTicking(){return!!(this.timeout||this.interval)}nextTick(){if(this.isTicking())throw new L("A timer is already running");this.powerManager.isGoingDown()?this.shutdown():(this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.timeout=setTimeout(()=>{this.timeout=null,this.onTick()},this.time)))}nextTickFn(t){this.onNextTickFn=t,this.nextTick()}runTimer(){if(this.isTicking())throw new L("A timer is already running");this.powerManager.isGoingUp()&&this.powerManager.commit(),this.powerManager.isRunning()&&(this.interval=setInterval(()=>this.onTick(),this.time))}};var _=class{constructor(r,t=1e3){this.ticker=null;this.powerManager=null;this.getTicker=()=>{if(!this.ticker)throw new y("Expected an instance of Ticker");return this.ticker};this.onTick=()=>{this.work(r=>{if(r)throw r;this.getTicker().nextTick()})};this.run=()=>{if(this.managed)throw new y("You can not run a managed worker");let r=this.getPowerManager();r.goingUp(),this.getTicker().nextTick(),r.commit()};this.quit=r=>{if(this.managed)r();else{let t=this.getPowerManager();t.goingDown();let i=this.getTicker();i.on("down",()=>{t.commit(),r()}),i.quit()}};this.managed=r,r||(this.ticker=new S(this.onTick,t),this.powerManager=new f)}getPowerManager(){if(!this.powerManager)throw new y("Expected an instance of PowerSwitch");return this.powerManager}};var Q=class extends P{constructor(t,i,e,n){super();this.onTick=()=>{b.waterfall([t=>{this.lock.acquireOrExtend((i,e)=>{e===2&&this.logger.info(`Workers are exclusively running from this instance (Lock ID ${this.lock.getId()}).`),t(i)})},t=>{this.workerPool.work(t)}],t=>{!t||t instanceof T?this.ticker.nextTick():this.emit("error",t)})};this.clearWorkerPool=t=>{this.workerPool.clear(t)};this.stopTicker=t=>{this.ticker.once("down",t),this.ticker.quit()};this.releaseLock=t=>{this.lock.releaseLock(t)};this.run=()=>{this.emit("up"),this.ticker.nextTick()};this.quit=t=>{b.waterfall([this.stopTicker,this.clearWorkerPool,this.releaseLock],()=>{this.emit("down"),t()})};this.powerManager=new f,this.redisClient=t,this.logger=n,this.lock=new F(t,i,6e4),this.ticker=new S(this.onTick),this.workerPool=e}addWorker(t){this.workerPool.add(t)}};var B=class{constructor(){this.pool=[];this.work=r=>{if(this.pool.length){let t=this.pool.map(i=>e=>i.work(e));b.waterfall(t,r)}else r()};this.add=r=>(this.pool.push(r),this.pool.length);this.clear=r=>{b.each(this.pool,(t,i,e)=>{t.quit(e)},()=>{this.pool=[],r()})}}};import{createLogger as pt}from"bunyan";var D=()=>{},kt={debug:D,warn:D,info:D,error:D},v=null;function vt(){v=null}function ft(o){if(v)throw new U("Logger has been already initialized.");v=o}function Ct(o,r){var t;if(!o.enabled)return kt;if(v||(v=pt(Z(z({},(t=o.options)!=null?t:{}),{name:"redis-smq"}))),r){let i=(e,n)=>(s,...l)=>{let a=typeof s=="string"?`${r} | ${s}`:s;return n[e](a,...l)};return{info:i("info",v),warn:i("warn",v),debug:i("debug",v),error:i("error",v)}}return v}var It={getLogger:Ct,setLogger:ft,destroy:vt};import{createClient as wt}from"@redis/client";import{WatchError as xt}from"@redis/client";var q=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lRem(r,t,i),this}lpop(r){return this.multi.lPop(r),this}lpush(r,t){return this.multi.lPush(r,t),this}ltrim(r,t,i){return this.multi.lTrim(r,t,i),this}rpop(r){return this.multi.rPop(r),this}rpush(r,t){return this.multi.rPush(r,t),this}zadd(r,t,i){return this.multi.zAdd(r,{score:t,value:i}),this}zrem(r,t){return this.multi.zRem(r,t),this}sadd(r,t){return this.multi.sAdd(r,t),this}srem(r,t){return this.multi.sRem(r,t),this}hset(r,t,i){return this.multi.hSet(r,t,i),this}hdel(r,t){return this.multi.hDel(r,t),this}hincrby(r,t,i){return this.multi.hIncrBy(r,t,i),this}pexpire(r,t){return this.multi.pExpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rPopLPush(r,t),this}del(r){return this.multi.del(r),this}exec(r){this.multi.exec().then(t=>r(null,t)).catch(t=>{t instanceof xt?r(new E):r(t)})}};var X=class extends k{constructor(t={}){super();this.client=wt(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")}),this.client.connect()}set(t,i,e,n){this.client.set(t,i,z(z({},e.expire?{[e.expire.mode]:e.expire.value}:{}),e.exists?{[e.exists]:!0}:{})).then(s=>n(null,s)).catch(n)}zadd(t,i,e,n){this.client.zAdd(t,{score:i,value:e}).then(s=>n(null,s)).catch(n)}multi(){return new q(this.client)}watch(t,i){this.client.watch(t).then(e=>i(null,e)).catch(i)}unwatch(t){this.client.unwatch().then(i=>t(null,i)).catch(t)}sismember(t,i,e){this.client.sIsMember(t,i).then(n=>e(null,Number(n))).catch(e)}zcard(t,i){this.client.zCard(t).then(e=>i(null,e)).catch(i)}zrange(t,i,e,n){this.client.zRange(t,i,e).then(s=>n(null,(Array.isArray(s)?s:[]).map(l=>String(l)))).catch(n)}zrevrange(t,i,e,n){this.client.sendCommand(["ZREVRANGE",t,String(i),String(e)]).then(s=>n(null,(Array.isArray(s)?s:[]).map(l=>String(l)))).catch(n)}zrem(t,i,e){this.client.zRem(t,i).then(n=>e(null,n)).catch(e)}psubscribe(t){this.client.pSubscribe(t,(i,e)=>{this.client.emit("pmessage",t,e,i)})}punsubscribe(t){this.client.pUnsubscribe(t).catch(()=>{})}subscribe(t){this.client.subscribe(t,(i,e)=>{this.client.emit("message",e,i)})}unsubscribe(t){this.client.unsubscribe(t).catch(()=>{})}zrangebyscore(t,i,e,n,s,l){this.client.zRangeByScore(t,i,e,{LIMIT:{offset:n,count:s}}).then(a=>l(null,a)).catch(l)}smembers(t,i){this.client.sMembers(t).then(e=>i(null,e)).catch(i)}sscan(t,i,e,n){let s=[t,Number(i),e];this.client.sScan(...s).then(({cursor:l,members:a})=>{n(null,{cursor:String(l),items:a})}).catch(n)}zscan(t,i,e,n){let s=[t,Number(i),e];this.client.zScan(...s).then(({cursor:l,members:a})=>{let c=new Set;for(let g of a)c.add(g.value);n(null,{cursor:String(l),items:[...c]})}).catch(n)}sadd(t,i,e){this.client.sAdd(t,i).then(n=>e(null,n)).catch(e)}srem(t,i,e){this.client.sRem(t,i).then(n=>e(null,n)).catch(e)}hgetall(t,i){this.client.hGetAll(t).then(e=>i(null,e)).catch(i)}hscan(t,i,e,n){let s=[t,Number(i),e];this.client.hScan(...s).then(({cursor:l,tuples:a})=>{let c={};for(;a.length;){let g=a.shift();g&&(c[g.field]=g.value)}n(null,{cursor:String(l),result:c})}).catch(n)}hget(t,i,e){this.client.hGet(t,i).then(n=>e(null,n!=null?n:null)).catch(e)}hset(t,i,e,n){this.client.hSet(t,i,e).then(s=>n(null,s)).catch(n)}hdel(t,i,e){this.client.hDel(t,i).then(n=>e(null,Number(n))).catch(e)}lrange(t,i,e,n){this.client.lRange(t,i,e).then(s=>n(null,s)).catch(n)}hkeys(t,i){this.client.hKeys(t).then(e=>i(null,e)).catch(i)}hlen(t,i){this.client.hLen(t).then(e=>i(null,e)).catch(i)}brpoplpush(t,i,e,n){this.client.brPopLPush(t,i,e).then(s=>n(null,s)).catch(n)}rpoplpush(t,i,e){this.client.rPopLPush(t,i).then(n=>e(null,n)).catch(e)}zrangebyscorewithscores(t,i,e,n){this.client.zRangeByScoreWithScores(t,i,e).then(s=>{let l={};for(let{score:a,value:c}of s)l[a]=c;n(null,l)}).catch(n)}rpop(t,i){this.client.rPop(t).then(e=>i(null,e)).catch(i)}lrem(t,i,e,n){this.client.lRem(t,i,e).then(s=>n(null,s)).catch(n)}publish(t,i,e){this.client.publish(t,i).then(n=>e(null,n)).catch(e)}flushall(t){this.client.flushAll().then(i=>t(null,i)).catch(t)}loadScript(t,i){this.client.sendCommand(["SCRIPT","LOAD",t]).then(e=>i(null,typeof e=="string"?e:String(e))).catch(i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.sendCommand(["evalsha",...n.concat(s).map(l=>String(l))]).then(l=>{Array.isArray(l)?e(null,l.map(a=>a instanceof Buffer?a.toString():a)):l instanceof Buffer?e(null,l.toString()):e(null,l)}).catch(e)}get(t,i){this.client.sendCommand(["get",t]).then(e=>i(null,typeof e=="string"?e:null)).catch(i)}del(t,i){this.client.del(t).then(e=>i(null,e)).catch(i)}llen(t,i){this.client.sendCommand(["llen",t]).then(e=>i(null,Number(e))).catch(i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lMove(t,i,e,n).then(l=>s(null,l)).catch(s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.sendCommand(["zremrangebyscore",t,`${i}`,`${e}`]).then(s=>n(null,Number(s))).catch(n)}hmget(t,i,e){this.client.hmGet(t,i).then(n=>e(null,n)).catch(e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect().catch(()=>{})}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info().then(i=>t(null,i)).catch(t)}on(t,i){return this.client.on(t,i),this}};import Tt from"ioredis";var G=class{constructor(r){this.multi=r.multi()}lrem(r,t,i){return this.multi.lrem(r,t,i),this}lpop(r){return this.multi.lpop(r),this}lpush(r,t){return this.multi.lpush(r,t),this}ltrim(r,t,i){return this.multi.ltrim(r,t,i),this}rpop(r){return this.multi.rpop(r),this}rpush(r,t){return this.multi.rpush(r,t),this}zadd(r,t,i){return this.multi.zadd(r,t,i),this}zrem(r,t){return this.multi.zrem(r,...typeof t=="string"?[t]:t),this}sadd(r,t){return this.multi.sadd(r,t),this}srem(r,t){return this.multi.srem(r,...typeof t=="string"?[t]:t),this}hset(r,t,i){return this.multi.hset(r,t,i),this}hdel(r,t){return this.multi.hdel(r,...typeof t=="string"?[t]:t),this}hincrby(r,t,i){return this.multi.hincrby(r,t,i),this}pexpire(r,t){return this.multi.pexpire(r,t),this}expire(r,t){return this.multi.expire(r,t),this}rpoplpush(r,t){return this.multi.rpoplpush(r,t),this}del(r){return this.multi.del(...typeof r=="string"?[r]:r),this}exec(r){this.multi.exec((t,i)=>{if(t)r(t);else if(!i)r(new E);else{let e=[],n=null;for(let s of i){if(!Array.isArray(s)){n=new u("Expected an array reply from multi.exec()");break}let[l,a]=s;if(l instanceof Error){n=l;break}e.push(a)}n?r(n):r(null,e)}})}};var V=class extends k{constructor(t={}){super();this.client=new Tt(t),this.client.once("ready",()=>{this.connectionClosed=!1,this.emit("ready")}),this.client.once("end",()=>{this.connectionClosed=!0,this.emit("end")})}set(t,i,e,n){e.exists&&e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,e.exists,n):e.expire?this.client.set(t,i,e.expire.mode,e.expire.value,n):e.exists?this.client.set(t,i,e.exists,n):this.client.set(t,i,n)}zadd(t,i,e,n){this.client.zadd(t,i,e,n)}multi(){return new G(this.client)}watch(t,i){this.client.watch(t,i)}unwatch(t){this.client.unwatch(t)}sismember(t,i,e){this.client.sismember(t,i,e)}sscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.sscan(...s,(l,[a,c])=>{l?n(l):n(null,{cursor:a,items:c})})}zcard(t,i){this.client.zcard(t,i)}zrange(t,i,e,n){this.client.zrange(t,i,e,n)}zscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.zscan(...s,(l,a)=>{if(l)n(l);else if(!a)n(new m);else{let c=new Set,[g,x]=a;for(;x.length;){let W=String(x.shift()),St=String(x.shift());c.add(W)}n(null,{cursor:g,items:[...c]})}})}zrevrange(t,i,e,n){this.client.zrevrange(t,i,e,n)}zrem(t,i,e){this.client.zrem(t,i,e)}psubscribe(t){this.client.psubscribe(t)}punsubscribe(t){this.client.punsubscribe(t)}subscribe(t){this.client.subscribe(t)}unsubscribe(t){this.client.unsubscribe(t)}zrangebyscore(t,i,e,n,s,l){this.client.zrangebyscore(t,i,e,"LIMIT",n,s,l)}smembers(t,i){this.client.smembers(t,i)}sadd(t,i,e){this.client.sadd(t,i,e)}srem(t,i,e){this.client.srem(t,i,e)}hgetall(t,i){this.client.hgetall(t,i)}hscan(t,i,e,n){let s=[t,i];e.MATCH&&s.push("MATCH",e.MATCH),e.COUNT&&s.push("COUNT",String(e.COUNT)),this.client.hscan(...s,(l,[a,c])=>{if(l)n(l);else{let g={};for(;c.length;){let x=String(c.shift());g[x]=String(c.shift())}n(null,{cursor:a,result:g})}})}hget(t,i,e){this.client.hget(t,i,e)}hset(t,i,e,n){this.client.hset(t,i,e,n)}hdel(t,i,e){this.client.hdel(t,i,e)}lrange(t,i,e,n){this.client.lrange(t,i,e,n)}hkeys(t,i){this.client.hkeys(t,i)}hlen(t,i){this.client.hlen(t,i)}brpoplpush(t,i,e,n){this.client.brpoplpush(t,i,e,n)}rpoplpush(t,i,e){this.client.rpoplpush(t,i,e)}zrangebyscorewithscores(t,i,e,n){this.client.zrangebyscore(t,i,e,"WITHSCORES",(s,l)=>{if(s)n(s);else{let a=l!=null?l:[],c={};for(let g=a.splice(0,2);g.length>0;g=a.splice(0,2)){let[x,W]=g;c[W]=x}n(null,c)}})}rpop(t,i){this.client.rpop(t,i)}lrem(t,i,e,n){this.client.lrem(t,i,e,n)}publish(t,i,e){this.client.publish(t,i,e)}flushall(t){this.client.flushall(t)}loadScript(t,i){this.client.script("load",t,i)}evalsha(t,i,e){let n=[t],s=Array.isArray(i)?i:[i];this.client.evalsha(n.concat(s),e)}get(t,i){this.client.get(t,i)}del(t,i){this.client.del(t,i)}llen(t,i){this.client.llen(t,i)}lmove(t,i,e,n,s){this.validateRedisVersion(6,2)?this.client.lmove(t,i,e,n,s):s(new u("Command not supported by your Redis server. Minimal required Redis server version is 6.2.0."))}zremrangebyscore(t,i,e,n){this.client.zremrangebyscore(t,i,e,n)}hmget(t,i,e){this.client.hmget(t,i,e)}halt(t){this.connectionClosed?t():(this.client.once("end",t),this.end())}end(){this.connectionClosed||this.client.disconnect(!1)}quit(t=()=>{}){this.connectionClosed?t():(this.client.once("end",t),this.client.quit())}getInfo(t){this.client.info(t)}on(t,i){return this.client.on(t,i),this}};function Et(o){return o.client==="redis"?new X(o.options):new V(o.options)}function yt(o,r){let t=Et(o);t.once("ready",()=>{b.waterfall([i=>t.validateRedisServerSupport(i),i=>t.loadScripts(i)],i=>{if(i)throw i;r(null,t)})})}var Rt={createInstance:yt};export{m as CallbackEmptyReplyError,K as CallbackInvalidReplyError,j as ERedisConfigClient,P as EventEmitter,F as Lock,C as LockAbortError,T as LockAcquireError,h as LockError,M as LockExtendError,O as LockMethodNotAllowedError,A as LockNotAcquiredError,H as LockNotReleasedError,U as LoggerError,w as PanicError,f as PowerSwitch,I as PowerSwitchError,k as RedisClient,u as RedisClientError,d as RedisSMQError,S as Ticker,L as TickerError,E as WatchedKeysChangedError,_ as Worker,y as WorkerError,B as WorkerPool,Q as WorkerRunner,b as async,It as logger,Rt as redis};
|
|
4
3
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redis-smq-common",
|
|
3
|
-
"version": "3.0.0-rc.
|
|
3
|
+
"version": "3.0.0-rc.9",
|
|
4
4
|
"description": "RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.",
|
|
5
5
|
"author": "Weyoss <weyoss@protonmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,12 +38,10 @@
|
|
|
38
38
|
"@redis/client": "1.1.0",
|
|
39
39
|
"@types/bunyan": "1.8.7",
|
|
40
40
|
"@types/ioredis": "4.28.1",
|
|
41
|
-
"@types/redis": "2.8.32",
|
|
42
41
|
"bluebird": "3.7.2",
|
|
43
42
|
"bunyan": "1.8.15",
|
|
44
43
|
"ioredis": "4.28.0",
|
|
45
44
|
"lodash": "4.17.21",
|
|
46
|
-
"redis": "3.1.2",
|
|
47
45
|
"uuid": "8.3.2"
|
|
48
46
|
},
|
|
49
47
|
"devDependencies": {
|