Skip to content

Commit fc48e3b

Browse files
committed
pref(downloadInfo): optimize requests
1 parent 9826396 commit fc48e3b

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ import {downloadFileBrowser} from "ipull/dist/browser.js";
5656

5757
const downloader = await downloadFileBrowser({
5858
url: 'https://example.com/file.large',
59-
acceptRangeIsKnown: true // cors origin request will not return the range header, but we can force it to be true (multi-connection download)
59+
acceptRangeIsKnown: true, // cors origin request will not return the range header, but we can force it to be true (multi-connection download)
60+
// defaultFetchDownloadInfo: { // if we know the file size and other info, we can set it manually to overcome CORS issues && prevent multiple requests
61+
// acceptRanges: true,
62+
// length: 40789822,
63+
// }
6064
});
6165

6266
await downloader.download();

src/download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-fetch.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,14 @@ export default class DownloadEngineFetchStreamFetch extends BaseDownloadEngineFe
101101
const fileName = parseContentDisposition(response.headers.get("content-disposition"));
102102

103103
let length = parseInt(response.headers.get("content-length")!) || 0;
104-
if (response.headers.get("content-encoding") || browserCheck() && MIN_LENGTH_FOR_MORE_INFO_REQUEST < length) {
105-
length = acceptRange ? await this.fetchDownloadInfoWithoutRetryContentRange(url, method === "GET" ? response : undefined) : 0;
104+
const contentEncoding = response.headers.get("content-encoding");
105+
106+
if (contentEncoding && contentEncoding !== "identity") {
107+
length = 0; // If content is encoded, we cannot determine the length reliably
108+
}
109+
110+
if (acceptRange && length === 0 && browserCheck() && MIN_LENGTH_FOR_MORE_INFO_REQUEST < length) {
111+
length = await this.fetchDownloadInfoWithoutRetryContentRange(url, method === "GET" ? response : undefined);
106112
}
107113

108114
return {

src/download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-xhr.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,22 @@ export default class DownloadEngineFetchStreamXhr extends BaseDownloadEngineFetc
204204

205205
xhr.onload = async () => {
206206
if (xhr.status >= 200 && xhr.status < 300) {
207-
const contentLength = parseInt(xhr.getResponseHeader("content-length")!);
208-
const length = MIN_LENGTH_FOR_MORE_INFO_REQUEST < contentLength ? await this.fetchDownloadInfoWithoutRetryContentRange(url, method === "GET" ? xhr : undefined) : 0;
209207
const fileName = parseContentDisposition(xhr.getResponseHeader("content-disposition"));
210208
const acceptRange = this.options.acceptRangeIsKnown ?? xhr.getResponseHeader("Accept-Ranges") === "bytes";
209+
const contentEncoding = xhr.getResponseHeader("content-encoding");
210+
211+
let length = parseInt(xhr.getResponseHeader("content-length")!) || 0;
212+
if (contentEncoding && contentEncoding !== "identity") {
213+
length = 0; // If content is encoded, we cannot determine the length reliably
214+
}
215+
216+
if (acceptRange && length === 0 && MIN_LENGTH_FOR_MORE_INFO_REQUEST < length) {
217+
length = await this.fetchDownloadInfoWithoutRetryContentRange(url, method === "GET" ? xhr : undefined);
218+
}
211219

212220
resolve({
213-
length,
214221
acceptRange,
222+
length,
215223
newURL: xhr.responseURL,
216224
fileName
217225
});

0 commit comments

Comments
 (0)