From 5f71e274222eb78410bc2cf611664be8f2096705 Mon Sep 17 00:00:00 2001 From: ading2210 Date: Sun, 4 Feb 2024 03:43:19 -0500 Subject: [PATCH] various bugfixes --- client/javascript/main.js | 3 +++ client/javascript/websocket.js | 24 +++++++++++++++++------- client/package.json | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/client/javascript/main.js b/client/javascript/main.js index 9ecc78c..ed4453c 100644 --- a/client/javascript/main.js +++ b/client/javascript/main.js @@ -153,6 +153,9 @@ function merge_arrays(arrays) { function create_response(response_data, response_info) { response_info.ok = response_info.status >= 200 && response_info.status < 300; response_info.statusText = status_messages[response_info.status] || ""; + if (response_info.status === 204 || response_info.status === 205) { + response_data = null; + } //construct base response object let response_obj = new Response(response_data, response_info); diff --git a/client/javascript/websocket.js b/client/javascript/websocket.js index 76ee79e..9f7b272 100644 --- a/client/javascript/websocket.js +++ b/client/javascript/websocket.js @@ -1,7 +1,7 @@ //class for custom websocket class CurlWebSocket extends EventTarget { - constructor(url, protocols=[]) { + constructor(url, protocols=[], websocket_debug=false) { super(); check_loaded(true); if (!url.startsWith("wss://") && !url.startsWith("ws://")) { @@ -12,6 +12,7 @@ class CurlWebSocket extends EventTarget { this.protocols = protocols; this.binaryType = "blob"; this.recv_buffer = []; + this.websocket_debug = websocket_debug; //legacy event handlers this.onopen = () => {}; @@ -33,7 +34,16 @@ class CurlWebSocket extends EventTarget { let 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(); } @@ -134,7 +144,7 @@ class CurlWebSocket extends EventTarget { if (this.status === this.CLOSED) { return; } - + let data_array; if (typeof data === "string") { data_array = new TextEncoder().encode(data); @@ -153,15 +163,15 @@ class CurlWebSocket extends EventTarget { if (ArrayBuffer.isView(data) && data instanceof DataView) { data_array = new Uint8Array(data.buffer); } - //regular typed arrays - else if (ArrayBuffer.isView(data)) { - data_array = Uint8Array.from(data); - } //regular arraybuffers else { data_array = new Uint8Array(data); } } + //regular typed arrays + else if (ArrayBuffer.isView(data)) { + data_array = Uint8Array.from(data); + } else { throw "invalid data type to be sent"; } diff --git a/client/package.json b/client/package.json index 5edc336..9f7a878 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "libcurl.js", - "version": "0.3.1", + "version": "0.3.2", "description": "An experimental port of libcurl to WebAssembly for use in the browser.", "main": "libcurl.mjs", "scripts": {