Electron 更新器 API 参考
此页面记录了 Electron Updater 的所有可用方法、事件和配置选项。
通知应用程序就绪()
Section titled “通知应用程序就绪()”必须在每次应用程序启动时调用。 确认捆绑包已成功加载并防止自动回滚。
await updater.notifyAppReady();下载(选项)
Section titled “下载(选项)”从 URL 下载捆绑包。
const bundle = await updater.download({ url: 'https://example.com/bundle.zip', version: '1.0.1', checksum: 'sha256-hash', // Optional but recommended sessionKey: '...', // For encrypted bundles});参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
url | 字符串 | 是的 | 下载包的 URL |
version | 字符串 | 是的 | 捆绑包的版本标识符 |
checksum | 字符串 | 没有 | 用于验证的 SHA256 校验和 |
sessionKey | 字符串 | 没有 | 加密包的会话密钥 |
返回: BundleInfo 对象,带有 id、version、status
下一个(选项)
Section titled “下一个(选项)”对要在下次应用程序重新启动时加载的包进行排队。
await updater.next({ id: 'bundle-id' });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | 字符串 | 是的 | 队列中的捆绑 ID |
设置(选项)
Section titled “设置(选项)”立即切换到捆绑包并重新加载应用程序。
await updater.set({ id: 'bundle-id' });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | 字符串 | 是的 | 要激活的捆绑包 ID |
重新加载()
Section titled “重新加载()”使用当前包手动重新加载应用程序。
await updater.reload();删除(选项)
Section titled “删除(选项)”从存储中删除包。
await updater.delete({ id: 'bundle-id' });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | 字符串 | 是的 | 要删除的捆绑包 ID |
重置(选项)
Section titled “重置(选项)”重置为内置版本或上次成功的捆绑包。
// Reset to builtinawait updater.reset({ toLastSuccessful: false });
// Reset to last successful bundleawait updater.reset({ toLastSuccessful: true });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
toLastSuccessful | 布尔 | 没有 | 如果为 true,则重置为最后一个成功的捆绑包而不是内置 |
###当前()
获取有关当前捆绑包和本机版本的信息。
const info = await updater.current();// { bundle: { id, version, status }, native: '1.0.0' }列表(选项)
Section titled “列表(选项)”列出所有下载的捆绑包。
const bundles = await updater.list();// [{ id, version, status, downloaded, checksum }, ...]getNextBundle()
Section titled “getNextBundle()”让捆绑包排队等待下次重新启动。
const next = await updater.getNextBundle();// { id, version, status } or nullgetFailedUpdate()
Section titled “getFailedUpdate()”获取有关上次失败更新的信息(对于调试回滚很有用)。
const failed = await updater.getFailedUpdate();// { id, version, reason } or nullgetBuiltinVersion()
Section titled “getBuiltinVersion()”获取应用程序二进制文件附带的版本。
const version = await updater.getBuiltinVersion();// '1.0.0'获取最新(选项)
Section titled “获取最新(选项)”检查服务器是否有最新的可用版本。
const latest = await updater.getLatest();
if (latest.url && !latest.error) { // Update available console.log('New version:', latest.version); console.log('Download URL:', latest.url);} else if (latest.error) { console.error('Error checking updates:', latest.error);}退货:
| 物业 | 类型 | 描述 |
|---|---|---|
url | 字符串 | 下载地址(无更新则为空) |
version | 字符串 | 可用版本 |
checksum | 字符串 | SHA256 校验和 |
sessionKey | 字符串 | 加密会话密钥 |
error | 字符串 | 检查失败时出现错误消息 |
message | 字符串 | 服务器消息 |
设置频道(选项)
Section titled “设置频道(选项)”将设备分配给特定通道。
await updater.setChannel({ channel: 'beta' });```### 取消设置频道(选项)
删除通道分配并使用默认值。
```typescriptawait updater.unsetChannel();获取频道()
Section titled “获取频道()”获取当前通道分配。
const channel = await updater.getChannel();// { channel: 'production', status: 'set' }列表频道()
Section titled “列表频道()”列出此应用程序的所有可用频道。
const channels = await updater.listChannels();// ['production', 'beta', 'staging']控制何时应用下载的更新。
setMultiDelay(选项)
Section titled “setMultiDelay(选项)”设置应用更新之前必须满足的条件。
// Wait for app to be backgroundedawait updater.setMultiDelay({ delayConditions: [{ kind: 'background' }]});
// Wait until specific dateawait updater.setMultiDelay({ delayConditions: [{ kind: 'date', value: '2024-12-25T00:00:00Z' }]});
// Wait for app to be killed and restartedawait updater.setMultiDelay({ delayConditions: [{ kind: 'kill' }]});
// Multiple conditions (all must be met)await updater.setMultiDelay({ delayConditions: [ { kind: 'background' }, { kind: 'date', value: '2024-12-25T00:00:00Z' } ]});延迟条件类型:
| 亲切 | 价值 | 描述 |
|---|---|---|
background | 可选持续时间(毫秒) | 等待应用程序进入后台 |
kill | - | 等待应用程序被终止并重新启动 |
date | ISO 日期字符串 | 等到特定日期/时间 |
nativeVersion | 版本字符串 | 等待本机应用程序更新 |
取消延迟()
Section titled “取消延迟()”清除所有延迟条件并在下次检查时立即应用更新。
await updater.cancelDelay();获取设备ID()
Section titled “获取设备ID()”获取唯一的设备标识符。
const deviceId = await updater.getDeviceId();// 'uuid-xxxx-xxxx-xxxx'setCustomId(选项)
Section titled “setCustomId(选项)”为设备设置自定义标识符(对于分析有用)。
await updater.setCustomId({ customId: 'user-123' });setUpdateUrl(选项)
Section titled “setUpdateUrl(选项)”在运行时更改更新服务器 URL。
await updater.setUpdateUrl({ url: 'https://my-server.com/updates' });setStatsUrl(选项)
Section titled “setStatsUrl(选项)”更改统计报告 URL。
await updater.setStatsUrl({ url: 'https://my-server.com/stats' });setChannelUrl(选项)
Section titled “setChannelUrl(选项)”更改频道管理 URL。
await updater.setChannelUrl({ url: 'https://my-server.com/channel' });setAppId(选项)
Section titled “setAppId(选项)”在运行时更改应用程序 ID。
await updater.setAppId({ appId: 'com.example.newapp' });getAppId()
Section titled “getAppId()”获取当前应用程序ID。
const appId = await updater.getAppId();setDebugMenu(选项)
Section titled “setDebugMenu(选项)”启用或禁用调试菜单。
await updater.setDebugMenu({ enabled: true });isDebugMenuEnabled()
Section titled “isDebugMenuEnabled()”检查调试菜单是否启用。
const enabled = await updater.isDebugMenuEnabled();使用 addListener 监听更新事件:
updater.addListener('eventName', (event) => { // Handle event});| 活动 | 有效负载 | 描述 |
|---|---|---|
download | { percent, status } | 下载进度更新 |
updateAvailable | { bundle } | 有新更新可用 |
noNeedUpdate | { message } | 已经是最新的了 |
downloadComplete | { bundle } | 下载成功 |
downloadFailed | { bundle, error } | 下载失败 |
breakingAvailable | { bundle } | 提供不兼容的更新(需要本机更新) |
updateFailed | { bundle, reason } | 更新安装失败 |
appReloaded | {} | 应用程序已重新加载 |
appReady | {} | notifyAppReady() 被称为 |
示例:完整事件处理
Section titled “示例:完整事件处理”// Progress trackingupdater.addListener('download', (event) => { updateProgressBar(event.percent);});
// Update available notificationupdater.addListener('updateAvailable', (event) => { showNotification(`Update ${event.bundle.version} available!`);});
// Handle completionupdater.addListener('downloadComplete', async (event) => { // Queue for next restart await updater.next({ id: event.bundle.id }); showNotification('Update will apply on next restart');});
// Handle failuresupdater.addListener('updateFailed', (event) => { console.error('Update failed:', event.reason); reportError(event);});构造函数选项
Section titled “构造函数选项”ElectronUpdater 的完整配置选项:
const updater = new ElectronUpdater({ // Required appId: 'com.example.app',
// Version override version: '1.0.0', // Override builtin version detection
// Server URLs updateUrl: 'https://plugin.capgo.app/updates', channelUrl: 'https://plugin.capgo.app/channel_self', statsUrl: 'https://plugin.capgo.app/stats',
// Behavior autoUpdate: true, // Enable automatic update checks appReadyTimeout: 10000, // Milliseconds before rollback (default: 10000) autoDeleteFailed: true, // Auto-delete failed bundles autoDeletePrevious: true, // Auto-delete old bundles resetWhenUpdate: true, // Reset to builtin on native update
// Channels defaultChannel: 'production',
// Direct Update Mode directUpdate: false, // 'atInstall' | 'onLaunch' | 'always' | false
// Security publicKey: '...', // RSA public key for E2E encryption
// Dynamic Configuration allowModifyUrl: false, // Allow runtime URL changes allowModifyAppId: false, // Allow runtime App ID changes persistCustomId: false, // Persist custom ID across updates persistModifyUrl: false, // Persist URL changes
// Debug debugMenu: false, // Enable debug menu (Ctrl+Shift+D) disableJSLogging: false, // Disable console logs
// Periodic Updates periodCheckDelay: 0, // Seconds between auto-checks (0 = disabled, min 600)});