feat: add helper functions to natives store + refac

This commit is contained in:
Percs 2024-12-10 20:45:51 +00:00 committed by GitHub
parent 70f0315791
commit ef8735f95a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 164 additions and 121 deletions

View file

@ -18,6 +18,16 @@ import { createWrapFn } from "./shared/wrap";
import { NavigateEvent } from "./events";
import type { URLMeta } from "../shared/rewriters/url";
type NativeStore = {
store: Record<string, any>;
call: (target: string, that: any, ...args) => any;
construct: (target: string, ...args) => any;
};
type DescriptorStore = {
store: Record<string, PropertyDescriptor>;
get: (target: string, that: any) => any;
set: (target: string, that: any, value: any) => void;
};
//eslint-disable-next-line
export type AnyFunction = Function;
@ -65,8 +75,8 @@ export class ScramjetClient {
serviceWorker: ServiceWorkerContainer;
bare: BareClientType;
descriptors: Record<string, PropertyDescriptor> = {};
natives: Record<string, any>;
natives: NativeStore;
descriptors: DescriptorStore;
wrapfn: (i: any, ...args: any) => any;
cookieStore = new CookieStore();
@ -120,7 +130,8 @@ export class ScramjetClient {
})
);
}
this.natives = new Proxy(
this.natives = {
store: new Proxy(
{},
{
get: (target, prop: string) => {
@ -140,8 +151,22 @@ export class ScramjetClient {
return target[prop];
},
}
);
this.descriptors = new Proxy(
),
construct(target: string, ...args) {
const original = this.store[target];
if (!original) return;
return new original(...args);
},
call(target: string, that: any, ...args) {
const original = this.store[target];
if (!original) return;
return original.call(that, ...args);
},
};
this.descriptors = {
store: new Proxy(
{},
{
get: (target, prop: string) => {
@ -155,13 +180,29 @@ export class ScramjetClient {
if (!realTarget) return;
const original = nativeGetOwnPropertyDescriptor(realTarget, realProp);
const original = nativeGetOwnPropertyDescriptor(
realTarget,
realProp
);
target[prop] = original;
return target[prop];
},
}
);
),
get(target: string, that: any) {
const original = this.store[target];
if (!original) return;
return original.get.call(that);
},
set(target: string, that: any, value: any) {
const original = this.store[target];
if (!original) return;
original.set.call(that, value);
},
};
// eslint-disable-next-line @typescript-eslint/no-this-alias
const client = this;
this.meta = {
@ -285,7 +326,7 @@ export class ScramjetClient {
if (!target) return;
const original = Reflect.get(target, prop);
this.natives[name] = original;
this.natives.store[name] = original;
this.RawProxy(target, prop, handler);
}
@ -412,7 +453,7 @@ export class ScramjetClient {
if (!target) return;
const original = nativeGetOwnPropertyDescriptor(target, prop);
this.descriptors[name] = original;
this.descriptors.store[name] = original;
return this.RawTrap(target, prop, descriptor);
}

View file

@ -127,7 +127,8 @@ export default function (client: ScramjetClient, self: typeof window) {
}
if (
client.natives["Element.prototype.hasAttribute"].call(
client.natives.call(
"Element.prototype.hasAttribute",
ctx.this,
`scramjet-attr-${name}`
)
@ -185,7 +186,7 @@ export default function (client: ScramjetClient, self: typeof window) {
// i actually need to do something with this
client.Proxy("Element.prototype.setAttributeNode", {
apply(ctx) {},
apply(_ctx) {},
});
client.Proxy("Element.prototype.setAttributeNS", {
@ -203,7 +204,8 @@ export default function (client: ScramjetClient, self: typeof window) {
if (ruleList) {
ctx.args[2] = ruleList.fn(value, client.meta, client.cookieStore);
client.natives["Element.prototype.setAttribute"].call(
client.natives.call(
"Element.prototype.setAttribute",
ctx.this,
`scramjet-attr-${ctx.args[1]}`,
value
@ -216,7 +218,8 @@ export default function (client: ScramjetClient, self: typeof window) {
apply(ctx) {
if (ctx.args[0].startsWith("scramjet-attr")) return ctx.return(undefined);
if (
client.natives["Element.prototype.hasAttribute"].call(
client.natives.call(
"Element.prototype.hasAttribute",
ctx.this,
ctx.args[0]
)
@ -230,7 +233,8 @@ export default function (client: ScramjetClient, self: typeof window) {
apply(ctx) {
if (ctx.args[0].startsWith("scramjet-attr")) return ctx.return(false);
if (
client.natives["Element.prototype.hasAttribute"].call(
client.natives.call(
"Element.prototype.hasAttribute",
ctx.this,
ctx.args[0]
)
@ -245,7 +249,8 @@ export default function (client: ScramjetClient, self: typeof window) {
let newval;
if (ctx.this instanceof self.HTMLScriptElement) {
newval = rewriteJs(value, "(anonymous script element)", client.meta);
client.natives["Element.prototype.setAttribute"].call(
client.natives.call(
"Element.prototype.setAttribute",
ctx.this,
"scramjet-attr-script-source-src",
bytesToBase64(encoder.encode(newval))
@ -264,9 +269,11 @@ export default function (client: ScramjetClient, self: typeof window) {
},
get(ctx) {
if (ctx.this instanceof self.HTMLScriptElement) {
const scriptSource = client.natives[
"Element.prototype.getAttribute"
].call(ctx.this, "scramjet-attr-script-source-src");
const scriptSource = client.natives.call(
"Element.prototype.getAttribute",
ctx.this,
"scramjet-attr-script-source-src"
);
if (scriptSource) {
return atob(scriptSource);
@ -358,11 +365,10 @@ export default function (client: ScramjetClient, self: typeof window) {
],
{
get(ctx) {
const contentwindow =
client.descriptors[
`${ctx.this.constructor.name}.prototype.contentWindow`
].get;
const realwin = contentwindow.apply(ctx.this);
const realwin = client.descriptors.get(
`${ctx.this.constructor.name}.prototype.contentWindow`,
ctx.this
);
if (!realwin) return realwin;
if (SCRAMJETCLIENT in realwin) {

View file

@ -67,8 +67,7 @@ export default function (client: ScramjetClient, _self: Self) {
url += "&type=module";
}
const nativeSharedWorker = client.natives["SharedWorker"];
const worker = new nativeSharedWorker(url);
const worker = client.natives.construct("SharedWorker", url);
const handle = worker.port;

View file

@ -3,7 +3,7 @@ import { config } from "../../shared";
import { rewriteUrl } from "../../shared/rewriters/url";
export default function (client: ScramjetClient, self: Self) {
const Function = client.natives["Function"];
const Function = client.natives.store["Function"];
self[config.globals.importfn] = function (base: string) {
return function (url: string) {

View file

@ -5,7 +5,7 @@ import { ScramjetClient } from "../../client";
export default function (client: ScramjetClient, self: Self) {
let worker;
if (self.Worker && flagEnabled("syncxhr", client.url)) {
worker = new client.natives["Worker"](config.files.sync);
worker = client.natives.construct("Worker", config.files.sync);
}
const ARGS = Symbol("xhr original args");
const HEADERS = Symbol("xhr headers");
@ -44,7 +44,7 @@ export default function (client: ScramjetClient, self: Self) {
const sab = new SharedArrayBuffer(1024, { maxByteLength: 2147483647 });
const view = new DataView(sab);
client.natives["Worker.prototype.postMessage"].call(worker, {
client.natives.call("Worker.prototype.postMessage", worker, {
sab,
args,
headers: ctx.this[HEADERS],

View file

@ -1,10 +1,8 @@
import { iswindow } from "..";
import { BareMuxConnection } from "../../shared";
import { rewriteUrl } from "../../shared";
import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, self: typeof globalThis) {
if (self.Worker) {
export default function (client: ScramjetClient, _self: typeof globalThis) {
client.Proxy("Worker", {
construct({ args, call }) {
args[0] = rewriteUrl(args[0], client.meta) + "?dest=worker";
@ -18,7 +16,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
(async () => {
const port = await conn.getInnerPort();
client.natives["Worker.prototype.postMessage"].call(
client.natives.call(
"Worker.prototype.postMessage",
worker,
{
$scramjet$type: "baremuxinit",
@ -29,14 +28,6 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
})();
},
});
}
if (iswindow) {
client.Proxy("Worklet.prototype.addModule", {
apply(ctx) {
if (ctx.args[0]) ctx.args[0] = rewriteUrl(ctx.args[0], client.meta);
},
});
// sharedworkers can only be constructed from window
client.Proxy("SharedWorker", {
@ -61,7 +52,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
(async () => {
const port = await conn.getInnerPort();
client.natives["MessagePort.prototype.postMessage"].call(
client.natives.call(
"MessagePort.prototype.postMessage",
worker.port,
{
$scramjet$type: "baremuxinit",
@ -72,5 +64,10 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
})();
},
});
}
client.Proxy("Worklet.prototype.addModule", {
apply(ctx) {
if (ctx.args[0]) ctx.args[0] = rewriteUrl(ctx.args[0], client.meta);
},
});
}