kill snapshot.ts (untested)

This commit is contained in:
velzie 2024-08-25 14:55:20 -04:00
parent 9a39435c7d
commit 7f398cda17
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
7 changed files with 33 additions and 18 deletions

View file

@ -47,6 +47,9 @@ export class ScramjetClient {
locationProxy: any;
serviceWorker: ServiceWorkerContainer;
nativeDescriptors: Record<string, PropertyDescriptor> = {};
natives: Record<string, any> = {};
cookieStore = new CookieStore();
eventcallbacks: Map<
@ -76,7 +79,7 @@ export class ScramjetClient {
}
hook() {
this.serviceWorker = navigator.serviceWorker;
this.serviceWorker = this.global.navigator.serviceWorker;
// @ts-ignore
const context = import.meta.webpackContext(".", {
recursive: true,
@ -120,6 +123,9 @@ export class ScramjetClient {
const split = name.split(".");
const prop = split.pop();
const target = split.reduce((a, b) => a?.[b], this.global);
const original = Reflect.get(target, prop);
this.natives[prop] = original;
this.RawProxy(target, prop, handler);
}
RawProxy(target: any, prop: string, handler: Proxy) {
@ -186,7 +192,7 @@ export class ScramjetClient {
target[prop] = new Proxy(value, h);
}
Trap<T>(name: string | string[], descriptor: Trap<T>) {
Trap<T>(name: string | string[], descriptor: Trap<T>): PropertyDescriptor {
if (Array.isArray(name)) {
for (const n of name) {
this.Trap(n, descriptor);
@ -199,9 +205,16 @@ export class ScramjetClient {
const prop = split.pop();
const target = split.reduce((a, b) => a?.[b], this.global);
this.RawTrap(target, prop, descriptor);
const original = Object.getOwnPropertyDescriptor(target, prop);
this.nativeDescriptors[name] = original;
return this.RawTrap(target, prop, descriptor);
}
RawTrap<T>(target: any, prop: string, descriptor: Trap<T>) {
RawTrap<T>(
target: any,
prop: string,
descriptor: Trap<T>
): PropertyDescriptor {
if (!target) return;
if (!prop) return;
if (!Reflect.has(target, prop)) return;
@ -250,6 +263,8 @@ export class ScramjetClient {
desc.configurable = oldDescriptor.configurable;
Object.defineProperty(target, prop, desc);
return oldDescriptor;
}
get url(): URL {

View file

@ -4,7 +4,7 @@ import { rewriteJs } from "../shared";
function rewriteFunction(ctx: ProxyCtx) {
const stringifiedFunction = ctx.fn(...ctx.args).toString();
ctx.return(Function(`return ${rewriteJs(stringifiedFunction)}`)());
ctx.return(ctx.fn(`return ${rewriteJs(stringifiedFunction)}`)());
}
export default function (client: ScramjetClient, self: Self) {

View file

@ -1,6 +1,9 @@
import { ScramjetClient } from "../client";
import { config, encodeUrl } from "../shared";
export default function (client, self) {
export default function (client: ScramjetClient, self: Self) {
const Function = client.natives.Function;
self[config.importfn] = function (base: string) {
return function (url: string) {
const resolved = new URL(url, base).href;

View file

@ -8,7 +8,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
if (args[0].startsWith("blob:") || args[0].startsWith("data:")) {
if (args[0].startsWith("blob:")) {
args[0] =
"data:application/javascript;base64," + btoa(syncfetch(args[0]));
"data:application/javascript;base64," +
btoa(syncfetch(client, args[0]));
}
args[0] = "/scramjet/worker?data=" + args[0];
@ -30,9 +31,12 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
});
}
function syncfetch(url: string) {
function syncfetch(client: ScramjetClient, url: string) {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
const realOpen = client.natives["XMLHttpRequest.prototype.open"].bind(xhr);
realOpen("GET", url, false);
xhr.send();
return xhr.responseText;

View file

@ -1,4 +1,3 @@
import { Request } from "../snapshot";
import { ScramjetClient } from "./client";
import { decodeUrl, encodeUrl } from "./shared";
@ -71,6 +70,8 @@ function handleMessage(
if (handler.event !== "fetch") continue;
const request = data.scramjet$request;
const Request = client.natives["Request"];
const fakeRequest = new Request(decodeUrl(request.url), {
body: request.body,
headers: new Headers(request.headers),

View file

@ -25,7 +25,6 @@ export class CookieStore {
sameSite,
...parsed[0],
};
dbg.log("cookie", cookie);
if (!cookie.domain) cookie.domain = "." + url.hostname;
if (!cookie.domain.startsWith(".")) cookie.domain = "." + cookie.domain;

View file

@ -1,7 +0,0 @@
// safe version of the globals we overwrite
export const Function = self.Function;
export const URL = self.URL;
export const fetch = self.fetch;
export const Request = self.Request;
export const XMLHttpRequest = self.XMLHttpRequest;