From 0939dd2b57b0ddd8cfb9b77d5a548ad738c445da Mon Sep 17 00:00:00 2001 From: velzie Date: Sat, 31 Aug 2024 11:01:51 -0400 Subject: [PATCH] misc refactors --- src/client/client.ts | 20 ++++++++--- src/client/dom/css.ts | 2 +- src/client/dom/element.ts | 4 +-- src/client/dom/fontface.ts | 2 +- src/client/dom/history.ts | 2 +- src/client/dom/open.ts | 2 +- src/client/dom/origin.ts | 2 +- src/client/dom/serviceworker.ts | 4 +-- src/client/global.ts | 8 ++--- src/client/location.ts | 4 +-- src/client/shared/eval.ts | 2 +- src/client/shared/function.ts | 2 +- src/client/shared/import.ts | 2 +- src/client/shared/realm.ts | 2 +- src/client/shared/requests/fetch.ts | 2 +- src/client/shared/requests/websocket.ts | 2 +- src/client/shared/requests/xmlhttprequest.ts | 2 +- src/client/shared/worker.ts | 2 +- src/client/shared/wrap.ts | 2 +- src/client/swruntime.ts | 2 +- src/client/worker/importScripts.ts | 2 +- src/{client => }/shared.ts | 2 +- src/shared/rewriters/js.ts | 3 +- src/worker/fetch.ts | 37 +++++++------------- src/worker/index.ts | 34 +++++++++++++----- 25 files changed, 82 insertions(+), 66 deletions(-) rename src/{client => }/shared.ts (87%) diff --git a/src/client/client.ts b/src/client/client.ts index 0f1ff29..510532d 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -6,7 +6,7 @@ import { createGlobalProxy } from "./global"; import { getOwnPropertyDescriptorHandler } from "./helpers"; import { createLocationProxy } from "./location"; import { nativeGetOwnPropertyDescriptor } from "./natives"; -import { CookieStore, config, decodeUrl } from "./shared"; +import { CookieStore, config, decodeUrl, encodeUrl } from "../shared"; import { createWrapFn } from "./shared/wrap"; 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) { if (Array.isArray(name)) { for (const n of name) { @@ -320,8 +334,4 @@ export class ScramjetClient { return oldDescriptor; } - - get url(): URL { - return new URL(decodeUrl(location.href)); - } } diff --git a/src/client/dom/css.ts b/src/client/dom/css.ts index 0701036..21884eb 100644 --- a/src/client/dom/css.ts +++ b/src/client/dom/css.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "../client"; -import { rewriteCss } from "../shared"; +import { rewriteCss } from "../../shared"; const cssProperties = [ "background", diff --git a/src/client/dom/element.ts b/src/client/dom/element.ts index a45b129..aa7e52f 100644 --- a/src/client/dom/element.ts +++ b/src/client/dom/element.ts @@ -1,14 +1,14 @@ import { SCRAMJETCLIENT } from "../../symbols"; import { ScramjetClient } from "../client"; import { nativeGetOwnPropertyDescriptor } from "../natives"; -import { config, decodeUrl, htmlRules, unrewriteHtml } from "../shared"; +import { config, decodeUrl, htmlRules, unrewriteHtml } from "../../shared"; import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset, -} from "../shared"; +} from "../../shared"; export default function (client: ScramjetClient, self: typeof window) { const attrObject = { diff --git a/src/client/dom/fontface.ts b/src/client/dom/fontface.ts index b0133b6..567c42f 100644 --- a/src/client/dom/fontface.ts +++ b/src/client/dom/fontface.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "../client"; -import { decodeUrl, rewriteCss } from "../shared"; +import { decodeUrl, rewriteCss } from "../../shared"; export default function (client: ScramjetClient, self: typeof window) { client.Proxy("FontFace", { diff --git a/src/client/dom/history.ts b/src/client/dom/history.ts index 4ca4d7f..69559dd 100644 --- a/src/client/dom/history.ts +++ b/src/client/dom/history.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "../client"; -import { encodeUrl } from "../shared"; +import { encodeUrl } from "../../shared"; export default function (client: ScramjetClient, self: typeof globalThis) { client.Proxy("history.pushState", { diff --git a/src/client/dom/open.ts b/src/client/dom/open.ts index 6b72c1c..5550276 100644 --- a/src/client/dom/open.ts +++ b/src/client/dom/open.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "../shared"; +import { encodeUrl } from "../../shared"; import { ScramjetClient } from "../client"; import { SCRAMJETCLIENT } from "../../symbols"; diff --git a/src/client/dom/origin.ts b/src/client/dom/origin.ts index 78b43c6..0245eb7 100644 --- a/src/client/dom/origin.ts +++ b/src/client/dom/origin.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "../client"; -import { decodeUrl } from "../shared"; +import { decodeUrl } from "../../shared"; export default function (client: ScramjetClient, self: typeof window) { client.Trap("origin", { diff --git a/src/client/dom/serviceworker.ts b/src/client/dom/serviceworker.ts index 5f5c4eb..2d2c29c 100644 --- a/src/client/dom/serviceworker.ts +++ b/src/client/dom/serviceworker.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "../shared"; +import { config, encodeUrl } from "../../shared"; import { ScramjetClient } from "../client"; import { type MessageC2W } from "../../worker"; 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 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) { Reflect.deleteProperty(Navigator.prototype, "serviceWorker"); } diff --git a/src/client/global.ts b/src/client/global.ts index d0b528f..51a971f 100644 --- a/src/client/global.ts +++ b/src/client/global.ts @@ -1,7 +1,7 @@ -import { encodeUrl } from "./shared"; +import { encodeUrl } from "../shared"; import { ScramjetClient } from "./client"; import { indirectEval } from "./shared/eval"; -import { config } from "./shared"; +import { config } from "../shared"; import { getOwnPropertyDescriptorHandler } from "./helpers"; export function createGlobalProxy( @@ -18,7 +18,7 @@ export function createGlobalProxy( prop ) ) - return self[config.wrapfn](self[prop]); + return client.wrapfn(self[prop]); if (prop === "$scramjet") return; @@ -31,7 +31,7 @@ export function createGlobalProxy( set(target, prop, value) { if (prop === "location") { - location.href = encodeUrl(value); + client.url = value; return; } diff --git a/src/client/location.ts b/src/client/location.ts index fe1a0d6..5457cd0 100644 --- a/src/client/location.ts +++ b/src/client/location.ts @@ -1,7 +1,7 @@ // @ts-nocheck import { ScramjetClient } from "./client"; import { nativeGetOwnPropertyDescriptor } from "./natives"; -import { encodeUrl, decodeUrl } from "./shared"; +import { encodeUrl, decodeUrl } from "../shared"; export function createLocationProxy( client: ScramjetClient, @@ -39,7 +39,7 @@ export function createLocationProxy( apply(target, thisArg, args) { let url = new URL(client.url.href); url[prop] = args[0]; - self.location.href = encodeUrl(url.href); + client.url = url; }, }); } diff --git a/src/client/shared/eval.ts b/src/client/shared/eval.ts index 56e369b..cacd1ca 100644 --- a/src/client/shared/eval.ts +++ b/src/client/shared/eval.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "../client"; -import { config, rewriteJs } from "../shared"; +import { config, rewriteJs } from "../../shared"; export default function (client: ScramjetClient, self: Self) { // used for proxying *direct eval* diff --git a/src/client/shared/function.ts b/src/client/shared/function.ts index 9b94770..af489ab 100644 --- a/src/client/shared/function.ts +++ b/src/client/shared/function.ts @@ -1,5 +1,5 @@ import { ScramjetClient, ProxyCtx, Proxy } from "../client"; -import { rewriteJs } from "../shared"; +import { rewriteJs } from "../../shared"; function rewriteFunction(ctx: ProxyCtx) { const stringifiedFunction = ctx.fn(...ctx.args).toString(); diff --git a/src/client/shared/import.ts b/src/client/shared/import.ts index 87464bb..4325d99 100644 --- a/src/client/shared/import.ts +++ b/src/client/shared/import.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "../client"; -import { config, encodeUrl } from "../shared"; +import { config, encodeUrl } from "../../shared"; export default function (client: ScramjetClient, self: Self) { const Function = client.natives.Function; diff --git a/src/client/shared/realm.ts b/src/client/shared/realm.ts index 35564ee..ccf5d84 100644 --- a/src/client/shared/realm.ts +++ b/src/client/shared/realm.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "../client"; -import { config } from "../shared"; +import { config } from "../../shared"; export const POLLUTANT = Symbol.for("scramjet realm pollutant"); diff --git a/src/client/shared/requests/fetch.ts b/src/client/shared/requests/fetch.ts index 56c357e..c776db5 100644 --- a/src/client/shared/requests/fetch.ts +++ b/src/client/shared/requests/fetch.ts @@ -3,7 +3,7 @@ import { isemulatedsw } from "../.."; import { decodeUrl } from "../../../shared/rewriters/url"; import { ScramjetClient } from "../../client"; -import { encodeUrl, rewriteHeaders } from "../../shared"; +import { encodeUrl, rewriteHeaders } from "../../../shared"; export default function (client: ScramjetClient, self: typeof globalThis) { client.Proxy("fetch", { diff --git a/src/client/shared/requests/websocket.ts b/src/client/shared/requests/websocket.ts index 617396d..5440c23 100644 --- a/src/client/shared/requests/websocket.ts +++ b/src/client/shared/requests/websocket.ts @@ -1,6 +1,6 @@ import { iswindow, isworker } from "../.."; import { ScramjetClient } from "../../client"; -import { BareClient } from "../../shared"; +import { BareClient } from "../../../shared"; const bare = iswindow && new BareClient(); diff --git a/src/client/shared/requests/xmlhttprequest.ts b/src/client/shared/requests/xmlhttprequest.ts index 43242d6..6188d07 100644 --- a/src/client/shared/requests/xmlhttprequest.ts +++ b/src/client/shared/requests/xmlhttprequest.ts @@ -1,4 +1,4 @@ -import { encodeUrl, rewriteHeaders } from "../../shared"; +import { encodeUrl, rewriteHeaders } from "../../../shared"; export default function (client, self) { client.Proxy("XMLHttpRequest.prototype.open", { diff --git a/src/client/shared/worker.ts b/src/client/shared/worker.ts index 16b14c1..c131ce6 100644 --- a/src/client/shared/worker.ts +++ b/src/client/shared/worker.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "../shared"; +import { encodeUrl } from "../../shared"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient, self: typeof globalThis) { diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index 62abbe6..897ad30 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -1,7 +1,7 @@ import { iswindow, isworker } from ".."; import { SCRAMJETCLIENT } from "../../symbols"; import { ScramjetClient } from "../client"; -import { config } from "../shared"; +import { config } from "../../shared"; export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { return function (identifier: any, args: any) { diff --git a/src/client/swruntime.ts b/src/client/swruntime.ts index 7ba9dfe..c701f21 100644 --- a/src/client/swruntime.ts +++ b/src/client/swruntime.ts @@ -1,5 +1,5 @@ import { ScramjetClient } from "./client"; -import { decodeUrl, encodeUrl } from "./shared"; +import { decodeUrl, encodeUrl } from "../shared"; export class ScramjetServiceWorkerRuntime { recvport: MessagePort; diff --git a/src/client/worker/importScripts.ts b/src/client/worker/importScripts.ts index 8ed0219..ca62ec5 100644 --- a/src/client/worker/importScripts.ts +++ b/src/client/worker/importScripts.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "../shared"; +import { encodeUrl } from "../../shared"; export default function (client, self) { client.Proxy("importScripts", { diff --git a/src/client/shared.ts b/src/shared.ts similarity index 87% rename from src/client/shared.ts rename to src/shared.ts index 743abdb..57dd47d 100644 --- a/src/client/shared.ts +++ b/src/shared.ts @@ -1,5 +1,5 @@ export const { - util: { BareClient }, + util: { BareClient, ScramjetHeaders }, url: { encodeUrl, decodeUrl }, rewrite: { rewriteCss, diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index 0be28da..e45ade4 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -7,6 +7,7 @@ import { rewrite_js, rewrite_js_from_arraybuffer, } from "../../../rewriter/out/rewriter.js"; +import { config } from "../../shared"; initSync( new WebAssembly.Module( @@ -57,7 +58,7 @@ export function rewriteJsNaiive(js: string | ArrayBuffer, origin?: URL) { } return ` - with (${self.$scramjet.config.wrapfn}(globalThis)) { + with (${config.wrapfn}(globalThis)) { ${js} diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index c3937a4..fccc707 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -5,35 +5,22 @@ import { MessageW2C, ScramjetServiceWorker } from "."; import { renderError } from "./error"; import { FakeServiceWorker } from "./fakesw"; import { CookieStore } from "../shared/cookie"; - -const { encodeUrl, decodeUrl } = self.$scramjet.shared.url; -const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = - self.$scramjet.shared.rewrite; -const { parseDomain, ScramjetHeaders } = self.$scramjet.shared.util; +import { + ScramjetHeaders, + decodeUrl, + encodeUrl, + rewriteCss, + rewriteHeaders, + rewriteHtml, + rewriteJs, + rewriteWorkers, +} from "../shared"; export async function swfetch( 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); if (urlParam.has("url")) { diff --git a/src/worker/index.ts b/src/worker/index.ts index 2a730c4..cd2e8de 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -2,15 +2,11 @@ import IDBMap from "@webreflection/idb-map"; import { FakeServiceWorker } from "./fakesw"; import { swfetch } from "./fetch"; import { ScramjetThreadpool } from "./threadpool"; - -declare global { - interface Window { - ScramjetServiceWorker; - } -} +import type BareClient from "@mercuryworkshop/bare-mux"; +import { rewriteWorkers } from "../shared"; export class ScramjetServiceWorker { - client: typeof self.$scramjet.shared.util.BareClient.prototype; + client: BareClient; config: typeof self.$scramjet.config; threadpool: ScramjetThreadpool; @@ -112,7 +108,29 @@ export class ScramjetServiceWorker { 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;