Redis.d.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /// <reference types="node" />
  2. import { EventEmitter } from "events";
  3. import Cluster from "./cluster";
  4. import Command from "./Command";
  5. import { RedisOptions } from "./redis/RedisOptions";
  6. import ScanStream from "./ScanStream";
  7. import { Transaction } from "./transaction";
  8. import { Callback, NetStream, ScanStreamOptions, WriteableStream } from "./types";
  9. import Commander from "./utils/Commander";
  10. declare type RedisStatus = "wait" | "reconnecting" | "connecting" | "connect" | "ready" | "close" | "end";
  11. /**
  12. * This is the major component of ioredis.
  13. * Use it to connect to a standalone Redis server or Sentinels.
  14. *
  15. * ```typescript
  16. * const redis = new Redis(); // Default port is 6379
  17. * async function main() {
  18. * redis.set("foo", "bar");
  19. * redis.get("foo", (err, result) => {
  20. * // `result` should be "bar"
  21. * console.log(err, result);
  22. * });
  23. * // Or use Promise
  24. * const result = await redis.get("foo");
  25. * }
  26. * ```
  27. */
  28. declare class Redis extends Commander {
  29. static Cluster: typeof Cluster;
  30. static Command: typeof Command;
  31. /**
  32. * Default options
  33. */
  34. private static defaultOptions;
  35. /**
  36. * Create a Redis instance.
  37. * This is the same as `new Redis()` but is included for compatibility with node-redis.
  38. */
  39. static createClient(...args: ConstructorParameters<typeof Redis>): Redis;
  40. options: RedisOptions;
  41. status: RedisStatus;
  42. /**
  43. * @ignore
  44. */
  45. stream: NetStream;
  46. /**
  47. * @ignore
  48. */
  49. isCluster: boolean;
  50. private connector;
  51. private reconnectTimeout;
  52. private condition;
  53. private commandQueue;
  54. private offlineQueue;
  55. private connectionEpoch;
  56. private retryAttempts;
  57. private manuallyClosing;
  58. private _autoPipelines;
  59. private _runningAutoPipelines;
  60. constructor(port: number, host: string, options: RedisOptions);
  61. constructor(path: string, options: RedisOptions);
  62. constructor(port: number, options: RedisOptions);
  63. constructor(port: number, host: string);
  64. constructor(options: RedisOptions);
  65. constructor(port: number);
  66. constructor(path: string);
  67. constructor();
  68. get autoPipelineQueueSize(): number;
  69. /**
  70. * Create a connection to Redis.
  71. * This method will be invoked automatically when creating a new Redis instance
  72. * unless `lazyConnect: true` is passed.
  73. *
  74. * When calling this method manually, a Promise is returned, which will
  75. * be resolved when the connection status is ready.
  76. */
  77. connect(callback?: Callback<void>): Promise<void>;
  78. /**
  79. * Disconnect from Redis.
  80. *
  81. * This method closes the connection immediately,
  82. * and may lose some pending replies that haven't written to client.
  83. * If you want to wait for the pending replies, use Redis#quit instead.
  84. */
  85. disconnect(reconnect?: boolean): void;
  86. /**
  87. * Disconnect from Redis.
  88. *
  89. * @deprecated
  90. */
  91. end(): void;
  92. /**
  93. * Create a new instance with the same options as the current one.
  94. *
  95. * @example
  96. * ```js
  97. * var redis = new Redis(6380);
  98. * var anotherRedis = redis.duplicate();
  99. * ```
  100. */
  101. duplicate(override?: Partial<RedisOptions>): Redis;
  102. /**
  103. * Listen for all requests received by the server in real time.
  104. *
  105. * This command will create a new connection to Redis and send a
  106. * MONITOR command via the new connection in order to avoid disturbing
  107. * the current connection.
  108. *
  109. * @param callback The callback function. If omit, a promise will be returned.
  110. * @example
  111. * ```js
  112. * var redis = new Redis();
  113. * redis.monitor(function (err, monitor) {
  114. * // Entering monitoring mode.
  115. * monitor.on('monitor', function (time, args, source, database) {
  116. * console.log(time + ": " + util.inspect(args));
  117. * });
  118. * });
  119. *
  120. * // supports promise as well as other commands
  121. * redis.monitor().then(function (monitor) {
  122. * monitor.on('monitor', function (time, args, source, database) {
  123. * console.log(time + ": " + util.inspect(args));
  124. * });
  125. * });
  126. * ```
  127. */
  128. monitor(callback?: Callback<Redis>): Promise<Redis>;
  129. /**
  130. * Send a command to Redis
  131. *
  132. * This method is used internally and in most cases you should not
  133. * use it directly. If you need to send a command that is not supported
  134. * by the library, you can use the `call` method:
  135. *
  136. * ```js
  137. * const redis = new Redis();
  138. *
  139. * redis.call('set', 'foo', 'bar');
  140. * // or
  141. * redis.call(['set', 'foo', 'bar']);
  142. * ```
  143. *
  144. * @ignore
  145. */
  146. sendCommand(command: Command, stream?: WriteableStream): unknown;
  147. scanStream(options?: ScanStreamOptions): ScanStream;
  148. scanBufferStream(options?: ScanStreamOptions): ScanStream;
  149. sscanStream(key: string, options?: ScanStreamOptions): ScanStream;
  150. sscanBufferStream(key: string, options?: ScanStreamOptions): ScanStream;
  151. hscanStream(key: string, options?: ScanStreamOptions): ScanStream;
  152. hscanBufferStream(key: string, options?: ScanStreamOptions): ScanStream;
  153. zscanStream(key: string, options?: ScanStreamOptions): ScanStream;
  154. zscanBufferStream(key: string, options?: ScanStreamOptions): ScanStream;
  155. /**
  156. * Emit only when there's at least one listener.
  157. *
  158. * @ignore
  159. */
  160. silentEmit(eventName: string, arg?: unknown): boolean;
  161. /**
  162. * Get description of the connection. Used for debugging.
  163. */
  164. private _getDescription;
  165. private resetCommandQueue;
  166. private resetOfflineQueue;
  167. private recoverFromFatalError;
  168. private handleReconnection;
  169. private parseOptions;
  170. /**
  171. * Change instance's status
  172. */
  173. private setStatus;
  174. private createScanStream;
  175. /**
  176. * Flush offline queue and command queue with error.
  177. *
  178. * @param error The error object to send to the commands
  179. * @param options options
  180. */
  181. private flushQueue;
  182. /**
  183. * Check whether Redis has finished loading the persistent data and is able to
  184. * process commands.
  185. */
  186. private _readyCheck;
  187. }
  188. interface Redis extends EventEmitter {
  189. }
  190. interface Redis extends Transaction {
  191. }
  192. export default Redis;