rename encodeUrl to rewriteUrl

This commit is contained in:
velzie 2024-10-12 15:00:03 -04:00
parent 839b490c80
commit 3062db5df9
26 changed files with 78 additions and 75 deletions

View file

@ -10,8 +10,8 @@ import {
BareClient, BareClient,
CookieStore, CookieStore,
config, config,
decodeUrl, unrewriteUrl,
encodeUrl, rewriteUrl,
} from "../shared"; } from "../shared";
import type BareClientType from "@mercuryworkshop/bare-mux"; import type BareClientType from "@mercuryworkshop/bare-mux";
import { createWrapFn } from "./shared/wrap"; import { createWrapFn } from "./shared/wrap";
@ -199,7 +199,7 @@ export class ScramjetClient {
} }
get url(): URL { get url(): URL {
return new URL(decodeUrl(this.global.location.href)); return new URL(unrewriteUrl(this.global.location.href));
} }
set url(url: URL | string) { set url(url: URL | string) {
@ -211,7 +211,7 @@ export class ScramjetClient {
} }
if (ev.defaultPrevented) return; if (ev.defaultPrevented) return;
this.global.location.href = encodeUrl(ev.url, this.meta); this.global.location.href = rewriteUrl(ev.url, this.meta);
} }
// below are the utilities for proxying and trapping dom APIs // below are the utilities for proxying and trapping dom APIs

View file

@ -1,4 +1,4 @@
import { encodeUrl } from "../shared/rewriters/url"; import { rewriteUrl } from "../shared";
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { getOwnPropertyDescriptorHandler } from "./helpers"; import { getOwnPropertyDescriptorHandler } from "./helpers";
@ -22,7 +22,7 @@ export function createDocumentProxy(
}, },
set(target, prop, newValue) { set(target, prop, newValue) {
if (prop === "location") { if (prop === "location") {
location.href = encodeUrl(newValue, client.meta); location.href = rewriteUrl(newValue, client.meta);
return; return;
} }

View file

@ -1,9 +1,9 @@
import { SCRAMJETCLIENT } from "../../symbols"; import { SCRAMJETCLIENT } from "../../symbols";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { nativeGetOwnPropertyDescriptor } from "../natives"; import { nativeGetOwnPropertyDescriptor } from "../natives";
import { decodeUrl, htmlRules, unrewriteHtml } from "../../shared"; import { unrewriteUrl, htmlRules, unrewriteHtml } from "../../shared";
import { import {
encodeUrl, rewriteUrl,
rewriteCss, rewriteCss,
rewriteHtml, rewriteHtml,
rewriteJs, rewriteJs,
@ -57,7 +57,7 @@ export default function (client: ScramjetClient, self: typeof window) {
Object.defineProperty(element.prototype, attr, { Object.defineProperty(element.prototype, attr, {
get() { get() {
if (["src", "data", "href", "action", "formaction"].includes(attr)) { if (["src", "data", "href", "action", "formaction"].includes(attr)) {
return decodeUrl(descriptor.get.call(this)); return unrewriteUrl(descriptor.get.call(this));
} }
return descriptor.get.call(this); return descriptor.get.call(this);
@ -74,7 +74,7 @@ export default function (client: ScramjetClient, self: typeof window) {
let origin = new URL(value.substring("blob:".length)); let origin = new URL(value.substring("blob:".length));
value = "blob:" + location.origin + origin.pathname; value = "blob:" + location.origin + origin.pathname;
} else { } else {
value = encodeUrl(value, client.meta); value = rewriteUrl(value, client.meta);
} }
} else if (attr === "srcdoc") { } else if (attr === "srcdoc") {
value = rewriteHtml( value = rewriteHtml(
@ -117,7 +117,7 @@ export default function (client: ScramjetClient, self: typeof window) {
const href = desc.get.call(ctx.this); const href = desc.get.call(ctx.this);
if (!href) return href; if (!href) return href;
const url = new URL(decodeUrl(href)); const url = new URL(unrewriteUrl(href));
return url[prop]; return url[prop];
}, },

View file

@ -1,11 +1,11 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { encodeUrl } from "../../shared"; import { rewriteUrl } from "../../shared";
import { UrlChangeEvent } from "../events"; import { UrlChangeEvent } from "../events";
export default function (client: ScramjetClient, self: typeof globalThis) { export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("history.pushState", { client.Proxy("history.pushState", {
apply(ctx) { apply(ctx) {
if (ctx.args[2]) ctx.args[2] = encodeUrl(ctx.args[2], client.meta); if (ctx.args[2]) ctx.args[2] = rewriteUrl(ctx.args[2], client.meta);
ctx.call(); ctx.call();
const ev = new UrlChangeEvent(client.url.href); const ev = new UrlChangeEvent(client.url.href);
@ -15,7 +15,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("history.replaceState", { client.Proxy("history.replaceState", {
apply(ctx) { apply(ctx) {
if (ctx.args[2]) ctx.args[2] = encodeUrl(ctx.args[2], client.meta); if (ctx.args[2]) ctx.args[2] = rewriteUrl(ctx.args[2], client.meta);
ctx.call(); ctx.call();
const ev = new UrlChangeEvent(client.url.href); const ev = new UrlChangeEvent(client.url.href);

View file

@ -1,11 +1,11 @@
import { encodeUrl } from "../../shared"; import { rewriteUrl } from "../../shared";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { SCRAMJETCLIENT } from "../../symbols"; import { SCRAMJETCLIENT } from "../../symbols";
export default function (client: ScramjetClient) { export default function (client: ScramjetClient) {
client.Proxy("window.open", { client.Proxy("window.open", {
apply(ctx) { apply(ctx) {
if (ctx.args[0]) ctx.args[0] = encodeUrl(ctx.args[0], client.meta); if (ctx.args[0]) ctx.args[0] = rewriteUrl(ctx.args[0], client.meta);
if (["_parent", "_top", "_unfencedTop"].includes(ctx.args[1])) if (["_parent", "_top", "_unfencedTop"].includes(ctx.args[1]))
ctx.args[1] = "_self"; ctx.args[1] = "_self";

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { decodeUrl } from "../../shared"; import { unrewriteUrl } from "../../shared";
export default function (client: ScramjetClient, self: typeof window) { export default function (client: ScramjetClient, self: typeof window) {
client.Trap("origin", { client.Trap("origin", {

View file

@ -1,9 +1,10 @@
import { decodeUrl } from "../../shared"; import { unrewriteUrl } from "../../shared";
import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, self: typeof globalThis) { export default function (client: ScramjetClient, self: typeof globalThis) {
client.Trap("PerformanceEntry.prototype.name", { client.Trap("PerformanceEntry.prototype.name", {
get(ctx) { get(ctx) {
return decodeUrl(ctx.get()); return unrewriteUrl(ctx.get() as string);
}, },
}); });
} }

View file

@ -1,4 +1,4 @@
import { config, encodeUrl } from "../../shared"; import { config, rewriteUrl } from "../../shared";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { type MessageC2W } from "../../worker"; import { type MessageC2W } from "../../worker";
import { getOwnPropertyDescriptorHandler } from "../helpers"; import { getOwnPropertyDescriptorHandler } from "../helpers";
@ -61,7 +61,7 @@ export default function (client: ScramjetClient, self: Self) {
client.Proxy("navigator.serviceWorker.register", { client.Proxy("navigator.serviceWorker.register", {
apply(ctx) { apply(ctx) {
if (ctx.args[0] instanceof URL) ctx.args[0] = ctx.args[0].href; if (ctx.args[0] instanceof URL) ctx.args[0] = ctx.args[0].href;
let url = encodeUrl(ctx.args[0], client.meta) + "?dest=serviceworker"; let url = rewriteUrl(ctx.args[0], client.meta) + "?dest=serviceworker";
if (ctx.args[1] && ctx.args[1].type === "module") { if (ctx.args[1] && ctx.args[1].type === "module") {
url += "&type=module"; url += "&type=module";
} }

View file

@ -1,7 +1,7 @@
// @ts-nocheck // @ts-nocheck
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { nativeGetOwnPropertyDescriptor } from "./natives"; import { nativeGetOwnPropertyDescriptor } from "./natives";
import { decodeUrl, encodeUrl } from "../shared"; import { unrewriteUrl, rewriteUrl } from "../shared";
import { iswindow } from "."; import { iswindow } from ".";
export function createLocationProxy( export function createLocationProxy(
@ -77,7 +77,7 @@ export function createLocationProxy(
if (self.location.assign) if (self.location.assign)
fakeLocation.assign = new Proxy(self.location.assign, { fakeLocation.assign = new Proxy(self.location.assign, {
apply(target, thisArg, args) { apply(target, thisArg, args) {
args[0] = encodeUrl(args[0], client.meta); args[0] = rewriteUrl(args[0], client.meta);
Reflect.apply(target, self.location, args); Reflect.apply(target, self.location, args);
}, },
}); });
@ -90,7 +90,7 @@ export function createLocationProxy(
if (self.location.replace) if (self.location.replace)
fakeLocation.replace = new Proxy(self.location.replace, { fakeLocation.replace = new Proxy(self.location.replace, {
apply(target, thisArg, args) { apply(target, thisArg, args) {
args[0] = encodeUrl(args[0], client.meta); args[0] = rewriteUrl(args[0], client.meta);
Reflect.apply(target, self.location, args); Reflect.apply(target, self.location, args);
}, },
}); });

View file

@ -1,4 +1,4 @@
import { config, decodeUrl } from "../../shared"; import { config, unrewriteUrl } from "../../shared";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
export const enabled = () => self.$scramjet.config.flags.cleanerrors; export const enabled = () => self.$scramjet.config.flags.cleanerrors;
@ -20,7 +20,7 @@ export default function (client: ScramjetClient, _self: Self) {
} }
try { try {
newstack = newstack.replaceAll(url, decodeUrl(url)); newstack = newstack.replaceAll(url, unrewriteUrl(url));
} catch {} } catch {}
} }

View file

@ -1,6 +1,6 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { config } from "../../shared"; import { config } from "../../shared";
import { encodeUrl } from "../../shared/rewriters/url"; import { rewriteUrl } from "../../shared/rewriters/url";
export default function (client: ScramjetClient, self: Self) { export default function (client: ScramjetClient, self: Self) {
const Function = client.natives.Function; const Function = client.natives.Function;
@ -9,7 +9,9 @@ export default function (client: ScramjetClient, self: Self) {
return function (url: string) { return function (url: string) {
const resolved = new URL(url, base).href; const resolved = new URL(url, base).href;
return Function(`return import("${encodeUrl(resolved, client.meta)}")`)(); return Function(
`return import("${rewriteUrl(resolved, client.meta)}")`
)();
}; };
}; };

View file

@ -1,10 +1,10 @@
import { encodeUrl } from "../../../shared/rewriters/url"; import { rewriteUrl } from "../../../shared";
import { ScramjetClient } from "../../client"; import { ScramjetClient } from "../../client";
export default function (client: ScramjetClient, self) { export default function (client: ScramjetClient, self) {
client.Proxy("navigator.sendBeacon", { client.Proxy("navigator.sendBeacon", {
apply(ctx) { apply(ctx) {
ctx.args[0] = encodeUrl(ctx.args[0], client.meta); ctx.args[0] = rewriteUrl(ctx.args[0], client.meta);
}, },
}); });
} }

View file

@ -1,16 +1,16 @@
import { decodeUrl, encodeUrl } from "../../../shared"; import { unrewriteUrl, rewriteUrl } from "../../../shared";
import { ScramjetClient } from "../../client"; import { ScramjetClient } from "../../client";
export default function (client: ScramjetClient) { export default function (client: ScramjetClient) {
client.Proxy("EventSource", { client.Proxy("EventSource", {
construct(ctx) { construct(ctx) {
ctx.args[0] = encodeUrl(ctx.args[0], client.meta); ctx.args[0] = rewriteUrl(ctx.args[0], client.meta);
}, },
}); });
client.Trap("EventSource.prototype.url", { client.Trap("EventSource.prototype.url", {
get(ctx) { get(ctx) {
decodeUrl(ctx.get() as string); unrewriteUrl(ctx.get() as string);
}, },
}); });
} }

View file

@ -1,15 +1,15 @@
// ts throws an error if you dont do window.fetch // ts throws an error if you dont do window.fetch
import { isemulatedsw } from "../.."; import { isemulatedsw } from "../..";
import { decodeUrl } from "../../../shared/rewriters/url"; import { unrewriteUrl } from "../../../shared";
import { ScramjetClient } from "../../client"; import { ScramjetClient } from "../../client";
import { encodeUrl, rewriteHeaders } from "../../../shared"; import { rewriteUrl, rewriteHeaders } from "../../../shared";
export default function (client: ScramjetClient, self: typeof globalThis) { export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("fetch", { client.Proxy("fetch", {
apply(ctx) { apply(ctx) {
if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) { if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) {
ctx.args[0] = encodeUrl(ctx.args[0].toString(), client.meta); ctx.args[0] = rewriteUrl(ctx.args[0].toString(), client.meta);
if (isemulatedsw) ctx.args[0] += "?from=swruntime"; if (isemulatedsw) ctx.args[0] += "?from=swruntime";
} }
@ -25,7 +25,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("Request", { client.Proxy("Request", {
construct(ctx) { construct(ctx) {
if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) { if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) {
ctx.args[0] = encodeUrl(ctx.args[0].toString(), client.meta); ctx.args[0] = rewriteUrl(ctx.args[0].toString(), client.meta);
if (isemulatedsw) ctx.args[0] += "?from=swruntime"; if (isemulatedsw) ctx.args[0] += "?from=swruntime";
} }
@ -34,13 +34,13 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Trap("Response.prototype.url", { client.Trap("Response.prototype.url", {
get(ctx) { get(ctx) {
return decodeUrl(ctx.get() as string); return unrewriteUrl(ctx.get() as string);
}, },
}); });
client.Trap("Request.prototype.url", { client.Trap("Request.prototype.url", {
get(ctx) { get(ctx) {
return decodeUrl(ctx.get() as string); return unrewriteUrl(ctx.get() as string);
}, },
}); });

View file

@ -1,4 +1,4 @@
import { config, decodeUrl, encodeUrl } from "../../../shared"; import { config, unrewriteUrl, rewriteUrl } from "../../../shared";
import { ScramjetClient } from "../../client"; import { ScramjetClient } from "../../client";
let nativeworker; let nativeworker;
let postmessage; let postmessage;
@ -18,7 +18,7 @@ export default function (client: ScramjetClient, self: Self) {
client.Proxy("XMLHttpRequest.prototype.open", { client.Proxy("XMLHttpRequest.prototype.open", {
apply(ctx) { apply(ctx) {
if (ctx.args[1]) ctx.args[1] = encodeUrl(ctx.args[1], client.meta); if (ctx.args[1]) ctx.args[1] = rewriteUrl(ctx.args[1], client.meta);
ctx.this[ARGS] = ctx.args; ctx.this[ARGS] = ctx.args;
}, },
}); });
@ -128,7 +128,7 @@ export default function (client: ScramjetClient, self: Self) {
client.Trap("XMLHttpRequest.prototype.responseURL", { client.Trap("XMLHttpRequest.prototype.responseURL", {
get(ctx) { get(ctx) {
return decodeUrl(ctx.get() as string); return unrewriteUrl(ctx.get() as string);
}, },
}); });
} }

View file

@ -1,6 +1,6 @@
import { iswindow } from ".."; import { iswindow } from "..";
import { BareMuxConnection } from "../../shared"; import { BareMuxConnection } from "../../shared";
import { encodeUrl } from "../../shared/rewriters/url"; import { rewriteUrl } from "../../shared";
import type { MessageC2W } from "../../worker"; import type { MessageC2W } from "../../worker";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
@ -14,7 +14,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
construct({ args, call }) { construct({ args, call }) {
if (args[0] instanceof URL) args[0] = args[0].href; if (args[0] instanceof URL) args[0] = args[0].href;
args[0] = encodeUrl(args[0], client.meta) + "?dest=worker"; args[0] = rewriteUrl(args[0], client.meta) + "?dest=worker";
if (args[1] && args[1].type === "module") { if (args[1] && args[1].type === "module") {
args[0] += "&type=module"; args[0] += "&type=module";
@ -56,7 +56,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
if (iswindow) { if (iswindow) {
client.Proxy("Worklet.prototype.addModule", { client.Proxy("Worklet.prototype.addModule", {
apply(ctx) { apply(ctx) {
if (ctx.args[0]) ctx.args[0] = encodeUrl(ctx.args[0], client.meta); if (ctx.args[0]) ctx.args[0] = rewriteUrl(ctx.args[0], client.meta);
}, },
}); });

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { decodeUrl } from "../shared"; import { unrewriteUrl } from "../shared";
export class ScramjetServiceWorkerRuntime { export class ScramjetServiceWorkerRuntime {
recvport: MessagePort; recvport: MessagePort;
@ -72,7 +72,7 @@ function handleMessage(
const request = data.scramjet$request; const request = data.scramjet$request;
const Request = client.natives["Request"]; const Request = client.natives["Request"];
const fakeRequest = new Request(decodeUrl(request.url), { const fakeRequest = new Request(unrewriteUrl(request.url), {
body: request.body, body: request.body,
headers: new Headers(request.headers), headers: new Headers(request.headers),
method: request.method, method: request.method,

View file

@ -1,11 +1,11 @@
import { encodeUrl } from "../../shared"; import { rewriteUrl } from "../../shared";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, _self: Self) { export default function (client: ScramjetClient, _self: Self) {
client.Proxy("importScripts", { client.Proxy("importScripts", {
apply(ctx) { apply(ctx) {
for (const i in ctx.args) { for (const i in ctx.args) {
ctx.args[i] = encodeUrl(ctx.args[i], client.meta); ctx.args[i] = rewriteUrl(ctx.args[i], client.meta);
} }
}, },
}); });

View file

@ -1,6 +1,6 @@
export const { export const {
util: { BareClient, ScramjetHeaders, BareMuxConnection }, util: { BareClient, ScramjetHeaders, BareMuxConnection },
url: { encodeUrl, decodeUrl }, url: { rewriteUrl, unrewriteUrl },
rewrite: { rewrite: {
rewriteCss, rewriteCss,
unrewriteCss, unrewriteCss,

View file

@ -1,4 +1,4 @@
import { encodeUrl, decodeUrl } from "./rewriters/url"; import { rewriteUrl, unrewriteUrl } from "./rewriters/url";
import { rewriteCss, unrewriteCss } from "./rewriters/css"; import { rewriteCss, unrewriteCss } from "./rewriters/css";
import { rewriteHtml, rewriteSrcset } from "./rewriters/html"; import { rewriteHtml, rewriteSrcset } from "./rewriters/html";
import { rewriteJs } from "./rewriters/js"; import { rewriteJs } from "./rewriters/js";
@ -18,8 +18,8 @@ self.$scramjet.shared = {
ScramjetHeaders, ScramjetHeaders,
}, },
url: { url: {
encodeUrl, rewriteUrl,
decodeUrl, unrewriteUrl,
}, },
rewrite: { rewrite: {
rewriteCss, rewriteCss,

View file

@ -1,7 +1,7 @@
// This CSS rewriter uses code from Meteor // This CSS rewriter uses code from Meteor
// You can find the original source code at https://github.com/MeteorProxy/Meteor // You can find the original source code at https://github.com/MeteorProxy/Meteor
import { URLMeta, encodeUrl, decodeUrl } from "./url"; import { URLMeta, rewriteUrl, unrewriteUrl } from "./url";
export function rewriteCss(css: string, meta: URLMeta) { export function rewriteCss(css: string, meta: URLMeta) {
const regex = const regex =
@ -18,7 +18,7 @@ export function rewriteCss(css: string, meta: URLMeta) {
importContent importContent
) => { ) => {
const url = urlContent || importContent; const url = urlContent || importContent;
const encodedUrl = encodeUrl(url.trim(), meta); const encodedUrl = rewriteUrl(url.trim(), meta);
if (importStatement) { if (importStatement) {
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`; return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
@ -48,7 +48,7 @@ export function unrewriteCss(css: string) {
importContent importContent
) => { ) => {
const url = urlContent || importContent; const url = urlContent || importContent;
const encodedUrl = decodeUrl(url.trim()); const encodedUrl = unrewriteUrl(url.trim());
if (importStatement) { if (importStatement) {
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`; return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;

View file

@ -1,6 +1,6 @@
// TODO this whole file should be inlined and deleted it's a weird relic from ssd era // TODO this whole file should be inlined and deleted it's a weird relic from ssd era
import { URLMeta, encodeUrl } from "./url"; import { URLMeta, rewriteUrl } from "./url";
import { BareHeaders } from "@mercuryworkshop/bare-mux"; import { BareHeaders } from "@mercuryworkshop/bare-mux";
const cspHeaders = [ const cspHeaders = [
"cross-origin-embedder-policy", "cross-origin-embedder-policy",
@ -27,7 +27,7 @@ const cspHeaders = [
const urlHeaders = ["location", "content-location", "referer"]; const urlHeaders = ["location", "content-location", "referer"];
function rewriteLinkHeader(link: string, meta: URLMeta) { function rewriteLinkHeader(link: string, meta: URLMeta) {
return link.replace(/<(.*)>/gi, (match) => encodeUrl(match, meta)); return link.replace(/<(.*)>/gi, (match) => rewriteUrl(match, meta));
} }
export function rewriteHeaders(rawHeaders: BareHeaders, meta: URLMeta) { export function rewriteHeaders(rawHeaders: BareHeaders, meta: URLMeta) {
@ -43,7 +43,7 @@ export function rewriteHeaders(rawHeaders: BareHeaders, meta: URLMeta) {
urlHeaders.forEach((header) => { urlHeaders.forEach((header) => {
if (headers[header]) if (headers[header])
headers[header] = encodeUrl(headers[header]?.toString() as string, meta); headers[header] = rewriteUrl(headers[header]?.toString() as string, meta);
}); });
if (typeof headers["link"] === "string") { if (typeof headers["link"] === "string") {

View file

@ -1,7 +1,7 @@
import { ElementType, Parser } from "htmlparser2"; import { ElementType, Parser } from "htmlparser2";
import { ChildNode, DomHandler, Element } from "domhandler"; import { ChildNode, DomHandler, Element } from "domhandler";
import render from "dom-serializer"; import render from "dom-serializer";
import { URLMeta, encodeUrl } from "./url"; import { URLMeta, rewriteUrl } from "./url";
import { rewriteCss } from "./css"; import { rewriteCss } from "./css";
import { rewriteJs } from "./js"; import { rewriteJs } from "./js";
import { CookieStore } from "../cookie"; import { CookieStore } from "../cookie";
@ -109,7 +109,7 @@ export const htmlRules: {
}[] = [ }[] = [
{ {
fn: (value: string, meta: URLMeta) => { fn: (value: string, meta: URLMeta) => {
return encodeUrl(value, meta); return rewriteUrl(value, meta);
}, },
// url rewrites // url rewrites
@ -238,7 +238,7 @@ function traverseParsedHtml(
) { ) {
const contentArray = node.attribs.content.split("url="); const contentArray = node.attribs.content.split("url=");
if (contentArray[1]) if (contentArray[1])
contentArray[1] = encodeUrl(contentArray[1].trim(), meta); contentArray[1] = rewriteUrl(contentArray[1].trim(), meta);
node.attribs.content = contentArray.join("url="); node.attribs.content = contentArray.join("url=");
} }
} }
@ -263,7 +263,7 @@ export function rewriteSrcset(srcset: string, meta: URLMeta) {
if (!sufixes) return ""; if (!sufixes) return "";
const rewrittenUrls = urls.map((url, i) => { const rewrittenUrls = urls.map((url, i) => {
if (url && sufixes[i]) { if (url && sufixes[i]) {
return encodeUrl(url, meta) + sufixes[i]; return rewriteUrl(url, meta) + sufixes[i];
} }
}); });

View file

@ -23,7 +23,7 @@ export function unrewriteBlob(url: string) {
return "blob:" + location.origin + blob.pathname; return "blob:" + location.origin + blob.pathname;
} }
export function encodeUrl(url: string | URL, meta: URLMeta) { export function rewriteUrl(url: string | URL, meta: URLMeta) {
if (url instanceof URL) { if (url instanceof URL) {
url = url.href; url = url.href;
} }
@ -39,7 +39,7 @@ export function encodeUrl(url: string | URL, meta: URLMeta) {
} else { } else {
let base = meta.base.href; let base = meta.base.href;
if (base.startsWith("about:")) base = decodeUrl(self.location.href); // jank!!!!! weird jank!!! if (base.startsWith("about:")) base = unrewriteUrl(self.location.href); // jank!!!!! weird jank!!!
return ( return (
location.origin + location.origin +
@ -49,7 +49,7 @@ export function encodeUrl(url: string | URL, meta: URLMeta) {
} }
} }
export function decodeUrl(url: string | URL) { export function unrewriteUrl(url: string | URL) {
if (url instanceof URL) { if (url instanceof URL) {
url = url.href; url = url.href;
} }

6
src/types.d.ts vendored
View file

@ -1,5 +1,5 @@
import { ScramjetController } from "./controller/index"; import { ScramjetController } from "./controller/index";
import { encodeUrl, decodeUrl } from "./shared/rewriters/url"; import { rewriteUrl, unrewriteUrl } from "./shared/rewriters/url";
import { rewriteCss, unrewriteCss } from "./shared/rewriters/css"; import { rewriteCss, unrewriteCss } from "./shared/rewriters/css";
import { import {
htmlRules, htmlRules,
@ -55,8 +55,8 @@ declare global {
$scramjet: { $scramjet: {
shared: { shared: {
url: { url: {
encodeUrl: typeof encodeUrl; rewriteUrl: typeof rewriteUrl;
decodeUrl: typeof decodeUrl; unrewriteUrl: typeof unrewriteUrl;
}; };
rewrite: { rewrite: {
rewriteCss: typeof rewriteCss; rewriteCss: typeof rewriteCss;

View file

@ -5,8 +5,8 @@ import { FakeServiceWorker } from "./fakesw";
import { CookieStore } from "../shared/cookie"; import { CookieStore } from "../shared/cookie";
import { import {
ScramjetHeaders, ScramjetHeaders,
decodeUrl, unrewriteUrl,
encodeUrl, rewriteUrl,
rewriteCss, rewriteCss,
rewriteHeaders, rewriteHeaders,
rewriteHtml, rewriteHtml,
@ -32,7 +32,7 @@ export async function swfetch(
if (urlParam.has("url")) { if (urlParam.has("url")) {
return Response.redirect( return Response.redirect(
encodeUrl(urlParam.get("url"), newmeta(new URL(urlParam.get("url")))) rewriteUrl(urlParam.get("url"), newmeta(new URL(urlParam.get("url"))))
); );
} }
@ -88,7 +88,7 @@ export async function swfetch(
}); });
} }
const url = new URL(decodeUrl(requesturl)); const url = new URL(unrewriteUrl(requesturl));
const activeWorker: FakeServiceWorker | null = this.serviceWorkers.find( const activeWorker: FakeServiceWorker | null = this.serviceWorkers.find(
(w) => w.origin === url.origin (w) => w.origin === url.origin
@ -119,7 +119,7 @@ export async function swfetch(
new URL(client.url).pathname.startsWith(self.$scramjet.config.prefix) new URL(client.url).pathname.startsWith(self.$scramjet.config.prefix)
) { ) {
// TODO: i was against cors emulation but we might actually break stuff if we send full origin/referrer always // TODO: i was against cors emulation but we might actually break stuff if we send full origin/referrer always
const clientURL = new URL(decodeUrl(client.url)); const clientURL = new URL(unrewriteUrl(client.url));
if (clientURL.toString().includes("youtube.com")) { if (clientURL.toString().includes("youtube.com")) {
// console.log(headers); // console.log(headers);
} else { } else {
@ -165,7 +165,7 @@ export async function swfetch(
if (!["document", "iframe"].includes(request.destination)) if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 }); return new Response(undefined, { status: 500 });
return renderError(err, decodeUrl(request.url)); return renderError(err, unrewriteUrl(request.url));
} }
} }