<commit_before>'use strict';

/**
    Cuddle.js

    @author <NAME>
*/

var https = require('https'),
    http = require('http'),
    url = require('url'),

    logger = {
        log: function () {}
    },

    stringify = function (obj) {
        var ret = [],
            key;

        for (key in obj) {
            ret.push(
                obj[key] === null ? encodeURIComponent(key) : encodeURIComponent(key) + '=' + encodeURIComponent(
                    obj[key])
            );
        }

        return ret.join('&');
    },

    Request = function (method) {
        this.method = method;
        this.secure = false;
        this.started = false;
        this.follow = false;
        this.request_opts = {};
        this.headers = {};
        this.retries = 0;
        this.max_retry = 3;
        this.callbacks = {};
        this.encoding = 'utf8';

        this.to_string = function () {
            return [
                this.method,
                ' ',
                'http',
                this.secured ? 's' : '',
                '://',
                this.host,
                ':',
                this.port,
                this.path,
                '\nPayload:\t',
                JSON.stringify(this.data),
                '\nHeaders:\t',
                JSON.stringify(this.headers)
            ].join('');
        };

        this.raw = function () {
            console.log('\tcudl.raw() is deprecated');
            return this;
        };

        this.to = function (host, port, path) {

            if (!port && !path) {
                host = url.parse(host);
                this.host = host.hostname;
                this.path = host.path;
                this.port = host.port ? host.port : 80;

                if (host.protocol === 'https:') {
                    this.port = 443;
                    this.secure = true;
                }
            }
            else {
                this.path = path;
                this.host = host;
                this.port = port;
            }

            this.request_opts = {
                host: this.host,
                port: this.port
            };

            return this;
        };

        this.set_max_retry = function (max) {
            this.max_retry = max;
            return this;
        };

        this.secured = function () {
            this.secure = true;
            return this;
        };

        this.add_header = function (key, value) {
            this.headers[key] = value;
            return this;
        };

        this.add_opts = function (key, value) {
            this.request_opts[key] = value;
            return this;
        };

        this.then = function (cb) {
            if (!this.cb) {
                this.cb = cb;
            }

            if (!this.started) {
                this.send();
            }
            return this;
        };

        this.args = function () {
            this.additional_arguments = arguments;
            return this;
        };

        this.set_before_json = function (fn) {
            this.before_json = fn;
            return this;
        };

        this.set_encoding = function (encoding) {
            this.encoding = encoding;
            return this;
        };

        this.follow_redirects = function (max_redirects) {
            this.max_redirects = +max_redirects || 3;
            this.follow = true;
            return this;
        };

        this.stringify = stringify;

        this.retry = function () {
            this.retries++;
            if (this.retries > this.max_retry) {
                logger.log('error', 'Reached max retries');
                this.cb({
                        message: 'Reached max retries',
                        url: this.host + ':' + this.port + this.path
                    },
                    null,
                    this,
                    this.additional_arguments
                );
                return this;
            }
            logger.log('warn', 'Retrying request');
            return this.send(this.data);
        };

        this.pipe = function (stream) {
            logger.log('verbose', 'Piping file..');
            this._stream = stream;
            return this;
        };

        this.on = function (event_type, next) {

            if (this.callbacks[event_type]) {
                this.callbacks[event_type] = this.callbacks[event_type].push(next);
            }

            this.callbacks[event_type] = [next];

            return this;
        };

        this.emit = function (event_type, err, response) {
            if (typeof (this.callbacks[event_type])) {
                this.callbacks[event_type].forEach(function (callback) {
                    callback(err, response);
                });
            }

            return this;
        };

        this.send = function (data) {
            var new_path = this.path,
                self = this,
                protocol,
                payload,
                req;

            this.started = true;
            this.data = data;

            if (data && this.method === 'GET') {
                new_path += '?' + this.stringify(data);
            }
            else {
                if (!this.headers['Content-Type']) {
                    payload = this.stringify(data);
                    this.headers['Content-Type'] = 'application/x-www-form-urlencoded';
                    this.headers['Content-Length'] = payload.length;<commit_msg>fixed callback typo<commit_after>'use strict';

/**
    Cuddle.js

    @author <NAME>
*/

var https = require('https'),
    http = require('http'),
    url = require('url'),

    logger = {
        log: function () {}
    },

    stringify = function (obj) {
        var ret = [],
            key;

        for (key in obj) {
            ret.push(
                obj[key] === null ? encodeURIComponent(key) : encodeURIComponent(key) + '=' + encodeURIComponent(
                    obj[key])
            );
        }

        return ret.join('&');
    },

    Request = function (method) {
        this.method = method;
        this.secure = false;
        this.started = false;
        this.follow = false;
        this.request_opts = {};
        this.headers = {};
        this.retries = 0;
        this.max_retry = 3;
        this.callbacks = {};
        this.encoding = 'utf8';

        this.to_string = function () {
            return [
                this.method,
                ' ',
                'http',
                this.secured ? 's' : '',
                '://',
                this.host,
                ':',
                this.port,
                this.path,
                '\nPayload:\t',
                JSON.stringify(this.data),
                '\nHeaders:\t',
                JSON.stringify(this.headers)
            ].join('');
        };

        this.raw = function () {
            console.log('\tcudl.raw() is deprecated');
            return this;
        };

        this.to = function (host, port, path) {

            if (!port && !path) {
                host = url.parse(host);
                this.host = host.hostname;
                this.path = host.path;
                this.port = host.port ? host.port : 80;

                if (host.protocol === 'https:') {
                    this.port = 443;
                    this.secure = true;
                }
            }
            else {
                this.path = path;
                this.host = host;
                this.port = port;
            }

            this.request_opts = {
                host: this.host,
                port: this.port
            };

            return this;
        };

        this.set_max_retry = function (max) {
            this.max_retry = max;
            return this;
        };

        this.secured = function () {
            this.secure = true;
            return this;
        };

        this.add_header = function (key, value) {
            this.headers[key] = value;
            return this;
        };

        this.add_opts = function (key, value) {
            this.request_opts[key] = value;
            return this;
        };

        this.then = function (cb) {
            if (!this.cb) {
                this.cb = cb;
            }

            if (!this.started) {
                this.send();
            }
            return this;
        };

        this.args = function () {
            this.additional_arguments = arguments;
            return this;
        };

        this.set_before_json = function (fn) {
            this.before_json = fn;
            return this;
        };

        this.set_encoding = function (encoding) {
            this.encoding = encoding;
            return this;
        };

        this.follow_redirects = function (max_redirects) {
            this.max_redirects = +max_redirects || 3;
            this.follow = true;
            return this;
        };

        this.stringify = stringify;

        this.retry = function () {
            this.retries++;
            if (this.retries > this.max_retry) {
                logger.log('error', 'Reached max retries');
                this.cb({
                        message: 'Reached max retries',
                        url: this.host + ':' + this.port + this.path
                    },
                    null,
                    this,
                    this.additional_arguments
                );
                return this;
            }
            logger.log('warn', 'Retrying request');
            return this.send(this.data);
        };

        this.pipe = function (stream) {
            logger.log('verbose', 'Piping file..');
            this._stream = stream;
            return this;
        };

        this.on = function (event_type, next) {

            if (this.callbacks[event_type]) {
                this.callbacks[event_type] = this.callbacks[event_type].push(next);
            }

            this.callbacks[event_type] = [next];

            return this;
        };

        this.emit = function (event_type, err, response) {
            if (this.callbacks[event_type]) {
                this.callbacks[event_type].forEach(function (callback) {
                    callback(err, response);
                });
            }

            return this;
        };

        this.send = function (data) {
            var new_path = this.path,
                self = this,
                protocol,
                payload,
                req;

            this.started = true;
            this.data = data;

            if (data && this.method === 'GET') {
                new_path += '?' + this.stringify(data);
            }
            else {
                if (!this.headers['Content-Type']) {
                    payload = this.stringify(data);
                    this.headers['Content-Type'] = 'application/x-www-form-urlencoded';
                    this.headers['Content-Length'] = payload.length;
{'fraction_non_alphanumeric': 0.11981398741679584, 'fraction_numerical': 0.001641287498860217, 'mean_word_length': 1.0796359499431172, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '840042', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2802, 'n_words': 566}