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

fix(crc64-nvme-crt): return checksum for empty string if called without data #6798

Merged
merged 1 commit into from
Jan 15, 2025
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
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor issue:

given that CrtCrc64Nvme implements Checksum, an instance of this may be called "checksum", so this private property would be checksum.checksum.

perhaps it would more correctly be called "hash"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update this in a separate PR, since we use checksum for the private member in NodeCrc32 too.


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