bufferedstream
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -88,2 +88,10 @@ var util = require('util'); | ||
this.emit('resume'); | ||
if (!this.empty) { | ||
flushOnNextTick(this); | ||
} | ||
if (this.ended) { | ||
endOnNextTick(this); | ||
} | ||
}; | ||
@@ -109,12 +117,3 @@ | ||
if (!this._flushing) { | ||
var self = this; | ||
var interval = setInterval(function () { | ||
self.flush(); | ||
if (self.empty) { | ||
self._flushing = false; | ||
clearInterval(interval); | ||
} | ||
}, 0); | ||
flushOnNextTick(this); | ||
this._flushing = true; | ||
@@ -174,10 +173,3 @@ } | ||
var self = this; | ||
var interval = setInterval(function () { | ||
if (self.empty) { | ||
self.destroy(); | ||
self.emit('end'); | ||
clearInterval(interval); | ||
} | ||
}, 0); | ||
endOnNextTick(this); | ||
}; | ||
@@ -195,1 +187,32 @@ | ||
}; | ||
function flushOnNextTick(stream) { | ||
var flush = function () { | ||
stream.flush(); | ||
if (stream.empty) { | ||
stream._flushing = false; | ||
} else if (stream._wait) { | ||
return; | ||
} else { | ||
process.nextTick(flush); | ||
} | ||
}; | ||
process.nextTick(flush); | ||
} | ||
function endOnNextTick(stream) { | ||
var end = function () { | ||
if (stream.empty) { | ||
stream.destroy(); | ||
stream.emit('end'); | ||
} else if (stream._wait) { | ||
return; | ||
} else { | ||
process.nextTick(end); | ||
} | ||
}; | ||
process.nextTick(end); | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "A base stream class for node that reliably buffers until next tick", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"repository": { | ||
@@ -13,3 +13,3 @@ "type": "git", | ||
"scripts": { | ||
"test": "mocha test/buffered-stream-test.js" | ||
"test": "mocha test" | ||
}, | ||
@@ -16,0 +16,0 @@ "engines": { |
[![build status](https://secure.travis-ci.org/mjijackson/bufferedstream.png)](http://travis-ci.org/mjijackson/bufferedstream) | ||
BufferedStream is a reliable base class for streams in node programs that | ||
buffers data until the next tick of the event loop. | ||
BufferedStream is a reliable base class for streams in node programs that buffers data until the next tick of the event loop. | ||
## Rationale | ||
The details of streams are still being worked out in node core. As of this | ||
writing there are several different types of streams in node, including some | ||
objects that do not actually inherit from Stream. The goal of this library is | ||
to iron out the differences between the various stream-like objects and give | ||
user code a reliable, documented API they can use now. If the situation ever | ||
improves in node core (which we all hope it will) this code may become obsolete. | ||
The details of streams are still being worked out in node core. As of this writing there are several different types of streams in node, including some objects that do not actually inherit from Stream. The goal of this library is to iron out the differences between the various stream-like objects and give user code a reliable, documented API they can use now. If the situation ever improves in node core (which we all hope it will) this code may become obsolete. | ||
@@ -23,28 +17,23 @@ ## Installation | ||
The key feature of this class is that anything you write to the stream in the | ||
current tick of the event loop is buffered until the next tick. This allows you | ||
to register event handlers, pause the stream, etc. reliably without losing any | ||
data. | ||
The key feature of this class is that anything you write to the stream in the current tick of the event loop is buffered until the next tick. This allows you to register event handlers, pause the stream, etc. reliably without losing any data. | ||
var BufferedStream = require('bufferedstream'); | ||
```javascript | ||
var BufferedStream = require('bufferedstream'); | ||
var stream = new BufferedStream; | ||
stream.write('Hello '); | ||
stream.pause(); | ||
var stream = new BufferedStream; | ||
stream.write('Hello '); | ||
stream.pause(); | ||
setTimeout(function () { | ||
stream.write('IHdvcmxkLg==', 'base64'); | ||
stream.resume(); | ||
stream.on('data', function (chunk) { | ||
console.log(chunk.toString()); // Hello world. | ||
}); | ||
}, 10); | ||
setTimeout(function () { | ||
stream.write('IHdvcmxkLg==', 'base64'); | ||
stream.resume(); | ||
stream.on('data', function (chunk) { | ||
console.log(chunk.toString()); // Hello world. | ||
}); | ||
}, 10); | ||
``` | ||
The `BufferedStream` constructor may also accept a "source" which may be another | ||
stream that will be piped directly through to this stream or a string. This is | ||
useful for wrapping various stream-like objects and normalizing their behavior | ||
across implementations. | ||
The `BufferedStream` constructor may also accept a "source" which may be another stream that will be piped directly through to this stream or a string. This is useful for wrapping various stream-like objects and normalizing their behavior across implementations. | ||
Please see the source code for more information. The module is small enough (and | ||
well-documented) that it should be easy to digest in a quick skim. | ||
Please see the source code for more information. The module is small enough (and well-documented) that it should be easy to digest in a quick skim. | ||
@@ -59,20 +48,8 @@ ## Tests | ||
Copyright 2011 Michael Jackson | ||
Copyright 2012 Michael Jackson | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
The software is provided "as is", without warranty of any kind, express or | ||
implied, including but not limited to the warranties of merchantability, | ||
fitness for a particular purpose and non-infringement. In no event shall the | ||
authors or copyright holders be liable for any claim, damages or other | ||
liability, whether in an action of contract, tort or otherwise, arising from, | ||
out of or in connection with the software or the use or other dealings in | ||
the software. | ||
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14109
8
354
0
54