UNPKG

@litert/redis

Version:

A redis protocol implement for Node.js.

1,963 lines (1,962 loc) 53.6 kB
"use strict"; /** * Copyright 2025 Angus.Fenying <fenying@litert.org> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.COMMANDS = void 0; const U = require("./Utils"); const E = require("./Errors"); function isStringOK(data) { return 'OK' === (data?.toString()); } function isIntegerOne(data) { return 1 === data; } function createDefaultPreparer(cmd) { return (...args) => ({ args, cmd }); } exports.COMMANDS = { /** * Command: append * @see https://redis.io/docs/latest/commands/append */ 'append': { 'prepare': createDefaultPreparer('APPEND') }, /** * Command: auth * @see https://redis.io/docs/latest/commands/auth */ 'auth': { 'prepare': createDefaultPreparer('AUTH') }, /** * Command: ping * @see https://redis.io/docs/latest/commands/ping */ 'ping': { prepare(text) { return { 'cmd': 'PING', 'args': text ? [text] : [] }; }, process(data) { return data instanceof Buffer ? data.toString() : data; } }, /** * Command: hRandField * @see https://redis.io/docs/latest/commands/hrandfield */ 'hRandField': { prepare(key, count) { return { 'cmd': 'HRANDFIELD', 'args': [key, count] }; }, process: U.list2StringList }, /** * Command: hRandField * @see https://redis.io/docs/latest/commands/hrandfield */ 'hRandField$': { prepare(key, count) { return { 'cmd': 'HRANDFIELD', 'args': [key, count] }; }, process: U.list2BufferList }, /** * Command: hRandField * @see https://redis.io/docs/latest/commands/hrandfield */ 'hRandFieldWithValues': { prepare(key, count) { return { 'cmd': 'HRANDFIELD', 'args': [key, count, 'WITHVALUES'] }; }, process: U.pairList2NullableStringDict }, /** * Command: hRandField * @see https://redis.io/docs/latest/commands/hrandfield */ 'hRandFieldWithValues$': { prepare(key, count) { return { 'cmd': 'HRANDFIELD', 'args': [key, count, 'WITHVALUES'] }; }, process: U.pairList2NullableBufferDict }, /** * Command: incr * @see https://redis.io/docs/latest/commands/incr */ 'incr': { prepare(key, step = 1) { return { 'cmd': 'INCRBY', 'args': [key, step] }; }, // process: parseInt, // always returning integer }, /** * Command: incrByFloat * @see https://redis.io/docs/latest/commands/incrbyfloat */ 'incrByFloat': { prepare(key, step = 1) { return { 'cmd': 'INCRBYFLOAT', 'args': [key, step] }; }, process: parseFloat, }, /** * Command: decr * @see https://redis.io/docs/latest/commands/decr */ 'decr': { prepare(key, step = 1) { return { 'cmd': 'DECRBY', 'args': [key, step] }; }, // process: parseInt, // always returning integer }, /** * Command: incrByFloat * @see https://redis.io/docs/latest/commands/incrbyfloat */ 'decrByFloat': { prepare(key, step = 1) { return { 'cmd': 'INCRBYFLOAT', 'args': [key, -step] }; }, process: parseFloat, }, /** * Command: del * @see https://redis.io/docs/latest/commands/del */ 'del': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'DEL', args: keys }; } return { cmd: 'DEL', args: [keys] }; } }, /** * Command: unlink * @see https://redis.io/docs/latest/commands/unlink */ 'unlink': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'UNLINK', args: keys }; } return { cmd: 'UNLINK', args: [keys] }; } }, /** * Command: get * @see https://redis.io/docs/latest/commands/get */ 'get': { prepare: createDefaultPreparer('GET'), process: U.nullableBuffer2String }, /** * Command: get * @see https://redis.io/docs/latest/commands/get */ 'get$': { prepare: createDefaultPreparer('GET'), }, /** * Command: getdel * @see https://redis.io/docs/latest/commands/getdel */ 'getAndDel': { prepare: createDefaultPreparer('GETDEL'), process: U.nullableBuffer2String }, /** * Command: getdel * @see https://redis.io/docs/latest/commands/getdel */ 'getAndDel$': { prepare: createDefaultPreparer('GETDEL'), }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getEx': { prepare(key, seconds) { return { 'args': [key, 'EX', seconds], 'cmd': 'GETEX', }; }, process: U.nullableBuffer2String }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getEx$': { prepare(key, seconds) { return { 'args': [key, 'EX', seconds], 'cmd': 'GETEX' }; }, }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getExAt': { prepare(key, tsInSec) { return { 'args': [key, 'EXAT', tsInSec], 'cmd': 'GETEX', }; }, process: U.nullableBuffer2String }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getExAt$': { prepare(key, seconds) { return { 'args': [key, 'EXAT', seconds], 'cmd': 'GETEX' }; }, }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getPEx': { prepare(key, seconds) { return { 'args': [key, 'PX', seconds], 'cmd': 'GETEX', }; }, process: U.nullableBuffer2String }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getPEx$': { prepare(key, seconds) { return { 'args': [key, 'PX', seconds], 'cmd': 'GETEX' }; }, }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getPExAt': { prepare(key, tsInSec) { return { 'args': [key, 'PXAT', tsInSec], 'cmd': 'GETEX', }; }, process: U.nullableBuffer2String }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getPExAt$': { prepare(key, seconds) { return { 'args': [key, 'PXAT', seconds], 'cmd': 'GETEX' }; }, }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getAndPersist': { prepare(key) { return { 'args': [key, 'PERSIST'], 'cmd': 'GETEX', }; }, process: U.nullableBuffer2String }, /** * Command: getex * @see https://redis.io/docs/latest/commands/getex */ 'getAndPersist$': { prepare(key) { return { 'args': [key, 'PERSIST'], 'cmd': 'GETEX' }; }, }, /** * Command: getSet * @see https://redis.io/docs/latest/commands/getset */ 'getSet': { prepare: createDefaultPreparer('GETSET'), process: U.nullableBuffer2String }, /** * Command: getSet * @see https://redis.io/docs/latest/commands/getset */ 'getSet$': { prepare: createDefaultPreparer('GETSET'), }, /** * Command: set * @see https://redis.io/docs/latest/commands/set */ 'set': { prepare(k, v, ttl) { return { cmd: 'SET', args: ttl ? [k, v, 'EX', ttl] : [k, v] }; }, process: isStringOK }, /** * Command: setNX * @see https://redis.io/docs/latest/commands/setnx */ 'setNX': { prepare(k, v, ttl) { return { cmd: 'SET', args: ttl ? [k, v, 'EX', ttl, 'NX'] : [k, v, 'NX'] }; }, process: isStringOK }, /** * Command: setEX * @see https://redis.io/docs/latest/commands/setex */ 'setEX': { prepare(key, value, ttl) { return { cmd: 'SET', args: [key, value, 'EX', ttl] }; }, process: isStringOK }, /** * Command: pSetNX * @see https://redis.io/docs/latest/commands/psetnx */ 'pSetNX': { prepare(key, value, ttl) { return { cmd: 'SET', args: [key, value, 'PX', ttl, 'NX'] }; }, process: isStringOK }, /** * Command: pSetEx * @see https://redis.io/docs/latest/commands/psetex */ 'pSetEx': { prepare(key, value, ttl) { return { cmd: 'SET', args: [key, value, 'PX', ttl] }; }, process: isStringOK }, /** * Command: replace * @see https://redis.io/docs/latest/commands/replace */ 'replace': { prepare(key, value, ttl) { return { cmd: 'SET', args: ttl ? [key, value, 'EX', ttl, 'XX'] : [key, value, 'XX'] }; }, process: isStringOK }, /** * Command: pReplace * @see https://redis.io/docs/latest/commands/preplace */ 'pReplace': { prepare(key, value, ttl) { return { cmd: 'SET', args: ttl ? [key, value, 'PX', ttl, 'XX'] : [key, value, 'XX'] }; }, process: isStringOK }, /** * Command: ttl * @see https://redis.io/docs/latest/commands/ttl */ 'ttl': { prepare: createDefaultPreparer('TTL') }, /** * Command: pTTL * @see https://redis.io/docs/latest/commands/pttl */ 'pTTL': { prepare: createDefaultPreparer('PTTL') }, /** * Command: expire * @see https://redis.io/docs/latest/commands/expire */ 'expire': { prepare: createDefaultPreparer('EXPIRE'), process: isIntegerOne }, /** * Command: pExpire * @see https://redis.io/docs/latest/commands/pexpire */ 'pExpire': { prepare: createDefaultPreparer('PEXPIRE'), process: isIntegerOne }, /** * Command: expireAt * @see https://redis.io/docs/latest/commands/expireat */ 'expireAt': { prepare: createDefaultPreparer('EXPIREAT'), process: isIntegerOne }, /** * Command: pExpireAt * @see https://redis.io/docs/latest/commands/pexpireat */ 'pExpireAt': { prepare: createDefaultPreparer('PEXPIREAT'), process: isIntegerOne }, /** * Command: persist * @see https://redis.io/docs/latest/commands/persist */ 'persist': { prepare: createDefaultPreparer('PERSIST'), process: isIntegerOne }, /** * Command: exists * @see https://redis.io/docs/latest/commands/exists */ 'exists': { prepare: createDefaultPreparer('EXISTS'), process: isIntegerOne }, /** * Command: exists * @see https://redis.io/docs/latest/commands/exists */ 'mExists': { prepare(keys) { return { cmd: 'EXISTS', args: keys }; } }, /** * Command: type * @see https://redis.io/docs/latest/commands/type */ 'type': { prepare: createDefaultPreparer('TYPE'), process: U.buffer2String }, /** * Command: move * @see https://redis.io/docs/latest/commands/move */ 'move': { prepare: createDefaultPreparer('MOVE'), process: isIntegerOne }, /** * Command: swapdb * @see https://redis.io/docs/latest/commands/swapdb */ 'swapDB': { prepare: createDefaultPreparer('SWAPDB'), process: isStringOK }, /** * Command: copy * @see https://redis.io/docs/latest/commands/copy */ 'copy': { prepare(srcKey, destKey, destDB = null, overwrite = false) { const ret = { cmd: 'COPY', args: [srcKey, destKey] }; if (destDB !== null) { ret.args.push('DB', destDB); } if (overwrite) { ret.args.push('REPLACE'); } return ret; }, process: isIntegerOne }, /** * Command: randomKey * @see https://redis.io/docs/latest/commands/randomkey */ 'randomKey': { prepare: createDefaultPreparer('RANDOMKEY'), process: U.buffer2String }, /** * Command: rename * @see https://redis.io/docs/latest/commands/rename */ 'rename': { prepare: createDefaultPreparer('RENAME'), process: null }, /** * Command: renameNX * @see https://redis.io/docs/latest/commands/renamenx */ 'renameNX': { prepare: createDefaultPreparer('RENAMENX'), process: isIntegerOne }, /** * Command: select * @see https://redis.io/docs/latest/commands/select */ 'select': { prepare: createDefaultPreparer('SELECT'), process: null }, /** * Command: flushDb * @see https://redis.io/docs/latest/commands/flushdb */ 'flushDb': { prepare(async = false) { return { cmd: 'FLUSHDB', args: async ? ['ASYNC'] : [] }; }, process: null }, /** * Command: flushAll * @see https://redis.io/docs/latest/commands/flushall */ 'flushAll': { prepare(async = false) { return { cmd: 'FLUSHALL', args: async ? ['ASYNC'] : [] }; }, process: null }, /** * Command: hDel * @see https://redis.io/docs/latest/commands/hdel */ 'hDel': { prepare(key, fields) { if (Array.isArray(fields)) { if (!fields.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'HDEL', args: [key, ...fields] }; } return { cmd: 'HDEL', args: [key, fields] }; } }, /** * Command: hGet * @see https://redis.io/docs/latest/commands/hget */ 'hGet': { prepare: createDefaultPreparer('HGET'), process: U.nullableBuffer2String }, /** * Command: hGet * @see https://redis.io/docs/latest/commands/hget */ 'hGet$': { prepare: createDefaultPreparer('HGET') }, /** * Command: hSet * @see https://redis.io/docs/latest/commands/hset */ 'hSet': { prepare: createDefaultPreparer('HSET'), process: isIntegerOne }, /** * Command: hSetNX * @see https://redis.io/docs/latest/commands/hsetnx */ 'hSetNX': { prepare: createDefaultPreparer('HSETNX'), process: isIntegerOne }, /** * Command: hExists * @see https://redis.io/docs/latest/commands/hexists */ 'hExists': { prepare: createDefaultPreparer('HEXISTS'), process: isIntegerOne }, /** * Command: keys * @see https://redis.io/docs/latest/commands/keys */ 'keys': { prepare: createDefaultPreparer('KEYS'), process: U.list2StringList }, /** * Command: dump * @see https://redis.io/docs/latest/commands/dump */ 'dump': { prepare: createDefaultPreparer('DUMP') }, /** * Command: restore * @see https://redis.io/docs/latest/commands/restore */ 'restore': { prepare(key, value, ttl, replace, absTTL, freq, idleTime) { const args = [key, ttl, value]; if (replace) { args.push('REPLACE'); } if (absTTL) { args.push('ABSTTL'); } if (idleTime) { args.push('IDLETIME', idleTime); } if (freq) { args.push('FREQ', freq); } return { cmd: 'RESTORE', args }; }, process: null }, /** * Command: strLen * @see https://redis.io/docs/latest/commands/strlen */ 'strLen': { prepare: createDefaultPreparer('STRLEN') }, /** * Command: touch * @see https://redis.io/docs/latest/commands/touch */ 'touch': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'TOUCH', args: keys }; } return { cmd: 'TOUCH', args: [keys] }; } }, /** * Command: mGet * @see https://redis.io/docs/latest/commands/mget */ 'mGet': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'MGET', args: keys }; } return { cmd: 'MGET', args: [keys] }; }, process(data, args) { return U.list2NullableStringDict(args, data); } }, /** * Command: mGet * @see https://redis.io/docs/latest/commands/mget */ 'mGet$': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'MGET', args: keys }; } return { cmd: 'MGET', args: [keys] }; }, process(data, args) { return U.list2NullableBufferDict(args, data); } }, /** * Command: mSet * @see https://redis.io/docs/latest/commands/mset */ 'mSet': { prepare(kv) { const args = []; for (const k in kv) { args.push(k, kv[k]); } return { cmd: 'MSET', args }; }, process: null }, /** * Command: mSetNX * @see https://redis.io/docs/latest/commands/msetnx */ 'mSetNX': { prepare(kv) { const args = []; for (const k in kv) { args.push(k, kv[k]); } return { cmd: 'MSETNX', args }; }, process: isIntegerOne }, /** * Command: hIncr * @see https://redis.io/docs/latest/commands/hincrby */ 'hIncr': { prepare(key, field, step = 1) { return { cmd: 'HINCRBY', args: [key, field, step] }; }, // process: parseInt, // always returning integer }, /** * Command: hIncrByFloat * @see https://redis.io/docs/latest/commands/hincrbyfloat */ 'hIncrByFloat': { prepare(key, field, step = 1) { return { cmd: 'HINCRBYFLOAT', args: [key, field, step] }; }, process: parseFloat, }, /** * Command: hIncr * @see https://redis.io/docs/latest/commands/hincrby */ 'hDecr': { prepare(key, field, step = 1) { return { cmd: 'HINCRBY', args: [key, field, -step] }; }, // process: parseInt, // always returning integer }, /** * Command: hIncrByFloat * @see https://redis.io/docs/latest/commands/hincrbyfloat */ 'hDecrByFloat': { prepare(key, field, step = 1) { return { cmd: 'HINCRBYFLOAT', args: [key, field, -step] }; }, process: parseFloat }, /** * Command: hKeys * @see https://redis.io/docs/latest/commands/hkeys */ 'hKeys': { prepare: createDefaultPreparer('HKEYS'), process: U.list2StringList }, /** * Command: hVals * @see https://redis.io/docs/latest/commands/hvals */ 'hVals': { prepare: createDefaultPreparer('HVALS'), process: U.list2StringList }, /** * Command: hVals * @see https://redis.io/docs/latest/commands/hvals */ 'hVals$': { prepare: createDefaultPreparer('HVALS'), process: U.list2BufferList }, /** * Command: hLen * @see https://redis.io/docs/latest/commands/hlen */ 'hLen': { prepare: createDefaultPreparer('HLEN') }, /** * Command: hMGet * @see https://redis.io/docs/latest/commands/hmget */ 'hMGet': { prepare(key, fields) { if (Array.isArray(fields)) { if (!fields.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'HMGET', args: [key, ...fields] }; } return { cmd: 'HMGET', args: [key, fields] }; }, process(data, args) { return U.list2NullableStringDict(args.slice(1), data); } }, /** * Command: hMGet * @see https://redis.io/docs/latest/commands/hmget */ 'hMGet$': { prepare(key, fields) { if (Array.isArray(fields)) { if (!fields.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'HMGET', args: [key, ...fields] }; } return { cmd: 'HMGET', args: [key, fields] }; }, process(data, args) { return U.list2NullableBufferDict(args.slice(1), data); } }, /** * Command: hGetAll * @see https://redis.io/docs/latest/commands/hgetall */ 'hGetAll': { prepare: createDefaultPreparer('HGETALL'), process: U.pairList2NullableStringDict }, /** * Command: hGetAll * @see https://redis.io/docs/latest/commands/hgetall */ 'hGetAll$': { prepare: createDefaultPreparer('HGETALL'), process: U.pairList2NullableBufferDict }, /** * Command: hMSet * @see https://redis.io/docs/latest/commands/hmset */ 'hMSet': { prepare(k, fv) { const args = [k]; for (const f in fv) { args.push(f, fv[f]); } return { cmd: 'HMSET', args }; }, process: null }, /** * Command: hStrLen * @see https://redis.io/docs/latest/commands/hstrlen */ 'hStrLen': { prepare: createDefaultPreparer('HSTRLEN') }, /** * Command: scan * @see https://redis.io/docs/latest/commands/scan */ 'scan': { prepare(cur, p, cn) { const args = [cur]; if (p) { args.push('MATCH', p); } if (cn) { args.push('COUNT', cn); } return { cmd: 'SCAN', args }; }, process(data) { return { 'nextCursor': parseInt(data[0][1].toString()), 'items': U.list2StringList(data[1][1]) }; } }, /** * Command: hScan * @see https://redis.io/docs/latest/commands/hscan */ 'hScan': { prepare(k, cur, p, cn) { const args = [k, cur]; if (p) { args.push('MATCH', p); } if (cn) { args.push('COUNT', cn); } return { cmd: 'HSCAN', args }; }, process(data) { return { 'nextCursor': parseInt(data[0][1].toString()), 'items': U.list2StringList(data[1][1]) }; } }, /** * Command: sAdd * @see https://redis.io/docs/latest/commands/sadd */ 'sAdd': { prepare(key, values) { return { cmd: 'SADD', args: [key, ...values] }; } }, /** * Command: sCard * @see https://redis.io/docs/latest/commands/scard */ 'sCard': { prepare: createDefaultPreparer('SCARD') }, /** * Command: sDiff * @see https://redis.io/docs/latest/commands/sdiff */ 'sDiff': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SDIFF', args: keys }; } return { cmd: 'SDIFF', args: [keys] }; }, process: U.list2StringList }, /** * Command: sDiff * @see https://redis.io/docs/latest/commands/sdiff */ 'sDiff$': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SDIFF', args: keys }; } return { cmd: 'SDIFF', args: [keys] }; }, process: U.list2BufferList }, /** * Command: sDiffStore * @see https://redis.io/docs/latest/commands/sdiffstore */ 'sDiffStore': { prepare(keys, target) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SDIFFSTORE', args: [target, ...keys] }; } return { cmd: 'SDIFFSTORE', args: [keys] }; } }, /** * Command: sInter * @see https://redis.io/docs/latest/commands/sinter */ 'sInter': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SINTER', args: keys }; } return { cmd: 'SINTER', args: [keys] }; }, process: U.list2StringList }, /** * Command: sInter * @see https://redis.io/docs/latest/commands/sinter */ 'sInter$': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SINTER', args: keys }; } return { cmd: 'SINTER', args: [keys] }; }, process: U.list2BufferList }, /** * Command: sInterStore * @see https://redis.io/docs/latest/commands/sinterstore */ 'sInterStore': { prepare(keys, target) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SINTERSTORE', args: [target, ...keys] }; } return { cmd: 'SINTERSTORE', args: [keys] }; } }, /** * Command: sUnion * @see https://redis.io/docs/latest/commands/sunion */ 'sUnion': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SUNION', args: keys }; } return { cmd: 'SUNION', args: [keys] }; }, process: U.list2StringList }, /** * Command: sUnion * @see https://redis.io/docs/latest/commands/sunion */ 'sUnion$': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SUNION', args: keys }; } return { cmd: 'SUNION', args: [keys] }; }, process: U.list2BufferList }, /** * Command: sUnionStore * @see https://redis.io/docs/latest/commands/sunionstore */ 'sUnionStore': { prepare(keys) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'SUNIONSTORE', args: keys }; } return { cmd: 'SUNIONSTORE', args: [keys] }; } }, /** * Command: sIsMember * @see https://redis.io/docs/latest/commands/sismember */ 'sIsMember': { prepare: createDefaultPreparer('SISMEMBER'), process: isIntegerOne }, /** * Command: sMembers * @see https://redis.io/docs/latest/commands/smembers */ 'sMembers': { prepare: createDefaultPreparer('SMEMBERS'), process: U.list2StringList }, /** * Command: sMembers * @see https://redis.io/docs/latest/commands/smembers */ 'sMembers$': { prepare: createDefaultPreparer('SMEMBERS'), process: U.list2BufferList }, /** * Command: sMove * @see https://redis.io/docs/latest/commands/smove */ 'sMove': { prepare: createDefaultPreparer('SMOVE'), process: isIntegerOne }, /** * Command: sPop * @see https://redis.io/docs/latest/commands/spop */ 'sPop': { prepare: createDefaultPreparer('SPOP'), process: U.list2StringList }, /** * Command: sPop * @see https://redis.io/docs/latest/commands/spop */ 'sPop$': { prepare: createDefaultPreparer('SPOP'), process: U.list2BufferList }, /** * Command: sRandMember * @see https://redis.io/docs/latest/commands/srandmember */ 'sRandMember': { prepare: createDefaultPreparer('SRANDMEMBER'), process: U.list2StringList }, /** * Command: sRandMember * @see https://redis.io/docs/latest/commands/srandmember */ 'sRandMember$': { prepare: createDefaultPreparer('SRANDMEMBER'), process: U.list2BufferList }, /** * Command: sRem * @see https://redis.io/docs/latest/commands/srem */ 'sRem': { prepare: (key, members) => ({ args: [key, ...members], cmd: 'SREM' }) }, /** * Command: sScan * @see https://redis.io/docs/latest/commands/sscan */ 'sScan': { prepare(k, cur, p, cn) { const args = [k, cur]; if (p) { args.push('MATCH', p); } if (cn) { args.push('COUNT', cn); } return { cmd: 'SSCAN', args }; }, process(data) { return { 'nextCursor': parseInt(data[0][1].toString()), 'items': U.list2StringList(data[1][1]) }; } }, /** * Command: sScan * @see https://redis.io/docs/latest/commands/sscan */ 'sScan$': { prepare(k, cur, p, cn) { const args = [k, cur]; if (p) { args.push('MATCH', p); } if (cn) { args.push('COUNT', cn); } return { cmd: 'SSCAN', args }; }, process(data) { return { 'nextCursor': parseInt(data[0][1].toString()), 'items': U.list2StringList(data[1][1]) }; } }, /** * Command: bLPop * @see https://redis.io/docs/latest/commands/blpop */ 'bLPop': { prepare(keys, timeout) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'BLPOP', args: [...keys, timeout] }; } return { cmd: 'BLPOP', args: [keys, timeout] }; }, process: U.pairList2StringDict }, /** * Command: bLPop * @see https://redis.io/docs/latest/commands/blpop */ 'bLPop$': { prepare(keys, timeout) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'BLPOP', args: [...keys, timeout] }; } return { cmd: 'BLPOP', args: [keys, timeout] }; }, process: U.pairList2BufferDict }, /** * Command: bRPop * @see https://redis.io/docs/latest/commands/brpop */ 'bRPop': { prepare(keys, timeout) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'BRPOP', args: [...keys, timeout] }; } return { cmd: 'BRPOP', args: [keys, timeout] }; }, process: U.pairList2StringDict }, /** * Command: bRPop * @see https://redis.io/docs/latest/commands/brpop */ 'bRPop$': { prepare(keys, timeout) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'BRPOP', args: [...keys, timeout] }; } return { cmd: 'BRPOP', args: [keys, timeout] }; }, process: U.pairList2BufferDict }, /** * Command: bRPopLPush * @see https://redis.io/docs/latest/commands/brpoplpush */ 'bRPopLPush': { prepare: createDefaultPreparer('BRPOPLPUSH'), process: U.buffer2String }, /** * Command: bRPopLPush * @see https://redis.io/docs/latest/commands/brpoplpush */ 'bRPopLPush$': { prepare: createDefaultPreparer('BRPOPLPUSH') }, /** * Command: lIndex * @see https://redis.io/docs/latest/commands/lindex */ 'lIndex': { prepare: createDefaultPreparer('LINDEX'), process: U.buffer2String }, /** * Command: lIndex * @see https://redis.io/docs/latest/commands/lindex */ 'lIndex$': { prepare: createDefaultPreparer('LINDEX') }, /** * Command: lInsert * @see https://redis.io/docs/latest/commands/linsert */ 'lInsertBefore': { prepare(key, pivot, value) { return { args: [key, 'BEFORE', pivot, value], cmd: 'LINSERT' }; } }, /** * Command: lInsert * @see https://redis.io/docs/latest/commands/linsert */ 'lInsertAfter': { prepare(key, pivot, value) { return { args: [key, 'AFTER', pivot, value], cmd: 'LINSERT' }; } }, /** * Command: lLen * @see https://redis.io/docs/latest/commands/llen */ 'lLen': { prepare: createDefaultPreparer('LLEN') }, /** * Command: lPop * @see https://redis.io/docs/latest/commands/lpop */ 'lPop': { prepare(key, count) { return { args: count === undefined ? [key] : [key, count], cmd: 'LPOP' }; }, process(data, args) { switch (args.length) { case 1: return data?.toString() ?? null; default: return U.list2StringList(data); } } }, /** * Command: lPop * @see https://redis.io/docs/latest/commands/lpop */ 'lPop$': { prepare(key, count) { return { args: count === undefined ? [key] : [key, count], cmd: 'LPOP' }; }, process(data, args) { switch (args.length) { case 1: return data ?? null; default: return U.list2BufferList(data); } } }, /** * Command: lPush * @see https://redis.io/docs/latest/commands/lpush */ 'lPush': { prepare(key, values) { return { args: [key, ...values], cmd: 'LPUSH' }; } }, /** * Command: lPushX * @see https://redis.io/docs/latest/commands/lpushx */ 'lPushX': { prepare(key, values) { return { args: [key, ...values], cmd: 'LPUSHX' }; } }, /** * Command: lRange * @see https://redis.io/docs/latest/commands/lrange */ 'lRange': { prepare: createDefaultPreparer('LRANGE'), process: U.list2StringList }, /** * Command: lRange * @see https://redis.io/docs/latest/commands/lrange */ 'lRange$': { prepare: createDefaultPreparer('LRANGE'), process: U.list2BufferList }, /** * Command: lRem * @see https://redis.io/docs/latest/commands/lrem */ 'lRem': { prepare: createDefaultPreparer('LREM') }, /** * Command: lSet * @see https://redis.io/docs/latest/commands/lset */ 'lSet': { prepare: createDefaultPreparer('LSET'), process: null }, /** * Command: lTrim * @see https://redis.io/docs/latest/commands/ltrim */ 'lTrim': { prepare: createDefaultPreparer('LTRIM'), process: null }, /** * Command: rPop * @see https://redis.io/docs/latest/commands/rpop */ 'rPop': { prepare(key, count) { return { args: count === undefined ? [key] : [key, count], cmd: 'RPOP' }; }, process(data, args) { switch (args.length) { case 1: return data?.toString() ?? null; default: return U.list2StringList(data); } } }, /** * Command: rPop * @see https://redis.io/docs/latest/commands/rpop */ 'rPop$': { prepare(key, count) { return { args: count === undefined ? [key] : [key, count], cmd: 'RPOP' }; }, process(data, args) { switch (args.length) { case 1: return data ?? null; default: return U.list2BufferList(data); } } }, /** * Command: rPopLPush * @see https://redis.io/docs/latest/commands/rpoplpush */ 'rPopLPush': { prepare: createDefaultPreparer('RPOPLPUSH'), process: U.buffer2String }, /** * Command: rPopLPush * @see https://redis.io/docs/latest/commands/rpoplpush */ 'rPopLPush$': { prepare: createDefaultPreparer('RPOPLPUSH') }, /** * Command: rPush * @see https://redis.io/docs/latest/commands/rpush */ 'rPush': { prepare(key, values) { return { args: [key, ...values], cmd: 'RPUSH' }; } }, /** * Command: rPushX * @see https://redis.io/docs/latest/commands/rpushx */ 'rPushX': { prepare(key, values) { return { args: [key, ...values], cmd: 'RPUSHX' }; } }, /** * Command: lMove * @see https://redis.io/docs/latest/commands/lmove */ 'lMove': { prepare(srcKey, destKey, srcDir, destDir) { return { cmd: 'LMOVE', args: [srcKey, destKey, srcDir, destDir] }; }, process: U.nullableBuffer2String }, /** * Command: lMove * @see https://redis.io/docs/latest/commands/lmove */ 'lMove$': { prepare(srcKey, destKey, srcDir, destDir) { return { cmd: 'LMOVE', args: [srcKey, destKey, srcDir, destDir] }; } }, /** * Command: bLMove * @see https://redis.io/docs/latest/commands/blmove */ 'bLMove': { prepare(srcKey, destKey, srcDir, destDir, timeout) { return { cmd: 'BLMOVE', args: [srcKey, destKey, srcDir, destDir, timeout] }; }, process: U.nullableBuffer2String }, /** * Command: bLMove * @see https://redis.io/docs/latest/commands/blmove */ 'bLMove$': { prepare(srcKey, destKey, srcDir, destDir, timeout) { return { cmd: 'BLMOVE', args: [srcKey, destKey, srcDir, destDir, timeout] }; } }, /** * Command: zRem * @see https://redis.io/docs/latest/commands/zrem */ 'zRem': { prepare: (key, members) => ({ args: [key, ...members], cmd: 'ZREM' }) }, /** * Command: zAdd * @see https://redis.io/docs/latest/commands/zadd */ 'zAdd': { prepare(key, scoreOrItems, memberOrOptions) { const args = [key]; // --- zAdd(key, score, member) --- if (typeof scoreOrItems === 'number') { args.push(scoreOrItems, memberOrOptions); return { 'cmd': 'ZADD', 'args': args, 'ctx': { 'legacy': true } }; } // --- zAdd(key, elements, options?) --- const options = memberOrOptions; if (options?.mode) { args.push(options.mode); } if (options?.updateIf) { args.push(options.updateIf); } if (options?.returnChanged) { args.push('CH'); } if (options?.incr) { args.push('INCR'); } for (const item of scoreOrItems) { args.push(item.score, item.member); } return { 'cmd': 'ZADD', 'args': args }; }, process(data, args, ctx) { // old: boolean if (ctx?.['legacy']) { return data === 1; } // INCR: parse to number if (data instanceof Buffer) { return parseFloat(data.toString()); } // New calling method without INCR: returns number return data; } }, /** * Command: zRange * @see https://redis.io/docs/latest/commands/zrange */ 'zRangeWithScores': { prepare: (key, start, stop, options) => { const args = [key, start, stop]; if (options?.by) { args.push('BY' + options.by); } if (options?.reverse) { args.push('REV'); } if (options?.offset !== undefined && options?.count !== undefined) { args.push('LIMIT', options.offset, options.count); } args.push('WITHSCORES'); return { 'cmd': 'ZRANGE', 'args': args }; }, process: (items) => { const ret = []; for (let i = 0; i < items.length; i = i + 2) { ret.push({ 'member': items[i][1].toString(), 'score': parseFloat(items[i + 1][1].toString()) }); } return ret; } }, /** * Command: zRange * @see https://redis.io/docs/latest/commands/zrange */ 'zRangeWithScores$': { prepare: (key, start, stop, options) => { const args = [key, start, stop]; if (options?.by) { args.push('BY' + options.by); } if (options?.reverse) { args.push('REV'); } if (options?.offset !== undefined && options?.count !== undefined) { args.push('LIMIT', options.offset, options.count); } args.push('WITHSCORES'); return { 'cmd': 'ZRANGE', 'args': args }; }, process: (items) => { const ret = []; for (let i = 0; i < items.length; i = i + 2) { ret.push({ 'member': items[i][1], 'score': parseFloat(items[i + 1][1].toString()) }); } return ret; } }, /** * Command: pfAdd * @see https://redis.io/docs/latest/commands/pfadd */ 'pfAdd': { prepare(key, values) { return { args: [key, ...values], cmd: 'PFADD' }; }, process: isIntegerOne }, /** * Command: pfCount * @see https://redis.io/docs/latest/commands/pfcount */ 'pfCount': { prepare: createDefaultPreparer('PFCOUNT') }, /** * Command: pfMerge * @see https://redis.io/docs/latest/commands/pfmerge */ 'pfMerge': { prepare(keys, target) { if (Array.isArray(keys)) { if (!keys.length) { throw new E.E_INVALID_PARAM(); } return { cmd: 'PFMERGE', args: [target, ...keys] }; } return { cmd: 'PFMERGE', args: [target, keys] }; }, process: null }, /** * Command: publish * @see https://redis.io/docs/latest/commands/publish */ 'publish': { prepare: createDefaultPreparer('PUBLISH') }, 'pubSubChannels': { prepare(p) { return {