diff --git a/src/client/client.ts b/src/client/client.ts index 292a1c6..01f8720 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -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; @@ -77,6 +78,7 @@ export class ScramjetClient { natives: NativeStore; descriptors: DescriptorStore; + sourcemaps: SourceMaps; wrapfn: (i: any, ...args: any) => any; cookieStore = new CookieStore(); @@ -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 { @@ -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( {}, diff --git a/src/client/shared/sourcemaps.ts b/src/client/shared/sourcemaps.ts index 77fd824..8c0598d 100644 --- a/src/client/shared/sourcemaps.ts +++ b/src/client/shared/sourcemaps.ts @@ -1,4 +1,5 @@ import { flagEnabled } from "../../scramjet"; +import { SCRAMJETCLIENT, SCRAMJETCLIENTNAME } from "../../symbols"; import { ProxyCtx, ScramjetClient } from "../client"; enum RewriteType { @@ -20,6 +21,8 @@ type Rewrite = { } ); +export type SourceMaps = Record; + function getEnd(rewrite: Rewrite): number { if (rewrite.type === RewriteType.Insert) { return rewrite.start + rewrite.size; @@ -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, tag: string) { const sourcemap = Uint8Array.from(buf); const view = new DataView(sourcemap.buffer); @@ -65,7 +92,7 @@ function registerRewrites(buf: Array, tag: string) { } } - sourcemaps[tag] = rewrites; + self[SCRAMJETCLIENT].sourcemaps[tag] = rewrites; } function doUnrewrite(ctx: ProxyCtx) { @@ -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); @@ -137,8 +164,6 @@ function doUnrewrite(ctx: ProxyCtx) { return ctx.return(newString); } -const sourcemaps: Record = {}; - export const enabled = (client: ScramjetClient) => flagEnabled("sourcemaps", client.url); diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index a78ba21..302e669 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -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 }; } diff --git a/src/symbols.ts b/src/symbols.ts index 5505d8b..a7b6a99 100644 --- a/src/symbols.ts +++ b/src/symbols.ts @@ -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");