feat(pubky): add PubkyClient.testnet() helper

This commit is contained in:
nazeh
2024-07-30 15:55:20 +03:00
parent d8d3a43abd
commit 7e451a5d00
6 changed files with 27 additions and 5 deletions

View File

@@ -24,6 +24,8 @@ pub struct Config {
/// Defaults to a directory in the OS data directory
storage: Option<PathBuf>,
keypair: Keypair,
dht_request_timeout: Option<Duration>,
}
impl Config {
@@ -52,6 +54,7 @@ impl Config {
storage,
port: Some(15411),
keypair: Keypair::from_secret_key(&[0_u8; 32]),
dht_request_timeout: Some(Duration::from_millis(10)),
..Default::default()
}
}
@@ -101,6 +104,10 @@ impl Config {
pub fn keypair(&self) -> &Keypair {
&self.keypair
}
pub(crate) fn dht_request_timeout(&self) -> Option<Duration> {
self.dht_request_timeout
}
}
impl Default for Config {
@@ -111,6 +118,7 @@ impl Default for Config {
domain: "localhost".to_string(),
storage: None,
keypair: Keypair::random(),
dht_request_timeout: None,
}
}
}

View File

@@ -39,6 +39,7 @@ impl Homeserver {
let pkarr_client = PkarrClient::new(Settings {
dht: DhtSettings {
bootstrap: config.bootstsrap(),
request_timeout: config.dht_request_timeout(),
..Default::default()
},
..Default::default()

View File

@@ -2,7 +2,7 @@
"name": "@synonymdev/pubky",
"type": "module",
"description": "Pubky client",
"version": "0.0.3",
"version": "0.1.1",
"license": "MIT",
"repository": {
"type": "git",

View File

@@ -3,7 +3,7 @@ import test from 'tape'
import { PubkyClient, Keypair, PublicKey } from '../index.js'
test('auth', async (t) => {
const client = new PubkyClient().setPkarrRelays(["http://localhost:15411/pkarr"])
const client = PubkyClient.testnet();
const keypair = Keypair.random()
const publicKey = keypair.public_key()

View File

@@ -3,7 +3,7 @@ import test from 'tape'
import { PubkyClient, Keypair, PublicKey } from '../index.js'
test('public: put/get', async (t) => {
const client = new PubkyClient().setPkarrRelays(["http://localhost:15411/pkarr"])
const client = PubkyClient.testnet();
const keypair = Keypair.random();
@@ -20,7 +20,7 @@ test('public: put/get', async (t) => {
// GET public data without signup or signin
{
const client = new PubkyClient().setPkarrRelays(["http://localhost:15411/pkarr"])
const client = PubkyClient.testnet();
let response = await client.get(publicKey, "/pub/example.com/arbitrary");

View File

@@ -25,6 +25,7 @@ impl Default for PubkyClient {
}
static DEFAULT_RELAYS: [&str; 1] = ["https://relay.pkarr.org"];
static TESTNET_RELAYS: [&str; 1] = ["http://localhost:15411/pkarr"];
#[wasm_bindgen]
impl PubkyClient {
@@ -37,7 +38,18 @@ impl PubkyClient {
}
}
/// Set the relays used for publishing and resolving Pkarr packets.
/// Create a client with with configurations appropriate for local testing:
/// - set Pkarr relays to `["http://localhost:15411/pkarr"]` instead of default relay.
#[wasm_bindgen]
pub fn testnet() -> Self {
Self {
http: reqwest::Client::builder().build().unwrap(),
session_cookies: Arc::new(RwLock::new(HashSet::new())),
pkarr_relays: TESTNET_RELAYS.into_iter().map(|s| s.to_string()).collect(),
}
}
/// Set Pkarr relays used for publishing and resolving Pkarr packets.
///
/// By default, [PubkyClient] will use `["https://relay.pkarr.org"]`
#[wasm_bindgen(js_name = "setPkarrRelays")]
@@ -51,6 +63,7 @@ impl PubkyClient {
self
}
// Read the set of pkarr relays used by this client.
#[wasm_bindgen(js_name = "getPkarrRelays")]
pub fn get_pkarr_relays(&self) -> Vec<JsValue> {
self.pkarr_relays