Skip to content

Download Core

npm versionDownloadsInstall sizenpmBuild Status

Provide a high performance download file function based on undici.

  • Support download by range request
    • Customize the range size
  • Support validating the checksum
    • If the validation matched, it won't download the file.
    • Also support customize validation.
  • Support download and fallback to another url
  • Support AbortSignal
  • Fully customizable retry logic

Usage

Download the file by url

ts
import { download } from '@xmcl/file-transfer'

await download({
  url: 'http://example.com/file.zip', // required
  destination: 'file.zip', // required
  headers: { // optional
    'customized': 'header'
  },
  abortSignal: new AbortController().signal, // optional
  progressController: (url, chunkSize, progress, total) => { // optional
    console.log(url)
    console.log(chunkSize)
    console.log(progress)
    console.log(total)
  },
  // use validator to validate the file
  validator: { // optional
    algorithm: 'sha1',
    hash: '1234567890abcdef1234567890abcdef12345678',
  }
})

Download with fallback url

ts
import { download } from '@xmcl/file-transfer'

await download({
  // using array to fallback
  url: ['http://example.com/file.zip', 'http://example.com/fallback.zip'],
  destination: 'file.zip',
})

🧾 Classes

🤝 Interfaces

🏭 Functions

createDefaultRetryHandler

ts
createDefaultRetryHandler(maxRetryCount: number= 3): RetryPolicy

Parameters

  • maxRetryCount: number

Return Type

  • RetryPolicy

Defined in: packages/file-transfer/retry.ts:46

createProgressController

ts
createProgressController(onProgress: Function): ProgressController

Parameters

  • onProgress: Function

Return Type

  • ProgressController

Defined in: packages/file-transfer/progress.ts:11

download

ts
download(options: DownloadOptions): Promise<void>

Download url or urls to a file path. This process is abortable, it's compatible with the dom like AbortSignal.

Parameters

  • options: DownloadOptions

Return Type

  • Promise<void>

Defined in: packages/file-transfer/download.ts:90

isRangePolicy

ts
isRangePolicy(rangeOptions: RangePolicy | DefaultRangePolicyOptions): rangeOptions is RangePolicy

Parameters

  • rangeOptions: RangePolicy | DefaultRangePolicyOptions

Return Type

  • rangeOptions is RangePolicy

Defined in: packages/file-transfer/rangePolicy.ts:10

isRetryHandler

ts
isRetryHandler(options: RetryPolicy | DefaultRetryPolicyOptions): options is RetryPolicy

Parameters

  • options: RetryPolicy | DefaultRetryPolicyOptions

Return Type

  • options is RetryPolicy

Defined in: packages/file-transfer/retry.ts:35

isValidator

ts
isValidator(options: Validator | ChecksumValidatorOptions): options is Validator

Parameters

  • options: Validator | ChecksumValidatorOptions

Return Type

  • options is Validator

Defined in: packages/file-transfer/validator.ts:32

resolveAbortSignal

ts
resolveAbortSignal(signal: AbortSignal): AbortSignal

Parameters

  • signal: AbortSignal

Return Type

  • AbortSignal

Defined in: packages/file-transfer/abort.ts:1

resolveAgent

ts
resolveAgent(agent: DownloadAgentOptions | DownloadAgent): DownloadAgent

Parameters

  • agent: DownloadAgentOptions | DownloadAgent

Return Type

  • DownloadAgent

Defined in: packages/file-transfer/agent.ts:21

resolveProgressController

ts
resolveProgressController(controller: ProgressController | Function): ProgressController

Parameters

  • controller: ProgressController | Function

Return Type

  • ProgressController

Defined in: packages/file-transfer/progress.ts:23

resolveRangePolicy

ts
resolveRangePolicy(rangeOptions: RangePolicy | DefaultRangePolicyOptions): RangePolicy

Parameters

  • rangeOptions: RangePolicy | DefaultRangePolicyOptions

Return Type

  • RangePolicy

Defined in: packages/file-transfer/rangePolicy.ts:15

resolveRetryHandler

ts
resolveRetryHandler(options: RetryPolicy | DefaultRetryPolicyOptions): RetryPolicy

Parameters

  • options: RetryPolicy | DefaultRetryPolicyOptions

Return Type

  • RetryPolicy

Defined in: packages/file-transfer/retry.ts:40

resolveValidator

ts
resolveValidator(options: Validator | ChecksumValidatorOptions): Validator

Parameters

  • options: Validator | ChecksumValidatorOptions

Return Type

  • Validator

Defined in: packages/file-transfer/validator.ts:37

Released under the MIT License.