Skip to content

Commit 1024941

Browse files
committed
fix(dynamic-length): send the last chunk of dynamic length
1 parent 022ca6a commit 1024941

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export default class DownloadEngineFetchStreamFetch extends BaseDownloadEngineFe
139139
smartSplit.addChunk(chunkInfo.value);
140140
this.state.onProgress?.(smartSplit.savedLength);
141141
}
142+
143+
smartSplit.closeAndSendLeftoversIfLengthIsUnknown();
142144
}
143145

144146
protected _wrapperStreamNotResponding<T>(promise: Promise<T> | T): Promise<T | void> | T | void {

src/download/download-engine/streams/download-engine-fetch-stream/utils/smart-chunk-split.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class SmartChunkSplit {
1414
private readonly _lastChunkSize: number;
1515
private _bytesWriteLocation: number;
1616
private _chunks: Uint8Array[] = [];
17+
private _closed = false;
1718

1819
public constructor(_callback: WriteCallback, _options: SmartChunkSplitOptions) {
1920
this._options = _options;
@@ -36,7 +37,16 @@ export default class SmartChunkSplit {
3637
return this._chunks.reduce((acc, chunk) => acc + chunk.length, 0);
3738
}
3839

40+
closeAndSendLeftoversIfLengthIsUnknown() {
41+
if (this._chunks.length > 0 && this._options.endChunk === Infinity) {
42+
this._callback(this._chunks, this._bytesWriteLocation, this._options.startChunk++);
43+
}
44+
this._closed = true;
45+
}
46+
3947
private _sendChunk() {
48+
if (this._closed) return;
49+
4050
const checkThreshold = () =>
4151
(this._options.endChunk - this._options.startChunk === 1 ?
4252
this._lastChunkSize : this._options.chunkSize);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default async function streamResponse(stream: IStreamResponse, downloadEn
2020
});
2121

2222
stream.on("close", () => {
23-
smartSplit.sendLeftovers();
23+
smartSplit.closeAndSendLeftoversIfLengthIsUnknown();
2424
resolve();
2525
});
2626

test/daynamic-content-length.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ describe("Dynamic content download", async () => {
2828
const downloader = await downloadFile({
2929
url: DYNAMIC_DOWNLOAD_FILE,
3030
directory: ".",
31-
fileName: IPUll_FILE
31+
fileName: IPUll_FILE,
32+
defaultFetchDownloadInfo: {
33+
acceptRange: false,
34+
length: 0
35+
}
3236
});
3337

3438
await downloader.download();
@@ -41,7 +45,11 @@ describe("Dynamic content download", async () => {
4145

4246
test.concurrent("Browser Download", async (context) => {
4347
const downloader = await downloadFileBrowser({
44-
url: DYNAMIC_DOWNLOAD_FILE
48+
url: DYNAMIC_DOWNLOAD_FILE,
49+
defaultFetchDownloadInfo: {
50+
acceptRange: false,
51+
length: 0
52+
}
4553
});
4654

4755
await downloader.download();

0 commit comments

Comments
 (0)