diff --git a/README.md b/README.md index 61fdd6e..d0c404c 100644 --- a/README.md +++ b/README.md @@ -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) }) @@ -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) diff --git a/index.js b/index.js index 0e631ea..1d8146b 100644 --- a/index.js +++ b/index.js @@ -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() } \ No newline at end of file diff --git a/package.json b/package.json index 64968ea..4f02d7d 100644 --- a/package.json +++ b/package.json @@ -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 (http://jongleberry.com)", "license": "MIT", "repository": "stream-utils/passthrough-counter", diff --git a/test/test.js b/test/test.js index cbffa41..5d67ade 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); + }); + }) })