Skip to content

Commit

Permalink
feat(types): add checksum interface (#4216)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS authored Nov 30, 2022
1 parent a27adec commit 89598b6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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

0 comments on commit 89598b6

Please sign in to comment.