Skip to content

Commit

Permalink
fix unrewriting stolen functions
Browse files Browse the repository at this point in the history
  • Loading branch information
r58Playz committed Mar 9, 2025
1 parent 3948171 commit ad73d45
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
26 changes: 15 additions & 11 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 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 { SourceMaps } from "./shared/sourcemaps";

type NativeStore = {
store: Record<string, any>;
Expand Down Expand Up @@ -77,6 +78,7 @@ export class ScramjetClient {

natives: NativeStore;
descriptors: DescriptorStore;
sourcemaps: SourceMaps;
wrapfn: (i: any, ...args: any) => any;

cookieStore = new CookieStore();
Expand All @@ -102,17 +104,6 @@ export class ScramjetClient {
throw new Error();
}

this.serviceWorker = this.global.navigator.serviceWorker;

if (iswindow) {
this.documentProxy = createDocumentProxy(this, global);

global.document[SCRAMJETCLIENT] = this;
}

this.locationProxy = createLocationProxy(this, global);
this.globalProxy = createGlobalProxy(this, global);
this.wrapfn = createWrapFn(this, global);
if (iswindow) {
this.bare = new BareClient();
} else {
Expand All @@ -130,6 +121,19 @@ export class ScramjetClient {
})
);
}

this.serviceWorker = this.global.navigator.serviceWorker;

if (iswindow) {
this.documentProxy = createDocumentProxy(this, global);

global.document[SCRAMJETCLIENT] = this;
}

this.locationProxy = createLocationProxy(this, global);
this.globalProxy = createGlobalProxy(this, global);
this.wrapfn = createWrapFn(this, global);
this.sourcemaps = {};
this.natives = {
store: new Proxy(
{},
Expand Down
33 changes: 29 additions & 4 deletions src/client/shared/sourcemaps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { flagEnabled } from "../../scramjet";
import { SCRAMJETCLIENT, SCRAMJETCLIENTNAME } from "../../symbols";
import { ProxyCtx, ScramjetClient } from "../client";

enum RewriteType {
Expand All @@ -20,6 +21,8 @@ type Rewrite = {
}
);

export type SourceMaps = Record<string, Rewrite[]>;

function getEnd(rewrite: Rewrite): number {
if (rewrite.type === RewriteType.Insert) {
return rewrite.start + rewrite.size;
Expand All @@ -31,6 +34,30 @@ function getEnd(rewrite: Rewrite): number {

const scramtag_ident = "/*scramtag ";

// some sites like to steal funcs from frames and then unrewrite them
function searchRewrites(tag: string): Rewrite[] | undefined {
function searchFrame(globalThis: Self) {
const SCRAMJETCLIENT = globalThis.Symbol.for(SCRAMJETCLIENTNAME);
if (globalThis[SCRAMJETCLIENT].sourcemaps[tag])
return globalThis[SCRAMJETCLIENT].sourcemaps[tag];

// no enhanced for :frowning2:
for (let i = 0; i < globalThis.frames.length; i++) {
const rewrites = searchFrame(globalThis.frames[i].self);
if (rewrites) return rewrites;
}
}

let globalThis = self;
let rewrites = searchFrame(globalThis);
if (rewrites) return rewrites;
while (globalThis.parent && globalThis.parent !== globalThis.window) {
globalThis = globalThis.parent.self;
let rewrites = searchFrame(globalThis);
if (rewrites) return rewrites;
}
}

function registerRewrites(buf: Array<number>, tag: string) {
const sourcemap = Uint8Array.from(buf);
const view = new DataView(sourcemap.buffer);
Expand Down Expand Up @@ -65,7 +92,7 @@ function registerRewrites(buf: Array<number>, tag: string) {
}
}

sourcemaps[tag] = rewrites;
self[SCRAMJETCLIENT].sourcemaps[tag] = rewrites;
}

function doUnrewrite(ctx: ProxyCtx) {
Expand Down Expand Up @@ -94,7 +121,7 @@ function doUnrewrite(ctx: ProxyCtx) {
const scramtagend = stringified.indexOf("*/", scramtagstart);
const tag = stringified.substring(firstspace + 1, scramtagend);

const rewrites = sourcemaps[tag];
const rewrites = searchRewrites(tag);

if (!rewrites) {
console.warn("failed to get rewrites for tag", tag);
Expand Down Expand Up @@ -137,8 +164,6 @@ function doUnrewrite(ctx: ProxyCtx) {
return ctx.return(newString);
}

const sourcemaps: Record<string, Rewrite[]> = {};

export const enabled = (client: ScramjetClient) =>
flagEnabled("sourcemaps", client.url);

Expand Down
1 change: 0 additions & 1 deletion src/shared/rewriters/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function rewriteJsWasm(
} catch (err) {
const err1 = err as Error;
console.warn("failed rewriting js for", source, err1, input);
err1.message = `failed rewriting js for "${source}": ${err1.message}`;

return { js: input, tag: "", map: null };
}
Expand Down
3 changes: 2 additions & 1 deletion src/symbols.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// see types.d.ts for what these mean
export const SCRAMJETCLIENT = Symbol.for("scramjet client global");
export const SCRAMJETCLIENTNAME = "scramjet client global";
export const SCRAMJETCLIENT = Symbol.for(SCRAMJETCLIENTNAME);
export const SCRAMJETFRAME = Symbol.for("scramjet frame handle");

0 comments on commit ad73d45

Please sign in to comment.