Normalize tor urls to use ws://

This commit is contained in:
Jon Staab
2025-01-14 15:58:04 -08:00
parent e72d49e563
commit 72e2787ec2
4 changed files with 12 additions and 5 deletions
+7 -2
View File
@@ -231,6 +231,7 @@ export type RouterScenarioOptions = {
limit?: number
allowLocal?: boolean
allowOnion?: boolean
allowInsecure?: boolean
}
export class RouterScenario {
@@ -265,6 +266,8 @@ export class RouterScenario {
allowOnion = (allowOnion: boolean) => this.clone({allowOnion})
allowInsecure = (allowInsecure: boolean) => this.clone({allowInsecure})
weight = (scale: number) =>
this.update(selection => ({...selection, weight: selection.weight * scale}))
@@ -276,12 +279,14 @@ export class RouterScenario {
const limit = this.getLimit()
const fallbackPolicy = this.getPolicy()
const relayWeights = new Map<string, number>()
const {allowOnion, allowLocal, allowInsecure} = this.options
for (const {weight, relays} of this.selections) {
for (const relay of relays) {
if (!isRelayUrl(relay)) continue
if (!this.options.allowOnion && isOnionUrl(relay)) continue
if (!this.options.allowLocal && isLocalUrl(relay)) continue
if (!allowOnion && isOnionUrl(relay)) continue
if (!allowLocal && isLocalUrl(relay)) continue
if (!allowInsecure && relay.startsWith("ws://") && !isOnionUrl(relay)) continue
relayWeights.set(relay, add(weight, relayWeights.get(relay)))
}
+3 -1
View File
@@ -1102,7 +1102,9 @@ export const fetchJson = async (url: string, opts: FetchOpts = {}) => {
opts.headers = {}
}
opts.headers["Accept"] = "application/json"
if (!opts.headers["Accept"]) {
opts.headers["Accept"] = "application/json"
}
const res = await fetch(url, opts as RequestInit)
const json = await res.json()
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@welshman/util",
"version": "0.0.56",
"version": "0.0.57",
"author": "hodlbod",
"license": "MIT",
"description": "A collection of nostr-related utilities.",
+1 -1
View File
@@ -62,7 +62,7 @@ export const isIPAddress = (url: string) => Boolean(url.match(/\d+\.\d+\.\d+\.\d
export const isShareableRelayUrl = (url: string) => Boolean(isRelayUrl(url) && !isLocalUrl(url))
export const normalizeRelayUrl = (url: string) => {
const prefix = url.match(/^wss?:\/\//)?.[0] || "wss://"
const prefix = url.match(/^wss?:\/\//)?.[0] || (isOnionUrl(url) ? "ws://" : "wss://")
// Use our library to normalize
url = normalizeUrl(url, {stripHash: true, stripAuthentication: false})