From 215ff85b6d325b8c6e5663f1d99c289a6ddbefae Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 9 Jul 2023 14:23:02 -0500 Subject: [PATCH] Add Conf.url() function for producing local absolute URLs --- src/config.ts | 11 +++++++++++ src/middleware/auth98.ts | 7 +------ src/note.ts | 6 ++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/config.ts b/src/config.ts index fd208c0..42d6ecb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -21,6 +21,17 @@ const Conf = { get publishRelays() { return ['wss://relay.mostr.pub']; }, + /** Merges the path with the localDomain. */ + url(path: string): string { + if (path.startsWith('/')) { + // Path is a path. + return new URL(path, Conf.localDomain).toString(); + } else { + // Path is possibly a full URL. Replace the domain. + const { pathname } = new URL(path); + return new URL(pathname, Conf.localDomain).toString(); + } + }, }; export { Conf }; diff --git a/src/middleware/auth98.ts b/src/middleware/auth98.ts index 4910b0e..99865a2 100644 --- a/src/middleware/auth98.ts +++ b/src/middleware/auth98.ts @@ -28,7 +28,7 @@ function auth98(opts: Auth98Opts = {}): AppMiddleware { .refine((event) => { const url = findTag(event.tags, 'u')?.[1]; try { - return url === localUrl(c.req.url); + return url === Conf.url(c.req.url); } catch (_e) { return false; } @@ -51,11 +51,6 @@ function auth98(opts: Auth98Opts = {}): AppMiddleware { }; } -function localUrl(url: string): string { - const { pathname } = new URL(url); - return new URL(pathname, Conf.localDomain).toString(); -} - const requireProof: AppMiddleware = async (c, next) => { const pubkey = c.get('pubkey'); const proof = c.get('proof'); diff --git a/src/note.ts b/src/note.ts index a4344ea..514044f 100644 --- a/src/note.ts +++ b/src/note.ts @@ -4,13 +4,11 @@ import { linkify, linkifyStr, mime, nip19, nip21 } from '@/deps.ts'; linkify.registerCustomProtocol('nostr', true); linkify.registerCustomProtocol('wss'); -const url = (path: string) => new URL(path, Conf.localDomain).toString(); - const linkifyOpts: linkify.Opts = { render: { hashtag: ({ content }) => { const tag = content.replace(/^#/, ''); - const href = url(`/tags/${tag}`); + const href = Conf.url(`/tags/${tag}`); return `#${tag}`; }, url: ({ content }) => { @@ -19,7 +17,7 @@ const linkifyOpts: linkify.Opts = { const pubkey = getDecodedPubkey(decoded); if (pubkey) { const name = pubkey.substring(0, 8); - const href = url(`/users/${pubkey}`); + const href = Conf.url(`/users/${pubkey}`); return `@${name}`; } else { return '';