From db0754805b4d6c5d8a4d86af7cb107db87bda303 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Sun, 1 May 2022 15:37:54 -0700 Subject: [PATCH] refactor: Add better typing support on AppUpdater as an event emitter (#6825) --- .changeset/thin-toys-report.md | 5 +++ packages/electron-updater/package.json | 3 +- packages/electron-updater/src/AppUpdater.ts | 31 +++++++++++++++++-- .../src/electronHttpExecutor.ts | 3 +- packages/electron-updater/src/main.ts | 4 +-- pnpm-lock.yaml | 8 +++++ test/src/helpers/updaterTestUtil.ts | 2 +- test/src/updater/nsisUpdaterTest.ts | 2 +- 8 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 .changeset/thin-toys-report.md diff --git a/.changeset/thin-toys-report.md b/.changeset/thin-toys-report.md new file mode 100644 index 0000000000..97412f9eb3 --- /dev/null +++ b/.changeset/thin-toys-report.md @@ -0,0 +1,5 @@ +--- +"electron-updater": patch +--- + +Added types for AppUpdater's events diff --git a/packages/electron-updater/package.json b/packages/electron-updater/package.json index d9cd95a656..1a342bf6db 100644 --- a/packages/electron-updater/package.json +++ b/packages/electron-updater/package.json @@ -29,7 +29,8 @@ "@types/fs-extra": "9.0.13", "@types/js-yaml": "4.0.3", "@types/lodash.escaperegexp": "4.1.6", - "@types/lodash.isequal": "4.5.5" + "@types/lodash.isequal": "4.5.5", + "typed-emitter": "^2.1.0" }, "typings": "./out/main.d.ts", "publishConfig": { diff --git a/packages/electron-updater/src/AppUpdater.ts b/packages/electron-updater/src/AppUpdater.ts index b5b6676591..25dd721287 100644 --- a/packages/electron-updater/src/AppUpdater.ts +++ b/packages/electron-updater/src/AppUpdater.ts @@ -1,4 +1,15 @@ -import { AllPublishOptions, asArray, CancellationToken, newError, PublishConfiguration, UpdateInfo, UUID, DownloadOptions, CancellationError } from "builder-util-runtime" +import { + AllPublishOptions, + asArray, + CancellationToken, + newError, + PublishConfiguration, + UpdateInfo, + UUID, + DownloadOptions, + CancellationError, + ProgressInfo, +} from "builder-util-runtime" import { randomBytes } from "crypto" import { EventEmitter } from "events" import { mkdir, outputFile, readFile, rename, unlink } from "fs-extra" @@ -10,14 +21,28 @@ import { eq as isVersionsEqual, gt as isVersionGreaterThan, lt as isVersionLessT import { AppAdapter } from "./AppAdapter" import { createTempUpdateFile, DownloadedUpdateHelper } from "./DownloadedUpdateHelper" import { ElectronAppAdapter } from "./ElectronAppAdapter" -import { ElectronHttpExecutor, getNetSession } from "./electronHttpExecutor" +import { ElectronHttpExecutor, getNetSession, LoginCallback } from "./electronHttpExecutor" import { GenericProvider } from "./providers/GenericProvider" import { DOWNLOAD_PROGRESS, Logger, Provider, ResolvedUpdateFileInfo, UPDATE_DOWNLOADED, UpdateCheckResult, UpdateDownloadedEvent, UpdaterSignal } from "./main" import { createClient, isUrlProbablySupportMultiRangeRequests } from "./providerFactory" import { ProviderPlatform } from "./providers/Provider" +import type TypedEmitter from "typed-emitter" import Session = Electron.Session +import { AuthInfo } from "electron" + +export type AppUpdaterEvents = { + error: (error: Error, message?: string) => void + login: (info: AuthInfo, callback: LoginCallback) => void + "checking-for-update": () => void + "update-not-available": (info: UpdateInfo) => void + "update-available": (info: UpdateInfo) => void + "update-downloaded": (event: UpdateDownloadedEvent) => void + "download-progress": (info: ProgressInfo) => void + "update-cancelled": (info: UpdateInfo) => void + "appimage-filename-updated": (path: string) => void +} -export abstract class AppUpdater extends EventEmitter { +export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter) { /** * Whether to automatically download an update when it is found. */ diff --git a/packages/electron-updater/src/electronHttpExecutor.ts b/packages/electron-updater/src/electronHttpExecutor.ts index 037d466723..f60af8e04b 100644 --- a/packages/electron-updater/src/electronHttpExecutor.ts +++ b/packages/electron-updater/src/electronHttpExecutor.ts @@ -1,4 +1,5 @@ import { DownloadOptions, HttpExecutor, configureRequestOptions, configureRequestUrl } from "builder-util-runtime" +import { AuthInfo } from "electron" import { RequestOptions } from "http" import Session = Electron.Session import ClientRequest = Electron.ClientRequest @@ -15,7 +16,7 @@ export function getNetSession(): Session { export class ElectronHttpExecutor extends HttpExecutor { private cachedSession: Session | null = null - constructor(private readonly proxyLoginCallback?: (authInfo: any, callback: LoginCallback) => void) { + constructor(private readonly proxyLoginCallback?: (authInfo: AuthInfo, callback: LoginCallback) => void) { super() } diff --git a/packages/electron-updater/src/main.ts b/packages/electron-updater/src/main.ts index a2bbdec5ce..cd25146b53 100644 --- a/packages/electron-updater/src/main.ts +++ b/packages/electron-updater/src/main.ts @@ -56,8 +56,8 @@ export interface UpdateCheckResult { export type UpdaterEvents = "login" | "checking-for-update" | "update-available" | "update-not-available" | "update-cancelled" | "download-progress" | "update-downloaded" | "error" -export const DOWNLOAD_PROGRESS: UpdaterEvents = "download-progress" -export const UPDATE_DOWNLOADED: UpdaterEvents = "update-downloaded" +export const DOWNLOAD_PROGRESS = "download-progress" +export const UPDATE_DOWNLOADED = "update-downloaded" export type LoginHandler = (authInfo: any, callback: LoginCallback) => void diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d1d90f63fb..4f86f82ac2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -380,6 +380,7 @@ importers: lodash.escaperegexp: ^4.1.2 lodash.isequal: ^4.5.0 semver: ^7.3.5 + typed-emitter: ^2.1.0 dependencies: '@types/semver': 7.3.9 builder-util-runtime: link:../builder-util-runtime @@ -394,6 +395,7 @@ importers: '@types/js-yaml': 4.0.3 '@types/lodash.escaperegexp': 4.1.6 '@types/lodash.isequal': 4.5.5 + typed-emitter: 2.1.0 test: specifiers: @@ -11119,6 +11121,12 @@ packages: engines: {node: '>=8'} dev: true + /typed-emitter/2.1.0: + resolution: {integrity: sha512-g/KzbYKbH5C2vPkaXGu8DJlHrGKHLsM25Zg9WuC9pMGfuvT+X25tZQWo5fK1BjBm8+UrVE9LDCvaY0CQk+fXDA==} + optionalDependencies: + rxjs: 7.5.5 + dev: true + /typedarray-to-buffer/3.1.5: resolution: {integrity: sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==} dependencies: diff --git a/test/src/helpers/updaterTestUtil.ts b/test/src/helpers/updaterTestUtil.ts index 47d26bef9b..a8af85c13e 100644 --- a/test/src/helpers/updaterTestUtil.ts +++ b/test/src/helpers/updaterTestUtil.ts @@ -82,7 +82,7 @@ export function tuneTestUpdater(updater: AppUpdater, options?: TestOnlyUpdaterOp export function trackEvents(updater: AppUpdater) { const actualEvents: Array = [] - for (const eventName of ["checking-for-update", "update-available", "update-downloaded", "error"]) { + for (const eventName of ["checking-for-update", "update-available", "update-downloaded", "error"] as const) { updater.addListener(eventName, () => { actualEvents.push(eventName) }) diff --git a/test/src/updater/nsisUpdaterTest.ts b/test/src/updater/nsisUpdaterTest.ts index 48da3d8210..03ac05989b 100644 --- a/test/src/updater/nsisUpdaterTest.ts +++ b/test/src/updater/nsisUpdaterTest.ts @@ -26,7 +26,7 @@ test("downgrade (disallowed, beta)", async () => { }) const actualEvents: Array = [] - const expectedEvents = ["checking-for-update", "update-not-available"] + const expectedEvents = ["checking-for-update", "update-not-available"] as const for (const eventName of expectedEvents) { updater.addListener(eventName, () => { actualEvents.push(eventName)