mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-13 14:30:02 -04:00
support all request body types
This commit is contained in:
parent
997b2afca4
commit
e2b50db5fa
6 changed files with 56 additions and 60 deletions
|
@ -39,18 +39,13 @@ function get_error_str(error_code) {
|
|||
return UTF8ToString(error_ptr);
|
||||
}
|
||||
|
||||
//convert any data to a uint8array
|
||||
async function data_to_array(data) {
|
||||
//convert various data types to a uint8array (blobs excluded)
|
||||
function data_to_array(data) {
|
||||
let data_array = null;
|
||||
if (typeof data === "string") {
|
||||
data_array = new TextEncoder().encode(data);
|
||||
}
|
||||
|
||||
else if (data instanceof Blob) {
|
||||
let array_buffer = await data.arrayBuffer();
|
||||
data_array = new Uint8Array(array_buffer);
|
||||
}
|
||||
|
||||
//any typedarray
|
||||
else if (data instanceof ArrayBuffer) {
|
||||
//dataview objects
|
||||
|
@ -67,16 +62,9 @@ async function data_to_array(data) {
|
|||
}
|
||||
}
|
||||
|
||||
else if (data instanceof ReadableStream) {
|
||||
let chunks = [];
|
||||
for await (let chunk of data) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
data_array = merge_arrays(chunks);
|
||||
}
|
||||
|
||||
else {
|
||||
throw "invalid data type to be sent";
|
||||
}
|
||||
|
||||
return data_array;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue