<commit_before>'use strict';

var
  cleanURL = require('../plugins/cleanurl'),
  XHR = require('./xhr'),
  delay = require('./utils/delay'),
  createError = require('./error').create,
  Response = require('./response'),
  Request = require('./request'),
  extend = require('xtend'),
  once = require('./utils/once');

var i;

function factory(defaults, plugins) {
  defaults = defaults || {};
  plugins = plugins || [];

  function http(req, cb) {
    var xhr, plugin, done, k, timeoutId, supportsLoadAndErrorEvents;

    req = new Request(extend(defaults, req));

    for (i = 0; i < plugins.length; i++) {
      plugin = plugins[i];
      if (plugin.processRequest) {
        plugin.processRequest(req);
      }
    }

    // Give the plugins a chance to create the XHR object
    for (i = 0; i < plugins.length; i++) {
      plugin = plugins[i];
      if (plugin.createXHR) {
        xhr = plugin.createXHR(req);
        break; // First come, first serve
      }
    }
    xhr = xhr || new XHR();

    req.xhr = xhr;

    // Use a single completion callback. This can be called with or without
    // an error. If no error is passed, the request will be examined to see
    // if it was successful.
    done = once(delay(function(rawError) {
      clearTimeout(timeoutId);
      xhr.onload = xhr.onerror = xhr.onabort = xhr.onreadystatechange = xhr.ontimeout = xhr.onprogress = null;

      var err = getError(req, rawError);

      var res = err || Response.fromRequest(req);
      for (i = 0; i < plugins.length; i++) {
        plugin = plugins[i];
        if (plugin.processResponse) {
          plugin.processResponse(res);
        }
      }
      if (err) {
        if (req.onerror) {
          req.onerror(err);
        }
      } else {
        if (req.onload) {
          req.onload(res);
        }
      }
      if (cb) {
        cb(err, err ? undefined : res);
      }
    }));

    supportsLoadAndErrorEvents = ('onload' in xhr) && ('onerror' in xhr);
    xhr.onload = function() { done(); };
    xhr.onerror = done;
    xhr.onabort = function() { done(); };

    // We'd rather use `onload`, `onerror`, and `onabort` since they're the
    // only way to reliably detect successes and failures but, if they
    // aren't available, we fall back to using `onreadystatechange`.
    xhr.onreadystatechange = function() {
      if (xhr.readyState !== 4) return;<commit_msg>Clean up callback invocation a little<commit_after>'use strict';

var
  cleanURL = require('../plugins/cleanurl'),
  XHR = require('./xhr'),
  delay = require('./utils/delay'),
  createError = require('./error').create,
  Response = require('./response'),
  Request = require('./request'),
  extend = require('xtend'),
  once = require('./utils/once');

var i;

function factory(defaults, plugins) {
  defaults = defaults || {};
  plugins = plugins || [];

  function http(req, cb) {
    var xhr, plugin, done, k, timeoutId, supportsLoadAndErrorEvents;

    req = new Request(extend(defaults, req));

    for (i = 0; i < plugins.length; i++) {
      plugin = plugins[i];
      if (plugin.processRequest) {
        plugin.processRequest(req);
      }
    }

    // Give the plugins a chance to create the XHR object
    for (i = 0; i < plugins.length; i++) {
      plugin = plugins[i];
      if (plugin.createXHR) {
        xhr = plugin.createXHR(req);
        break; // First come, first serve
      }
    }
    xhr = xhr || new XHR();

    req.xhr = xhr;

    // Use a single completion callback. This can be called with or without
    // an error. If no error is passed, the request will be examined to see
    // if it was successful.
    done = once(delay(function(rawError) {
      clearTimeout(timeoutId);
      xhr.onload = xhr.onerror = xhr.onabort = xhr.onreadystatechange = xhr.ontimeout = xhr.onprogress = null;

      var err = getError(req, rawError);

      var res = err || Response.fromRequest(req);
      for (i = 0; i < plugins.length; i++) {
        plugin = plugins[i];
        if (plugin.processResponse) {
          plugin.processResponse(res);
        }
      }

      // Invoke callbacks
      if (err && req.onerror) req.onerror(err);
      if (!err && req.onload) req.onload(res);
      if (cb) cb(err, err ? undefined : res);

    }));

    supportsLoadAndErrorEvents = ('onload' in xhr) && ('onerror' in xhr);
    xhr.onload = function() { done(); };
    xhr.onerror = done;
    xhr.onabort = function() { done(); };

    // We'd rather use `onload`, `onerror`, and `onabort` since they're the
    // only way to reliably detect successes and failures but, if they
    // aren't available, we fall back to using `onreadystatechange`.
    xhr.onreadystatechange = function() {
      if (xhr.readyState !== 4) return;
{'fraction_non_alphanumeric': 0.14539682539682539, 'fraction_numerical': 0.0016931216931216932, 'mean_word_length': 2.4852507374631267, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '3456523', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1458, 'n_words': 425}