{"size": 1267, "ext": "js", "max_stars_repo_path": "lib/stream.js", "max_stars_repo_name": "GeKorm/gulp-zopfli-green", "max_stars_repo_stars_event_min_datetime": "2018-04-10T07:12:49.000Z", "max_stars_repo_stars_event_max_datetime": "2019-11-13T10:25:10.000Z", "max_issues_repo_path": "lib/stream.js", "max_issues_repo_name": "GeKorm/gulp-zopfli-green", "max_issues_repo_issues_event_min_datetime": "2018-03-01T16:09:59.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-29T18:54:01.000Z", "max_forks_repo_path": "lib/stream.js", "max_forks_repo_name": "GeKorm/gulp-zopfli-green", "max_forks_repo_forks_event_min_datetime": "2018-07-22T13:49:40.000Z", "max_forks_repo_forks_event_max_datetime": "2018-07-22T13:49:40.000Z"}
{"max_stars_count": 4, "max_issues_count": 16, "max_forks_count": 1, "avg_line_length": 30.9024390244, "max_line_length": 74, "alphanum_fraction": 0.6614048934}
'use strict';

const through2 = require('through2');
const toArray = require('stream-to-array');
const compress = require('./compress');

module.exports = function(contents, options, callback) {
  const compressStream = compress(options, callback);
  if (!compressStream) return;

  // Check if the threshold option is set
  if (options.threshold) {
    // Check if the stream contents is less than the threshold
    toArray(contents, function(err, chunks) {
      if (err) {
        callback(err, null, false);
        return;
      }

      // Join chunks array into a single buffer
      const buffer = Buffer.concat(chunks);

      // Create a stream to return to the callback
      const contentStream = through2();
      contentStream.end(buffer);

      // Check if the stream content length is less than the threshold
      if (buffer.length < options.threshold) {
        // File does not meet the minimum size requirement for compression
        callback(null, contentStream, false);
      } else {
        // File meets the minimum size requirement for compression
        callback(null, contentStream.pipe(compressStream), true);
      }
    });
  } else {
    // Compress the file contents
    callback(null, contents.pipe(compressStream), true);
  }
};
