<commit_before>/**
 * Alias for require('http') that uses real http or fake-http modules and dispatches events.
 */

const Channel = require('chnl');
const logger = require('../utils/logger').create('Http');
const httpBrowserify = require('http-browserify');
const httpsBrowserify = require('https-browserify');
const fakeHttp = require('../utils/fake-http');

const LOOPBACK_HOST = 'autotester';

// intercept for event dispatching
const realHttpWrapped = intercept(httpBrowserify);
const realHttpsWrapped = intercept(httpsBrowserify);
const fakeHttpWrapped = intercept(fakeHttp);

exports.request = function (opts) {
  const http = opts.hostname === LOOPBACK_HOST
    ? fakeHttpWrapped
    : (opts.protocol === 'https:' ? realHttpsWrapped : realHttpWrapped);
  return http.request.apply(http, arguments);
};

exports.onRequest = new Channel();
exports.onRequestData = new Channel();
exports.onResponse = new Channel();

function intercept(http) {
  const origRequest = http.request;
  http.request = function (options, callback) {
    const request = origRequest.apply(http, arguments);
    // todo: dispatch async everywhere, to not break flow
    exports.onRequest.dispatch({request, options});
    request.on('response', response => {
      const chunks = [];
      response.on('data', chunk => chunks.push(chunk));
      response.on('end', () => {
        exports.onResponse.dispatch({request, options, response, data: chunks.join('')});
      });
    });

    const origWrite = request.write;
    request.write = function (data) {
      exports.onRequestData.dispatch({request, data});
      return origWrite.apply(request, arguments);
    };

    return request;
  };
  return http;
}
<commit_msg>use dispatchAsync in http alias events<commit_after>/**
 * Alias for require('http') that uses real http or fake-http modules and dispatches events.
 */

const Channel = require('chnl');
const logger = require('../utils/logger').create('Http');
const httpBrowserify = require('http-browserify');
const httpsBrowserify = require('https-browserify');
const fakeHttp = require('../utils/fake-http');

const LOOPBACK_HOST = 'autotester';

// intercept for event dispatching
const realHttpWrapped = intercept(httpBrowserify);
const realHttpsWrapped = intercept(httpsBrowserify);
const fakeHttpWrapped = intercept(fakeHttp);

exports.request = function (opts) {
  const http = opts.hostname === LOOPBACK_HOST
    ? fakeHttpWrapped
    : (opts.protocol === 'https:' ? realHttpsWrapped : realHttpWrapped);
  return http.request.apply(http, arguments);
};

exports.onRequest = new Channel();
exports.onRequestData = new Channel();
exports.onResponse = new Channel();

function intercept(http) {
  const origRequest = http.request;
  http.request = function (options, callback) {
    const request = origRequest.apply(http, arguments);
    exports.onRequest.dispatchAsync({request, options});
    request.on('response', response => {
      const chunks = [];
      response.on('data', chunk => chunks.push(chunk));
      response.on('end', () => {
        exports.onResponse.dispatchAsync({request, options, response, data: chunks.join('')});
      });
    });

    const origWrite = request.write;
    request.write = function (data) {
      exports.onRequestData.dispatchAsync({request, data});
      return origWrite.apply(request, arguments);
    };

    return request;
  };
  return http;
}

{'fraction_non_alphanumeric': 0.1403301886792453, 'fraction_numerical': 0.0, 'mean_word_length': 4.343307086614173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5585868', 'n_tokens_mistral': 955, 'n_tokens_neox': 942, 'n_words': 237}