Skip to content

Commit d0d20a8

Browse files
committed
add size method to return raw size of data passed-through.
1 parent c956078 commit d0d20a8

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Returns the checksum digest in unsigned form.
4444

4545
Returns the hexadecimal representation of the checksum digest. (ie E81722F0)
4646

47+
#### size()
48+
49+
Returns the raw size/length of passed-through data.
50+
4751
### Instance Options
4852

4953
Inherits [Transform Stream](http://nodejs.org/api/stream.html#stream_class_stream_transform) options.

lib/crc32-stream.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ function CRC32Stream(options) {
1414
Transform.call(this, options);
1515
this.checksum = new Buffer(4);
1616
this.checksum.writeInt32BE(0, 0);
17+
18+
this.rawSize = 0;
1719
}
1820

1921
inherits(CRC32Stream, Transform);
2022

2123
CRC32Stream.prototype._transform = function(chunk, encoding, callback) {
2224
if (chunk) {
2325
this.checksum = crc32(chunk, this.checksum);
26+
this.rawSize += chunk.length;
2427
}
2528

2629
callback(null, chunk);
@@ -34,4 +37,8 @@ CRC32Stream.prototype.hex = function() {
3437
return this.digest().toString(16).toUpperCase();
3538
};
3639

40+
CRC32Stream.prototype.size = function() {
41+
return this.rawSize;
42+
};
43+
3744
module.exports = CRC32Stream;

test/checksum.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('CRC32Stream', function() {
1616
checksum.on('end', function() {
1717
assert.equal(checksum.digest(), 3893830384);
1818
assert.equal(checksum.hex(), 'E81722F0');
19+
assert.equal(checksum.size(), 16384);
1920
done();
2021
});
2122

0 commit comments

Comments
 (0)