<commit_before>/*!
 * SubEtha-Client v0.0.0
 * http://github.com/bemson/subetha/
 *
 * Copyright, <NAME>
 * Released under the MIT License
 */
/* global define, require */
!function (inAMD, inCJS, Array, Date, Math, JSON, Object, RegExp, scope, undefined) {

  function initSubEtha() {

    var
      Morus = ((inCJS || inAMD) ? require('morus') : scope.Morus),
      JSONstringify = JSON.stringify,
      JSONparse = JSON.parse,
      mathRandom = Math.random,
      protoHas = Object.prototype.hasOwnProperty,
      protoSlice = Array.prototype.slice,
      doc = document,
      docBody,
      domReady = 0,
      rxp_guid = /[xy]/g,
      guidPattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',
      speakerKey = mathRandom(),
      exchangeDelimiter = '~',
      STATE_INITIAL = 0,
      STATE_QUEUED = 1,
      STATE_PENDING = 2,
      STATE_READY = 3,
      STATE_CLOSING = 4,
      JOIN_EVENT = '::join',
      DROP_EVENT = '::drop',
      CONNECT_EVENT = '::connect',
      DISCONNECT_EVENT = '::disconnect',
      // tests whether a string looks like a domain
      /*
      should pass:
        a.co
        a.co/
        a.com
        a.com/
        a.b.co
        a.b.co/
        a.b.com
        a.b.com/
        a-b.co
        a-b.co/
        a-b.com
        a-b.com/

      in order to prefix with "//"
      */
      r_domainish = /^([\w\-]+\.)+[conmi]\w{1,2}\b/,
      // ensures decoded data looks like a json
      r_jsonish = /^\s*{.+}\s*/,
      // for domainish urls, use http when in "file:" protocol
      networkPrefix = !location.protocol.indexOf('f') ? 'http://' : '//',
      defaultNetwork = 'about:blank',
      defaultChannel = 'public',
      ethaDiv = doc.createElement('div'),
      bridges = {},
      bridgeCnt = 0,
      // clients queued to open (before DOMReady)
      clientQueue = {},
      protocolVersion = 'se-0',
      privateKey = {},
      // add to global queue (before DOMReady)
      addClient = function (client) {
        clientQueue[client.id] = client;
      },
      // remove from global queue (before DOMReady)
      removeClient = function (client) {
        delete clientQueue[client.id];
        setClientState(client, STATE_INITIAL);
      },
      canPostObjects = !!function () {
        var yes = 1;

        // synchronous check for postMessage object support!
        // thx gregers@http://stackoverflow.com/a/20743286
        try {
          scope.postMessage({
            toString: function () {
              yes = 0;
            }
          }, '*');
        } catch (e) {}

        return yes;
      }(),
      postMessage = canPostObjects ?
        function (win, msg) {
          win.postMessage(msg, '*');
        } :
        function (win, msg) {
          win.postMessage(JSONstringify(msg), '*');
        },
      bind = scope.attachEvent ?
        function (object, eventName, callback) {
          object.attachEvent('on' + eventName, callback);
        } :
        function (object, eventName, callback) {
          object.addEventListener(eventName, callback, false);
        },
      unbind = scope.attachEvent ?
        function (object, eventName, callback) {
          object.detachEvent('on' + eventName, callback);
        } :
        function (object, eventName, callback) {
          object.removeEventListener(eventName, callback, false);
        },
      isArray = typeof Array.isArray === 'function' ?
        Array.isArray :
        function (obj) {
          return obj instanceof Array;
        },
      objectKeys = typeof Object.keys === 'function' ?
        Object.keys :
        function (obj) {
          var
            ary = [],
            key;

          if (obj && typeof obj === 'object') {
            for (key in obj) {
              if (protoHas.call(obj, key)) {
                ary.push(key);
              }
            }
          }
          return ary;
        },

      /*
      handler signature
        1. bridge instance
        2. msg (in payload)
        3. payload (decoded from event)
        4. native post-message event
      */
      bridgeDataHandlers = {

        // first message expected by bridge
        /*
        payload structure
        {                  [payload]
          mid: <guid>,
          type: "ready",
          sent: <date>,
          msg: <uri>            [msg]
        }
        */
        ready: function (bridge, origin) {
          var
            clients = bridge.clients,
            clientId;

          // cancel timer
          bridge.dDay();

          // capture bridge origin, for logging?
          bridge.origin = origin;

          // note that bridge is ready
          bridge.state = STATE_READY;

          // authorize queued clients
          for (clientId in clients) {
            bridge.auth(clients[clientId]);
          }
        },

        // auth response from bridge
        /*
        payload structure
        {                 [payload]
          mid: <guid>,
          type: "auth",
          sent: <date>,
          msg: {          [msg]
            id: <guid>,
            ok: booly,
            peers: {
              <guid>: {
                id: <guid>,
                channel: <channel-name>,
                origin: <uri>
              },
              ...
            },       // optional for failures
            reason: <string> // optional for faliures
          }
        }
        */
        auth: function  (bridge, msg) {
          var
            channels = bridge.channels,
            clients = bridge.clients,
            clientId = msg.id,
            hasPeers,
            client,
            clientPeers,
            networkPeers,
            peerId,
            channelName;

          if (!protoHas.call(clients, clientId)) {
            // avoid unknown clients
            return;
          }

          // target client
          client = clients[clientId];
          // get peers
          networkPeers = msg.peers;

          // determine whether to keep or reject client
          if (
            // rejected
            !msg.ok ||
            // unwarranted authorization (wrong state client)
            client.state !== STATE_PENDING
          ) {
            // boot client
            bridge.remove(client);
            return;
          }

          // clear peers
          client.peers = {};
          // add pre-existing peers
          for (peerId in networkPeers) {
            hasPeers = 1;
            addPeerToClient(client, networkPeers[peerId]);
          }

          // remove closured creds method - reveals prototyped noOp method
          delete client.creds;

          // add to channel index
          channelName = client.channel;
          if (!protoHas.call(channels, channelName)) {
            channels[channelName] = {};
            bridge.channelCnts[channelName] = 0;
          }
          // add to channels index
          channels[channelName][clientId] = client;
          bridge.channelCnts[channelName]++;

          // announce ready state<commit_msg>Clients firing join events at authentication, for pre-existing peers<commit_after>/*!
 * SubEtha-Client v0.0.0
 * http://github.com/bemson/subetha/
 *
 * Copyright, <NAME>
 * Released under the MIT License
 */
/* global define, require */
!function (inAMD, inCJS, Array, Date, Math, JSON, Object, RegExp, scope, undefined) {

  function initSubEtha() {

    var
      Morus = ((inCJS || inAMD) ? require('morus') : scope.Morus),
      JSONstringify = JSON.stringify,
      JSONparse = JSON.parse,
      mathRandom = Math.random,
      protoHas = Object.prototype.hasOwnProperty,
      protoSlice = Array.prototype.slice,
      doc = document,
      docBody,
      domReady = 0,
      rxp_guid = /[xy]/g,
      guidPattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',
      speakerKey = mathRandom(),
      exchangeDelimiter = '~',
      STATE_INITIAL = 0,
      STATE_QUEUED = 1,
      STATE_PENDING = 2,
      STATE_READY = 3,
      STATE_CLOSING = 4,
      JOIN_EVENT = '::join',
      DROP_EVENT = '::drop',
      CONNECT_EVENT = '::connect',
      DISCONNECT_EVENT = '::disconnect',
      // tests whether a string looks like a domain
      /*
      should pass:
        a.co
        a.co/
        a.com
        a.com/
        a.b.co
        a.b.co/
        a.b.com
        a.b.com/
        a-b.co
        a-b.co/
        a-b.com
        a-b.com/

      in order to prefix with "//"
      */
      r_domainish = /^([\w\-]+\.)+[conmi]\w{1,2}\b/,
      // ensures decoded data looks like a json
      r_jsonish = /^\s*{.+}\s*/,
      // for domainish urls, use http when in "file:" protocol
      networkPrefix = !location.protocol.indexOf('f') ? 'http://' : '//',
      defaultNetwork = 'about:blank',
      defaultChannel = 'public',
      ethaDiv = doc.createElement('div'),
      bridges = {},
      bridgeCnt = 0,
      // clients queued to open (before DOMReady)
      clientQueue = {},
      protocolVersion = 'se-0',
      privateKey = {},
      // add to global queue (before DOMReady)
      addClient = function (client) {
        clientQueue[client.id] = client;
      },
      // remove from global queue (before DOMReady)
      removeClient = function (client) {
        delete clientQueue[client.id];
        setClientState(client, STATE_INITIAL);
      },
      canPostObjects = !!function () {
        var yes = 1;

        // synchronous check for postMessage object support!
        // thx gregers@http://stackoverflow.com/a/20743286
        try {
          scope.postMessage({
            toString: function () {
              yes = 0;
            }
          }, '*');
        } catch (e) {}

        return yes;
      }(),
      postMessage = canPostObjects ?
        function (win, msg) {
          win.postMessage(msg, '*');
        } :
        function (win, msg) {
          win.postMessage(JSONstringify(msg), '*');
        },
      bind = scope.attachEvent ?
        function (object, eventName, callback) {
          object.attachEvent('on' + eventName, callback);
        } :
        function (object, eventName, callback) {
          object.addEventListener(eventName, callback, false);
        },
      unbind = scope.attachEvent ?
        function (object, eventName, callback) {
          object.detachEvent('on' + eventName, callback);
        } :
        function (object, eventName, callback) {
          object.removeEventListener(eventName, callback, false);
        },
      isArray = typeof Array.isArray === 'function' ?
        Array.isArray :
        function (obj) {
          return obj instanceof Array;
        },
      objectKeys = typeof Object.keys === 'function' ?
        Object.keys :
        function (obj) {
          var
            ary = [],
            key;

          if (obj && typeof obj === 'object') {
            for (key in obj) {
              if (protoHas.call(obj, key)) {
                ary.push(key);
              }
            }
          }
          return ary;
        },

      /*
      handler signature
        1. bridge instance
        2. msg (in payload)
        3. payload (decoded from event)
        4. native post-message event
      */
      bridgeDataHandlers = {

        // first message expected by bridge
        /*
        payload structure
        {                  [payload]
          mid: <guid>,
          type: "ready",
          sent: <date>,
          msg: <uri>            [msg]
        }
        */
        ready: function (bridge, origin) {
          var
            clients = bridge.clients,
            clientId;

          // cancel timer
          bridge.dDay();

          // capture bridge origin, for logging?
          bridge.origin = origin;

          // note that bridge is ready
          bridge.state = STATE_READY;

          // authorize queued clients
          for (clientId in clients) {
            bridge.auth(clients[clientId]);
          }
        },

        // auth response from bridge
        /*
        payload structure
        {                 [payload]
          mid: <guid>,
          type: "auth",
          sent: <date>,
          msg: {          [msg]
            id: <guid>,
            ok: booly,
            peers: {
              <guid>: {
                id: <guid>,
                channel: <channel-name>,
                origin: <uri>
              },
              ...
            },       // optional for failures
            reason: <string> // optional for faliures
          }
        }
        */
        auth: function  (bridge, msg) {
          var
            channels = bridge.channels,
            clients = bridge.clients,
            clientId = msg.id,
            hasPeers,
            client,
            clientPeers,
            networkPeers,
            peerId,
            channelName;

          if (!protoHas.call(clients, clientId)) {
            // avoid unknown clients
            return;
          }

          // target client
          client = clients[clientId];
          // get peers
          networkPeers = msg.peers;

          // determine whether to keep or reject client
          if (
            // rejected
            !msg.ok ||
            // unwarranted authorization (wrong state client)
            client.state !== STATE_PENDING
          ) {
            // boot client
            bridge.remove(client);
            return;
          }

          // clear peers
          clientPeers = client.peers = {};
          // add pre-existing peers
          for (peerId in networkPeers) {
            hasPeers = 1;
            addPeerToClient(client, networkPeers[peerId]);
          }

          // remove closured creds method - reveals prototyped noOp method
          delete client.creds;

          // add to channel index
          channelName = client.channel;
          if (!protoHas.call(channels, channelName)) {
            channels[channelName] = {};
            bridge.channelCnts[channelName] = 0;
          }
          // add to channels index
          channels[channelName][clientId] = client;
          bridge.channelCnts[channelName]++;

          // announce ready state
{'fraction_non_alphanumeric': 0.11472614903743696, 'fraction_numerical': 0.004262271790864531, 'mean_word_length': 1.3950323239197007, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '2059825', 'n_tokens_mistral': 3941, 'n_tokens_neox': 3818, 'n_words': 948}