All files / src/sdam server_selection_events.ts

100% Statements 27/27
100% Branches 0/0
100% Functions 5/5
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143135x 135x                             135x                                                 83520x 83520x 83520x                 135x   37137x 37137x               37137x                 135x   72x 72x                     72x 72x                 135x   36885x 36885x                         36885x 36885x 36885x 36885x                 135x   9426x 9426x                     9426x 9426x      
import { HostAddress } from '.././utils';
import {
  SERVER_SELECTION_FAILED,
  SERVER_SELECTION_STARTED,
  SERVER_SELECTION_SUCCEEDED,
  WAITING_FOR_SUITABLE_SERVER
} from '../constants';
import { type ReadPreference } from '../read_preference';
import { type ServerSelector } from './server_selection';
import type { TopologyDescription } from './topology_description';
 
/**
 * The base export class for all logs published from server selection
 * @internal
 * @category Log Type
 */
export abstract class ServerSelectionEvent {
  /** String representation of the selector being used to select the server.
   *  Defaults to 'custom selector' for application-provided custom selector case.
   */
  selector: string | ReadPreference | ServerSelector;
  /** The name of the operation for which a server is being selected.  */
  operation: string;
  /** 	The current topology description.  */
  topologyDescription: TopologyDescription;
 
  /** @internal */
  abstract name:
    | typeof SERVER_SELECTION_STARTED
    | typeof SERVER_SELECTION_SUCCEEDED
    | typeof SERVER_SELECTION_FAILED
    | typeof WAITING_FOR_SUITABLE_SERVER;
 
  abstract message: string;
 
  /** @internal */
  constructor(
    selector: string | ReadPreference | ServerSelector,
    topologyDescription: TopologyDescription,
    operation: string
  ) {
    this.selector = selector;
    this.operation = operation;
    this.topologyDescription = topologyDescription;
  }
}
 
/**
 * An event published when server selection starts
 * @internal
 * @category Event
 */
export class ServerSelectionStartedEvent extends ServerSelectionEvent {
  /** @internal */
  name = SERVER_SELECTION_STARTED;
  message = 'Server selection started';
 
  /** @internal */
  constructor(
    selector: string | ReadPreference | ServerSelector,
    topologyDescription: TopologyDescription,
    operation: string
  ) {
    super(selector, topologyDescription, operation);
  }
}
 
/**
 * An event published when a server selection fails
 * @internal
 * @category Event
 */
export class ServerSelectionFailedEvent extends ServerSelectionEvent {
  /** @internal */
  name = SERVER_SELECTION_FAILED;
  message = 'Server selection failed';
  /** Representation of the error the driver will throw regarding server selection failing. */
  failure: Error;
 
  /** @internal */
  constructor(
    selector: string | ReadPreference | ServerSelector,
    topologyDescription: TopologyDescription,
    error: Error,
    operation: string
  ) {
    super(selector, topologyDescription, operation);
    this.failure = error;
  }
}
 
/**
 * An event published when server selection succeeds
 * @internal
 * @category Event
 */
export class ServerSelectionSucceededEvent extends ServerSelectionEvent {
  /** @internal */
  name = SERVER_SELECTION_SUCCEEDED;
  message = 'Server selection succeeded';
  /** 	The hostname, IP address, or Unix domain socket path for the selected server. */
  serverHost: string;
  /** The port for the selected server. Optional; not present for Unix domain sockets. When the user does not specify a port and the default (27017) is used, the driver SHOULD include it here. */
  serverPort: number | undefined;
 
  /** @internal */
  constructor(
    selector: string | ReadPreference | ServerSelector,
    topologyDescription: TopologyDescription,
    address: string,
    operation: string
  ) {
    super(selector, topologyDescription, operation);
    const { host, port } = HostAddress.fromString(address).toHostPort();
    this.serverHost = host;
    this.serverPort = port;
  }
}
 
/**
 * An event published when server selection is waiting for a suitable server to become available
 * @internal
 * @category Event
 */
export class WaitingForSuitableServerEvent extends ServerSelectionEvent {
  /** @internal */
  name = WAITING_FOR_SUITABLE_SERVER;
  message = 'Waiting for suitable server to become available';
  /** The remaining time left until server selection will time out. */
  remainingTimeMS: number;
 
  /** @internal */
  constructor(
    selector: string | ReadPreference | ServerSelector,
    topologyDescription: TopologyDescription,
    remainingTimeMS: number,
    operation: string
  ) {
    super(selector, topologyDescription, operation);
    this.remainingTimeMS = remainingTimeMS;
  }
}