Skip to content

Commit

Permalink
feat: add helper functions to natives store + refac
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol authored Dec 10, 2024
1 parent 70f0315 commit ef8735f
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 117 deletions.
115 changes: 78 additions & 37 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -120,48 +130,79 @@ export class ScramjetClient {
})
);
}
this.natives = new Proxy(
{},
{
get: (target, prop: string) => {
if (prop in target) {
return target[prop];
}
this.natives = {
store: new Proxy(
{},
{
get: (target, prop: string) => {
if (prop in target) {
return target[prop];
}

const split = prop.split(".");
const realProp = split.pop();
const realTarget = split.reduce((a, b) => a?.[b], this.global);
const split = prop.split(".");
const realProp = split.pop();
const realTarget = split.reduce((a, b) => a?.[b], this.global);

if (!realTarget) return;
if (!realTarget) return;

const original = Reflect.get(realTarget, realProp);
target[prop] = original;
const original = Reflect.get(realTarget, realProp);
target[prop] = original;

return target[prop];
},
}
);
this.descriptors = new Proxy(
{},
{
get: (target, prop: string) => {
if (prop in target) {
return target[prop];
}
},
}
),
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;

const split = prop.split(".");
const realProp = split.pop();
const realTarget = split.reduce((a, b) => a?.[b], this.global);
return original.call(that, ...args);
},
};
this.descriptors = {
store: new Proxy(
{},
{
get: (target, prop: string) => {
if (prop in target) {
return target[prop];
}

if (!realTarget) return;
const split = prop.split(".");
const realProp = split.pop();
const realTarget = split.reduce((a, b) => a?.[b], this.global);

const original = nativeGetOwnPropertyDescriptor(realTarget, realProp);
target[prop] = original;
if (!realTarget) return;

return target[prop];
},
}
);
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 = {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
34 changes: 20 additions & 14 deletions src/client/dom/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
Expand Down Expand Up @@ -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", {
Expand All @@ -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
Expand All @@ -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]
)
Expand All @@ -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]
)
Expand All @@ -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))
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/client/dom/serviceworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/client/shared/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/client/shared/requests/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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],
Expand Down
Loading

0 comments on commit ef8735f

Please sign in to comment.