Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add request.pause() to notification about progress #1077

Merged
merged 2 commits into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions packages/electron-builder-http/src/httpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
20 changes: 9 additions & 11 deletions packages/electron-builder/src/util/nodeHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down