diff --git a/README.md b/README.md index 43b6bb8..031a1cb 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ git clone https://github.com/ading2210/libcurl.js cd libcurl.js/client ./build.sh ``` -Make sure you have emscripten, git, and the various C build tools installed. The build script will generate `client/out/libcurl.js`. +Make sure you have emscripten, git, and the various C build tools installed. The build script will generate `client/out/libcurl.js` as well as `client/out/libcurl_module.mjs`, which is an ES6 module. ## Javascript API: @@ -41,6 +41,12 @@ let r = await libcurl.fetch("https://ading.dev"); console.log(await r.text()); ``` +### Changing the Websocket URL: +You can change the URL of the websocket proxy by using `libcurl.set_websocket`. +```js +libcurl.set_websocket("ws://localhost:6001/"); +``` + ## Proxy Server: The proxy server consists of a [SOCKS5 proxy server](https://github.com/Amaindex/asyncio-socks-server) behind a [websocket TCP proxy](https://github.com/novnc/websockify). diff --git a/client/build.sh b/client/build.sh index a894253..57dfd04 100755 --- a/client/build.sh +++ b/client/build.sh @@ -5,7 +5,8 @@ set -e INCLUDE_DIR="build/curl-wasm/include/" LIB_DIR="build/curl-wasm/lib/" OUT_FILE="out/libcurl.js" -MODULE_FILE="out/libcurl_module.js" +ES6_FILE="out/libcurl_module.mjs" +MODULE_FILE="out/emscripten_compiled.js" WRAPPER_SOURCE="main.js" EXPORTED_FUNCS="_load_certs,_perform_request" @@ -13,6 +14,10 @@ RUNTIME_METHODS="addFunction,removeFunction,allocate,ALLOC_NORMAL" COMPILER_OPTIONS="-o $MODULE_FILE -lcurl -lssl -lcrypto -lcjson -lz -lbrotlidec -lbrotlicommon -I $INCLUDE_DIR -L $LIB_DIR" EMSCRIPTEN_OPTIONS="-lwebsocket.js -sSINGLE_FILE -sASYNCIFY -sALLOW_TABLE_GROWTH -sEXPORTED_FUNCTIONS=$EXPORTED_FUNCS -sEXPORTED_RUNTIME_METHODS=$RUNTIME_METHODS" +if [ "$1" = "release" ]; then + COMPILER_OPTIONS="-O3 $COMPILER_OPTIONS" +fi + #ensure deps are compiled tools/all_deps.sh tools/generate_cert.sh @@ -32,4 +37,9 @@ sed -i 's/function _emscripten_console_error(str) {/& if(UTF8ToString(str).endsW #merge compiled emscripten module and wrapper code cp $WRAPPER_SOURCE $OUT_FILE -sed -i "/__emscripten_output__/r $MODULE_FILE" $OUT_FILE \ No newline at end of file +sed -i "/__emscripten_output__/r $MODULE_FILE" $OUT_FILE +rm $MODULE_FILE + +#generate es6 module +cp $OUT_FILE $ES6_FILE +sed -i 's/window.libcurl/export const libcurl/' $ES6_FILE \ No newline at end of file diff --git a/client/main.c b/client/main.c index c7e1036..9e7f7fc 100644 --- a/client/main.c +++ b/client/main.c @@ -103,6 +103,7 @@ void perform_request(const char* url, const char* json_params, DataCallback data curl_multi_add_handle(multi_handle, http_handle); CURLMcode mc; + struct CURLMsg *m; do { mc = curl_multi_perform(multi_handle, &still_running); @@ -114,12 +115,15 @@ void perform_request(const char* url, const char* json_params, DataCallback data break; } + int msgq = 0; + m = curl_multi_info_read(multi_handle, &msgq); + //ensure we dont block the main thread emscripten_sleep(0); } while(still_running); - int error = (int) mc; + int error = (int) m->data.result; long response_code; curl_easy_getinfo(http_handle, CURLINFO_RESPONSE_CODE, &response_code); diff --git a/client/main.js b/client/main.js index b55c29c..d8ff917 100644 --- a/client/main.js +++ b/client/main.js @@ -96,6 +96,7 @@ class Headers { } } this[prop] = value; + return true; } } @@ -214,6 +215,7 @@ function libcurl_fetch(url, params={}) { let finish_callback = (error, response_info) => { if (error != 0) { reject("libcurl.js encountered an error: " + error); + return; } let response_data = merge_arrays(chunks); let response_obj = create_response(response_data, response_info);