<commit_before>'use strict';

const getRawBody = require('raw-body')
    , typer = require('media-typer')
    , querystring = require('querystring')
    , events = require('harken')
    , isDefined = require('./tools').isDefined
    , parseForm = require('./parseForm')

    , os = require('os')
    , fs = require('fs')
    , tempPath = os.tmpDir()
    , path = require('path')
    , Busboy = require('busboy')

    , parser = (connection, callback, scope) => {// parse out the body
        const contentType = connection.req.headers['content-type'];

        let encoding = 'UTF-8';

        if (isDefined(contentType)){
            encoding = typer.parse(contentType).parameters.charset || 'UTF-8';
        }

        getRawBody(connection.req, {
            length: connection.req.headers['content-length']
            , limit: '1mb'
            , encoding: encoding
        }, (err, string) => {

            if (err){
                events.emit('error:parse', err);
                callback.apply(scope, [ null, err ]);
                return;
            }

            if (contentType === 'application/x-www-form-urlencoded'){
                callback.apply(scope, [ querystring.parse(string) ]);
                return;
            }

            try {
                callback.apply(scope, [ JSON.parse(string) ]);
            } catch (e) {
                if (contentType === 'application/json') {
                    events.emit('error:parse', e);
                    callback.apply(scope, [ null, e ]);
                } else {
                    callback.apply(scope, [ parseForm(string) ]);
                }
            }
        });
    }

    , fileParser = (connection, callback, scope) => {
        const fileArray = []
            , busboy = new Busboy({ headers: connection.req.headers });

        busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
            // const saveTo = path.join(tempPath, path.basename(fieldname));

            fileArray.push({
                file: file
                , filename: filename
                , mimetype: mimetype
                , encoding: encoding
            });

            // file.pipe(fs.createWriteStream(saveTo));
        });

        busboy.on('finish', () => {
            callback.apply(scope || {}, [ fileArray ]);
        });

        return connection.req.pipe(busboy);
    };

module.exports = parser;
module.exports.files = fileParser;
<commit_msg>Switches multipart parsing to busboy<commit_after>'use strict';

const getRawBody = require('raw-body')
    , typer = require('media-typer')
    , querystring = require('querystring')
    , events = require('harken')
    , isDefined = require('./tools').isDefined
    , parseForm = require('./parseForm')

    , os = require('os')
    , fs = require('fs')
    , path = require('path')
    , Busboy = require('busboy')

    , fileParser = (connection, callback, options) => {
        const form = {}
            , scope = options ? options.scope : null
            // , saveFile = options ? options.saveFile : true
            , busboy = new Busboy({ headers: connection.req.headers });

        busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {

            file.pipe(fs.createWriteStream(path.join(os.tmpdir(), filename)));

            form[fieldname] = {
                tempFile: path.join(os.tmpdir(), filename)
                , encoding: encoding
                , mimetype: mimetype
                , file: file
            };
        });

        busboy.on('field', (fieldname, val) => {
            form[fieldname] = val;
        });

        busboy.on('finish', () => {
            callback.apply(scope, [ form ]);
        });

        connection.req.pipe(busboy);
    }

    , parser = (connection, callback, scopeIn) => {// parse out the body
        const contentType = connection.req.headers['content-type'].split(';')[0]
            , scope = scopeIn;

        let encoding = 'UTF-8';

        if (isDefined(contentType)){
            encoding = typer.parse(contentType).parameters.charset || 'UTF-8';
        }

        if (contentType === 'multipart/form-data') {
            fileParser(connection, callback, { scope: scope });
        } else {
            getRawBody(connection.req, {
                length: connection.req.headers['content-length']
                , limit: '1mb'
                , encoding: encoding
            }, (err, string) => {

                if (err){
                    events.emit('error:parse', err);
                    callback.apply(scope, [ null, err ]);
                    return;
                }

                if (contentType === 'application/x-www-form-urlencoded'){
                    callback.apply(scope, [ querystring.parse(string) ]);
                    return;
                }

                try {
                    callback.apply(scope, [ JSON.parse(string) ]);
                } catch (e) {
                    if (contentType === 'application/json') {
                        events.emit('error:parse', e);
                        callback.apply(scope, [ null, e ]);
                    } else {
                        callback.apply(scope, [ parseForm(string) ]);
                    }
                }
            });
        }

    };

module.exports = parser;
module.exports.files = fileParser;

{'fraction_non_alphanumeric': 0.14357729648991785, 'fraction_numerical': 0.0013069454817027633, 'mean_word_length': 1.5979631425800194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '6765773', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1403, 'n_words': 268}