From 6a6e0903ee44ff771937ec7711edad9422f50576 Mon Sep 17 00:00:00 2001 From: velzie Date: Tue, 15 Jul 2025 12:05:05 -0400 Subject: [PATCH 1/5] get basic all bundle to build --- INTERCEPTION_PROXY_BIBLE.md | 1 + eslint.config.mjs | 2 +- frontend/index.html | 2 +- rspack.config.js | 9 +++--- src/client/client.ts | 3 +- src/client/index.ts | 57 ++++++++++++++++++------------------- src/entry.ts | 27 ++++++++++++++++++ src/global.d.ts | 2 ++ src/scramjet.ts | 37 ------------------------ src/shared/index.ts | 50 +++++++++++++------------------- src/types.ts | 39 ------------------------- src/worker/fetch.ts | 2 +- static/index.html | 2 +- 13 files changed, 88 insertions(+), 145 deletions(-) create mode 100644 INTERCEPTION_PROXY_BIBLE.md create mode 100644 src/entry.ts delete mode 100644 src/scramjet.ts diff --git a/INTERCEPTION_PROXY_BIBLE.md b/INTERCEPTION_PROXY_BIBLE.md new file mode 100644 index 00000000..0220e04c --- /dev/null +++ b/INTERCEPTION_PROXY_BIBLE.md @@ -0,0 +1 @@ +# The Interception Proxy Bible diff --git a/eslint.config.mjs b/eslint.config.mjs index 5c09f89a..573e591b 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -53,7 +53,7 @@ export default [ "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/ban-ts-comment": "off", "@typescript-eslint/ban-types": "off", - + "@typescript-eslint/no-require-imports": "off", "@typescript-eslint/no-unused-vars": [ "warn", { diff --git a/frontend/index.html b/frontend/index.html index 02e5ba03..47f55283 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -6,7 +6,7 @@ Vite + TS - + diff --git a/rspack.config.js b/rspack.config.js index baa1ca59..21387d33 100644 --- a/rspack.config.js +++ b/rspack.config.js @@ -14,10 +14,11 @@ export default defineConfig({ mode: "development", devtool: "source-map", entry: { - shared: join(__dirname, "src/shared/index.ts"), - worker: join(__dirname, "src/worker/index.ts"), - client: join(__dirname, "src/client/index.ts"), - controller: join(__dirname, "src/controller/index.ts"), + all: join(__dirname, "src/entry.ts"), + // shared: join(__dirname, "src/shared/index.ts"), + // worker: join(__dirname, "src/worker/index.ts"), + // client: join(__dirname, "src/client/index.ts"), + // controller: join(__dirname, "src/controller/index.ts"), sync: join(__dirname, "src/sync.ts"), }, resolve: { diff --git a/src/client/client.ts b/src/client/client.ts index dc29ce65..0299bed3 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -365,7 +365,6 @@ export class ScramjetClient { } hook() { - // @ts-ignore const context = import.meta.webpackContext(".", { recursive: true, }); @@ -373,7 +372,7 @@ export class ScramjetClient { const modules: ScramjetModule[] = []; for (const key of context.keys()) { - const module: ScramjetModule = context(key); + const module = context(key) as ScramjetModule; if (!key.endsWith(".ts")) continue; if ( (key.startsWith("./dom/") && "window" in this.global) || diff --git a/src/client/index.ts b/src/client/index.ts index 7112753e..56b95ad9 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -21,35 +21,34 @@ function createFrameId() { .join("")}`; } -dbg.log("initializing scramjet client"); -// if it already exists, that means the handlers have probably already been setup by the parent document -if (!(SCRAMJETCLIENT in >self)) { - loadCodecs(); - - const client = new ScramjetClient(self); - const frame: HTMLIFrameElement = self.frameElement as HTMLIFrameElement; - if (frame && !frame.name) { - // all frames need to be named for our logic to work - frame.name = createFrameId(); +export function clientInitHook() { + dbg.log("initializing scramjet client"); + // if it already exists, that means the handlers have probably already been setup by the parent document + if (!(SCRAMJETCLIENT in >self)) { + loadCodecs(); + + const client = new ScramjetClient(self); + const frame: HTMLIFrameElement = self.frameElement as HTMLIFrameElement; + if (frame && !frame.name) { + // all frames need to be named for our logic to work + frame.name = createFrameId(); + } + + if (self.COOKIE) client.loadcookies(self.COOKIE); + + client.hook(); + + if (isemulatedsw) { + const runtime = new ScramjetServiceWorkerRuntime(client); + runtime.hook(); + } + + const contextev = new ScramjetContextEvent(client.global.window, client); + client.frame?.dispatchEvent(contextev); + const urlchangeev = new UrlChangeEvent(client.url.href); + if (!client.isSubframe) client.frame?.dispatchEvent(urlchangeev); } - if (self.COOKIE) client.loadcookies(self.COOKIE); - - client.hook(); - - if (isemulatedsw) { - const runtime = new ScramjetServiceWorkerRuntime(client); - runtime.hook(); - } - - const contextev = new ScramjetContextEvent(client.global.window, client); - client.frame?.dispatchEvent(contextev); - const urlchangeev = new UrlChangeEvent(client.url.href); - if (!client.isSubframe) client.frame?.dispatchEvent(urlchangeev); -} - -Reflect.deleteProperty(self, "WASM"); -Reflect.deleteProperty(self, "COOKIE"); -if ("document" in self && document?.currentScript) { - document.currentScript.remove(); + Reflect.deleteProperty(self, "WASM"); + Reflect.deleteProperty(self, "COOKIE"); } diff --git a/src/entry.ts b/src/entry.ts new file mode 100644 index 00000000..5fe1b12b --- /dev/null +++ b/src/entry.ts @@ -0,0 +1,27 @@ +/// + +export function $scramjetLoadController() { + return require("./controller/index"); +} + +export function $scramjetLoadClient() { + const client = require("./client/index"); + client.clientInitHook(); + + return client; +} + +export function $scramjetLoadWorker() { + return require("./worker/index"); +} + +export const $scramjet = { + version: { + build: COMMITHASH, + version: VERSION, + }, +}; + +if ("document" in self && document?.currentScript) { + document.currentScript.remove(); +} diff --git a/src/global.d.ts b/src/global.d.ts index 4c069f25..3aca0d51 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,3 +1,5 @@ +/// + declare const dbg: { log: (message: string, ...args: any[]) => void; warn: (message: string, ...args: any[]) => void; diff --git a/src/scramjet.ts b/src/scramjet.ts deleted file mode 100644 index 8846090a..00000000 --- a/src/scramjet.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { ScramjetFlags } from "./types"; - -if (!("$scramjet" in self)) { - // @ts-expect-error ts stuff - self.$scramjet = { - version: { - build: COMMITHASH, - version: VERSION, - }, - codec: {}, - flagEnabled, - }; -} - -export const $scramjet = self.$scramjet; - -const nativeFunction = Function; -export function loadCodecs() { - $scramjet.codec.encode = nativeFunction( - `return ${$scramjet.config.codec.encode}` - )() as any; - $scramjet.codec.decode = nativeFunction( - `return ${$scramjet.config.codec.decode}` - )() as any; -} - -export function flagEnabled(flag: keyof ScramjetFlags, url: URL): boolean { - const value = $scramjet.config.flags[flag]; - for (const regex in $scramjet.config.siteFlags) { - const partialflags = $scramjet.config.siteFlags[regex]; - if (new RegExp(regex).test(url.href) && flag in partialflags) { - return partialflags[flag]; - } - } - - return value; -} diff --git a/src/shared/index.ts b/src/shared/index.ts index a896c55d..84966944 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -14,36 +14,26 @@ import { parseDomain } from "parse-domain"; import { ScramjetHeaders } from "./headers"; import { CookieStore } from "./cookie"; import { htmlRules, unrewriteHtml } from "./rewriters/html"; -import { $scramjet } from "../scramjet"; +import { config } from "../shared"; +import { ScramjetFlags } from "../types"; -$scramjet.shared = { - util: { - parseDomain, - BareClient, - BareMuxConnection, - ScramjetHeaders, - }, - url: { - rewriteUrl, - unrewriteUrl, - rewriteBlob, - unrewriteBlob, - }, - rewrite: { - rewriteUrl, - rewriteCss, - unrewriteCss, - rewriteHtml, - unrewriteHtml, - rewriteSrcset, - rewriteJs, - rewriteHeaders, - rewriteWorkers, - htmlRules, - }, - CookieStore, -}; +export let codecEncode: (input: string) => string; +export let codecDecode: (input: string) => string; -if ("document" in self && document?.currentScript) { - document.currentScript.remove(); +const nativeFunction = Function; +export function loadCodecs() { + codecEncode = nativeFunction(`return ${config.codec.encode}`)() as any; + codecDecode = nativeFunction(`return ${config.codec.decode}`)() as any; +} + +export function flagEnabled(flag: keyof ScramjetFlags, url: URL): boolean { + const value = config.flags[flag]; + for (const regex in config.siteFlags) { + const partialflags = config.siteFlags[regex]; + if (new RegExp(regex).test(url.href) && flag in partialflags) { + return partialflags[flag]; + } + } + + return value; } diff --git a/src/types.ts b/src/types.ts index 43df32c6..ca5befca 100644 --- a/src/types.ts +++ b/src/types.ts @@ -73,45 +73,6 @@ export interface ScramjetInitConfig } declare global { interface Window { - $scramjet: { - shared: { - url: { - rewriteUrl: typeof rewriteUrl; - unrewriteUrl: typeof unrewriteUrl; - rewriteBlob: typeof rewriteBlob; - unrewriteBlob: typeof unrewriteBlob; - }; - rewrite: { - rewriteUrl: typeof rewriteUrl; - rewriteCss: typeof rewriteCss; - unrewriteCss: typeof unrewriteCss; - rewriteHtml: typeof rewriteHtml; - unrewriteHtml: typeof unrewriteHtml; - rewriteSrcset: typeof rewriteSrcset; - rewriteJs: typeof rewriteJs; - rewriteHeaders: typeof rewriteHeaders; - rewriteWorkers: typeof rewriteWorkers; - htmlRules: typeof htmlRules; - }; - util: { - BareClient: typeof BareClient; - BareMuxConnection: typeof BareMuxConnection; - ScramjetHeaders: typeof ScramjetHeaders; - parseDomain: typeof parseDomain; - }; - CookieStore: typeof CookieStore; - rewriter?: { rewriter: Rewriter; inUse: boolean }[]; - }; - config: ScramjetConfig; - codec: { - encode: (url: string) => string; - decode: (url: string) => string; - }; - version: { - version: string; - build: string; - }; - }; COOKIE: string; WASM: string; REAL_WASM: Uint8Array; diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index a038d3ec..c24400eb 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -67,7 +67,7 @@ export async function handleFetch( } const url = new URL(unrewriteUrl(requestUrl)); - let meta: URLMeta = { + const meta: URLMeta = { origin: url, base: url, }; diff --git a/static/index.html b/static/index.html index 85237bde..d600f7ed 100644 --- a/static/index.html +++ b/static/index.html @@ -53,7 +53,7 @@ - + From 117a5d40e4e24b6f7ac9ee07cee40d1664e70124 Mon Sep 17 00:00:00 2001 From: velzie Date: Tue, 15 Jul 2025 13:33:04 -0400 Subject: [PATCH 2/5] update all imports to not use $scramjet --- src/client/client.ts | 16 +-- src/client/document.ts | 2 +- src/client/dom/beacon.ts | 2 +- src/client/dom/css.ts | 2 +- src/client/dom/document.ts | 2 +- src/client/dom/element.ts | 12 +-- src/client/dom/fontface.ts | 2 +- src/client/dom/history.ts | 2 +- src/client/dom/open.ts | 2 +- src/client/dom/performance.ts | 2 +- src/client/dom/protocol.ts | 2 +- src/client/dom/serviceworker.ts | 4 +- src/client/index.ts | 2 +- src/client/location.ts | 7 +- src/client/shared/blob.ts | 2 +- src/client/shared/caches.ts | 2 +- src/client/shared/err.ts | 2 +- src/client/shared/error.ts | 6 +- src/client/shared/eval.ts | 3 +- src/client/shared/event.ts | 2 +- src/client/shared/function.ts | 2 +- src/client/shared/requests/eventsource.ts | 2 +- src/client/shared/requests/fetch.ts | 3 +- src/client/shared/requests/xmlhttprequest.ts | 4 +- src/client/shared/sourcemaps.ts | 3 +- src/client/shared/worker.ts | 4 +- src/client/swruntime.ts | 2 +- src/client/worker/importScripts.ts | 2 +- src/controller/index.ts | 30 +++--- src/log.ts | 2 +- src/shared.ts | 20 ---- src/shared/htmlRules.ts | 95 +++++++++++++++++ src/shared/index.ts | 25 ++--- src/shared/rewriters/html.ts | 104 ++----------------- src/shared/rewriters/js.ts | 4 +- src/shared/rewriters/url.ts | 22 ++-- src/shared/rewriters/wasm.ts | 34 +++--- src/shared/rewriters/worker.ts | 11 +- src/types.ts | 29 +----- src/worker/error.ts | 4 +- src/worker/fetch.ts | 32 +++--- src/worker/index.ts | 21 ++-- 42 files changed, 238 insertions(+), 293 deletions(-) delete mode 100644 src/shared.ts create mode 100644 src/shared/htmlRules.ts diff --git a/src/client/client.ts b/src/client/client.ts index 0299bed3..6170f53c 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -8,16 +8,18 @@ import { createLocationProxy } from "./location"; import { nativeGetOwnPropertyDescriptor } from "./natives"; import { BareClient, - CookieStore, - config, - unrewriteUrl, - rewriteUrl, -} from "../shared"; -import type { BareClient as BareClientType } from "@mercuryworkshop/bare-mux"; + type BareClient as BareClientType, +} from "@mercuryworkshop/bare-mux"; import { createWrapFn } from "./shared/wrap"; import { NavigateEvent } from "./events"; -import type { URLMeta } from "../shared/rewriters/url"; +import { + rewriteUrl, + unrewriteUrl, + type URLMeta, +} from "../shared/rewriters/url"; import { SourceMaps } from "./shared/sourcemaps"; +import { config } from "../shared"; +import { CookieStore } from "../shared/cookie"; type NativeStore = { store: Record; diff --git a/src/client/document.ts b/src/client/document.ts index c05f1797..35247ab0 100644 --- a/src/client/document.ts +++ b/src/client/document.ts @@ -1,4 +1,4 @@ -import { rewriteUrl } from "../shared"; +import { rewriteUrl } from "../shared/rewriters/url"; import { ScramjetClient } from "./client"; import { getOwnPropertyDescriptorHandler } from "./helpers"; diff --git a/src/client/dom/beacon.ts b/src/client/dom/beacon.ts index a82dab6e..62f52a63 100644 --- a/src/client/dom/beacon.ts +++ b/src/client/dom/beacon.ts @@ -1,4 +1,4 @@ -import { rewriteUrl } from "../../shared"; +import { rewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient, _self: Self) { diff --git a/src/client/dom/css.ts b/src/client/dom/css.ts index c4a27688..075deacb 100644 --- a/src/client/dom/css.ts +++ b/src/client/dom/css.ts @@ -1,5 +1,5 @@ +import { rewriteCss, unrewriteCss } from "../../shared/rewriters/css"; import { ScramjetClient } from "../client"; -import { rewriteCss, unrewriteCss } from "../../shared"; export default function (client: ScramjetClient) { client.Proxy("CSSStyleDeclaration.prototype.setProperty", { diff --git a/src/client/dom/document.ts b/src/client/dom/document.ts index d5e52a22..38327b56 100644 --- a/src/client/dom/document.ts +++ b/src/client/dom/document.ts @@ -1,4 +1,4 @@ -import { rewriteHtml } from "../../shared"; +import { rewriteHtml } from "../../shared/rewriters/html"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient, _self: Self) { diff --git a/src/client/dom/element.ts b/src/client/dom/element.ts index 7d87b73c..4a849c02 100644 --- a/src/client/dom/element.ts +++ b/src/client/dom/element.ts @@ -1,13 +1,11 @@ +import { htmlRules } from "../../shared/htmlRules"; +import { rewriteCss, unrewriteCss } from "../../shared/rewriters/css"; +import { rewriteHtml, unrewriteHtml } from "../../shared/rewriters/html"; +import { rewriteJs } from "../../shared/rewriters/js"; +import { rewriteUrl, unrewriteUrl } from "../../shared/rewriters/url"; import { SCRAMJETCLIENT } from "../../symbols"; import { ScramjetClient } from "../client"; import { nativeGetOwnPropertyDescriptor } from "../natives"; -import { - unrewriteUrl, - rewriteUrl, - htmlRules, - unrewriteHtml, -} from "../../shared"; -import { unrewriteCss, rewriteCss, rewriteHtml, rewriteJs } from "../../shared"; const encoder = new TextEncoder(); function bytesToBase64(bytes: Uint8Array) { diff --git a/src/client/dom/fontface.ts b/src/client/dom/fontface.ts index faef8c77..d72fbf82 100644 --- a/src/client/dom/fontface.ts +++ b/src/client/dom/fontface.ts @@ -1,5 +1,5 @@ +import { rewriteCss } from "../../shared/rewriters/css"; import { ScramjetClient } from "../client"; -import { rewriteCss } from "../../shared"; export default function (client: ScramjetClient, _self: Self) { client.Proxy("FontFace", { diff --git a/src/client/dom/history.ts b/src/client/dom/history.ts index 129b4cc0..e7e0f053 100644 --- a/src/client/dom/history.ts +++ b/src/client/dom/history.ts @@ -1,5 +1,5 @@ +import { rewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; -import { rewriteUrl } from "../../shared"; import { UrlChangeEvent } from "../events"; export default function (client: ScramjetClient, _self: Self) { diff --git a/src/client/dom/open.ts b/src/client/dom/open.ts index 9a8e1252..f15040d7 100644 --- a/src/client/dom/open.ts +++ b/src/client/dom/open.ts @@ -1,6 +1,6 @@ -import { rewriteUrl } from "../../shared"; import { ScramjetClient } from "../client"; import { SCRAMJETCLIENT } from "../../symbols"; +import { rewriteUrl } from "../../shared/rewriters/url"; export default function (client: ScramjetClient) { client.Proxy("window.open", { diff --git a/src/client/dom/performance.ts b/src/client/dom/performance.ts index ebfec3df..3a8655eb 100644 --- a/src/client/dom/performance.ts +++ b/src/client/dom/performance.ts @@ -1,4 +1,4 @@ -import { unrewriteUrl } from "../../shared"; +import { unrewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient, _self: Self) { diff --git a/src/client/dom/protocol.ts b/src/client/dom/protocol.ts index a556264a..2debe462 100644 --- a/src/client/dom/protocol.ts +++ b/src/client/dom/protocol.ts @@ -1,4 +1,4 @@ -import { rewriteUrl } from "../../shared"; +import { rewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient, self: Self) { diff --git a/src/client/dom/serviceworker.ts b/src/client/dom/serviceworker.ts index d85bdc1d..82bfbe76 100644 --- a/src/client/dom/serviceworker.ts +++ b/src/client/dom/serviceworker.ts @@ -1,7 +1,7 @@ -import { rewriteUrl } from "../../shared"; import { ScramjetClient } from "../client"; import { type MessageC2W } from "../../worker"; -import { flagEnabled } from "../../scramjet"; +import { flagEnabled } from "../../shared"; +import { rewriteUrl } from "../../shared/rewriters/url"; // we need a late order because we're mangling with addEventListener at a higher level export const order = 2; diff --git a/src/client/index.ts b/src/client/index.ts index 56b95ad9..c69ee81b 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -1,6 +1,6 @@ // entrypoint for scramjet.client.js -import { loadCodecs } from "../scramjet"; +import { loadCodecs } from "../shared/index"; import { SCRAMJETCLIENT } from "../symbols"; import { ScramjetClient } from "./client"; import { ScramjetContextEvent, UrlChangeEvent } from "./events"; diff --git a/src/client/location.ts b/src/client/location.ts index 065b2a8d..3fa2e6fd 100644 --- a/src/client/location.ts +++ b/src/client/location.ts @@ -1,9 +1,8 @@ -// @ts-nocheck import { ScramjetClient } from "./client"; import { nativeGetOwnPropertyDescriptor } from "./natives"; -import { rewriteUrl } from "../shared"; import { UrlChangeEvent } from "./events"; import { iswindow } from "."; +import { rewriteUrl } from "../shared/rewriters/url"; export function createLocationProxy( client: ScramjetClient, @@ -11,7 +10,7 @@ export function createLocationProxy( ) { const Location = iswindow ? self.Location : self.WorkerLocation; // location cannot be Proxy()d - const fakeLocation = {}; + const fakeLocation: any = {}; Object.setPrototypeOf(fakeLocation, Location.prototype); fakeLocation.constructor = Location; @@ -32,7 +31,7 @@ export function createLocationProxy( const native = nativeGetOwnPropertyDescriptor(descriptorSource, prop); if (!native) continue; - const desc = { + const desc: Partial = { configurable: true, enumerable: true, }; diff --git a/src/client/shared/blob.ts b/src/client/shared/blob.ts index 16215ea2..b362ee60 100644 --- a/src/client/shared/blob.ts +++ b/src/client/shared/blob.ts @@ -1,4 +1,4 @@ -import { rewriteBlob, unrewriteBlob } from "../../shared"; +import { rewriteBlob, unrewriteBlob } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient) { // hide the origin from object urls from the page diff --git a/src/client/shared/caches.ts b/src/client/shared/caches.ts index 5c2f3b84..1ff9e298 100644 --- a/src/client/shared/caches.ts +++ b/src/client/shared/caches.ts @@ -1,4 +1,4 @@ -import { rewriteUrl } from "../../shared"; +import { rewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient, _self: Self) { diff --git a/src/client/shared/err.ts b/src/client/shared/err.ts index 03b4a78d..2f71b7e9 100644 --- a/src/client/shared/err.ts +++ b/src/client/shared/err.ts @@ -1,4 +1,4 @@ -import { flagEnabled } from "../../scramjet"; +import { flagEnabled } from "../../shared"; import { ScramjetClient } from "../client"; export const enabled = (client: ScramjetClient) => diff --git a/src/client/shared/error.ts b/src/client/shared/error.ts index c228a304..5fdfd9d7 100644 --- a/src/client/shared/error.ts +++ b/src/client/shared/error.ts @@ -1,5 +1,5 @@ -import { flagEnabled } from "../../scramjet"; -import { config, unrewriteUrl } from "../../shared"; +import { config, flagEnabled } from "../../shared"; +import { unrewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export const enabled = (client: ScramjetClient) => @@ -13,7 +13,7 @@ export default function (client: ScramjetClient, _self: Self) { for (let i = 0; i < stack.length; i++) { const url = stack[i].getFileName(); - if (url.endsWith(config.files.client)) { + if (url.endsWith(config.files.all)) { // strip stack frames including scramjet handlers from the trace const lines = newstack.split("\n"); const line = lines.find((l) => l.includes(url)); diff --git a/src/client/shared/eval.ts b/src/client/shared/eval.ts index 83f2639c..aae63eea 100644 --- a/src/client/shared/eval.ts +++ b/src/client/shared/eval.ts @@ -1,5 +1,6 @@ +import { config } from "../../shared"; +import { rewriteJs } from "../../shared/rewriters/js"; import { ScramjetClient } from "../client"; -import { config, rewriteJs } from "../../shared"; export default function (client: ScramjetClient, self: Self) { // used for proxying *direct eval* diff --git a/src/client/shared/event.ts b/src/client/shared/event.ts index 07b3799a..c96fe665 100644 --- a/src/client/shared/event.ts +++ b/src/client/shared/event.ts @@ -1,5 +1,5 @@ import { iswindow } from ".."; -import { unrewriteUrl } from "../../shared"; +import { unrewriteUrl } from "../../shared/rewriters/url"; import { SCRAMJETCLIENT } from "../../symbols"; import { ScramjetClient } from "../client"; import { getOwnPropertyDescriptorHandler } from "../helpers"; diff --git a/src/client/shared/function.ts b/src/client/shared/function.ts index c8954099..61e5ffdb 100644 --- a/src/client/shared/function.ts +++ b/src/client/shared/function.ts @@ -1,5 +1,5 @@ +import { rewriteJs } from "../../shared/rewriters/js"; import { ScramjetClient, ProxyCtx, Proxy } from "../client"; -import { rewriteJs } from "../../shared"; function rewriteFunction(ctx: ProxyCtx, client: ScramjetClient) { const stringifiedFunction = ctx.call().toString(); diff --git a/src/client/shared/requests/eventsource.ts b/src/client/shared/requests/eventsource.ts index a8399cc1..3579eadb 100644 --- a/src/client/shared/requests/eventsource.ts +++ b/src/client/shared/requests/eventsource.ts @@ -1,4 +1,4 @@ -import { unrewriteUrl, rewriteUrl } from "../../../shared"; +import { rewriteUrl, unrewriteUrl } from "../../../shared/rewriters/url"; import { ScramjetClient } from "../../client"; export default function (client: ScramjetClient) { diff --git a/src/client/shared/requests/fetch.ts b/src/client/shared/requests/fetch.ts index 23e01a1b..1b8cc5dc 100644 --- a/src/client/shared/requests/fetch.ts +++ b/src/client/shared/requests/fetch.ts @@ -1,9 +1,8 @@ // ts throws an error if you dont do window.fetch import { isemulatedsw } from "../.."; -import { unrewriteUrl } from "../../../shared"; +import { rewriteUrl, unrewriteUrl } from "../../../shared/rewriters/url"; import { ScramjetClient } from "../../client"; -import { rewriteUrl } from "../../../shared"; export default function (client: ScramjetClient) { client.Proxy("fetch", { diff --git a/src/client/shared/requests/xmlhttprequest.ts b/src/client/shared/requests/xmlhttprequest.ts index 6625b171..430bb86e 100644 --- a/src/client/shared/requests/xmlhttprequest.ts +++ b/src/client/shared/requests/xmlhttprequest.ts @@ -1,5 +1,5 @@ -import { flagEnabled } from "../../../scramjet"; -import { config, unrewriteUrl, rewriteUrl } from "../../../shared"; +import { config, flagEnabled } from "../../../shared"; +import { rewriteUrl, unrewriteUrl } from "../../../shared/rewriters/url"; import { ScramjetClient } from "../../client"; export default function (client: ScramjetClient, self: Self) { diff --git a/src/client/shared/sourcemaps.ts b/src/client/shared/sourcemaps.ts index 63a8efdb..e8d32e3e 100644 --- a/src/client/shared/sourcemaps.ts +++ b/src/client/shared/sourcemaps.ts @@ -1,4 +1,4 @@ -import { flagEnabled } from "../../scramjet"; +import { flagEnabled } from "../../shared"; import { SCRAMJETCLIENT, SCRAMJETCLIENTNAME } from "../../symbols"; import { ProxyCtx, ScramjetClient } from "../client"; @@ -141,6 +141,7 @@ function doUnrewrite(ctx: ProxyCtx) { if (!rewrites) { console.warn("failed to get rewrites for tag", tag); + return ctx.return(stringified); } diff --git a/src/client/shared/worker.ts b/src/client/shared/worker.ts index 58ea4daf..78bc951a 100644 --- a/src/client/shared/worker.ts +++ b/src/client/shared/worker.ts @@ -1,5 +1,5 @@ -import { BareMuxConnection } from "../../shared"; -import { rewriteUrl } from "../../shared"; +import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; +import { rewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient, _self: typeof globalThis) { diff --git a/src/client/swruntime.ts b/src/client/swruntime.ts index aa581852..622dcc65 100644 --- a/src/client/swruntime.ts +++ b/src/client/swruntime.ts @@ -1,5 +1,5 @@ +import { unrewriteUrl } from "../shared/rewriters/url"; import { ScramjetClient } from "./client"; -import { unrewriteUrl } from "../shared"; export class ScramjetServiceWorkerRuntime { recvport: MessagePort; diff --git a/src/client/worker/importScripts.ts b/src/client/worker/importScripts.ts index 9e5daf0e..551aba0b 100644 --- a/src/client/worker/importScripts.ts +++ b/src/client/worker/importScripts.ts @@ -1,4 +1,4 @@ -import { rewriteUrl } from "../../shared"; +import { rewriteUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; export default function (client: ScramjetClient) { diff --git a/src/controller/index.ts b/src/controller/index.ts index 0a802842..2c30d784 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -1,6 +1,12 @@ +import { + codecDecode, + codecEncode, + config, + loadCodecs, + setConfig, +} from "../shared/index"; import { ScramjetConfig, ScramjetInitConfig } from "../types"; import { ScramjetFrame } from "./frame"; -import { $scramjet, loadCodecs } from "../scramjet"; export class ScramjetController { private db: IDBDatabase; @@ -20,10 +26,8 @@ export class ScramjetController { pushsourcemapfn: "$scramjet$pushsourcemap", }, files: { - wasm: "/scramjet.wasm.js", - shared: "/scramjet.shared.js", - worker: "/scramjet.worker.js", - client: "/scramjet.client.js", + wasm: "/scramjet.wasm.wasm", + all: "/scramjet.all.js", sync: "/scramjet.sync.js", }, flags: { @@ -65,7 +69,7 @@ export class ScramjetController { const newConfig = deepMerge(defaultConfig, config); newConfig.codec.encode = newConfig.codec.encode.toString(); newConfig.codec.decode = newConfig.codec.decode.toString(); - $scramjet.config = newConfig as ScramjetConfig; + setConfig(newConfig as ScramjetConfig); } async init(): Promise { @@ -74,7 +78,7 @@ export class ScramjetController { await this.openIDB(); navigator.serviceWorker.controller?.postMessage({ scramjet$type: "loadConfig", - config: $scramjet.config, + config, }); dbg.log("config loaded"); } @@ -90,14 +94,14 @@ export class ScramjetController { encodeUrl(url: string | URL): string { if (url instanceof URL) url = url.toString(); - return $scramjet.config.prefix + $scramjet.codec.encode(url); + return config.prefix + codecEncode(url); } decodeUrl(url: string | URL) { if (url instanceof URL) url = url.toString(); - const prefixed = location.origin + $scramjet.config.prefix; + const prefixed = location.origin + config.prefix; - return $scramjet.codec.decode(url.slice(prefixed.length)); + return codecDecode(url.slice(prefixed.length)); } async openIDB(): Promise { @@ -139,7 +143,7 @@ export class ScramjetController { } const tx = this.db.transaction("config", "readwrite"); const store = tx.objectStore("config"); - const req = store.put($scramjet.config, "config"); + const req = store.put(config, "config"); return new Promise((resolve, reject) => { req.onsuccess = resolve; @@ -147,8 +151,8 @@ export class ScramjetController { }); } - async modifyConfig(config: ScramjetConfig) { - $scramjet.config = Object.assign({}, $scramjet.config, config); + async modifyConfig(newconfig: ScramjetConfig) { + setConfig(Object.assign({}, config, newconfig)); loadCodecs(); await this.#saveConfig(); diff --git a/src/log.ts b/src/log.ts index 3762ae88..83cb40ad 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,4 +1,4 @@ -import { flagEnabled } from "./scramjet"; +import { flagEnabled } from "./shared"; import type { URLMeta } from "./shared/rewriters/url"; const logfuncs = { diff --git a/src/shared.ts b/src/shared.ts deleted file mode 100644 index fd25f69f..00000000 --- a/src/shared.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { $scramjet } from "./scramjet"; - -export const { - util: { BareClient, ScramjetHeaders, BareMuxConnection }, - url: { rewriteUrl, unrewriteUrl, rewriteBlob, unrewriteBlob }, - rewrite: { - rewriteCss, - unrewriteCss, - rewriteHtml, - unrewriteHtml, - rewriteSrcset, - rewriteJs, - rewriteHeaders, - rewriteWorkers, - htmlRules, - }, - CookieStore, -} = $scramjet.shared; - -export const config = $scramjet.config; diff --git a/src/shared/htmlRules.ts b/src/shared/htmlRules.ts new file mode 100644 index 00000000..392c29c2 --- /dev/null +++ b/src/shared/htmlRules.ts @@ -0,0 +1,95 @@ +import { CookieStore } from "./cookie"; +import { rewriteCss } from "./rewriters/css"; +import { rewriteHtml, rewriteSrcset } from "./rewriters/html"; +import { rewriteUrl, unrewriteBlob, URLMeta } from "./rewriters/url"; + +export const htmlRules: { + [key: string]: "*" | string[] | ((...any: any[]) => string | null); + fn: (value: string, meta: URLMeta, cookieStore: CookieStore) => string | null; +}[] = [ + { + fn: (value: string, meta: URLMeta) => { + return rewriteUrl(value, meta); + }, + + // url rewrites + src: ["embed", "script", "img", "frame", "source", "input", "track"], + href: ["a", "link", "area", "use", "image"], + data: ["object"], + action: ["form"], + formaction: ["button", "input", "textarea", "submit"], + poster: ["video"], + "xlink:href": ["image"], + }, + { + fn: (value: string, meta: URLMeta) => { + let url = rewriteUrl(value, meta); + if (meta.topFrameName) + url += `?topFrame=${meta.topFrameName}&parentFrame=${meta.parentFrameName}`; + + return url; + }, + src: ["iframe"], + }, + { + fn: (value: string, meta: URLMeta) => { + if (value.startsWith("blob:")) { + // for media elements specifically they must take the original blob + // because they can't be fetch'd + return unrewriteBlob(value); + } + + return rewriteUrl(value, meta); + }, + src: ["video", "audio"], + }, + { + fn: () => "", + + integrity: ["script", "link"], + }, + { + fn: () => null, + + // csp stuff that must be deleted + nonce: "*", + csp: ["iframe"], + credentialless: ["iframe"], + }, + { + fn: (value: string, meta: URLMeta) => rewriteSrcset(value, meta), + + // srcset + srcset: ["img", "source"], + imagesrcset: ["link"], + }, + { + fn: (value: string, meta: URLMeta, cookieStore: CookieStore) => + rewriteHtml( + value, + cookieStore, + { + // for srcdoc origin is the origin of the page that the iframe is on. base and path get dropped + origin: new URL(meta.origin.origin), + base: new URL(meta.origin.origin), + }, + true + ), + + // srcdoc + srcdoc: ["iframe"], + }, + { + fn: (value: string, meta: URLMeta) => rewriteCss(value, meta), + style: "*", + }, + { + fn: (value: string, meta: URLMeta) => { + if (value === "_top" || value === "_unfencedTop") + return meta.topFrameName; + else if (value === "_parent") return meta.parentFrameName; + else return value; + }, + target: ["a", "base"], + }, +]; diff --git a/src/shared/index.ts b/src/shared/index.ts index 84966944..acdb785d 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -1,21 +1,4 @@ -import { - rewriteUrl, - unrewriteUrl, - rewriteBlob, - unrewriteBlob, -} from "./rewriters/url"; -import { rewriteCss, unrewriteCss } from "./rewriters/css"; -import { rewriteHtml, rewriteSrcset } from "./rewriters/html"; -import { rewriteJs } from "./rewriters/js"; -import { rewriteHeaders } from "./rewriters/headers"; -import { rewriteWorkers } from "./rewriters/worker"; -import { BareClient, BareMuxConnection } from "@mercuryworkshop/bare-mux"; -import { parseDomain } from "parse-domain"; -import { ScramjetHeaders } from "./headers"; -import { CookieStore } from "./cookie"; -import { htmlRules, unrewriteHtml } from "./rewriters/html"; -import { config } from "../shared"; -import { ScramjetFlags } from "../types"; +import { ScramjetConfig, ScramjetFlags } from "../types"; export let codecEncode: (input: string) => string; export let codecDecode: (input: string) => string; @@ -37,3 +20,9 @@ export function flagEnabled(flag: keyof ScramjetFlags, url: URL): boolean { return value; } + +export let config: ScramjetConfig; +export function setConfig(newConfig: ScramjetConfig) { + config = newConfig; + loadCodecs(); +} diff --git a/src/shared/rewriters/html.ts b/src/shared/rewriters/html.ts index 01cbb9c3..53e8e0a5 100644 --- a/src/shared/rewriters/html.ts +++ b/src/shared/rewriters/html.ts @@ -5,9 +5,9 @@ import { URLMeta, rewriteUrl } from "./url"; import { rewriteCss } from "./css"; import { rewriteJs } from "./js"; import { CookieStore } from "../cookie"; -import { unrewriteBlob } from "../../shared/rewriters/url"; -import { $scramjet } from "../../scramjet"; import { getRewriter } from "./wasm"; +import { config } from ".."; +import { htmlRules } from "../htmlRules"; const encoder = new TextEncoder(); function rewriteHtmlInner( @@ -46,7 +46,7 @@ function rewriteHtmlInner( const dump = JSON.stringify(cookieStore.dump()); const injected = ` self.COOKIE = ${dump}; - self.$scramjet.config = ${JSON.stringify($scramjet.config)}; + $scramjetLoadClient(${JSON.stringify(config)}); if ("document" in self && document?.currentScript) { document.currentScript.remove(); } @@ -58,10 +58,9 @@ function rewriteHtmlInner( const base64Injected = bytesToBase64(encoder.encode(injected)); head.children.unshift( - script($scramjet.config.files.wasm), - script($scramjet.config.files.shared), - script("data:application/javascript;base64," + base64Injected), - script($scramjet.config.files.client) + script(config.files.wasm), + script(config.files.all), + script("data:application/javascript;base64," + base64Injected) ); } @@ -150,97 +149,6 @@ export function unrewriteHtml(html: string) { }); } -export const htmlRules: { - [key: string]: "*" | string[] | ((...any: any[]) => string | null); - fn: (value: string, meta: URLMeta, cookieStore: CookieStore) => string | null; -}[] = [ - { - fn: (value: string, meta: URLMeta) => { - return rewriteUrl(value, meta); - }, - - // url rewrites - src: ["embed", "script", "img", "frame", "source", "input", "track"], - href: ["a", "link", "area", "use", "image"], - data: ["object"], - action: ["form"], - formaction: ["button", "input", "textarea", "submit"], - poster: ["video"], - "xlink:href": ["image"], - }, - { - fn: (value: string, meta: URLMeta) => { - let url = rewriteUrl(value, meta); - if (meta.topFrameName) - url += `?topFrame=${meta.topFrameName}&parentFrame=${meta.parentFrameName}`; - - return url; - }, - src: ["iframe"], - }, - { - fn: (value: string, meta: URLMeta) => { - if (value.startsWith("blob:")) { - // for media elements specifically they must take the original blob - // because they can't be fetch'd - return unrewriteBlob(value); - } - - return rewriteUrl(value, meta); - }, - src: ["video", "audio"], - }, - { - fn: () => "", - - integrity: ["script", "link"], - }, - { - fn: () => null, - - // csp stuff that must be deleted - nonce: "*", - csp: ["iframe"], - credentialless: ["iframe"], - }, - { - fn: (value: string, meta: URLMeta) => rewriteSrcset(value, meta), - - // srcset - srcset: ["img", "source"], - imagesrcset: ["link"], - }, - { - fn: (value: string, meta: URLMeta, cookieStore: CookieStore) => - rewriteHtml( - value, - cookieStore, - { - // for srcdoc origin is the origin of the page that the iframe is on. base and path get dropped - origin: new URL(meta.origin.origin), - base: new URL(meta.origin.origin), - }, - true - ), - - // srcdoc - srcdoc: ["iframe"], - }, - { - fn: (value: string, meta: URLMeta) => rewriteCss(value, meta), - style: "*", - }, - { - fn: (value: string, meta: URLMeta) => { - if (value === "_top" || value === "_unfencedTop") - return meta.topFrameName; - else if (value === "_parent") return meta.parentFrameName; - else return value; - }, - target: ["a", "base"], - }, -]; - // i need to add the attributes in during rewriting function traverseParsedHtml( diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index f907ea10..2bda17b6 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -1,6 +1,6 @@ +import { config, flagEnabled } from ".."; import { URLMeta } from "./url"; -import { $scramjet, flagEnabled } from "../../scramjet"; import { getRewriter, JsRewriterOutput } from "./wasm"; Error.stackTraceLimit = 50; @@ -81,7 +81,7 @@ function rewriteJsNaiive(js: string | ArrayBuffer) { } return ` - with (${$scramjet.config.globals.wrapfn}(globalThis)) { + with (${config.globals.wrapfn}(globalThis)) { ${js} diff --git a/src/shared/rewriters/url.ts b/src/shared/rewriters/url.ts index 892712b7..028a57e2 100644 --- a/src/shared/rewriters/url.ts +++ b/src/shared/rewriters/url.ts @@ -1,4 +1,5 @@ -import { $scramjet } from "../../scramjet"; +import { codecDecode, codecEncode } from ".."; +import { config } from "../../shared"; import { rewriteJs } from "./js"; export type URLMeta = { @@ -37,9 +38,9 @@ export function rewriteUrl(url: string | URL, meta: URLMeta) { rewriteJs(url.slice("javascript:".length), "(javascript: url)", meta) ); } else if (url.startsWith("blob:")) { - return location.origin + $scramjet.config.prefix + url; + return location.origin + config.prefix + url; } else if (url.startsWith("data:")) { - return location.origin + $scramjet.config.prefix + url; + return location.origin + config.prefix + url; } else if (url.startsWith("mailto:") || url.startsWith("about:")) { return url; } else { @@ -48,15 +49,12 @@ export function rewriteUrl(url: string | URL, meta: URLMeta) { if (base.startsWith("about:")) base = unrewriteUrl(self.location.href); // jank!!!!! weird jank!!! const realUrl = tryCanParseURL(url, base); if (!realUrl) return url; - const encodedHash = $scramjet.codec.encode(realUrl.hash.slice(1)); + const encodedHash = codecEncode(realUrl.hash.slice(1)); const realHash = encodedHash ? "#" + encodedHash : ""; realUrl.hash = ""; return ( - location.origin + - $scramjet.config.prefix + - $scramjet.codec.encode(realUrl.href) + - realHash + location.origin + config.prefix + codecEncode(realUrl.href) + realHash ); } } @@ -68,7 +66,7 @@ export function unrewriteUrl(url: string | URL) { url = url.split("?")[0]; } - const prefixed = location.origin + $scramjet.config.prefix; + const prefixed = location.origin + config.prefix; if (url.startsWith("javascript:")) { //TODO @@ -85,12 +83,10 @@ export function unrewriteUrl(url: string | URL) { } else { const realUrl = tryCanParseURL(url); if (!realUrl) return url; - const decodedHash = $scramjet.codec.decode(realUrl.hash.slice(1)); + const decodedHash = codecDecode(realUrl.hash.slice(1)); const realHash = decodedHash ? "#" + decodedHash : ""; realUrl.hash = ""; - return $scramjet.codec.decode( - realUrl.href.slice(prefixed.length) + realHash - ); + return codecDecode(realUrl.href.slice(prefixed.length) + realHash); } } diff --git a/src/shared/rewriters/wasm.ts b/src/shared/rewriters/wasm.ts index e154406e..3931db5a 100644 --- a/src/shared/rewriters/wasm.ts +++ b/src/shared/rewriters/wasm.ts @@ -1,64 +1,64 @@ // i am a cat. i like to be petted. i like to be fed. i like to be import { initSync, Rewriter } from "../../../rewriter/wasm/out/wasm.js"; import type { JsRewriterOutput } from "../../../rewriter/wasm/out/wasm.js"; +import { config, flagEnabled } from ".."; export type { JsRewriterOutput, Rewriter }; -import { $scramjet, flagEnabled } from "../../scramjet"; -import { URLMeta } from "./url.js"; +import { URLMeta } from "./url"; +let wasm_u8: Uint8Array; if (self.WASM) - self.REAL_WASM = Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)); + wasm_u8 = Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)); // only use in sw export async function asyncSetWasm() { - const buf = await fetch($scramjet.config.files.wasm).then((r) => - r.arrayBuffer() - ); - self.REAL_WASM = new Uint8Array(buf); + const buf = await fetch(config.files.wasm).then((r) => r.arrayBuffer()); + wasm_u8 = new Uint8Array(buf); } const decoder = new TextDecoder(); let MAGIC = "\0asm".split("").map((x) => x.charCodeAt(0)); function initWasm() { - if (!(self.REAL_WASM && self.REAL_WASM instanceof Uint8Array)) + if (!(wasm_u8 instanceof Uint8Array)) throw new Error("rewriter wasm not found (was it fetched correctly?)"); - if (![...self.REAL_WASM.slice(0, 4)].every((x, i) => x === MAGIC[i])) + if (![...wasm_u8.slice(0, 4)].every((x, i) => x === MAGIC[i])) throw new Error( "rewriter wasm does not have wasm magic (was it fetched correctly?)\nrewriter wasm contents: " + - decoder.decode(self.REAL_WASM) + decoder.decode(wasm_u8) ); initSync({ - module: new WebAssembly.Module(self.REAL_WASM), + module: new WebAssembly.Module(wasm_u8), }); } +let rewriters = []; export function getRewriter(meta: URLMeta): [Rewriter, () => void] { initWasm(); - if (!$scramjet.shared.rewriter) $scramjet.shared.rewriter = []; let obj: { rewriter: Rewriter; inUse: boolean }; - let index = $scramjet.shared.rewriter.findIndex((x) => !x.inUse); - let len = $scramjet.shared.rewriter.length; + let index = rewriters.findIndex((x) => !x.inUse); + let len = rewriters.length; if (index === -1) { if (flagEnabled("rewriterLogs", meta.base)) console.log(`creating new rewriter, ${len} rewriters made already`); - let rewriter = new Rewriter($scramjet); + let rewriter = new Rewriter({}); obj = { rewriter, inUse: false }; - $scramjet.shared.rewriter.push(obj); + rewriters.push(obj); } else { if (flagEnabled("rewriterLogs", meta.base)) console.log( `using cached rewriter ${index} from list of ${len} rewriters` ); - obj = $scramjet.shared.rewriter[index]; + obj = rewriters[index]; } obj.inUse = true; + return [obj.rewriter, () => (obj.inUse = false)]; } diff --git a/src/shared/rewriters/worker.ts b/src/shared/rewriters/worker.ts index 7d29473a..31f01053 100644 --- a/src/shared/rewriters/worker.ts +++ b/src/shared/rewriters/worker.ts @@ -1,4 +1,4 @@ -import { $scramjet } from "../../scramjet"; +import { config } from ".."; import { rewriteJs } from "./js"; import { URLMeta } from "./url"; @@ -12,16 +12,15 @@ export function rewriteWorkers( const module = type === "module"; const script = (script) => { if (module) { - str += `import "${$scramjet.config.files[script]}"\n`; + str += `import "${config.files[script]}"\n`; } else { - str += `importScripts("${$scramjet.config.files[script]}");\n`; + str += `importScripts("${config.files[script]}");\n`; } }; script("wasm"); - script("shared"); - str += `self.$scramjet.config = ${JSON.stringify($scramjet.config)};`; - script("client"); + script("all"); + str += `$scramjetLoadClient(${JSON.stringify(config)})`; let rewritten = rewriteJs(js, url, meta, module); if (rewritten instanceof Uint8Array) { diff --git a/src/types.ts b/src/types.ts index ca5befca..79ae1def 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,28 +1,7 @@ -import { ScramjetController } from "./controller/index"; -import { - rewriteBlob, - rewriteUrl, - unrewriteBlob, - unrewriteUrl, -} from "./shared/rewriters/url"; -import { rewriteCss, unrewriteCss } from "./shared/rewriters/css"; -import { - htmlRules, - rewriteHtml, - rewriteSrcset, - unrewriteHtml, -} from "./shared/rewriters/html"; -import { rewriteJs } from "./shared/rewriters/js"; -import { rewriteHeaders } from "./shared/rewriters/headers"; -import { rewriteWorkers } from "./shared/rewriters/worker"; -import { BareClient, BareMuxConnection } from "@mercuryworkshop/bare-mux"; -import { parseDomain } from "parse-domain"; -import { ScramjetHeaders } from "./shared/headers"; -import { CookieStore } from "./shared/cookie"; -import { SCRAMJETCLIENT, SCRAMJETFRAME } from "./symbols"; import { ScramjetClient } from "./client/client"; +import { ScramjetController } from "./controller"; import { ScramjetFrame } from "./controller/frame"; -import { Rewriter } from "./shared/rewriters/wasm"; +import { SCRAMJETCLIENT, SCRAMJETFRAME } from "./symbols"; export type ScramjetFlags = { serviceworkers: boolean; @@ -50,9 +29,7 @@ export interface ScramjetConfig { }; files: { wasm: string; - shared: string; - worker: string; - client: string; + all: string; sync: string; }; flags: ScramjetFlags; diff --git a/src/worker/error.ts b/src/worker/error.ts index 927d4089..8a0a1637 100644 --- a/src/worker/error.ts +++ b/src/worker/error.ts @@ -1,4 +1,4 @@ -import { $scramjet } from "../scramjet"; +import { $scramjet } from "../entry"; export function errorTemplate(trace: string, fetchedURL: string) { // turn script into a data URI so we don"t have to escape any HTML values @@ -9,7 +9,7 @@ export function errorTemplate(trace: string, fetchedURL: string) { reload.addEventListener("click", () => location.reload()); version.textContent = ${JSON.stringify($scramjet.version.version)}; build.textContent = ${JSON.stringify($scramjet.version.build)}; - + document.getElementById('copy-button').addEventListener('click', async () => { const text = document.getElementById('errorTrace').value; await navigator.clipboard.writeText(text); diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index c24400eb..faedb4c9 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -3,15 +3,7 @@ import { ScramjetServiceWorker } from "."; import { renderError } from "./error"; import { FakeServiceWorker } from "./fakesw"; import { CookieStore } from "../shared/cookie"; -import { - ScramjetHeaders, - unrewriteUrl, - rewriteCss, - rewriteHeaders, - rewriteHtml, - rewriteWorkers, - unrewriteBlob, -} from "../shared"; + import { getSiteDirective } from "../shared/security/siteTests"; import { initializeTracker, @@ -22,9 +14,18 @@ import { getReferrerPolicy, } from "../shared/security/forceReferrer"; -import type { URLMeta } from "../shared/rewriters/url"; -import { $scramjet, flagEnabled } from "../scramjet"; +import { + unrewriteBlob, + unrewriteUrl, + type URLMeta, +} from "../shared/rewriters/url"; import { rewriteJsWithMap } from "../shared/rewriters/js"; +import { ScramjetHeaders } from "../shared/headers"; +import { config, flagEnabled } from "../shared"; +import { rewriteHeaders } from "../shared/rewriters/headers"; +import { rewriteHtml } from "../shared/rewriters/html"; +import { rewriteCss } from "../shared/rewriters/css"; +import { rewriteWorkers } from "../shared/rewriters/worker"; export async function handleFetch( this: ScramjetServiceWorker, @@ -138,10 +139,7 @@ export async function handleFetch( headers.set(key, value); } - if ( - client && - new URL(client.url).pathname.startsWith($scramjet.config.prefix) - ) { + if (client && new URL(client.url).pathname.startsWith(config.prefix)) { // TODO: i was against cors emulation but we might actually break stuff if we send full origin/referrer always const clientURL = new URL(unrewriteUrl(client.url)); if (clientURL.toString().includes("youtube.com")) { @@ -173,7 +171,7 @@ export async function handleFetch( // Trace backwards while (currentReferrer) { - if (!currentReferrer.includes($scramjet.config.prefix)) { + if (!currentReferrer.includes(config.prefix)) { isTopLevelProxyNavigation = true; break; } @@ -225,7 +223,7 @@ export async function handleFetch( request.referrer !== "" && request.referrer !== "no-referrer" ) { - if (request.referrer.includes($scramjet.config.prefix)) { + if (request.referrer.includes(config.prefix)) { const unrewrittenReferrer = unrewriteUrl(request.referrer); if (unrewrittenReferrer) { const referrerUrl = new URL(unrewrittenReferrer); diff --git a/src/worker/index.ts b/src/worker/index.ts index 35c95d91..82be5a74 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -1,9 +1,10 @@ import { FakeServiceWorker } from "./fakesw"; import { handleFetch } from "./fetch"; -import type BareClient from "@mercuryworkshop/bare-mux"; +import BareClient from "@mercuryworkshop/bare-mux"; import { ScramjetConfig } from "../types"; -import { $scramjet, loadCodecs } from "../scramjet"; import { asyncSetWasm } from "../shared/rewriters/wasm"; +import { CookieStore } from "../shared/cookie"; +import { config, loadCodecs, setConfig } from "../shared"; export class ScramjetServiceWorker extends EventTarget { client: BareClient; @@ -12,13 +13,13 @@ export class ScramjetServiceWorker extends EventTarget { syncPool: Record void> = {}; synctoken = 0; - cookieStore = new $scramjet.shared.CookieStore(); + cookieStore = new CookieStore(); serviceWorkers: FakeServiceWorker[] = []; constructor() { super(); - this.client = new $scramjet.shared.util.BareClient(); + this.client = new BareClient(); const db = indexedDB.open("$scramjet", 1); @@ -89,19 +90,17 @@ export class ScramjetServiceWorker extends EventTarget { const db = request.result; const tx = db.transaction("config", "readonly"); const store = tx.objectStore("config"); - const config = store.get("config"); + const storedconfig = store.get("config"); - config.onsuccess = async () => { - this.config = config.result; - $scramjet.config = config.result; - - loadCodecs(); + storedconfig.onsuccess = async () => { + this.config = storedconfig.result; + setConfig(storedconfig.result); await asyncSetWasm(); resolve(); }; - config.onerror = () => reject(config.error); + storedconfig.onerror = () => reject(storedconfig.error); }; request.onerror = () => reject(request.error); From 17482c8c800e6e0ed271da1ec7e6f318352a9855 Mon Sep 17 00:00:00 2001 From: velzie Date: Wed, 16 Jul 2025 10:08:14 -0400 Subject: [PATCH 3/5] bump rspack --- package.json | 4 +- pnpm-lock.yaml | 295 ++++++++++++++++++++++++++----------------------- 2 files changed, 161 insertions(+), 138 deletions(-) diff --git a/package.json b/package.json index 016f57e0..fad59744 100644 --- a/package.json +++ b/package.json @@ -43,8 +43,8 @@ "@nebula-services/bare-server-node": "^2.0.4", "@playwright/test": "^1.53.1", "@rsdoctor/rspack-plugin": "^1.1.5", - "@rspack/cli": "^1.4.1", - "@rspack/core": "^1.4.1", + "@rspack/cli": "^1.4.8", + "@rspack/core": "^1.4.8", "@types/eslint": "^9.6.1", "@types/estree": "^1.0.8", "@types/node": "^24.0.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee41559e..42327b5d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,13 +62,13 @@ importers: version: 1.53.1 '@rsdoctor/rspack-plugin': specifier: ^1.1.5 - version: 1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1) + version: 1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1) '@rspack/cli': - specifier: ^1.4.1 - version: 1.4.1(@rspack/core@1.4.1)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1) + specifier: ^1.4.8 + version: 1.4.8(@rspack/core@1.4.8)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1) '@rspack/core': - specifier: ^1.4.1 - version: 1.4.1 + specifier: ^1.4.8 + version: 1.4.8 '@types/eslint': specifier: ^9.6.1 version: 9.6.1 @@ -104,7 +104,7 @@ importers: version: 3.6.2 ts-checker-rspack-plugin: specifier: ^1.1.4 - version: 1.1.4(@rspack/core@1.4.1)(typescript@5.8.3) + version: 1.1.4(@rspack/core@1.4.8)(typescript@5.8.3) tslib: specifier: ^2.8.1 version: 2.8.1 @@ -216,14 +216,14 @@ packages: resolution: {integrity: sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==} engines: {node: '>=10.0.0'} - '@emnapi/core@1.4.3': - resolution: {integrity: sha512-4m62DuCE07lw01soJwPiBGC0nAww0Q+RY70VZ+n49yDIO13yyinhbWCeNnaob0lakDtWQzSdtNWzJeOJt2ma+g==} + '@emnapi/core@1.4.4': + resolution: {integrity: sha512-A9CnAbC6ARNMKcIcrQwq6HeHCjpcBZ5wSx4U01WXCqEKlrzB9F9315WDNHkrs2xbx7YjjSxbUYxuN6EQzpcY2g==} - '@emnapi/runtime@1.4.3': - resolution: {integrity: sha512-pBPWdu6MLKROBX05wSNKcNb++m5Er+KQ9QkB+WVM+pW2Kx9hoSrVTnu3BdkI5eBLZoKu/J6mW/B6i6bJB2ytXQ==} + '@emnapi/runtime@1.4.4': + resolution: {integrity: sha512-hHyapA4A3gPaDCNfiqyZUStTMqIkKRshqPIuDOXv1hcBnD4U3l8cP0T1HMCfGRxQ6V64TGCcoswChANyOAwbQg==} - '@emnapi/wasi-threads@1.0.2': - resolution: {integrity: sha512-5n3nTJblwRi8LlXkJ9eBzu+kZR8Yxcc7ubakyQTFzPMtIhFpUBRbsnc2Dv88IZDIbCDlBiWrknhB4Lsz7mg6BA==} + '@emnapi/wasi-threads@1.0.3': + resolution: {integrity: sha512-8K5IFFsQqF9wQNJptGbS6FNKgUTsSRYnTqNCG1vPP8jFdjSv18n2mQfJpkt2Oibo9iBEzcDnDxNwKTzC7svlJw==} '@esbuild/aix-ppc64@0.25.6': resolution: {integrity: sha512-ShbM/3XxwuxjFiuVBHA+d3j5dyac0aEVVq1oluIDf71hUw0aRF59dV/efUsIwFnR6m8JNM2FjZOzmaZ8yG61kw==} @@ -512,9 +512,15 @@ packages: '@jridgewell/sourcemap-codec@1.5.0': resolution: {integrity: sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ==} + '@jridgewell/sourcemap-codec@1.5.4': + resolution: {integrity: sha512-VT2+G1VQs/9oz078bLrYbecdZKs912zQlkelYpuf+SXF+QvZDYJlbx/LSx+meSAwdDFnF8FVXW92AVjjkVmgFw==} + '@jridgewell/trace-mapping@0.3.25': resolution: {integrity: sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==} + '@jridgewell/trace-mapping@0.3.29': + resolution: {integrity: sha512-uw6guiW/gcAGPDhLmd77/6lW8QLeiV5RUTsAX46Db6oLhGaVj4lhnPwb184s1bkc8kdVg/+h988dro8GRDpmYQ==} + '@jsonjoy.com/base64@1.1.2': resolution: {integrity: sha512-q6XAnWQDIMA3+FTiOYajoYqySkO+JSat0ytXGSuRdq9uXE7o92gzuQwQM14xaCRlBLGq3v5miDGC4vkVTn54xA==} engines: {node: '>=10.0'} @@ -562,26 +568,26 @@ packages: resolution: {integrity: sha512-egc9jbBmNqPXGOkvOOG142SLaBL3jBkz0IGnhYYWOnmg5FeC6KfGSRU5xlk+dKYecE3tuH03bFNi3/vBAOoJKA==} hasBin: true - '@module-federation/error-codes@0.15.0': - resolution: {integrity: sha512-CFJSF+XKwTcy0PFZ2l/fSUpR4z247+Uwzp1sXVkdIfJ/ATsnqf0Q01f51qqSEA6MYdQi6FKos9FIcu3dCpQNdg==} + '@module-federation/error-codes@0.16.0': + resolution: {integrity: sha512-TfmA45b8vvISniGudMg8jjIy1q3tLPon0QN/JdFp5f8AJ8/peICN5b+dkEQnWsAVg2fEusYhk9dO7z3nUeJM8A==} - '@module-federation/runtime-core@0.15.0': - resolution: {integrity: sha512-RYzI61fRDrhyhaEOXH3AgIGlHiot0wPFXu7F43cr+ZnTi+VlSYWLdlZ4NBuT9uV6JSmH54/c+tEZm5SXgKR2sQ==} + '@module-federation/runtime-core@0.16.0': + resolution: {integrity: sha512-5SECQowG4hlUVBRk/y6bnYLfxbsl5NcMmqn043WPe7NDOhGQWbTuYibJ3Bk+ZBv5U4uYLEmXipBGDc1FKsHklQ==} - '@module-federation/runtime-tools@0.15.0': - resolution: {integrity: sha512-kzFn3ObUeBp5vaEtN1WMxhTYBuYEErxugu1RzFUERD21X3BZ+b4cWwdFJuBDlsmVjctIg/QSOoZoPXRKAO0foA==} + '@module-federation/runtime-tools@0.16.0': + resolution: {integrity: sha512-OzmXNluXBQ2E6znzX4m9CJt1MFHVGmbN8c8MSKcYIDcLzLSKBQAiaz9ZUMhkyWx2YrPgD134glyPEqJrc+fY8A==} - '@module-federation/runtime@0.15.0': - resolution: {integrity: sha512-dTPsCNum9Bhu3yPOcrPYq0YnM9eCMMMNB1wuiqf1+sFbQlNApF0vfZxooqz3ln0/MpgE0jerVvFsLVGfqvC9Ug==} + '@module-federation/runtime@0.16.0': + resolution: {integrity: sha512-6o84WI8Qhc9O3HwPLx89kTvOSkyUOHQr73R/zr0I04sYhlMJgw5xTwXeGE7bQAmNgbJclzW9Kh7JTP7+3o3CHg==} - '@module-federation/sdk@0.15.0': - resolution: {integrity: sha512-PWiYbGcJrKUD6JZiEPihrXhV3bgXdll4bV7rU+opV7tHaun+Z0CdcawjZ82Xnpb8MCPGmqHwa1MPFeUs66zksw==} + '@module-federation/sdk@0.16.0': + resolution: {integrity: sha512-UXJW1WWuDoDmScX0tpISjl4xIRPzAiN62vg9etuBdAEUM+ja9rz/zwNZaByiUPFS2aqlj2RHenCRvIapE8mYEg==} - '@module-federation/webpack-bundler-runtime@0.15.0': - resolution: {integrity: sha512-i+3wu2Ljh2TmuUpsnjwZVupOVqV50jP0ndA8PSP4gwMKlgdGeaZ4VH5KkHAXGr2eiYUxYLMrJXz1+eILJqeGDg==} + '@module-federation/webpack-bundler-runtime@0.16.0': + resolution: {integrity: sha512-yqIDQTelJZP0Rxml0OXv4Er8Kbdxy7NFh6PCzPwDFWI1SkiokJ3uXQJBvtlxZ3lOnCDYOzdHstqa8sJG4JP02Q==} - '@napi-rs/wasm-runtime@0.2.11': - resolution: {integrity: sha512-9DPkXtvHydrcOsopiYpUgPHpmj0HWZKMUnL2dZqpvC42lsratuBG06V5ipyno0fUek5VlFsNQ+AcFATSrJXgMA==} + '@napi-rs/wasm-runtime@0.2.12': + resolution: {integrity: sha512-ZVWUcfwY4E/yPitQJl481FjFo3K22D6qF0DuFH6Y/nbnE11GY5uguDxZMGXPQ8WQ0128MXQD7TnfHyK4oWoIJQ==} '@nebula-services/bare-server-node@2.0.4': resolution: {integrity: sha512-Jcr+QtkLJVmppdbBarEbRp1TtCsL4pjFIcX6+KPURRqcsOP7hZfYclhjmCserwEC7jT+WBduXpFd3qwqeRBNew==} @@ -799,66 +805,66 @@ packages: '@rsdoctor/utils@1.1.5': resolution: {integrity: sha512-EKjYD+OVo9UdsG1RT/D5RWva2RYn51cZ70e0PAHav0J9EGOWa7dAkKr0ygn4AaRUBdviWJap8W9+NAaI20YyqA==} - '@rspack/binding-darwin-arm64@1.4.1': - resolution: {integrity: sha512-enh5DYbpaexdEmjbcxj3BJDauP3w+20jFKWvKROtAQV350PUw0bf2b4WOgngIH9hBzlfjpXNYAk6T5AhVAlY3Q==} + '@rspack/binding-darwin-arm64@1.4.8': + resolution: {integrity: sha512-PQRNjC3Fc0avpx8Gk+sT5P+HAXxTSzmBA8lU7QLlmbW5GGXO2taVhNstbZ4oxyIX5uDVZpQ2yQ2E0zXirK6/UQ==} cpu: [arm64] os: [darwin] - '@rspack/binding-darwin-x64@1.4.1': - resolution: {integrity: sha512-KoehyhBji4TLXhn4mqOUw6xsQNRzNVA9XcCm1Jx+M1Qb0dhMTNfduvBSyXuRV5+/QaRbk7+4UJbyRNFUtt96kA==} + '@rspack/binding-darwin-x64@1.4.8': + resolution: {integrity: sha512-ZnPZbo1dhhbfevxSS99y8w02xuEbxyiV1HaUie/S8jzy9DPmk+4Br+DddufnibPNU85e3BZKjp+HDFMYkdn6cg==} cpu: [x64] os: [darwin] - '@rspack/binding-linux-arm64-gnu@1.4.1': - resolution: {integrity: sha512-PJ5cHqvrj1bK7jH5DVrdKoR8Fy+p6l9baxXajq/6xWTxP+4YTdEtLsRZnpLMS1Ho2RRpkxDWJn+gdlKuleNioQ==} + '@rspack/binding-linux-arm64-gnu@1.4.8': + resolution: {integrity: sha512-mJK9diM4Gd8RIGO90AZnl27WwUuAOoRplPQv9G+Vxu2baCt1xE1ccf8PntIJ70/rMgsUdnmkR5qQBaGxhAMJvA==} cpu: [arm64] os: [linux] - '@rspack/binding-linux-arm64-musl@1.4.1': - resolution: {integrity: sha512-cpDz+z3FwVQfK6VYfXQEb0ym6fFIVmvK4y3R/2VAbVGWYVxZB5I6AcSdOWdDnpppHmcHpf+qQFlwhHvbpMMJNQ==} + '@rspack/binding-linux-arm64-musl@1.4.8': + resolution: {integrity: sha512-+n9QxeDDZKwVB4D6cwpNRJzsCeuwNqd/fwwbMQVTctJ+GhIHlUPsE8y5tXN7euU7kDci81wMBBFlt6LtXNcssA==} cpu: [arm64] os: [linux] - '@rspack/binding-linux-x64-gnu@1.4.1': - resolution: {integrity: sha512-jjTx53CpiYWK7fAv5qS8xHEytFK6gLfZRk+0kt2YII6uqez/xQ3SRcboreH8XbJcBoxINBzMNMf5/SeMBZ939A==} + '@rspack/binding-linux-x64-gnu@1.4.8': + resolution: {integrity: sha512-rEypDlbIfv9B/DcZ2vYVWs56wo5VWE5oj/TvM9JT+xuqwvVWsN/A2TPMiU6QBgOKGXat3EM/MEgx8NhNZUpkXg==} cpu: [x64] os: [linux] - '@rspack/binding-linux-x64-musl@1.4.1': - resolution: {integrity: sha512-FAyR3Og81Smtr/CnsuTiW4ZCYAPCqeV73lzMKZ9xdVUgM9324ryEgqgX38GZLB5Mo7cvQhv7/fpMeHQo16XQCw==} + '@rspack/binding-linux-x64-musl@1.4.8': + resolution: {integrity: sha512-o9OsvJ7olH0JPU9exyIaYTNQ+aaR5CNAiinkxr+LkV2i3DMIi/+pDVveDiodYjVhzZjWfsP/z8QPO4c6Z06bEw==} cpu: [x64] os: [linux] - '@rspack/binding-wasm32-wasi@1.4.1': - resolution: {integrity: sha512-3Q1VICIQP4GsaTJEmmwfowQ48NvhlL0CKH88l5+mbji2rBkGx7yR67pPdfCVNjXcCtFoemTYw98eaumJTjC++g==} + '@rspack/binding-wasm32-wasi@1.4.8': + resolution: {integrity: sha512-hF5gqT0aQ66VUclM2A9MSB6zVdEJqzp++TAXaShBK/eVBI0R4vWrMfJ2TOdzEsSbg4gXgeG4swURpHva3PKbcA==} cpu: [wasm32] - '@rspack/binding-win32-arm64-msvc@1.4.1': - resolution: {integrity: sha512-DdLPOy1J98kn45uEhiEqlBKgMvet+AxOzX2OcrnU0wQXthGM9gty1YXYNryOhlK+X+eOcwcP3GbnDOAKi8nKqw==} + '@rspack/binding-win32-arm64-msvc@1.4.8': + resolution: {integrity: sha512-umD0XzesJq4nnStv9/2/VOmzNUWHfLMIjeHmiHYHpc7iVC0SkXgIdc6Ac7c+g2q7/V3/MFxL66Y60oy7lQE3fg==} cpu: [arm64] os: [win32] - '@rspack/binding-win32-ia32-msvc@1.4.1': - resolution: {integrity: sha512-13s8fYtyC9DyvKosD2Kvzd6fVZDZZyPp91L4TEXWaO0CFhaCbtLTYIntExq9MwtKHYKKx7bchIFw93o0xjKjUg==} + '@rspack/binding-win32-ia32-msvc@1.4.8': + resolution: {integrity: sha512-Uu+F/sxz7GgIMbuCCZVOD1HPjoHQdyrFHi/TE2EmuZzs9Ji9a9mtNJNrKc8+h9YFpaLeade7cbMDjRu4MHxiVA==} cpu: [ia32] os: [win32] - '@rspack/binding-win32-x64-msvc@1.4.1': - resolution: {integrity: sha512-ubQW8FcLnwljDanwTzkC9Abyo59gmX8m9uVr1GHOEuEU9Cua0KMijX2j/MYfiziz4nuQgv1saobY7K1I5nE3YA==} + '@rspack/binding-win32-x64-msvc@1.4.8': + resolution: {integrity: sha512-BVkOfJDZnexHNpGgc/sWENyGrsle1jUQTeUEdSyNYsu4Elsgk/T9gnGK8xyLRd2c6k20M5FN38t0TumCp4DscQ==} cpu: [x64] os: [win32] - '@rspack/binding@1.4.1': - resolution: {integrity: sha512-zYgOmI+LC2zxB/LIcnaeK66ElFHaPChdWzRruTT1LAFFwpgGkBGAwFoP27PDnxQW0Aejci21Ld8X9tyxm08QFw==} + '@rspack/binding@1.4.8': + resolution: {integrity: sha512-VKE+2InUdudBUOn3xMZfK9a6KlOwmSifA0Nupjsh7N9/brcBfJtJGSDCnfrIKCq54FF+QAUCgcNAS0DB4/tZmw==} - '@rspack/cli@1.4.1': - resolution: {integrity: sha512-ZKJQD8rq9RR43MaclYf941kJbBNHGI8Kh8I/QfmL90+Q1dLsr0VEdTHH5W4LtD5Kv4dt2QI0Cx3n/568usbpHg==} + '@rspack/cli@1.4.8': + resolution: {integrity: sha512-rqQ8iI/zKaT+xiETFQvzzZI4Bpx5hk0IR4BXJwiR/llPQLN/oc1saKyatsn2/p4r0+ABLMftdzKPv6FzIvnzZA==} hasBin: true peerDependencies: '@rspack/core': ^1.0.0-alpha || ^1.x - '@rspack/core@1.4.1': - resolution: {integrity: sha512-UTRCTQk2G8YiPBiMvfn8FcysxeO4Muek6a/Z39Cw2r4ZI8k5iPnKiyZboTJLS7120PwWBw2SO+QQje35Z44x0g==} + '@rspack/core@1.4.8': + resolution: {integrity: sha512-ARHuZ+gx3P//RIUKSjk/riQUn/D5tCwCWbfgeM5pk/Ti2JsgVnqiP9Sksge8JovVPf7b6Zgw73Cq5FpX4aOXeQ==} engines: {node: '>=16.0.0'} peerDependencies: '@swc/helpers': '>=0.5.1' @@ -879,8 +885,8 @@ packages: '@socket.io/component-emitter@3.1.2': resolution: {integrity: sha512-9BCxFwvbGg/RsZK9tjXd8s4UcwR0MWeFQ1XEKIQVVvAGJyINdrqKMcTRyLoK8Rse1GjzLV9cwjWV1olXRWEXVA==} - '@tybys/wasm-util@0.9.0': - resolution: {integrity: sha512-6+7nlbMVX/PVDCwaIQ8nTOPveOcFLSt8GcXdx8hD0bt39uWxYT88uXzqTd4fTvqta7oeUJqudepapKNt2DYJFw==} + '@tybys/wasm-util@0.10.0': + resolution: {integrity: sha512-VyyPYFlOMNylG45GoAe0xDoLwWuowvf92F9kySqzYh8vmYm7D2u4iUJKa1tOUpS70Ku13ASrOkS4ScXFsTaCNQ==} '@types/body-parser@1.19.6': resolution: {integrity: sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g==} @@ -936,6 +942,9 @@ packages: '@types/node-forge@1.3.11': resolution: {integrity: sha512-FQx220y22OKNTqaByeBGqHWYz4cl94tpcxeFdvBo3wjG6XPBuZ0BNgNZRV5J5TFmmcsJ4IzsLkmGRiQbnYsBEQ==} + '@types/node@24.0.14': + resolution: {integrity: sha512-4zXMWD91vBLGRtHK3YbIoFMia+1nqEz72coM42C5ETjnNCa/heoj7NT1G67iAfOqMmcfhuCZ4uNpyz8EjlAejw==} + '@types/node@24.0.6': resolution: {integrity: sha512-ZOyn+gOs749xU7ovp+Ibj0g1o3dFRqsfPnT22C2t5JzcRvgsEDpGawPbCISGKLudJk9Y0wiu9sYd6kUh0pc9TA==} @@ -3032,18 +3041,18 @@ snapshots: '@discoveryjs/json-ext@0.5.7': {} - '@emnapi/core@1.4.3': + '@emnapi/core@1.4.4': dependencies: - '@emnapi/wasi-threads': 1.0.2 + '@emnapi/wasi-threads': 1.0.3 tslib: 2.8.1 optional: true - '@emnapi/runtime@1.4.3': + '@emnapi/runtime@1.4.4': dependencies: tslib: 2.8.1 optional: true - '@emnapi/wasi-threads@1.0.2': + '@emnapi/wasi-threads@1.0.3': dependencies: tslib: 2.8.1 optional: true @@ -3272,11 +3281,20 @@ snapshots: '@jridgewell/sourcemap-codec@1.5.0': {} + '@jridgewell/sourcemap-codec@1.5.4': + optional: true + '@jridgewell/trace-mapping@0.3.25': dependencies: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.5.0 + '@jridgewell/trace-mapping@0.3.29': + dependencies: + '@jridgewell/resolve-uri': 3.1.2 + '@jridgewell/sourcemap-codec': 1.5.4 + optional: true + '@jsonjoy.com/base64@1.1.2(tslib@2.8.1)': dependencies: tslib: 2.8.1 @@ -3326,36 +3344,36 @@ snapshots: transitivePeerDependencies: - utf-8-validate - '@module-federation/error-codes@0.15.0': {} + '@module-federation/error-codes@0.16.0': {} - '@module-federation/runtime-core@0.15.0': + '@module-federation/runtime-core@0.16.0': dependencies: - '@module-federation/error-codes': 0.15.0 - '@module-federation/sdk': 0.15.0 + '@module-federation/error-codes': 0.16.0 + '@module-federation/sdk': 0.16.0 - '@module-federation/runtime-tools@0.15.0': + '@module-federation/runtime-tools@0.16.0': dependencies: - '@module-federation/runtime': 0.15.0 - '@module-federation/webpack-bundler-runtime': 0.15.0 + '@module-federation/runtime': 0.16.0 + '@module-federation/webpack-bundler-runtime': 0.16.0 - '@module-federation/runtime@0.15.0': + '@module-federation/runtime@0.16.0': dependencies: - '@module-federation/error-codes': 0.15.0 - '@module-federation/runtime-core': 0.15.0 - '@module-federation/sdk': 0.15.0 + '@module-federation/error-codes': 0.16.0 + '@module-federation/runtime-core': 0.16.0 + '@module-federation/sdk': 0.16.0 - '@module-federation/sdk@0.15.0': {} + '@module-federation/sdk@0.16.0': {} - '@module-federation/webpack-bundler-runtime@0.15.0': + '@module-federation/webpack-bundler-runtime@0.16.0': dependencies: - '@module-federation/runtime': 0.15.0 - '@module-federation/sdk': 0.15.0 + '@module-federation/runtime': 0.16.0 + '@module-federation/sdk': 0.16.0 - '@napi-rs/wasm-runtime@0.2.11': + '@napi-rs/wasm-runtime@0.2.12': dependencies: - '@emnapi/core': 1.4.3 - '@emnapi/runtime': 1.4.3 - '@tybys/wasm-util': 0.9.0 + '@emnapi/core': 1.4.4 + '@emnapi/runtime': 1.4.4 + '@tybys/wasm-util': 0.10.0 optional: true '@nebula-services/bare-server-node@2.0.4(bufferutil@4.0.9)': @@ -3502,13 +3520,13 @@ snapshots: '@rsdoctor/client@1.1.5': {} - '@rsdoctor/core@1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1)': + '@rsdoctor/core@1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1)': dependencies: '@rsbuild/plugin-check-syntax': 1.3.0 - '@rsdoctor/graph': 1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1) - '@rsdoctor/sdk': 1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1) - '@rsdoctor/types': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) - '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) + '@rsdoctor/graph': 1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1) + '@rsdoctor/sdk': 1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1) + '@rsdoctor/types': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) + '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) axios: 1.10.0 browserslist-load-config: 1.0.0 enhanced-resolve: 5.12.0 @@ -3528,10 +3546,10 @@ snapshots: - utf-8-validate - webpack - '@rsdoctor/graph@1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1)': + '@rsdoctor/graph@1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1)': dependencies: - '@rsdoctor/types': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) - '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) + '@rsdoctor/types': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) + '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) lodash.unionby: 4.8.0 socket.io: 4.8.1(bufferutil@4.0.9) source-map: 0.7.4 @@ -3542,16 +3560,16 @@ snapshots: - utf-8-validate - webpack - '@rsdoctor/rspack-plugin@1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1)': + '@rsdoctor/rspack-plugin@1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1)': dependencies: - '@rsdoctor/core': 1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1) - '@rsdoctor/graph': 1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1) - '@rsdoctor/sdk': 1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1) - '@rsdoctor/types': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) - '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) + '@rsdoctor/core': 1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1) + '@rsdoctor/graph': 1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1) + '@rsdoctor/sdk': 1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1) + '@rsdoctor/types': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) + '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) lodash: 4.17.21 optionalDependencies: - '@rspack/core': 1.4.1 + '@rspack/core': 1.4.8 transitivePeerDependencies: - '@rsbuild/core' - bufferutil @@ -3560,12 +3578,12 @@ snapshots: - utf-8-validate - webpack - '@rsdoctor/sdk@1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1)': + '@rsdoctor/sdk@1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1)': dependencies: '@rsdoctor/client': 1.1.5 - '@rsdoctor/graph': 1.1.5(@rspack/core@1.4.1)(bufferutil@4.0.9)(webpack@5.97.1) - '@rsdoctor/types': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) - '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) + '@rsdoctor/graph': 1.1.5(@rspack/core@1.4.8)(bufferutil@4.0.9)(webpack@5.97.1) + '@rsdoctor/types': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) + '@rsdoctor/utils': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) '@types/fs-extra': 11.0.4 body-parser: 1.20.3 cors: 2.8.5 @@ -3585,20 +3603,20 @@ snapshots: - utf-8-validate - webpack - '@rsdoctor/types@1.1.5(@rspack/core@1.4.1)(webpack@5.97.1)': + '@rsdoctor/types@1.1.5(@rspack/core@1.4.8)(webpack@5.97.1)': dependencies: '@types/connect': 3.4.38 '@types/estree': 1.0.5 '@types/tapable': 2.2.7 source-map: 0.7.4 optionalDependencies: - '@rspack/core': 1.4.1 + '@rspack/core': 1.4.8 webpack: 5.97.1 - '@rsdoctor/utils@1.1.5(@rspack/core@1.4.1)(webpack@5.97.1)': + '@rsdoctor/utils@1.1.5(@rspack/core@1.4.8)(webpack@5.97.1)': dependencies: '@babel/code-frame': 7.26.2 - '@rsdoctor/types': 1.1.5(@rspack/core@1.4.1)(webpack@5.97.1) + '@rsdoctor/types': 1.1.5(@rspack/core@1.4.8)(webpack@5.97.1) '@types/estree': 1.0.5 acorn: 8.15.0 acorn-import-attributes: 1.9.5(acorn@8.15.0) @@ -3619,56 +3637,56 @@ snapshots: - supports-color - webpack - '@rspack/binding-darwin-arm64@1.4.1': + '@rspack/binding-darwin-arm64@1.4.8': optional: true - '@rspack/binding-darwin-x64@1.4.1': + '@rspack/binding-darwin-x64@1.4.8': optional: true - '@rspack/binding-linux-arm64-gnu@1.4.1': + '@rspack/binding-linux-arm64-gnu@1.4.8': optional: true - '@rspack/binding-linux-arm64-musl@1.4.1': + '@rspack/binding-linux-arm64-musl@1.4.8': optional: true - '@rspack/binding-linux-x64-gnu@1.4.1': + '@rspack/binding-linux-x64-gnu@1.4.8': optional: true - '@rspack/binding-linux-x64-musl@1.4.1': + '@rspack/binding-linux-x64-musl@1.4.8': optional: true - '@rspack/binding-wasm32-wasi@1.4.1': + '@rspack/binding-wasm32-wasi@1.4.8': dependencies: - '@napi-rs/wasm-runtime': 0.2.11 + '@napi-rs/wasm-runtime': 0.2.12 optional: true - '@rspack/binding-win32-arm64-msvc@1.4.1': + '@rspack/binding-win32-arm64-msvc@1.4.8': optional: true - '@rspack/binding-win32-ia32-msvc@1.4.1': + '@rspack/binding-win32-ia32-msvc@1.4.8': optional: true - '@rspack/binding-win32-x64-msvc@1.4.1': + '@rspack/binding-win32-x64-msvc@1.4.8': optional: true - '@rspack/binding@1.4.1': + '@rspack/binding@1.4.8': optionalDependencies: - '@rspack/binding-darwin-arm64': 1.4.1 - '@rspack/binding-darwin-x64': 1.4.1 - '@rspack/binding-linux-arm64-gnu': 1.4.1 - '@rspack/binding-linux-arm64-musl': 1.4.1 - '@rspack/binding-linux-x64-gnu': 1.4.1 - '@rspack/binding-linux-x64-musl': 1.4.1 - '@rspack/binding-wasm32-wasi': 1.4.1 - '@rspack/binding-win32-arm64-msvc': 1.4.1 - '@rspack/binding-win32-ia32-msvc': 1.4.1 - '@rspack/binding-win32-x64-msvc': 1.4.1 - - '@rspack/cli@1.4.1(@rspack/core@1.4.1)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1)': + '@rspack/binding-darwin-arm64': 1.4.8 + '@rspack/binding-darwin-x64': 1.4.8 + '@rspack/binding-linux-arm64-gnu': 1.4.8 + '@rspack/binding-linux-arm64-musl': 1.4.8 + '@rspack/binding-linux-x64-gnu': 1.4.8 + '@rspack/binding-linux-x64-musl': 1.4.8 + '@rspack/binding-wasm32-wasi': 1.4.8 + '@rspack/binding-win32-arm64-msvc': 1.4.8 + '@rspack/binding-win32-ia32-msvc': 1.4.8 + '@rspack/binding-win32-x64-msvc': 1.4.8 + + '@rspack/cli@1.4.8(@rspack/core@1.4.8)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1)': dependencies: '@discoveryjs/json-ext': 0.5.7 - '@rspack/core': 1.4.1 - '@rspack/dev-server': 1.1.3(@rspack/core@1.4.1)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1) + '@rspack/core': 1.4.8 + '@rspack/dev-server': 1.1.3(@rspack/core@1.4.8)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1) colorette: 2.0.20 exit-hook: 4.0.0 interpret: 3.1.1 @@ -3684,15 +3702,15 @@ snapshots: - webpack - webpack-cli - '@rspack/core@1.4.1': + '@rspack/core@1.4.8': dependencies: - '@module-federation/runtime-tools': 0.15.0 - '@rspack/binding': 1.4.1 + '@module-federation/runtime-tools': 0.16.0 + '@rspack/binding': 1.4.8 '@rspack/lite-tapable': 1.0.1 - '@rspack/dev-server@1.1.3(@rspack/core@1.4.1)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1)': + '@rspack/dev-server@1.1.3(@rspack/core@1.4.8)(@types/express@4.17.23)(bufferutil@4.0.9)(webpack@5.97.1)': dependencies: - '@rspack/core': 1.4.1 + '@rspack/core': 1.4.8 chokidar: 3.6.0 http-proxy-middleware: 2.0.9(@types/express@4.17.23) p-retry: 6.2.1 @@ -3711,7 +3729,7 @@ snapshots: '@socket.io/component-emitter@3.1.2': {} - '@tybys/wasm-util@0.9.0': + '@tybys/wasm-util@0.10.0': dependencies: tslib: 2.8.1 optional: true @@ -3790,6 +3808,11 @@ snapshots: dependencies: '@types/node': 24.0.6 + '@types/node@24.0.14': + dependencies: + undici-types: 7.8.0 + optional: true + '@types/node@24.0.6': dependencies: undici-types: 7.8.0 @@ -4978,7 +5001,7 @@ snapshots: jest-worker@27.5.1: dependencies: - '@types/node': 24.0.6 + '@types/node': 24.0.14 merge-stream: 2.0.0 supports-color: 8.1.1 optional: true @@ -5669,7 +5692,7 @@ snapshots: terser-webpack-plugin@5.3.14(webpack@5.97.1): dependencies: - '@jridgewell/trace-mapping': 0.3.25 + '@jridgewell/trace-mapping': 0.3.29 jest-worker: 27.5.1 schema-utils: 4.3.2 serialize-javascript: 6.0.2 @@ -5721,7 +5744,7 @@ snapshots: dependencies: typescript: 5.8.3 - ts-checker-rspack-plugin@1.1.4(@rspack/core@1.4.1)(typescript@5.8.3): + ts-checker-rspack-plugin@1.1.4(@rspack/core@1.4.8)(typescript@5.8.3): dependencies: '@babel/code-frame': 7.27.1 '@rspack/lite-tapable': 1.0.1 @@ -5732,7 +5755,7 @@ snapshots: picocolors: 1.1.1 typescript: 5.8.3 optionalDependencies: - '@rspack/core': 1.4.1 + '@rspack/core': 1.4.8 tslib@2.8.1: {} From 7ffb641b1046dc9ec5ff7d61e367b7284ee72c13 Mon Sep 17 00:00:00 2001 From: velzie Date: Wed, 16 Jul 2025 10:09:44 -0400 Subject: [PATCH 4/5] get unified bundle working, update demo ui to use new paths --- lib/index.d.ts | 10 ++++++++-- src/client/index.ts | 5 +++-- src/client/shared/sourcemaps.ts | 26 +++++++++++--------------- src/entry.ts | 25 +++++++++++++------------ src/shared/rewriters/js.ts | 5 +---- src/shared/rewriters/wasm.ts | 24 +++++++++++++++++++++--- src/shared/rewriters/worker.ts | 2 +- src/worker/error.ts | 6 ++---- src/worker/fetch.ts | 4 ++-- static/sw.js | 6 ++++-- static/ui.js | 10 +++++----- 11 files changed, 71 insertions(+), 52 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 10d1fe17..3d13e0ec 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,16 +1,22 @@ declare const scramjetPath: string; import * as controller from "../dist/types/controller/index.ts"; +import * as worker from "../dist/types/worker/index.ts"; import * as types from "../dist/types/types.ts"; import * as frame from "../dist/types/controller/frame.ts"; declare global { - const ScramjetController: typeof controller.ScramjetController; - const ScramjetFrame: typeof frame.ScramjetFrame; + function $scramjetLoadController(): typeof controller; + function $scramjetLoadWorker(): typeof worker; + function $scramjetLoadClient(config: ScramjetConfig); type ScramjetController = controller.ScramjetController; type ScramjetFrame = frame.ScramjetFrame; type ScramjetConfig = types.ScramjetConfig; type ScramjetInitConfig = types.ScramjetInitConfig; + var $scramjetVersion: { + build: string; + version: string; + }; } export { scramjetPath }; diff --git a/src/client/index.ts b/src/client/index.ts index c69ee81b..2d6a1246 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -1,6 +1,6 @@ // entrypoint for scramjet.client.js -import { loadCodecs } from "../shared/index"; +import { loadCodecs, setConfig } from "../shared/index"; import { SCRAMJETCLIENT } from "../symbols"; import { ScramjetClient } from "./client"; import { ScramjetContextEvent, UrlChangeEvent } from "./events"; @@ -21,7 +21,8 @@ function createFrameId() { .join("")}`; } -export function clientInitHook() { +export function clientInitHook(config: ScramjetConfig) { + setConfig(config); dbg.log("initializing scramjet client"); // if it already exists, that means the handlers have probably already been setup by the parent document if (!(SCRAMJETCLIENT in >self)) { diff --git a/src/client/shared/sourcemaps.ts b/src/client/shared/sourcemaps.ts index e8d32e3e..8e45257f 100644 --- a/src/client/shared/sourcemaps.ts +++ b/src/client/shared/sourcemaps.ts @@ -1,4 +1,4 @@ -import { flagEnabled } from "../../shared"; +import { config, flagEnabled } from "../../shared"; import { SCRAMJETCLIENT, SCRAMJETCLIENTNAME } from "../../symbols"; import { ProxyCtx, ScramjetClient } from "../client"; @@ -185,20 +185,16 @@ export const enabled = (client: ScramjetClient) => export default function (client: ScramjetClient, self: Self) { // every script will push a sourcemap - Object.defineProperty( - self, - globalThis.$scramjet.config.globals.pushsourcemapfn, - { - value: (buf: Array, tag: string) => { - const before = performance.now(); - registerRewrites(buf, tag); - dbg.time(client.meta, before, `scramtag parse for ${tag}`); - }, - enumerable: false, - writable: false, - configurable: false, - } - ); + Object.defineProperty(self, config.globals.pushsourcemapfn, { + value: (buf: Array, tag: string) => { + const before = performance.now(); + registerRewrites(buf, tag); + dbg.time(client.meta, before, `scramtag parse for ${tag}`); + }, + enumerable: false, + writable: false, + configurable: false, + }); // when we rewrite javascript it will make function.toString leak internals // this can lead to double rewrites which is bad diff --git a/src/entry.ts b/src/entry.ts index 5fe1b12b..348d1478 100644 --- a/src/entry.ts +++ b/src/entry.ts @@ -1,27 +1,28 @@ /// +import "../lib/index.d.ts"; -export function $scramjetLoadController() { +self.$scramjetLoadController = function () { return require("./controller/index"); -} +}; -export function $scramjetLoadClient() { +self.$scramjetLoadClient = function (config) { const client = require("./client/index"); - client.clientInitHook(); + client.clientInitHook(config); return client; -} +}; -export function $scramjetLoadWorker() { +self.$scramjetLoadWorker = function () { return require("./worker/index"); -} +}; -export const $scramjet = { - version: { - build: COMMITHASH, - version: VERSION, - }, +export const $scramjetVersion = { + build: COMMITHASH, + version: VERSION, }; +self.$scramjetVersion = $scramjetVersion; + if ("document" in self && document?.currentScript) { document.currentScript.remove(); } diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index 2bda17b6..79a340a0 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -45,10 +45,7 @@ function rewriteJsWasm( let { js, map, scramtag, errors } = out; if (flagEnabled("sourcemaps", meta.base) && !globalThis.clients) { - globalThis[globalThis.$scramjet.config.globals.pushsourcemapfn]( - Array.from(map), - scramtag - ); + globalThis[config.globals.pushsourcemapfn](Array.from(map), scramtag); map = null; } diff --git a/src/shared/rewriters/wasm.ts b/src/shared/rewriters/wasm.ts index 3931db5a..49f36aea 100644 --- a/src/shared/rewriters/wasm.ts +++ b/src/shared/rewriters/wasm.ts @@ -1,11 +1,14 @@ // i am a cat. i like to be petted. i like to be fed. i like to be import { initSync, Rewriter } from "../../../rewriter/wasm/out/wasm.js"; import type { JsRewriterOutput } from "../../../rewriter/wasm/out/wasm.js"; -import { config, flagEnabled } from ".."; +import { codecDecode, codecEncode, config, flagEnabled } from ".."; export type { JsRewriterOutput, Rewriter }; -import { URLMeta } from "./url"; +import { rewriteUrl, URLMeta } from "./url"; +import { htmlRules } from "../htmlRules"; +import { rewriteCss, unrewriteCss } from "./css"; +import { rewriteJs } from "./js"; let wasm_u8: Uint8Array; if (self.WASM) @@ -47,7 +50,22 @@ export function getRewriter(meta: URLMeta): [Rewriter, () => void] { if (flagEnabled("rewriterLogs", meta.base)) console.log(`creating new rewriter, ${len} rewriters made already`); - let rewriter = new Rewriter({}); + let rewriter = new Rewriter({ + config, + shared: { + rewrite: { + htmlRules, + rewriteUrl, + rewriteCss, + rewriteJs, + }, + }, + flagEnabled, + codec: { + encode: codecEncode, + decode: codecDecode, + }, + }); obj = { rewriter, inUse: false }; rewriters.push(obj); } else { diff --git a/src/shared/rewriters/worker.ts b/src/shared/rewriters/worker.ts index 31f01053..e0762932 100644 --- a/src/shared/rewriters/worker.ts +++ b/src/shared/rewriters/worker.ts @@ -20,7 +20,7 @@ export function rewriteWorkers( script("wasm"); script("all"); - str += `$scramjetLoadClient(${JSON.stringify(config)})`; + str += `$scramjetLoadClient(${JSON.stringify(config)});`; let rewritten = rewriteJs(js, url, meta, module); if (rewritten instanceof Uint8Array) { diff --git a/src/worker/error.ts b/src/worker/error.ts index 8a0a1637..75bfd803 100644 --- a/src/worker/error.ts +++ b/src/worker/error.ts @@ -1,5 +1,3 @@ -import { $scramjet } from "../entry"; - export function errorTemplate(trace: string, fetchedURL: string) { // turn script into a data URI so we don"t have to escape any HTML values const script = ` @@ -7,8 +5,8 @@ export function errorTemplate(trace: string, fetchedURL: string) { fetchedURL.textContent = ${JSON.stringify(fetchedURL)}; for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(location.hostname)}; reload.addEventListener("click", () => location.reload()); - version.textContent = ${JSON.stringify($scramjet.version.version)}; - build.textContent = ${JSON.stringify($scramjet.version.build)}; + version.textContent = ${JSON.stringify($scramjetVersion)}; + build.textContent = ${JSON.stringify($scramjetVersion)}; document.getElementById('copy-button').addEventListener('click', async () => { const text = document.getElementById('errorTrace').value; diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index faedb4c9..d8e27c6b 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -289,13 +289,13 @@ export async function handleFetch( message: err.message, url: request.url, destination: request.destination, - timestamp: new Date().toISOString(), }; if (err.stack) { errorDetails["stack"] = err.stack; } console.error("ERROR FROM SERVICE WORKER FETCH: ", errorDetails); + console.error(err); if (!["document", "iframe"].includes(request.destination)) return new Response(undefined, { status: 500 }); @@ -508,7 +508,7 @@ async function rewriteBody( if (js instanceof Uint8Array) { js = new TextDecoder().decode(js); } - const sourcemapfn = `${globalThis.$scramjet.config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");`; + const sourcemapfn = `${config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");`; const strictMode = /^\s*(['"])use strict\1;?/; if (strictMode.test(js)) { js = js.replace(strictMode, `$&\n${sourcemapfn}`); diff --git a/static/sw.js b/static/sw.js index 83ea084f..d3705508 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,3 +1,5 @@ +/// + // dumb hack to allow firefox to work (please dont do this in prod) if (navigator.userAgent.includes("Firefox")) { Object.defineProperty(globalThis, "crossOriginIsolated", { @@ -6,8 +8,8 @@ if (navigator.userAgent.includes("Firefox")) { }); } -importScripts("/scram/scramjet.shared.js", "/scram/scramjet.worker.js"); - +importScripts("/scram/scramjet.all.js"); +const { ScramjetServiceWorker } = $scramjetLoadWorker(); const scramjet = new ScramjetServiceWorker(); async function handleRequest(event) { diff --git a/static/ui.js b/static/ui.js index c0cd5c0e..3c6313ce 100644 --- a/static/ui.js +++ b/static/ui.js @@ -1,9 +1,9 @@ +const { ScramjetController } = $scramjetLoadController(); + const scramjet = new ScramjetController({ files: { wasm: "/scram/scramjet.wasm.wasm", - worker: "/scram/scramjet.worker.js", - client: "/scram/scramjet.client.js", - shared: "/scram/scramjet.shared.js", + all: "/scram/scramjet.all.js", sync: "/scram/scramjet.sync.js", }, flags: { @@ -283,7 +283,7 @@ function BrowserApp() { const cfg = h(Config); document.body.appendChild(cfg); - this.githubURL = `https://github.com/MercuryWorkshop/scramjet/commit/${$scramjet.version.build}`; + this.githubURL = `https://github.com/MercuryWorkshop/scramjet/commit/${$scramjetVersion.build}`; return html`
@@ -302,7 +302,7 @@ function BrowserApp() {

- scramjet ${$scramjet.version.version} ${$scramjet.version.build} + scramjet ${$scramjetVersion.version} ${$scramjetVersion.build}

${frame.frame} From f2cabf04da6ad2e61c8a3a984b6e8059bc176372 Mon Sep 17 00:00:00 2001 From: velzie Date: Wed, 16 Jul 2025 11:49:41 -0400 Subject: [PATCH 5/5] fix build --- src/controller/index.ts | 2 -- src/types.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/src/controller/index.ts b/src/controller/index.ts index 2c30d784..e0312b8b 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -158,5 +158,3 @@ export class ScramjetController { await this.#saveConfig(); } } - -window.ScramjetController = ScramjetController; diff --git a/src/types.ts b/src/types.ts index 79ae1def..52376cda 100644 --- a/src/types.ts +++ b/src/types.ts @@ -53,7 +53,6 @@ declare global { COOKIE: string; WASM: string; REAL_WASM: Uint8Array; - ScramjetController: typeof ScramjetController; // the scramjet client belonging to a window [SCRAMJETCLIENT]: ScramjetClient;