Skip to content

Commit

Permalink
feat: default download info
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Feb 26, 2024
1 parent 6abd0c3 commit 24d6701
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -46,6 +46,6 @@ export type {
DownloadEngineOptionsBrowser,
DownloadEngineWriteStreamBrowserWriter,
DownloadEngineFileOptionsWithDefaults as DownloadEngineFileOptions,
DownloadEngineFetchStreamOptions,
BaseDownloadEngineFetchStreamOptions,
TransferProgressWithStatus
};
5 changes: 4 additions & 1 deletion src/download/download-engine/engine/base-download-engine.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -13,6 +15,7 @@ export type BaseDownloadEngineOptions<FetchStrategy = "xhr" | "fetch" | "localFi
headers?: Record<string, string>;
acceptRangeIsKnown?: boolean;
fetchStrategy?: FetchStrategy;
defaultFetchDownloadInfo?: BaseDownloadEngineFetchStreamOptions["defaultFetchDownloadInfo"];
};

export interface BaseDownloadEngineEvents extends Omit<DownloadEngineFileEvents, "progress"> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import retry from "async-retry";

export type DownloadEngineFetchStreamOptions = {
export type BaseDownloadEngineFetchStreamOptions = {
retry?: retry.Options
headers?: Record<string, string>,
/**
* 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

Check warning on line 7 in src/download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.ts

View workflow job for this annotation

GitHub Actions / test

This line has a length of 153. Maximum allowed is 140
*/
acceptRangeAlwaysTrue?: boolean
defaultFetchDownloadInfo?: { length: number, acceptRange: boolean }
};

export default abstract class BaseDownloadEngineFetchStream {
constructor(public readonly options: Partial<DownloadEngineFetchStreamOptions> = {}) {
constructor(public readonly options: Partial<BaseDownloadEngineFetchStreamOptions> = {}) {
}

public async fetchBytes(url: string, start: number, end: number, onProgress?: (length: number) => void) {
Expand All @@ -22,7 +23,7 @@ export default abstract class BaseDownloadEngineFetchStream {
protected abstract _fetchBytesWithoutRetry(url: string, start: number, end: number, onProgress?: (length: number) => void): Promise<Uint8Array>;

Check warning on line 23 in src/download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.ts

View workflow job for this annotation

GitHub Actions / test

This line has a length of 148. Maximum allowed is 140

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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -41,7 +41,7 @@ export type {
TransferCliStatus,
DownloadEngineOptionsNodejs,
DownloadEngineFileOptionsWithDefaults as DownloadEngineFileOptions,
DownloadEngineFetchStreamOptions,
BaseDownloadEngineFetchStreamOptions,
TransferProgressWithStatus
};

0 comments on commit 24d6701

Please sign in to comment.