diff --git a/pubky/pkg/test/auth.js b/pubky/pkg/test/auth.js index a3d6898..555153b 100644 --- a/pubky/pkg/test/auth.js +++ b/pubky/pkg/test/auth.js @@ -1,10 +1,12 @@ import test from 'tape' -import { Client, Keypair, PublicKey } from '../index.cjs' +import { Client, Keypair, PublicKey, setLogLevel } from '../index.cjs' const Homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo') -test('auth', async (t) => { +test.only('auth', async (t) => { + setLogLevel("debug"); + const client = Client.testnet(); const keypair = Keypair.random() diff --git a/pubky/pkg/test/http.js b/pubky/pkg/test/http.js index 16b04db..abe0618 100644 --- a/pubky/pkg/test/http.js +++ b/pubky/pkg/test/http.js @@ -6,7 +6,7 @@ const TLD = '8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo'; // TODO: test HTTPs too somehow. -test.only("basic fetch", async (t) => { +test("basic fetch", async (t) => { let client = Client.testnet(); // Normal TLD diff --git a/pubky/src/shared/auth.rs b/pubky/src/shared/auth.rs index ab4364e..fcc319f 100644 --- a/pubky/src/shared/auth.rs +++ b/pubky/src/shared/auth.rs @@ -62,9 +62,7 @@ impl Client { return Ok(None); } - if !res.status().is_success() { - res.error_for_status_ref()?; - }; + res.error_for_status_ref()?; let bytes = res.bytes().await?; diff --git a/pubky/src/wasm/http.rs b/pubky/src/wasm/http.rs index 3ea8790..9729c21 100644 --- a/pubky/src/wasm/http.rs +++ b/pubky/src/wasm/http.rs @@ -5,6 +5,11 @@ use wasm_bindgen::prelude::*; use reqwest::Url; +use futures_lite::StreamExt; + +use pkarr::extra::endpoints::{Endpoint, EndpointsResolver}; +use pkarr::PublicKey; + use crate::Client; #[wasm_bindgen] @@ -31,6 +36,50 @@ impl Client { Ok(js_fetch(&js_req)) } + + pub(super) async fn transform_url(&self, url: &mut Url) { + if url.scheme() == "pubky" { + *url = Url::parse(&format!("https{}", &url.as_str()[5..])) + .expect("couldn't replace pubky:// with https://"); + url.set_host(Some(&format!("_pubky.{}", url.host_str().unwrap_or("")))) + .expect("couldn't map pubk:// to https://_pubky."); + } + + let qname = url.host_str().unwrap_or("").to_string(); + + if PublicKey::try_from(qname.to_string()).is_ok() { + let mut stream = self.pkarr.resolve_https_endpoints(&qname); + + let mut so_far: Option = None; + + // TODO: currently we return the first thing we can see, + // in the future we might want to failover to other endpoints + while so_far.is_none() { + while let Some(endpoint) = stream.next().await { + if endpoint.domain() != "." { + so_far = Some(endpoint); + } + } + } + + if let Some(e) = so_far { + // TODO: detect loopback IPs and other equivilants to localhost + if self.testnet && e.domain() == "localhost" { + url.set_scheme("http") + .expect("couldn't replace pubky:// with http://"); + } + + url.set_host(Some(e.domain())) + .expect("coultdn't use the resolved endpoint's domain"); + url.set_port(Some(e.port())) + .expect("coultdn't use the resolved endpoint's port"); + } else { + // TODO: didn't find any domain, what to do? + } + } + + log::debug!("Transformed URL to: {}", url.as_str()); + } } #[wasm_bindgen] diff --git a/pubky/src/wasm/internals.rs b/pubky/src/wasm/internals.rs index 3634f76..ac64f83 100644 --- a/pubky/src/wasm/internals.rs +++ b/pubky/src/wasm/internals.rs @@ -4,11 +4,6 @@ use reqwest::{IntoUrl, Method, RequestBuilder}; use url::Url; -use futures_lite::StreamExt; - -use pkarr::extra::endpoints::{Endpoint, EndpointsResolver}; -use pkarr::PublicKey; - use crate::Client; impl Client { @@ -21,48 +16,4 @@ impl Client { self.http.request(method, url).fetch_credentials_include() } - - pub(super) async fn transform_url(&self, url: &mut Url) { - if url.scheme() == "pubky" { - url.set_scheme("https") - .expect("couldn't replace pubky:// with https://"); - url.set_host(Some(&format!("_pubky.{}", url.host_str().unwrap_or("")))) - .expect("couldn't map pubk:// to https://_pubky."); - } - - let qname = url.host_str().unwrap_or("").to_string(); - - // TODO: detect loopback IPs and other equivilants to localhost - if qname == "localhost" && self.testnet { - url.set_scheme("http") - .expect("couldn't replace pubky:// with http://"); - } - - if PublicKey::try_from(qname.to_string()).is_ok() { - let mut stream = self.pkarr.resolve_https_endpoints(&qname); - - let mut so_far: Option = None; - - // TODO: currently we return the first thing we can see, - // in the future we might want to failover to other endpoints - while so_far.is_none() { - while let Some(endpoint) = stream.next().await { - if endpoint.domain() != "." { - so_far = Some(endpoint); - } - } - } - - if let Some(e) = so_far { - url.set_host(Some(e.domain())) - .expect("coultdn't use the resolved endpoint's domain"); - url.set_port(Some(e.port())) - .expect("coultdn't use the resolved endpoint's port"); - } else { - // TODO: didn't find any domain, what to do? - } - } - - log::debug!("Transformed URL to: {}", url.as_str()); - } }