add support for relative urls

This commit is contained in:
ading2210 2024-04-30 13:22:47 -04:00
parent 294fbd65f3
commit 8ac0f40896
4 changed files with 23 additions and 4 deletions

View file

@ -1,5 +1,9 @@
# Libcurl.js Changelog:
## v0.6.8 (4/30/24):
- Add support for relative URLs in HTTP sessions
- Better error handling for invalid URLs
## v0.6.7 (3/26/24):
- Fix handling of `Request` objects when passed into `libcurl.fetch`
@ -91,14 +95,14 @@
- Add an option to redirect the verbose curl output.
- Use separate callbacks for stdout and stderr.
## v0.1.2 (2/1/23):
## v0.1.2 (2/1/24):
- Fix bundling the WASM into a single file
- Add unit tests
## v0.1.1 (1/29/23):
## v0.1.1 (1/29/24):
- Don't set a default websocket proxy URL
## v0.1.0 (1/28/23):
## v0.1.0 (1/28/24):
- Initial release on NPM
- Add Github Actions for automatic builds
- Add WebSocket support

View file

@ -134,6 +134,17 @@ Each HTTP session has the following methods available:
- `export_cookies` - Export any cookies which were recorded in the session. This will return an empty string if cookies are disabled or no cookies have been set yet.
- `close` - Close all connections and clean up the session. You must call this after you are done using the session, otherwise it will leak memory.
The following attributes are supported:
- `base_url` - Set the base URL used to resolve relative URLs. If this is not defined then an error will be thrown when attempting to fetch a relative URL.
```js
let session = new libcurl.HTTPSession();
session.base_url = "https://ading.dev";
let r = await session.fetch("/projects/");
console.log(await r.text());
session.close();
```
### Creating WebSocket Connections:
To use WebSockets, create a `libcurl.CurlWebSocket` object, which takes the following arguments:
- `url` - The Websocket URL.

View file

@ -2,6 +2,7 @@ class HTTPSession extends CurlSession {
constructor(options={}) {
super();
this.options = options;
this.base_url = undefined;
this.set_connections(50, 40);
this.import_cookies();
@ -90,6 +91,9 @@ class HTTPSession extends CurlSession {
params.headers = params.headers || Object.fromEntries(resource.headers);
params.method = params.method || resource.method;
}
else {
url = (new URL(url, this.base_url)).href;
}
let body = await this.constructor.create_options(params);
return await this.request_async(url, params, body);
}

View file

@ -1,6 +1,6 @@
{
"name": "libcurl.js",
"version": "0.6.7",
"version": "0.6.8",
"description": "An experimental port of libcurl to WebAssembly for use in the browser.",
"main": "libcurl.mjs",
"exports": {