From 839b490c804bccfdaa5e3ea7c65bd7462f794e3d Mon Sep 17 00:00:00 2001 From: velzie Date: Sat, 12 Oct 2024 14:54:20 -0400 Subject: [PATCH] get rid of some jank in url.ts --- src/shared/rewriters/url.ts | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/shared/rewriters/url.ts b/src/shared/rewriters/url.ts index 237ea0d..83de6e7 100644 --- a/src/shared/rewriters/url.ts +++ b/src/shared/rewriters/url.ts @@ -13,7 +13,16 @@ function tryCanParseURL(url: string, origin?: string | URL): URL | null { } } -// something is broken with this but i didn't debug it +export function rewriteBlob(url: string, meta: URLMeta) { + let blob = new URL(url.substring("blob:".length)); + return "blob:" + meta.origin.origin + blob.pathname; +} + +export function unrewriteBlob(url: string) { + let blob = new URL(url.substring("blob:".length)); + return "blob:" + location.origin + blob.pathname; +} + export function encodeUrl(url: string | URL, meta: URLMeta) { if (url instanceof URL) { url = url.href; @@ -25,8 +34,7 @@ export function encodeUrl(url: string | URL, meta: URLMeta) { return location.origin + self.$scramjet.config.prefix + url; } else if (url.startsWith("data:")) { return location.origin + self.$scramjet.config.prefix + url; - } else if (/^(#|mailto|about)/.test(url)) { - // TODO this regex is jank but i'm not fixing it + } else if (url.startsWith("mailto:") || url.startsWith("about:")) { return url; } else { let base = meta.base.href; @@ -41,22 +49,24 @@ export function encodeUrl(url: string | URL, meta: URLMeta) { } } -// something is also broken with this but i didn't debug it export function decodeUrl(url: string | URL) { if (url instanceof URL) { url = url.href; } - if ( - tryCanParseURL(url)?.pathname.startsWith( - self.$scramjet.config.prefix + "worker" - ) - ) { - return new URL(new URL(url).searchParams.get("origin")).href; - } + const prefixed = location.origin + self.$scramjet.config.prefix; - // TODO: unrewrite rewritten blobs - if (/^(#|about|data|mailto|javascript)/.test(url)) { + if (url.startsWith("javascript:")) { + //TODO + return url; + } else if (url.startsWith("blob:")) { + // realistically this shouldn't happen + return url; + } else if (url.startsWith(prefixed + "blob:")) { + return url.substring(prefixed.length); + } else if (url.startsWith(prefixed + "data:")) { + return url.substring(prefixed.length); + } else if (url.startsWith("mailto:") || url.startsWith("about:")) { return url; } else if (tryCanParseURL(url)) { return self.$scramjet.codec.decode(