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,
CookieStore,
config,
decodeUrl,
encodeUrl,
unrewriteUrl,
rewriteUrl,
} from "../shared";
import type BareClientType from "@mercuryworkshop/bare-mux";
import { createWrapFn } from "./shared/wrap";
@ -199,7 +199,7 @@ export class ScramjetClient {
}
get url(): URL {
return new URL(decodeUrl(this.global.location.href));
return new URL(unrewriteUrl(this.global.location.href));
}
set url(url: URL | string) {
@ -211,7 +211,7 @@ export class ScramjetClient {
}
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

View file

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

View file

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

View file

@ -1,11 +1,11 @@
import { ScramjetClient } from "../client";
import { encodeUrl } from "../../shared";
import { rewriteUrl } from "../../shared";
import { UrlChangeEvent } from "../events";
export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("history.pushState", {
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();
const ev = new UrlChangeEvent(client.url.href);
@ -15,7 +15,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("history.replaceState", {
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();
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 "../../symbols";
export default function (client: ScramjetClient) {
client.Proxy("window.open", {
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]))
ctx.args[1] = "_self";

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client";
import { decodeUrl } from "../../shared";
import { unrewriteUrl } from "../../shared";
export default function (client: ScramjetClient, self: typeof window) {
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) {
client.Trap("PerformanceEntry.prototype.name", {
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 { type MessageC2W } from "../../worker";
import { getOwnPropertyDescriptorHandler } from "../helpers";
@ -61,7 +61,7 @@ export default function (client: ScramjetClient, self: Self) {
client.Proxy("navigator.serviceWorker.register", {
apply(ctx) {
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") {
url += "&type=module";
}

View file

@ -1,7 +1,7 @@
// @ts-nocheck
import { ScramjetClient } from "./client";
import { nativeGetOwnPropertyDescriptor } from "./natives";
import { decodeUrl, encodeUrl } from "../shared";
import { unrewriteUrl, rewriteUrl } from "../shared";
import { iswindow } from ".";
export function createLocationProxy(
@ -77,7 +77,7 @@ export function createLocationProxy(
if (self.location.assign)
fakeLocation.assign = new Proxy(self.location.assign, {
apply(target, thisArg, args) {
args[0] = encodeUrl(args[0], client.meta);
args[0] = rewriteUrl(args[0], client.meta);
Reflect.apply(target, self.location, args);
},
});
@ -90,7 +90,7 @@ export function createLocationProxy(
if (self.location.replace)
fakeLocation.replace = new Proxy(self.location.replace, {
apply(target, thisArg, args) {
args[0] = encodeUrl(args[0], client.meta);
args[0] = rewriteUrl(args[0], client.meta);
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";
export const enabled = () => self.$scramjet.config.flags.cleanerrors;
@ -20,7 +20,7 @@ export default function (client: ScramjetClient, _self: Self) {
}
try {
newstack = newstack.replaceAll(url, decodeUrl(url));
newstack = newstack.replaceAll(url, unrewriteUrl(url));
} catch {}
}

View file

@ -1,6 +1,6 @@
import { ScramjetClient } from "../client";
import { config } from "../../shared";
import { encodeUrl } from "../../shared/rewriters/url";
import { rewriteUrl } from "../../shared/rewriters/url";
export default function (client: ScramjetClient, self: Self) {
const Function = client.natives.Function;
@ -9,7 +9,9 @@ export default function (client: ScramjetClient, self: Self) {
return function (url: string) {
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";
export default function (client: ScramjetClient, self) {
client.Proxy("navigator.sendBeacon", {
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";
export default function (client: ScramjetClient) {
client.Proxy("EventSource", {
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", {
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
import { isemulatedsw } from "../..";
import { decodeUrl } from "../../../shared/rewriters/url";
import { unrewriteUrl } from "../../../shared";
import { ScramjetClient } from "../../client";
import { encodeUrl, rewriteHeaders } from "../../../shared";
import { rewriteUrl, rewriteHeaders } from "../../../shared";
export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("fetch", {
apply(ctx) {
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";
}
@ -25,7 +25,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("Request", {
construct(ctx) {
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";
}
@ -34,13 +34,13 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Trap("Response.prototype.url", {
get(ctx) {
return decodeUrl(ctx.get() as string);
return unrewriteUrl(ctx.get() as string);
},
});
client.Trap("Request.prototype.url", {
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";
let nativeworker;
let postmessage;
@ -18,7 +18,7 @@ export default function (client: ScramjetClient, self: Self) {
client.Proxy("XMLHttpRequest.prototype.open", {
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;
},
});
@ -128,7 +128,7 @@ export default function (client: ScramjetClient, self: Self) {
client.Trap("XMLHttpRequest.prototype.responseURL", {
get(ctx) {
return decodeUrl(ctx.get() as string);
return unrewriteUrl(ctx.get() as string);
},
});
}

View file

@ -1,6 +1,6 @@
import { iswindow } from "..";
import { BareMuxConnection } from "../../shared";
import { encodeUrl } from "../../shared/rewriters/url";
import { rewriteUrl } from "../../shared";
import type { MessageC2W } from "../../worker";
import { ScramjetClient } from "../client";
@ -14,7 +14,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
construct({ args, call }) {
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") {
args[0] += "&type=module";
@ -56,7 +56,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
if (iswindow) {
client.Proxy("Worklet.prototype.addModule", {
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 { decodeUrl } from "../shared";
import { unrewriteUrl } from "../shared";
export class ScramjetServiceWorkerRuntime {
recvport: MessagePort;
@ -72,7 +72,7 @@ function handleMessage(
const request = data.scramjet$request;
const Request = client.natives["Request"];
const fakeRequest = new Request(decodeUrl(request.url), {
const fakeRequest = new Request(unrewriteUrl(request.url), {
body: request.body,
headers: new Headers(request.headers),
method: request.method,

View file

@ -1,11 +1,11 @@
import { encodeUrl } from "../../shared";
import { rewriteUrl } from "../../shared";
import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, _self: Self) {
client.Proxy("importScripts", {
apply(ctx) {
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 {
util: { BareClient, ScramjetHeaders, BareMuxConnection },
url: { encodeUrl, decodeUrl },
url: { rewriteUrl, unrewriteUrl },
rewrite: {
rewriteCss,
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 { rewriteHtml, rewriteSrcset } from "./rewriters/html";
import { rewriteJs } from "./rewriters/js";
@ -18,8 +18,8 @@ self.$scramjet.shared = {
ScramjetHeaders,
},
url: {
encodeUrl,
decodeUrl,
rewriteUrl,
unrewriteUrl,
},
rewrite: {
rewriteCss,

View file

@ -1,7 +1,7 @@
// This CSS rewriter uses code from 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) {
const regex =
@ -18,7 +18,7 @@ export function rewriteCss(css: string, meta: URLMeta) {
importContent
) => {
const url = urlContent || importContent;
const encodedUrl = encodeUrl(url.trim(), meta);
const encodedUrl = rewriteUrl(url.trim(), meta);
if (importStatement) {
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
@ -48,7 +48,7 @@ export function unrewriteCss(css: string) {
importContent
) => {
const url = urlContent || importContent;
const encodedUrl = decodeUrl(url.trim());
const encodedUrl = unrewriteUrl(url.trim());
if (importStatement) {
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
import { URLMeta, encodeUrl } from "./url";
import { URLMeta, rewriteUrl } from "./url";
import { BareHeaders } from "@mercuryworkshop/bare-mux";
const cspHeaders = [
"cross-origin-embedder-policy",
@ -27,7 +27,7 @@ const cspHeaders = [
const urlHeaders = ["location", "content-location", "referer"];
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) {
@ -43,7 +43,7 @@ export function rewriteHeaders(rawHeaders: BareHeaders, meta: URLMeta) {
urlHeaders.forEach((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") {

View file

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

6
src/types.d.ts vendored
View file

@ -1,5 +1,5 @@
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 {
htmlRules,
@ -55,8 +55,8 @@ declare global {
$scramjet: {
shared: {
url: {
encodeUrl: typeof encodeUrl;
decodeUrl: typeof decodeUrl;
rewriteUrl: typeof rewriteUrl;
unrewriteUrl: typeof unrewriteUrl;
};
rewrite: {
rewriteCss: typeof rewriteCss;

View file

@ -5,8 +5,8 @@ import { FakeServiceWorker } from "./fakesw";
import { CookieStore } from "../shared/cookie";
import {
ScramjetHeaders,
decodeUrl,
encodeUrl,
unrewriteUrl,
rewriteUrl,
rewriteCss,
rewriteHeaders,
rewriteHtml,
@ -32,7 +32,7 @@ export async function swfetch(
if (urlParam.has("url")) {
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(
(w) => w.origin === url.origin
@ -119,7 +119,7 @@ export async function swfetch(
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
const clientURL = new URL(decodeUrl(client.url));
const clientURL = new URL(unrewriteUrl(client.url));
if (clientURL.toString().includes("youtube.com")) {
// console.log(headers);
} else {
@ -165,7 +165,7 @@ export async function swfetch(
if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 });
return renderError(err, decodeUrl(request.url));
return renderError(err, unrewriteUrl(request.url));
}
}