From 24d6701abf86527c8b307a63d946a22675376733 Mon Sep 17 00:00:00 2001 From: ido Date: Mon, 26 Feb 2024 22:06:42 +0200 Subject: [PATCH] feat: default download info --- src/browser.ts | 4 ++-- .../download-engine/engine/base-download-engine.ts | 5 ++++- .../base-download-engine-fetch-stream.ts | 7 ++++--- src/index.ts | 4 ++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/browser.ts b/src/browser.ts index d973842..2a6d0a6 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -11,7 +11,7 @@ import EmptyResponseError from "./download/download-engine/streams/download-engi import StatusCodeError from "./download/download-engine/streams/download-engine-fetch-stream/errors/status-code-error.js"; import XhrError from "./download/download-engine/streams/download-engine-fetch-stream/errors/xhr-error.js"; import BaseDownloadEngineFetchStream, { - DownloadEngineFetchStreamOptions + BaseDownloadEngineFetchStreamOptions } from "./download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.js"; import DownloadEngineFetchStreamFetch from "./download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-fetch.js"; @@ -46,6 +46,6 @@ export type { DownloadEngineOptionsBrowser, DownloadEngineWriteStreamBrowserWriter, DownloadEngineFileOptionsWithDefaults as DownloadEngineFileOptions, - DownloadEngineFetchStreamOptions, + BaseDownloadEngineFetchStreamOptions, TransferProgressWithStatus }; diff --git a/src/download/download-engine/engine/base-download-engine.ts b/src/download/download-engine/engine/base-download-engine.ts index 8ee2a69..27da8ca 100644 --- a/src/download/download-engine/engine/base-download-engine.ts +++ b/src/download/download-engine/engine/base-download-engine.ts @@ -1,6 +1,8 @@ import {DownloadEngineFileOptions, DownloadFile} from "../types.js"; import DownloadEngineFile, {DownloadEngineFileEvents} from "../download-engine-file.js"; -import BaseDownloadEngineFetchStream from "../streams/download-engine-fetch-stream/base-download-engine-fetch-stream.js"; +import BaseDownloadEngineFetchStream, { + BaseDownloadEngineFetchStreamOptions +} from "../streams/download-engine-fetch-stream/base-download-engine-fetch-stream.js"; import UrlInputError from "./error/url-input-error.js"; import Emittery from "emittery"; import ProgressStatisticsBuilder, {TransferProgressWithStatus} from "../../transfer-visualize/progress-statistics-builder.js"; @@ -13,6 +15,7 @@ export type BaseDownloadEngineOptions; acceptRangeIsKnown?: boolean; fetchStrategy?: FetchStrategy; + defaultFetchDownloadInfo?: BaseDownloadEngineFetchStreamOptions["defaultFetchDownloadInfo"]; }; export interface BaseDownloadEngineEvents extends Omit { diff --git a/src/download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.ts b/src/download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.ts index 8de2887..9ade071 100644 --- a/src/download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.ts +++ b/src/download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.ts @@ -1,16 +1,17 @@ import retry from "async-retry"; -export type DownloadEngineFetchStreamOptions = { +export type BaseDownloadEngineFetchStreamOptions = { retry?: retry.Options headers?: Record, /** * If true, parallel download will be enabled even if the server does not return `accept-range` header, this is good when using cross-origin requests */ acceptRangeAlwaysTrue?: boolean + defaultFetchDownloadInfo?: { length: number, acceptRange: boolean } }; export default abstract class BaseDownloadEngineFetchStream { - constructor(public readonly options: Partial = {}) { + constructor(public readonly options: Partial = {}) { } public async fetchBytes(url: string, start: number, end: number, onProgress?: (length: number) => void) { @@ -22,7 +23,7 @@ export default abstract class BaseDownloadEngineFetchStream { protected abstract _fetchBytesWithoutRetry(url: string, start: number, end: number, onProgress?: (length: number) => void): Promise; public async fetchDownloadInfo(url: string): Promise<{ length: number, acceptRange: boolean }> { - return await retry(async () => { + return this.options.defaultFetchDownloadInfo ?? await retry(async () => { return await this._fetchDownloadInfoWithoutRetry(url); }, this.options.retry); } diff --git a/src/index.ts b/src/index.ts index 8ae2ef5..74909bd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import DownloadEngineFile, {DownloadEngineFileOptionsWithDefaults} from "./downl import {downloadFile, downloadSequence} from "./download/node-download.js"; import BaseDownloadEngine from "./download/download-engine/engine/base-download-engine.js"; import BaseDownloadEngineFetchStream, { - DownloadEngineFetchStreamOptions + BaseDownloadEngineFetchStreamOptions } from "./download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.js"; import DownloadEngineFetchStreamFetch from "./download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-fetch.js"; @@ -41,7 +41,7 @@ export type { TransferCliStatus, DownloadEngineOptionsNodejs, DownloadEngineFileOptionsWithDefaults as DownloadEngineFileOptions, - DownloadEngineFetchStreamOptions, + BaseDownloadEngineFetchStreamOptions, TransferProgressWithStatus };