mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
Merge branch 'main' of https://github.com/MercuryWorkshop/aerojet
This commit is contained in:
commit
7014c2595d
5 changed files with 24 additions and 17 deletions
0
rewriter/build.sh
Normal file → Executable file
0
rewriter/build.sh
Normal file → Executable file
|
@ -76,7 +76,7 @@ fastify.register(fastifyStatic, {
|
|||
});
|
||||
fastify.listen({
|
||||
port: process.env.PORT || 1337,
|
||||
// host: "0.0.0.0",
|
||||
host: "0.0.0.0",
|
||||
});
|
||||
|
||||
writeFileSync(
|
||||
|
|
|
@ -24,6 +24,10 @@ const cspHeaders = [
|
|||
|
||||
const urlHeaders = ["location", "content-location", "referer"];
|
||||
|
||||
function rewriteLinkHeader(link: string, origin?: URL) {
|
||||
return link.replace(/<(.*)>/gi, (match) => encodeUrl(match, origin));
|
||||
}
|
||||
|
||||
export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
||||
const headers = {};
|
||||
|
||||
|
@ -43,11 +47,13 @@ export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
|||
);
|
||||
});
|
||||
|
||||
// if (headers["link"]) {
|
||||
// headers["link"] = headers["link"].replace(/<(.*?)>/gi, (match) =>
|
||||
// encodeUrl(match, origin)
|
||||
// );
|
||||
// }
|
||||
if (typeof headers["link"] === "string") {
|
||||
headers["link"] = rewriteLinkHeader(headers["link"], origin);
|
||||
} else if (Array.isArray(headers["link"])) {
|
||||
headers["link"] = headers["link"].map((link) =>
|
||||
rewriteLinkHeader(link, origin)
|
||||
);
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
|
|
@ -20,7 +20,8 @@ Error.stackTraceLimit = 50;
|
|||
|
||||
global.rws = rewriteJs;
|
||||
export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
||||
if ("window" in globalThis) origin ??= new URL(decodeUrl(location.href));
|
||||
if ("window" in globalThis)
|
||||
origin = origin ?? new URL(decodeUrl(location.href));
|
||||
|
||||
const before = performance.now();
|
||||
if (typeof js === "string") {
|
||||
|
@ -48,7 +49,8 @@ export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
|||
//
|
||||
// if you can ensure all the preconditions are met this is faster than full rewrites
|
||||
export function rewriteJsNaiive(js: string | ArrayBuffer, origin?: URL) {
|
||||
if ("window" in globalThis) origin ??= new URL(decodeUrl(location.href));
|
||||
if ("window" in globalThis)
|
||||
origin = origin ?? new URL(decodeUrl(location.href));
|
||||
|
||||
if (typeof js !== "string") {
|
||||
js = new TextDecoder().decode(js);
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
import { rewriteJs } from "./js";
|
||||
|
||||
function canParseUrl(url: string, origin?: URL) {
|
||||
function tryCanParseURL(url: string, origin?: string | URL): URL | null {
|
||||
try {
|
||||
new URL(url, origin);
|
||||
|
||||
return true;
|
||||
return new URL(url, origin);
|
||||
} catch {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,7 +34,7 @@ export function encodeUrl(url: string | URL, origin?: URL) {
|
|||
return "javascript:" + rewriteJs(url.slice("javascript:".length), origin);
|
||||
} else if (/^(#|mailto|about|data|blob)/.test(url)) {
|
||||
return url;
|
||||
} else if (canParseUrl(url, origin)) {
|
||||
} else if (tryCanParseURL(url, origin)) {
|
||||
return (
|
||||
location.origin +
|
||||
self.$scramjet.config.prefix +
|
||||
|
@ -52,15 +50,16 @@ export function decodeUrl(url: string | URL) {
|
|||
}
|
||||
|
||||
if (
|
||||
URL.canParse(url) &&
|
||||
new URL(url).pathname.startsWith(self.$scramjet.config.prefix + "worker")
|
||||
tryCanParseURL(url)?.pathname.startsWith(
|
||||
self.$scramjet.config.prefix + "worker"
|
||||
)
|
||||
) {
|
||||
return new URL(new URL(url).searchParams.get("origin")).href;
|
||||
}
|
||||
|
||||
if (/^(#|about|data|mailto|javascript)/.test(url)) {
|
||||
return url;
|
||||
} else if (canParseUrl(url)) {
|
||||
} else if (tryCanParseURL(url)) {
|
||||
return self.$scramjet.codec.decode(
|
||||
url.slice((location.origin + self.$scramjet.config.prefix).length)
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue