mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-11 13:30:01 -04:00
add abort events and load_wasm promise rejection
This commit is contained in:
parent
15a6df827e
commit
23dd3bd981
5 changed files with 29 additions and 9 deletions
|
@ -1,5 +1,10 @@
|
|||
# Libcurl.js Changelog:
|
||||
|
||||
## v0.6.14 (9/4/24):
|
||||
- All dependencies have been updated to the latest versions
|
||||
- `libcurl.load_wasm` now returns a promise that resolves when the WASM has finished loading
|
||||
- Unused protocol support has been disabled in the curl binary, resulting in 100kb binary size savings
|
||||
|
||||
## v0.6.13 (9/1/24):
|
||||
- A minor bug where the hostname for TCP connections would be wrong in certain cases has been fixed.
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||

|
||||

|
||||
|
||||
This is an experimental port of [libcurl](https://curl.se/libcurl/) to WebAssembly for use in the browser. It provides an interface compatible with the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), allowing you to proxy HTTPS requests from the browser with full TLS encryption. Unlike previous implementations, the proxy server cannot read the contents of your requests.
|
||||
This is a port of [libcurl](https://curl.se/libcurl/) to WebAssembly for use in the browser. It provides an interface compatible with the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), allowing you to proxy HTTPS requests from the browser with full TLS encryption. Unlike previous implementations, the proxy server cannot read the contents of your requests.
|
||||
|
||||
## Table of Contents:
|
||||
- [Features](#features)
|
||||
|
@ -74,7 +74,7 @@ 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.
|
||||
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. If the WASM is already loaded, the function will return immediately. If the WASM loading fails, the promise will be rejected with an error message.
|
||||
```js
|
||||
async function main() {
|
||||
await libcurl.load_wasm("/out/libcurl.wasm");
|
||||
|
@ -84,13 +84,13 @@ 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.
|
||||
Alternatively, prebuilt versions can be found on NPM and jsDelivr. You can use the [following URLs](https://cdn.jsdelivr.net/npm/libcurl.js@latest/) to load libcurl.js from a third party CDN.
|
||||
```
|
||||
https://cdn.jsdelivr.net/npm/libcurl.js@latest/libcurl.js
|
||||
https://cdn.jsdelivr.net/npm/libcurl.js@latest/libcurl.wasm
|
||||
```
|
||||
|
||||
To know when libcurl.js has finished loading, you can use the `libcurl_load` DOM event.
|
||||
To know when libcurl.js has finished loading, you can use the `libcurl_load` DOM event. The `libcurl_abort` event will trigger if the Emscripten runtime gets aborted due to a critical error. The `libcurl.events` object contains an `EventTarget` where these events will also be emitted.
|
||||
```js
|
||||
document.addEventListener("libcurl_load", ()=>{
|
||||
libcurl.set_websocket(`wss://${location.hostname}/ws/`);
|
||||
|
|
|
@ -89,6 +89,14 @@ function main() {
|
|||
}
|
||||
}
|
||||
|
||||
function abort_callback(reason) {
|
||||
let abort_event = new CustomEvent("libcurl_abort", {detail: reason});
|
||||
api.events.dispatchEvent(abort_event);
|
||||
if (ENVIRONMENT_IS_WEB) {
|
||||
document.dispatchEvent(abort_event);
|
||||
}
|
||||
}
|
||||
|
||||
function load_wasm(url) {
|
||||
if (wasm_ready) return;
|
||||
|
||||
|
@ -99,13 +107,20 @@ function load_wasm(url) {
|
|||
run();
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (wasm_ready) return resolve();
|
||||
api.events.addEventListener("libcurl_load", () => {resolve()});
|
||||
api.events.addEventListener("libcurl_load", () => {
|
||||
resolve();
|
||||
}, {once: true});
|
||||
api.events.addEventListener("libcurl_abort", (event) => {
|
||||
reject(event.detail);
|
||||
}, {once: true});
|
||||
});
|
||||
}
|
||||
|
||||
Module.onRuntimeInitialized = main;
|
||||
Module.onAbort = abort_callback;
|
||||
|
||||
api = {
|
||||
set_websocket: set_websocket_url,
|
||||
load_wasm: load_wasm,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "libcurl.js",
|
||||
"version": "0.6.13",
|
||||
"version": "0.6.14",
|
||||
"description": "An experimental port of libcurl to WebAssembly for use in the browser.",
|
||||
"main": "libcurl.mjs",
|
||||
"exports": {
|
||||
|
|
|
@ -3,15 +3,15 @@
|
|||
<head>
|
||||
<link rel="icon" href="data:;base64,=">
|
||||
|
||||
<script src="./out/libcurl_full.js"></script>
|
||||
<script src="./out/libcurl_full.js" defer onload="main()"></script>
|
||||
<script>
|
||||
async function main() {
|
||||
console.log("libcurl.js ready?", libcurl.ready);
|
||||
console.log("waiting for wasm load");
|
||||
await libcurl.load_wasm();
|
||||
console.log("libcurl.js ready?", libcurl.ready);
|
||||
console.log("loaded libcurl.js", libcurl.version.lib);
|
||||
}
|
||||
main();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue