Buffer-backed Streams for reading and writing.
Simple Readable and Writable Streams that use a Buffer to store received data, or for data to send out. Useful for test code, debugging, and a wide range of other utilities.
npm install stream-buffers --save
To use the stream buffers in your module, simply import it and away you go.
var streamBuffers = ;
WritableStreamBuffer implements the standard
stream.Writable interface. All writes to this stream will accumulate in an internal
Buffer. If the internal buffer overflows it will be resized automatically. The initial size of the Buffer and the amount in which it grows can be configured in the constructor.
var myWritableStreamBuffer =initialSize: 100 * 1024 // start at 100 kilobytes.incrementAmount: 10 * 1024 // grow by 10 kilobytes each time buffer overflows.;
The default initial size and increment amount are stored in the following constants:
streamBuffersDEFAULT_INITIAL_SIZE // (8 * 1024)streamBuffersDEFAULT_INCREMENT_AMOUNT // (8 * 1024)
Writing is standard Stream stuff:
myWritableStreamBuffer;// - or -myWritableStreamBuffer;
You can query the size of the data being held in the Buffer, and also how big the Buffer's max capacity currently is:
myWritableStreamBuffer;streamBufferssize; // 4.streamBuffers; // Whatever was configured as initial size. In our example: (100 * 1024).
Retrieving the contents of the Buffer is simple.
// Gets all held data as a Buffer.myWritableStreamBuffer;// Gets all held data as a utf8 string.myWritableStreamBuffer;// Gets first 5 bytes as a Buffer.myWritableStreamBuffer;// Gets first 5 bytes as a utf8 string.myWritableStreamBuffer;
Care should be taken when getting encoded strings from WritableStream, as it doesn't really care about the contents (multi-byte characters will not be respected).
Destroying or ending the WritableStream will not delete the contents of Buffer, but will disallow any further writes.
myWritableStreamBuffer;myWritableStreamBuffer;myWritableStreamBuffer; // -> 'ASDF'
ReadableStreamBuffer implements the standard
stream.Readable, but can have data inserted into it. This data will then be pumped out in chunks as readable events. The data to be sent out is held in a Buffer, which can grow in much the same way as a
WritableStreamBuffer does, if data is being put in Buffer faster than it is being pumped out.
The frequency in which chunks are pumped out, and the size of the chunks themselves can be configured in the constructor. The initial size and increment amount of internal Buffer can be configured too. In the following example 2kb chunks will be output every 10 milliseconds:
var myReadableStreamBuffer =frequency: 10 // in milliseconds.chunkSize: 2048 // in bytes.;
Default frequency and chunk size:
streamBuffersDEFAULT_CHUNK_SIZE // (1024)streamBuffersDEFAULT_FREQUENCY // (1)
Putting data in Buffer to be pumped out is easy:
Chunks are pumped out via standard
stream.Readable semantics. This means you can use the old streams1 way:
Or the streams2+ way:
ReadableStreamBuffer is simply an implementation of
stream.Readble, it implements pause / resume / setEncoding / etc.
Once you're done putting data into a
ReadableStreamBuffer, you can call
stop() on it.
ReadableStreamBuffer is done pumping out the data in its internal buffer, it will emit the usual
end event. You cannot write any more data to the stream once you've called
stop() on it.
Not supposed to be a speed demon, it's more for tests/debugging or weird edge cases. It works with an internal buffer that it copies contents to/from/around.
Thanks to the following people for taking some time to contribute to this project.
node-stream-buffer is free and unencumbered public domain software. For more information, see the accompanying UNLICENSE file.