Electron Updater API 参考文档
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
本页介绍了 Electron Updater 的所有可用方法、事件及配置选项。
核心方法
标题为“核心方法”的章节notifyAppReady()
标题为“notifyAppReady()”的章节每次应用启动时都必须调用。 确认 bundle 已成功加载,并防止自动回滚。
await updater.notifyAppReady();download(options)
“download(options)” 部分从 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
next(options)
标题为 “next(options)” 的章节将 bundle 加入队列,在下次应用重启时加载。
await updater.next({ id: 'bundle-id' });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | 字符串 | 是 | 要加入队列的 Bundle ID |
set(options)
标题为“set(options)”的章节立即切换到指定 bundle 并重新加载应用。
await updater.set({ id: 'bundle-id' });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | 字符串 | 是 | 要激活的 Bundle ID |
reload()
标题为 “reload()” 的章节使用当前 bundle 手动重新加载应用。
await updater.reload();delete(options)
标题为 “delete(options)” 的章节从存储中删除 bundle。
await updater.delete({ id: 'bundle-id' });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | 字符串 | 是 | 要删除的 Bundle ID |
reset(options)
标题为“reset(options)”的章节重置为内置版本或最近一次成功的 bundle。
// Reset to builtinawait updater.reset({ toLastSuccessful: false });
// Reset to last successful bundleawait updater.reset({ toLastSuccessful: true });参数:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
toLastSuccessful | 布尔值 | 否 | 如果为 true,则重置为上一次成功的 bundle,而非内置版本 |
Bundle 信息
标题为“Bundle 信息”的章节current()
标题为“current()”的章节获取当前 bundle 和原生版本的信息。
const info = await updater.current();// { bundle: { id, version, status }, native: '1.0.0' }list(options)
标题为“list(options)”的章节列出所有已下载的 bundle。
const bundles = await updater.list();// [{ id, version, status, downloaded, checksum }, ...]getNextBundle()
“getNextBundle()”章节获取排队等待下次重启时应用的 bundle。
const next = await updater.getNextBundle();// { id, version, status } or nullgetFailedUpdate()
标题为“getFailedUpdate()”的章节获取上次更新失败的信息(用于调试回滚)。
const failed = await updater.getFailedUpdate();// { id, version, reason } or nullgetBuiltinVersion()
标题为 “getBuiltinVersion()” 的章节获取随应用二进制文件一起发布的版本。
const version = await updater.getBuiltinVersion();// '1.0.0'更新检查
标题为“更新检查”的章节getLatest(options)
标题为“getLatest(options)”的章节检查服务器上可用的最新版本。
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 管理
标题为“Channel 管理”的章节setChannel(options)
标题为“setChannel(options)”的章节将设备分配到指定的 Channel。
await updater.setChannel({ channel: 'beta' });unsetChannel(options)
标题为“unsetChannel(options)”的章节移除通道分配,使用默认通道。
await updater.unsetChannel();getChannel()
“getChannel()” 章节获取当前通道分配。
const channel = await updater.getChannel();// { channel: 'production', status: 'set' }listChannels()
“listChannels()” 章节列出此应用的所有可用通道。
const channels = await updater.listChannels();// ['production', 'beta', 'staging']控制何时应用已下载的更新。
setMultiDelay(options)
标题为“setMultiDelay(options)”的章节设置应用更新前必须满足的条件。
// 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 | 版本字符串 | 等待原生应用更新 |
cancelDelay()
标题为“cancelDelay()”的章节清除所有延迟条件,并在下次检查时立即应用更新。
await updater.cancelDelay();设备标识
标题为“设备标识”的章节getDeviceId()
“getDeviceId()” 章节获取设备的唯一标识符。
const deviceId = await updater.getDeviceId();// 'uuid-xxxx-xxxx-xxxx'setCustomId(options)
“setCustomId(options)” 章节为设备设置自定义标识符(常用于数据分析)。
await updater.setCustomId({ customId: 'user-123' });配置
“配置” 章节setUpdateUrl(options)
“setUpdateUrl(options)” 章节在运行时更改更新服务器 URL。
await updater.setUpdateUrl({ url: 'https://my-server.com/updates' });setStatsUrl(options)
标题为“setStatsUrl(options)”的章节更改统计上报 URL。
await updater.setStatsUrl({ url: 'https://my-server.com/stats' });setChannelUrl(options)
标题为“setChannelUrl(options)”的章节更改频道管理 URL。
await updater.setChannelUrl({ url: 'https://my-server.com/channel' });setAppId(options)
标题为“setAppId(options)”的章节在运行时更改 App ID。
await updater.setAppId({ appId: 'com.example.newapp' });getAppId()
标题为“getAppId()”的章节获取当前 App ID。
const appId = await updater.getAppId();setDebugMenu(options)
标题为“setDebugMenu(options)”的章节启用或禁用调试菜单。
await updater.setDebugMenu({ enabled: true });isDebugMenuEnabled()
标题为“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() 已调用 |
示例:完整的事件处理
标题为“示例:完整的事件处理”的章节// 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);});构造函数选项
标题为“构造函数选项”的章节以下内容的完整配置选项: 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 参考文档
标题为“继续阅读 Electron Updater API 参考文档”的章节如果你正在使用 Electron Updater API 参考文档 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/electron-updater 用于“使用 @capgo/electron-updater”中的原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。