Browse Source

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>
Jonas Kruckenberg 3 years ago
parent
commit
9b19a805aa

+ 5 - 0
.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.

File diff suppressed because it is too large
+ 0 - 0
core/tauri/scripts/bundle.js


+ 1 - 1
tooling/api/src/event.ts

@@ -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 }

+ 4 - 2
tooling/api/src/fs.ts

@@ -122,8 +122,8 @@ 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',
@@ -131,6 +131,8 @@ async function readBinaryFile(
       options
     }
   })
+
+  return Uint8Array.from(arr)
 }
 
 /**

+ 1 - 1
tooling/api/src/helpers/event.ts

@@ -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',

+ 12 - 4
tooling/api/src/http.ts

@@ -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 {
@@ -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)
   }
 
   /**
@@ -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))
   }
 }
 

+ 3 - 2
tooling/api/src/shell.ts

@@ -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)
       }
     })
   }

+ 3 - 2
tooling/api/src/window.ts

@@ -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: {
@@ -1012,7 +1012,8 @@ class WindowManager extends WebviewWindowHandle {
           cmd: {
             type: 'setIcon',
             payload: {
-              icon
+              // correctly serialize Uint8Arrays
+              icon: typeof icon === 'string' ? icon : Array.from(icon)
             }
           }
         }

Some files were not shown because too many files changed in this diff