Skip to content

Commit 61ba3d0

Browse files
authored
Merge pull request #10 from realityking/node-6
Require Node.js 6.9, update dependencies, use modern JS syntax
2 parents f3eae3c + a395c7f commit 61ba3d0

File tree

10 files changed

+147
-156
lines changed

10 files changed

+147
-156
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ node_js:
44
- 10
55
- 8
66
- 6
7-
- 4
87
matrix:
98
fast_finish: true

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ You can also use `npm install https://github.com/archiverjs/node-crc32-stream/ar
1717
Inherits [Transform Stream](http://nodejs.org/api/stream.html#stream_class_stream_transform) options and methods.
1818

1919
```js
20-
var CRC32Stream = require('crc32-stream');
20+
const {CRC32Stream} = require('crc32-stream');
2121

22-
var source = fs.createReadStream('file.txt');
23-
var checksum = new CRC32Stream();
22+
const source = fs.createReadStream('file.txt');
23+
const checksum = new CRC32Stream();
2424

2525
checksum.on('end', function(err) {
2626
// do something with checksum.digest() here
@@ -39,10 +39,10 @@ checksum.end();
3939
Inherits [zlib.DeflateRaw](http://nodejs.org/api/zlib.html#zlib_class_zlib_deflateraw) options and methods.
4040

4141
```js
42-
var DeflateCRC32Stream = require('crc32-stream').DeflateCRC32Stream;
42+
const {DeflateCRC32Stream} = require('crc32-stream');
4343

44-
var source = fs.createReadStream('file.txt');
45-
var checksum = new DeflateCRC32Stream();
44+
const source = fs.createReadStream('file.txt');
45+
const checksum = new DeflateCRC32Stream();
4646

4747
checksum.on('end', function(err) {
4848
// do something with checksum.digest() here

appveyor.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ environment:
33
- nodejs_version: '10'
44
- nodejs_version: '8'
55
- nodejs_version: '6'
6-
- nodejs_version: '4'
76
install:
87
- ps: Install-Product node $env:nodejs_version
98
- set CI=true

lib/crc32-stream.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,44 @@
55
* Licensed under the MIT license.
66
* https://github.com/archiverjs/node-crc32-stream/blob/master/LICENSE-MIT
77
*/
8-
var inherits = require('util').inherits;
9-
var Transform = require('readable-stream').Transform;
108

11-
var crc32 = require('crc').crc32;
9+
'use strict';
1210

13-
var CRC32Stream = module.exports = function CRC32Stream(options) {
14-
Transform.call(this, options);
15-
this.checksum = Buffer.allocUnsafe(4);
16-
this.checksum.writeInt32BE(0, 0);
11+
const {Transform} = require('readable-stream');
1712

18-
this.rawSize = 0;
19-
};
13+
const {crc32} = require('crc');
2014

21-
inherits(CRC32Stream, Transform);
15+
class CRC32Stream extends Transform {
16+
constructor(options) {
17+
super(options);
18+
this.checksum = Buffer.allocUnsafe(4);
19+
this.checksum.writeInt32BE(0, 0);
2220

23-
CRC32Stream.prototype._transform = function(chunk, encoding, callback) {
24-
if (chunk) {
25-
this.checksum = crc32(chunk, this.checksum);
26-
this.rawSize += chunk.length;
21+
this.rawSize = 0;
2722
}
2823

29-
callback(null, chunk);
30-
};
24+
_transform(chunk, encoding, callback) {
25+
if (chunk) {
26+
this.checksum = crc32(chunk, this.checksum);
27+
this.rawSize += chunk.length;
28+
}
3129

32-
CRC32Stream.prototype.digest = function(encoding) {
33-
var checksum = Buffer.allocUnsafe(4);
34-
checksum.writeUInt32BE(this.checksum >>> 0, 0);
35-
return encoding ? checksum.toString(encoding) : checksum;
36-
};
30+
callback(null, chunk);
31+
}
32+
33+
digest(encoding) {
34+
const checksum = Buffer.allocUnsafe(4);
35+
checksum.writeUInt32BE(this.checksum >>> 0, 0);
36+
return encoding ? checksum.toString(encoding) : checksum;
37+
}
3738

38-
CRC32Stream.prototype.hex = function() {
39-
return this.digest('hex').toUpperCase();
40-
};
39+
hex() {
40+
return this.digest('hex').toUpperCase();
41+
}
42+
43+
size() {
44+
return this.rawSize;
45+
}
46+
}
4147

42-
CRC32Stream.prototype.size = function() {
43-
return this.rawSize;
44-
};
48+
module.exports = CRC32Stream;

lib/deflate-crc32-stream.js

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,58 @@
55
* Licensed under the MIT license.
66
* https://github.com/archiverjs/node-crc32-stream/blob/master/LICENSE-MIT
77
*/
8-
var zlib = require('zlib');
9-
var inherits = require('util').inherits;
108

11-
var crc32 = require('crc').crc32;
9+
'use strict';
1210

13-
var DeflateCRC32Stream = module.exports = function (options) {
14-
zlib.DeflateRaw.call(this, options);
11+
const {DeflateRaw} = require('zlib');
1512

16-
this.checksum = Buffer.allocUnsafe(4);
17-
this.checksum.writeInt32BE(0, 0);
13+
const {crc32} = require('crc');
1814

19-
this.rawSize = 0;
20-
this.compressedSize = 0;
15+
class DeflateCRC32Stream extends DeflateRaw {
16+
constructor(options) {
17+
super(options);
2118

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);
3321

34-
DeflateCRC32Stream.prototype.push = function(chunk, encoding) {
35-
if (chunk) {
36-
this.compressedSize += chunk.length;
22+
this.rawSize = 0;
23+
this.compressedSize = 0;
3724
}
3825

39-
return zlib.DeflateRaw.prototype.push.call(this, chunk, encoding);
40-
};
26+
push(chunk, encoding) {
27+
if (chunk) {
28+
this.compressedSize += chunk.length;
29+
}
4130

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);
4632
}
4733

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+
}
5039

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+
}
5642

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+
}
6048

61-
DeflateCRC32Stream.prototype.size = function(compressed) {
62-
compressed = compressed || false;
49+
hex() {
50+
return this.digest('hex').toUpperCase();
51+
}
6352

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+
}
6859
}
69-
};
60+
}
61+
62+
module.exports = DeflateCRC32Stream;

lib/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* Licensed under the MIT license.
66
* https://github.com/archiverjs/node-crc32-stream/blob/master/LICENSE-MIT
77
*/
8-
exports = module.exports = require('./crc32-stream');
9-
exports.CRC32Stream = exports;
10-
exports.DeflateCRC32Stream = require('./deflate-crc32-stream');
8+
9+
'use strict';
10+
11+
module.exports = {
12+
CRC32Stream: require('./crc32-stream'),
13+
DeflateCRC32Stream: require('./deflate-crc32-stream')
14+
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
"lib"
2121
],
2222
"engines": {
23-
"node": ">= 4.5.0"
23+
"node": ">= 6.9.0"
2424
},
2525
"scripts": {
2626
"test": "mocha --reporter dot"
2727
},
2828
"dependencies": {
2929
"crc": "^3.4.4",
30-
"readable-stream": "^2.0.0"
30+
"readable-stream": "^3.2.0"
3131
},
3232
"devDependencies": {
3333
"chai": "^4.0.0",
34-
"mocha": "^5.0.0"
34+
"mocha": "^6.0.2"
3535
},
3636
"keywords": [
3737
"crc32-stream",

test/checksum.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/*global before,describe,it */
2-
var assert = require('chai').assert;
32

4-
var helpers = require('./helpers');
5-
var BinaryStream = helpers.BinaryStream;
6-
var DeadEndStream = helpers.DeadEndStream;
3+
'use strict';
74

8-
var CRC32Stream = require('../lib/crc32-stream.js');
5+
const {assert} = require('chai');
6+
7+
const {BinaryStream, DeadEndStream} = require('./helpers');
8+
9+
const CRC32Stream = require('../lib/crc32-stream.js');
910

1011
describe('CRC32Stream', function() {
1112
it('should checksum data while passing through data', function(done) {
12-
var binary = new BinaryStream(1024 * 16);
13-
var checksum = new CRC32Stream();
14-
var deadend = new DeadEndStream();
13+
const binary = new BinaryStream(1024 * 16);
14+
const checksum = new CRC32Stream();
15+
const deadend = new DeadEndStream();
1516

1617
checksum.on('end', function() {
1718
assert.equal(checksum.digest().readUInt32BE(0), 3893830384);
@@ -26,8 +27,8 @@ describe('CRC32Stream', function() {
2627
});
2728

2829
it('should gracefully handle having no data chunks passed to it', function(done) {
29-
var checksum = new CRC32Stream();
30-
var deadend = new DeadEndStream();
30+
const checksum = new CRC32Stream();
31+
const deadend = new DeadEndStream();
3132

3233
checksum.on('end', function() {
3334
assert.equal(checksum.digest().readUInt32BE(0), 0);

test/deflate.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/*global before,describe,it */
2-
var assert = require('chai').assert;
32

4-
var helpers = require('./helpers');
5-
var BinaryStream = helpers.BinaryStream;
6-
var DeadEndStream = helpers.DeadEndStream;
3+
'use strict';
74

8-
var DeflateCRC32Stream = require('../lib/deflate-crc32-stream.js');
5+
const {assert} = require('chai');
6+
7+
const {BinaryStream, DeadEndStream} = require('./helpers');
8+
9+
const DeflateCRC32Stream = require('../lib/deflate-crc32-stream.js');
910

1011
describe('DeflateCRC32Stream', function() {
1112
it('should checksum data while passing through data', function(done) {
12-
var binary = new BinaryStream(1024 * 16);
13-
var checksum = new DeflateCRC32Stream();
14-
var deadend = new DeadEndStream();
13+
const binary = new BinaryStream(1024 * 16);
14+
const checksum = new DeflateCRC32Stream();
15+
const deadend = new DeadEndStream();
1516

1617
checksum.on('end', function() {
1718
assert.equal(checksum.digest().readUInt32BE(0), 3893830384);
@@ -27,8 +28,8 @@ describe('DeflateCRC32Stream', function() {
2728
});
2829

2930
it('should gracefully handle having no data chunks passed to it', function(done) {
30-
var checksum = new DeflateCRC32Stream();
31-
var deadend = new DeadEndStream();
31+
const checksum = new DeflateCRC32Stream();
32+
const deadend = new DeadEndStream();
3233

3334
checksum.on('end', function() {
3435
assert.equal(checksum.digest().readUInt32BE(0), 0);

0 commit comments

Comments
 (0)