You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.2 KiB
62 lines
1.2 KiB
/**
|
|
* node-crc32-stream
|
|
*
|
|
* Copyright (c) 2014 Chris Talkington, contributors.
|
|
* Licensed under the MIT license.
|
|
* https://github.com/archiverjs/node-crc32-stream/blob/master/LICENSE-MIT
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const {DeflateRaw} = require('zlib');
|
|
|
|
const crc32 = require('crc-32');
|
|
|
|
class DeflateCRC32Stream extends DeflateRaw {
|
|
constructor(options) {
|
|
super(options);
|
|
|
|
this.checksum = Buffer.allocUnsafe(4);
|
|
this.checksum.writeInt32BE(0, 0);
|
|
|
|
this.rawSize = 0;
|
|
this.compressedSize = 0;
|
|
}
|
|
|
|
push(chunk, encoding) {
|
|
if (chunk) {
|
|
this.compressedSize += chunk.length;
|
|
}
|
|
|
|
return super.push(chunk, encoding);
|
|
}
|
|
|
|
write(chunk, enc, cb) {
|
|
if (chunk) {
|
|
this.checksum = crc32.buf(chunk, this.checksum) >>> 0;
|
|
this.rawSize += chunk.length;
|
|
}
|
|
|
|
return super.write(chunk, enc, cb);
|
|
}
|
|
|
|
digest(encoding) {
|
|
const checksum = Buffer.allocUnsafe(4);
|
|
checksum.writeUInt32BE(this.checksum >>> 0, 0);
|
|
return encoding ? checksum.toString(encoding) : checksum;
|
|
}
|
|
|
|
hex() {
|
|
return this.digest('hex').toUpperCase();
|
|
}
|
|
|
|
size(compressed = false) {
|
|
if (compressed) {
|
|
return this.compressedSize;
|
|
} else {
|
|
return this.rawSize;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = DeflateCRC32Stream;
|
|
|