Skip to content

Commit 9b19a80

Browse files
JonasKruckenbergamrbashirlucasfernog
authored
fix(api.js) Replace number[]with Uint8Array. fixes #3306 (#3305)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com> Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent f5109e0 commit 9b19a80

File tree

8 files changed

+30
-13
lines changed

8 files changed

+30
-13
lines changed

.changes/api-use-uint8array.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
**Breaking change:** Replaces all usages of `number[]` with `Uint8Array` to be closer aligned with the wider JS ecosystem.

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/api/src/event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async function once<T>(
110110
* @returns
111111
*/
112112
async function emit(event: string, payload?: unknown): Promise<void> {
113-
return emitEvent(event, null, payload)
113+
return emitEvent(event, undefined, payload)
114114
}
115115

116116
export type { Event, EventName, EventCallback, UnlistenFn }

tooling/api/src/fs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,17 @@ async function readTextFile(
122122
async function readBinaryFile(
123123
filePath: string,
124124
options: FsOptions = {}
125-
): Promise<number[]> {
126-
return invokeTauriCommand<number[]>({
125+
): Promise<Uint8Array> {
126+
const arr = await invokeTauriCommand<number[]>({
127127
__tauriModule: 'Fs',
128128
message: {
129129
cmd: 'readFile',
130130
path: filePath,
131131
options
132132
}
133133
})
134+
135+
return Uint8Array.from(arr)
134136
}
135137

136138
/**

tooling/api/src/helpers/event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { invokeTauriCommand } from './tauri'
1616
async function emit(
1717
event: string,
1818
windowLabel?: WindowLabel,
19-
payload?: string
19+
payload?: unknown
2020
): Promise<void> {
2121
await invokeTauriCommand({
2222
__tauriModule: 'Event',

tooling/api/src/http.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ enum ResponseType {
3737
Binary = 3
3838
}
3939

40-
type Part = string | number[]
40+
type Part = string | Uint8Array
4141

4242
/** The body object to be used on POST and PUT requests. */
4343
class Body {
@@ -58,7 +58,14 @@ class Body {
5858
* @return The body object ready to be used on the POST and PUT requests.
5959
*/
6060
static form(data: Record<string, Part>): Body {
61-
return new Body('Form', data)
61+
const form: Record<string, string | number[]> = {}
62+
for (const key in data) {
63+
// eslint-disable-next-line security/detect-object-injection
64+
const v = data[key]
65+
// eslint-disable-next-line security/detect-object-injection
66+
form[key] = typeof v === 'string' ? v : Array.from(v)
67+
}
68+
return new Body('Form', form)
6269
}
6370

6471
/**
@@ -90,8 +97,9 @@ class Body {
9097
*
9198
* @return The body object ready to be used on the POST and PUT requests.
9299
*/
93-
static bytes(bytes: number[]): Body {
94-
return new Body('Bytes', bytes)
100+
static bytes(bytes: Uint8Array): Body {
101+
// stringifying Uint8Array doesn't return an array of numbers, so we create one here
102+
return new Body('Bytes', Array.from(bytes))
95103
}
96104
}
97105

tooling/api/src/shell.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ class Child {
149149
*
150150
* @return A promise indicating the success or failure of the operation.
151151
*/
152-
async write(data: string | number[]): Promise<void> {
152+
async write(data: string | Uint8Array): Promise<void> {
153153
return invokeTauriCommand({
154154
__tauriModule: 'Shell',
155155
message: {
156156
cmd: 'stdinWrite',
157157
pid: this.pid,
158-
buffer: data
158+
// correctly serialize Uint8Arrays
159+
buffer: typeof data === 'string' ? data : Array.from(data)
159160
}
160161
})
161162
}

tooling/api/src/window.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ class WindowManager extends WebviewWindowHandle {
10021002
* @param icon Icon bytes or path to the icon file.
10031003
* @returns A promise indicating the success or failure of the operation.
10041004
*/
1005-
async setIcon(icon: string | number[]): Promise<void> {
1005+
async setIcon(icon: string | Uint8Array): Promise<void> {
10061006
return invokeTauriCommand({
10071007
__tauriModule: 'Window',
10081008
message: {
@@ -1012,7 +1012,8 @@ class WindowManager extends WebviewWindowHandle {
10121012
cmd: {
10131013
type: 'setIcon',
10141014
payload: {
1015-
icon
1015+
// correctly serialize Uint8Arrays
1016+
icon: typeof icon === 'string' ? icon : Array.from(icon)
10161017
}
10171018
}
10181019
}

0 commit comments

Comments
 (0)