Skip to content
Closed
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
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
sudo: false
language: node_js
node_js:
- 8
- 7
- 6
- 4
- '0.12'
- '0.10'
matrix:
fast_finish: true
fast_finish: true
7 changes: 3 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '7'
- nodejs_version: '6'
- nodejs_version: '4'
- nodejs_version: '0.12'
- nodejs_version: '0.10'
install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
Expand All @@ -19,4 +18,4 @@ clone_depth: 1
test_script:
- node --version
- npm --version
- npm test
- npm test
81 changes: 36 additions & 45 deletions lib/deflate-crc32-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,54 @@
*/
var zlib = require('zlib');
var inherits = require('util').inherits;

var crc32 = require('crc').crc32;

var DeflateCRC32Stream = module.exports = function (options) {
zlib.DeflateRaw.call(this, options);

this.checksum = new Buffer(4);
this.checksum.writeInt32BE(0, 0);
class DeflateCRC32Stream extends zlib.DeflateRaw {
constructor(options) {
super(options);
this.checksum = new Buffer(4);
this.checksum.writeInt32BE(0, 0);

this.rawSize = 0;
this.compressedSize = 0;

// BC v0.8
if (typeof zlib.DeflateRaw.prototype.push !== 'function') {
this.on('data', function(chunk) {
if (chunk) {
this.compressedSize += chunk.length;
}
});
this.rawSize = 0;
this.compressedSize = 0;
}
};

inherits(DeflateCRC32Stream, zlib.DeflateRaw);
push(chunk, encoding) {
if (chunk) {
this.compressedSize += chunk.length;
}

DeflateCRC32Stream.prototype.push = function(chunk, encoding) {
if (chunk) {
this.compressedSize += chunk.length;
return zlib.DeflateRaw.prototype.push.call(this, chunk, encoding);
}

return zlib.DeflateRaw.prototype.push.call(this, chunk, encoding);
};
write(chunk, enc, cb) {
if (chunk) {
this.checksum = crc32(chunk, this.checksum);
this.rawSize += chunk.length;
}

DeflateCRC32Stream.prototype.write = function(chunk, enc, cb) {
if (chunk) {
this.checksum = crc32(chunk, this.checksum);
this.rawSize += chunk.length;
return zlib.DeflateRaw.prototype.write.call(this, chunk, enc, cb);
}

return zlib.DeflateRaw.prototype.write.call(this, chunk, enc, cb);
};

DeflateCRC32Stream.prototype.digest = function(encoding) {
var checksum = new Buffer(4);
checksum.writeUInt32BE(this.checksum >>> 0, 0);
return encoding ? checksum.toString(encoding) : checksum;
};
digest(encoding) {
var checksum = new Buffer(4);
checksum.writeUInt32BE(this.checksum >>> 0, 0);
return encoding ? checksum.toString(encoding) : checksum;
}

DeflateCRC32Stream.prototype.hex = function() {
return this.digest('hex').toUpperCase();
};
hex() {
return this.digest('hex').toUpperCase();
}

DeflateCRC32Stream.prototype.size = function(compressed) {
compressed = compressed || false;
size(compressed) {
compressed = compressed || false;

if (compressed) {
return this.compressedSize;
} else {
return this.rawSize;
if (compressed) {
return this.compressedSize;
} else {
return this.rawSize;
}
}
};
}

module.exports = DeflateCRC32Stream