1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/**
* @typedef {{callback: string, error: string, data: *}} IsolationPayload - a valid isolation payload
*/
;(function () {
/**
* @type {string}
*/
const pattern = window.__TAURI_INTERNALS__.__TAURI_PATTERN__.pattern
/**
* @type {string}
*/
const isolationOrigin = __TEMPLATE_isolation_origin__
/**
* @type {{queue: object[], ready: boolean, frame: HTMLElement | null}}
*/
const isolation = Object.create(null)
isolation.queue = []
isolation.ready = false
isolation.frame = null
/**
* Detects if a message event is a valid isolation message.
*
* @param {MessageEvent<object>} event - a message event that is expected to be an isolation message
* @return {boolean} - if the event was a valid isolation message
*/
function isIsolationMessage(event) {
if (
typeof event.data === 'object'
&& typeof event.data.payload === 'object'
) {
const keys = Object.keys(event.data.payload || {})
return (
keys.length > 0
&& keys.every(
(key) => key === 'contentType' || key === 'nonce' || key === 'payload'
)
)
}
return false
}
/**
* Detects if data is able to transform into an isolation payload.
*
* @param {object} data - object that is expected to contain at least a callback and error identifier
* @return {boolean} - if the data is able to transform into an isolation payload
*/
function isIsolationPayload(data) {
return (
typeof data === 'object'
&& 'callback' in data
&& 'error' in data
&& !isIsolationMessage(data)
)
}
/**
* Sends a properly formatted message to the isolation frame.
*
* @param {IsolationPayload} data - data that has been validated to be an isolation payload
*/
function sendIsolationMessage(data) {
// set the frame dom element if it's not been set before
if (!isolation.frame) {
const frame = document.querySelector('iframe#__tauri_isolation__')
if (frame.src.startsWith(isolationOrigin)) {
isolation.frame = frame
} else {
console.error(
'Tauri IPC found an isolation iframe, but it had the wrong origin'
)
}
}
// ensure we have the target to send the message to
if (!isolation.frame || !isolation.frame.contentWindow) {
console.error(
'Tauri "Isolation" Pattern could not find the Isolation iframe window'
)
return
}
// `postMessage` uses `structuredClone` to serialize the data before sending it
// unlike `JSON.stringify`, we can't extend a type with a method similar to `toJSON`
// so that `structuredClone` would use, so until https://github.com/whatwg/html/issues/7428
// we manually call `toIPC`
function serializeIpcPayload(data) {
// if this value changes, make sure to update it in:
// 1. process-ipc-message-fn.js
// 2. core.ts
const SERIALIZE_TO_IPC_FN = '__TAURI_TO_IPC_KEY__'
if (
typeof data === 'object'
&& data !== null
&& 'constructor' in data
&& data.constructor === Array
) {
return data.map((v) => serializeIpcPayload(v))
}
if (
typeof data === 'object'
&& data !== null
&& SERIALIZE_TO_IPC_FN in data
) {
return data[SERIALIZE_TO_IPC_FN]()
}
if (
typeof data === 'object'
&& data !== null
&& 'constructor' in data
&& data.constructor === Object
) {
const acc = {}
Object.entries(data).forEach(([k, v]) => {
acc[k] = serializeIpcPayload(v)
})
return acc
}
return data
}
data.payload = serializeIpcPayload(data.payload)
isolation.frame.contentWindow.postMessage(
data,
'*' /* todo: set this to the secure origin */
)
}
Object.defineProperty(window.__TAURI_INTERNALS__, 'ipc', {
// todo: JSDoc this function
value: Object.freeze((message) => {
switch (pattern) {
case 'brownfield':
window.__TAURI_INTERNALS__.postMessage(message)
break
case 'isolation':
if (!isIsolationPayload(message)) {
console.error(
'Tauri "Isolation" Pattern found an invalid isolation message payload',
message
)
break
}
if (isolation.ready) {
sendIsolationMessage(message)
} else {
isolation.queue.push(message)
}
break
case 'error':
console.error(
'Tauri IPC found a Tauri Pattern, but it was an error. Check for other log messages to find the cause.'
)
break
default:
console.error(
'Tauri IPC did not find a Tauri Pattern that it understood.'
)
break
}
})
})
/**
* IMPORTANT: See isolation_secure.js for the isolation frame implementation.
* main frame -> isolation frame = isolation payload
* isolation frame -> main frame = isolation message
*/
if (pattern === 'isolation') {
window.addEventListener(
'message',
(event) => {
// watch for the isolation frame being ready and flush any queued messages
if (event.data === '__TAURI_ISOLATION_READY__') {
isolation.ready = true
for (const message of isolation.queue) {
sendIsolationMessage(message)
}
isolation.queue = []
return
}
if (isIsolationMessage(event)) {
window.__TAURI_INTERNALS__.postMessage(event.data)
}
},
false
)
}
})()