Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(middleware-flexible-checksums): move inline class NodeCrc32 outside #6648

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading