Command.d.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /// <reference types="node" />
  2. import { Callback, Respondable, CommandParameter } from "./types";
  3. export declare type ArgumentType = string | Buffer | number | (string | Buffer | number | any[])[];
  4. interface CommandOptions {
  5. /**
  6. * Set the encoding of the reply, by default buffer will be returned.
  7. */
  8. replyEncoding?: BufferEncoding | null;
  9. errorStack?: Error;
  10. keyPrefix?: string;
  11. /**
  12. * Force the command to be readOnly so it will also execute on slaves
  13. */
  14. readOnly?: boolean;
  15. }
  16. declare type ArgumentTransformer = (args: any[]) => any[];
  17. declare type ReplyTransformer = (reply: any) => any;
  18. export interface CommandNameFlags {
  19. VALID_IN_SUBSCRIBER_MODE: [
  20. "subscribe",
  21. "psubscribe",
  22. "unsubscribe",
  23. "punsubscribe",
  24. "ssubscribe",
  25. "sunsubscribe",
  26. "ping",
  27. "quit"
  28. ];
  29. VALID_IN_MONITOR_MODE: ["monitor", "auth"];
  30. ENTER_SUBSCRIBER_MODE: ["subscribe", "psubscribe", "ssubscribe"];
  31. EXIT_SUBSCRIBER_MODE: ["unsubscribe", "punsubscribe", "sunsubscribe"];
  32. WILL_DISCONNECT: ["quit"];
  33. HANDSHAKE_COMMANDS: ["auth", "select", "client", "readonly", "info"];
  34. IGNORE_RECONNECT_ON_ERROR: ["client"];
  35. BLOCKING_COMMANDS: [
  36. "blpop",
  37. "brpop",
  38. "brpoplpush",
  39. "blmove",
  40. "bzpopmin",
  41. "bzpopmax",
  42. "bzmpop",
  43. "blmpop",
  44. "xread",
  45. "xreadgroup"
  46. ];
  47. LAST_ARG_TIMEOUT_COMMANDS: [
  48. "blpop",
  49. "brpop",
  50. "brpoplpush",
  51. "blmove",
  52. "bzpopmin",
  53. "bzpopmax"
  54. ];
  55. FIRST_ARG_TIMEOUT_COMMANDS: ["bzmpop", "blmpop"];
  56. BLOCK_OPTION_COMMANDS: ["xread", "xreadgroup"];
  57. }
  58. /**
  59. * Command instance
  60. *
  61. * It's rare that you need to create a Command instance yourself.
  62. *
  63. * ```js
  64. * var infoCommand = new Command('info', null, function (err, result) {
  65. * console.log('result', result);
  66. * });
  67. *
  68. * redis.sendCommand(infoCommand);
  69. *
  70. * // When no callback provided, Command instance will have a `promise` property,
  71. * // which will resolve/reject with the result of the command.
  72. * var getCommand = new Command('get', ['foo']);
  73. * getCommand.promise.then(function (result) {
  74. * console.log('result', result);
  75. * });
  76. * ```
  77. */
  78. export default class Command implements Respondable {
  79. name: string;
  80. static FLAGS: {
  81. [key in keyof CommandNameFlags]: CommandNameFlags[key];
  82. };
  83. private static flagMap?;
  84. private static _transformer;
  85. /**
  86. * Check whether the command has the flag
  87. */
  88. static checkFlag<T extends keyof CommandNameFlags>(flagName: T, commandName: string): commandName is CommandNameFlags[T][number];
  89. static setArgumentTransformer(name: string, func: ArgumentTransformer): void;
  90. static setReplyTransformer(name: string, func: ReplyTransformer): void;
  91. private static getFlagMap;
  92. ignore?: boolean;
  93. isReadOnly?: boolean;
  94. args: CommandParameter[];
  95. inTransaction: boolean;
  96. pipelineIndex?: number;
  97. isResolved: boolean;
  98. reject: (err: Error) => void;
  99. resolve: (result: any) => void;
  100. promise: Promise<any>;
  101. private replyEncoding;
  102. private errorStack;
  103. private bufferMode;
  104. private callback;
  105. private transformed;
  106. private _commandTimeoutTimer?;
  107. private _blockingTimeoutTimer?;
  108. private _blockingDeadline?;
  109. private slot?;
  110. private keys?;
  111. /**
  112. * Creates an instance of Command.
  113. * @param name Command name
  114. * @param args An array of command arguments
  115. * @param options
  116. * @param callback The callback that handles the response.
  117. * If omit, the response will be handled via Promise
  118. */
  119. constructor(name: string, args?: Array<ArgumentType>, options?: CommandOptions, callback?: Callback);
  120. getSlot(): number;
  121. getKeys(): Array<string | Buffer>;
  122. /**
  123. * Convert command to writable buffer or string
  124. */
  125. toWritable(_socket: object): string | Buffer;
  126. stringifyArguments(): void;
  127. /**
  128. * Convert buffer/buffer[] to string/string[],
  129. * and apply reply transformer.
  130. */
  131. transformReply(result: Buffer | Buffer[]): string | string[] | Buffer | Buffer[];
  132. /**
  133. * Set the wait time before terminating the attempt to execute a command
  134. * and generating an error.
  135. */
  136. setTimeout(ms: number): void;
  137. /**
  138. * Set a timeout for blocking commands.
  139. * When the timeout expires, the command resolves with null (matching Redis behavior).
  140. * This handles the case of undetectable network failures (e.g., docker network disconnect)
  141. * where the TCP connection becomes a zombie and no close event fires.
  142. */
  143. setBlockingTimeout(ms: number): void;
  144. /**
  145. * Extract the blocking timeout from the command arguments.
  146. *
  147. * @returns The timeout in seconds, null for indefinite blocking (timeout of 0),
  148. * or undefined if this is not a blocking command
  149. */
  150. extractBlockingTimeout(): number | null | undefined;
  151. /**
  152. * Clear the command and blocking timers
  153. */
  154. private _clearTimers;
  155. private initPromise;
  156. /**
  157. * Iterate through the command arguments that are considered keys.
  158. */
  159. private _iterateKeys;
  160. /**
  161. * Convert the value from buffer to the target encoding.
  162. */
  163. private _convertValue;
  164. }
  165. export {};