Skip to content

Commit

Permalink
fix(api.js) Replace number[]with Uint8Array. fixes #3306 (#3305)
Browse files Browse the repository at this point in the history
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
3 people committed Feb 5, 2022
1 parent f5109e0 commit 9b19a80
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changes/api-use-uint8array.md
@@ -0,0 +1,5 @@
---
"api": patch
---

**Breaking change:** Replaces all usages of `number[]` with `Uint8Array` to be closer aligned with the wider JS ecosystem.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tooling/api/src/event.ts
Expand Up @@ -110,7 +110,7 @@ async function once<T>(
* @returns
*/
async function emit(event: string, payload?: unknown): Promise<void> {
return emitEvent(event, null, payload)
return emitEvent(event, undefined, payload)
}

export type { Event, EventName, EventCallback, UnlistenFn }
Expand Down
6 changes: 4 additions & 2 deletions tooling/api/src/fs.ts
Expand Up @@ -122,15 +122,17 @@ async function readTextFile(
async function readBinaryFile(
filePath: string,
options: FsOptions = {}
): Promise<number[]> {
return invokeTauriCommand<number[]>({
): Promise<Uint8Array> {
const arr = await invokeTauriCommand<number[]>({
__tauriModule: 'Fs',
message: {
cmd: 'readFile',
path: filePath,
options
}
})

return Uint8Array.from(arr)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tooling/api/src/helpers/event.ts
Expand Up @@ -16,7 +16,7 @@ import { invokeTauriCommand } from './tauri'
async function emit(
event: string,
windowLabel?: WindowLabel,
payload?: string
payload?: unknown
): Promise<void> {
await invokeTauriCommand({
__tauriModule: 'Event',
Expand Down
16 changes: 12 additions & 4 deletions tooling/api/src/http.ts
Expand Up @@ -37,7 +37,7 @@ enum ResponseType {
Binary = 3
}

type Part = string | number[]
type Part = string | Uint8Array

/** The body object to be used on POST and PUT requests. */
class Body {
Expand All @@ -58,7 +58,14 @@ class Body {
* @return The body object ready to be used on the POST and PUT requests.
*/
static form(data: Record<string, Part>): Body {
return new Body('Form', data)
const form: Record<string, string | number[]> = {}
for (const key in data) {
// eslint-disable-next-line security/detect-object-injection
const v = data[key]
// eslint-disable-next-line security/detect-object-injection
form[key] = typeof v === 'string' ? v : Array.from(v)
}
return new Body('Form', form)
}

/**
Expand Down Expand Up @@ -90,8 +97,9 @@ class Body {
*
* @return The body object ready to be used on the POST and PUT requests.
*/
static bytes(bytes: number[]): Body {
return new Body('Bytes', bytes)
static bytes(bytes: Uint8Array): Body {
// stringifying Uint8Array doesn't return an array of numbers, so we create one here
return new Body('Bytes', Array.from(bytes))
}
}

Expand Down
5 changes: 3 additions & 2 deletions tooling/api/src/shell.ts
Expand Up @@ -149,13 +149,14 @@ class Child {
*
* @return A promise indicating the success or failure of the operation.
*/
async write(data: string | number[]): Promise<void> {
async write(data: string | Uint8Array): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Shell',
message: {
cmd: 'stdinWrite',
pid: this.pid,
buffer: data
// correctly serialize Uint8Arrays
buffer: typeof data === 'string' ? data : Array.from(data)
}
})
}
Expand Down
5 changes: 3 additions & 2 deletions tooling/api/src/window.ts
Expand Up @@ -1002,7 +1002,7 @@ class WindowManager extends WebviewWindowHandle {
* @param icon Icon bytes or path to the icon file.
* @returns A promise indicating the success or failure of the operation.
*/
async setIcon(icon: string | number[]): Promise<void> {
async setIcon(icon: string | Uint8Array): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Window',
message: {
Expand All @@ -1012,7 +1012,8 @@ class WindowManager extends WebviewWindowHandle {
cmd: {
type: 'setIcon',
payload: {
icon
// correctly serialize Uint8Arrays
icon: typeof icon === 'string' ? icon : Array.from(icon)
}
}
}
Expand Down

0 comments on commit 9b19a80

Please sign in to comment.