|
| 1 | +// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= |
| 14 | + |
| 15 | +import type { |
| 16 | + AppUpdater, |
| 17 | + ResolvedUpdateFileInfo, |
| 18 | + UpdateInfo, |
| 19 | +} from 'electron-updater'; |
| 20 | +import type { ProviderRuntimeOptions } from 'electron-updater/out/providers/Provider'; |
| 21 | +import { createRequire } from 'node:module'; |
| 22 | + |
| 23 | +const require = createRequire(import.meta.url); |
| 24 | + |
| 25 | +const { GitHubProvider } = |
| 26 | + require('electron-updater/out/providers/GitHubProvider') as { |
| 27 | + GitHubProvider: new ( |
| 28 | + options: GitHubProviderOptions, |
| 29 | + updater: AppUpdater, |
| 30 | + runtimeOptions: ProviderRuntimeOptions |
| 31 | + ) => object; |
| 32 | + }; |
| 33 | + |
| 34 | +const { resolveFiles } = require('electron-updater/out/providers/Provider') as { |
| 35 | + resolveFiles: ( |
| 36 | + updateInfo: UpdateInfo, |
| 37 | + baseUrl: URL, |
| 38 | + pathTransformer?: (path: string) => string |
| 39 | + ) => Array<ResolvedUpdateFileInfo>; |
| 40 | +}; |
| 41 | + |
| 42 | +export const DEFAULT_CDN_RELEASE_BASE_URL = 'https://cdn.eigent.ai/releases'; |
| 43 | + |
| 44 | +export type UpdatePlatformDirectory = |
| 45 | + | 'mac-arm64' |
| 46 | + | 'mac-intel' |
| 47 | + | 'win-x64' |
| 48 | + | 'linux-x64'; |
| 49 | + |
| 50 | +type GitHubProviderOptions = { |
| 51 | + provider: 'github'; |
| 52 | + owner: string; |
| 53 | + repo: string; |
| 54 | + releaseType?: 'draft' | 'prerelease' | 'release' | null; |
| 55 | + channel?: string | null; |
| 56 | + host?: string | null; |
| 57 | + protocol?: 'https' | 'http' | null; |
| 58 | + token?: string | null; |
| 59 | + private?: boolean | null; |
| 60 | + tagNamePrefix?: string; |
| 61 | + vPrefixedTagName?: boolean; |
| 62 | +}; |
| 63 | + |
| 64 | +export type GitHubReleaseCdnOptions = { |
| 65 | + owner: string; |
| 66 | + repo: string; |
| 67 | + releaseType?: 'draft' | 'prerelease' | 'release' | null; |
| 68 | + channel?: string | null; |
| 69 | + host?: string | null; |
| 70 | + protocol?: 'https' | 'http' | null; |
| 71 | + token?: string | null; |
| 72 | + private?: boolean | null; |
| 73 | + tagNamePrefix?: string; |
| 74 | + vPrefixedTagName?: boolean; |
| 75 | + cdnBaseUrl: string; |
| 76 | + platformDir: UpdatePlatformDirectory; |
| 77 | +}; |
| 78 | + |
| 79 | +export function normalizeCdnReleaseBaseUrl(url: string): string { |
| 80 | + return url.replace(/\/+$/, ''); |
| 81 | +} |
| 82 | + |
| 83 | +export function getUpdatePlatformDirectory( |
| 84 | + platform: NodeJS.Platform, |
| 85 | + arch: NodeJS.Architecture |
| 86 | +): UpdatePlatformDirectory | null { |
| 87 | + switch (platform) { |
| 88 | + case 'darwin': |
| 89 | + if (arch === 'arm64') { |
| 90 | + return 'mac-arm64'; |
| 91 | + } |
| 92 | + if (arch === 'x64') { |
| 93 | + return 'mac-intel'; |
| 94 | + } |
| 95 | + return null; |
| 96 | + case 'win32': |
| 97 | + return arch === 'x64' ? 'win-x64' : null; |
| 98 | + case 'linux': |
| 99 | + return arch === 'x64' ? 'linux-x64' : null; |
| 100 | + default: |
| 101 | + return null; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export function getGitHubReleaseChannel( |
| 106 | + platform: NodeJS.Platform, |
| 107 | + arch: NodeJS.Architecture |
| 108 | +): string { |
| 109 | + if (platform === 'darwin') { |
| 110 | + return arch === 'arm64' ? 'latest-arm64' : 'latest-x64'; |
| 111 | + } |
| 112 | + |
| 113 | + return 'latest'; |
| 114 | +} |
| 115 | + |
| 116 | +export function buildVersionedReleaseBaseUrl( |
| 117 | + cdnBaseUrl: string, |
| 118 | + version: string, |
| 119 | + platformDir: UpdatePlatformDirectory |
| 120 | +): string { |
| 121 | + return `${normalizeCdnReleaseBaseUrl(cdnBaseUrl)}/v${version}/${platformDir}/`; |
| 122 | +} |
| 123 | + |
| 124 | +export class GitHubReleaseCdnProvider extends GitHubProvider { |
| 125 | + private readonly cdnBaseUrl: string; |
| 126 | + private readonly platformDir: UpdatePlatformDirectory; |
| 127 | + |
| 128 | + constructor( |
| 129 | + options: GitHubReleaseCdnOptions, |
| 130 | + updater: AppUpdater, |
| 131 | + runtimeOptions: ProviderRuntimeOptions |
| 132 | + ) { |
| 133 | + const githubOptions: GitHubProviderOptions = { |
| 134 | + provider: 'github', |
| 135 | + owner: options.owner, |
| 136 | + repo: options.repo, |
| 137 | + releaseType: options.releaseType, |
| 138 | + channel: options.channel, |
| 139 | + host: options.host, |
| 140 | + protocol: options.protocol, |
| 141 | + token: options.token, |
| 142 | + private: options.private, |
| 143 | + tagNamePrefix: options.tagNamePrefix, |
| 144 | + vPrefixedTagName: options.vPrefixedTagName, |
| 145 | + }; |
| 146 | + |
| 147 | + super(githubOptions, updater, runtimeOptions); |
| 148 | + |
| 149 | + this.cdnBaseUrl = normalizeCdnReleaseBaseUrl(options.cdnBaseUrl); |
| 150 | + this.platformDir = options.platformDir; |
| 151 | + } |
| 152 | + |
| 153 | + resolveFiles(updateInfo: UpdateInfo): Array<ResolvedUpdateFileInfo> { |
| 154 | + const versionedBaseUrl = new URL( |
| 155 | + buildVersionedReleaseBaseUrl( |
| 156 | + this.cdnBaseUrl, |
| 157 | + updateInfo.version, |
| 158 | + this.platformDir |
| 159 | + ) |
| 160 | + ); |
| 161 | + |
| 162 | + return resolveFiles(updateInfo, versionedBaseUrl, (filePath: string) => |
| 163 | + filePath.replace(/ /g, '-') |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments