@@ -3,10 +3,11 @@ import { IncomingMessage, ClientRequest, Agent } from "http"
3
3
import * as https from "https"
4
4
import { createWriteStream , ensureDir , readFile } from "fs-extra-p"
5
5
import BluebirdPromise from "bluebird-lst-c"
6
+ import { PassThrough } from "stream"
6
7
import * as path from "path"
7
8
import { homedir } from "os"
8
9
import { parse as parseIni } from "ini"
9
- import { HttpExecutor , DownloadOptions , HttpError , DigestTransform , checkSha2 , calculateDownloadProgress , maxRedirects } from "electron-builder-http"
10
+ import { HttpExecutor , DownloadOptions , HttpError , DigestTransform , checkSha2 , ProgressCallbackTransform , maxRedirects } from "electron-builder-http"
10
11
import { RequestOptions } from "https"
11
12
import { safeLoad } from "js-yaml"
12
13
import { parse as parseUrl } from "url"
@@ -17,7 +18,7 @@ export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest
17
18
18
19
download ( url : string , destination : string , options ?: DownloadOptions | null ) : Promise < string > {
19
20
return < BluebirdPromise < string > > ( this . httpsAgent || ( this . httpsAgent = createAgent ( ) ) )
20
- . then ( it => new BluebirdPromise ( ( resolve , reject ) => {
21
+ . then ( it => new BluebirdPromise ( ( resolve , reject ) => {
21
22
this . doDownload ( url , destination , 0 , options || { } , it , ( error : Error ) => {
22
23
if ( error == null ) {
23
24
resolve ( destination )
@@ -71,26 +72,23 @@ export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest
71
72
return
72
73
}
73
74
75
+ var transferProgressNotifier = new PassThrough ( )
76
+
74
77
if ( options . onProgress != null ) {
75
78
const total = parseInt ( response . headers [ "content-length" ] , 10 )
76
- const start = Date . now ( )
77
- let transferred = 0
78
79
79
- response . on ( "data" , ( chunk : any ) => {
80
- transferred = calculateDownloadProgress ( total , start , transferred , chunk , options . onProgress )
81
- } )
80
+ transferProgressNotifier = new ProgressCallbackTransform ( options . onProgress , total )
82
81
83
- response . pause ( )
84
82
}
85
-
86
83
ensureDirPromise
87
84
. then ( ( ) => {
88
85
const fileOut = createWriteStream ( destination )
89
86
if ( options . sha2 == null ) {
90
- response . pipe ( fileOut )
87
+ response . pipe ( transferProgressNotifier )
88
+ . pipe ( fileOut )
91
89
}
92
90
else {
93
- response
91
+ response . pipe ( transferProgressNotifier )
94
92
. pipe ( new DigestTransform ( options . sha2 ) )
95
93
. pipe ( fileOut )
96
94
}
0 commit comments