Skip to content

Commit 7fa56bc

Browse files
maczikaszdevelar
authored andcommitted
fix: use transformer to notify about progress
Closes #1077
1 parent 744998b commit 7fa56bc

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

packages/electron-auto-updater/src/electronHttpExecutor.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Socket } from "net"
22
import { net } from "electron"
33
import { createWriteStream, ensureDir } from "fs-extra-p"
4+
import { PassThrough } from "stream"
45
import BluebirdPromise from "bluebird-lst-c"
56
import * as path from "path"
6-
import { HttpExecutor, DownloadOptions, HttpError, DigestTransform, checkSha2, calculateDownloadProgress, maxRedirects } from "electron-builder-http"
7+
import { HttpExecutor, DownloadOptions, HttpError, DigestTransform, checkSha2, ProgressCallbackTransform, maxRedirects } from "electron-builder-http"
78
import { safeLoad } from "js-yaml"
89
import _debug from "debug"
910
import Debugger = debug.Debugger
@@ -75,24 +76,24 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,
7576
return
7677
}
7778

79+
80+
var transferProgressNotifier = new PassThrough()
81+
7882
if (options.onProgress != null) {
7983
const total = parseInt(String(safeGetHeader(response, "content-length")), 10)
80-
const start = Date.now()
81-
let transferred = 0
8284

83-
response.on("data", (chunk: any) => {
84-
transferred = calculateDownloadProgress(total, start, transferred, chunk, options.onProgress)
85-
})
86-
}
85+
transferProgressNotifier = new ProgressCallbackTransform(options.onProgress, total)
8786

87+
}
8888
ensureDirPromise
8989
.then(() => {
9090
const fileOut = createWriteStream(destination)
9191
if (options.sha2 == null) {
92-
response.pipe(fileOut)
92+
response.pipe(transferProgressNotifier)
93+
.pipe(fileOut)
9394
}
9495
else {
95-
response
96+
response.pipe(transferProgressNotifier)
9697
.pipe(new DigestTransform(options.sha2))
9798
.pipe(fileOut)
9899
}

packages/electron-builder-http/src/httpExecutor.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ export class HttpError extends Error {
6464
}
6565
}
6666

67+
export class ProgressCallbackTransform extends Transform {
68+
69+
private start = Date.now()
70+
private transferred = 0
71+
72+
constructor(private onProgress: any, private total: number) {
73+
super()
74+
}
75+
76+
_transform(chunk: any, encoding: string, callback: Function) {
77+
this.transferred = calculateDownloadProgress(this.total, this.start, this.transferred, chunk, this.onProgress)
78+
callback(null, chunk)
79+
}
80+
81+
}
82+
6783
export class DigestTransform extends Transform {
6884
private readonly digester = createHash("sha256")
6985

packages/electron-builder/src/util/nodeHttpExecutor.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { IncomingMessage, ClientRequest, Agent } from "http"
33
import * as https from "https"
44
import { createWriteStream, ensureDir, readFile } from "fs-extra-p"
55
import BluebirdPromise from "bluebird-lst-c"
6+
import { PassThrough } from "stream"
67
import * as path from "path"
78
import { homedir } from "os"
89
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"
1011
import { RequestOptions } from "https"
1112
import { safeLoad } from "js-yaml"
1213
import { parse as parseUrl } from "url"
@@ -17,7 +18,7 @@ export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest
1718

1819
download(url: string, destination: string, options?: DownloadOptions | null): Promise<string> {
1920
return <BluebirdPromise<string>>(this.httpsAgent || (this.httpsAgent = createAgent()))
20-
.then(it => new BluebirdPromise( (resolve, reject) => {
21+
.then(it => new BluebirdPromise((resolve, reject) => {
2122
this.doDownload(url, destination, 0, options || {}, it, (error: Error) => {
2223
if (error == null) {
2324
resolve(destination)
@@ -71,26 +72,23 @@ export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest
7172
return
7273
}
7374

75+
var transferProgressNotifier = new PassThrough()
76+
7477
if (options.onProgress != null) {
7578
const total = parseInt(response.headers["content-length"], 10)
76-
const start = Date.now()
77-
let transferred = 0
7879

79-
response.on("data", (chunk: any) => {
80-
transferred = calculateDownloadProgress(total, start, transferred, chunk, options.onProgress)
81-
})
80+
transferProgressNotifier = new ProgressCallbackTransform(options.onProgress, total)
8281

83-
response.pause()
8482
}
85-
8683
ensureDirPromise
8784
.then(() => {
8885
const fileOut = createWriteStream(destination)
8986
if (options.sha2 == null) {
90-
response.pipe(fileOut)
87+
response.pipe(transferProgressNotifier)
88+
.pipe(fileOut)
9189
}
9290
else {
93-
response
91+
response.pipe(transferProgressNotifier)
9492
.pipe(new DigestTransform(options.sha2))
9593
.pipe(fileOut)
9694
}

0 commit comments

Comments
 (0)