🚀 Socket Launch Week 🚀 Day 1: Introducing .NET Support in Socket.Learn More

bufferedstream

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bufferedstream - npm Package Compare versions

Comparing version

to
2.2.0

@@ -11,2 +11,16 @@ var d = require('d');

/**
* The default maximum buffer size.
*/
var DEFAULT_MAX_SIZE = Math.pow(2, 16); // 64k
/**
* The set of valid encodings.
*/
var VALID_ENCODINGS = {
utf8: true,
hex: true,
base64: true
};
/**
* A flexible event emitter for binary data that reliably emits data on the

@@ -16,9 +30,8 @@ * next turn of the event loop.

* The maxSize determines the number of bytes the buffer can hold before it is
* considered "full". This argument may be omitted to indicate this stream has
* no maximum size.
* considered "full". Defaults to 64k.
*
* The source and sourceEncoding arguments may be used to easily wrap this
* stream around another, or a simple string. If the source is another stream,
* it is piped to this stream. If it's a string, it is used as the entire
* contents of this stream and passed to end.
* it is piped to this stream. If it's a string or binary data, it is used as
* the entire contents of the stream.
*

@@ -37,3 +50,3 @@ * NOTE: The maxSize is a soft limit that is only used to determine when calls

source = maxSize;
maxSize = Infinity;
maxSize = DEFAULT_MAX_SIZE;
}

@@ -68,20 +81,2 @@

/**
* For compat with node streams && pipe.
*/
addListener: d(ee.methods.on),
removeListener: d(ee.methods.off),
removeAllListeners: d(function () {
allOff(this);
return this;
}),
/**
* Sets this stream's encoding. If an encoding is set, this stream will emit
* strings using that encoding. Otherwise, it emits binary objects.
*/
setEncoding: d(function (encoding) {
this.encoding = encoding;
}),
/**
* A read-only property that returns true if this stream has no data to emit.

@@ -101,2 +96,15 @@ */

/**
* Sets this stream's encoding. If an encoding is set, this stream will emit
* strings using that encoding. Otherwise, it emits binary objects.
*
* Supported encodings are: "utf8", "hex", and "base64".
*/
setEncoding: d(function (encoding) {
if (VALID_ENCODINGS[encoding] !== true)
throw new Error('Invalid encoding: ' + encoding);
this.encoding = encoding;
}),
/**
* Prevents this stream from emitting data events until resume is called.

@@ -195,2 +203,10 @@ * Note: This does not prevent writes to this stream.

// For compat with node streams && pipe.
addListener: d(ee.methods.on),
removeListener: d(ee.methods.off),
removeAllListeners: d(function () {
allOff(this);
return this;
}),
/**

@@ -287,5 +303,7 @@ * Writes the given chunk of data to this stream. Returns false if this

if (stream.ended && !stream.paused) {
stream._chunks = null;
stream.emit('end');
if (stream.ended) {
if (!stream.paused) {
stream._chunks = null;
stream.emit('end');
}
} else if (stream._wasFull && !stream.full) {

@@ -292,0 +310,0 @@ stream._wasFull = false;

@@ -1,3 +0,7 @@

= 2.0.0 / 2014-07-10
= HEAD
* Maximum stream length defaults to 64k
= 2.1.0 / 2014-07-10
* Introduced browser compatibility

@@ -4,0 +8,0 @@ * Removed dependency on node

{
"name": "bufferedstream",
"description": "A solid, reliable stream class for JavaScript",
"description": "A reliable read/write stream class for node.js and browsers",
"author": "Michael Jackson <mjijackson@gmail.com>",
"version": "2.1.0",
"version": "2.2.0",
"repository": {

@@ -7,0 +7,0 @@ "type": "git",

[![build status](https://secure.travis-ci.org/mjackson/bufferedstream.png)](http://travis-ci.org/mjackson/bufferedstream)
BufferedStream is a flexible event emitter for binary data that reliably emits data on the next turn of the event loop. This greatly enhances the usability of streams by making it easy to setup listeners in the same turn of the event loop before data is emitted.
BufferedStream is a reliable read/write stream class for node.js and browsers. All data that is written to a BufferedStream is buffered until the next turn of the event loop. This greatly enhances the usability of streams by making it easy to setup listeners in the same turn of the event loop before data is emitted.
BufferedStream is designed to operate efficiently in both node.js and browsers.
This class follows the first version of the node streams API, which is powerful because of its simplicity. Node has since moved on to other, much more complex streams implementations, but there never was a problem with the initial API. The only problems were with node's implementation. For example, streams did not always wait until the next tick to emit data. Also, some streams did not respect `pause`/`resume` semantics.
BufferedStream addresses these problems by providing a well-tested, performant implementation that preserves the original streams API and works in both node.js and browsers.
## Installation

@@ -8,0 +10,0 @@

@@ -80,2 +80,36 @@ var assert = require('assert');

describe('that is ended but paused in a "data" event handler', function () {
var stream;
beforeEach(function () {
stream = new BufferedStream(3);
});
it('does not emit "drain" events', function (done) {
var endWasCalled = false;
stream.on('end', function () {
endWasCalled = true;
});
var drainWasCalled = false;
stream.on('drain', function () {
drainWasCalled = true;
});
stream.end('hello');
assert(stream.full);
stream.on('data', function () {
stream.pause();
setTimeout(function () {
stream.resume();
setTimeout(function () {
expect(endWasCalled).toEqual(true);
expect(drainWasCalled).toEqual(false);
done();
});
});
});
});
});
describe('when paused and resumed multiple times', function () {

@@ -82,0 +116,0 @@ var count;