This commit is contained in:
velzie 2024-08-31 11:52:00 -04:00
parent 08ccb4f56e
commit 7adf76d37f
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
2 changed files with 48 additions and 37 deletions

View file

@ -2,15 +2,17 @@
import { ScramjetClient } from "./client";
import { nativeGetOwnPropertyDescriptor } from "./natives";
import { encodeUrl, decodeUrl } from "../shared";
import { iswindow } from ".";
export function createLocationProxy(
client: ScramjetClient,
self: typeof globalThis
) {
const Location = iswindow ? self.Location : self.WorkerLocation;
// location cannot be Proxy()d
const fakeLocation = {};
Object.setPrototypeOf(fakeLocation, self.Location.prototype);
fakeLocation.constructor = self.Location;
Object.setPrototypeOf(fakeLocation, Location.prototype);
fakeLocation.constructor = Location;
const urlprops = [
"protocol",
@ -25,15 +27,19 @@ export function createLocationProxy(
];
for (const prop of urlprops) {
const native = nativeGetOwnPropertyDescriptor(self.location, prop);
if (!native) continue;
const desc = {
configurable: true,
enumerable: true,
get: new Proxy(native.get, {
};
if (native.get) {
desc.get = new Proxy(native.get, {
apply() {
return client.url[prop];
},
}),
};
});
}
if (native.set) {
desc.set = new Proxy(native.set, {
apply(target, thisArg, args) {
@ -52,22 +58,27 @@ export function createLocationProxy(
return client.url.href;
},
});
if (self.location.valueOf)
fakeLocation.valueOf = new Proxy(self.location.valueOf, {
apply() {
return client.url.href;
},
});
if (self.location.assign)
fakeLocation.assign = new Proxy(self.location.assign, {
apply(target, thisArg, args) {
args[0] = encodeUrl(args[0]);
Reflect.apply(target, thisArg, args);
},
});
if (self.location.reload)
fakeLocation.reload = new Proxy(self.location.reload, {
apply(target, thisArg, args) {
Reflect.apply(target, thisArg, args);
},
});
if (self.location.replace)
fakeLocation.replace = new Proxy(self.location.replace, {
apply(target, thisArg, args) {
args[0] = encodeUrl(args[0]);

View file

@ -103,14 +103,14 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
return t;
};
client.Proxy("Promise.prototype.catch", {
apply(ctx) {
ctx.args[0] = new Proxy(ctx.args[0], {
apply(target, thisArg, argArray) {
// console.warn("CAUGHT PROMISE REJECTION", argArray);
Reflect.apply(target, thisArg, argArray);
},
});
},
});
// client.Proxy("Promise.prototype.catch", {
// apply(ctx) {
// ctx.args[0] = new Proxy(ctx.args[0], {
// apply(target, thisArg, argArray) {
// // console.warn("CAUGHT PROMISE REJECTION", argArray);
// Reflect.apply(target, thisArg, argArray);
// },
// });
// },
// });
}