Skip to content

Commit e841f89

Browse files
authored
Revert "feat: add support for differential zip updates on macOS (#7709)"
This reverts commit 79df542.
1 parent cb335ec commit e841f89

File tree

5 files changed

+112
-680
lines changed

5 files changed

+112
-680
lines changed

packages/electron-updater/src/AppUpdater.ts

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
DownloadOptions,
1010
CancellationError,
1111
ProgressInfo,
12-
BlockMap,
1312
} from "builder-util-runtime"
1413
import { randomBytes } from "crypto"
1514
import { EventEmitter } from "events"
@@ -30,10 +29,6 @@ import { ProviderPlatform } from "./providers/Provider"
3029
import type { TypedEmitter } from "tiny-typed-emitter"
3130
import Session = Electron.Session
3231
import { AuthInfo } from "electron"
33-
import { gunzipSync } from "zlib"
34-
import { blockmapFiles } from "./util"
35-
import { DifferentialDownloaderOptions } from "./differentialDownloader/DifferentialDownloader"
36-
import { GenericDifferentialDownloader } from "./differentialDownloader/GenericDifferentialDownloader"
3732

3833
export type AppUpdaterEvents = {
3934
error: (error: Error, message?: string) => void
@@ -702,63 +697,6 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
702697
log.info(`New version ${version} has been downloaded to ${updateFile}`)
703698
return await done(true)
704699
}
705-
protected async differentialDownloadInstaller(
706-
fileInfo: ResolvedUpdateFileInfo,
707-
downloadUpdateOptions: DownloadUpdateOptions,
708-
installerPath: string,
709-
provider: Provider<any>,
710-
oldInstallerFileName: string
711-
): Promise<boolean> {
712-
try {
713-
if (this._testOnlyOptions != null && !this._testOnlyOptions.isUseDifferentialDownload) {
714-
return true
715-
}
716-
const blockmapFileUrls = blockmapFiles(fileInfo.url, this.app.version, downloadUpdateOptions.updateInfoAndProvider.info.version)
717-
this._logger.info(`Download block maps (old: "${blockmapFileUrls[0]}", new: ${blockmapFileUrls[1]})`)
718-
719-
const downloadBlockMap = async (url: URL): Promise<BlockMap> => {
720-
const data = await this.httpExecutor.downloadToBuffer(url, {
721-
headers: downloadUpdateOptions.requestHeaders,
722-
cancellationToken: downloadUpdateOptions.cancellationToken,
723-
})
724-
725-
if (data == null || data.length === 0) {
726-
throw new Error(`Blockmap "${url.href}" is empty`)
727-
}
728-
729-
try {
730-
return JSON.parse(gunzipSync(data).toString())
731-
} catch (e: any) {
732-
throw new Error(`Cannot parse blockmap "${url.href}", error: ${e}`)
733-
}
734-
}
735-
736-
const downloadOptions: DifferentialDownloaderOptions = {
737-
newUrl: fileInfo.url,
738-
oldFile: path.join(this.downloadedUpdateHelper!.cacheDir, oldInstallerFileName),
739-
logger: this._logger,
740-
newFile: installerPath,
741-
isUseMultipleRangeRequest: provider.isUseMultipleRangeRequest,
742-
requestHeaders: downloadUpdateOptions.requestHeaders,
743-
cancellationToken: downloadUpdateOptions.cancellationToken,
744-
}
745-
746-
if (this.listenerCount(DOWNLOAD_PROGRESS) > 0) {
747-
downloadOptions.onProgress = it => this.emit(DOWNLOAD_PROGRESS, it)
748-
}
749-
750-
const blockMapDataList = await Promise.all(blockmapFileUrls.map(u => downloadBlockMap(u)))
751-
await new GenericDifferentialDownloader(fileInfo.info, this.httpExecutor, downloadOptions).download(blockMapDataList[0], blockMapDataList[1])
752-
return false
753-
} catch (e: any) {
754-
this._logger.error(`Cannot download differentially, fallback to full download: ${e.stack || e}`)
755-
if (this._testOnlyOptions != null) {
756-
// test mode
757-
throw e
758-
}
759-
return true
760-
}
761-
}
762700
}
763701

764702
export interface DownloadUpdateOptions {

packages/electron-updater/src/MacUpdater.ts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AllPublishOptions, newError, safeStringifyJson } from "builder-util-runtime"
2-
import { pathExistsSync, stat } from "fs-extra"
3-
import { createReadStream, copyFileSync } from "fs"
4-
import * as path from "path"
2+
import { stat } from "fs-extra"
3+
import { createReadStream } from "fs"
54
import { createServer, IncomingMessage, Server, ServerResponse } from "http"
65
import { AppAdapter } from "./AppAdapter"
76
import { AppUpdater, DownloadUpdateOptions } from "./AppUpdater"
@@ -27,7 +26,6 @@ export class MacUpdater extends AppUpdater {
2726
})
2827
this.nativeUpdater.on("update-downloaded", () => {
2928
this.squirrelDownloadedUpdate = true
30-
this.debug("nativeUpdater.update-downloaded")
3129
})
3230
}
3331

@@ -37,17 +35,6 @@ export class MacUpdater extends AppUpdater {
3735
}
3836
}
3937

40-
private closeServerIfExists() {
41-
if (this.server) {
42-
this.debug("Closing proxy server")
43-
this.server.close(err => {
44-
if (err) {
45-
this.debug("proxy server wasn't already open, probably attempted closing again as a safety check before quit")
46-
}
47-
})
48-
}
49-
}
50-
5138
protected async doDownloadUpdate(downloadUpdateOptions: DownloadUpdateOptions): Promise<Array<string>> {
5239
let files = downloadUpdateOptions.updateInfoAndProvider.provider.resolveFiles(downloadUpdateOptions.updateInfoAndProvider.info)
5340

@@ -92,26 +79,12 @@ export class MacUpdater extends AppUpdater {
9279
throw newError(`ZIP file not provided: ${safeStringifyJson(files)}`, "ERR_UPDATER_ZIP_FILE_NOT_FOUND")
9380
}
9481

95-
const provider = downloadUpdateOptions.updateInfoAndProvider.provider
96-
const CURRENT_MAC_APP_ZIP_FILE_NAME = "update.zip"
97-
9882
return this.executeDownload({
9983
fileExtension: "zip",
10084
fileInfo: zipFileInfo,
10185
downloadUpdateOptions,
102-
task: async (destinationFile, downloadOptions) => {
103-
const cachedFile = path.join(this.downloadedUpdateHelper!.cacheDir, CURRENT_MAC_APP_ZIP_FILE_NAME)
104-
const canDifferentialDownload = () => {
105-
if (!pathExistsSync(cachedFile)) {
106-
log.info("Unable to locate previous update.zip for differential download (is this first install?), falling back to full download")
107-
return false
108-
}
109-
return !downloadUpdateOptions.disableDifferentialDownload
110-
}
111-
if (canDifferentialDownload() && (await this.differentialDownloadInstaller(zipFileInfo, downloadUpdateOptions, destinationFile, provider, CURRENT_MAC_APP_ZIP_FILE_NAME))) {
112-
await this.httpExecutor.download(zipFileInfo.url, destinationFile, downloadOptions)
113-
}
114-
copyFileSync(destinationFile, cachedFile)
86+
task: (destinationFile, downloadOptions) => {
87+
return this.httpExecutor.download(zipFileInfo.url, destinationFile, downloadOptions)
11588
},
11689
done: event => this.updateDownloaded(zipFileInfo, event),
11790
})
@@ -123,8 +96,8 @@ export class MacUpdater extends AppUpdater {
12396

12497
const log = this._logger
12598
const logContext = `fileToProxy=${zipFileInfo.url.href}`
126-
this.closeServerIfExists()
12799
this.debug(`Creating proxy server for native Squirrel.Mac (${logContext})`)
100+
this.server?.close()
128101
this.server = createServer()
129102
this.debug(`Proxy server for native Squirrel.Mac is created (${logContext})`)
130103
this.server.on("close", () => {
@@ -243,12 +216,12 @@ export class MacUpdater extends AppUpdater {
243216
if (this.squirrelDownloadedUpdate) {
244217
// update already fetched by Squirrel, it's ready to install
245218
this.nativeUpdater.quitAndInstall()
246-
this.closeServerIfExists()
219+
this.server?.close()
247220
} else {
248221
// Quit and install as soon as Squirrel get the update
249222
this.nativeUpdater.on("update-downloaded", () => {
250223
this.nativeUpdater.quitAndInstall()
251-
this.closeServerIfExists()
224+
this.server?.close()
252225
})
253226

254227
if (!this.autoInstallOnAppQuit) {

packages/electron-updater/src/NsisUpdater.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { AllPublishOptions, newError, PackageFileInfo, CURRENT_APP_INSTALLER_FILE_NAME, CURRENT_APP_PACKAGE_FILE_NAME } from "builder-util-runtime"
1+
import { AllPublishOptions, newError, PackageFileInfo, BlockMap, CURRENT_APP_PACKAGE_FILE_NAME, CURRENT_APP_INSTALLER_FILE_NAME } from "builder-util-runtime"
22
import * as path from "path"
33
import { AppAdapter } from "./AppAdapter"
44
import { DownloadUpdateOptions } from "./AppUpdater"
55
import { BaseUpdater, InstallOptions } from "./BaseUpdater"
66
import { DifferentialDownloaderOptions } from "./differentialDownloader/DifferentialDownloader"
77
import { FileWithEmbeddedBlockMapDifferentialDownloader } from "./differentialDownloader/FileWithEmbeddedBlockMapDifferentialDownloader"
8-
import { DOWNLOAD_PROGRESS, verifyUpdateCodeSignature } from "./main"
8+
import { GenericDifferentialDownloader } from "./differentialDownloader/GenericDifferentialDownloader"
9+
import { DOWNLOAD_PROGRESS, ResolvedUpdateFileInfo, verifyUpdateCodeSignature } from "./main"
10+
import { blockmapFiles } from "./util"
911
import { findFile, Provider } from "./providers/Provider"
1012
import { unlink } from "fs-extra"
1113
import { verifySignature } from "./windowsExecutableCodeSignatureVerifier"
1214
import { URL } from "url"
15+
import { gunzipSync } from "zlib"
1316

1417
export class NsisUpdater extends BaseUpdater {
1518
/**
@@ -64,7 +67,7 @@ export class NsisUpdater extends BaseUpdater {
6467
if (
6568
isWebInstaller ||
6669
downloadUpdateOptions.disableDifferentialDownload ||
67-
(await this.differentialDownloadInstaller(fileInfo, downloadUpdateOptions, destinationFile, provider, CURRENT_APP_INSTALLER_FILE_NAME))
70+
(await this.differentialDownloadInstaller(fileInfo, downloadUpdateOptions, destinationFile, provider))
6871
) {
6972
await this.httpExecutor.download(fileInfo.url, destinationFile, downloadOptions)
7073
}
@@ -173,6 +176,63 @@ export class NsisUpdater extends BaseUpdater {
173176
return true
174177
}
175178

179+
private async differentialDownloadInstaller(
180+
fileInfo: ResolvedUpdateFileInfo,
181+
downloadUpdateOptions: DownloadUpdateOptions,
182+
installerPath: string,
183+
provider: Provider<any>
184+
): Promise<boolean> {
185+
try {
186+
if (this._testOnlyOptions != null && !this._testOnlyOptions.isUseDifferentialDownload) {
187+
return true
188+
}
189+
const blockmapFileUrls = blockmapFiles(fileInfo.url, this.app.version, downloadUpdateOptions.updateInfoAndProvider.info.version)
190+
this._logger.info(`Download block maps (old: "${blockmapFileUrls[0]}", new: ${blockmapFileUrls[1]})`)
191+
192+
const downloadBlockMap = async (url: URL): Promise<BlockMap> => {
193+
const data = await this.httpExecutor.downloadToBuffer(url, {
194+
headers: downloadUpdateOptions.requestHeaders,
195+
cancellationToken: downloadUpdateOptions.cancellationToken,
196+
})
197+
198+
if (data == null || data.length === 0) {
199+
throw new Error(`Blockmap "${url.href}" is empty`)
200+
}
201+
202+
try {
203+
return JSON.parse(gunzipSync(data).toString())
204+
} catch (e: any) {
205+
throw new Error(`Cannot parse blockmap "${url.href}", error: ${e}`)
206+
}
207+
}
208+
209+
const downloadOptions: DifferentialDownloaderOptions = {
210+
newUrl: fileInfo.url,
211+
oldFile: path.join(this.downloadedUpdateHelper!.cacheDir, CURRENT_APP_INSTALLER_FILE_NAME),
212+
logger: this._logger,
213+
newFile: installerPath,
214+
isUseMultipleRangeRequest: provider.isUseMultipleRangeRequest,
215+
requestHeaders: downloadUpdateOptions.requestHeaders,
216+
cancellationToken: downloadUpdateOptions.cancellationToken,
217+
}
218+
219+
if (this.listenerCount(DOWNLOAD_PROGRESS) > 0) {
220+
downloadOptions.onProgress = it => this.emit(DOWNLOAD_PROGRESS, it)
221+
}
222+
223+
const blockMapDataList = await Promise.all(blockmapFileUrls.map(u => downloadBlockMap(u)))
224+
await new GenericDifferentialDownloader(fileInfo.info, this.httpExecutor, downloadOptions).download(blockMapDataList[0], blockMapDataList[1])
225+
return false
226+
} catch (e: any) {
227+
this._logger.error(`Cannot download differentially, fallback to full download: ${e.stack || e}`)
228+
if (this._testOnlyOptions != null) {
229+
// test mode
230+
throw e
231+
}
232+
return true
233+
}
234+
}
235+
176236
private async differentialDownloadWebPackage(
177237
downloadUpdateOptions: DownloadUpdateOptions,
178238
packageInfo: PackageFileInfo,

0 commit comments

Comments
 (0)