dispatch urlchange event on client creation

This commit is contained in:
Percs 2025-03-04 12:25:19 -06:00
parent 6e451eee7f
commit fd346e8c6d
8 changed files with 268 additions and 265 deletions

View file

@ -42,21 +42,21 @@
"@nebula-services/bare-server-node": "^2.0.4", "@nebula-services/bare-server-node": "^2.0.4",
"@playwright/test": "^1.50.1", "@playwright/test": "^1.50.1",
"@rsdoctor/rspack-plugin": "^0.4.13", "@rsdoctor/rspack-plugin": "^0.4.13",
"@rspack/cli": "^1.2.5", "@rspack/cli": "^1.2.7",
"@rspack/core": "^1.2.5", "@rspack/core": "^1.2.7",
"@types/eslint": "^9.6.1", "@types/eslint": "^9.6.1",
"@types/estree": "^1.0.6", "@types/estree": "^1.0.6",
"@types/node": "^22.13.5", "@types/node": "^22.13.9",
"@types/serviceworker": "^0.0.121", "@types/serviceworker": "^0.0.123",
"@typescript-eslint/eslint-plugin": "^8.24.1", "@typescript-eslint/eslint-plugin": "^8.26.0",
"@typescript-eslint/parser": "^8.24.1", "@typescript-eslint/parser": "^8.26.0",
"dotenv": "^16.4.7", "dotenv": "^16.4.7",
"eslint": "^9.21.0", "eslint": "^9.21.0",
"fastify": "^5.2.1", "fastify": "^5.2.1",
"playwright": "^1.50.1", "playwright": "^1.50.1",
"prettier": "^3.5.1", "prettier": "^3.5.3",
"tslib": "^2.8.1", "tslib": "^2.8.1",
"typescript": "^5.7.3" "typescript": "^5.8.2"
}, },
"dependencies": { "dependencies": {
"@mercuryworkshop/bare-mux": "^2.1.7", "@mercuryworkshop/bare-mux": "^2.1.7",

472
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

View file

@ -254,7 +254,16 @@ export class ScramjetClient {
return sframe; return sframe;
} }
get isSubframe(): boolean {
if (!iswindow) return false;
const frame = this.descriptors.get("window.frameElement", this.global);
if (!frame) return false; // we're top level
const sframe = frame[SCRAMJETFRAME];
if (!sframe) return true;
return false;
}
loadcookies(cookiestr: string) { loadcookies(cookiestr: string) {
this.cookieStore.load(cookiestr); this.cookieStore.load(cookiestr);
} }

View file

@ -10,7 +10,7 @@ export default function (client: ScramjetClient, _self: Self) {
ctx.call(); ctx.call();
const ev = new UrlChangeEvent(client.url.href); const ev = new UrlChangeEvent(client.url.href);
if (client.frame) client.frame.dispatchEvent(ev); if (!client.isSubframe) client.frame?.dispatchEvent(ev);
}, },
}); });
@ -21,7 +21,7 @@ export default function (client: ScramjetClient, _self: Self) {
ctx.call(); ctx.call();
const ev = new UrlChangeEvent(client.url.href); const ev = new UrlChangeEvent(client.url.href);
if (client.frame) client.frame.dispatchEvent(ev); if (!client.isSubframe) client.frame?.dispatchEvent(ev);
}, },
}); });
} }

View file

@ -10,7 +10,7 @@ export class UrlChangeEvent extends Event {
} }
} }
export class ScramjetContextInit extends Event { export class ScramjetContextEvent extends Event {
constructor(public window: Self) { constructor(public window: Self) {
super("contextInit"); super("contextInit");
} }

View file

@ -3,7 +3,7 @@
import { loadCodecs } from "../scramjet"; import { loadCodecs } from "../scramjet";
import { SCRAMJETCLIENT } from "../symbols"; import { SCRAMJETCLIENT } from "../symbols";
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { ScramjetContextInit } from "./events"; import { ScramjetContextEvent, UrlChangeEvent } from "./events";
import { ScramjetServiceWorkerRuntime } from "./swruntime"; import { ScramjetServiceWorkerRuntime } from "./swruntime";
export const iswindow = "window" in self && window instanceof Window; export const iswindow = "window" in self && window instanceof Window;
@ -30,8 +30,10 @@ if (!(SCRAMJETCLIENT in <Partial<typeof self>>self)) {
runtime.hook(); runtime.hook();
} }
const ev = new ScramjetContextInit(client.global.window); const contextev = new ScramjetContextEvent(client.global.window);
client.frame?.dispatchEvent(ev); client.frame?.dispatchEvent(contextev);
const urlchangeev = new UrlChangeEvent(client.url.href);
if (!client.isSubframe) client.frame?.dispatchEvent(urlchangeev);
} }
Reflect.deleteProperty(self, "WASM"); Reflect.deleteProperty(self, "WASM");

View file

@ -41,7 +41,7 @@ export async function handleFetch(
requesturl.searchParams.delete("dest"); requesturl.searchParams.delete("dest");
} }
if (requesturl.pathname == this.config.files.wasm) { if (requesturl.pathname === this.config.files.wasm) {
return fetch(this.config.files.wasm).then(async (x) => { return fetch(this.config.files.wasm).then(async (x) => {
const buf = await x.arrayBuffer(); const buf = await x.arrayBuffer();
const b64 = btoa( const b64 = btoa(
@ -54,7 +54,8 @@ export async function handleFetch(
); );
let payload = ""; let payload = "";
payload += `if ("document" in self && document.currentScript) { document.currentScript.remove(); }\n`; payload +=
"if ('document' in self && document.currentScript) { document.currentScript.remove(); }\n";
payload += `self.WASM = '${b64}';`; payload += `self.WASM = '${b64}';`;
return new Response(payload, { return new Response(payload, {
@ -164,11 +165,11 @@ export async function handleFetch(
const ev = new ScramjetRequestEvent( const ev = new ScramjetRequestEvent(
url, url,
headers.headers,
request.body, request.body,
request.method, request.method,
request.destination, request.destination,
client, client
headers.headers
); );
this.dispatchEvent(ev); this.dispatchEvent(ev);
@ -396,11 +397,11 @@ export class ScramjetHandleResponseEvent extends Event {
export class ScramjetRequestEvent extends Event { export class ScramjetRequestEvent extends Event {
constructor( constructor(
public url: URL, public url: URL,
public requestHeaders: Record<string, string>,
public body: BodyType, public body: BodyType,
public method: string, public method: string,
public destination: string, public destination: string,
public client: Client, public client: Client
public requestHeaders: Record<string, string>
) { ) {
super("request"); super("request");
} }

View file

@ -207,15 +207,6 @@ function BrowserApp() {
if (!e.url) return; if (!e.url) return;
this.url = e.url; this.url = e.url;
}); });
frame.frame.addEventListener("load", () => {
let url = frame.frame.contentWindow.location.href;
if (!url) return;
if (url === "about:blank") return;
this.url = $scramjet.codec.decode(
url.substring((location.href + "/scramjet").length)
);
});
const handleSubmit = () => { const handleSubmit = () => {
this.url = this.url.trim(); this.url = this.url.trim();