Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions INTERCEPTION_PROXY_BIBLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# The Interception Proxy Bible
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>

<script src="/scram/scramjet.controller.js"></script>
<script src="/scram/scramjet.all.js"></script>
</head>

<body>
Expand Down
10 changes: 8 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
295 changes: 159 additions & 136 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
19 changes: 10 additions & 9 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
Expand Down Expand Up @@ -365,15 +367,14 @@ export class ScramjetClient {
}

hook() {
// @ts-ignore
const context = import.meta.webpackContext(".", {
recursive: true,
});

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) ||
Expand Down
2 changes: 1 addition & 1 deletion src/client/document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rewriteUrl } from "../shared";
import { rewriteUrl } from "../shared/rewriters/url";
import { ScramjetClient } from "./client";
import { getOwnPropertyDescriptorHandler } from "./helpers";

Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/beacon.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/css.ts
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/document.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions src/client/dom/element.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/fontface.ts
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/history.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/open.ts
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/performance.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/dom/protocol.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/client/dom/serviceworker.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
60 changes: 30 additions & 30 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// entrypoint for scramjet.client.js

import { loadCodecs } from "../scramjet";
import { loadCodecs, setConfig } from "../shared/index";
import { SCRAMJETCLIENT } from "../symbols";
import { ScramjetClient } from "./client";
import { ScramjetContextEvent, UrlChangeEvent } from "./events";
Expand All @@ -21,35 +21,35 @@ 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 <Partial<typeof self>>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(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 <Partial<typeof self>>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");
}
7 changes: 3 additions & 4 deletions src/client/location.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// @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,
self: typeof globalThis
) {
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;

Expand All @@ -32,7 +31,7 @@ export function createLocationProxy(
const native = nativeGetOwnPropertyDescriptor(descriptorSource, prop);
if (!native) continue;

const desc = {
const desc: Partial<PropertyDescriptor> = {
configurable: true,
enumerable: true,
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/shared/blob.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/client/shared/caches.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/shared/err.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flagEnabled } from "../../scramjet";
import { flagEnabled } from "../../shared";
import { ScramjetClient } from "../client";

export const enabled = (client: ScramjetClient) =>
Expand Down
6 changes: 3 additions & 3 deletions src/client/shared/error.ts
Original file line number Diff line number Diff line change
@@ -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) =>
Expand All @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion src/client/shared/eval.ts
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
2 changes: 1 addition & 1 deletion src/client/shared/event.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/client/shared/function.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/client/shared/requests/eventsource.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/client/shared/requests/fetch.ts
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down
4 changes: 2 additions & 2 deletions src/client/shared/requests/xmlhttprequest.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Loading
Loading