Fix url normalization

This commit is contained in:
Jonathan Staab 2023-10-11 16:57:11 -07:00
parent 4b5808d9e1
commit 99f0b8d593
2 changed files with 18 additions and 6 deletions

View File

@ -20,6 +20,7 @@
- [x] Use real links so cmd+click works - [x] Use real links so cmd+click works
- [x] Add show/hide replies toggle to feeds - [x] Add show/hide replies toggle to feeds
- [x] Don't ruin gifs and webp uploads - [x] Don't ruin gifs and webp uploads
- [x] Normalize urls more robustly
# 0.3.10 # 0.3.10

View File

@ -1,8 +1,9 @@
import {nip19} from "nostr-tools" import {nip19} from "nostr-tools"
import normalizeUrl from "normalize-url"
import {sortBy, pluck, uniq, nth, prop, last} from "ramda" import {sortBy, pluck, uniq, nth, prop, last} from "ramda"
import {chain, displayList, first, tryFunc} from "hurdak" import {chain, displayList, first} from "hurdak"
import {fuzzy, stripProto} from "src/util/misc" import {fuzzy, stripProto} from "src/util/misc"
import {fromNostrURI, findReplyId, findRootId, Tags} from "src/util/nostr" import {fromNostrURI, LOCAL_RELAY_URL, findReplyId, findRootId, Tags} from "src/util/nostr"
import type {Event} from "src/engine/events/model" import type {Event} from "src/engine/events/model"
import {env} from "src/engine/session/state" import {env} from "src/engine/session/state"
import {stateKey} from "src/engine/session/derived" import {stateKey} from "src/engine/session/derived"
@ -31,12 +32,22 @@ export const isShareableRelay = (url: string) =>
!url.slice(6).match(/\/npub/) !url.slice(6).match(/\/npub/)
export const normalizeRelayUrl = (url: string) => { export const normalizeRelayUrl = (url: string) => {
// If it doesn't start with a compatible protocol, strip the proto and add wss if (url === LOCAL_RELAY_URL) {
if (!url.match(/^(wss|local):\/\/.+/)) { return url
url = "wss://" + stripProto(url)
} }
return (tryFunc(() => new URL(url).href.replace(/\/+$/, "").toLowerCase()) || "") as string // Use our library to normalize
url = normalizeUrl(url, {stripHash: true, stripAuthentication: false})
// Strip the protocol since only wss works
url = stripProto(url)
// Urls without pathnames are supposed to have a trailing slash
if (!url.includes("/")) {
url += "/"
}
return "wss://" + url
} }
export const urlToRelay = url => ({url: normalizeRelayUrl(url)} as Relay) export const urlToRelay = url => ({url: normalizeRelayUrl(url)} as Relay)