<commit_before>// Load modules

var Async = require('async');
var Negotiator = require('negotiator');
var Zlib = require('zlib');
var Base = require('./base');
var Headers = require('./headers');
var Utils = require('../utils');
var Boom = require('boom');


// Declare internals

var internals = {};


// Generic response  (Base -> Generic)

exports = module.exports = internals.Generic = function () {

    Utils.assert(this.constructor !== internals.Generic, 'Generic must not be instantiated directly');

    Base.call(this);
    this.variety = 'generic';
    this.varieties.generic = true;

    this._code = 200;
    this._payload = [];
    this._headers = {};

    return this;
};

Utils.inherits(internals.Generic, Base);


internals.Generic.prototype.getTtl = function () {

    return this._code === 200 ? this._flags.ttl : 0;
};


internals.Generic.prototype._prepare = function (request, callback) {

    var self = this;

    this._wasPrepared = true;

    var length = 0;
    this._payload.forEach(function (chunk) {
        length += Buffer.byteLength(chunk);
    });

    if (request.jsonp) {
        this.header('Content-Type', 'text/javascript');
        length += request.jsonp.length + 3;

        var payload = [request.jsonp, '('];
        this._payload.forEach(function (chunk) {
            payload.push(chunk.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'));
        });
        payload.push(');');
        this._payload = payload;
    }

    this.header('Content-Length', length);

    if (this._flags.location) {
        this._headers.Location = Headers.location(this._flags.location, request);
    }

    Headers.cache(this, request);
    Headers.cors(this, request);
    Headers.state(this, request, function (err) {

        if (err) {
            return callback(err);
        }

        return callback(self);
    });
};


internals.Generic.prototype._transmit = function (request, callback) {

    var self = this;

    var prepare = function () {
        
        if (!self._payload.length ||
            request.method === 'head') {

            return send();
        }

        var availableEncodings = ['gzip', 'deflate', 'identity'];
        var negotiator = new Negotiator(request.raw.req);
        var encoding = negotiator.preferredEncoding(availableEncodings);

        if (encoding === 'gzip') {
            return prepareGzip();
        }
        else if (encoding === 'deflate') {
            return prepareDeflate();
        }

        return send(self._payload);
    };
<commit_msg>Small refactor<commit_after>// Load modules

var Async = require('async');
var Negotiator = require('negotiator');
var Zlib = require('zlib');
var Base = require('./base');
var Headers = require('./headers');
var Utils = require('../utils');
var Boom = require('boom');


// Declare internals

var internals = {};


// Generic response  (Base -> Generic)

exports = module.exports = internals.Generic = function () {

    Utils.assert(this.constructor !== internals.Generic, 'Generic must not be instantiated directly');

    Base.call(this);
    this.variety = 'generic';
    this.varieties.generic = true;

    this._code = 200;
    this._payload = [];
    this._headers = {};

    return this;
};

Utils.inherits(internals.Generic, Base);


internals.Generic.prototype.getTtl = function () {

    return this._code === 200 ? this._flags.ttl : 0;
};


internals.Generic.prototype._prepare = function (request, callback) {

    var self = this;

    this._wasPrepared = true;

    var length = 0;
    this._payload.forEach(function (chunk) {
        length += Buffer.byteLength(chunk);
    });

    if (request.jsonp) {
        this.header('Content-Type', 'text/javascript');
        length += request.jsonp.length + 3;

        var payload = [request.jsonp, '('];
        this._payload.forEach(function (chunk) {
            payload.push(chunk.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'));
        });
        payload.push(');');
        this._payload = payload;
    }

    this.header('Content-Length', length);

    if (this._flags.location) {
        this._headers.Location = Headers.location(this._flags.location, request);
    }

    Headers.cache(this, request);
    Headers.cors(this, request);
    Headers.state(this, request, function (err) {

        if (err) {
            return callback(err);
        }

        return callback(self);
    });
};


internals.Generic.prototype._transmit = function (request, callback) {

    var self = this;

    var prepare = function () {
        
        if (!self._payload.length ||
            request.method === 'head') {

            return send();
        }

        var availableEncodings = ['gzip', 'deflate', 'identity'];
        var negotiator = new Negotiator(request.raw.req);
        var encoding = negotiator.preferredEncoding(availableEncodings);

        if (encoding === 'gzip') {
            return prepareGzip();
        }
        if (encoding === 'deflate') {
            return prepareDeflate();
        }

        return send(self._payload);
    };

{'fraction_non_alphanumeric': 0.15242221346987003, 'fraction_numerical': 0.009846396218983852, 'mean_word_length': 2.514878892733564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4002706', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1465, 'n_words': 320}