Merge branch 'main' into v0.4

This commit is contained in:
ading2210 2024-03-04 14:00:04 -05:00
commit 96ff9c337a
3 changed files with 53 additions and 5 deletions

View file

@ -51,6 +51,13 @@ document.addEventListener("libcurl_load", ()=>{
});
```
Alternatively, the `libcurl.onload` callback can be used.
```js
libcurl.onload = () => {
console.log("libcurl.js ready!");
}
```
Once loaded, there will be a `window.libcurl` object which includes all the API functions. The `libcurl.ready` property can also be used to know if the WASM has loaded.
### Making HTTP Requests:

View file

@ -30,6 +30,7 @@ var event_loop = null;
var active_requests = 0;
var wasm_ready = false;
var version_dict = null;
var api = null;
const libcurl_version = "__library_version__";
function check_loaded(check_websocket) {
@ -231,7 +232,7 @@ async function libcurl_fetch(url, params={}) {
function set_websocket_url(url) {
websocket_url = url;
if (!Module.websocket) {
if (!Module.websocket && ENVIRONMENT_IS_WEB) {
document.addEventListener("libcurl_load", () => {
set_websocket_url(url);
});
@ -256,8 +257,11 @@ function main() {
_init_curl();
set_websocket_url(websocket_url);
if (ENVIRONMENT_IS_WEB) {
let load_event = new Event("libcurl_load");
document.dispatchEvent(load_event);
}
api.onload();
}
function load_wasm(url) {
@ -267,7 +271,7 @@ function load_wasm(url) {
}
Module.onRuntimeInitialized = main;
return {
api = {
fetch: libcurl_fetch,
set_websocket: set_websocket_url,
load_wasm: load_wasm,
@ -286,6 +290,10 @@ return {
set stdout(callback) {out = callback},
get stderr() {return err},
set stderr(callback) {err = callback},
}
onload() {}
};
return api;
})()

33
client/worker.html Normal file
View file

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="data:;base64,=">
<script id="worker1" type="javascript/worker">
importScripts(`${location.origin}/out/libcurl.js`);
async function main() {
libcurl.set_websocket(`${location.origin.replace("http", "ws")}/`);
self.postMessage("loaded libcurl.js v" + libcurl.version.lib);
let r = await libcurl.fetch("https://ifconfig.me/all", {_libcurl_verbose: 1});
self.postMessage(await r.text());
}
libcurl.onload = main;
libcurl.stdout = self.postMessage;
libcurl.stderr = self.postMessage;
libcurl.load_wasm(`${location.origin}/out/libcurl.wasm`);
</script>
<script>
var blob = new Blob([
document.querySelector('#worker1').textContent
], {type: "text/javascript"});
var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function(e) {
console.log("Received: " + e.data);
}
</script>
</head>
<body>
<p>emscripten tests</p>
</body>
</html>