feat(api): add support to ArrayBuffer (#4579)

This commit is contained in:
Lucas Fernandes Nogueira
2022-07-05 17:40:36 -03:00
committed by GitHub
parent b02fc90f45
commit 92aca55a6f
4 changed files with 19 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
"api": patch
---
Add support to `ArrayBuffer` in `Body.bytes` and `writeBinaryFile`.

File diff suppressed because one or more lines are too long

View File

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

View File

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