diff --git a/README.md b/README.md index 796a554..ef9a338 100644 --- a/README.md +++ b/README.md @@ -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 ``` +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 diff --git a/client/javascript/main.js b/client/javascript/main.js index 8f2f779..8ce24cc 100644 --- a/client/javascript/main.js +++ b/client/javascript/main.js @@ -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; diff --git a/client/use_bundled.html b/client/use_bundled.html new file mode 100644 index 0000000..7626d97 --- /dev/null +++ b/client/use_bundled.html @@ -0,0 +1,20 @@ + + + + + + + + + +

emscripten tests

+ + \ No newline at end of file