Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ var Counter = require('passthrough-counter')

var stream = fs.createReadStream('package.json')
var output = fs.createWriteStream('package.json.gz')
var counter = new Counter();

counter.on('progress', function (length) {
// current chunk length
});

stream
.pipe(zlib.createGzip())
.pipe(Counter)
.pipe(counter)
.once('finish', function () {
console.log('final gzipped length is ' + this.length)
})
Expand All @@ -28,6 +33,10 @@ stream
The total number of bytes pass through the stream.
You can check this once `finish` is emitted.

### {Event} progress(length)

Event with chunk length emitted each time chunk is received

## License

The MIT License (MIT)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ function Counter(options) {
Counter.prototype._transform = function (chunk, encoding, callback) {
this.length += chunk.length
this.push(chunk)
this.emit('progress', chunk.length);
callback()
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "passthrough-counter",
"description": "Get the total buffer length of a stream.",
"version": "1.0.0",
"version": "1.0.1",
"author": "Jonathan Ong <[email protected]> (http://jongleberry.com)",
"license": "MIT",
"repository": "stream-utils/passthrough-counter",
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ describe('Passthrough Counter', function () {
})
.resume()
})

it('should have progress', function(done) {
var stream = fs.createReadStream(pack)
var counter = Counter()
var currentLength = 0

counter.on('progress', function (length) {
currentLength += length;
})

stream.pipe(counter)
.once('finish', function() {
assert.equal(currentLength, length);
done();
});
})
})