Redis.d.ts 8.0 KB

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