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({
|
fastify.listen({
|
||||||
port: process.env.PORT || 1337,
|
port: process.env.PORT || 1337,
|
||||||
// host: "0.0.0.0",
|
host: "0.0.0.0",
|
||||||
});
|
});
|
||||||
|
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
|
|
|
@ -24,6 +24,10 @@ const cspHeaders = [
|
||||||
|
|
||||||
const urlHeaders = ["location", "content-location", "referer"];
|
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) {
|
export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
||||||
const headers = {};
|
const headers = {};
|
||||||
|
|
||||||
|
@ -43,11 +47,13 @@ export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// if (headers["link"]) {
|
if (typeof headers["link"] === "string") {
|
||||||
// headers["link"] = headers["link"].replace(/<(.*?)>/gi, (match) =>
|
headers["link"] = rewriteLinkHeader(headers["link"], origin);
|
||||||
// encodeUrl(match, origin)
|
} else if (Array.isArray(headers["link"])) {
|
||||||
// );
|
headers["link"] = headers["link"].map((link) =>
|
||||||
// }
|
rewriteLinkHeader(link, origin)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,8 @@ Error.stackTraceLimit = 50;
|
||||||
|
|
||||||
global.rws = rewriteJs;
|
global.rws = rewriteJs;
|
||||||
export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
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();
|
const before = performance.now();
|
||||||
if (typeof js === "string") {
|
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
|
// if you can ensure all the preconditions are met this is faster than full rewrites
|
||||||
export function rewriteJsNaiive(js: string | ArrayBuffer, origin?: URL) {
|
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") {
|
if (typeof js !== "string") {
|
||||||
js = new TextDecoder().decode(js);
|
js = new TextDecoder().decode(js);
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
import { rewriteJs } from "./js";
|
import { rewriteJs } from "./js";
|
||||||
|
|
||||||
function canParseUrl(url: string, origin?: URL) {
|
function tryCanParseURL(url: string, origin?: string | URL): URL | null {
|
||||||
try {
|
try {
|
||||||
new URL(url, origin);
|
return new URL(url, origin);
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch {
|
} 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);
|
return "javascript:" + rewriteJs(url.slice("javascript:".length), origin);
|
||||||
} else if (/^(#|mailto|about|data|blob)/.test(url)) {
|
} else if (/^(#|mailto|about|data|blob)/.test(url)) {
|
||||||
return url;
|
return url;
|
||||||
} else if (canParseUrl(url, origin)) {
|
} else if (tryCanParseURL(url, origin)) {
|
||||||
return (
|
return (
|
||||||
location.origin +
|
location.origin +
|
||||||
self.$scramjet.config.prefix +
|
self.$scramjet.config.prefix +
|
||||||
|
@ -52,15 +50,16 @@ export function decodeUrl(url: string | URL) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
URL.canParse(url) &&
|
tryCanParseURL(url)?.pathname.startsWith(
|
||||||
new URL(url).pathname.startsWith(self.$scramjet.config.prefix + "worker")
|
self.$scramjet.config.prefix + "worker"
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
return new URL(new URL(url).searchParams.get("origin")).href;
|
return new URL(new URL(url).searchParams.get("origin")).href;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (/^(#|about|data|mailto|javascript)/.test(url)) {
|
if (/^(#|about|data|mailto|javascript)/.test(url)) {
|
||||||
return url;
|
return url;
|
||||||
} else if (canParseUrl(url)) {
|
} else if (tryCanParseURL(url)) {
|
||||||
return self.$scramjet.codec.decode(
|
return self.$scramjet.codec.decode(
|
||||||
url.slice((location.origin + self.$scramjet.config.prefix).length)
|
url.slice((location.origin + self.$scramjet.config.prefix).length)
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue