Skip to content

Commit

Permalink
fix: use transformer to notify about progress
Browse files Browse the repository at this point in the history
Closes #1077
maczikasz authored and develar committed Jan 3, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 744998b commit 7fa56bc
Showing 3 changed files with 35 additions and 20 deletions.
19 changes: 10 additions & 9 deletions packages/electron-auto-updater/src/electronHttpExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Socket } from "net"
import { net } from "electron"
import { createWriteStream, ensureDir } from "fs-extra-p"
import { PassThrough } from "stream"
import BluebirdPromise from "bluebird-lst-c"
import * as path from "path"
import { HttpExecutor, DownloadOptions, HttpError, DigestTransform, checkSha2, calculateDownloadProgress, maxRedirects } from "electron-builder-http"
import { HttpExecutor, DownloadOptions, HttpError, DigestTransform, checkSha2, ProgressCallbackTransform, maxRedirects } from "electron-builder-http"
import { safeLoad } from "js-yaml"
import _debug from "debug"
import Debugger = debug.Debugger
@@ -75,24 +76,24 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,
return
}


var transferProgressNotifier = new PassThrough()

if (options.onProgress != null) {
const total = parseInt(String(safeGetHeader(response, "content-length")), 10)
const start = Date.now()
let transferred = 0

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

}
ensureDirPromise
.then(() => {
const fileOut = createWriteStream(destination)
if (options.sha2 == null) {
response.pipe(fileOut)
response.pipe(transferProgressNotifier)
.pipe(fileOut)
}
else {
response
response.pipe(transferProgressNotifier)
.pipe(new DigestTransform(options.sha2))
.pipe(fileOut)
}
16 changes: 16 additions & 0 deletions packages/electron-builder-http/src/httpExecutor.ts
Original file line number Diff line number Diff line change
@@ -64,6 +64,22 @@ export class HttpError extends Error {
}
}

export class ProgressCallbackTransform extends Transform {

private start = Date.now()
private transferred = 0

constructor(private onProgress: any, private total: number) {
super()
}

_transform(chunk: any, encoding: string, callback: Function) {
this.transferred = calculateDownloadProgress(this.total, this.start, this.transferred, chunk, this.onProgress)
callback(null, chunk)
}

}

export class DigestTransform extends Transform {
private readonly digester = createHash("sha256")

20 changes: 9 additions & 11 deletions packages/electron-builder/src/util/nodeHttpExecutor.ts
Original file line number Diff line number Diff line change
@@ -3,10 +3,11 @@ import { IncomingMessage, ClientRequest, Agent } from "http"
import * as https from "https"
import { createWriteStream, ensureDir, readFile } from "fs-extra-p"
import BluebirdPromise from "bluebird-lst-c"
import { PassThrough } from "stream"
import * as path from "path"
import { homedir } from "os"
import { parse as parseIni } from "ini"
import { HttpExecutor, DownloadOptions, HttpError, DigestTransform, checkSha2, calculateDownloadProgress, maxRedirects } from "electron-builder-http"
import { HttpExecutor, DownloadOptions, HttpError, DigestTransform, checkSha2, ProgressCallbackTransform, maxRedirects } from "electron-builder-http"
import { RequestOptions } from "https"
import { safeLoad } from "js-yaml"
import { parse as parseUrl } from "url"
@@ -17,7 +18,7 @@ export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest

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

var transferProgressNotifier = new PassThrough()

if (options.onProgress != null) {
const total = parseInt(response.headers["content-length"], 10)
const start = Date.now()
let transferred = 0

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

response.pause()
}

ensureDirPromise
.then(() => {
const fileOut = createWriteStream(destination)
if (options.sha2 == null) {
response.pipe(fileOut)
response.pipe(transferProgressNotifier)
.pipe(fileOut)
}
else {
response
response.pipe(transferProgressNotifier)
.pipe(new DigestTransform(options.sha2))
.pipe(fileOut)
}

0 comments on commit 7fa56bc

Please sign in to comment.