Skip to main content

Overview

The Model Download Manager provides a complete solution for downloading, caching, and managing Sherpa-ONNX model assets from official GitHub Releases. It handles archive models (.tar.bz2) and single-file models (.onnx), with built-in checksum validation, progress tracking, and disk space management.
Import from: react-native-sherpa-onnx/download

Key Features

  • Official model registry from GitHub Releases
  • Archive extraction with native libarchive (tar.bz2)
  • Checksum validation (SHA-256) for integrity verification
  • Download resume support for interrupted downloads
  • Progress events with speed and ETA calculation
  • Disk space checking before downloads
  • LRU cache cleanup for automatic model management
  • Retry logic with exponential backoff

Model Categories

The download manager organizes models by category:

Quick Start

Typical flow: refresh the model registry, download a model, then initialize STT/TTS with the local path.

API Reference

refreshModelsByCategory

Fetch and cache the latest model list from GitHub Releases. Use this before showing the available models UI.
ModelCategory
required
The model category to refresh
object
ModelMetaBase[]
Array of model metadata objects

listModelsByCategory

Return the cached model list. If no cache exists yet, returns an empty array.
ModelMetaBase[]
Array of cached model metadata

downloadModelByCategory

Download a model by ID with support for progress callbacks, cancellation, and automatic retries.
ModelCategory
required
The model category
string
required
Model ID to download
object
DownloadResult

getLocalModelPathByCategory

Get the local path of a downloaded model for initialization. Updates the model’s lastUsed timestamp.
string | null
Local path to the model directory, or null if not downloaded

listDownloadedModelsByCategory

Return only models that are fully downloaded on this device.
ModelMetaBase[]
Array of downloaded model metadata

isModelDownloadedByCategory

Check whether a specific model is downloaded and ready to use.
boolean
true if the model is downloaded and ready, false otherwise

getModelsCacheStatusByCategory

Return the last update timestamp for the cached model registry.
CacheStatus

getModelByIdByCategory

Return metadata for a specific model ID from the cached registry.
ModelMetaBase | null
Model metadata or null if not found

deleteModelByCategory

Remove a downloaded model and its cached files from disk.
This permanently deletes all model files. Use with caution.

clearModelCacheByCategory

Clear the cached model registry for a category. Does not delete downloaded models.

Progress Events

subscribeDownloadProgress

Subscribe to global download progress updates for all models. Returns an unsubscribe function.
DownloadProgressListener
required
Callback function that receives progress updates

subscribeModelsListUpdated

Subscribe to model list refresh events. Returns an unsubscribe function.

Checksum Validation

The download manager validates SHA-256 checksums when available to ensure file integrity:
1

Download checksum.txt

Fetches checksum.txt from GitHub Release and caches it in memory
2

Archive validation (tar.bz2)

Uses native libarchive hashing during extraction for efficient validation
3

Single-file validation (.onnx)

Uses local SHA-256 calculation after download
4

Fallback to GitHub digest

If checksum.txt doesn’t list a file, uses the GitHub asset digest if available

Checksum Issue Handling

By default, if checksum validation fails, the user is prompted with an alert dialog to either:
  • Delete and cancel: Remove the downloaded file and abort
  • Keep file: Continue with the potentially corrupted file
You can provide a custom handler:

LRU Cache Management

listDownloadedModelsWithMetadata

Get all downloaded models with metadata including download time, last used time, and disk size.

cleanupLeastRecentlyUsed

Automatically remove least recently used models to free disk space.
ModelCategory
required
Model category to clean up
object
string[]
Array of deleted model IDs

Download Resume

The download manager supports automatic resume for interrupted downloads:
  • Partial downloads are preserved when download is interrupted
  • Resume headers are sent on retry to continue from last byte
  • Non-resumable errors (404, 410) trigger full file deletion and restart
  • Extracted directories are cleaned up but archives are kept for resume

Best Practices

Cache Before Download

Always call refreshModelsByCategory before showing the model list UI to ensure users see the latest available models.

Progress Feedback

Use onProgress callbacks to show download progress to users. Display phase, percentage, speed, and ETA.

Error Handling

Handle download errors gracefully. Network failures will auto-retry with exponential backoff.

Disk Space

Use cleanupLeastRecentlyUsed to automatically manage disk space when approaching limits.

Download Resume

Leverage automatic resume support by preserving partial downloads on cancellation.

Checksum Validation

Don’t skip checksum validation. It ensures model integrity and prevents corrupted files.

Storage Locations

Models are stored in platform-specific directories:
  • iOS: DocumentDirectoryPath/sherpa-onnx/models/{category}/
  • Android: DocumentDirectoryPath/sherpa-onnx/models/{category}/
Cache files are stored in:
  • DocumentDirectoryPath/sherpa-onnx/cache/
Each downloaded model has:
  • Model directory: {category}/{model-id}/
  • Ready marker: {category}/{model-id}/.ready
  • Manifest: {category}/{model-id}/manifest.json