<commit_before>"use strict";

var util = require('util');
var stream = require('stream');
var _ = require('lodash');

var RiakFsWriteStream = function(riakfs, _path, options) {

    if (!(this instanceof RiakFsWriteStream)) {
        return new RiakFsWriteStream(riakfs, _path, options);
    }

    options = _.partialRight(_.merge, _.defaults)(options || {}, {
        flags: 'w',
        encoding: null,
        highWaterMark: 256 * 1024,
        decodeStrings: true
    });

    stream.Writable.call(this, options);

    this.flags = options.flags;
    this.riakfs = riakfs;
    this.path = _path;
    this.bytesWritten = 0;

    this.open();

    this.once('finish', this.close);
};

util.inherits(RiakFsWriteStream, stream.Writable);

module.exports = RiakFsWriteStream;

RiakFsWriteStream.prototype.destroySoon = RiakFsWriteStream.prototype.end;

RiakFsWriteStream.prototype.destroy = function() {
    if (this.destroyed) {
        return;
    }

    this.destroyed = true;

    if (this.fd) {
        this.close();
    }
};

RiakFsWriteStream.prototype.open = function() {
    var self = this;

    if (self._opening || self.fd) {
        return;
    }
    self._opening = true;

    self.riakfs.open(self.path, self.flags)<commit_msg>Catch finish event and close all resources before re-emitting it<commit_after>"use strict";

var util = require('util');
var stream = require('stream');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;

var RiakFsWriteStream = function(riakfs, _path, options) {

    if (!(this instanceof RiakFsWriteStream)) {
        return new RiakFsWriteStream(riakfs, _path, options);
    }

    options = _.partialRight(_.merge, _.defaults)(options || {}, {
        flags: 'w',
        encoding: null,
        highWaterMark: 256 * 1024,
        decodeStrings: true
    });

    stream.Writable.call(this, options);

    this.flags = options.flags;
    this.riakfs = riakfs;
    this.path = _path;
    this.bytesWritten = 0;

    this.open();
};

util.inherits(RiakFsWriteStream, stream.Writable);

module.exports = RiakFsWriteStream;

RiakFsWriteStream.prototype.destroySoon = RiakFsWriteStream.prototype.end;


// catch Writable `finish` event and close everything first
// this is a temporary fix for
// https://github.com/nodejs/node/issues/2994
// https://github.com/nodejs/node/issues/4672
// https://github.com/nodejs/node/pull/2314
RiakFsWriteStream.prototype.emit = function () {
    var self = this, args = arguments, event = args[0];
    if(event !== 'finish'){
        EventEmitter.prototype.emit.apply(self, args);
    } else {
        self.once('close', function () {
            EventEmitter.prototype.emit.apply(self, args);
        });
        self.close();
    }
};

RiakFsWriteStream.prototype.destroy = function() {
    if (this.destroyed) {
        return;
    }

    this.destroyed = true;

    if (this.fd) {
        this.close();
    }
};

RiakFsWriteStream.prototype.open = function() {
    var self = this;

    if (self._opening || self.fd) {
        return;
    }
    self._opening = true;

    self.riakfs.open(self.path, self.flags)
{'fraction_non_alphanumeric': 0.13841717398269784, 'fraction_numerical': 0.009291893623838513, 'mean_word_length': 2.9469026548672566, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, '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': '2836439', 'n_tokens_mistral': 1074, 'n_tokens_neox': 994, 'n_words': 201}