mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
refactor globalProxy
This commit is contained in:
parent
add57d720b
commit
5e4f498dfe
2 changed files with 18 additions and 20 deletions
|
@ -4,34 +4,28 @@ import { indirectEval } from "./shared/eval";
|
||||||
// import { config } from "../shared";
|
// import { config } from "../shared";
|
||||||
import { getOwnPropertyDescriptorHandler } from "./helpers";
|
import { getOwnPropertyDescriptorHandler } from "./helpers";
|
||||||
|
|
||||||
|
export const UNSAFE_GLOBALS = [
|
||||||
|
"window",
|
||||||
|
"top",
|
||||||
|
"self",
|
||||||
|
"globalThis",
|
||||||
|
"parent",
|
||||||
|
"document",
|
||||||
|
"frames",
|
||||||
|
"eval",
|
||||||
|
];
|
||||||
|
|
||||||
export function createGlobalProxy(
|
export function createGlobalProxy(
|
||||||
client: ScramjetClient,
|
client: ScramjetClient,
|
||||||
self: typeof globalThis
|
self: typeof globalThis
|
||||||
): typeof globalThis {
|
): typeof globalThis {
|
||||||
return new Proxy(self, {
|
return new Proxy(self, {
|
||||||
get(target, prop) {
|
get(target, prop) {
|
||||||
if (prop === "location") return client.locationProxy;
|
|
||||||
|
|
||||||
if (
|
|
||||||
typeof prop === "string" &&
|
|
||||||
[
|
|
||||||
"window",
|
|
||||||
"top",
|
|
||||||
"self",
|
|
||||||
"globalThis",
|
|
||||||
"parent",
|
|
||||||
"document",
|
|
||||||
"frames",
|
|
||||||
].includes(prop)
|
|
||||||
)
|
|
||||||
return client.wrapfn(self[prop]);
|
|
||||||
|
|
||||||
if (prop === "$scramjet") return;
|
|
||||||
|
|
||||||
if (prop === "eval") return indirectEval.bind(client);
|
|
||||||
|
|
||||||
const value = Reflect.get(target, prop);
|
const value = Reflect.get(target, prop);
|
||||||
|
|
||||||
|
if (typeof prop === "string" && UNSAFE_GLOBALS.includes(prop))
|
||||||
|
return client.wrapfn(value);
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { SCRAMJETCLIENT } from "../../symbols";
|
||||||
import { ScramjetClient } from "../client";
|
import { ScramjetClient } from "../client";
|
||||||
import { config } from "../../shared";
|
import { config } from "../../shared";
|
||||||
import { argdbg } from "./err";
|
import { argdbg } from "./err";
|
||||||
|
import { indirectEval } from "./eval";
|
||||||
|
|
||||||
export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
|
export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
|
||||||
return function (identifier: any, args: any) {
|
return function (identifier: any, args: any) {
|
||||||
|
@ -46,6 +47,9 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
|
||||||
} else if (isworker && identifier instanceof self.WorkerGlobalScope) {
|
} else if (isworker && identifier instanceof self.WorkerGlobalScope) {
|
||||||
return client.globalProxy;
|
return client.globalProxy;
|
||||||
}
|
}
|
||||||
|
if (identifier == self.eval) {
|
||||||
|
return indirectEval.bind(client);
|
||||||
|
}
|
||||||
|
|
||||||
return identifier;
|
return identifier;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue