Skip to content

Commit ad73d45

Browse files
committed
fix unrewriting stolen functions
1 parent 3948171 commit ad73d45

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

src/client/client.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { BareClient as BareClientType } from "@mercuryworkshop/bare-mux";
1717
import { createWrapFn } from "./shared/wrap";
1818
import { NavigateEvent } from "./events";
1919
import type { URLMeta } from "../shared/rewriters/url";
20+
import { SourceMaps } from "./shared/sourcemaps";
2021

2122
type NativeStore = {
2223
store: Record<string, any>;
@@ -77,6 +78,7 @@ export class ScramjetClient {
7778

7879
natives: NativeStore;
7980
descriptors: DescriptorStore;
81+
sourcemaps: SourceMaps;
8082
wrapfn: (i: any, ...args: any) => any;
8183

8284
cookieStore = new CookieStore();
@@ -102,17 +104,6 @@ export class ScramjetClient {
102104
throw new Error();
103105
}
104106

105-
this.serviceWorker = this.global.navigator.serviceWorker;
106-
107-
if (iswindow) {
108-
this.documentProxy = createDocumentProxy(this, global);
109-
110-
global.document[SCRAMJETCLIENT] = this;
111-
}
112-
113-
this.locationProxy = createLocationProxy(this, global);
114-
this.globalProxy = createGlobalProxy(this, global);
115-
this.wrapfn = createWrapFn(this, global);
116107
if (iswindow) {
117108
this.bare = new BareClient();
118109
} else {
@@ -130,6 +121,19 @@ export class ScramjetClient {
130121
})
131122
);
132123
}
124+
125+
this.serviceWorker = this.global.navigator.serviceWorker;
126+
127+
if (iswindow) {
128+
this.documentProxy = createDocumentProxy(this, global);
129+
130+
global.document[SCRAMJETCLIENT] = this;
131+
}
132+
133+
this.locationProxy = createLocationProxy(this, global);
134+
this.globalProxy = createGlobalProxy(this, global);
135+
this.wrapfn = createWrapFn(this, global);
136+
this.sourcemaps = {};
133137
this.natives = {
134138
store: new Proxy(
135139
{},

src/client/shared/sourcemaps.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { flagEnabled } from "../../scramjet";
2+
import { SCRAMJETCLIENT, SCRAMJETCLIENTNAME } from "../../symbols";
23
import { ProxyCtx, ScramjetClient } from "../client";
34

45
enum RewriteType {
@@ -20,6 +21,8 @@ type Rewrite = {
2021
}
2122
);
2223

24+
export type SourceMaps = Record<string, Rewrite[]>;
25+
2326
function getEnd(rewrite: Rewrite): number {
2427
if (rewrite.type === RewriteType.Insert) {
2528
return rewrite.start + rewrite.size;
@@ -31,6 +34,30 @@ function getEnd(rewrite: Rewrite): number {
3134

3235
const scramtag_ident = "/*scramtag ";
3336

37+
// some sites like to steal funcs from frames and then unrewrite them
38+
function searchRewrites(tag: string): Rewrite[] | undefined {
39+
function searchFrame(globalThis: Self) {
40+
const SCRAMJETCLIENT = globalThis.Symbol.for(SCRAMJETCLIENTNAME);
41+
if (globalThis[SCRAMJETCLIENT].sourcemaps[tag])
42+
return globalThis[SCRAMJETCLIENT].sourcemaps[tag];
43+
44+
// no enhanced for :frowning2:
45+
for (let i = 0; i < globalThis.frames.length; i++) {
46+
const rewrites = searchFrame(globalThis.frames[i].self);
47+
if (rewrites) return rewrites;
48+
}
49+
}
50+
51+
let globalThis = self;
52+
let rewrites = searchFrame(globalThis);
53+
if (rewrites) return rewrites;
54+
while (globalThis.parent && globalThis.parent !== globalThis.window) {
55+
globalThis = globalThis.parent.self;
56+
let rewrites = searchFrame(globalThis);
57+
if (rewrites) return rewrites;
58+
}
59+
}
60+
3461
function registerRewrites(buf: Array<number>, tag: string) {
3562
const sourcemap = Uint8Array.from(buf);
3663
const view = new DataView(sourcemap.buffer);
@@ -65,7 +92,7 @@ function registerRewrites(buf: Array<number>, tag: string) {
6592
}
6693
}
6794

68-
sourcemaps[tag] = rewrites;
95+
self[SCRAMJETCLIENT].sourcemaps[tag] = rewrites;
6996
}
7097

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

97-
const rewrites = sourcemaps[tag];
124+
const rewrites = searchRewrites(tag);
98125

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

140-
const sourcemaps: Record<string, Rewrite[]> = {};
141-
142167
export const enabled = (client: ScramjetClient) =>
143168
flagEnabled("sourcemaps", client.url);
144169

src/shared/rewriters/js.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ function rewriteJsWasm(
5757
} catch (err) {
5858
const err1 = err as Error;
5959
console.warn("failed rewriting js for", source, err1, input);
60-
err1.message = `failed rewriting js for "${source}": ${err1.message}`;
6160

6261
return { js: input, tag: "", map: null };
6362
}

src/symbols.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// see types.d.ts for what these mean
2-
export const SCRAMJETCLIENT = Symbol.for("scramjet client global");
2+
export const SCRAMJETCLIENTNAME = "scramjet client global";
3+
export const SCRAMJETCLIENT = Symbol.for(SCRAMJETCLIENTNAME);
34
export const SCRAMJETFRAME = Symbol.for("scramjet frame handle");

0 commit comments

Comments
 (0)