Skip to content

Commit

Permalink
fix: cached update validation failing on undefined filename (#4929)
Browse files Browse the repository at this point in the history
* fix: cached update validation failing on undefined filename

Closes #4928

This change is to remove the use of `==` in favour of `===` (i.e. remove all uses of abstract equality operator in favour of the strict equality operator)

* fix: handle case where cachedInfo object is undefined

* fix: early return if update file does not exist
  • Loading branch information
b-zurg authored May 14, 2020
1 parent dfcd010 commit 4a9e5ef
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/electron-updater/src/DownloadedUpdateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createHash } from "crypto"
import { createReadStream } from "fs"
import isEqual from "lodash.isequal"
import { Logger, ResolvedUpdateFileInfo } from "./main"
import { pathExists, readJson, emptyDir, outputJson, unlink } from "fs-extra"
import { pathExists, readJson, emptyDir, outputJson, unlink, pathExistsSync } from "fs-extra"
import * as path from "path"

/** @private **/
Expand Down Expand Up @@ -48,7 +48,7 @@ export class DownloadedUpdateHelper {

// update has already been downloaded from some previous app launch
const cachedUpdateFile = await this.getValidCachedUpdateFile(fileInfo, logger)
if (cachedUpdateFile == null) {
if (cachedUpdateFile === null) {
return null
}
logger.info(`Update has already been downloaded to ${updateFile}).`)
Expand Down Expand Up @@ -90,23 +90,36 @@ export class DownloadedUpdateHelper {
}
}

/**
* Returns "update-info.json" which is created in the update cache directory's "pending" subfolder after the first update is downloaded. If the update file does not exist then the cache is cleared and recreated. If the update file exists then its properties are validated.
* @param fileInfo
* @param logger
*/
private async getValidCachedUpdateFile(fileInfo: ResolvedUpdateFileInfo, logger: Logger): Promise<string | null> {
const updateInfoFilePath: string = this.getUpdateInfoFile()

const doesUpdateInfoFileExist = await pathExistsSync(updateInfoFilePath);
if(!doesUpdateInfoFileExist) {
return null;
}


let cachedInfo: CachedUpdateInfo
const updateInfoFile = this.getUpdateInfoFile()
try {
cachedInfo = await readJson(updateInfoFile)
cachedInfo = await readJson(updateInfoFilePath)
}
catch (e) {
catch (error) {
let message = `No cached update info available`
if (e.code !== "ENOENT") {
if (error.code !== "ENOENT") {
await this.cleanCacheDirForPendingUpdate()
message += ` (error on read: ${e.message})`
message += ` (error on read: ${error.message})`
}
logger.info(message)
return null
}

if (cachedInfo.fileName == null) {
const isCachedInfoFileNameValid = cachedInfo?.fileName !== null ?? false
if (!isCachedInfoFileNameValid) {
logger.warn(`Cached update info is corrupted: no fileName, directory for cached update will be cleaned`)
await this.cleanCacheDirForPendingUpdate()
return null
Expand Down Expand Up @@ -182,4 +195,4 @@ export async function createTempUpdateFile(name: string, cacheDir: string, log:
}
}
return result
}
}

0 comments on commit 4a9e5ef

Please sign in to comment.