1
0

index.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. "use strict";
  2. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  5. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  6. };
  7. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  8. if (kind === "m") throw new TypeError("Private method is not writable");
  9. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  10. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  11. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  12. };
  13. var _RedisClient_instances, _a, _RedisClient_options, _RedisClient_socket, _RedisClient_queue, _RedisClient_isolationPool, _RedisClient_v4, _RedisClient_selectedDB, _RedisClient_initiateOptions, _RedisClient_initiateQueue, _RedisClient_initiateSocket, _RedisClient_initiateIsolationPool, _RedisClient_legacyMode, _RedisClient_legacySendCommand, _RedisClient_defineLegacyCommand, _RedisClient_pingTimer, _RedisClient_setPingTimer, _RedisClient_sendCommand, _RedisClient_pubSubCommand, _RedisClient_tick, _RedisClient_addMultiCommands, _RedisClient_destroyIsolationPool;
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. const commands_1 = require("./commands");
  16. const socket_1 = require("./socket");
  17. const commands_queue_1 = require("./commands-queue");
  18. const multi_command_1 = require("./multi-command");
  19. const events_1 = require("events");
  20. const command_options_1 = require("../command-options");
  21. const commander_1 = require("../commander");
  22. const generic_pool_1 = require("generic-pool");
  23. const errors_1 = require("../errors");
  24. const url_1 = require("url");
  25. const pub_sub_1 = require("./pub-sub");
  26. const package_json_1 = require("../../package.json");
  27. class RedisClient extends events_1.EventEmitter {
  28. static commandOptions(options) {
  29. return (0, command_options_1.commandOptions)(options);
  30. }
  31. static extend(extensions) {
  32. const Client = (0, commander_1.attachExtensions)({
  33. BaseClass: _a,
  34. modulesExecutor: _a.prototype.commandsExecutor,
  35. modules: extensions?.modules,
  36. functionsExecutor: _a.prototype.functionsExecuter,
  37. functions: extensions?.functions,
  38. scriptsExecutor: _a.prototype.scriptsExecuter,
  39. scripts: extensions?.scripts
  40. });
  41. if (Client !== _a) {
  42. Client.prototype.Multi = multi_command_1.default.extend(extensions);
  43. }
  44. return Client;
  45. }
  46. static create(options) {
  47. return new (_a.extend(options))(options);
  48. }
  49. static parseURL(url) {
  50. // https://www.iana.org/assignments/uri-schemes/prov/redis
  51. const { hostname, port, protocol, username, password, pathname } = new url_1.URL(url), parsed = {
  52. socket: {
  53. host: hostname
  54. }
  55. };
  56. if (protocol === 'rediss:') {
  57. parsed.socket.tls = true;
  58. }
  59. else if (protocol !== 'redis:') {
  60. throw new TypeError('Invalid protocol');
  61. }
  62. if (port) {
  63. parsed.socket.port = Number(port);
  64. }
  65. if (username) {
  66. parsed.username = decodeURIComponent(username);
  67. }
  68. if (password) {
  69. parsed.password = decodeURIComponent(password);
  70. }
  71. if (pathname.length > 1) {
  72. const database = Number(pathname.substring(1));
  73. if (isNaN(database)) {
  74. throw new TypeError('Invalid pathname');
  75. }
  76. parsed.database = database;
  77. }
  78. return parsed;
  79. }
  80. get options() {
  81. return __classPrivateFieldGet(this, _RedisClient_options, "f");
  82. }
  83. get isOpen() {
  84. return __classPrivateFieldGet(this, _RedisClient_socket, "f").isOpen;
  85. }
  86. get isReady() {
  87. return __classPrivateFieldGet(this, _RedisClient_socket, "f").isReady;
  88. }
  89. get isPubSubActive() {
  90. return __classPrivateFieldGet(this, _RedisClient_queue, "f").isPubSubActive;
  91. }
  92. get v4() {
  93. if (!__classPrivateFieldGet(this, _RedisClient_options, "f")?.legacyMode) {
  94. throw new Error('the client is not in "legacy mode"');
  95. }
  96. return __classPrivateFieldGet(this, _RedisClient_v4, "f");
  97. }
  98. constructor(options) {
  99. super();
  100. _RedisClient_instances.add(this);
  101. Object.defineProperty(this, "commandOptions", {
  102. enumerable: true,
  103. configurable: true,
  104. writable: true,
  105. value: _a.commandOptions
  106. });
  107. _RedisClient_options.set(this, void 0);
  108. _RedisClient_socket.set(this, void 0);
  109. _RedisClient_queue.set(this, void 0);
  110. _RedisClient_isolationPool.set(this, void 0);
  111. _RedisClient_v4.set(this, {});
  112. _RedisClient_selectedDB.set(this, 0);
  113. _RedisClient_pingTimer.set(this, void 0);
  114. Object.defineProperty(this, "select", {
  115. enumerable: true,
  116. configurable: true,
  117. writable: true,
  118. value: this.SELECT
  119. });
  120. Object.defineProperty(this, "subscribe", {
  121. enumerable: true,
  122. configurable: true,
  123. writable: true,
  124. value: this.SUBSCRIBE
  125. });
  126. Object.defineProperty(this, "unsubscribe", {
  127. enumerable: true,
  128. configurable: true,
  129. writable: true,
  130. value: this.UNSUBSCRIBE
  131. });
  132. Object.defineProperty(this, "pSubscribe", {
  133. enumerable: true,
  134. configurable: true,
  135. writable: true,
  136. value: this.PSUBSCRIBE
  137. });
  138. Object.defineProperty(this, "pUnsubscribe", {
  139. enumerable: true,
  140. configurable: true,
  141. writable: true,
  142. value: this.PUNSUBSCRIBE
  143. });
  144. Object.defineProperty(this, "sSubscribe", {
  145. enumerable: true,
  146. configurable: true,
  147. writable: true,
  148. value: this.SSUBSCRIBE
  149. });
  150. Object.defineProperty(this, "sUnsubscribe", {
  151. enumerable: true,
  152. configurable: true,
  153. writable: true,
  154. value: this.SUNSUBSCRIBE
  155. });
  156. Object.defineProperty(this, "quit", {
  157. enumerable: true,
  158. configurable: true,
  159. writable: true,
  160. value: this.QUIT
  161. });
  162. Object.defineProperty(this, "multi", {
  163. enumerable: true,
  164. configurable: true,
  165. writable: true,
  166. value: this.MULTI
  167. });
  168. __classPrivateFieldSet(this, _RedisClient_options, __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_initiateOptions).call(this, options), "f");
  169. __classPrivateFieldSet(this, _RedisClient_queue, __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_initiateQueue).call(this), "f");
  170. __classPrivateFieldSet(this, _RedisClient_socket, __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_initiateSocket).call(this), "f");
  171. // should be initiated in connect, not here
  172. // TODO: consider breaking in v5
  173. __classPrivateFieldSet(this, _RedisClient_isolationPool, __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_initiateIsolationPool).call(this), "f");
  174. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_legacyMode).call(this);
  175. }
  176. duplicate(overrides) {
  177. return new (Object.getPrototypeOf(this).constructor)({
  178. ...__classPrivateFieldGet(this, _RedisClient_options, "f"),
  179. ...overrides
  180. });
  181. }
  182. async connect() {
  183. // see comment in constructor
  184. __classPrivateFieldSet(this, _RedisClient_isolationPool, __classPrivateFieldGet(this, _RedisClient_isolationPool, "f") ?? __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_initiateIsolationPool).call(this), "f");
  185. await __classPrivateFieldGet(this, _RedisClient_socket, "f").connect();
  186. return this;
  187. }
  188. async commandsExecutor(command, args) {
  189. const { args: redisArgs, options } = (0, commander_1.transformCommandArguments)(command, args);
  190. return (0, commander_1.transformCommandReply)(command, await __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, redisArgs, options), redisArgs.preserve);
  191. }
  192. sendCommand(args, options) {
  193. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, args, options);
  194. }
  195. async functionsExecuter(fn, args, name) {
  196. const { args: redisArgs, options } = (0, commander_1.transformCommandArguments)(fn, args);
  197. return (0, commander_1.transformCommandReply)(fn, await this.executeFunction(name, fn, redisArgs, options), redisArgs.preserve);
  198. }
  199. executeFunction(name, fn, args, options) {
  200. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, (0, commander_1.fCallArguments)(name, fn, args), options);
  201. }
  202. async scriptsExecuter(script, args) {
  203. const { args: redisArgs, options } = (0, commander_1.transformCommandArguments)(script, args);
  204. return (0, commander_1.transformCommandReply)(script, await this.executeScript(script, redisArgs, options), redisArgs.preserve);
  205. }
  206. async executeScript(script, args, options) {
  207. const redisArgs = ['EVALSHA', script.SHA1];
  208. if (script.NUMBER_OF_KEYS !== undefined) {
  209. redisArgs.push(script.NUMBER_OF_KEYS.toString());
  210. }
  211. redisArgs.push(...args);
  212. try {
  213. return await __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, redisArgs, options);
  214. }
  215. catch (err) {
  216. if (!err?.message?.startsWith?.('NOSCRIPT')) {
  217. throw err;
  218. }
  219. redisArgs[0] = 'EVAL';
  220. redisArgs[1] = script.SCRIPT;
  221. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, redisArgs, options);
  222. }
  223. }
  224. async SELECT(options, db) {
  225. if (!(0, command_options_1.isCommandOptions)(options)) {
  226. db = options;
  227. options = null;
  228. }
  229. await __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, ['SELECT', db.toString()], options);
  230. __classPrivateFieldSet(this, _RedisClient_selectedDB, db, "f");
  231. }
  232. SUBSCRIBE(channels, listener, bufferMode) {
  233. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").subscribe(pub_sub_1.PubSubType.CHANNELS, channels, listener, bufferMode));
  234. }
  235. UNSUBSCRIBE(channels, listener, bufferMode) {
  236. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").unsubscribe(pub_sub_1.PubSubType.CHANNELS, channels, listener, bufferMode));
  237. }
  238. PSUBSCRIBE(patterns, listener, bufferMode) {
  239. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").subscribe(pub_sub_1.PubSubType.PATTERNS, patterns, listener, bufferMode));
  240. }
  241. PUNSUBSCRIBE(patterns, listener, bufferMode) {
  242. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").unsubscribe(pub_sub_1.PubSubType.PATTERNS, patterns, listener, bufferMode));
  243. }
  244. SSUBSCRIBE(channels, listener, bufferMode) {
  245. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").subscribe(pub_sub_1.PubSubType.SHARDED, channels, listener, bufferMode));
  246. }
  247. SUNSUBSCRIBE(channels, listener, bufferMode) {
  248. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").unsubscribe(pub_sub_1.PubSubType.SHARDED, channels, listener, bufferMode));
  249. }
  250. getPubSubListeners(type) {
  251. return __classPrivateFieldGet(this, _RedisClient_queue, "f").getPubSubListeners(type);
  252. }
  253. extendPubSubChannelListeners(type, channel, listeners) {
  254. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").extendPubSubChannelListeners(type, channel, listeners));
  255. }
  256. extendPubSubListeners(type, listeners) {
  257. return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_pubSubCommand).call(this, __classPrivateFieldGet(this, _RedisClient_queue, "f").extendPubSubListeners(type, listeners));
  258. }
  259. QUIT() {
  260. return __classPrivateFieldGet(this, _RedisClient_socket, "f").quit(async () => {
  261. if (__classPrivateFieldGet(this, _RedisClient_pingTimer, "f"))
  262. clearTimeout(__classPrivateFieldGet(this, _RedisClient_pingTimer, "f"));
  263. const quitPromise = __classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(['QUIT']);
  264. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_tick).call(this);
  265. const [reply] = await Promise.all([
  266. quitPromise,
  267. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_destroyIsolationPool).call(this)
  268. ]);
  269. return reply;
  270. });
  271. }
  272. executeIsolated(fn) {
  273. if (!__classPrivateFieldGet(this, _RedisClient_isolationPool, "f"))
  274. return Promise.reject(new errors_1.ClientClosedError());
  275. return __classPrivateFieldGet(this, _RedisClient_isolationPool, "f").use(fn);
  276. }
  277. MULTI() {
  278. return new this.Multi(this.multiExecutor.bind(this), __classPrivateFieldGet(this, _RedisClient_options, "f")?.legacyMode);
  279. }
  280. async multiExecutor(commands, selectedDB, chainId) {
  281. if (!__classPrivateFieldGet(this, _RedisClient_socket, "f").isOpen) {
  282. return Promise.reject(new errors_1.ClientClosedError());
  283. }
  284. const promise = chainId ?
  285. // if `chainId` has a value, it's a `MULTI` (and not "pipeline") - need to add the `MULTI` and `EXEC` commands
  286. Promise.all([
  287. __classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(['MULTI'], { chainId }),
  288. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_addMultiCommands).call(this, commands, chainId),
  289. __classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(['EXEC'], { chainId })
  290. ]) :
  291. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_addMultiCommands).call(this, commands);
  292. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_tick).call(this);
  293. const results = await promise;
  294. if (selectedDB !== undefined) {
  295. __classPrivateFieldSet(this, _RedisClient_selectedDB, selectedDB, "f");
  296. }
  297. return results;
  298. }
  299. async *scanIterator(options) {
  300. let cursor = 0;
  301. do {
  302. const reply = await this.scan(cursor, options);
  303. cursor = reply.cursor;
  304. for (const key of reply.keys) {
  305. yield key;
  306. }
  307. } while (cursor !== 0);
  308. }
  309. async *hScanIterator(key, options) {
  310. let cursor = 0;
  311. do {
  312. const reply = await this.hScan(key, cursor, options);
  313. cursor = reply.cursor;
  314. for (const tuple of reply.tuples) {
  315. yield tuple;
  316. }
  317. } while (cursor !== 0);
  318. }
  319. async *sScanIterator(key, options) {
  320. let cursor = 0;
  321. do {
  322. const reply = await this.sScan(key, cursor, options);
  323. cursor = reply.cursor;
  324. for (const member of reply.members) {
  325. yield member;
  326. }
  327. } while (cursor !== 0);
  328. }
  329. async *zScanIterator(key, options) {
  330. let cursor = 0;
  331. do {
  332. const reply = await this.zScan(key, cursor, options);
  333. cursor = reply.cursor;
  334. for (const member of reply.members) {
  335. yield member;
  336. }
  337. } while (cursor !== 0);
  338. }
  339. async disconnect() {
  340. if (__classPrivateFieldGet(this, _RedisClient_pingTimer, "f"))
  341. clearTimeout(__classPrivateFieldGet(this, _RedisClient_pingTimer, "f"));
  342. __classPrivateFieldGet(this, _RedisClient_queue, "f").flushAll(new errors_1.DisconnectsClientError());
  343. __classPrivateFieldGet(this, _RedisClient_socket, "f").disconnect();
  344. await __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_destroyIsolationPool).call(this);
  345. }
  346. ref() {
  347. __classPrivateFieldGet(this, _RedisClient_socket, "f").ref();
  348. }
  349. unref() {
  350. __classPrivateFieldGet(this, _RedisClient_socket, "f").unref();
  351. }
  352. }
  353. _a = RedisClient, _RedisClient_options = new WeakMap(), _RedisClient_socket = new WeakMap(), _RedisClient_queue = new WeakMap(), _RedisClient_isolationPool = new WeakMap(), _RedisClient_v4 = new WeakMap(), _RedisClient_selectedDB = new WeakMap(), _RedisClient_pingTimer = new WeakMap(), _RedisClient_instances = new WeakSet(), _RedisClient_initiateOptions = function _RedisClient_initiateOptions(options) {
  354. if (options?.url) {
  355. const parsed = _a.parseURL(options.url);
  356. if (options.socket) {
  357. parsed.socket = Object.assign(options.socket, parsed.socket);
  358. }
  359. Object.assign(options, parsed);
  360. }
  361. if (options?.database) {
  362. __classPrivateFieldSet(this, _RedisClient_selectedDB, options.database, "f");
  363. }
  364. return options;
  365. }, _RedisClient_initiateQueue = function _RedisClient_initiateQueue() {
  366. return new commands_queue_1.default(__classPrivateFieldGet(this, _RedisClient_options, "f")?.commandsQueueMaxLength, (channel, listeners) => this.emit('sharded-channel-moved', channel, listeners));
  367. }, _RedisClient_initiateSocket = function _RedisClient_initiateSocket() {
  368. const socketInitiator = async () => {
  369. const promises = [];
  370. if (__classPrivateFieldGet(this, _RedisClient_selectedDB, "f") !== 0) {
  371. promises.push(__classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(['SELECT', __classPrivateFieldGet(this, _RedisClient_selectedDB, "f").toString()], { asap: true }));
  372. }
  373. if (__classPrivateFieldGet(this, _RedisClient_options, "f")?.readonly) {
  374. promises.push(__classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(commands_1.default.READONLY.transformArguments(), { asap: true }));
  375. }
  376. if (!__classPrivateFieldGet(this, _RedisClient_options, "f")?.disableClientInfo) {
  377. promises.push(__classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(['CLIENT', 'SETINFO', 'LIB-VER', package_json_1.version], { asap: true }).catch(err => {
  378. if (!(err instanceof errors_1.ErrorReply)) {
  379. throw err;
  380. }
  381. }));
  382. promises.push(__classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand([
  383. 'CLIENT', 'SETINFO', 'LIB-NAME',
  384. __classPrivateFieldGet(this, _RedisClient_options, "f")?.clientInfoTag ? `node-redis(${__classPrivateFieldGet(this, _RedisClient_options, "f").clientInfoTag})` : 'node-redis'
  385. ], { asap: true }).catch(err => {
  386. if (!(err instanceof errors_1.ErrorReply)) {
  387. throw err;
  388. }
  389. }));
  390. }
  391. if (__classPrivateFieldGet(this, _RedisClient_options, "f")?.name) {
  392. promises.push(__classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(commands_1.default.CLIENT_SETNAME.transformArguments(__classPrivateFieldGet(this, _RedisClient_options, "f").name), { asap: true }));
  393. }
  394. if (__classPrivateFieldGet(this, _RedisClient_options, "f")?.username || __classPrivateFieldGet(this, _RedisClient_options, "f")?.password) {
  395. promises.push(__classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(commands_1.default.AUTH.transformArguments({
  396. username: __classPrivateFieldGet(this, _RedisClient_options, "f").username,
  397. password: __classPrivateFieldGet(this, _RedisClient_options, "f").password ?? ''
  398. }), { asap: true }));
  399. }
  400. const resubscribePromise = __classPrivateFieldGet(this, _RedisClient_queue, "f").resubscribe();
  401. if (resubscribePromise) {
  402. promises.push(resubscribePromise);
  403. }
  404. if (promises.length) {
  405. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_tick).call(this, true);
  406. await Promise.all(promises);
  407. }
  408. };
  409. return new socket_1.default(socketInitiator, __classPrivateFieldGet(this, _RedisClient_options, "f")?.socket)
  410. .on('data', chunk => __classPrivateFieldGet(this, _RedisClient_queue, "f").onReplyChunk(chunk))
  411. .on('error', err => {
  412. this.emit('error', err);
  413. if (__classPrivateFieldGet(this, _RedisClient_socket, "f").isOpen && !__classPrivateFieldGet(this, _RedisClient_options, "f")?.disableOfflineQueue) {
  414. __classPrivateFieldGet(this, _RedisClient_queue, "f").flushWaitingForReply(err);
  415. }
  416. else {
  417. __classPrivateFieldGet(this, _RedisClient_queue, "f").flushAll(err);
  418. }
  419. })
  420. .on('connect', () => {
  421. this.emit('connect');
  422. })
  423. .on('ready', () => {
  424. this.emit('ready');
  425. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_setPingTimer).call(this);
  426. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_tick).call(this);
  427. })
  428. .on('reconnecting', () => this.emit('reconnecting'))
  429. .on('drain', () => __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_tick).call(this))
  430. .on('end', () => this.emit('end'));
  431. }, _RedisClient_initiateIsolationPool = function _RedisClient_initiateIsolationPool() {
  432. return (0, generic_pool_1.createPool)({
  433. create: async () => {
  434. const duplicate = this.duplicate({
  435. isolationPoolOptions: undefined
  436. }).on('error', err => this.emit('error', err));
  437. await duplicate.connect();
  438. return duplicate;
  439. },
  440. destroy: client => client.disconnect()
  441. }, __classPrivateFieldGet(this, _RedisClient_options, "f")?.isolationPoolOptions);
  442. }, _RedisClient_legacyMode = function _RedisClient_legacyMode() {
  443. var _b, _c;
  444. if (!__classPrivateFieldGet(this, _RedisClient_options, "f")?.legacyMode)
  445. return;
  446. __classPrivateFieldGet(this, _RedisClient_v4, "f").sendCommand = __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).bind(this);
  447. this.sendCommand = (...args) => {
  448. const result = __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_legacySendCommand).call(this, ...args);
  449. if (result) {
  450. result.promise
  451. .then(reply => result.callback(null, reply))
  452. .catch(err => result.callback(err));
  453. }
  454. };
  455. for (const [name, command] of Object.entries(commands_1.default)) {
  456. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, name, command);
  457. (_b = this)[_c = name.toLowerCase()] ?? (_b[_c] = this[name]);
  458. }
  459. // hard coded commands
  460. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'SELECT');
  461. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'select');
  462. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'SUBSCRIBE');
  463. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'subscribe');
  464. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'PSUBSCRIBE');
  465. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'pSubscribe');
  466. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'UNSUBSCRIBE');
  467. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'unsubscribe');
  468. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'PUNSUBSCRIBE');
  469. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'pUnsubscribe');
  470. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'QUIT');
  471. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_defineLegacyCommand).call(this, 'quit');
  472. }, _RedisClient_legacySendCommand = function _RedisClient_legacySendCommand(...args) {
  473. const callback = typeof args[args.length - 1] === 'function' ?
  474. args.pop() :
  475. undefined;
  476. const promise = __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, (0, commander_1.transformLegacyCommandArguments)(args));
  477. if (callback)
  478. return {
  479. promise,
  480. callback
  481. };
  482. promise.catch(err => this.emit('error', err));
  483. }, _RedisClient_defineLegacyCommand = function _RedisClient_defineLegacyCommand(name, command) {
  484. __classPrivateFieldGet(this, _RedisClient_v4, "f")[name] = this[name].bind(this);
  485. this[name] = command && command.TRANSFORM_LEGACY_REPLY && command.transformReply ?
  486. (...args) => {
  487. const result = __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_legacySendCommand).call(this, name, ...args);
  488. if (result) {
  489. result.promise
  490. .then(reply => result.callback(null, command.transformReply(reply)))
  491. .catch(err => result.callback(err));
  492. }
  493. } :
  494. (...args) => this.sendCommand(name, ...args);
  495. }, _RedisClient_setPingTimer = function _RedisClient_setPingTimer() {
  496. if (!__classPrivateFieldGet(this, _RedisClient_options, "f")?.pingInterval || !__classPrivateFieldGet(this, _RedisClient_socket, "f").isReady)
  497. return;
  498. clearTimeout(__classPrivateFieldGet(this, _RedisClient_pingTimer, "f"));
  499. __classPrivateFieldSet(this, _RedisClient_pingTimer, setTimeout(() => {
  500. if (!__classPrivateFieldGet(this, _RedisClient_socket, "f").isReady)
  501. return;
  502. // using #sendCommand to support legacy mode
  503. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_sendCommand).call(this, ['PING'])
  504. .then(reply => this.emit('ping-interval', reply))
  505. .catch(err => this.emit('error', err))
  506. .finally(() => __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_setPingTimer).call(this));
  507. }, __classPrivateFieldGet(this, _RedisClient_options, "f").pingInterval), "f");
  508. }, _RedisClient_sendCommand = function _RedisClient_sendCommand(args, options) {
  509. if (!__classPrivateFieldGet(this, _RedisClient_socket, "f").isOpen) {
  510. return Promise.reject(new errors_1.ClientClosedError());
  511. }
  512. else if (options?.isolated) {
  513. return this.executeIsolated(isolatedClient => isolatedClient.sendCommand(args, {
  514. ...options,
  515. isolated: false
  516. }));
  517. }
  518. else if (!__classPrivateFieldGet(this, _RedisClient_socket, "f").isReady && __classPrivateFieldGet(this, _RedisClient_options, "f")?.disableOfflineQueue) {
  519. return Promise.reject(new errors_1.ClientOfflineError());
  520. }
  521. const promise = __classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(args, options);
  522. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_tick).call(this);
  523. return promise;
  524. }, _RedisClient_pubSubCommand = function _RedisClient_pubSubCommand(promise) {
  525. if (promise === undefined)
  526. return Promise.resolve();
  527. __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_tick).call(this);
  528. return promise;
  529. }, _RedisClient_tick = function _RedisClient_tick(force = false) {
  530. if (__classPrivateFieldGet(this, _RedisClient_socket, "f").writableNeedDrain || (!force && !__classPrivateFieldGet(this, _RedisClient_socket, "f").isReady)) {
  531. return;
  532. }
  533. __classPrivateFieldGet(this, _RedisClient_socket, "f").cork();
  534. while (!__classPrivateFieldGet(this, _RedisClient_socket, "f").writableNeedDrain) {
  535. const args = __classPrivateFieldGet(this, _RedisClient_queue, "f").getCommandToSend();
  536. if (args === undefined)
  537. break;
  538. __classPrivateFieldGet(this, _RedisClient_socket, "f").writeCommand(args);
  539. }
  540. }, _RedisClient_addMultiCommands = function _RedisClient_addMultiCommands(commands, chainId) {
  541. return Promise.all(commands.map(({ args }) => __classPrivateFieldGet(this, _RedisClient_queue, "f").addCommand(args, { chainId })));
  542. }, _RedisClient_destroyIsolationPool = async function _RedisClient_destroyIsolationPool() {
  543. await __classPrivateFieldGet(this, _RedisClient_isolationPool, "f").drain();
  544. await __classPrivateFieldGet(this, _RedisClient_isolationPool, "f").clear();
  545. __classPrivateFieldSet(this, _RedisClient_isolationPool, undefined, "f");
  546. };
  547. exports.default = RedisClient;
  548. (0, commander_1.attachCommands)({
  549. BaseClass: RedisClient,
  550. commands: commands_1.default,
  551. executor: RedisClient.prototype.commandsExecutor
  552. });
  553. RedisClient.prototype.Multi = multi_command_1.default;