<commit_before>const common = require('./common')

module.exports = function transformResponseBody(middleware, filter) {
  return function (req, res, next) {
    // Apply the request filter is necessary
    if (filter && !common.filterRequest(filter, res)) {
      return next()
    }

    // If body field is already present, just continue with it
    if (res.body) {
      return middleware(req, res, next)
    }

    var buf = []
    var length = 0
    var closed = false
    var headArgs = null

    var _end = res.end
    var _write = res.write
    var _writeHead = res.writeHead

    // Wrap native http.OutgoingMessage methods
    res.writeHead = function (code, message, headers) {
      headArgs = Array.apply(null, arguments)
    }

    res.write = function (data) {
      var cb = getCallback(arguments)
      if (closed) return cb && cb()

      if (data) {
        length += +data.length || 0
        buf.push(data)
      }

      if (cb) cb()
    }

    res.end = function (data) {
      var cb = getCallback(arguments)
      if (closed) return cb && cb()

      if (data && typeof data !== 'function') {
        buf.push(data)
      }

      var body = Buffer.concat(buf, length)

      // Expose the body
      res.body = res._originalBody = body

      // Restore and clean references
      cleanupAndRestore()

      middleware(req, res, finisher(cb))
    }

    req.on('close', onClose)

    function onClose() {
      closed = true
      cleanupAndRestore()
    }

    function cleanupAndRestore() {
      // Restore methods
      res.writeHead = _writeHead
      res.write = _write
      res.end = _end

      // Clean references to prevent leaks
      buf = body = _end = _write = headArgs = null
      req.removeListener('close', onClose)
    }

    function finisher(cb) {
      return function (err, body, encoding) {
        if (err) {
          res.statusCode = +err.status || 500
          return res.end(err.message || err)
        }

        if (!res.headersSent && headArgs) {
          res.writeHead.apply(res, headArgs)
        }

        if (body) {<commit_msg>refactor(responseBody): handle client close event<commit_after>const common = require('./common')

module.exports = function transformResponseBody(middleware, filter) {
  return function (req, res, next) {
    // Apply the request filter is necessary
    if (filter && !common.filterRequest(filter, res)) {
      return next()
    }

    // If body field is already present, just continue with it
    if (res.body) {
      return middleware(req, res, next)
    }

    var buf = []
    var length = 0
    var closed = false
    var headArgs = null

    var _end = res.end
    var _write = res.write
    var _writeHead = res.writeHead

    // Wrap native http.OutgoingMessage methods
    res.writeHead = function (code, message, headers) {
      headArgs = Array.apply(null, arguments)
    }

    res.write = function (data) {
      var cb = getCallback(arguments)
      if (closed) return cb && cb()

      if (data) {
        length += +data.length || 0
        buf.push(data)
      }

      if (cb) cb()
    }

    res.end = function (data) {
      var cb = getCallback(arguments)
      if (closed) return cb && cb()

      if (data && typeof data !== 'function') {
        buf.push(data)
      }

      var body = Buffer.concat(buf, length)

      // Expose the body
      res.body = res._originalBody = body

      // Restore and clean references
      cleanupAndRestore()

      middleware(req, res, finisher(cb))
    }

    req.on('close', onClose)

    function onClose() {
      closed = true
      cleanupAndRestore()
    }

    function cleanupAndRestore() {
      // Restore methods
      res.writeHead = _writeHead
      res.write = _write
      res.end = _end

      // Clean references to prevent leaks
      buf = body = _end = _write = headArgs = null
      req.removeListener('close', onClose)
    }

    function finisher(cb) {
      return function (err, body, encoding) {
        if (closed) return cb && cb()

        if (err) {
          res.statusCode = +err.status || 500
          return res.end(err.message || err)
        }

        if (!res.headersSent && headArgs) {
          res.writeHead.apply(res, headArgs)
        }

        if (body) {
{'fraction_non_alphanumeric': 0.11277842907385698, 'fraction_numerical': 0.0023446658851113715, 'mean_word_length': 2.069064748201439, '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': '2339616', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1244, 'n_words': 367}