Skip to content

Commit

Permalink
feat(api): add support to ArrayBuffer (#4579)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jul 5, 2022
1 parent b02fc90 commit 92aca55
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/api-support-array-buffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

Add support to `ArrayBuffer` in `Body.bytes` and `writeBinaryFile`.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions tooling/api/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ interface FsTextFileOption {
contents: string
}

type BinaryFileContents = Iterable<number> | ArrayLike<number>
type BinaryFileContents = Iterable<number> | ArrayLike<number> | ArrayBuffer

/** Options object used to write a binary data to a file. */
interface FsBinaryFileOption {
Expand Down Expand Up @@ -352,7 +352,11 @@ async function writeBinaryFile(
message: {
cmd: 'writeFile',
path: file.path,
contents: Array.from(file.contents),
contents: Array.from(
file.contents instanceof ArrayBuffer
? new Uint8Array(file.contents)
: file.contents
),
options: fileOptions
}
})
Expand Down
9 changes: 7 additions & 2 deletions tooling/api/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ class Body {
*
* @return The body object ready to be used on the POST and PUT requests.
*/
static bytes(bytes: Iterable<number> | ArrayLike<number>): Body {
static bytes(
bytes: Iterable<number> | ArrayLike<number> | ArrayBuffer
): Body {
// stringifying Uint8Array doesn't return an array of numbers, so we create one here
return new Body('Bytes', Array.from(bytes))
return new Body(
'Bytes',
Array.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes)
)
}
}

Expand Down

0 comments on commit 92aca55

Please sign in to comment.