mirror of
https://github.com/aljazceru/pubky-core.git
synced 2026-01-04 14:54:29 +01:00
fix(js): replace pubky:// with https:// in transform_url
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?;
|
||||
|
||||
|
||||
@@ -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://<pubky> to https://_pubky.<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<Endpoint> = 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]
|
||||
|
||||
@@ -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://<pubky> to https://_pubky.<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<Endpoint> = 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user