<commit_before>'use strict';

var streamFile = require('../../src/getContents/streamFile');
var fs = require('graceful-fs');

function writeStream (writePath, file, cb) {
  var opt = {
    mode: file.stat.mode
  };

  var outStream = fs.createWriteStream(writePath, opt);

  file.contents.once('error', cb);
  outStream.once('error', cb);
  outStream.once('finish', function() {
    streamFile(file, cb);
  });

  file.contents.pipe(outStream);
}

module.exports = writeStream;<commit_msg>fix dest memory leak when writing streams<commit_after>'use strict';

var streamFile = require('../../src/getContents/streamFile');
var fs = require('graceful-fs');

function writeStream (writePath, file, cb) {
  var opt = {
    mode: file.stat.mode
  };

  var outStream = fs.createWriteStream(writePath, opt);

  file.contents.once('error', complete);
  outStream.once('error', complete);
  outStream.once('finish', success);

  file.contents.pipe(outStream);

  function success() {
    streamFile(file, complete);
  }

  // cleanup
  function complete(err) {
    file.contents.removeListener('error', cb);
    outStream.removeListener('error', cb);
    outStream.removeListener('finish', success);
    cb(err);
  }
}

module.exports = writeStream;
{'fraction_non_alphanumeric': 0.16048387096774194, 'fraction_numerical': 0.0, 'mean_word_length': 4.258474576271187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6328800', 'n_tokens_mistral': 394, 'n_tokens_neox': 384, 'n_words': 81}