Skip to content

Commit

Permalink
refactor: extract configurePipes
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Jan 3, 2017
1 parent 101eeeb commit 84f014c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .idea/electron-builder.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 3 additions & 30 deletions packages/electron-auto-updater/src/electronHttpExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { Socket } from "net"
import { net } from "electron"
import { createWriteStream, ensureDir } from "fs-extra-p"
import { PassThrough } from "stream"
import { ensureDir } from "fs-extra-p"
import BluebirdPromise from "bluebird-lst-c"
import * as path from "path"
import { HttpExecutor, DownloadOptions, HttpError, DigestTransform, checkSha2, ProgressCallbackTransform, maxRedirects } from "electron-builder-http"
import { HttpExecutor, DownloadOptions, HttpError, checkSha2, maxRedirects, safeGetHeader, configurePipes } from "electron-builder-http"
import { safeLoad } from "js-yaml"
import _debug from "debug"
import Debugger = debug.Debugger
import { parse as parseUrl } from "url"

function safeGetHeader(response: Electron.IncomingMessage, headerKey: string) {
return response.headers[headerKey] ? response.headers[headerKey].pop() : null
}

export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions, Electron.ClientRequest> {
private readonly debug: Debugger = _debug("electron-builder")

Expand Down Expand Up @@ -76,30 +71,8 @@ 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)

transferProgressNotifier = new ProgressCallbackTransform(options.onProgress, total)

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

fileOut.on("finish", () => (<any>fileOut.close)(callback))
})
.then(() => configurePipes(options, response, destination, callback))
.catch(callback)
})
this.addTimeOutHandler(request, callback)
Expand Down
3 changes: 3 additions & 0 deletions packages/electron-builder-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"files": [
"out"
],
"dependencies": {
"fs-extra-p": "^3.0.3"
},
"typings": "./out/electron-builder-http.d.ts"
}
45 changes: 41 additions & 4 deletions packages/electron-builder-http/src/httpExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Url } from "url"
import { createHash } from "crypto"
import { Transform } from "stream"
import { createWriteStream } from "fs-extra-p"

export interface DownloadOptions {
skipDirCreation?: boolean
Expand Down Expand Up @@ -64,8 +65,7 @@ export class HttpError extends Error {
}
}

export class ProgressCallbackTransform extends Transform {

class ProgressCallbackTransform extends Transform {
private start = Date.now()
private transferred = 0

Expand All @@ -77,10 +77,9 @@ export class ProgressCallbackTransform extends Transform {
this.transferred = calculateDownloadProgress(this.total, this.start, this.transferred, chunk, this.onProgress)
callback(null, chunk)
}

}

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

constructor(private expected: string) {
Expand Down Expand Up @@ -132,4 +131,42 @@ export function calculateDownloadProgress(total: number, start: number, transfer
bytesPerSecond: Math.round(transferred / ((Date.now() - start) / 1000))
})
return transferred
}

export function safeGetHeader(response: any, headerKey: string) {
const value = response.headers[headerKey]
if (value == null) {
return null
}
else if (Array.isArray(value)) {
// electron API
return value.length === 0 ? null : value[value.length - 1]
}
else {
return value
}
}

export function configurePipes(options: DownloadOptions, response: any, destination: string, callback: (error: Error | null) => void) {
const streams: Array<any> = []
if (options.onProgress != null) {
const contentLength = safeGetHeader(response, "content-length")
if (contentLength != null) {
streams.push(new ProgressCallbackTransform(options.onProgress, parseInt(contentLength, 10)))
}
}

if (options.sha2 != null) {
streams.push(new DigestTransform(options.sha2))
}

const fileOut = createWriteStream(destination)
streams.push(fileOut)

let lastStream = response
for (const stream of streams) {
lastStream = lastStream.pipe(stream)
}

fileOut.on("finish", () => (<any>fileOut.close)(callback))
}
3 changes: 2 additions & 1 deletion packages/electron-builder-http/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"src/**/*.ts"
],
"files": [
"../../node_modules/@types/node/index.d.ts"
"../../node_modules/@types/node/index.d.ts",
"node_modules/fs-extra-p/index.d.ts"
]
}
30 changes: 4 additions & 26 deletions packages/electron-builder/src/util/nodeHttpExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Socket } from "net"
import { IncomingMessage, ClientRequest, Agent } from "http"
import * as https from "https"
import { createWriteStream, ensureDir, readFile } from "fs-extra-p"
import { 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, ProgressCallbackTransform, maxRedirects } from "electron-builder-http"
import { HttpExecutor, DownloadOptions, HttpError, configurePipes, checkSha2, maxRedirects } from "electron-builder-http"
import { RequestOptions } from "https"
import { safeLoad } from "js-yaml"
import { parse as parseUrl } from "url"
Expand Down Expand Up @@ -72,30 +71,9 @@ export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest
return
}

var transferProgressNotifier = new PassThrough()

if (options.onProgress != null) {
const total = parseInt(response.headers["content-length"], 10)

transferProgressNotifier = new ProgressCallbackTransform(options.onProgress, total)

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

fileOut.on("finish", () => (<any>fileOut.close)(callback))
})
.catch(callback)
.then(() => configurePipes(options, response, destination, callback))
.catch(callback)
})
this.addTimeOutHandler(request, callback)
request.on("error", callback)
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.5.29.tgz#29f4dd9314fbccb080d8bd84b9c23811ec5090c2"

"@types/node@*":
version "6.0.55"
resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.55.tgz#e5cb679a43561f42afd1bd6d58d3992ec8f31720"
version "6.0.56"
resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.56.tgz#12bc7fff825e72807f55dcbce17e9db6177713dd"

"@types/source-map-support@^0.2.28":
version "0.2.28"
Expand Down

0 comments on commit 84f014c

Please sign in to comment.