mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-13 14:30:02 -04:00
74 lines
No EOL
1.7 KiB
JavaScript
74 lines
No EOL
1.7 KiB
JavaScript
//a case insensitive dictionary for request headers
|
|
class HeadersDict {
|
|
constructor(obj) {
|
|
for (let key in obj) {
|
|
this[key] = obj[key];
|
|
}
|
|
return new Proxy(this, this);
|
|
}
|
|
get(target, prop) {
|
|
let keys = Object.keys(this);
|
|
for (let key of keys) {
|
|
if (key.toLowerCase() === prop.toLowerCase()) {
|
|
return this[key];
|
|
}
|
|
}
|
|
}
|
|
set(target, prop, value) {
|
|
let keys = Object.keys(this);
|
|
for (let key of keys) {
|
|
if (key.toLowerCase() === prop.toLowerCase()) {
|
|
this[key] = value;
|
|
}
|
|
}
|
|
this[prop] = value;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function allocate_str(str) {
|
|
return allocate(intArrayFromString(str), ALLOC_NORMAL);
|
|
}
|
|
|
|
function allocate_array(array) {
|
|
return allocate(array, ALLOC_NORMAL);
|
|
}
|
|
|
|
function get_error_str(error_code) {
|
|
let error_ptr = _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) {
|
|
//data already in correct type
|
|
if (data instanceof Uint8Array) {
|
|
return data;
|
|
}
|
|
|
|
else if (typeof data === "string") {
|
|
return new TextEncoder().encode(data);
|
|
}
|
|
|
|
else if (data instanceof ArrayBuffer) {
|
|
return new Uint8Array(data);
|
|
}
|
|
|
|
//dataview objects or any other typedarray
|
|
else if (ArrayBuffer.isView(data)) {
|
|
return new Uint8Array(data.buffer);
|
|
}
|
|
|
|
throw "invalid data type to be sent";
|
|
} |