opusdev/vector-similarity-api
1
1import { HostAddress } from '.././utils';2import {3 SERVER_SELECTION_FAILED,4 SERVER_SELECTION_STARTED,5 SERVER_SELECTION_SUCCEEDED,6 WAITING_FOR_SUITABLE_SERVER7} from '../constants';8import { type ReadPreference } from '../read_preference';9import { type ServerSelector } from './server_selection';10import type { TopologyDescription } from './topology_description';11 12/**13 * The base export class for all logs published from server selection14 * @internal15 * @category Log Type16 */17export abstract class ServerSelectionEvent {18 /** String representation of the selector being used to select the server.19 * Defaults to 'custom selector' for application-provided custom selector case.20 */21 selector: string | ReadPreference | ServerSelector;22 /** The name of the operation for which a server is being selected. */23 operation: string;24 /** The current topology description. */25 topologyDescription: TopologyDescription;26 27 /** @internal */28 abstract name:29 | typeof SERVER_SELECTION_STARTED30 | typeof SERVER_SELECTION_SUCCEEDED31 | typeof SERVER_SELECTION_FAILED32 | typeof WAITING_FOR_SUITABLE_SERVER;33 34 abstract message: string;35 36 /** @internal */37 constructor(38 selector: string | ReadPreference | ServerSelector,39 topologyDescription: TopologyDescription,40 operation: string41 ) {42 this.selector = selector;43 this.operation = operation;44 this.topologyDescription = topologyDescription;45 }46}47 48/**49 * An event published when server selection starts50 * @internal51 * @category Event52 */53export class ServerSelectionStartedEvent extends ServerSelectionEvent {54 /** @internal */55 name = SERVER_SELECTION_STARTED;56 message = 'Server selection started';57 58 /** @internal */59 constructor(60 selector: string | ReadPreference | ServerSelector,61 topologyDescription: TopologyDescription,62 operation: string63 ) {64 super(selector, topologyDescription, operation);65 }66}67 68/**69 * An event published when a server selection fails70 * @internal71 * @category Event72 */73export class ServerSelectionFailedEvent extends ServerSelectionEvent {74 /** @internal */75 name = SERVER_SELECTION_FAILED;76 message = 'Server selection failed';77 /** Representation of the error the driver will throw regarding server selection failing. */78 failure: Error;79 80 /** @internal */81 constructor(82 selector: string | ReadPreference | ServerSelector,83 topologyDescription: TopologyDescription,84 error: Error,85 operation: string86 ) {87 super(selector, topologyDescription, operation);88 this.failure = error;89 }90}91 92/**93 * An event published when server selection succeeds94 * @internal95 * @category Event96 */97export class ServerSelectionSucceededEvent extends ServerSelectionEvent {98 /** @internal */99 name = SERVER_SELECTION_SUCCEEDED;100 message = 'Server selection succeeded';101 /** The hostname, IP address, or Unix domain socket path for the selected server. */102 serverHost: string;103 /** 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. */104 serverPort: number | undefined;105 106 /** @internal */107 constructor(108 selector: string | ReadPreference | ServerSelector,109 topologyDescription: TopologyDescription,110 address: string,111 operation: string112 ) {113 super(selector, topologyDescription, operation);114 const { host, port } = HostAddress.fromString(address).toHostPort();115 this.serverHost = host;116 this.serverPort = port;117 }118}119 120/**121 * An event published when server selection is waiting for a suitable server to become available122 * @internal123 * @category Event124 */125export class WaitingForSuitableServerEvent extends ServerSelectionEvent {126 /** @internal */127 name = WAITING_FOR_SUITABLE_SERVER;128 message = 'Waiting for suitable server to become available';129 /** The remaining time left until server selection will time out. */130 remainingTimeMS: number;131 132 /** @internal */133 constructor(134 selector: string | ReadPreference | ServerSelector,135 topologyDescription: TopologyDescription,136 remainingTimeMS: number,137 operation: string138 ) {139 super(selector, topologyDescription, operation);140 this.remainingTimeMS = remainingTimeMS;141 }142}143 