fix(updater): don't mutate the caller's options in the JS API (#3690)

`convertToRustHeaders` replaced `options.headers` of the object passed to
`check`, `download` and `downloadAndInstall` with an array of entries. The
headers are now converted into the invoke payload only (`toRustHeaders`).

E2E: `check` leaves the options object untouched.

plugins/updater/guest-js/index.ts
This commit is contained in:
Lucas Fernandes Nogueira authored and GitHub committed 2026-10-11 16:17:29 -03:00
1 parent 24e6451b11
commit a819187e12
4 files changed
+29 -13

No files matched your search

@@ -0,0 +1,6 @@
---
updater: patch
updater-js: patch
---
`check()`, `Update.download()` and `Update.downloadAndInstall()` no longer replace the `headers` of the options object they are given with an array.
@@ -74,4 +74,14 @@ describePlugin('updater', { desktopOnly: true }, () => {
})
expect(version).toBe(UPDATER_FIXTURE_VERSION)
})
it('check does not modify the options it is given', async () => {
const headers = await tauri(async (api) => {
const options = { headers: { 'x-e2e-updater': 'yes' } }
const update = await api.updater.check(options)
await update?.close()
return options.headers
})
expect(headers).toEqual({ 'x-e2e-updater': 'yes' })
})
})
+1 -1
View File
@@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_PLUGIN_UPDATER__=function(e,n){"use strict";class t extends n.Resource{constructor(e){super(e.rid),this.available=!0,this.currentVersion=e.currentVersion,this.version=e.version,this.date=e.date,this.body=e.body,this.rawJson=e.rawJson}async download(e,t){a(t);const s=new n.Channel;e&&(s.onmessage=e);const d=await n.invoke("plugin:updater|download",{onEvent:s,rid:this.rid,...t});await(this.downloadedBytes?.close().catch(()=>{})),this.downloadedBytes=new n.Resource(d)}async install(e){if(!this.downloadedBytes)throw new Error("Update.install called before Update.download");await n.invoke("plugin:updater|install",{updateRid:this.rid,bytesRid:this.downloadedBytes.rid,...e}),this.downloadedBytes=void 0}async downloadAndInstall(e,t){a(t);const s=new n.Channel;e&&(s.onmessage=e),await n.invoke("plugin:updater|download_and_install",{onEvent:s,rid:this.rid,...t})}async close(){await(this.downloadedBytes?.close()),await super.close()}}function a(e){e?.headers&&(e.headers=Array.from(new Headers(e.headers).entries()))}return e.Update=t,e.check=async function(e){a(e);const s=await n.invoke("plugin:updater|check",{...e});return s?new t(s):null},e}({},window.__TAURI__.core);Object.defineProperty(window.__TAURI__,"updater",{value:__TAURI_PLUGIN_UPDATER__})}
if("__TAURI__"in window){var __TAURI_PLUGIN_UPDATER__=function(e,n){"use strict";class a extends n.Resource{constructor(e){super(e.rid),this.available=!0,this.currentVersion=e.currentVersion,this.version=e.version,this.date=e.date,this.body=e.body,this.rawJson=e.rawJson}async download(e,a){const s=new n.Channel;e&&(s.onmessage=e);const d=await n.invoke("plugin:updater|download",{onEvent:s,rid:this.rid,...a,headers:t(a?.headers)});await(this.downloadedBytes?.close().catch(()=>{})),this.downloadedBytes=new n.Resource(d)}async install(e){if(!this.downloadedBytes)throw new Error("Update.install called before Update.download");await n.invoke("plugin:updater|install",{updateRid:this.rid,bytesRid:this.downloadedBytes.rid,...e}),this.downloadedBytes=void 0}async downloadAndInstall(e,a){const s=new n.Channel;e&&(s.onmessage=e),await n.invoke("plugin:updater|download_and_install",{onEvent:s,rid:this.rid,...a,headers:t(a?.headers)})}async close(){await(this.downloadedBytes?.close()),await super.close()}}function t(e){return e?Array.from(new Headers(e).entries()):void 0}return e.Update=a,e.check=async function(e){const s=await n.invoke("plugin:updater|check",{...e,headers:t(e?.headers)});return s?new a(s):null},e}({},window.__TAURI__.core);Object.defineProperty(window.__TAURI__,"updater",{value:__TAURI_PLUGIN_UPDATER__})}
+12 -12
View File
@@ -155,7 +155,6 @@ class Update extends Resource {
onEvent?: (progress: DownloadEvent) => void,
options?: DownloadOptions
): Promise<void> {
convertToRustHeaders(options)
const channel = new Channel<DownloadEvent>()
if (onEvent) {
channel.onmessage = onEvent
@@ -163,7 +162,8 @@ class Update extends Resource {
const downloadedBytesRid = await invoke<number>('plugin:updater|download', {
onEvent: channel,
rid: this.rid,
...options
...options,
headers: toRustHeaders(options?.headers)
})
// release the bytes of a previous download, which would otherwise be kept
// in memory until the webview is destroyed
@@ -234,7 +234,6 @@ class Update extends Resource {
onEvent?: (progress: DownloadEvent) => void,
options?: DownloadOptions & InstallOptions
): Promise<void> {
convertToRustHeaders(options)
const channel = new Channel<DownloadEvent>()
if (onEvent) {
channel.onmessage = onEvent
@@ -242,7 +241,8 @@ class Update extends Resource {
await invoke('plugin:updater|download_and_install', {
onEvent: channel,
rid: this.rid,
...options
...options,
headers: toRustHeaders(options?.headers)
})
}
@@ -286,21 +286,21 @@ class Update extends Resource {
* @since 2.0.0
*/
async function check(options?: CheckOptions): Promise<Update | null> {
convertToRustHeaders(options)
const metadata = await invoke<UpdateMetadata | null>('plugin:updater|check', {
...options
...options,
headers: toRustHeaders(options?.headers)
})
return metadata ? new Update(metadata) : null
}
/**
* Converts the headers in options to be an {@linkcode Array<[string, string]>} which is what the Rust side expects
* Converts headers to an {@linkcode Array<[string, string]>}, which is what the Rust side expects,
* without modifying the caller's options.
*/
function convertToRustHeaders(options?: { headers?: HeadersInit }) {
if (options?.headers) {
options.headers = Array.from(new Headers(options.headers).entries())
}
function toRustHeaders(
headers?: HeadersInit
): Array<[string, string]> | undefined {
return headers ? Array.from(new Headers(headers).entries()) : undefined
}
export type { CheckOptions, DownloadOptions, DownloadEvent }