From b576afb97117141416cf5c9adc98ee696a7c6c59 Mon Sep 17 00:00:00 2001 From: codytseng Date: Thu, 27 Mar 2025 23:21:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/MailboxSetting/index.tsx | 3 +++ src/components/RelaySetsSetting/RelayUrl.tsx | 3 +++ .../SaveRelayDropdownMenu/index.tsx | 2 +- src/lib/event.ts | 1 + src/lib/url.ts | 23 +++++++++++-------- src/providers/FeedProvider.tsx | 2 +- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/components/MailboxSetting/index.tsx b/src/components/MailboxSetting/index.tsx index 5db8041a..6e6f8025 100644 --- a/src/components/MailboxSetting/index.tsx +++ b/src/components/MailboxSetting/index.tsx @@ -47,6 +47,9 @@ export default function MailboxSetting() { const saveNewMailboxRelay = (url: string) => { if (url === '') return null const normalizedUrl = normalizeUrl(url) + if (!normalizedUrl) { + return t('Invalid relay URL') + } if (relays.some((r) => r.url === normalizedUrl)) { return t('Relay already exists') } diff --git a/src/components/RelaySetsSetting/RelayUrl.tsx b/src/components/RelaySetsSetting/RelayUrl.tsx index 3262edfd..5f0105ce 100644 --- a/src/components/RelaySetsSetting/RelayUrl.tsx +++ b/src/components/RelaySetsSetting/RelayUrl.tsx @@ -29,6 +29,9 @@ export default function RelayUrls({ relaySetId }: { relaySetId: string }) { const saveNewRelayUrl = () => { if (newRelayUrl === '') return const normalizedUrl = normalizeUrl(newRelayUrl) + if (!normalizedUrl) { + return setNewRelayUrlError(t('Invalid relay URL')) + } if (relaySet.relayUrls.includes(normalizedUrl)) { return setNewRelayUrlError(t('Relay already exists')) } diff --git a/src/components/SaveRelayDropdownMenu/index.tsx b/src/components/SaveRelayDropdownMenu/index.tsx index 117838b0..47eaf579 100644 --- a/src/components/SaveRelayDropdownMenu/index.tsx +++ b/src/components/SaveRelayDropdownMenu/index.tsx @@ -23,7 +23,7 @@ export default function SaveRelayDropdownMenu({ }) { const { t } = useTranslation() const { relaySets } = useRelaySets() - const normalizedUrls = useMemo(() => urls.map((url) => normalizeUrl(url)), [urls]) + const normalizedUrls = useMemo(() => urls.map((url) => normalizeUrl(url)).filter(Boolean), [urls]) const alreadySaved = useMemo( () => relaySets.some((set) => normalizedUrls.every((url) => set.relayUrls.includes(url))), [relaySets, normalizedUrls] diff --git a/src/lib/event.ts b/src/lib/event.ts index 890e48b4..72431623 100644 --- a/src/lib/event.ts +++ b/src/lib/event.ts @@ -142,6 +142,7 @@ export function getRelayListFromRelayListEvent(event?: Event) { if (!url || !isWebsocketUrl(url)) return const normalizedUrl = normalizeUrl(url) + if (!normalizedUrl) return switch (type) { case 'write': relayList.write.push(normalizedUrl) diff --git a/src/lib/url.ts b/src/lib/url.ts index 43cf41b4..b75bd2de 100644 --- a/src/lib/url.ts +++ b/src/lib/url.ts @@ -4,15 +4,20 @@ export function isWebsocketUrl(url: string): boolean { // copy from nostr-tools/utils export function normalizeUrl(url: string): string { - if (url.indexOf('://') === -1) url = 'wss://' + url - const p = new URL(url) - p.pathname = p.pathname.replace(/\/+/g, '/') - if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1) - if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) - p.port = '' - p.searchParams.sort() - p.hash = '' - return p.toString() + try { + if (url.indexOf('://') === -1) url = 'wss://' + url + const p = new URL(url) + p.pathname = p.pathname.replace(/\/+/g, '/') + if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1) + if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) + p.port = '' + p.searchParams.sort() + p.hash = '' + return p.toString() + } catch { + console.error('Invalid URL:', url) + return '' + } } export function normalizeHttpUrl(url: string): string { diff --git a/src/providers/FeedProvider.tsx b/src/providers/FeedProvider.tsx index 5c43a8f1..496e343a 100644 --- a/src/providers/FeedProvider.tsx +++ b/src/providers/FeedProvider.tsx @@ -56,7 +56,7 @@ export function FeedProvider({ children }: { children: React.ReactNode }) { const temporaryRelayUrls = searchParams .getAll('r') .map((url) => normalizeUrl(url)) - .filter((url) => isWebsocketUrl(url)) + .filter((url) => url && isWebsocketUrl(url)) if (temporaryRelayUrls.length) { return await switchFeed('temporary', { temporaryRelayUrls }) }