From 2f9be971780c26c2d5d8d83d28b25ed872c40719 Mon Sep 17 00:00:00 2001 From: codytseng Date: Tue, 25 Feb 2025 17:54:01 +0800 Subject: [PATCH] fix: catch some errors --- src/lib/event.ts | 14 +++++++++----- src/lib/url.ts | 26 +++++++++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/lib/event.ts b/src/lib/event.ts index 4458fa85..faa9c5b6 100644 --- a/src/lib/event.ts +++ b/src/lib/event.ts @@ -333,11 +333,15 @@ export function extractEmbeddedEventIds(event: Event) { const embeddedEventIds: string[] = [] const embeddedNoteRegex = /nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g ;(event.content.match(embeddedNoteRegex) || []).forEach((note) => { - const { type, data } = nip19.decode(note.split(':')[1]) - if (type === 'nevent') { - embeddedEventIds.push(data.id) - } else if (type === 'note') { - embeddedEventIds.push(data) + try { + const { type, data } = nip19.decode(note.split(':')[1]) + if (type === 'nevent') { + embeddedEventIds.push(data.id) + } else if (type === 'note') { + embeddedEventIds.push(data) + } + } catch { + // ignore } }) EVENT_EMBEDDED_EVENT_IDS_CACHE.set(event.id, embeddedEventIds) diff --git a/src/lib/url.ts b/src/lib/url.ts index fc659825..43cf41b4 100644 --- a/src/lib/url.ts +++ b/src/lib/url.ts @@ -16,15 +16,23 @@ export function normalizeUrl(url: string): string { } export function normalizeHttpUrl(url: string): string { - if (url.indexOf('://') === -1) url = 'https://' + 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 === 'http:') || (p.port === '443' && p.protocol === 'https:')) - p.port = '' - p.searchParams.sort() - p.hash = '' - return p.toString() + try { + if (url.indexOf('://') === -1) url = 'https://' + 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 === 'http:') || + (p.port === '443' && p.protocol === 'https:') + ) + p.port = '' + p.searchParams.sort() + p.hash = '' + return p.toString() + } catch { + console.error('Invalid URL:', url) + return url + } } export function simplifyUrl(url: string): string {