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

View file

@ -4,7 +4,7 @@ import { rewriteJs } from "../shared";
function rewriteFunction(ctx: ProxyCtx) { function rewriteFunction(ctx: ProxyCtx) {
const stringifiedFunction = ctx.fn(...ctx.args).toString(); 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) { export default function (client: ScramjetClient, self: Self) {

View file

@ -1,6 +1,9 @@
import { ScramjetClient } from "../client";
import { config, encodeUrl } from "../shared"; 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) { self[config.importfn] = function (base: string) {
return function (url: string) { return function (url: string) {
const resolved = new URL(url, base).href; 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].startsWith("data:")) {
if (args[0].startsWith("blob:")) { if (args[0].startsWith("blob:")) {
args[0] = 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]; 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(); const xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
const realOpen = client.natives["XMLHttpRequest.prototype.open"].bind(xhr);
realOpen("GET", url, false);
xhr.send(); xhr.send();
return xhr.responseText; return xhr.responseText;

View file

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

View file

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