<commit_before>'use strict';

function passthrough(originalReq, originalRes, proxyRes){
  console.log(proxyRes.statusCode, 'PASSTHROUGH', originalReq.url);

  originalRes.writeHead(
    proxyRes.statusCode,
    proxyRes.statusMessage,
    proxyRes.headers
  );

  proxyRes.pipe(originalRes);
}

function proxyReponder(porous, originalReq, originalRes) {
  return function proxyRespond(proxyRes) {
    return passthrough(originalReq, originalRes, proxyRes);
  };
}

module.exports = proxyReponder;<commit_msg>Add rough middleware support to proxyResponder<commit_after>'use strict';

var zlib = require('zlib');
var handleError = require('./handleError');

function passthrough(originalReq, originalRes, proxyRes){
  console.log(proxyRes.statusCode, 'PASSTHROUGH', originalReq.url);

  originalRes.writeHead(
    proxyRes.statusCode,
    proxyRes.statusMessage,
    proxyRes.headers
  );

  proxyRes.pipe(originalRes);
}

function callMiddleware(middlewares, req, res, done){
  function next(){
    callMiddleware(middlewares, req, res, done);
  }

  var middleware = middlewares.shift();

  if (middleware) {
    return middleware.handle(req, res, next);
  }
  done();
}

function ableToAlter(proxyRes){
  return proxyRes.headers['content-type'] &&
    proxyRes.headers['content-type'].indexOf('text/html') !== -1;
}

function getResponder(porous, originalReq, originalRes, proxyRes) {
  function respond(){
    console.log(proxyRes.statusCode, originalReq.url);

    var headers = proxyRes.headers;
    headers['content-encoding'] = 'utf8';
    headers['content-length'] = Buffer.byteLength(proxyRes.body, 'utf8');

    originalRes.writeHead(proxyRes.statusCode, proxyRes.statusMessage, headers);
    originalRes.end(proxyRes.body, 'utf8');
  }

  return function responder() {
    callMiddleware(porous.middlewares, originalReq, proxyRes, respond);
  };
}

function proxyReponder(porous, originalReq, originalRes) {
  return function proxyRespond(proxyRes) {
    if (!ableToAlter) {
      return passthrough(originalReq, originalRes, proxyRes);
    }

    proxyRes.body = '';

    function concatChunk(chunk) {
      proxyRes.body += chunk;
    }

    if(proxyRes.headers['content-encoding'] === 'gzip'){
      var gunzip = zlib.createGunzip();
      proxyRes.pipe(gunzip);

      gunzip
        .on('data', concatChunk)
        .on('error', handleError)
        .on('end', getResponder(porous, originalReq, originalRes, proxyRes));
    } else {
      proxyRes
        .on('data', concatChunk)
        .on('error', handleError)
        .on('end', getResponder(porous, originalReq, originalRes, proxyRes));
    }
  };
}

module.exports = proxyReponder;
{'fraction_non_alphanumeric': 0.13192612137203166, 'fraction_numerical': 0.0015077271013946476, 'mean_word_length': 4.24505928853755, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4360994', 'n_tokens_mistral': 848, 'n_tokens_neox': 825, 'n_words': 154}