From 17b37b72c0afe2de19d26f3ff7e647400a2f9675 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Fri, 8 Nov 2024 12:54:49 -0800 Subject: [PATCH] chore(middleware-flexible-checksums): move inline class NodeCrc32 outside (#6648) --- .../src/getCrc32ChecksumAlgorithmFunction.ts | 34 ++++++++++--------- .../selectChecksumAlgorithmFunction.spec.ts | 4 +-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts b/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts index 3803ccfdedae..92046dfdd7a1 100644 --- a/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts +++ b/packages/middleware-flexible-checksums/src/getCrc32ChecksumAlgorithmFunction.ts @@ -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; }; diff --git a/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.spec.ts b/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.spec.ts index e3c41477aea4..7d1e7b785e81 100644 --- a/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.spec.ts +++ b/packages/middleware-flexible-checksums/src/selectChecksumAlgorithmFunction.spec.ts @@ -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, () => { @@ -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],