mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
fix unrewriting stolen functions
This commit is contained in:
parent
3948171dc6
commit
ad73d45b45
4 changed files with 46 additions and 17 deletions
|
@ -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>;
|
||||
|
@ -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(
|
||||
{},
|
||||
|
|
|
@ -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<string, Rewrite[]>;
|
||||
|
||||
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<number>, tag: string) {
|
||||
const sourcemap = Uint8Array.from(buf);
|
||||
const view = new DataView(sourcemap.buffer);
|
||||
|
@ -65,7 +92,7 @@ function registerRewrites(buf: Array<number>, 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<string, Rewrite[]> = {};
|
||||
|
||||
export const enabled = (client: ScramjetClient) =>
|
||||
flagEnabled("sourcemaps", client.url);
|
||||
|
||||
|
|
|
@ -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 };
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue