bufferedstream
Advanced tools
Comparing version
@@ -116,2 +116,6 @@ var util = require('util'); | ||
if (this.ended) { | ||
throw new Error('Stream is already ended'); | ||
} | ||
if (typeof chunk === 'string') { | ||
@@ -123,2 +127,3 @@ chunk = new Buffer(chunk, encoding); | ||
this.size += chunk.length; | ||
flushOnNextTick(this); | ||
@@ -145,13 +150,8 @@ | ||
if (arguments.length > 0) { | ||
this.write(chunk, encoding); | ||
} | ||
if (chunk != null) this.write(chunk, encoding); | ||
this.ended = true; | ||
// If the stream isn't already scheduled to flush on the next tick we can | ||
// safely end it now. Otherwise it will end after the next flush. | ||
if (!this._flushing) { | ||
end(this); | ||
} | ||
// Trigger the flush cycle one last time to emit any data that was written | ||
// before we called end. | ||
flushOnNextTick(this); | ||
}; | ||
@@ -192,8 +192,2 @@ | ||
// If the stream was full at one point but isn't now, emit "drain". | ||
if (stream._wasFull && !stream.full) { | ||
stream._wasFull = false; | ||
stream.emit('drain'); | ||
} | ||
// If the stream was paused in some data event handler, break. | ||
@@ -207,3 +201,10 @@ if (stream.paused) { | ||
end(stream); | ||
return; | ||
} | ||
// If the stream was full at one point but isn't now, emit "drain". | ||
if (stream._wasFull && !stream.full) { | ||
stream._wasFull = false; | ||
stream.emit('drain'); | ||
} | ||
} | ||
@@ -210,0 +211,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "A base stream class for node that reliably buffers until next tick", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -103,2 +103,18 @@ var assert = require('assert'); | ||
}); | ||
it('only emits "end" after it is resumed', function (done) { | ||
var endWasCalled = false; | ||
stream.on('end', function () { | ||
endWasCalled = true; | ||
}); | ||
stream.end(); | ||
assert.equal(endWasCalled, false); | ||
stream.resume(); | ||
setTimeout(function () { | ||
assert.equal(endWasCalled, true); | ||
done(); | ||
}, 1); | ||
}); | ||
}); | ||
@@ -138,2 +154,10 @@ }); | ||
it('throws when a stream is already ended', function () { | ||
var stream = new BufferedStream; | ||
stream.end(); | ||
assert.throws(function () { | ||
stream.write('test'); | ||
}, /already ended/); | ||
}); | ||
describe('when called with a string in base64 encoding', function () { | ||
@@ -140,0 +164,0 @@ it('uses the proper encoding', function (callback) { |
Sorry, the diff of this file is not supported yet
17909
4.17%452
4.63%