Skip to content

Commit 53efca3

Browse files
authored
fix esm export (#131)
1 parent d797b1d commit 53efca3

File tree

12 files changed

+224
-200
lines changed

12 files changed

+224
-200
lines changed

.github/workflows/nodejs.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ jobs:
1717
node-version: [18.x, 20.x]
1818

1919
steps:
20-
- uses: actions/checkout@v4.2.1
21-
- name: Use Node.js ${{ matrix.node-version }}
22-
uses: actions/setup-node@v4.0.4
23-
with:
24-
node-version: ${{ matrix.node-version }}
25-
- name: npm install and test
26-
run: |
27-
npm ci
28-
npm test
29-
env:
30-
CI: true
20+
- uses: actions/checkout@v4.2.1
21+
- name: Use Node.js ${{ matrix.node-version }}
22+
uses: actions/setup-node@v4.0.4
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
- name: npm install and test
26+
run: |
27+
npm ci
28+
npm test
29+
env:
30+
CI: true

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore artifacts:
2+
build
3+
coverage

CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
#### Code Style Guide
44

5-
* code should be indented with 2 spaces
6-
* single quotes should be used where feasible
7-
* commas should be followed by a single space (function params, etc)
8-
* variable declaration should include `var`, [no multiple declarations](http://benalman.com/news/2012/05/multiple-var-statements-javascript/)
5+
- code should be indented with 2 spaces
6+
- single quotes should be used where feasible
7+
- commas should be followed by a single space (function params, etc)
8+
- variable declaration should include `var`, [no multiple declarations](http://benalman.com/news/2012/05/multiple-var-statements-javascript/)
99

1010
#### Tests
1111

12-
* tests should be added to the nodeunit configs in `test/`
13-
* tests can be run with `npm test`
14-
* see existing tests for guidance
12+
- tests should be added to the nodeunit configs in `test/`
13+
- tests can be run with `npm test`
14+
- see existing tests for guidance

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ 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-
const {CRC32Stream} = require('crc32-stream');
20+
const { CRC32Stream } = require("crc32-stream");
2121

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

25-
checksum.on('end', function(err) {
25+
checksum.on("end", function (err) {
2626
// do something with checksum.digest() here
2727
});
2828

2929
// either pipe it
3030
source.pipe(checksum);
3131

3232
// or write it
33-
checksum.write('string');
33+
checksum.write("string");
3434
checksum.end();
3535
```
3636

@@ -39,20 +39,20 @@ checksum.end();
3939
Inherits [zlib.DeflateRaw](http://nodejs.org/api/zlib.html#zlib_class_zlib_deflateraw) options and methods.
4040

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

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

47-
checksum.on('end', function(err) {
47+
checksum.on("end", function (err) {
4848
// do something with checksum.digest() here
4949
});
5050

5151
// either pipe it
5252
source.pipe(checksum);
5353

5454
// or write it
55-
checksum.write('string');
55+
checksum.write("string");
5656
checksum.end();
5757
```
5858

lib/crc32-stream.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ import { Transform } from "readable-stream";
22
import crc32 from "crc-32";
33

44
export default class CRC32Stream extends Transform {
5-
constructor(options) {
6-
super(options);
7-
this.checksum = Buffer.allocUnsafe(4);
8-
this.checksum.writeInt32BE(0, 0);
9-
this.rawSize = 0;
5+
constructor(options) {
6+
super(options);
7+
this.checksum = Buffer.allocUnsafe(4);
8+
this.checksum.writeInt32BE(0, 0);
9+
this.rawSize = 0;
10+
}
11+
_transform(chunk, encoding, callback) {
12+
if (chunk) {
13+
this.checksum = crc32.buf(chunk, this.checksum) >>> 0;
14+
this.rawSize += chunk.length;
1015
}
11-
_transform(chunk, encoding, callback) {
12-
if (chunk) {
13-
this.checksum = crc32.buf(chunk, this.checksum) >>> 0;
14-
this.rawSize += chunk.length;
15-
}
16-
callback(null, chunk);
17-
}
18-
digest(encoding) {
19-
const checksum = Buffer.allocUnsafe(4);
20-
checksum.writeUInt32BE(this.checksum >>> 0, 0);
21-
return encoding ? checksum.toString(encoding) : checksum;
22-
}
23-
hex() {
24-
return this.digest('hex').toUpperCase();
25-
}
26-
size() {
27-
return this.rawSize;
28-
}
29-
}
16+
callback(null, chunk);
17+
}
18+
digest(encoding) {
19+
const checksum = Buffer.allocUnsafe(4);
20+
checksum.writeUInt32BE(this.checksum >>> 0, 0);
21+
return encoding ? checksum.toString(encoding) : checksum;
22+
}
23+
hex() {
24+
return this.digest("hex").toUpperCase();
25+
}
26+
size() {
27+
return this.rawSize;
28+
}
29+
}

lib/deflate-crc32-stream.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@ import { DeflateRaw } from "zlib";
22
import crc32 from "crc-32";
33

44
export default class DeflateCRC32Stream extends DeflateRaw {
5-
constructor(options) {
6-
super(options);
7-
this.checksum = Buffer.allocUnsafe(4);
8-
this.checksum.writeInt32BE(0, 0);
9-
this.rawSize = 0;
10-
this.compressedSize = 0;
5+
constructor(options) {
6+
super(options);
7+
this.checksum = Buffer.allocUnsafe(4);
8+
this.checksum.writeInt32BE(0, 0);
9+
this.rawSize = 0;
10+
this.compressedSize = 0;
11+
}
12+
push(chunk, encoding) {
13+
if (chunk) {
14+
this.compressedSize += chunk.length;
1115
}
12-
push(chunk, encoding) {
13-
if (chunk) {
14-
this.compressedSize += chunk.length;
15-
}
16-
return super.push(chunk, encoding);
16+
return super.push(chunk, encoding);
17+
}
18+
_transform(chunk, encoding, callback) {
19+
if (chunk) {
20+
this.checksum = crc32.buf(chunk, this.checksum) >>> 0;
21+
this.rawSize += chunk.length;
1722
}
18-
_transform(chunk, encoding, callback) {
19-
if (chunk) {
20-
this.checksum = crc32.buf(chunk, this.checksum) >>> 0;
21-
this.rawSize += chunk.length;
22-
}
23-
super._transform(chunk, encoding, callback);
24-
}
25-
digest(encoding) {
26-
const checksum = Buffer.allocUnsafe(4);
27-
checksum.writeUInt32BE(this.checksum >>> 0, 0);
28-
return encoding ? checksum.toString(encoding) : checksum;
29-
}
30-
hex() {
31-
return this.digest('hex').toUpperCase();
32-
}
33-
size(compressed = false) {
34-
if (compressed) {
35-
return this.compressedSize;
36-
}
37-
else {
38-
return this.rawSize;
39-
}
23+
super._transform(chunk, encoding, callback);
24+
}
25+
digest(encoding) {
26+
const checksum = Buffer.allocUnsafe(4);
27+
checksum.writeUInt32BE(this.checksum >>> 0, 0);
28+
return encoding ? checksum.toString(encoding) : checksum;
29+
}
30+
hex() {
31+
return this.digest("hex").toUpperCase();
32+
}
33+
size(compressed = false) {
34+
if (compressed) {
35+
return this.compressedSize;
36+
} else {
37+
return this.rawSize;
4038
}
39+
}
4140
}

lib/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
* Licensed under the MIT license.
66
* https://github.com/archiverjs/node-crc32-stream/blob/master/LICENSE-MIT
77
*/
8-
export const CRC32Stream = require('./crc32-stream');
9-
export const DeflateCRC32Stream = require('./deflate-crc32-stream');
8+
import CRC32Stream from "./crc32-stream.js";
9+
import DeflateCRC32Stream from "./deflate-crc32-stream.js";
10+
11+
export { CRC32Stream, DeflateCRC32Stream };
1012
export default {
11-
CRC32Stream,
12-
DeflateCRC32Stream
13+
CRC32Stream,
14+
DeflateCRC32Stream,
1315
};

package-lock.json

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
},
3333
"devDependencies": {
3434
"chai": "^5.1.1",
35-
"mocha": "10.7.3"
35+
"mocha": "10.7.3",
36+
"prettier": "3.3.3"
3637
},
3738
"keywords": [
3839
"crc32-stream",

test/checksum.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,44 @@ import crc32 from "crc-32";
33
import { BinaryStream, DeadEndStream } from "./helpers/index.js";
44
import CRC32Stream from "../lib/crc32-stream.js";
55
/*global before,describe,it */
6-
describe('CRC32Stream', function () {
7-
it('should checksum data while passing through data', function (done) {
8-
const binary = new BinaryStream(1024 * 16);
9-
const checksum = new CRC32Stream();
10-
const deadend = new DeadEndStream();
11-
checksum.on('end', function () {
12-
assert.equal(checksum.digest().readUInt32BE(0), 3893830384);
13-
assert.equal(checksum.digest('hex'), 'e81722f0');
14-
assert.equal(checksum.hex(), 'E81722F0');
15-
assert.equal(checksum.size(), 16384);
16-
done();
17-
});
18-
checksum.pipe(deadend);
19-
binary.pipe(checksum);
6+
7+
describe("CRC32Stream", function () {
8+
it("should checksum data while passing through data", function (done) {
9+
const binary = new BinaryStream(1024 * 16);
10+
const checksum = new CRC32Stream();
11+
const deadend = new DeadEndStream();
12+
checksum.on("end", function () {
13+
assert.equal(checksum.digest().readUInt32BE(0), 3893830384);
14+
assert.equal(checksum.digest("hex"), "e81722f0");
15+
assert.equal(checksum.hex(), "E81722F0");
16+
assert.equal(checksum.size(), 16384);
17+
done();
2018
});
21-
it('should have same checksum when bytes written together or separately', function (done) {
22-
const checksum = new CRC32Stream();
23-
const deadend = new DeadEndStream();
24-
const expectedChecksumValue = crc32.buf([157, 10, 217, 109, 100, 200, 300]) >>> 0;
25-
checksum.on('end', function () {
26-
assert.equal(checksum.digest().readUInt32BE(0), expectedChecksumValue);
27-
done();
28-
});
29-
checksum.write(Buffer.from([157, 10, 217, 109]));
30-
checksum.write(Buffer.from([100, 200, 300]));
31-
checksum.end();
32-
checksum.pipe(deadend);
19+
checksum.pipe(deadend);
20+
binary.pipe(checksum);
21+
});
22+
it("should have same checksum when bytes written together or separately", function (done) {
23+
const checksum = new CRC32Stream();
24+
const deadend = new DeadEndStream();
25+
const expectedChecksumValue =
26+
crc32.buf([157, 10, 217, 109, 100, 200, 300]) >>> 0;
27+
checksum.on("end", function () {
28+
assert.equal(checksum.digest().readUInt32BE(0), expectedChecksumValue);
29+
done();
3330
});
34-
it('should gracefully handle having no data chunks passed to it', function (done) {
35-
const checksum = new CRC32Stream();
36-
const deadend = new DeadEndStream();
37-
checksum.on('end', function () {
38-
assert.equal(checksum.digest().readUInt32BE(0), 0);
39-
done();
40-
});
41-
checksum.pipe(deadend);
42-
checksum.end();
31+
checksum.write(Buffer.from([157, 10, 217, 109]));
32+
checksum.write(Buffer.from([100, 200, 300]));
33+
checksum.end();
34+
checksum.pipe(deadend);
35+
});
36+
it("should gracefully handle having no data chunks passed to it", function (done) {
37+
const checksum = new CRC32Stream();
38+
const deadend = new DeadEndStream();
39+
checksum.on("end", function () {
40+
assert.equal(checksum.digest().readUInt32BE(0), 0);
41+
done();
4342
});
43+
checksum.pipe(deadend);
44+
checksum.end();
45+
});
4446
});

0 commit comments

Comments
 (0)