diff --git a/packages/middleware-flexible-checksums/package.json b/packages/middleware-flexible-checksums/package.json index b929c541b01b4..c26fe92c26657 100644 --- a/packages/middleware-flexible-checksums/package.json +++ b/packages/middleware-flexible-checksums/package.json @@ -17,10 +17,13 @@ "main": "./dist-cjs/index.js", "module": "./dist-es/index.js", "browser": { + "./dist-es/getCrc32ChecksumAlgorithmFunction": "./dist-es/getCrc32ChecksumAlgorithmFunction.browser", "./dist-es/streams/create-read-stream-on-buffer": "./dist-es/streams/create-read-stream-on-buffer.browser" }, "react-native": { + "./dist-es/getCrc32ChecksumAlgorithmFunction": "./dist-es/getCrc32ChecksumAlgorithmFunction.browser", "./dist-es/streams/create-read-stream-on-buffer": "./dist-es/streams/create-read-stream-on-buffer.browser", + "./dist-cjs/getCrc32ChecksumAlgorithmFunction": "./dist-cjs/getCrc32ChecksumAlgorithmFunction.browser", "./dist-cjs/streams/create-read-stream-on-buffer": "./dist-cjs/streams/create-read-stream-on-buffer.browser" }, "types": "./dist-types/index.d.ts", @@ -32,6 +35,7 @@ "dependencies": { "@aws-crypto/crc32": "5.2.0", "@aws-crypto/crc32c": "5.2.0", + "@aws-crypto/util": "5.2.0", "@aws-sdk/core": "*", "@aws-sdk/types": "*", "@smithy/is-array-buffer": "^3.0.0", diff --git a/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.browser.ts b/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.browser.ts new file mode 100644 index 0000000000000..1c482b41706c2 --- /dev/null +++ b/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.browser.ts @@ -0,0 +1,3 @@ +import { AwsCrc32 } from "@aws-crypto/crc32"; + +export const getCrc32ChecksumAlgorithmFunction = () => AwsCrc32; diff --git a/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts b/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts new file mode 100644 index 0000000000000..8d38987a975f7 --- /dev/null +++ b/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts @@ -0,0 +1,28 @@ +import { AwsCrc32 } from "@aws-crypto/crc32"; +import { numToUint8 } from "@aws-crypto/util"; +import { Checksum } from "@smithy/types"; +import 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 { + private 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; + } + }; +}; diff --git a/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.ts b/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.ts index 39e0f192245a0..74a09add4a300 100644 --- a/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.ts +++ b/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.ts @@ -1,9 +1,9 @@ -import { AwsCrc32 } from "@aws-crypto/crc32"; import { AwsCrc32c } from "@aws-crypto/crc32c"; import { ChecksumConstructor, HashConstructor } from "@smithy/types"; import { PreviouslyResolved } from "./configuration"; import { ChecksumAlgorithm } from "./constants"; +import { getCrc32ChecksumAlgorithmFunction } from "./getCrc32ChecksumAlgorithmFunction"; /** * Returns the function that will compute the checksum for the given {@link ChecksumAlgorithm}. @@ -14,7 +14,7 @@ export const selectChecksumAlgorithmFunction = ( ): ChecksumConstructor | HashConstructor => ({ [ChecksumAlgorithm.MD5]: config.md5, - [ChecksumAlgorithm.CRC32]: AwsCrc32, + [ChecksumAlgorithm.CRC32]: getCrc32ChecksumAlgorithmFunction(), [ChecksumAlgorithm.CRC32C]: AwsCrc32c, [ChecksumAlgorithm.SHA1]: config.sha1, [ChecksumAlgorithm.SHA256]: config.sha256, diff --git a/yarn.lock b/yarn.lock index cecbf306d72da..5c0b0a0276059 100644 --- a/yarn.lock +++ b/yarn.lock @@ -74,7 +74,7 @@ dependencies: tslib "^2.6.2" -"@aws-crypto/util@^5.2.0": +"@aws-crypto/util@5.2.0", "@aws-crypto/util@^5.2.0": version "5.2.0" resolved "https://registry.npmjs.org/@aws-crypto/util/-/util-5.2.0.tgz" integrity sha512-4RkU9EsI6ZpBve5fseQlGNUWKMa1RLPQ1dnjnQoe07ldfIzcsGb5hC5W0Dm7u423KWzawlrpbjXBrXCEv9zazQ==