<commit_before>import { reject } from './helpers/array-helpers';

function trimQueryPartFromURL(url) {
  const queryIndex = url.indexOf('?');
  return queryIndex >= 0 ? url.slice(0, queryIndex) : url;
}

/*
 * The network bridge is a way for the mock websocket object to 'communicate' with
 * all available servers. This is a singleton object so it is important that you
 * clean up urlMap whenever you are finished.
 */
class NetworkBridge {
  constructor() {
    this.urlMap = {};
  }

  /*
   * Attaches a websocket object to the urlMap hash so that it can find the server
   * it is connected to and the server in turn can find it.
   *
   * @param {object} websocket - websocket object to add to the urlMap hash
   * @param {string} url
   */
  attachWebSocket(websocket, url) {
    const serverURL = trimQueryPartFromURL(url);
    const connectionLookup = this.urlMap[serverURL];

    if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) === -1) {
      connectionLookup.websockets.push(websocket);
      return connectionLookup.server;
    }
  }

  /*
   * Attaches a websocket to a room
   */
  addMembershipToRoom(websocket, room) {
    const connectionLookup = this.urlMap[trimQueryPartFromURL(websocket.url)];

    if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) !== -1) {
      if (!connectionLookup.roomMemberships[room]) {
        connectionLookup.roomMemberships[room] = [];
      }

      connectionLookup.roomMemberships[room].push(websocket);
    }
  }

  /*
   * Attaches a server object to the urlMap hash so that it can find a websockets
   * which are connected to it and so that websockets can in turn can find it.
   *
   * @param {object} server - server object to add to the urlMap hash
   * @param {string} url
   */
  attachServer(server, url) {
    const connectionLookup = this.urlMap[url];

    if (!connectionLookup) {
      this.urlMap[url] = {
        server,
        websockets: [],
        roomMemberships: {}
      };

      return server;
    }
  }

  /*
   * Finds the server which is 'running' on the given url.
   *
   * @param {string} url - the url to use to find which server is running on it
   */
  serverLookup(url) {
    const serverURL = trimQueryPartFromURL(url);<commit_msg>trim query params for attachServer method lookup<commit_after>import { reject } from './helpers/array-helpers';

function trimQueryPartFromURL(url) {
  const queryIndex = url.indexOf('?');
  return queryIndex >= 0 ? url.slice(0, queryIndex) : url;
}

/*
 * The network bridge is a way for the mock websocket object to 'communicate' with
 * all available servers. This is a singleton object so it is important that you
 * clean up urlMap whenever you are finished.
 */
class NetworkBridge {
  constructor() {
    this.urlMap = {};
  }

  /*
   * Attaches a websocket object to the urlMap hash so that it can find the server
   * it is connected to and the server in turn can find it.
   *
   * @param {object} websocket - websocket object to add to the urlMap hash
   * @param {string} url
   */
  attachWebSocket(websocket, url) {
    const serverURL = trimQueryPartFromURL(url);
    const connectionLookup = this.urlMap[serverURL];

    if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) === -1) {
      connectionLookup.websockets.push(websocket);
      return connectionLookup.server;
    }
  }

  /*
   * Attaches a websocket to a room
   */
  addMembershipToRoom(websocket, room) {
    const connectionLookup = this.urlMap[trimQueryPartFromURL(websocket.url)];

    if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) !== -1) {
      if (!connectionLookup.roomMemberships[room]) {
        connectionLookup.roomMemberships[room] = [];
      }

      connectionLookup.roomMemberships[room].push(websocket);
    }
  }

  /*
   * Attaches a server object to the urlMap hash so that it can find a websockets
   * which are connected to it and so that websockets can in turn can find it.
   *
   * @param {object} server - server object to add to the urlMap hash
   * @param {string} url
   */
  attachServer(server, url) {
    const serverUrl = trimQueryPartFromURL(url);
    const connectionLookup = this.urlMap[serverUrl];

    if (!connectionLookup) {
      this.urlMap[serverUrl] = {
        server,
        websockets: [],
        roomMemberships: {}
      };

      return server;
    }
  }

  /*
   * Finds the server which is 'running' on the given url.
   *
   * @param {string} url - the url to use to find which server is running on it
   */
  serverLookup(url) {
    const serverURL = trimQueryPartFromURL(url);
{'fraction_non_alphanumeric': 0.10008445945945946, 'fraction_numerical': 0.0016891891891891893, 'mean_word_length': 3.1443569553805775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6235698', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1357, 'n_words': 442}