Skip to main content

HasherService

HasherService defines the API to calculate hashes of files, chunks, streams, and buffers. It is abstract; real implementations are in the infrastructure layer (e.g., HashWasmHasherService in Node.js).

This page documents the public API available in the Node.js SDK.


Methods

MethodReturnsDescription
hashFile(filePath, rootDir, chunkSize)Promise<FileEntry>Returns a FileEntry calculating the file hash and chunk hashes.
hashStream(stream, chunkSize, onChunk)Promise<void>Processes a stream of chunks and returns every chunk via callback.
hashBuffer(data)Promise<string>Returns the hash of a buffer (hex string).
verifyChunk(data, expectedHash)Promise<boolean>Verifies that a chunk has the expected hash.
verifyFile(path, expectedHash)Promise<boolean>Verifies that a file has the expected hash.
createStreamingHasher()Promise<StreamingHasher>Creates a StreamingHasher object for incremental hashing.

Method Details

hashFile(filePath, rootDir, chunkSize)

Returns a FileEntry of the given file, calculating its hash and chunk hashes.

IMPORTANT NOTE: selected chunkSize must be the same in all operations of rac-delta

Parameters:

NameTypeDescription
filePathstringRelative path of the file (dir/file.txt)
rootDirstringRoot directory of the index (dir)
chunkSizenumberSize in bytes of each chunk (recommended 1 MB)

Returns: Promise<FileEntry>


hashStream(stream, chunkSize, onChunk)

Processes a stream of chunks, calling onChunk for each processed chunk.

Parameters:

NameTypeDescription
streamAsyncChunkStreamThe input chunk stream
chunkSizenumberThe size of the chunks to be processed and emitted.
onChunk(data: Uint8Array, chunk: Chunk) => voidCallback for each hashed chunk
export interface AsyncChunkStream extends AsyncIterable<Uint8Array> {
nextChunk(): Promise<Uint8Array | null>;
reset?(): Promise<void>;
close?(): Promise<void>;
}

Returns: Promise<void>


hashBuffer(data)

Hashes a buffer.

ParameterTypeDescription
dataUint8ArrayThe buffer to hash

Returns: Promise<string> (hex string)


verifyChunk(data, expectedHash)

Checks if a chunk has the expected hash.

ParameterTypeDescription
dataUint8ArrayChunk data
expectedHashstringExpected hash

Returns: Promise<boolean>


verifyFile(path, expectedHash)

Checks if a file has the expected hash.

ParameterTypeDescription
pathstringFile path
expectedHashstringExpected hash

Returns: Promise<boolean>


createStreamingHasher()

Creates a StreamingHasher instance:

export interface StreamingHasher {
update(data: Uint8Array | Buffer): void;
digest(encoding?: 'hex'): string;
}

Returns: Promise<StreamingHasher>

Example

const fileEntry = await racDeltaClient.hasher.hashFile('my-dir/file.txt', 'my-dir', 1024 * 1024);