add option to capture stdout

This commit is contained in:
ading2210 2024-02-02 23:11:59 +00:00
parent bb9d9239c0
commit 3c517bbaf5
11 changed files with 64 additions and 20 deletions

View file

@ -20,6 +20,10 @@ jobs:
working-directory: ./client working-directory: ./client
run: ./build.sh all run: ./build.sh all
- name: run tests
working-directory: ./client
run: ./tests/run.sh || true
- name: upload img - name: upload img
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:

View file

@ -84,7 +84,17 @@ You can change the URL of the websocket proxy by using `libcurl.set_websocket`.
```js ```js
libcurl.set_websocket("ws://localhost:6001/"); libcurl.set_websocket("ws://localhost:6001/");
``` ```
If the websocket proxy URL is not set and one of the other API functions is called, an error will be thrown. If the websocket proxy URL is not set and one of the other API functions is called, an error will be thrown. Note that this URL must end with a trailing slash.
### Getting Libcurl's Output:
If you want more information about a connection, you can pass the `_libcurl_verbose` argument to the `libcurl.fetch` function.
```js
await libcurl.fetch("https://example.com", {_libcurl_verbose: 1});
```
By default this will print the output to the browser console, but you can set `libcurl.stdout` and `libcurl.stderr` to intercept these messages. This callback will be executed on every line of text that libcurl outputs.
```js
libcurl.stderr = (text) => {document.body.innerHTML += text};
```
## Proxy Server: ## Proxy Server:
The proxy server consists of a standard [Wisp](https://github.com/MercuryWorkshop/wisp-protocol) server, allowing multiple TCP connections to share the same websocket. The proxy server consists of a standard [Wisp](https://github.com/MercuryWorkshop/wisp-protocol) server, allowing multiple TCP connections to share the same websocket.
@ -93,7 +103,7 @@ To host the proxy server, run the following commands:
``` ```
git clone https://github.com/ading2210/libcurl.js --recursive git clone https://github.com/ading2210/libcurl.js --recursive
cd libcurl.js cd libcurl.js
STATIC=$(pwd)/client server/run.sh server/run.sh --static=./client
``` ```
You can use the `HOST` and `PORT` environment variables to control the hostname and port that the proxy server listens on. You can use the `HOST` and `PORT` environment variables to control the hostname and port that the proxy server listens on.

View file

@ -3,7 +3,7 @@
<head> <head>
<link rel="icon" href="data:;base64,="> <link rel="icon" href="data:;base64,=">
<script defer src="./out/libcurl_full.js"></script> <script defer src="./out/libcurl.js" onload="libcurl.load_wasm('/out/libcurl.wasm');"></script>
<script> <script>
document.addEventListener("libcurl_load", ()=>{ document.addEventListener("libcurl_load", ()=>{
libcurl.set_websocket(`wss://${location.hostname}/ws/`); libcurl.set_websocket(`wss://${location.hostname}/ws/`);

View file

@ -294,7 +294,11 @@ return {
set_websocket: set_websocket_url, set_websocket: set_websocket_url,
load_wasm: load_wasm, load_wasm: load_wasm,
wisp: _wisp_connections, wisp: _wisp_connections,
WebSocket: CurlWebSocket WebSocket: CurlWebSocket,
get stdout() {return out},
set stdout(callback) {out = callback},
get stderr() {return err},
set stderr(callback) {err = callback}
} }
})() })()

View file

@ -1,6 +1,6 @@
{ {
"name": "libcurl.js", "name": "libcurl.js",
"version": "0.1.2", "version": "0.2.0",
"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": {

View file

@ -18,15 +18,12 @@
} }
} }
let script = document.createElement("script");
script.src = "/tests/scripts/" + location.hash.substring(1);
script.async = false;
script.defer = false;
document.head.append(script);
document.addEventListener("libcurl_load", async ()=>{ document.addEventListener("libcurl_load", async ()=>{
try { try {
libcurl.set_websocket(`ws://localhost:6001/ws/`); libcurl.set_websocket(`ws://localhost:6001/ws/`);
let r = await fetch("/tests/scripts/" + location.hash.substring(1));
eval(await r.text());
await test(); await test();
create_flag("success"); create_flag("success");
} }

View file

@ -4,8 +4,23 @@ set -e
trap "exit" INT TERM trap "exit" INT TERM
trap "kill 0" EXIT trap "kill 0" EXIT
STATIC="$(pwd)" ../server/run.sh >/dev/null & ../server/run.sh --static=$(pwd) >/dev/null &
echo -n "waiting for wisp server to start"
i=0
until $(curl --output /dev/null --silent --head "http://localhost:6001/"); do
if [ "$i" = "30" ]; then
echo -e "\ntests failed. wisp server failed to start"
exit 1
fi
echo -n "."
i=$(($i+1))
sleep 1
done
echo
sleep 1 sleep 1
echo "Running tests" echo "wisp server ready, running tests"
python3 tests/run_tests.py python3 tests/run_tests.py

View file

@ -14,8 +14,8 @@ class JSTest(unittest.TestCase):
options.add_argument("--headless") options.add_argument("--headless")
options.add_argument("--no-sandbox") options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-dev-shm-usage")
options.addArguments("--disable-browser-side-navigation") options.add_argument("--disable-browser-side-navigation")
options.addArguments("--disable-gpu") options.add_argument("--disable-gpu")
options.set_capability("goog:loggingPrefs", {"browser": "ALL"}) options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
self.browser = webdriver.Chrome(options=options) self.browser = webdriver.Chrome(options=options)
@ -46,5 +46,8 @@ class JSTest(unittest.TestCase):
def test_websocket(self): def test_websocket(self):
self.run_test("test_websocket.js") self.run_test("test_websocket.js")
def test_redirect_out(self):
self.run_test("redirect_out.js")
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View file

@ -0,0 +1,11 @@
async function test() {
let output = [];
function out_callback(text) {
output.push(text);
}
libcurl.stdout = out_callback;
libcurl.stderr = out_callback;
await libcurl.fetch("https://example.com/", {_libcurl_verbose: 1});
console.log(output);
assert(output[0] === "* Host example.com:443 was resolved.", "unexpected output in stderr");
}

View file

@ -6,15 +6,15 @@ set -e
SCRIPT_PATH=$(realpath $0) SCRIPT_PATH=$(realpath $0)
BASE_PATH=$(dirname $SCRIPT_PATH) BASE_PATH=$(dirname $SCRIPT_PATH)
SERVER_PATH="$BASE_PATH/wisp_server"
cd $BASE_PATH/wisp_server if [ ! -d "$SERVER_PATH.venv" ]; then
if [ ! -d ".venv" ]; then python3 -m venv $SERVER_PATH/.venv
python3 -m venv .venv
fi fi
source .venv/bin/activate source $SERVER_PATH/.venv/bin/activate
if ! python3 -c "import websockets" 2> /dev/null; then if ! python3 -c "import websockets" 2> /dev/null; then
pip3 install -r requirements.txt pip3 install -r requirements.txt
fi fi
python3 main.py python3 $SERVER_PATH/main.py "$@"

@ -1 +1 @@
Subproject commit bd7bca97b22023a1b49d0f5a09081ddcbb4ad49c Subproject commit 65d312414aa896d8f9e18f48e8fe37b72d75ee5c