Skip to content

feat(updater): Cache the new blockmap file and allow customization of the old blockmap file base URL. #9172

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

Merged
merged 12 commits into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
53 changes: 48 additions & 5 deletions packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { randomBytes } from "crypto"
import { release } from "os"
import { EventEmitter } from "events"
import { mkdir, outputFile, readFile, rename, unlink } from "fs-extra"
import { mkdir, outputFile, readFile, rename, unlink, copyFile, pathExists } from "fs-extra"
import { OutgoingHttpHeaders } from "http"
import { load } from "js-yaml"
import { Lazy } from "lazy-val"
Expand All @@ -31,7 +31,7 @@ import { Provider, ProviderPlatform } from "./providers/Provider"
import type { TypedEmitter } from "tiny-typed-emitter"
import Session = Electron.Session
import type { AuthInfo } from "electron"
import { gunzipSync } from "zlib"
import { gunzipSync, gzipSync } from "zlib"
import { blockmapFiles } from "./util"
import { DifferentialDownloaderOptions } from "./differentialDownloader/DifferentialDownloader"
import { GenericDifferentialDownloader } from "./differentialDownloader/GenericDifferentialDownloader"
Expand Down Expand Up @@ -117,6 +117,12 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
*/
forceDevUpdateConfig = false

/**
* The base URL of the old block map file.
* @default null
*/
private _oldBlockMapFileBaseUrl: string | null = null

/**
* The current application version.
*/
Expand All @@ -133,6 +139,14 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
return this._channel
}

get oldBlockMapFileBaseUrl(): string | null {
return this._oldBlockMapFileBaseUrl
}

set oldBlockMapFileBaseUrl(value: string | null) {
this._oldBlockMapFileBaseUrl = value
}

/**
* Set the update channel. Overrides `channel` in the update configuration.
*
Expand Down Expand Up @@ -736,6 +750,10 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
...updateInfo,
downloadedFile: updateFile,
})
const currentBlockMapFile = path.join(cacheDir, "current.blockmap")
if (await pathExists(currentBlockMapFile)) {
await copyFile(currentBlockMapFile, path.join(downloadedUpdateHelper.cacheDir, "current.blockmap"))
}
return packageFile == null ? [updateFile] : [updateFile, packageFile]
}

Expand Down Expand Up @@ -790,7 +808,7 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
if (this._testOnlyOptions != null && !this._testOnlyOptions.isUseDifferentialDownload) {
return true
}
const blockmapFileUrls = blockmapFiles(fileInfo.url, this.app.version, downloadUpdateOptions.updateInfoAndProvider.info.version)
const blockmapFileUrls = blockmapFiles(fileInfo.url, this.app.version, downloadUpdateOptions.updateInfoAndProvider.info.version, this._oldBlockMapFileBaseUrl)
this._logger.info(`Download block maps (old: "${blockmapFileUrls[0]}", new: ${blockmapFileUrls[1]})`)

const downloadBlockMap = async (url: URL): Promise<BlockMap> => {
Expand Down Expand Up @@ -824,8 +842,33 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
downloadOptions.onProgress = it => this.emit(DOWNLOAD_PROGRESS, it)
}

const blockMapDataList = await Promise.all(blockmapFileUrls.map(u => downloadBlockMap(u)))
await new GenericDifferentialDownloader(fileInfo.info, this.httpExecutor, downloadOptions).download(blockMapDataList[0], blockMapDataList[1])
const saveBlockMapToCacheDir = async (blockMapData: BlockMap, cacheDir: string) => {
const blockMapFile = path.join(cacheDir, "current.blockmap")
await outputFile(blockMapFile, gzipSync(JSON.stringify(blockMapData)))
}

const getBlockMapFromCacheDir = async (cacheDir: string) => {
const blockMapFile = path.join(cacheDir, "current.blockmap")
try {
if (await pathExists(blockMapFile)) {
return JSON.parse(gunzipSync(await readFile(blockMapFile)).toString())
}
} catch (e: any) {
this._logger.warn(`Cannot parse blockmap "${blockMapFile}", error: ${e}`)
}
return null
}

const newBlockMapData = await downloadBlockMap(blockmapFileUrls[1])
await saveBlockMapToCacheDir(newBlockMapData, this.downloadedUpdateHelper!.cacheDirForPendingUpdate)

// get old blockmap from cache dir first, if not found, download it
let oldBlockMapData = await getBlockMapFromCacheDir(this.downloadedUpdateHelper!.cacheDir)
if (oldBlockMapData == null) {
oldBlockMapData = await downloadBlockMap(blockmapFileUrls[0])
}

await new GenericDifferentialDownloader(fileInfo.info, this.httpExecutor, downloadOptions).download(oldBlockMapData, newBlockMapData)
return false
} catch (e: any) {
this._logger.error(`Cannot download differentially, fallback to full download: ${e.stack || e}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function doExecuteTasks(differentialDownloader: DifferentialDownloader, options:
for (let i = options.start; i < options.end; i++) {
const task = options.tasks[i]
if (task.kind === OperationKind.DOWNLOAD) {
ranges += `${task.start}-${task.end - 1}, `
ranges += `${task.start}-${task.end - 1},`
partIndexToTaskIndex.set(partCount, i)
partCount++
partIndexToLength.push(task.end - task.start)
Expand Down
8 changes: 6 additions & 2 deletions packages/electron-updater/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ export function getChannelFilename(channel: string): string {
return `${channel}.yml`
}

export function blockmapFiles(baseUrl: URL, oldVersion: string, newVersion: string): URL[] {
export function blockmapFiles(baseUrl: URL, oldVersion: string, newVersion: string, oldBlockMapFileBaseUrl: string | null = null): URL[] {
const newBlockMapUrl = newUrlFromBase(`${baseUrl.pathname}.blockmap`, baseUrl)
const oldBlockMapUrl = newUrlFromBase(`${baseUrl.pathname.replace(new RegExp(escapeRegExp(newVersion), "g"), oldVersion)}.blockmap`, baseUrl)
const oldBlockMapUrl = newUrlFromBase(
`${baseUrl.pathname.replace(new RegExp(escapeRegExp(newVersion), "g"), oldVersion)}.blockmap`,
oldBlockMapFileBaseUrl ? new URL(oldBlockMapFileBaseUrl) : baseUrl
)

return [oldBlockMapUrl, newBlockMapUrl]
}
6 changes: 3 additions & 3 deletions test/src/binDownloadTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test("download binary from Mirror with custom dir", async ({ expect }) => {
"linux-tools-mac-10.12.3.7z",
"SQ8fqIRVXuQVWnVgaMTDWyf2TLAJjJYw3tRSqQJECmgF6qdM7Kogfa6KD49RbGzzMYIFca9Uw3MdsxzOPRWcYw=="
)
delete process.env.ELECTRON_BUILDER_BINARIES_MIRROR
delete process.env.ELECTRON_BUILDER_BINARIES_MIRROR
delete process.env.ELECTRON_BUILDER_BINARIES_CUSTOM_DIR
expect(bin).toBeTruthy()
})
Expand All @@ -29,7 +29,7 @@ test("download binary from Mirror", async ({ expect }) => {
"linux-tools-mac-10.12.3.7z",
"SQ8fqIRVXuQVWnVgaMTDWyf2TLAJjJYw3tRSqQJECmgF6qdM7Kogfa6KD49RbGzzMYIFca9Uw3MdsxzOPRWcYw=="
)
delete process.env.ELECTRON_BUILDER_BINARIES_MIRROR
delete process.env.ELECTRON_BUILDER_BINARIES_MIRROR
expect(bin).toBeTruthy()
})

Expand All @@ -40,6 +40,6 @@ test("download binary from Mirror with Url override", async ({ expect }) => {
"linux-tools-mac-10.12.3.7z",
"SQ8fqIRVXuQVWnVgaMTDWyf2TLAJjJYw3tRSqQJECmgF6qdM7Kogfa6KD49RbGzzMYIFca9Uw3MdsxzOPRWcYw=="
)
delete process.env.ELECTRON_BUILDER_BINARIES_DOWNLOAD_OVERRIDE_URL
delete process.env.ELECTRON_BUILDER_BINARIES_DOWNLOAD_OVERRIDE_URL
expect(bin).toBeTruthy()
})
Loading