mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-12 05:50:01 -04:00
add option to capture stdout
This commit is contained in:
parent
bb9d9239c0
commit
3c517bbaf5
11 changed files with 64 additions and 20 deletions
4
.github/workflows/build.yaml
vendored
4
.github/workflows/build.yaml
vendored
|
@ -20,6 +20,10 @@ jobs:
|
|||
working-directory: ./client
|
||||
run: ./build.sh all
|
||||
|
||||
- name: run tests
|
||||
working-directory: ./client
|
||||
run: ./tests/run.sh || true
|
||||
|
||||
- name: upload img
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
|
|
14
README.md
14
README.md
|
@ -84,7 +84,17 @@ You can change the URL of the websocket proxy by using `libcurl.set_websocket`.
|
|||
```js
|
||||
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:
|
||||
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
|
||||
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.
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
<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>
|
||||
document.addEventListener("libcurl_load", ()=>{
|
||||
libcurl.set_websocket(`wss://${location.hostname}/ws/`);
|
||||
|
|
|
@ -294,7 +294,11 @@ return {
|
|||
set_websocket: set_websocket_url,
|
||||
load_wasm: load_wasm,
|
||||
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}
|
||||
}
|
||||
|
||||
})()
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "libcurl.js",
|
||||
"version": "0.1.2",
|
||||
"version": "0.2.0",
|
||||
"description": "An experimental port of libcurl to WebAssembly for use in the browser.",
|
||||
"main": "libcurl.mjs",
|
||||
"scripts": {
|
||||
|
|
|
@ -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 ()=>{
|
||||
try {
|
||||
libcurl.set_websocket(`ws://localhost:6001/ws/`);
|
||||
let r = await fetch("/tests/scripts/" + location.hash.substring(1));
|
||||
eval(await r.text());
|
||||
await test();
|
||||
create_flag("success");
|
||||
}
|
||||
|
|
|
@ -4,8 +4,23 @@ set -e
|
|||
|
||||
trap "exit" INT TERM
|
||||
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
|
||||
echo "Running tests"
|
||||
echo "wisp server ready, running tests"
|
||||
python3 tests/run_tests.py
|
||||
|
|
|
@ -14,8 +14,8 @@ class JSTest(unittest.TestCase):
|
|||
options.add_argument("--headless")
|
||||
options.add_argument("--no-sandbox")
|
||||
options.add_argument("--disable-dev-shm-usage")
|
||||
options.addArguments("--disable-browser-side-navigation")
|
||||
options.addArguments("--disable-gpu")
|
||||
options.add_argument("--disable-browser-side-navigation")
|
||||
options.add_argument("--disable-gpu")
|
||||
options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
|
||||
|
||||
self.browser = webdriver.Chrome(options=options)
|
||||
|
@ -46,5 +46,8 @@ class JSTest(unittest.TestCase):
|
|||
def test_websocket(self):
|
||||
self.run_test("test_websocket.js")
|
||||
|
||||
def test_redirect_out(self):
|
||||
self.run_test("redirect_out.js")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
11
client/tests/scripts/redirect_out.js
Normal file
11
client/tests/scripts/redirect_out.js
Normal 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");
|
||||
}
|
|
@ -6,15 +6,15 @@ set -e
|
|||
|
||||
SCRIPT_PATH=$(realpath $0)
|
||||
BASE_PATH=$(dirname $SCRIPT_PATH)
|
||||
SERVER_PATH="$BASE_PATH/wisp_server"
|
||||
|
||||
cd $BASE_PATH/wisp_server
|
||||
if [ ! -d ".venv" ]; then
|
||||
python3 -m venv .venv
|
||||
if [ ! -d "$SERVER_PATH.venv" ]; then
|
||||
python3 -m venv $SERVER_PATH/.venv
|
||||
fi
|
||||
source .venv/bin/activate
|
||||
source $SERVER_PATH/.venv/bin/activate
|
||||
|
||||
if ! python3 -c "import websockets" 2> /dev/null; then
|
||||
pip3 install -r requirements.txt
|
||||
fi
|
||||
|
||||
python3 main.py
|
||||
python3 $SERVER_PATH/main.py "$@"
|
|
@ -1 +1 @@
|
|||
Subproject commit bd7bca97b22023a1b49d0f5a09081ddcbb4ad49c
|
||||
Subproject commit 65d312414aa896d8f9e18f48e8fe37b72d75ee5c
|
Loading…
Add table
Add a link
Reference in a new issue