From 473f94957f2912173fbbcdece52c23e78d960668 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Tue, 14 Jan 2025 21:42:45 -0800 Subject: [PATCH] fix(crc64-nvme-crt): return checksum for empty string if called without data (#6798) --- packages/crc64-nvme-crt/src/CrtCrc64Nvme.spec.ts | 5 +++-- packages/crc64-nvme-crt/src/CrtCrc64Nvme.ts | 13 +++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/crc64-nvme-crt/src/CrtCrc64Nvme.spec.ts b/packages/crc64-nvme-crt/src/CrtCrc64Nvme.spec.ts index 6c71ae9ca944..90db0ae5430c 100644 --- a/packages/crc64-nvme-crt/src/CrtCrc64Nvme.spec.ts +++ b/packages/crc64-nvme-crt/src/CrtCrc64Nvme.spec.ts @@ -4,9 +4,10 @@ import { describe, expect, it } from "vitest"; import { CrtCrc64Nvme } from "./CrtCrc64Nvme"; describe(CrtCrc64Nvme.name, () => { - it("should throw an error if digest is called before update", async () => { + it("should return checksum for empty string if digest is called before update", async () => { const crc64 = new CrtCrc64Nvme(); - await expect(crc64.digest()).rejects.toThrowError("No data provided to checksum"); + const digest = await crc64.digest(); + expect(toBase64(digest)).toEqual("AAAAAAAAAAA="); }); it.each([ diff --git a/packages/crc64-nvme-crt/src/CrtCrc64Nvme.ts b/packages/crc64-nvme-crt/src/CrtCrc64Nvme.ts index 3f8be18f148f..0bcff3ccc3cf 100644 --- a/packages/crc64-nvme-crt/src/CrtCrc64Nvme.ts +++ b/packages/crc64-nvme-crt/src/CrtCrc64Nvme.ts @@ -3,20 +3,17 @@ import { Checksum } from "@smithy/types"; import { toUint8Array } from "@smithy/util-utf8"; export class CrtCrc64Nvme implements Checksum { - private previous: DataView | undefined; + private checksum: DataView = new DataView(new ArrayBuffer(8)); - update(chunk: Uint8Array) { - this.previous = checksums.crc64nvme(chunk, this.previous); + update(data: Uint8Array) { + this.checksum = checksums.crc64nvme(data, this.checksum); } async digest() { - if (!this.previous) { - throw new Error("No data provided to checksum"); - } - return toUint8Array(this.previous); + return toUint8Array(this.checksum); } reset() { - this.previous = undefined; + this.checksum = new DataView(new ArrayBuffer(8)); } }