Skip to content

feat(types): add checksum interface #4216

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

Merged
merged 1 commit into from
Nov 30, 2022
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
55 changes: 55 additions & 0 deletions packages/types/src/checksum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* An object that provides a checksum of data provided in chunks to `update`.
* The checksum may be performed incrementally as chunks are received or all
* at once when the checksum is finalized, depending on the underlying
* implementation.
*
* It's recommended to compute checksum incrementally to avoid reading the
* entire payload in memory.
*
* A class that implements this interface may accept an optional secret key in its
* constructor while computing checksum value, when using HMAC. If provided,
* this secret key would be used when computing checksum.
*/
export interface Checksum {
/**
* Constant length of the digest created by the algorithm in bytes.
*/
digestLength?: number;

/**
* Creates a new checksum object that contains a deep copy of the internal
* state of the current `Checksum` object.
*/
copy?(): Checksum;

/**
* Returns the digest of all of the data passed.
*/
digest(): Promise<Uint8Array>;

/**
* Allows marking a checksum for checksums that support the ability
* to mark and reset.
*
* @param {number} readLimit - The maximum limit of bytes that can be read
* before the mark position becomes invalid.
*/
mark?(readLimit: number): void;

/**
* Resets the checksum to its initial value.
*/
reset(): void;

/**
* Adds a chunk of data for which checksum needs to be computed.
* This can be called many times with new data as it is streamed.
*
* Implementations may override this method which passes second param
* which makes Checksum object stateless.
*
* @param {Uint8Array} chunk - The buffer to update checksum with.
*/
update(chunk: Uint8Array): void;
}
4 changes: 4 additions & 0 deletions packages/types/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Checksum } from "./checksum";

export type SourceData = string | ArrayBuffer | ArrayBufferView;

/**
* An object that provides a hash of data provided in chunks to `update`. The
* hash may be performed incrementally as chunks are received or all at once
* when the hash is finalized, depending on the underlying implementation.
*
* @deprecated use {@link Checksum}
*/
export interface Hash {
/**
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./abort";
export * from "./auth";
export * from "./checksum";
export * from "./client";
export * from "./command";
export * from "./credentials";
Expand Down