Redis.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.
  85. */
  86. connect(callback?: Callback<void>): Promise<void>;
  87. /**
  88. * Disconnect from Redis.
  89. *
  90. * This method closes the connection immediately,
  91. * and may lose some pending replies that haven't written to client.
  92. * If you want to wait for the pending replies, use Redis#quit instead.
  93. */
  94. disconnect(reconnect?: boolean): void;
  95. /**
  96. * Disconnect from Redis.
  97. *
  98. * @deprecated
  99. */
  100. end(): void;
  101. /**
  102. * Create a new instance with the same options as the current one.
  103. *
  104. * @example
  105. * ```js
  106. * var redis = new Redis(6380);
  107. * var anotherRedis = redis.duplicate();
  108. * ```
  109. */
  110. duplicate(override?: Partial<RedisOptions>): Redis;
  111. /**
  112. * Mode of the connection.
  113. *
  114. * One of `"normal"`, `"subscriber"`, or `"monitor"`. When the connection is
  115. * not in `"normal"` mode, certain commands are not allowed.
  116. */
  117. get mode(): "normal" | "subscriber" | "monitor";
  118. /**
  119. * Listen for all requests received by the server in real time.
  120. *
  121. * This command will create a new connection to Redis and send a
  122. * MONITOR command via the new connection in order to avoid disturbing
  123. * the current connection.
  124. *
  125. * @param callback The callback function. If omit, a promise will be returned.
  126. * @example
  127. * ```js
  128. * var redis = new Redis();
  129. * redis.monitor(function (err, monitor) {
  130. * // Entering monitoring mode.
  131. * monitor.on('monitor', function (time, args, source, database) {
  132. * console.log(time + ": " + util.inspect(args));
  133. * });
  134. * });
  135. *
  136. * // supports promise as well as other commands
  137. * redis.monitor().then(function (monitor) {
  138. * monitor.on('monitor', function (time, args, source, database) {
  139. * console.log(time + ": " + util.inspect(args));
  140. * });
  141. * });
  142. * ```
  143. */
  144. monitor(callback?: Callback<Redis>): Promise<Redis>;
  145. /**
  146. * Send a command to Redis
  147. *
  148. * This method is used internally and in most cases you should not
  149. * use it directly. If you need to send a command that is not supported
  150. * by the library, you can use the `call` method:
  151. *
  152. * ```js
  153. * const redis = new Redis();
  154. *
  155. * redis.call('set', 'foo', 'bar');
  156. * // or
  157. * redis.call(['set', 'foo', 'bar']);
  158. * ```
  159. *
  160. * @ignore
  161. */
  162. sendCommand(command: Command, stream?: WriteableStream): unknown;
  163. private setSocketTimeout;
  164. scanStream(options?: ScanStreamOptions): ScanStream;
  165. scanBufferStream(options?: ScanStreamOptions): ScanStream;
  166. sscanStream(key: string, options?: ScanStreamOptions): ScanStream;
  167. sscanBufferStream(key: string, options?: ScanStreamOptions): ScanStream;
  168. hscanStream(key: string, options?: ScanStreamOptions): ScanStream;
  169. hscanBufferStream(key: string, options?: ScanStreamOptions): ScanStream;
  170. zscanStream(key: string, options?: ScanStreamOptions): ScanStream;
  171. zscanBufferStream(key: string, options?: ScanStreamOptions): ScanStream;
  172. /**
  173. * Emit only when there's at least one listener.
  174. *
  175. * @ignore
  176. */
  177. silentEmit(eventName: string, arg?: unknown): boolean;
  178. /**
  179. * @ignore
  180. */
  181. recoverFromFatalError(_commandError: Error, err: Error, options: FlushQueueOptions): void;
  182. /**
  183. * @ignore
  184. */
  185. handleReconnection(err: Error, item: CommandItem): void;
  186. /**
  187. * Get description of the connection. Used for debugging.
  188. */
  189. private _getDescription;
  190. private resetCommandQueue;
  191. private resetOfflineQueue;
  192. private parseOptions;
  193. /**
  194. * Change instance's status
  195. */
  196. private setStatus;
  197. private createScanStream;
  198. /**
  199. * Flush offline queue and command queue with error.
  200. *
  201. * @param error The error object to send to the commands
  202. * @param options options
  203. */
  204. private flushQueue;
  205. /**
  206. * Check whether Redis has finished loading the persistent data and is able to
  207. * process commands.
  208. */
  209. private _readyCheck;
  210. }
  211. interface Redis extends EventEmitter {
  212. on(event: "message", cb: (channel: string, message: string) => void): this;
  213. once(event: "message", cb: (channel: string, message: string) => void): this;
  214. on(event: "messageBuffer", cb: (channel: Buffer, message: Buffer) => void): this;
  215. once(event: "messageBuffer", cb: (channel: Buffer, message: Buffer) => void): this;
  216. on(event: "pmessage", cb: (pattern: string, channel: string, message: string) => void): this;
  217. once(event: "pmessage", cb: (pattern: string, channel: string, message: string) => void): this;
  218. on(event: "pmessageBuffer", cb: (pattern: string, channel: Buffer, message: Buffer) => void): this;
  219. once(event: "pmessageBuffer", cb: (pattern: string, channel: Buffer, message: Buffer) => void): this;
  220. on(event: "error", cb: (error: Error) => void): this;
  221. once(event: "error", cb: (error: Error) => void): this;
  222. on(event: RedisStatus, cb: () => void): this;
  223. once(event: RedisStatus, cb: () => void): this;
  224. on(event: string | symbol, listener: (...args: any[]) => void): this;
  225. once(event: string | symbol, listener: (...args: any[]) => void): this;
  226. }
  227. interface Redis extends Transaction {
  228. }
  229. export default Redis;