From 238a9d59dfb23944b0c3e4a36fef8e421fb11e16 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 7 Nov 2024 23:29:17 +0000 Subject: [PATCH] feat(middleware-flexible-checksums): use Node.js native CRC32 checksum API --- .../package.json | 4 +++ ...tCrc32ChecksumAlgorithmFunction.browser.ts | 3 ++ .../src/getCrc32ChecksumAlgorithmFunction.ts | 28 +++++++++++++++++++ yarn.lock | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.browser.ts create mode 100644 packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts diff --git a/packages/middleware-flexible-checksums/package.json b/packages/middleware-flexible-checksums/package.json index b929c541b01b..c26fe92c2665 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 000000000000..1c482b41706c --- /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 000000000000..8d38987a975f --- /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/yarn.lock b/yarn.lock index cecbf306d72d..5c0b0a027605 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==