UNPKG

@valkey/client

Version:

The source code and documentation for this package are in the main [node-redis](https://github.com/redis/node-redis) repo.

201 lines (200 loc) 12.2 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); 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"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); 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"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _ValkeyCommandsQueue_instances, _a, _ValkeyCommandsQueue_flushQueue, _ValkeyCommandsQueue_maxLength, _ValkeyCommandsQueue_waitingToBeSent, _ValkeyCommandsQueue_waitingForReply, _ValkeyCommandsQueue_onShardedChannelMoved, _ValkeyCommandsQueue_pubSub, _ValkeyCommandsQueue_chainInExecution, _ValkeyCommandsQueue_decoder, _ValkeyCommandsQueue_pushPubSubCommand; Object.defineProperty(exports, "__esModule", { value: true }); const LinkedList = require("yallist"); const errors_1 = require("../errors"); const decoder_1 = require("./RESP2/decoder"); const encoder_1 = require("./RESP2/encoder"); const pub_sub_1 = require("./pub-sub"); const PONG = Buffer.from("pong"); class ValkeyCommandsQueue { get isPubSubActive() { return __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").isActive; } constructor(maxLength, onShardedChannelMoved) { _ValkeyCommandsQueue_instances.add(this); _ValkeyCommandsQueue_maxLength.set(this, void 0); _ValkeyCommandsQueue_waitingToBeSent.set(this, new LinkedList()); _ValkeyCommandsQueue_waitingForReply.set(this, new LinkedList()); _ValkeyCommandsQueue_onShardedChannelMoved.set(this, void 0); _ValkeyCommandsQueue_pubSub.set(this, new pub_sub_1.PubSub()); _ValkeyCommandsQueue_chainInExecution.set(this, void 0); _ValkeyCommandsQueue_decoder.set(this, new decoder_1.default({ returnStringsAsBuffers: () => { return (!!__classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").head?.value.returnBuffers || __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").isActive); }, onReply: (reply) => { if (__classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").isActive && Array.isArray(reply)) { if (__classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").handleMessageReply(reply)) return; const isShardedUnsubscribe = pub_sub_1.PubSub.isShardedUnsubscribe(reply); if (isShardedUnsubscribe && !__classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").length) { const channel = reply[1].toString(); __classPrivateFieldGet(this, _ValkeyCommandsQueue_onShardedChannelMoved, "f").call(this, channel, __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").removeShardedListeners(channel)); return; } else if (isShardedUnsubscribe || pub_sub_1.PubSub.isStatusReply(reply)) { const head = __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").head.value; if ((Number.isNaN(head.channelsCounter) && reply[2] === 0) || --head.channelsCounter === 0) { __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").shift().resolve(); } return; } if (PONG.equals(reply[0])) { const { resolve, returnBuffers } = __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").shift(), buffer = (reply[1].length === 0 ? reply[0] : reply[1]); resolve(returnBuffers ? buffer : buffer.toString()); return; } } const { resolve, reject } = __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").shift(); if (reply instanceof errors_1.ErrorReply) { reject(reply); } else { resolve(reply); } }, })); __classPrivateFieldSet(this, _ValkeyCommandsQueue_maxLength, maxLength, "f"); __classPrivateFieldSet(this, _ValkeyCommandsQueue_onShardedChannelMoved, onShardedChannelMoved, "f"); } addCommand(args, options) { if (__classPrivateFieldGet(this, _ValkeyCommandsQueue_maxLength, "f") && __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").length + __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").length >= __classPrivateFieldGet(this, _ValkeyCommandsQueue_maxLength, "f")) { return Promise.reject(new Error("The queue is full")); } else if (options?.signal?.aborted) { return Promise.reject(new errors_1.AbortError()); } return new Promise((resolve, reject) => { const node = new LinkedList.Node({ args, chainId: options?.chainId, returnBuffers: options?.returnBuffers, resolve, reject, }); if (options?.signal) { const listener = () => { __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").removeNode(node); node.value.reject(new errors_1.AbortError()); }; node.value.abort = { signal: options.signal, listener, }; // AbortSignal type is incorrent options.signal.addEventListener("abort", listener, { once: true, }); } if (options?.asap) { __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").unshiftNode(node); } else { __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").pushNode(node); } }); } subscribe(type, channels, listener, returnBuffers) { return __classPrivateFieldGet(this, _ValkeyCommandsQueue_instances, "m", _ValkeyCommandsQueue_pushPubSubCommand).call(this, __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").subscribe(type, channels, listener, returnBuffers)); } unsubscribe(type, channels, listener, returnBuffers) { return __classPrivateFieldGet(this, _ValkeyCommandsQueue_instances, "m", _ValkeyCommandsQueue_pushPubSubCommand).call(this, __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").unsubscribe(type, channels, listener, returnBuffers)); } resubscribe() { const commands = __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").resubscribe(); if (!commands.length) return; return Promise.all(commands.map((command) => __classPrivateFieldGet(this, _ValkeyCommandsQueue_instances, "m", _ValkeyCommandsQueue_pushPubSubCommand).call(this, command))); } extendPubSubChannelListeners(type, channel, listeners) { return __classPrivateFieldGet(this, _ValkeyCommandsQueue_instances, "m", _ValkeyCommandsQueue_pushPubSubCommand).call(this, __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").extendChannelListeners(type, channel, listeners)); } extendPubSubListeners(type, listeners) { return __classPrivateFieldGet(this, _ValkeyCommandsQueue_instances, "m", _ValkeyCommandsQueue_pushPubSubCommand).call(this, __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").extendTypeListeners(type, listeners)); } getPubSubListeners(type) { return __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").getTypeListeners(type); } getCommandToSend() { const toSend = __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").shift(); if (!toSend) return; let encoded; try { encoded = (0, encoder_1.default)(toSend.args); } catch (err) { toSend.reject(err); return; } __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f").push({ resolve: toSend.resolve, reject: toSend.reject, channelsCounter: toSend.channelsCounter, returnBuffers: toSend.returnBuffers, }); __classPrivateFieldSet(this, _ValkeyCommandsQueue_chainInExecution, toSend.chainId, "f"); return encoded; } onReplyChunk(chunk) { __classPrivateFieldGet(this, _ValkeyCommandsQueue_decoder, "f").write(chunk); } flushWaitingForReply(err) { __classPrivateFieldGet(this, _ValkeyCommandsQueue_decoder, "f").reset(); __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").reset(); __classPrivateFieldGet(_a, _a, "m", _ValkeyCommandsQueue_flushQueue).call(_a, __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f"), err); if (!__classPrivateFieldGet(this, _ValkeyCommandsQueue_chainInExecution, "f")) return; while (__classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").head?.value.chainId === __classPrivateFieldGet(this, _ValkeyCommandsQueue_chainInExecution, "f")) { __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").shift(); } __classPrivateFieldSet(this, _ValkeyCommandsQueue_chainInExecution, undefined, "f"); } flushAll(err) { __classPrivateFieldGet(this, _ValkeyCommandsQueue_decoder, "f").reset(); __classPrivateFieldGet(this, _ValkeyCommandsQueue_pubSub, "f").reset(); __classPrivateFieldGet(_a, _a, "m", _ValkeyCommandsQueue_flushQueue).call(_a, __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingForReply, "f"), err); __classPrivateFieldGet(_a, _a, "m", _ValkeyCommandsQueue_flushQueue).call(_a, __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f"), err); } } _a = ValkeyCommandsQueue, _ValkeyCommandsQueue_maxLength = new WeakMap(), _ValkeyCommandsQueue_waitingToBeSent = new WeakMap(), _ValkeyCommandsQueue_waitingForReply = new WeakMap(), _ValkeyCommandsQueue_onShardedChannelMoved = new WeakMap(), _ValkeyCommandsQueue_pubSub = new WeakMap(), _ValkeyCommandsQueue_chainInExecution = new WeakMap(), _ValkeyCommandsQueue_decoder = new WeakMap(), _ValkeyCommandsQueue_instances = new WeakSet(), _ValkeyCommandsQueue_flushQueue = function _ValkeyCommandsQueue_flushQueue(queue, err) { while (queue.length) { queue.shift().reject(err); } }, _ValkeyCommandsQueue_pushPubSubCommand = function _ValkeyCommandsQueue_pushPubSubCommand(command) { if (command === undefined) return; return new Promise((resolve, reject) => { __classPrivateFieldGet(this, _ValkeyCommandsQueue_waitingToBeSent, "f").push({ args: command.args, channelsCounter: command.channelsCounter, returnBuffers: true, resolve: () => { command.resolve(); resolve(); }, reject: (err) => { command.reject?.(); reject(err); }, }); }); }; exports.default = ValkeyCommandsQueue;