<commit_before>/*!
 * image-size-stream | MIT (c) <NAME>
 * https://github.com/shinnn/image-size-stream
*/
'use strict';

var through = require('through2');
var tryit = require('tryit');
var imageSize = require('image-size');

var DEFAULT_LIMIT = 128 * 1024;

module.exports = function createImageSizeStream(option) {
  option = option || {};
  if (option.limit !== undefined) {
    if (typeof option.limit !== 'number') {
      throw new TypeError(
        option.limit +
        ' is not a number. `limit` option must be a number.'
      );
    }
  } else {
    option.limit = DEFAULT_LIMIT;
  }

  var buffer = new Buffer(0);
  var dimensions;
  var detectionError;

  return through(function detectImageSize(chunk, enc, cb) {
    if (!dimensions) {
      buffer = Buffer.concat([buffer, chunk]);

      tryit(function() {
        dimensions = imageSize(buffer);
      }, function(err) {
        detectionError = err;
      });

      if (dimensions) {
        this.emit('size', dimensions);
      } else if (buffer.length > option.limit) {
        this.emit('error', new Error('Reached the limit before detecting image type.'));
      }
    }

    this.push(chunk);
    cb();
  }, function flush() {
    if (dimensions) {
      return;
    }

    if (buffer.length === 0) {
      this.emit('error', new Error('No bytes received.'));
      return;
    }

    this.emit('error', detectionError);
  });
};<commit_msg>specify buffer length explicitly for performance improvement<commit_after>/*!
 * image-size-stream | MIT (c) <NAME>
 * https://github.com/shinnn/image-size-stream
*/
'use strict';

var through = require('through2');
var tryit = require('tryit');
var imageSize = require('image-size');

var DEFAULT_LIMIT = 128 * 1024;

module.exports = function createImageSizeStream(option) {
  option = option || {};
  if (option.limit !== undefined) {
    if (typeof option.limit !== 'number') {
      throw new TypeError(
        option.limit +
        ' is not a number. `limit` option must be a number.'
      );
    }
  } else {
    option.limit = DEFAULT_LIMIT;
  }

  var buffer = new Buffer(0);
  var len = 0;
  var dimensions;
  var detectionError;

  return through(function detectImageSize(chunk, enc, cb) {
    if (!dimensions) {
      len += chunk.length;
      buffer = Buffer.concat([buffer, chunk], len);

      tryit(function() {
        dimensions = imageSize(buffer);
      }, function(err) {
        detectionError = err;
      });

      if (dimensions) {
        this.emit('size', dimensions);
      } else if (buffer.length > option.limit) {
        this.emit('error', new Error('Reached the limit before detecting image type.'));
      }
    }

    this.push(chunk);
    cb();
  }, function flush() {
    if (dimensions) {
      return;
    }

    if (buffer.length === 0) {
      this.emit('error', new Error('No bytes received.'));
      return;
    }

    this.emit('error', detectionError);
  });
};
{'fraction_non_alphanumeric': 0.15119453924914675, 'fraction_numerical': 0.007167235494880546, 'mean_word_length': 2.6546134663341645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '5666935', 'n_tokens_mistral': 912, 'n_tokens_neox': 884, 'n_words': 230}