-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware-flexible-checksums): use Node.js native CRC32 checksu…
…m API (#6641)
- Loading branch information
Showing
6 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { AwsCrc32 } from "@aws-crypto/crc32"; | ||
|
||
export const getCrc32ChecksumAlgorithmFunction = () => AwsCrc32; |
47 changes: 47 additions & 0 deletions
47
packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { AwsCrc32 } from "@aws-crypto/crc32"; | ||
import { numToUint8 } from "@aws-crypto/util"; | ||
import { describe, expect, test as it, vi } from "vitest"; | ||
import * as zlib from "zlib"; | ||
|
||
import { getCrc32ChecksumAlgorithmFunction } from "./getCrc32ChecksumAlgorithmFunction"; | ||
|
||
describe(getCrc32ChecksumAlgorithmFunction.name, () => { | ||
it("returns AwsCrc32 if zlib.crc32 is undefined", () => { | ||
vi.mock("zlib", () => ({ crc32: undefined })); | ||
expect(getCrc32ChecksumAlgorithmFunction()).toBe(AwsCrc32); | ||
}); | ||
|
||
it("returns NodeCrc32 if zlib.crc32 is defined", async () => { | ||
const mockData = new Uint8Array([1, 2, 3]); | ||
const mockChecksum = 42; | ||
|
||
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0. | ||
zlib.crc32 = vi | ||
.fn() | ||
.mockReturnValueOnce(mockChecksum) | ||
.mockReturnValueOnce(2 * mockChecksum); | ||
|
||
const crc32Fn = getCrc32ChecksumAlgorithmFunction(); | ||
expect(crc32Fn).not.toBe(AwsCrc32); | ||
|
||
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0. | ||
expect(zlib.crc32).not.toHaveBeenCalled(); | ||
const crc32 = new crc32Fn(); | ||
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0. | ||
expect(zlib.crc32).not.toHaveBeenCalled(); | ||
expect(await crc32.digest()).toEqual(numToUint8(0)); | ||
|
||
crc32.update(mockData); | ||
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0. | ||
expect(zlib.crc32).toHaveBeenCalledWith(mockData, 0); | ||
expect(await crc32.digest()).toEqual(numToUint8(mockChecksum)); | ||
|
||
crc32.update(mockData); | ||
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0. | ||
expect(zlib.crc32).toHaveBeenCalledWith(mockData, mockChecksum); | ||
expect(await crc32.digest()).toEqual(numToUint8(2 * mockChecksum)); | ||
|
||
crc32.reset(); | ||
expect(await crc32.digest()).toEqual(numToUint8(0)); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { AwsCrc32 } from "@aws-crypto/crc32"; | ||
import { numToUint8 } from "@aws-crypto/util"; | ||
import { Checksum } from "@smithy/types"; | ||
import * as zlib from "zlib"; | ||
|
||
export const getCrc32ChecksumAlgorithmFunction = () => { | ||
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0. | ||
if (typeof zlib.crc32 === "undefined") { | ||
return AwsCrc32; | ||
} | ||
|
||
return class NodeCrc32 implements Checksum { | ||
checksum = 0; | ||
|
||
update(data: Uint8Array) { | ||
// @ts-expect-error crc32 is defined only for Node.js >=v20.15.0 and >=v22.2.0. | ||
this.checksum = zlib.crc32(data, this.checksum); | ||
} | ||
|
||
async digest() { | ||
return numToUint8(this.checksum); | ||
} | ||
|
||
reset() { | ||
this.checksum = 0; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters