Skip to content

Commit 80a1186

Browse files
committed
feat: update write methods to include totalLength parameter for improved chunk processing
1 parent d5771d4 commit 80a1186

8 files changed

Lines changed: 15 additions & 16 deletions

File tree

src/download/download-engine/download-file/download-engine-file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,12 @@ export default class DownloadEngineFile extends EventEmitter<DownloadEngineFileE
359359
const allWrites = new Set<Promise<any>>();
360360

361361
let lastChunkSize = 0, lastInProgressIndex = startChunk;
362-
await fetchState.fetchChunks((chunks, writePosition, index) => {
362+
await fetchState.fetchChunks((chunks, writePosition, index, totalLength) => {
363363
if (this._closed || this._progress.chunks[index] != ChunkStatus.IN_PROGRESS) {
364364
return;
365365
}
366366

367-
const writePromise = this.options.writeStream.write(downloadedPartsSize + writePosition, chunks);
367+
const writePromise = this.options.writeStream.write(downloadedPartsSize + writePosition, chunks, totalLength);
368368
if (writePromise) {
369369
allWrites.add(writePromise);
370370
writePromise.then(() => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export type BaseDownloadEngineFetchStreamEvents = {
9696
streamNotRespondingOff: () => void;
9797
};
9898

99-
export type WriteCallback = (data: Uint8Array[], position: number, index: number) => void;
99+
export type WriteCallback = (data: Uint8Array[], position: number, index: number, totalLength: number) => void;
100100

101101
const DEFAULT_OPTIONS: BaseDownloadEngineFetchStreamOptions = {
102102
retryOnServerError: true,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export default class DownloadEngineFetchStreamXhr extends BaseDownloadEngineFetc
192192
if (this.aborted) return;
193193

194194
const chunk = await this.fetchBytes(this.state.activePart.downloadURL, this._startSize, this._endSize, this.state.onProgress);
195-
callback([chunk], this._startSize, this.state.startChunk++);
195+
callback([chunk], this._startSize, this.state.startChunk++, chunk.length);
196196
}
197197
}
198198

@@ -213,7 +213,7 @@ export default class DownloadEngineFetchStreamXhr extends BaseDownloadEngineFetc
213213

214214
const chunk = relevantContent.slice(start, end);
215215
totalReceivedLength += chunk.byteLength;
216-
callback([chunk], index * this.state.chunkSize, index++);
216+
callback([chunk], index * this.state.chunkSize, index++, chunk.length);
217217
}
218218
}
219219

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class SmartChunkSplit {
4343

4444
closeAndSendLeftoversIfLengthIsUnknown() {
4545
if (this._chunks.length > 0 && this._options.endChunk === Infinity) {
46-
this._callback(this._chunks, this._bytesWriteLocation, this._options.startChunk++);
46+
this._callback(this._chunks, this._bytesWriteLocation, this._options.startChunk++, this._savedLength);
4747
this._chunks = [];
4848
this._savedLength = 0;
4949
}
@@ -78,7 +78,7 @@ export default class SmartChunkSplit {
7878
}
7979

8080
this._savedLength -= calcChunkThreshold;
81-
this._callback(sendChunks, this._bytesWriteLocation, this._options.startChunk++);
81+
this._callback(sendChunks, this._bytesWriteLocation, this._options.startChunk++, calcChunkThreshold);
8282
this._bytesWriteLocation += calcChunkThreshold;
8383
break;
8484
}

src/download/download-engine/streams/download-engine-write-stream/base-download-engine-write-stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default abstract class BaseDownloadEngineWriteStream {
2-
abstract write(cursor: number, buffers: Uint8Array[]): Promise<void> | void;
2+
abstract write(cursor: number, buffers: Uint8Array[], totalLength: number): Promise<void> | void;
33

44
close(): void | Promise<void> {
55
}

src/download/download-engine/streams/download-engine-write-stream/download-engine-write-stream-browser.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type DownloadEngineWriteStreamOptionsBrowser = {
99
file?: DownloadFile
1010
};
1111

12-
export type DownloadEngineWriteStreamBrowserWriter = (cursor: number, buffers: Uint8Array[], options: DownloadEngineWriteStreamOptionsBrowser) => Promise<void> | void;
12+
export type DownloadEngineWriteStreamBrowserWriter = (cursor: number, buffers: Uint8Array[], options: DownloadEngineWriteStreamOptionsBrowser, totalLength: number) => Promise<void> | void;
1313

1414
export default class DownloadEngineWriteStreamBrowser extends BaseDownloadEngineWriteStream {
1515
protected readonly _writer?: DownloadEngineWriteStreamBrowserWriter;
@@ -44,13 +44,12 @@ export default class DownloadEngineWriteStreamBrowser extends BaseDownloadEngine
4444
return this._memory = newMemory;
4545
}
4646

47-
public write(cursor: number, buffers: Uint8Array[]) {
47+
public write(cursor: number, buffers: Uint8Array[], totalLength: number) {
4848
if (this.writerClosed) {
4949
throw new WriterIsClosedError();
5050
}
5151

5252
if (!this._writer) {
53-
const totalLength = buffers.reduce((sum, buffer) => sum + buffer.length, 0);
5453
const bigBuffer = this._ensureBuffer(cursor + totalLength);
5554
let writeLocation = cursor;
5655

@@ -63,7 +62,7 @@ export default class DownloadEngineWriteStreamBrowser extends BaseDownloadEngine
6362
return;
6463
}
6564

66-
return this._writer(cursor, buffers, this.options);
65+
return this._writer(cursor, buffers, this.options, totalLength);
6766
}
6867

6968
public get result() {

src/download/download-engine/streams/download-engine-write-stream/download-engine-write-stream-nodejs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export default class DownloadEngineWriteStreamNodejs extends BaseDownloadEngineW
101101
* Fragments are concatenated into a single Buffer, contiguous regions merged,
102102
* and auto-flushed when the adaptive threshold is exceeded.
103103
*/
104-
write(cursor: number, buffers: Uint8Array[]) {
105-
return this._writeQueue.addWrite(cursor, buffers);
104+
write(cursor: number, buffers: Uint8Array[], totalLength: number) {
105+
return this._writeQueue.addWrite(cursor, buffers, totalLength);
106106
}
107107

108108
ensureBytesSynced() {

src/download/download-engine/streams/download-engine-write-stream/utils/WriteQueue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export default class WriteQueue {
5757
* Buffer a write. Concatenates fragments into a single Buffer,
5858
* merges with adjacent regions, and flushes when threshold is exceeded.
5959
*/
60-
addWrite(cursor: number, buffers: Uint8Array[]): void | Promise<void> {
60+
addWrite(cursor: number, buffers: Uint8Array[], totalLength: number): void | Promise<void> {
6161
if (this._closed) {
6262
throw new WriterIsClosedError("Cannot add write to closed WriteQueue");
6363
}
6464

65-
const length = buffers.reduce((sum, buf) => sum + buf.length, 0);
65+
const length = totalLength;
6666

6767
const merged = this._tryMerge(cursor, buffers, length);
6868
if (!merged) {

0 commit comments

Comments
 (0)