跳转到内容

Electron 更新器 API 参考

此页面记录了 Electron Updater 的所有可用方法、事件和配置选项。

必须在每次应用程序启动时调用。 确认捆绑包已成功加载并防止自动回滚。

await updater.notifyAppReady();

从 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 对象,带有 idversionstatus

对要在下次应用程序重新启动时加载的包进行排队。

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

参数:

选项类型必填描述
id字符串是的队列中的捆绑 ID

立即切换到捆绑包并重新加载应用程序。

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

参数:

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

使用当前包手动重新加载应用程序。

await updater.reload();

从存储中删除包。

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

参数:

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

重置为内置版本或上次成功的捆绑包。

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

参数:

选项类型必填描述
toLastSuccessful布尔没有如果为 true,则重置为最后一个成功的捆绑包而不是内置

###当前()

获取有关当前捆绑包和本机版本的信息。

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

列出所有下载的捆绑包。

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

让捆绑包排队等待下次重新启动。

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

将设备分配给特定通道。

await updater.setChannel({ channel: 'beta' });
```### 取消设置频道(选项)
删除通道分配并使用默认值。
```typescript
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' });

在运行时更改应用程序 ID。

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

获取当前应用程序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)
});