misc refactors

This commit is contained in:
velzie 2024-08-31 11:01:51 -04:00
parent 819c1701d0
commit 0939dd2b57
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
25 changed files with 82 additions and 66 deletions

View file

@ -6,7 +6,7 @@ import { createGlobalProxy } from "./global";
import { getOwnPropertyDescriptorHandler } from "./helpers"; import { getOwnPropertyDescriptorHandler } from "./helpers";
import { createLocationProxy } from "./location"; import { createLocationProxy } from "./location";
import { nativeGetOwnPropertyDescriptor } from "./natives"; import { nativeGetOwnPropertyDescriptor } from "./natives";
import { CookieStore, config, decodeUrl } from "./shared"; import { CookieStore, config, decodeUrl, encodeUrl } from "../shared";
import { createWrapFn } from "./shared/wrap"; import { createWrapFn } from "./shared/wrap";
declare global { declare global {
@ -135,6 +135,20 @@ export class ScramjetClient {
} }
} }
get url(): URL {
return new URL(decodeUrl(self.location.href));
}
set url(url: URL | string) {
if (typeof url === "string") url = new URL(url);
self.location.href = encodeUrl(url.href);
}
// below are the utilities for proxying and trapping dom APIs
// you don't have to understand this it just makes the rest easier
// i'll document it eventually
Proxy(name: string | string[], handler: Proxy) { Proxy(name: string | string[], handler: Proxy) {
if (Array.isArray(name)) { if (Array.isArray(name)) {
for (const n of name) { for (const n of name) {
@ -320,8 +334,4 @@ export class ScramjetClient {
return oldDescriptor; return oldDescriptor;
} }
get url(): URL {
return new URL(decodeUrl(location.href));
}
} }

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { rewriteCss } from "../shared"; import { rewriteCss } from "../../shared";
const cssProperties = [ const cssProperties = [
"background", "background",

View file

@ -1,14 +1,14 @@
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 { config, decodeUrl, htmlRules, unrewriteHtml } from "../shared"; import { config, decodeUrl, htmlRules, unrewriteHtml } from "../../shared";
import { import {
encodeUrl, encodeUrl,
rewriteCss, rewriteCss,
rewriteHtml, rewriteHtml,
rewriteJs, rewriteJs,
rewriteSrcset, rewriteSrcset,
} from "../shared"; } from "../../shared";
export default function (client: ScramjetClient, self: typeof window) { export default function (client: ScramjetClient, self: typeof window) {
const attrObject = { const attrObject = {

View file

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

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { encodeUrl } from "../shared"; import { encodeUrl } from "../../shared";
export default function (client: ScramjetClient, self: typeof globalThis) { export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("history.pushState", { client.Proxy("history.pushState", {

View file

@ -1,4 +1,4 @@
import { encodeUrl } from "../shared"; import { encodeUrl } from "../../shared";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { SCRAMJETCLIENT } from "../../symbols"; import { SCRAMJETCLIENT } from "../../symbols";

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { decodeUrl } from "../shared"; import { decodeUrl } 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,4 +1,4 @@
import { encodeUrl } from "../shared"; import { config, encodeUrl } 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";
@ -6,7 +6,7 @@ import { getOwnPropertyDescriptorHandler } from "../helpers";
// we need a late order because we're mangling with addEventListener at a higher level // we need a late order because we're mangling with addEventListener at a higher level
export const order = 2; export const order = 2;
export const enabled = () => self.$scramjet.config.flags.serviceworkers; export const enabled = () => config.flags.serviceworkers;
export function disabled(client: ScramjetClient, self: Self) { export function disabled(client: ScramjetClient, self: Self) {
Reflect.deleteProperty(Navigator.prototype, "serviceWorker"); Reflect.deleteProperty(Navigator.prototype, "serviceWorker");
} }

View file

@ -1,7 +1,7 @@
import { encodeUrl } from "./shared"; import { encodeUrl } from "../shared";
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { indirectEval } from "./shared/eval"; import { indirectEval } from "./shared/eval";
import { config } from "./shared"; import { config } from "../shared";
import { getOwnPropertyDescriptorHandler } from "./helpers"; import { getOwnPropertyDescriptorHandler } from "./helpers";
export function createGlobalProxy( export function createGlobalProxy(
@ -18,7 +18,7 @@ export function createGlobalProxy(
prop prop
) )
) )
return self[config.wrapfn](self[prop]); return client.wrapfn(self[prop]);
if (prop === "$scramjet") return; if (prop === "$scramjet") return;
@ -31,7 +31,7 @@ export function createGlobalProxy(
set(target, prop, value) { set(target, prop, value) {
if (prop === "location") { if (prop === "location") {
location.href = encodeUrl(value); client.url = value;
return; return;
} }

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 { encodeUrl, decodeUrl } from "./shared"; import { encodeUrl, decodeUrl } from "../shared";
export function createLocationProxy( export function createLocationProxy(
client: ScramjetClient, client: ScramjetClient,
@ -39,7 +39,7 @@ export function createLocationProxy(
apply(target, thisArg, args) { apply(target, thisArg, args) {
let url = new URL(client.url.href); let url = new URL(client.url.href);
url[prop] = args[0]; url[prop] = args[0];
self.location.href = encodeUrl(url.href); client.url = url;
}, },
}); });
} }

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { config, rewriteJs } from "../shared"; import { config, rewriteJs } from "../../shared";
export default function (client: ScramjetClient, self: Self) { export default function (client: ScramjetClient, self: Self) {
// used for proxying *direct eval* // used for proxying *direct eval*

View file

@ -1,5 +1,5 @@
import { ScramjetClient, ProxyCtx, Proxy } from "../client"; import { ScramjetClient, ProxyCtx, Proxy } from "../client";
import { rewriteJs } from "../shared"; import { rewriteJs } from "../../shared";
function rewriteFunction(ctx: ProxyCtx) { function rewriteFunction(ctx: ProxyCtx) {
const stringifiedFunction = ctx.fn(...ctx.args).toString(); const stringifiedFunction = ctx.fn(...ctx.args).toString();

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { config, encodeUrl } from "../shared"; import { config, encodeUrl } from "../../shared";
export default function (client: ScramjetClient, self: Self) { export default function (client: ScramjetClient, self: Self) {
const Function = client.natives.Function; const Function = client.natives.Function;

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { config } from "../shared"; import { config } from "../../shared";
export const POLLUTANT = Symbol.for("scramjet realm pollutant"); export const POLLUTANT = Symbol.for("scramjet realm pollutant");

View file

@ -3,7 +3,7 @@
import { isemulatedsw } from "../.."; import { isemulatedsw } from "../..";
import { decodeUrl } from "../../../shared/rewriters/url"; import { decodeUrl } from "../../../shared/rewriters/url";
import { ScramjetClient } from "../../client"; import { ScramjetClient } from "../../client";
import { encodeUrl, rewriteHeaders } from "../../shared"; import { encodeUrl, 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", {

View file

@ -1,6 +1,6 @@
import { iswindow, isworker } from "../.."; import { iswindow, isworker } from "../..";
import { ScramjetClient } from "../../client"; import { ScramjetClient } from "../../client";
import { BareClient } from "../../shared"; import { BareClient } from "../../../shared";
const bare = iswindow && new BareClient(); const bare = iswindow && new BareClient();

View file

@ -1,4 +1,4 @@
import { encodeUrl, rewriteHeaders } from "../../shared"; import { encodeUrl, rewriteHeaders } from "../../../shared";
export default function (client, self) { export default function (client, self) {
client.Proxy("XMLHttpRequest.prototype.open", { client.Proxy("XMLHttpRequest.prototype.open", {

View file

@ -1,4 +1,4 @@
import { encodeUrl } from "../shared"; import { encodeUrl } from "../../shared";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, self: typeof globalThis) { export default function (client: ScramjetClient, self: typeof globalThis) {

View file

@ -1,7 +1,7 @@
import { iswindow, isworker } from ".."; import { iswindow, isworker } from "..";
import { SCRAMJETCLIENT } from "../../symbols"; import { SCRAMJETCLIENT } from "../../symbols";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { config } from "../shared"; import { config } from "../../shared";
export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
return function (identifier: any, args: any) { return function (identifier: any, args: any) {

View file

@ -1,5 +1,5 @@
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { decodeUrl, encodeUrl } from "./shared"; import { decodeUrl, encodeUrl } from "../shared";
export class ScramjetServiceWorkerRuntime { export class ScramjetServiceWorkerRuntime {
recvport: MessagePort; recvport: MessagePort;

View file

@ -1,4 +1,4 @@
import { encodeUrl } from "../shared"; import { encodeUrl } from "../../shared";
export default function (client, self) { export default function (client, self) {
client.Proxy("importScripts", { client.Proxy("importScripts", {

View file

@ -1,5 +1,5 @@
export const { export const {
util: { BareClient }, util: { BareClient, ScramjetHeaders },
url: { encodeUrl, decodeUrl }, url: { encodeUrl, decodeUrl },
rewrite: { rewrite: {
rewriteCss, rewriteCss,

View file

@ -7,6 +7,7 @@ import {
rewrite_js, rewrite_js,
rewrite_js_from_arraybuffer, rewrite_js_from_arraybuffer,
} from "../../../rewriter/out/rewriter.js"; } from "../../../rewriter/out/rewriter.js";
import { config } from "../../shared";
initSync( initSync(
new WebAssembly.Module( new WebAssembly.Module(
@ -57,7 +58,7 @@ export function rewriteJsNaiive(js: string | ArrayBuffer, origin?: URL) {
} }
return ` return `
with (${self.$scramjet.config.wrapfn}(globalThis)) { with (${config.wrapfn}(globalThis)) {
${js} ${js}

View file

@ -5,35 +5,22 @@ import { MessageW2C, ScramjetServiceWorker } from ".";
import { renderError } from "./error"; import { renderError } from "./error";
import { FakeServiceWorker } from "./fakesw"; import { FakeServiceWorker } from "./fakesw";
import { CookieStore } from "../shared/cookie"; import { CookieStore } from "../shared/cookie";
import {
const { encodeUrl, decodeUrl } = self.$scramjet.shared.url; ScramjetHeaders,
const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = decodeUrl,
self.$scramjet.shared.rewrite; encodeUrl,
const { parseDomain, ScramjetHeaders } = self.$scramjet.shared.util; rewriteCss,
rewriteHeaders,
rewriteHtml,
rewriteJs,
rewriteWorkers,
} from "../shared";
export async function swfetch( export async function swfetch(
this: ScramjetServiceWorker, this: ScramjetServiceWorker,
{ request, clientId }: FetchEvent request: Request,
client: Client | null
) { ) {
if (new URL(request.url).pathname.startsWith("/scramjet/worker")) {
const dataurl = new URL(request.url).searchParams.get("data");
const res = await fetch(dataurl);
const ab = await res.arrayBuffer();
const origin = new URL(
decodeURIComponent(new URL(request.url).searchParams.get("origin"))
);
const rewritten = rewriteWorkers(ab, new URL(origin));
return new Response(rewritten, {
headers: {
"Content-Type": "application/javascript",
},
});
}
const client = await self.clients.get(clientId);
const urlParam = new URLSearchParams(new URL(request.url).search); const urlParam = new URLSearchParams(new URL(request.url).search);
if (urlParam.has("url")) { if (urlParam.has("url")) {

View file

@ -2,15 +2,11 @@ import IDBMap from "@webreflection/idb-map";
import { FakeServiceWorker } from "./fakesw"; import { FakeServiceWorker } from "./fakesw";
import { swfetch } from "./fetch"; import { swfetch } from "./fetch";
import { ScramjetThreadpool } from "./threadpool"; import { ScramjetThreadpool } from "./threadpool";
import type BareClient from "@mercuryworkshop/bare-mux";
declare global { import { rewriteWorkers } from "../shared";
interface Window {
ScramjetServiceWorker;
}
}
export class ScramjetServiceWorker { export class ScramjetServiceWorker {
client: typeof self.$scramjet.shared.util.BareClient.prototype; client: BareClient;
config: typeof self.$scramjet.config; config: typeof self.$scramjet.config;
threadpool: ScramjetThreadpool; threadpool: ScramjetThreadpool;
@ -112,7 +108,29 @@ export class ScramjetServiceWorker {
else return false; else return false;
} }
public fetch = swfetch; async fetch({ request, clientId }: FetchEvent) {
if (new URL(request.url).pathname.startsWith("/scramjet/worker")) {
const dataurl = new URL(request.url).searchParams.get("data");
const res = await fetch(dataurl);
const ab = await res.arrayBuffer();
const origin = new URL(
decodeURIComponent(new URL(request.url).searchParams.get("origin"))
);
const rewritten = rewriteWorkers(ab, new URL(origin));
return new Response(rewritten, {
headers: {
"Content-Type": "application/javascript",
},
});
}
const client = await self.clients.get(clientId);
return swfetch.call(this, request, client);
}
} }
self.ScramjetServiceWorker = ScramjetServiceWorker; self.ScramjetServiceWorker = ScramjetServiceWorker;