mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-12 14:00:01 -04:00
don't set a default websocket url
This commit is contained in:
parent
58a8f2259e
commit
b071019761
8 changed files with 45 additions and 20 deletions
16
README.md
16
README.md
|
@ -23,7 +23,7 @@ Make sure you have emscripten, git, and the various C build tools installed. The
|
|||
sudo apt install make cmake emscripten autoconf automake libtool pkg-config wget xxd
|
||||
```
|
||||
|
||||
The build script will generate `client/out/libcurl.js` as well as `client/out/libcurl_module.mjs`, which is an ES6 module. You can supply the following arguments to the build script to control the build:
|
||||
The build script will generate `client/out/libcurl.js` as well as `client/out/libcurl.mjs`, which is an ES6 module. You can supply the following arguments to the build script to control the build:
|
||||
- `release` - Use all optimizations.
|
||||
- `single_file` - Include the WASM binary in the outputted JS using base64.
|
||||
- `all` - Build twice, once normally, and once as a single file.
|
||||
|
@ -34,12 +34,19 @@ The build script will generate `client/out/libcurl.js` as well as `client/out/li
|
|||
To import the library, follow the build instructions in the previous section, and copy `client/out/libcurl.js` and `client/out/libcurl.wasm` to a directory of your choice. After the script is loaded, call `libcurl.load_wasm`, specifying the url of the `libcurl.wasm` file.
|
||||
|
||||
```html
|
||||
<script defer src="./out/libcurl.js" onload="libcurl.load_wasm('/out/emscripten_compiled.wasm');"></script>
|
||||
<script defer src="./out/libcurl.js" onload="libcurl.load_wasm('/out/libcurl.wasm');"></script>
|
||||
```
|
||||
|
||||
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
|
||||
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.
|
||||
```js
|
||||
document.addEventListener("libcurl_load", ()=>{
|
||||
libcurl.set_websocket(`wss://${location.hostname}/ws/`);
|
||||
console.log("libcurl.js ready!");
|
||||
});
|
||||
```
|
||||
|
@ -63,14 +70,12 @@ Most of the standard Fetch API's features are supported, with the exception of:
|
|||
To use WebSockets, create a `libcurl.WebSocket` object, which works identically to the regular [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) object.
|
||||
```js
|
||||
let ws = new libcurl.WebSocket("wss://echo.websocket.org");
|
||||
ws.binaryType = "arraybuffer";
|
||||
ws.addEventListener("open", () => {
|
||||
console.log("ws connected!");
|
||||
ws.send("hello".repeat(128));
|
||||
});
|
||||
ws.addEventListener("message", (event) => {
|
||||
let text = new TextDecoder().decode(event.data);
|
||||
console.log(text);
|
||||
console.log(event.data);
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -79,6 +84,7 @@ 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.
|
||||
|
||||
## 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.
|
||||
|
|
|
@ -82,7 +82,7 @@ sed -i "/__extra_libraries__/r $JAVSCRIPT_DIR/messages.js" $OUT_FILE
|
|||
sed -i "/__extra_libraries__/r $JAVSCRIPT_DIR/websocket.js" $OUT_FILE
|
||||
|
||||
#apply patches
|
||||
python3 scripts/patcher.py $FRAGMENTS_DIR $OUT_FILE
|
||||
python3 tools/patch_js.py $FRAGMENTS_DIR $OUT_FILE
|
||||
|
||||
#generate es6 module
|
||||
cp $OUT_FILE $ES6_FILE
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
<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/`);
|
||||
console.log("libcurl.js ready!");
|
||||
});
|
||||
</script>
|
||||
|
|
|
@ -1,17 +1,35 @@
|
|||
//everything is wrapped in a function to prevent emscripten from polluting the global scope
|
||||
window.libcurl = (function() {
|
||||
/*
|
||||
ading2210/libcurl.js - A port of libcurl to WASM for the browser.
|
||||
Copyright (C) 2023 ading2210
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
if (typeof window === "undefined") {
|
||||
throw new Error("NodeJS is not supported. This only works inside the browser.");
|
||||
}
|
||||
|
||||
//everything is wrapped in a function to prevent emscripten from polluting the global scope
|
||||
window.libcurl = (function() {
|
||||
|
||||
//emscripten compiled code is inserted here
|
||||
/* __emscripten_output__ */
|
||||
|
||||
//extra client code goes here
|
||||
/* __extra_libraries__ */
|
||||
|
||||
var websocket_url = `wss://${location.hostname}/ws/`;
|
||||
var websocket_url = null;
|
||||
var event_loop = null;
|
||||
var active_requests = 0;
|
||||
var wasm_ready = false;
|
||||
|
@ -20,6 +38,9 @@ function check_loaded() {
|
|||
if (!wasm_ready) {
|
||||
throw new Error("wasm not loaded yet, please call libcurl.load_wasm first");
|
||||
}
|
||||
if (!websocket_url) {
|
||||
throw new Error("websocket proxy url not set, please call libcurl.set_websocket");
|
||||
}
|
||||
}
|
||||
|
||||
//a case insensitive dictionary for request headers
|
||||
|
@ -50,10 +71,6 @@ class HeadersDict {
|
|||
}
|
||||
}
|
||||
|
||||
function is_str(obj) {
|
||||
return typeof obj === 'string' || obj instanceof String;
|
||||
}
|
||||
|
||||
function allocate_str(str) {
|
||||
return allocate(intArrayFromString(str), ALLOC_NORMAL);
|
||||
}
|
||||
|
@ -247,7 +264,7 @@ async function libcurl_fetch(url, params={}) {
|
|||
}
|
||||
|
||||
function set_websocket_url(url) {
|
||||
check_loaded();
|
||||
websocket_url = url;
|
||||
if (!Module.websocket) {
|
||||
document.addEventListener("libcurl_load", () => {
|
||||
set_websocket_url(url);
|
||||
|
@ -257,7 +274,6 @@ function set_websocket_url(url) {
|
|||
}
|
||||
|
||||
function main() {
|
||||
console.log("emscripten module loaded");
|
||||
wasm_ready = true;
|
||||
_init_curl();
|
||||
set_websocket_url(websocket_url);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
class CurlWebSocket extends EventTarget {
|
||||
constructor(url, protocols=[]) {
|
||||
super();
|
||||
check_loaded();
|
||||
if (!url.startsWith("wss://") && !url.startsWith("ws://")) {
|
||||
throw new SyntaxError("invalid url");
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"name": "libcurl.js",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"description": "An experimental port of libcurl to WebAssembly for use in the browser.",
|
||||
"main": "index.js",
|
||||
"main": "libcurl.mjs",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
|
@ -1,9 +1,10 @@
|
|||
#!/bin/bash
|
||||
|
||||
#publish libcurl.js as an npm package
|
||||
#run build.sh first
|
||||
|
||||
cp npm/* out
|
||||
./build.sh all
|
||||
|
||||
cp package.json out
|
||||
cp ../README.md out
|
||||
cd out
|
||||
npm publish
|
Loading…
Add table
Add a link
Reference in a new issue