rewrite location proxy (regressions!)

This commit is contained in:
velzie 2024-08-30 18:26:59 -04:00
parent 2bc31d7e56
commit 7f8956979a
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
5 changed files with 132 additions and 95 deletions

View file

@ -1,44 +1,78 @@
// @ts-nocheck
import { ScramjetClient } from "./client";
import { nativeGetOwnPropertyDescriptor } from "./natives";
import { encodeUrl, decodeUrl } from "./shared";
export function createLocationProxy(
client: ScramjetClient,
self: typeof globalThis
) {
function createLocation() {
const loc = new URL(client.url.href);
// location cannot be Proxy()d
const fakeLocation = {};
Object.setPrototypeOf(fakeLocation, self.Location.prototype);
fakeLocation.constructor = self.Location;
loc.assign = (url: string) => self.location.assign(encodeUrl(url));
loc.reload = () => self.location.reload();
loc.replace = (url: string) => self.location.replace(encodeUrl(url));
loc.toString = () => loc.href;
return loc;
const urlprops = [
"hash",
"host",
"hostname",
"href",
"origin",
"pathname",
"port",
"search",
];
for (const prop of urlprops) {
const native = nativeGetOwnPropertyDescriptor(self.location, prop);
const desc = {
configurable: true,
enumerable: true,
get: new Proxy(native.get, {
apply() {
return client.url[prop];
},
}),
};
if (native.set) {
desc.set = new Proxy(native.set, {
apply(target, thisArg, args) {
let url = new URL(client.url.href);
url[prop] = args[0];
self.location.href = encodeUrl(url.href);
},
});
}
Object.defineProperty(fakeLocation, prop, desc);
}
return new Proxy(
{
host: "",
// functions
fakeLocation.toString = new Proxy(self.location.toString, {
apply() {
return client.url.href;
},
{
get(target, prop) {
const loc = createLocation();
});
fakeLocation.valueOf = new Proxy(self.location.valueOf, {
apply() {
return client.url.href;
},
});
fakeLocation.assign = new Proxy(self.location.assign, {
apply(target, thisArg, args) {
args[0] = encodeUrl(args[0]);
Reflect.apply(target, thisArg, args);
},
});
fakeLocation.reload = new Proxy(self.location.reload, {
apply(target, thisArg, args) {
Reflect.apply(target, thisArg, args);
},
});
fakeLocation.replace = new Proxy(self.location.replace, {
apply(target, thisArg, args) {
args[0] = encodeUrl(args[0]);
Reflect.apply(target, thisArg, args);
},
});
return loc[prop];
},
set(obj, prop, value) {
const loc = createLocation();
if (prop === "href") {
self.location.href = encodeUrl(value);
} else {
loc[prop] = value;
}
return true;
},
}
);
return fakeLocation;
}