Repository: electron-userland/electron-builder
File: packages/electron-updater/src/MacUpdater.ts (TypeScript)

Code before:

    // allow arm64 macs to install universal or rosetta2(x64) - https://github.com/electron-userland/electron-builder/pull/5524
    const isArm64 = (file: ResolvedUpdateFileInfo) => file.url.pathname.includes("arm64") || file.info.url?.includes("arm64")
    if (isArm64Mac && files.some(isArm64)) {
      files = files.filter(file => isArm64Mac === isArm64(file))
    } else {
      files = files.filter(file => !isArm64(file))
    }

    const zipFileInfo = findFile(files, "zip", ["pkg", "dmg"])

    if (zipFileInfo == null) {
      throw newError(`ZIP file not provided: ${safeStringifyJson(files)}`, "ERR_UPDATER_ZIP_FILE_NOT_FOUND")
    }

    const provider = downloadUpdateOptions.updateInfoAndProvider.provider
    const CURRENT_MAC_APP_ZIP_FILE_NAME = "update.zip"

    return this.executeDownload({
      fileExtension: "zip",
      fileInfo: zipFileInfo,
      downloadUpdateOptions,
      task: async (destinationFile, downloadOptions) => {
        const cachedUpdateFilePath = path.join(this.downloadedUpdateHelper!.cacheDir, CURRENT_MAC_APP_ZIP_FILE_NAME)
        const canDifferentialDownload = () => {
          if (!pathExistsSync(cachedUpdateFilePath)) {
            log.info("Unable to locate previous update.zip for differential download (is this first install?), falling back to full download")
            return false
          }
          return !downloadUpdateOptions.disableDifferentialDownload
        }
        let differentialDownloadFailed = true
        if (canDifferentialDownload()) {
          differentialDownloadFailed = await this.differentialDownloadInstaller(zipFileInfo, downloadUpdateOptions, destinationFile, provider, CURRENT_MAC_APP_ZIP_FILE_NAME)
        }

        if (differentialDownloadFailed) {
          await this.httpExecutor.download(zipFileInfo.url, destinationFile, downloadOptions)
        }
      },
      done: event => {
        try {
          const cachedUpdateFilePath = path.join(this.downloadedUpdateHelper!.cacheDir, CURRENT_MAC_APP_ZIP_FILE_NAME)
          copyFileSync(event.downloadedFile, cachedUpdateFilePath)
        } catch (error: any) {
          this._logger.error(`Unable to copy file for caching: ${error.message}`)
        }
        return this.updateDownloaded(zipFileInfo, event)
      },
    })
  }

Review comment (performance):
This looks good, but I'm curious as to why `cacheDir` is an empty string. Won't this `!pathExistsSync(...)` always return false and fallback to full download?

Code after:

    // allow arm64 macs to install universal or rosetta2(x64) - https://github.com/electron-userland/electron-builder/pull/5524
    const isArm64 = (file: ResolvedUpdateFileInfo) => file.url.pathname.includes("arm64") || file.info.url?.includes("arm64")
    if (isArm64Mac && files.some(isArm64)) {
      files = files.filter(file => isArm64Mac === isArm64(file))
    } else {
      files = files.filter(file => !isArm64(file))
    }

    const zipFileInfo = findFile(files, "zip", ["pkg", "dmg"])

    if (zipFileInfo == null) {
      throw newError(`ZIP file not provided: ${safeStringifyJson(files)}`, "ERR_UPDATER_ZIP_FILE_NOT_FOUND")
    }

    const provider = downloadUpdateOptions.updateInfoAndProvider.provider
    const CURRENT_MAC_APP_ZIP_FILE_NAME = "update.zip"

    return this.executeDownload({
      fileExtension: "zip",
      fileInfo: zipFileInfo,
      downloadUpdateOptions,
      task: async (destinationFile, downloadOptions) => {
        const cachedUpdateFilePath = path.join(this.downloadedUpdateHelper!.cacheDir, CURRENT_MAC_APP_ZIP_FILE_NAME)
        const canDifferentialDownload = () => {
          if (!pathExistsSync(cachedUpdateFilePath)) {
            log.info("Unable to locate previous update.zip for differential download (is this first install?), falling back to full download")
            return false
          }
          return !downloadUpdateOptions.disableDifferentialDownload
        }
        let differentialDownloadFailed = true
        if (canDifferentialDownload()) {
          differentialDownloadFailed = await this.differentialDownloadInstaller(zipFileInfo, downloadUpdateOptions, destinationFile, provider, CURRENT_MAC_APP_ZIP_FILE_NAME)
        }

        if (differentialDownloadFailed) {
          await this.httpExecutor.download(zipFileInfo.url, destinationFile, downloadOptions)
        }
      },
      done: event => {
        if (!downloadUpdateOptions.disableDifferentialDownload) {
          try {
            const cachedUpdateFilePath = path.join(this.downloadedUpdateHelper!.cacheDir, CURRENT_MAC_APP_ZIP_FILE_NAME)
            copyFileSync(event.downloadedFile, cachedUpdateFilePath)
          } catch (error: any) {
            this._logger.warn(`Unable to copy file for caching for future differential downloads: ${error.message}`)
          }
        }
        return this.updateDownloaded(zipFileInfo, event)
      },