Skip to content

Commit

Permalink
chore(middleware-flexible-checksums): move inline class NodeCrc32 out…
Browse files Browse the repository at this point in the history
…side (#6648)
  • Loading branch information
trivikr authored Nov 8, 2024
1 parent b2fb10a commit 17b37b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ import { numToUint8 } from "@aws-crypto/util";
import { Checksum } from "@smithy/types";
import * as zlib from "zlib";

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;
}
}

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;
}
};
return NodeCrc32;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AwsCrc32c } from "@aws-crypto/crc32c";
import { describe, expect, test as it, vi } from "vitest";

import { ChecksumAlgorithm } from "./constants";
// import { getCrc32ChecksumAlgorithmFunction } from "./getCrc32ChecksumAlgorithmFunction";
import { getCrc32ChecksumAlgorithmFunction } from "./getCrc32ChecksumAlgorithmFunction";
import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction";

describe(selectChecksumAlgorithmFunction.name, () => {
Expand All @@ -14,7 +14,7 @@ describe(selectChecksumAlgorithmFunction.name, () => {

it.each([
[ChecksumAlgorithm.MD5, mockConfig.md5],
// [ChecksumAlgorithm.CRC32, getCrc32ChecksumAlgorithmFunction()],
[ChecksumAlgorithm.CRC32, getCrc32ChecksumAlgorithmFunction()],
[ChecksumAlgorithm.CRC32C, AwsCrc32c],
[ChecksumAlgorithm.SHA1, mockConfig.sha1],
[ChecksumAlgorithm.SHA256, mockConfig.sha256],
Expand Down

0 comments on commit 17b37b7

Please sign in to comment.