various bugfixes

This commit is contained in:
ading2210 2024-02-04 03:43:19 -05:00
parent a5a537423e
commit 5f71e27422
3 changed files with 21 additions and 8 deletions

View file

@ -153,6 +153,9 @@ function merge_arrays(arrays) {
function create_response(response_data, response_info) { function create_response(response_data, response_info) {
response_info.ok = response_info.status >= 200 && response_info.status < 300; response_info.ok = response_info.status >= 200 && response_info.status < 300;
response_info.statusText = status_messages[response_info.status] || ""; response_info.statusText = status_messages[response_info.status] || "";
if (response_info.status === 204 || response_info.status === 205) {
response_data = null;
}
//construct base response object //construct base response object
let response_obj = new Response(response_data, response_info); let response_obj = new Response(response_data, response_info);

View file

@ -1,7 +1,7 @@
//class for custom websocket //class for custom websocket
class CurlWebSocket extends EventTarget { class CurlWebSocket extends EventTarget {
constructor(url, protocols=[]) { constructor(url, protocols=[], websocket_debug=false) {
super(); super();
check_loaded(true); check_loaded(true);
if (!url.startsWith("wss://") && !url.startsWith("ws://")) { if (!url.startsWith("wss://") && !url.startsWith("ws://")) {
@ -12,6 +12,7 @@ class CurlWebSocket extends EventTarget {
this.protocols = protocols; this.protocols = protocols;
this.binaryType = "blob"; this.binaryType = "blob";
this.recv_buffer = []; this.recv_buffer = [];
this.websocket_debug = websocket_debug;
//legacy event handlers //legacy event handlers
this.onopen = () => {}; this.onopen = () => {};
@ -33,7 +34,16 @@ class CurlWebSocket extends EventTarget {
let finish_callback = (error, response_info) => { let finish_callback = (error, response_info) => {
this.finish_callback(error, response_info); this.finish_callback(error, response_info);
} }
this.http_handle = perform_request(this.url, {}, data_callback, finish_callback, null); let options = {};
if (this.protocols) {
options.headers = {
"Sec-Websocket-Protocol": this.protocols.join(", "),
};
}
if (this.websocket_debug) {
options._libcurl_verbose = 1;
}
this.http_handle = perform_request(this.url, options, data_callback, finish_callback, null);
this.recv_loop(); this.recv_loop();
} }
@ -134,7 +144,7 @@ class CurlWebSocket extends EventTarget {
if (this.status === this.CLOSED) { if (this.status === this.CLOSED) {
return; return;
} }
let data_array; let data_array;
if (typeof data === "string") { if (typeof data === "string") {
data_array = new TextEncoder().encode(data); data_array = new TextEncoder().encode(data);
@ -153,15 +163,15 @@ class CurlWebSocket extends EventTarget {
if (ArrayBuffer.isView(data) && data instanceof DataView) { if (ArrayBuffer.isView(data) && data instanceof DataView) {
data_array = new Uint8Array(data.buffer); data_array = new Uint8Array(data.buffer);
} }
//regular typed arrays
else if (ArrayBuffer.isView(data)) {
data_array = Uint8Array.from(data);
}
//regular arraybuffers //regular arraybuffers
else { else {
data_array = new Uint8Array(data); data_array = new Uint8Array(data);
} }
} }
//regular typed arrays
else if (ArrayBuffer.isView(data)) {
data_array = Uint8Array.from(data);
}
else { else {
throw "invalid data type to be sent"; throw "invalid data type to be sent";
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "libcurl.js", "name": "libcurl.js",
"version": "0.3.1", "version": "0.3.2",
"description": "An experimental port of libcurl to WebAssembly for use in the browser.", "description": "An experimental port of libcurl to WebAssembly for use in the browser.",
"main": "libcurl.mjs", "main": "libcurl.mjs",
"scripts": { "scripts": {