Skip to content

Commit

Permalink
feat(electron-updated, nsis): cache downloaded update and reuse if va…
Browse files Browse the repository at this point in the history
…lid later
  • Loading branch information
MariaDima authored and develar committed Feb 18, 2018
1 parent 6fd68c8 commit ba4809a
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 159 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"app-builder-bin": "1.3.5",
"archiver": "^2.1.1",
"async-exit-hook": "^2.0.1",
"aws-sdk": "^2.196.0",
"aws-sdk": "^2.197.0",
"bluebird-lst": "^1.0.5",
"chalk": "^2.3.0",
"chromium-pickle-js": "^0.2.0",
Expand All @@ -41,7 +41,7 @@
"electron-download-tf": "4.3.4",
"electron-is-dev": "^0.3.0",
"electron-osx-sign": "0.4.8",
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"gitbook-plugin-footer": "^0.0.6",
"hosted-git-info": "^2.5.0",
"iconv-lite": "^0.4.19",
Expand Down Expand Up @@ -73,7 +73,7 @@
"@types/ejs": "^2.5.0",
"@types/electron-is-dev": "^0.3.0",
"@types/ini": "^1.3.29",
"@types/jest": "^22.1.2",
"@types/jest": "^22.1.3",
"@types/js-yaml": "^3.10.1",
"@types/lodash.isequal": "^4.5.2",
"@types/node-emoji": "^1.8.0",
Expand All @@ -94,7 +94,7 @@
"gitbook-plugin-edit-link": "^2.0.2",
"gitbook-plugin-github": "^2.0.0",
"gitbook-plugin-github-buttons": "^3.0.0",
"globby": "^8.0.0",
"globby": "^8.0.1",
"jest-cli": "^22.3.0",
"jest-junit": "^3.6.0",
"jsdoc-to-markdown": "^4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/builder-util-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"debug": "^3.1.0",
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"bluebird-lst": "^1.0.5",
"sax": "^1.2.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/builder-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"app-builder-bin": "1.3.5",
"temp-file": "^3.1.1",
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"is-ci": "^1.1.0",
"stat-mode": "^0.2.2",
"bluebird-lst": "^1.0.5",
Expand Down
3 changes: 1 addition & 2 deletions packages/builder-util/src/hash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import BluebirdPromise from "bluebird-lst"
import { createHash } from "crypto"
import { createReadStream } from "fs"

export function hashFile(file: string, algorithm: string = "sha512", encoding: "base64" | "hex" = "base64", options?: any) {
return new BluebirdPromise<string>((resolve, reject) => {
return new Promise<string>((resolve, reject) => {
const hash = createHash(algorithm)
hash
.on("error", reject)
Expand Down
2 changes: 1 addition & 1 deletion packages/dmg-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"vendor"
],
"dependencies": {
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"bluebird-lst": "^1.0.5",
"parse-color": "^1.0.0",
"builder-util": "^0.0.0-semantic-release",
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"builder-util": "0.0.0-semantic-release",
"electron-osx-sign": "0.4.8",
"electron-publish": "0.0.0-semantic-release",
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"hosted-git-info": "^2.5.0",
"is-ci": "^1.1.0",
"isbinaryfile": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder-squirrel-windows/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"builder-util": "^0.0.0-semantic-release",
"bluebird-lst": "^1.0.5",
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"archiver": "^2.1.1",
"sanitize-filename": "^1.6.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"chalk": "^2.3.0",
"builder-util-runtime": "0.0.0-semantic-release",
"builder-util": "0.0.0-semantic-release",
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"is-ci": "^1.1.0",
"read-config-file": "3.0.0",
"sanitize-filename": "^1.6.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"out"
],
"dependencies": {
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"mime": "^2.2.0",
"bluebird-lst": "^1.0.5",
"builder-util-runtime": "^0.0.0-semantic-release",
Expand Down
4 changes: 2 additions & 2 deletions packages/electron-publisher-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"out"
],
"dependencies": {
"fs-extra-p": "^4.5.0",
"aws-sdk": "^2.196.0",
"fs-extra-p": "^4.5.2",
"aws-sdk": "^2.197.0",
"mime": "^2.2.0",
"electron-publish": "~0.0.0-semantic-release",
"builder-util": "^0.0.0-semantic-release",
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"lazy-val": "^1.0.3",
"bluebird-lst": "^1.0.5",
"fs-extra-p": "^4.5.0",
"fs-extra-p": "^4.5.2",
"js-yaml": "^3.10.0",
"semver": "^5.5.0",
"source-map-support": "^0.5.3",
Expand Down
78 changes: 36 additions & 42 deletions packages/electron-updater/src/AppImageUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path"
import "source-map-support/register"
import { BaseUpdater } from "./BaseUpdater"
import { FileWithEmbeddedBlockMapDifferentialDownloader } from "./differentialDownloader/FileWithEmbeddedBlockMapDifferentialDownloader"
import { UPDATE_DOWNLOADED, UpdateCheckResult } from "./main"
import { UpdateCheckResult } from "./main"
import { findFile } from "./Provider"

export class AppImageUpdater extends BaseUpdater {
Expand Down Expand Up @@ -45,48 +45,42 @@ export class AppImageUpdater extends BaseUpdater {
sha512: fileInfo.info.sha512,
}

let installerPath = this.downloadedUpdateHelper.getDownloadedFile(updateInfo, fileInfo)
if (installerPath != null) {
return [installerPath]
}

await this.executeDownload(downloadOptions, fileInfo, async (tempDir, destinationFile) => {
installerPath = destinationFile

const oldFile = process.env.APPIMAGE!!
if (oldFile == null) {
throw newError("APPIMAGE env is not defined", "ERR_UPDATER_OLD_FILE_NOT_FOUND")
}

let isDownloadFull = false
try {
await new FileWithEmbeddedBlockMapDifferentialDownloader(fileInfo.info, this.httpExecutor, {
newUrl: fileInfo.url.href,
oldFile,
logger: this._logger,
newFile: installerPath,
useMultipleRangeRequest: provider.useMultipleRangeRequest,
requestHeaders,
})
.download()
}
catch (e) {
this._logger.error(`Cannot download differentially, fallback to full download: ${e.stack || e}`)
// during test (developer machine mac) we must throw error
isDownloadFull = process.platform === "linux"
}

if (isDownloadFull) {
await this.httpExecutor.download(fileInfo.url.href, installerPath, downloadOptions)
}

await chmod(installerPath, 0o755)
return await this.executeDownload({
fileExtension: "AppImage",
downloadOptions,
fileInfo,
updateInfo,
task: async updateFile => {
const oldFile = process.env.APPIMAGE!!
if (oldFile == null) {
throw newError("APPIMAGE env is not defined", "ERR_UPDATER_OLD_FILE_NOT_FOUND")
}

let isDownloadFull = false
try {
await new FileWithEmbeddedBlockMapDifferentialDownloader(fileInfo.info, this.httpExecutor, {
newUrl: fileInfo.url.href,
oldFile,
logger: this._logger,
newFile: updateFile,
useMultipleRangeRequest: provider.useMultipleRangeRequest,
requestHeaders,
})
.download()
}
catch (e) {
this._logger.error(`Cannot download differentially, fallback to full download: ${e.stack || e}`)
// during test (developer machine mac) we must throw error
isDownloadFull = process.platform === "linux"
}

if (isDownloadFull) {
await this.httpExecutor.download(fileInfo.url.href, updateFile, downloadOptions)
}

await chmod(updateFile, 0o755)
},
})

this.downloadedUpdateHelper.setDownloadedFile(installerPath!!, null, updateInfo, fileInfo)
this.addQuitHandler()
this.emit(UPDATE_DOWNLOADED, this.updateInfo)
return [installerPath!!]
}

protected doInstall(installerPath: string, isSilent: boolean, isRunAfter: boolean): boolean {
Expand Down
21 changes: 16 additions & 5 deletions packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ElectronHttpExecutor } from "./electronHttpExecutor"
import { GenericProvider } from "./GenericProvider"
import { Logger, Provider, UpdateCheckResult, UpdaterSignal } from "./main"
import { createClient } from "./providerFactory"
import { DownloadedUpdateHelper } from "./DownloadedUpdateHelper"

export abstract class AppUpdater extends EventEmitter {
/**
Expand Down Expand Up @@ -55,6 +56,8 @@ export abstract class AppUpdater extends EventEmitter {

private _channel: string | null = null

protected readonly downloadedUpdateHelper: DownloadedUpdateHelper

/**
* Get the update channel. Not applicable for GitHub. Doesn't return `channel` from the update configuration, only if was previously set.
*/
Expand Down Expand Up @@ -141,7 +144,7 @@ export abstract class AppUpdater extends EventEmitter {
/** @internal */
readonly httpExecutor: ElectronHttpExecutor

protected constructor(options: AllPublishOptions | null | undefined, app?: any) {
protected constructor(options: AllPublishOptions | null | undefined, app?: Electron.App) {
super()

this.on("error", (error: Error) => {
Expand All @@ -166,6 +169,8 @@ export abstract class AppUpdater extends EventEmitter {
})
}

this.downloadedUpdateHelper = new DownloadedUpdateHelper(this.app.getPath("userData"))

const currentVersionString = this.app.getVersion()
const currentVersion = parseVersion(currentVersionString)
if (currentVersion == null) {
Expand Down Expand Up @@ -276,15 +281,21 @@ export abstract class AppUpdater extends EventEmitter {
return headers
}

private async doCheckForUpdates(): Promise<UpdateCheckResult> {
protected async getUpdateInfo(): Promise<UpdateInfo> {
await this.untilAppReady

if (this.clientPromise == null) {
this.clientPromise = this.configOnDisk.value.then(it => createClient(it, this))
}

const client = await this.clientPromise
const stagingUserId = await this.stagingUserIdPromise.value
client.setRequestHeaders(this.computeFinalHeaders({"X-User-Staging-Id": stagingUserId}))
const updateInfo = await client.getLatestVersion()
return await client.getLatestVersion()
}

private async doCheckForUpdates(): Promise<UpdateCheckResult> {
const updateInfo = await this.getUpdateInfo()

const latestVersion = parseVersion(updateInfo.version)
if (latestVersion == null) {
Expand Down Expand Up @@ -313,7 +324,7 @@ export abstract class AppUpdater extends EventEmitter {
versionInfo: updateInfo,
updateInfo,
cancellationToken,
downloadPromise: this.autoDownload ? this.downloadUpdate(cancellationToken) : null,
downloadPromise: this.autoDownload ? this.downloadUpdate(cancellationToken) : null
}
}

Expand Down Expand Up @@ -430,4 +441,4 @@ export class NoOpLogger implements Logger {
error(message?: any) {
// ignore
}
}
}
Loading

0 comments on commit ba4809a

Please sign in to comment.