mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-13 14:30:02 -04:00
fix some logic with converting arraybuffers
This commit is contained in:
parent
e2b50db5fa
commit
954ce1f60b
2 changed files with 24 additions and 20 deletions
|
@ -160,7 +160,7 @@ The `TLSSocket.send` function can be used to send data to the socket. The only a
|
|||
let socket = new libcurl.TLSSocket("ading.dev", 443, {verbose: 1});
|
||||
socket.onopen = () => {
|
||||
console.log("socket connected!");
|
||||
let str = "GET /all HTTP/1.1\r\nHost: ading.dev\r\nConnection: close\r\n\r\n";
|
||||
let str = "GET / HTTP/1.1\r\nHost: ading.dev\r\nConnection: close\r\n\r\n";
|
||||
socket.send(new TextEncoder().encode(str));
|
||||
};
|
||||
socket.onmessage = (data) => {
|
||||
|
|
|
@ -39,32 +39,36 @@ function get_error_str(error_code) {
|
|||
return UTF8ToString(error_ptr);
|
||||
}
|
||||
|
||||
function merge_arrays(arrays) {
|
||||
let total_len = arrays.reduce((acc, val) => acc + val.length, 0);
|
||||
let new_array = new Uint8Array(total_len);
|
||||
let offset = 0;
|
||||
for (let array of arrays) {
|
||||
new_array.set(array, offset);
|
||||
offset += array.length;
|
||||
}
|
||||
return new_array;
|
||||
}
|
||||
|
||||
//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);
|
||||
//data already in correct type
|
||||
if (data instanceof Uint8Array) {
|
||||
return data;
|
||||
}
|
||||
|
||||
else if (typeof data === "string") {
|
||||
return new TextEncoder().encode(data);
|
||||
}
|
||||
|
||||
//any typedarray
|
||||
else if (data instanceof ArrayBuffer) {
|
||||
//dataview objects
|
||||
if (ArrayBuffer.isView(data) && data instanceof DataView) {
|
||||
data_array = new Uint8Array(data.buffer);
|
||||
return new Uint8Array(data);
|
||||
}
|
||||
//regular typed arrays
|
||||
|
||||
//dataview objects or any other typedarray
|
||||
else if (ArrayBuffer.isView(data)) {
|
||||
data_array = Uint8Array.from(data);
|
||||
}
|
||||
//regular arraybuffers
|
||||
else {
|
||||
data_array = new Uint8Array(data);
|
||||
}
|
||||
return new Uint8Array(data.buffer);
|
||||
}
|
||||
|
||||
else {
|
||||
throw "invalid data type to be sent";
|
||||
}
|
||||
|
||||
return data_array;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue