All files / Nodejs/lib flushwrite.js

78.72% Statements 37/47
64.28% Branches 18/28
100% Functions 6/6
89.18% Lines 33/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x 1x   1x   1x     37x   37x           37x   37x 37x 37x     1x   1x 37x 37x 37x 37x     1x 97x 84x     1x 36x 13x 13x 13x 13x 13x     1x 1x 1x 1x 1x 1x   1x    
var stream = require('readable-stream')
var inherits = require('inherits')
 
var SIGNAL_FLUSH = Buffer.from([0])
 
module.exports = WriteStream
 
function WriteStream(opts, write, flush) {
	Iif (!(this instanceof WriteStream)) return new WriteStream(opts, write, flush)
 
	Iif (typeof opts === 'function') {
		flush = write
		write = opts
		opts = {}
	}
 
	stream.Writable.call(this, opts)
 
	this.destroyed = false
	this._worker = write || null
	this._flush = flush || null
}
 
inherits(WriteStream, stream.Writable)
 
WriteStream.obj = function(opts, worker, flush) {
	Iif (typeof opts === 'function') return WriteStream.obj(null, opts, worker)
	if (!opts) opts = {}
	opts.objectMode = true
	return new WriteStream(opts, worker, flush)
}
 
WriteStream.prototype._write = function(data, enc, cb) {
	if (SIGNAL_FLUSH === data) this._flush(cb)
	else this._worker(data, enc, cb)
}
 
WriteStream.prototype.end = function(data, enc, cb) {
	if (!this._flush) return stream.Writable.prototype.end.apply(this, arguments)
	Iif (typeof data === 'function') return this.end(null, null, data)
	Iif (typeof enc === 'function') return this.end(data, null, enc)
	Iif (data) this.write(data)
	Eif (!this._writableState.ending) this.write(SIGNAL_FLUSH)
	return stream.Writable.prototype.end.call(this, cb)
}
 
WriteStream.prototype.destroy = function(err) {
	Iif (this.destroyed) return
	this.destroyed = true
	var self = this
	process.nextTick(function() {
		Iif (err)
			self.emit('error', err);
		self.emit('close');
	});
}