Skip to content

Commit adfe83e

Browse files
committed
test: src/getCrc32ChecksumAlgorithmFunction.spec.ts
1 parent 2cd531b commit adfe83e

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { AwsCrc32 } from "@aws-crypto/crc32";
2+
import { numToUint8 } from "@aws-crypto/util";
3+
import { describe, expect, test as it, vi } from "vitest";
4+
import zlib from "zlib";
5+
6+
import { getCrc32ChecksumAlgorithmFunction } from "./getCrc32ChecksumAlgorithmFunction";
7+
8+
describe(getCrc32ChecksumAlgorithmFunction.name, () => {
9+
it("returns AwsCrc32 if zlib.crc32 is undefined", () => {
10+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
11+
zlib.crc32 = undefined;
12+
expect(getCrc32ChecksumAlgorithmFunction()).toBe(AwsCrc32);
13+
});
14+
15+
it("returns NodeCrc32 if zlib.crc32 is defined", async () => {
16+
const mockData = new Uint8Array([1, 2, 3]);
17+
const mockChecksum = 42;
18+
19+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
20+
zlib.crc32 = vi
21+
.fn()
22+
.mockReturnValueOnce(mockChecksum)
23+
.mockReturnValueOnce(2 * mockChecksum);
24+
25+
const crc32Fn = getCrc32ChecksumAlgorithmFunction();
26+
expect(crc32Fn).not.toBe(AwsCrc32);
27+
28+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
29+
expect(zlib.crc32).not.toHaveBeenCalled();
30+
const crc32 = new crc32Fn();
31+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
32+
expect(zlib.crc32).not.toHaveBeenCalled();
33+
expect(await crc32.digest()).toEqual(numToUint8(0));
34+
35+
crc32.update(mockData);
36+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
37+
expect(zlib.crc32).toHaveBeenCalledWith(mockData, 0);
38+
expect(await crc32.digest()).toEqual(numToUint8(mockChecksum));
39+
40+
crc32.update(mockData);
41+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
42+
expect(zlib.crc32).toHaveBeenCalledWith(mockData, mockChecksum);
43+
expect(await crc32.digest()).toEqual(numToUint8(2 * mockChecksum));
44+
45+
crc32.reset();
46+
expect(await crc32.digest()).toEqual(numToUint8(0));
47+
});
48+
});

packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Checksum } from "@smithy/types";
44
import zlib from "zlib";
55

66
export const getCrc32ChecksumAlgorithmFunction = () => {
7-
// @ts-expect-error crc32 is defined only for Node.js >v20.15.0+ and >v22.2.0+.
7+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
88
if (typeof zlib.crc32 === "undefined") {
99
return AwsCrc32;
1010
}
@@ -13,7 +13,7 @@ export const getCrc32ChecksumAlgorithmFunction = () => {
1313
private checksum = 0;
1414

1515
update(data: Uint8Array) {
16-
// @ts-expect-error crc32 is defined only for Node.js >v20.15.0+ and >v22.2.0+.
16+
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0.
1717
this.checksum = zlib.crc32(data, this.checksum);
1818
}
1919

0 commit comments

Comments
 (0)