Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api.js) Replace number[]with Uint8Array. fixes #3306 #3305

Merged
merged 9 commits into from
Feb 5, 2022
5 changes: 5 additions & 0 deletions .changes/api-use-uint8array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

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.

6 changes: 4 additions & 2 deletions tooling/api/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,17 @@ async function readTextFile(
async function readBinaryFile(
filePath: string,
options: FsOptions = {}
): Promise<number[]> {
return invokeTauriCommand<number[]>({
): Promise<Uint8Array> {
const arr = invokeTauriCommand<number[]>({
JonasKruckenberg marked this conversation as resolved.
Show resolved Hide resolved
__tauriModule: 'Fs',
message: {
cmd: 'readBinaryFile',
path: filePath,
options
}
})

return Uint8Array.from(arr)
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tooling/api/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum ResponseType {
Binary = 3
}

type Part = string | number[]
type Part = string | Uint8Array
lucasfernog marked this conversation as resolved.
Show resolved Hide resolved

/** The body object to be used on POST and PUT requests. */
class Body {
Expand Down Expand Up @@ -90,8 +90,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
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,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
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,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 @@ -1016,7 +1016,8 @@ class WindowManager extends WebviewWindowHandle {
cmd: {
type: 'setIcon',
payload: {
icon
// correctly serialize Uint8Arrays
icon: typeof icon === 'string' ? icon : Array.from(icon)
}
}
}
Expand Down