Skip to main content

DownloadPipeline

DownloadPipeline is the base abstract class responsible for managing the file download flow in rac-delta. It provides the core structure for notifying progress, handling state changes, and coordinating downloads, but does not implement all the actual storage logic.

This class is meant to be extended by specialized pipelines such as HashDownloadPipeline and UrlDownloadPipeline.


Methods

Shared base methods.

MethodReturnsDescription
updateProgress(value, state, diskUsage?, speed?, options?)voidCallback for progress such as downloading or reconstructing files.
changeState(state, options?)voidCallback to notify state changes.
loadLocalIndex(dir)Promise<RDIndex>Will create a rd-index.json of a given directory.
findLocalIndex(localDir)Promise<RDIndex | null>Will find for a rd-index.json if exists on given folder.
verifyAndDeleteObsoleteChunks(plan, localDir, remoteIndex, chunkSource, options?)Promise<{ deletedFiles: string[]; verifiedFiles: string[]; rebuiltFiles: string[] }>This method will check for reconstructed files, verifying its hash and obsolete chunks.
saveLocalIndex(localDir, index)Promise<void>This method will save the new local index on given folder.

DownloadOptions

The DownloadOptions object allows you to customize the behavior of a download:

export interface DownloadOptions {
force?: boolean;
chunksSavePath?: string;
useExistingIndex?: boolean;
fileReconstructionConcurrency?: number;
inPlaceReconstructionThreshold?: number;
onProgress?: (
type: 'download' | 'deleting' | 'reconstructing',
progress: number,
diskUsage?: number,
speed?: number
) => void;
onStateChange?: (state: 'downloading' | 'reconstructing' | 'cleaning' | 'scanning') => void;
}
PropertyTypeDescription
forcebooleanIf true, downloads everything. If false, only new and modified chunks will be downloaded.
chunksSavePathstringPath where chunks will be saved if DownloadAllFirstToDisk strategy is set.
useExistingIndexbooleanIf true, will search first an existing rd-index in local dir.
fileReconstructionConcurrencynumberHow many files will be reconstructed concurrently.
inPlaceReconstructionThresholdnumberMinimum file size (in bytes) required to perform an in-place reconstruction instead of using a temporary file.
onProgress(type, progress, diskUsage?, speed?) => voidOptional callback to inform progress.
onStateChange(state) => voidOptional callback for state changes.

UpdateStrategy

Enum for different strategies for reconstructing and downloading files.

export enum UpdateStrategy {
DownloadAllFirstToMemory = 'download-all-first-to-memory',
StreamFromNetwork = 'stream-from-network',
DownloadAllFirstToDisk = 'download-all-first-to-disk',
}

For more info see: Download pipeline usage


Method details

updateProgress(value, state, diskUsage?, speed?, options?)

Used to call given onProgress callback inside options.

Parameters

NameTypeDescription
valuenumberProgress value (0 - 100)
statedownload | deleting | reconstructingWhich operation is being monitorized
diskUsage?numberSpeed in bytes/s (only for state reconstructing)
speed?numberSpeed in bytes/s (only for state download)
options?DownloadOptionsOptions object, see DownloadOptions above for more info (this method only uses callback)

Returns

void


changeState(state, options?)

Used to call given onStateChange callback inside options.

Parameters

NameTypeDescription
state'download' | 'deleting' | 'reconstructing'State of the flow.
options?DownloadOptionsOptions object, see DownloadOptions above for more info (this method only uses callback)

Returns

void


loadLocalIndex(dir)

This method will create a rd-index.json of a given directory, scanning files and generating hashes.

Parameters

NameTypeDescription
dirstringDirectory path to generate rd-index.

Returns

Promise<RDIndex>


findLocalIndex(localDir)

This method will find for a rd-index.json if exists on given folder.

Parameters

NameTypeDescription
localDirstringDirectory path to search rd-index.

Returns

Promise<RDIndex>


verifyAndDeleteObsoleteChunks(plan, localDir, remoteIndex, chunkSource, options?)

This method will check for reconstructed files, verifying its hash and obsolete chunks. If obsolete chunks are still present, it will delete them, and reconstruct file again if needed.

Parameters

NameTypeDescription
planDeltaPlanthe DeltaPlan generated by delta.compare of the two rd-index.json for the download.
localDirstringDirectory path to check files.
remoteIndexRDIndexThe remote index for reference.
chunkSourceChunkSourceChunkSource to download corrupt chunks in case of invalid files. See ChunkSource
options?DownloadOptionsOptions object, see DownloadOptions above for more info (this method only uses callback)

Returns

Promise<{ deletedFiles: string[]; verifiedFiles: string[]; rebuiltFiles: string[] }>


saveLocalIndex(localDir, index)

This method will save the new local index on given folder.

Parameters

NameTypeDescription
localDirstringDirectory where the new index will be saved.
indexRDIndexThe RDIndex object to save.

Returns

Promise<void>