Skip to content

Commit

Permalink
fix(crc64-nvme-crt): return checksum for empty string if called witho…
Browse files Browse the repository at this point in the history
…ut data (#6798)
  • Loading branch information
trivikr authored Jan 15, 2025
1 parent e1dff72 commit 473f949
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/crc64-nvme-crt/src/CrtCrc64Nvme.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
13 changes: 5 additions & 8 deletions packages/crc64-nvme-crt/src/CrtCrc64Nvme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 473f949

Please sign in to comment.