Command.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }
  34. /**
  35. * Command instance
  36. *
  37. * It's rare that you need to create a Command instance yourself.
  38. *
  39. * ```js
  40. * var infoCommand = new Command('info', null, function (err, result) {
  41. * console.log('result', result);
  42. * });
  43. *
  44. * redis.sendCommand(infoCommand);
  45. *
  46. * // When no callback provided, Command instance will have a `promise` property,
  47. * // which will resolve/reject with the result of the command.
  48. * var getCommand = new Command('get', ['foo']);
  49. * getCommand.promise.then(function (result) {
  50. * console.log('result', result);
  51. * });
  52. * ```
  53. */
  54. export default class Command implements Respondable {
  55. name: string;
  56. static FLAGS: {
  57. [key in keyof CommandNameFlags]: CommandNameFlags[key];
  58. };
  59. private static flagMap?;
  60. private static _transformer;
  61. /**
  62. * Check whether the command has the flag
  63. */
  64. static checkFlag<T extends keyof CommandNameFlags>(flagName: T, commandName: string): commandName is CommandNameFlags[T][number];
  65. static setArgumentTransformer(name: string, func: ArgumentTransformer): void;
  66. static setReplyTransformer(name: string, func: ReplyTransformer): void;
  67. private static getFlagMap;
  68. ignore?: boolean;
  69. isReadOnly?: boolean;
  70. args: CommandParameter[];
  71. inTransaction: boolean;
  72. pipelineIndex?: number;
  73. isResolved: boolean;
  74. reject: (err: Error) => void;
  75. resolve: (result: any) => void;
  76. promise: Promise<any>;
  77. private replyEncoding;
  78. private errorStack;
  79. private bufferMode;
  80. private callback;
  81. private transformed;
  82. private _commandTimeoutTimer?;
  83. private slot?;
  84. private keys?;
  85. /**
  86. * Creates an instance of Command.
  87. * @param name Command name
  88. * @param args An array of command arguments
  89. * @param options
  90. * @param callback The callback that handles the response.
  91. * If omit, the response will be handled via Promise
  92. */
  93. constructor(name: string, args?: Array<ArgumentType>, options?: CommandOptions, callback?: Callback);
  94. getSlot(): number;
  95. getKeys(): Array<string | Buffer>;
  96. /**
  97. * Convert command to writable buffer or string
  98. */
  99. toWritable(_socket: object): string | Buffer;
  100. stringifyArguments(): void;
  101. /**
  102. * Convert buffer/buffer[] to string/string[],
  103. * and apply reply transformer.
  104. */
  105. transformReply(result: Buffer | Buffer[]): string | string[] | Buffer | Buffer[];
  106. /**
  107. * Set the wait time before terminating the attempt to execute a command
  108. * and generating an error.
  109. */
  110. setTimeout(ms: number): void;
  111. private initPromise;
  112. /**
  113. * Iterate through the command arguments that are considered keys.
  114. */
  115. private _iterateKeys;
  116. /**
  117. * Convert the value from buffer to the target encoding.
  118. */
  119. private _convertValue;
  120. }
  121. export {};