All files / src/cmap/wire_protocol shared.ts

90.47% Statements 19/21
87.5% Branches 14/16
100% Functions 3/3
90% Lines 18/20

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 49564x 564x 564x       564x             564x   21171145x   21171145x       21171145x           21171145x     564x 35123054x 1867x     35121187x 14472246x         20648941x 45204x 80179x     20603737x    
import { MongoInvalidArgumentError } from '../../error';
import { ReadPreference, type ReadPreferenceLike } from '../../read_preference';
import { ServerType } from '../../sdam/common';
import type { Server } from '../../sdam/server';
import type { ServerDescription } from '../../sdam/server_description';
import type { Topology } from '../../sdam/topology';
import { TopologyDescription } from '../../sdam/topology_description';
import type { Connection } from '../connection';
 
export interface ReadPreferenceOption {
  readPreference?: ReadPreferenceLike;
}
 
export function getReadPreference(options?: ReadPreferenceOption): ReadPreference {
  // Default to command version of the readPreference.
  let readPreference = options?.readPreference ?? ReadPreference.primary;
 
  Iif (typeof readPreference === 'string') {
    readPreference = ReadPreference.fromString(readPreference);
  }
 
  Iif (!(readPreference instanceof ReadPreference)) {
    throw new MongoInvalidArgumentError(
      'Option "readPreference" must be a ReadPreference instance'
    );
  }
 
  return readPreference;
}
 
export function isSharded(topologyOrServer?: Topology | Server | Connection): boolean {
  if (topologyOrServer == null) {
    return false;
  }
 
  if (topologyOrServer.description && topologyOrServer.description.type === ServerType.Mongos) {
    return true;
  }
 
  // NOTE: This is incredibly inefficient, and should be removed once command construction
  // happens based on `Server` not `Topology`.
  if (topologyOrServer.description && topologyOrServer.description instanceof TopologyDescription) {
    const servers: ServerDescription[] = Array.from(topologyOrServer.description.servers.values());
    return servers.some((server: ServerDescription) => server.type === ServerType.Mongos);
  }
 
  return false;
}