socket.d.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. import { EventEmitter } from 'events';
  5. import * as net from 'net';
  6. import * as tls from 'tls';
  7. import { RedisCommandArguments } from '../commands';
  8. export interface RedisSocketCommonOptions {
  9. /**
  10. * Connection Timeout (in milliseconds)
  11. */
  12. connectTimeout?: number;
  13. /**
  14. * Toggle [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
  15. */
  16. noDelay?: boolean;
  17. /**
  18. * Toggle [`keep-alive`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay)
  19. */
  20. keepAlive?: number | false;
  21. /**
  22. * When the socket closes unexpectedly (without calling `.quit()`/`.disconnect()`), the client uses `reconnectStrategy` to decide what to do. The following values are supported:
  23. * 1. `false` -> do not reconnect, close the client and flush the command queue.
  24. * 2. `number` -> wait for `X` milliseconds before reconnecting.
  25. * 3. `(retries: number, cause: Error) => false | number | Error` -> `number` is the same as configuring a `number` directly, `Error` is the same as `false`, but with a custom error.
  26. * Defaults to `retries => Math.min(retries * 50, 500)`
  27. */
  28. reconnectStrategy?: false | number | ((retries: number, cause: Error) => false | Error | number);
  29. }
  30. type RedisNetSocketOptions = Partial<net.SocketConnectOpts> & {
  31. tls?: false;
  32. };
  33. export interface RedisTlsSocketOptions extends tls.ConnectionOptions {
  34. tls: true;
  35. }
  36. export type RedisSocketOptions = RedisSocketCommonOptions & (RedisNetSocketOptions | RedisTlsSocketOptions);
  37. export type RedisSocketInitiator = () => Promise<void>;
  38. export default class RedisSocket extends EventEmitter {
  39. #private;
  40. get isOpen(): boolean;
  41. get isReady(): boolean;
  42. get writableNeedDrain(): boolean;
  43. constructor(initiator: RedisSocketInitiator, options?: RedisSocketOptions);
  44. connect(): Promise<void>;
  45. writeCommand(args: RedisCommandArguments): void;
  46. disconnect(): void;
  47. quit<T>(fn: () => Promise<T>): Promise<T>;
  48. cork(): void;
  49. ref(): void;
  50. unref(): void;
  51. }
  52. export {};