get rid of some jank in url.ts

This commit is contained in:
velzie 2024-10-12 14:54:20 -04:00
parent 7f90663cc4
commit 839b490c80

View file

@ -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) { export function encodeUrl(url: string | URL, meta: URLMeta) {
if (url instanceof URL) { if (url instanceof URL) {
url = url.href; url = url.href;
@ -25,8 +34,7 @@ export function encodeUrl(url: string | URL, meta: URLMeta) {
return location.origin + self.$scramjet.config.prefix + url; return location.origin + self.$scramjet.config.prefix + url;
} else if (url.startsWith("data:")) { } else if (url.startsWith("data:")) {
return location.origin + self.$scramjet.config.prefix + url; return location.origin + self.$scramjet.config.prefix + url;
} else if (/^(#|mailto|about)/.test(url)) { } else if (url.startsWith("mailto:") || url.startsWith("about:")) {
// TODO this regex is jank but i'm not fixing it
return url; return url;
} else { } else {
let base = meta.base.href; 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) { export function decodeUrl(url: string | URL) {
if (url instanceof URL) { if (url instanceof URL) {
url = url.href; url = url.href;
} }
if ( const prefixed = location.origin + self.$scramjet.config.prefix;
tryCanParseURL(url)?.pathname.startsWith(
self.$scramjet.config.prefix + "worker"
)
) {
return new URL(new URL(url).searchParams.get("origin")).href;
}
// TODO: unrewrite rewritten blobs if (url.startsWith("javascript:")) {
if (/^(#|about|data|mailto|javascript)/.test(url)) { //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; return url;
} else if (tryCanParseURL(url)) { } else if (tryCanParseURL(url)) {
return self.$scramjet.codec.decode( return self.$scramjet.codec.decode(