<commit_before>var Stream = require('stream'),
    gm = require('gm'),
    isGmOperationByName = {},
    filterConstructorByOperationName = {};

['PngQuant', 'PngCrush', 'OptiPng', 'JpegTran'].forEach(function (constructorName) {
    filterConstructorByOperationName[constructorName.toLowerCase()] = require(constructorName.toLowerCase());
});

Object.keys(gm.prototype).forEach(function (propertyName) {
    if (!/^_|^(?:size|orientation|format|depth|color|res|filesize|identity|write|stream)$/.test(propertyName) &&
        typeof gm.prototype[propertyName] === 'function') {
        isGmOperationByName[propertyName] = true;
    }
});

module.exports = function getFiltersAndTargetContentTypeFromQueryString(queryString) {
    var filters = [],
        gmOperations = [],
        usedQueryStringFragments = [],
        leftOverQueryStringFragments = [],
        targetContentType;

    function flushGmOperations() {
        if (gmOperations.length > 0) {
            // For some reason the gm module doesn't expose itself as a readable/writable stream,
            // so we need to wrap it into one:

            var readStream = new Stream();
            readStream.readable = true;

            var readWriteStream = new Stream();
            readWriteStream.readable = readWriteStream.writable = true;
            readWriteStream.write = function (chunk) {
                readStream.emit('data', chunk);
            };
            readWriteStream.end = function (chunk) {
                readStream.emit('end');
            };

            var gmInstance = gm(readStream);
            gmOperations.forEach(function (gmOperation) {
                gmInstance = gmInstance[gmOperation.name].apply(gmInstance, gmOperation.args);
            });
            var seenData = false,
                hasEnded = false;
            gmInstance.stream(function (err, stdout, stderr) {
                if (err) {
                    hasEnded = true;
                    return readWriteStream.emit('error', err);
                }
                stdout.on('data', function (chunk) {
                    seenData = true;
                    readWriteStream.emit('data', chunk);
                }).on('end', function () {<commit_msg>getFiltersAndTargetContentTypeFromQueryString: Fixed end method of the readable/writable stream wrapped around the gm module.<commit_after>var Stream = require('stream'),
    gm = require('gm'),
    isGmOperationByName = {},
    filterConstructorByOperationName = {};

['PngQuant', 'PngCrush', 'OptiPng', 'JpegTran'].forEach(function (constructorName) {
    filterConstructorByOperationName[constructorName.toLowerCase()] = require(constructorName.toLowerCase());
});

Object.keys(gm.prototype).forEach(function (propertyName) {
    if (!/^_|^(?:size|orientation|format|depth|color|res|filesize|identity|write|stream)$/.test(propertyName) &&
        typeof gm.prototype[propertyName] === 'function') {
        isGmOperationByName[propertyName] = true;
    }
});

module.exports = function getFiltersAndTargetContentTypeFromQueryString(queryString) {
    var filters = [],
        gmOperations = [],
        usedQueryStringFragments = [],
        leftOverQueryStringFragments = [],
        targetContentType;

    function flushGmOperations() {
        if (gmOperations.length > 0) {
            // For some reason the gm module doesn't expose itself as a readable/writable stream,
            // so we need to wrap it into one:

            var readStream = new Stream();
            readStream.readable = true;

            var readWriteStream = new Stream();
            readWriteStream.readable = readWriteStream.writable = true;
            readWriteStream.write = function (chunk) {
                readStream.emit('data', chunk);
            };
            readWriteStream.end = function (chunk) {
                if (chunk) {
                    readWriteStream.write(chunk);
                }
                readStream.emit('end');
            };

            var gmInstance = gm(readStream);
            gmOperations.forEach(function (gmOperation) {
                gmInstance = gmInstance[gmOperation.name].apply(gmInstance, gmOperation.args);
            });
            var seenData = false,
                hasEnded = false;
            gmInstance.stream(function (err, stdout, stderr) {
                if (err) {
                    hasEnded = true;
                    return readWriteStream.emit('error', err);
                }
                stdout.on('data', function (chunk) {
                    seenData = true;
                    readWriteStream.emit('data', chunk);
                }).on('end', function () {
{'fraction_non_alphanumeric': 0.1151619129315891, 'fraction_numerical': 0.0004289084280506112, 'mean_word_length': 2.236641221374046, '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': '5039367', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1190, 'n_words': 241}