libcurl.load_wasm now returns a promise

This commit is contained in:
Allen Ding 2024-09-04 12:06:27 -07:00
parent 5bcff0228b
commit 15a6df827e
3 changed files with 51 additions and 10 deletions

View file

@ -42,7 +42,7 @@ This is an experimental port of [libcurl](https://curl.se/libcurl/) to WebAssemb
- Works inside web workers without needing special permissions or headers
- Works in all major browsers (Chromium >= 64, Firefox >= 65, Safari >= 14)
- Has the ability to create multiple independent sessions
- Small footprint size (800kb after compression) and low runtime memory usage
- Small footprint size (672KB after compression) and low runtime memory usage
- Support for Brotli and gzip compressed responses
## Building:
@ -74,6 +74,16 @@ To import the library, follow the build instructions in the previous section, an
<script defer src="./out/libcurl.js" onload="libcurl.load_wasm('/out/libcurl.wasm');"></script>
```
You may also call and await `libcurl.load_wasm` in your own async code. It returns a promise which resolves once libcurl.js is fully ready.
```js
async function main() {
await libcurl.load_wasm("/out/libcurl.wasm");
console.log(await libcurl.fetch("https://ading.dev/"));
}
main();
```
If you are using the single file version (`libcurl_full.js`), the `libcurl.load_wasm` function can still be used to wait for the WASM to load, although the url provided to it has no effect.
Alternatively, prebuilt versions can be found on NPM and jsDelivr. You can use the following URLs to load libcurl.js from a third party CDN.
```
https://cdn.jsdelivr.net/npm/libcurl.js@latest/libcurl.js

View file

@ -77,22 +77,32 @@ function main() {
wasm_ready = true;
_init_curl();
if (ENVIRONMENT_IS_WEB) {
let load_event = new Event("libcurl_load");
document.dispatchEvent(load_event);
}
if (!main_session && websocket_url) {
setup_main_session();
}
let load_event = new Event("libcurl_load");
api.events.dispatchEvent(load_event);
api.onload();
if (ENVIRONMENT_IS_WEB) {
document.dispatchEvent(load_event);
}
}
function load_wasm(url) {
wasmBinaryFile = url;
createWasm();
run();
if (wasm_ready) return;
//skip this if we are running in single file mode
if (!isDataURI(wasmBinaryFile)) {
wasmBinaryFile = url;
createWasm();
run();
}
return new Promise((resolve) => {
if (wasm_ready) return resolve();
api.events.addEventListener("libcurl_load", () => {resolve()});
});
}
Module.onRuntimeInitialized = main;
@ -128,7 +138,8 @@ api = {
get logger() {return logger},
set logger(func) {logger = func},
onload() {}
onload() {},
events: new EventTarget()
};
return api;

20
client/use_bundled.html Normal file
View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="data:;base64,=">
<script src="./out/libcurl_full.js"></script>
<script>
async function main() {
console.log("libcurl.js ready?", libcurl.ready);
await libcurl.load_wasm();
console.log("libcurl.js ready?", libcurl.ready);
console.log("loaded libcurl.js", libcurl.version.lib);
}
main();
</script>
</head>
<body>
<p>emscripten tests</p>
</body>
</html>