'use strict';
var fs       = require('fs');
var tempfile = require('tempfile2');

module.exports = function createWriteStream(params) {
  var path = tempfile(params);
  var ws = fs.createWriteStream(path);
  var close = function() {
    ws.end();
    fs.close(ws.fd);
  };

  ws.path = path;

  ws.cleanup = function(cb) {
    close();
    fs.unlink(path, function(err) {
      if (err) ws.emit('error', err);
      if (cb) cb(err);
    });
  };

  ws.cleanupSync = function() {
    close();
    try {
      fs.unlinkSync(path);
    } catch (err) {
      ws.emit('error', err);
    }
  };

  return ws;
};

'use strict';
var fs       = require('fs');
var tempfile = require('tempfile2');

module.exports = function createWriteStream(params) {
  var path = tempfile(params);
  var writeStream = fs.createWriteStream(path);

  writeStream.path = path;

  writeStream.cleanup = function(cb) {
    writeStream.end();
    fs.unlink(path, function(err) {
      if (err) writeStream.emit('error', err);
      if (cb) cb(err);
    });
  };

  writeStream.cleanupSync = function() {
    writeStream.end();
    try {
      fs.unlinkSync(path);
    } catch (err) {
      writeStream.emit('error', err);
    }
  };

  return writeStream;
};

Revert "ensure to close the file"
Revert "ensure to close the file"

This reverts commit f1d07a7b2aab2a7436e9cc7d5d05767dd5968dab.
