Permalink
Browse files

add stream-like interface to allow piping into a streamingbuffer

  • Loading branch information...
1 parent dd7b4b6 commit 728533f2558bd9bfe0e20b0be3b3fded33a8545c @kkaefer committed Jun 19, 2011
Showing with 245 additions and 189 deletions.
  1. +25 −3 streamingbuffer.js
  2. +220 −186 test/streamingbuffer.js
View
@@ -1,21 +1,44 @@
+var EventEmitter = require('events').EventEmitter;
exports = module.exports = StreamingBuffer;
function noop() {}
-require('util').inherits(StreamingBuffer, require('events').EventEmitter);
+require('util').inherits(StreamingBuffer, EventEmitter);
function StreamingBuffer() {
if (!(this instanceof StreamingBuffer)) return new StreamingBuffer();
+ EventEmitter.call(this);
this.buffers = [];
this.queue = [];
this.process = this.process.bind(this);
}
-StreamingBuffer.prototype.push = function(buffer) {
+// Emulate the buffer interface so that we can pipe into a StreamingBuffer.
+StreamingBuffer.prototype.readable = true;
+StreamingBuffer.prototype.writable = true;
+
+StreamingBuffer.prototype.write = function(buffer) {
this.buffers.push(buffer);
process.nextTick(this.process);
};
+StreamingBuffer.prototype.end = function() {
+ this.readable = false;
+ this.writable = false;
+};
+
+StreamingBuffer.prototype.destroy = function() {
+ this.buffers = [];
+ this.queue = [];
+ this.end();
+};
+
+StreamingBuffer.prototype.destroySoon = StreamingBuffer.prototype.destroy;
+
+StreamingBuffer.prototype.pause = noop;
+StreamingBuffer.prototype.resume = noop;
+StreamingBuffer.prototype.pipe = noop;
+
StreamingBuffer.prototype.process = function() {
while (this.queue.length) {
var task = this.queue[0];
@@ -47,7 +70,6 @@ StreamingBuffer.prototype.process = function() {
}
};
-
/**
* Gets the next line up to CRLF.
*
Oops, something went wrong.

0 comments on commit 728533f

Please sign in to comment.