|
5 | 5 | * Licensed under the MIT license.
|
6 | 6 | * https://github.com/archiverjs/node-crc32-stream/blob/master/LICENSE-MIT
|
7 | 7 | */
|
8 |
| -var zlib = require('zlib'); |
9 |
| -var inherits = require('util').inherits; |
10 | 8 |
|
11 |
| -var crc32 = require('crc').crc32; |
| 9 | +'use strict'; |
12 | 10 |
|
13 |
| -var DeflateCRC32Stream = module.exports = function (options) { |
14 |
| - zlib.DeflateRaw.call(this, options); |
| 11 | +const {DeflateRaw} = require('zlib'); |
15 | 12 |
|
16 |
| - this.checksum = Buffer.allocUnsafe(4); |
17 |
| - this.checksum.writeInt32BE(0, 0); |
| 13 | +const {crc32} = require('crc'); |
18 | 14 |
|
19 |
| - this.rawSize = 0; |
20 |
| - this.compressedSize = 0; |
| 15 | +class DeflateCRC32Stream extends DeflateRaw { |
| 16 | + constructor(options) { |
| 17 | + super(options); |
21 | 18 |
|
22 |
| - // BC v0.8 |
23 |
| - if (typeof zlib.DeflateRaw.prototype.push !== 'function') { |
24 |
| - this.on('data', function(chunk) { |
25 |
| - if (chunk) { |
26 |
| - this.compressedSize += chunk.length; |
27 |
| - } |
28 |
| - }); |
29 |
| - } |
30 |
| -}; |
31 |
| - |
32 |
| -inherits(DeflateCRC32Stream, zlib.DeflateRaw); |
| 19 | + this.checksum = Buffer.allocUnsafe(4); |
| 20 | + this.checksum.writeInt32BE(0, 0); |
33 | 21 |
|
34 |
| -DeflateCRC32Stream.prototype.push = function(chunk, encoding) { |
35 |
| - if (chunk) { |
36 |
| - this.compressedSize += chunk.length; |
| 22 | + this.rawSize = 0; |
| 23 | + this.compressedSize = 0; |
37 | 24 | }
|
38 | 25 |
|
39 |
| - return zlib.DeflateRaw.prototype.push.call(this, chunk, encoding); |
40 |
| -}; |
| 26 | + push(chunk, encoding) { |
| 27 | + if (chunk) { |
| 28 | + this.compressedSize += chunk.length; |
| 29 | + } |
41 | 30 |
|
42 |
| -DeflateCRC32Stream.prototype.write = function(chunk, enc, cb) { |
43 |
| - if (chunk) { |
44 |
| - this.checksum = crc32(chunk, this.checksum); |
45 |
| - this.rawSize += chunk.length; |
| 31 | + return super.push(chunk, encoding); |
46 | 32 | }
|
47 | 33 |
|
48 |
| - return zlib.DeflateRaw.prototype.write.call(this, chunk, enc, cb); |
49 |
| -}; |
| 34 | + write(chunk, enc, cb) { |
| 35 | + if (chunk) { |
| 36 | + this.checksum = crc32(chunk, this.checksum); |
| 37 | + this.rawSize += chunk.length; |
| 38 | + } |
50 | 39 |
|
51 |
| -DeflateCRC32Stream.prototype.digest = function(encoding) { |
52 |
| - var checksum = Buffer.allocUnsafe(4); |
53 |
| - checksum.writeUInt32BE(this.checksum >>> 0, 0); |
54 |
| - return encoding ? checksum.toString(encoding) : checksum; |
55 |
| -}; |
| 40 | + return super.write(chunk, enc, cb); |
| 41 | + } |
56 | 42 |
|
57 |
| -DeflateCRC32Stream.prototype.hex = function() { |
58 |
| - return this.digest('hex').toUpperCase(); |
59 |
| -}; |
| 43 | + digest(encoding) { |
| 44 | + const checksum = Buffer.allocUnsafe(4); |
| 45 | + checksum.writeUInt32BE(this.checksum >>> 0, 0); |
| 46 | + return encoding ? checksum.toString(encoding) : checksum; |
| 47 | + } |
60 | 48 |
|
61 |
| -DeflateCRC32Stream.prototype.size = function(compressed) { |
62 |
| - compressed = compressed || false; |
| 49 | + hex() { |
| 50 | + return this.digest('hex').toUpperCase(); |
| 51 | + } |
63 | 52 |
|
64 |
| - if (compressed) { |
65 |
| - return this.compressedSize; |
66 |
| - } else { |
67 |
| - return this.rawSize; |
| 53 | + size(compressed = false) { |
| 54 | + if (compressed) { |
| 55 | + return this.compressedSize; |
| 56 | + } else { |
| 57 | + return this.rawSize; |
| 58 | + } |
68 | 59 | }
|
69 |
| -}; |
| 60 | +} |
| 61 | + |
| 62 | +module.exports = DeflateCRC32Stream; |
0 commit comments