code: function maxStalenessReducer(readPreference, topologyDescription, servers) {
    if (readPreference.maxStalenessSeconds == null || readPreference.maxStalenessSeconds < 0) {
      return servers;
    }
  
    const maxStaleness = readPreference.maxStalenessSeconds;
    const maxStalenessVariance =
      (topologyDescription.heartbeatFrequencyMS + IDLE_WRITE_PERIOD) / 1000;
    if (maxStaleness < maxStalenessVariance) {
      throw MongoError(`maxStalenessSeconds must be at least ${maxStalenessVariance} seconds`);
    }
  
    if (maxStaleness < SMALLEST_MAX_STALENESS_SECONDS) {
      throw new MongoError(
        `maxStalenessSeconds must be at least ${SMALLEST_MAX_STALENESS_SECONDS} seconds`
      );
    }
  
    if (topologyDescription.type === TopologyType.ReplicaSetWithPrimary) {
      const primary = servers.filter(primaryFilter)[0];
      return servers.reduce((result, server) => {
        const stalenessMS =
          server.lastUpdateTime -
          server.lastWriteDate -
          (primary.lastUpdateTime - primary.lastWriteDate) +
          topologyDescription.heartbeatFrequencyMS;
  
        const staleness = stalenessMS / 1000;
        if (staleness <= readPreference.maxStalenessSeconds) result.push(server);
        return result;
      }, []);
    } else if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) {
      const sMax = servers.reduce((max, s) => (s.lastWriteDate > max.lastWriteDate ? s : max));
      return servers.reduce((result, server) => {
        const stalenessMS =
          sMax.lastWriteDate - server.lastWriteDate + topologyDescription.heartbeatFrequencyMS;
  
        const staleness = stalenessMS / 1000;
        if (staleness <= readPreference.maxStalenessSeconds) result.push(server);
        return result;
      }, []);
    }
  
    return servers;
  }
docs: reducers