Skip to content

Commit 97e3341

Browse files
authored
fix(api.js): mock __TAURI_INTERNALS__ in mockIPC and mockWindows (#8534)
* fix(api.js): mock `__TAURI_INTERNALS__` in `mockIPC` and `mockWindows` * mock internals.invoke instead of internals.ipc
1 parent 0ec28c3 commit 97e3341

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'patch:bug'
3+
---
4+
5+
`mockIPC` and `mockWindows` no longer crash if `window.__TAURI_INTERNALS__` is undefined.

tooling/api/src/mocks.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
interface IPCMessage {
6-
cmd: string
7-
callback: number
8-
error: number
9-
[key: string]: unknown
5+
import { InvokeArgs, InvokeOptions } from './core'
6+
7+
function mockInternals() {
8+
window.__TAURI_INTERNALS__ = window.__TAURI_INTERNALS__ ?? {}
109
}
1110

1211
/**
@@ -63,24 +62,41 @@ interface IPCMessage {
6362
* @since 1.0.0
6463
*/
6564
export function mockIPC(
66-
cb: (cmd: string, payload: Record<string, unknown>) => unknown
65+
cb: <T>(cmd: string, payload?: InvokeArgs) => Promise<T>
6766
): void {
68-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
69-
window.__TAURI_INTERNALS__.ipc = async ({
70-
cmd,
71-
callback,
72-
error,
73-
payload
74-
}: IPCMessage) => {
75-
try {
76-
// @ts-expect-error The function key is dynamic and therefore not typed
77-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
78-
window[`_${callback}`](await cb(cmd, payload))
79-
} catch (err) {
80-
// @ts-expect-error The function key is dynamic and therefore not typed
81-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
82-
window[`_${error}`](err)
83-
}
67+
mockInternals()
68+
69+
window.__TAURI_INTERNALS__.transformCallback = function transformCallback(
70+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
71+
callback?: (response: any) => void,
72+
once = false
73+
) {
74+
const identifier = window.crypto.getRandomValues(new Uint32Array(1))[0]
75+
const prop = `_${identifier}`
76+
77+
Object.defineProperty(window, prop, {
78+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
79+
value: (result: any) => {
80+
if (once) {
81+
Reflect.deleteProperty(window, prop)
82+
}
83+
84+
return callback && callback(result)
85+
},
86+
writable: false,
87+
configurable: true
88+
})
89+
90+
return identifier
91+
}
92+
93+
window.__TAURI_INTERNALS__.invoke = function <T>(
94+
cmd: string,
95+
args?: InvokeArgs,
96+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
97+
options?: InvokeOptions
98+
): Promise<T> {
99+
return cb(cmd, args)
84100
}
85101
}
86102

@@ -128,6 +144,7 @@ export function mockWindows(
128144
current: string,
129145
...additionalWindows: string[]
130146
): void {
147+
mockInternals()
131148
window.__TAURI_INTERNALS__.metadata = {
132149
windows: [current, ...additionalWindows].map((label) => ({ label })),
133150
currentWindow: { label: current }
@@ -153,7 +170,7 @@ export function mockWindows(
153170
* @since 1.6.0
154171
*/
155172
export function mockConvertFileSrc(osName: string): void {
156-
window.__TAURI_INTERNALS__ = window.__TAURI_INTERNALS__ ?? {}
173+
mockInternals()
157174
window.__TAURI_INTERNALS__.convertFileSrc = function (
158175
filePath,
159176
protocol = 'asset'
@@ -199,9 +216,9 @@ export function clearMocks(): void {
199216
if (window.__TAURI_INTERNALS__?.convertFileSrc)
200217
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
201218
delete window.__TAURI_INTERNALS__.convertFileSrc
202-
if (window.__TAURI_INTERNALS__?.ipc)
219+
if (window.__TAURI_INTERNALS__?.invoke)
203220
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
204-
delete window.__TAURI_INTERNALS__.ipc
221+
delete window.__TAURI_INTERNALS__.invoke
205222
if (window.__TAURI_INTERNALS__?.metadata)
206223
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
207224
delete window.__TAURI_INTERNALS__.metadata

0 commit comments

Comments
 (0)