跳转到正文

Electron Updater API 参考文档

GitHub

本页介绍了 Electron Updater 的所有可用方法、事件及配置选项。

每次应用启动时都必须调用。 确认 bundle 已成功加载,并防止自动回滚。

await updater.notifyAppReady();

从 URL 下载 bundle。

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字符串是用于下载 bundle 的 URL
version字符串是bundle 的版本标识符
checksum字符串否用于验证的 SHA256 校验和
sessionKey字符串否用于加密 bundle 的 Session key

返回值: BundleInfo 包含以下内容的 object id, version, status

将 bundle 加入队列,在下次应用重启时加载。

await updater.next({ id: 'bundle-id' });

参数:

选项类型必填描述
id字符串是要加入队列的 Bundle ID

立即切换到指定 bundle 并重新加载应用。

await updater.set({ id: 'bundle-id' });

参数:

选项类型必填描述
id字符串是要激活的 Bundle ID

使用当前 bundle 手动重新加载应用。

await updater.reload();

从存储中删除 bundle。

await updater.delete({ id: 'bundle-id' });

参数:

选项类型必填描述
id字符串是要删除的 Bundle ID

重置为内置版本或最近一次成功的 bundle。

// Reset to builtin
await updater.reset({ toLastSuccessful: false });
// Reset to last successful bundle
await updater.reset({ toLastSuccessful: true });

参数:

选项类型必填描述
toLastSuccessful布尔值否如果为 true,则重置为上一次成功的 bundle,而非内置版本

获取当前 bundle 和原生版本的信息。

const info = await updater.current();
// { bundle: { id, version, status }, native: '1.0.0' }

列出所有已下载的 bundle。

const bundles = await updater.list();
// [{ id, version, status, downloaded, checksum }, ...]

获取排队等待下次重启时应用的 bundle。

const next = await updater.getNextBundle();
// { id, version, status } or null

获取上次更新失败的信息(用于调试回滚)。

const failed = await updater.getFailedUpdate();
// { id, version, reason } or null

获取随应用二进制文件一起发布的版本。

const version = await updater.getBuiltinVersion();
// '1.0.0'

检查服务器上可用的最新版本。

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字符串下载 URL(无更新时为空)
version字符串可用版本
checksum字符串SHA256 校验和
sessionKey字符串加密会话密钥
error字符串检查失败时的错误消息
message字符串服务器消息

将设备分配到指定的 Channel。

await updater.setChannel({ channel: 'beta' });

移除通道分配,使用默认通道。

await updater.unsetChannel();

获取当前通道分配。

const channel = await updater.getChannel();
// { channel: 'production', status: 'set' }

列出此应用的所有可用通道。

const channels = await updater.listChannels();
// ['production', 'beta', 'staging']

控制何时应用已下载的更新。

设置应用更新前必须满足的条件。

// Wait for app to be backgrounded
await updater.setMultiDelay({
delayConditions: [{ kind: 'background' }]
});
// Wait until specific date
await updater.setMultiDelay({
delayConditions: [{ kind: 'date', value: '2024-12-25T00:00:00Z' }]
});
// Wait for app to be killed and restarted
await 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-等待应用被关闭并重启
dateISO 日期字符串等待到指定日期/时间
nativeVersion版本字符串等待原生应用更新

清除所有延迟条件,并在下次检查时立即应用更新。

await updater.cancelDelay();

获取设备的唯一标识符。

const deviceId = await updater.getDeviceId();
// 'uuid-xxxx-xxxx-xxxx'

为设备设置自定义标识符(常用于数据分析)。

await updater.setCustomId({ customId: 'user-123' });

在运行时更改更新服务器 URL。

await updater.setUpdateUrl({ url: 'https://my-server.com/updates' });

更改统计上报 URL。

await updater.setStatsUrl({ url: 'https://my-server.com/stats' });

更改频道管理 URL。

await updater.setChannelUrl({ url: 'https://my-server.com/channel' });

在运行时更改 App ID。

await updater.setAppId({ appId: 'com.example.newapp' });

获取当前 App ID。

const appId = await updater.getAppId();

启用或禁用调试菜单。

await updater.setDebugMenu({ enabled: true });

检查调试菜单是否已启用。

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() 已调用
// Progress tracking
updater.addListener('download', (event) => {
updateProgressBar(event.percent);
});
// Update available notification
updater.addListener('updateAvailable', (event) => {
showNotification(`Update ${event.bundle.version} available!`);
});
// Handle completion
updater.addListener('downloadComplete', async (event) => {
// Queue for next restart
await updater.next({ id: event.bundle.id });
showNotification('Update will apply on next restart');
});
// Handle failures
updater.addListener('updateFailed', (event) => {
console.error('Update failed:', event.reason);
reportError(event);
});

以下内容的完整配置选项: 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)
});

如果你正在使用 Electron Updater API 参考文档 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/electron-updater 用于“使用 @capgo/electron-updater”中的原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。