Skip to content

Commit

Permalink
feat(middleware-flexible-checksums): use Node.js native CRC32 checksu…
Browse files Browse the repository at this point in the history
…m API
  • Loading branch information
trivikr committed Nov 7, 2024
1 parent 0c558ef commit 238a9d5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/middleware-flexible-checksums/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AwsCrc32 } from "@aws-crypto/crc32";

export const getCrc32ChecksumAlgorithmFunction = () => AwsCrc32;
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 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;
}
};
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 238a9d5

Please sign in to comment.