@@ -2,6 +2,7 @@ import { newError } from "builder-util-runtime"
22import { createReadStream } from "fs"
33import { Writable } from "stream"
44import { Operation , OperationKind } from "./downloadPlanBuilder"
5+ import { ProgressInfo } from "./ProgressDifferentialDownloadCallbackTransform"
56
67const DOUBLE_CRLF = Buffer . from ( "\r\n\r\n" )
78
@@ -34,6 +35,11 @@ export function copyData(task: Operation, out: Writable, oldFileFd: number, reje
3435}
3536
3637export class DataSplitter extends Writable {
38+ private start = Date . now ( )
39+ private nextUpdate = this . start + 1000
40+ private transferred = 0
41+ private delta = 0
42+
3743 partIndex = - 1
3844
3945 private headerListBuffer : Buffer | null = null
@@ -49,7 +55,9 @@ export class DataSplitter extends Writable {
4955 private readonly partIndexToTaskIndex : Map < number , number > ,
5056 boundary : string ,
5157 private readonly partIndexToLength : Array < number > ,
52- private readonly finishHandler : ( ) => any
58+ private readonly finishHandler : ( ) => any ,
59+ private readonly grandTotalBytes : number ,
60+ private readonly onProgress ?: ( info : ProgressInfo ) => any
5361 ) {
5462 super ( )
5563
@@ -69,7 +77,27 @@ export class DataSplitter extends Writable {
6977 return
7078 }
7179
72- this . handleData ( data ) . then ( callback ) . catch ( callback )
80+ this . handleData ( data )
81+ . then ( ( ) => {
82+ if ( this . onProgress ) {
83+ const now = Date . now ( )
84+ if ( now >= this . nextUpdate || this . transferred === this . grandTotalBytes ) {
85+ this . nextUpdate = now + 1000
86+
87+ this . onProgress ( {
88+ total : this . grandTotalBytes ,
89+ delta : this . delta ,
90+ transferred : this . transferred ,
91+ percent : ( this . transferred / this . grandTotalBytes ) * 100 ,
92+ bytesPerSecond : Math . round ( this . transferred / ( ( now - this . start ) / 1000 ) ) ,
93+ } )
94+ this . delta = 0
95+ }
96+ }
97+
98+ callback ( )
99+ } )
100+ . catch ( callback )
73101 }
74102
75103 private async handleData ( chunk : Buffer ) : Promise < undefined > {
@@ -217,6 +245,8 @@ export class DataSplitter extends Writable {
217245
218246 private processPartData ( data : Buffer , start : number , end : number ) : Promise < void > {
219247 this . actualPartLength += end - start
248+ this . transferred += end - start
249+ this . delta += end - start
220250 const out = this . out
221251 if ( out . write ( start === 0 && data . length === end ? data : data . slice ( start , end ) ) ) {
222252 return Promise . resolve ( )
0 commit comments