Command.d.ts 3.8 KB

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