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 { 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));
}
}

View file

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

View file

@ -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 = {

View file

@ -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", {

View file

@ -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", {

View file

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

View file

@ -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", {

View file

@ -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");
}

View file

@ -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;
}

View file

@ -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;
},
});
}

View file

@ -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*

View file

@ -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();

View file

@ -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;

View file

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

View file

@ -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", {

View file

@ -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();

View file

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

View file

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

View file

@ -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) {

View file

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

View file

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

View file

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

View file

@ -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}

View file

@ -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")) {

View file

@ -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;