diff --git a/src/client/client.ts b/src/client/client.ts index 56ee77a..46457c1 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -63,17 +63,30 @@ export class ScramjetClient { recursive: true, }); + let modules = []; + for (const key of context.keys()) { + const module = context(key); if (!key.endsWith(".ts")) continue; if ( (key.startsWith("./dom/") && "window" in self) || (key.startsWith("./worker/") && "WorkerGlobalScope" in self) || key.startsWith("./shared/") ) { - const module = context(key); - module.default(this, this.global); + modules.push(module); } } + + modules.sort((a, b) => { + const aorder = a.order || 0; + const border = b.order || 0; + + return aorder - border; + }); + + for (const module of modules) { + module.default(this, this.global); + } } Proxy(name: string | string[], handler: Proxy) { diff --git a/src/client/dom/serviceworker.ts b/src/client/dom/serviceworker.ts index ae679b4..8f9e127 100644 --- a/src/client/dom/serviceworker.ts +++ b/src/client/dom/serviceworker.ts @@ -1,6 +1,9 @@ import { encodeUrl } from "../../shared/rewriters/url"; import { ScramjetClient } from "../client"; +// we need a late order because we're mangling with addEventListener at a higher level +export const order = 2; + export default function (client: ScramjetClient, self: Self) { let fakeregistrations = new WeakSet(); diff --git a/src/client/shared/event.ts b/src/client/shared/event.ts index c7450b4..de8ad59 100644 --- a/src/client/shared/event.ts +++ b/src/client/shared/event.ts @@ -8,10 +8,17 @@ export default function (client: ScramjetClient, self: Self) { const handlers = { message: { origin() { - return this.data.$scramjet$origin; + if (typeof this.data === "object" && "$scramjet$origin" in this.data) + return this.data.$scramjet$origin; + + // then it must be from a worker, which we aren't currently rewriting + return client.url.origin; }, data() { - return this.data.$scramjet$data; + if (typeof this.data === "object" && "$scramjet$data" in this.data) + return this.data.$scramjet$data; + + return this.data; }, }, }; @@ -46,8 +53,8 @@ export default function (client: ScramjetClient, self: Self) { client.Proxy("EventTarget.prototype.addEventListener", { apply(ctx) { - unproxy(ctx, client); - if (typeof ctx.args[1] === "object") + // if (ctx.args[0] === "message" && iswindow) debugger; + if (typeof ctx.args[1] === "function") ctx.args[1] = wraplistener(ctx.args[1]); }, }); diff --git a/src/client/shared/requests/websocket.ts b/src/client/shared/requests/websocket.ts index b22c1f6..617396d 100644 --- a/src/client/shared/requests/websocket.ts +++ b/src/client/shared/requests/websocket.ts @@ -1,8 +1,8 @@ -import { isworker } from "../.."; +import { iswindow, isworker } from "../.."; import { ScramjetClient } from "../../client"; import { BareClient } from "../../shared"; -const bare = new BareClient(); +const bare = iswindow && new BareClient(); export default function (client: ScramjetClient, self: typeof globalThis) { // r58 please fix diff --git a/src/client/shared/unproxy.ts b/src/client/shared/unproxy.ts index 4749256..7f21bc9 100644 --- a/src/client/shared/unproxy.ts +++ b/src/client/shared/unproxy.ts @@ -1,6 +1,9 @@ import { iswindow } from ".."; import { ProxyCtx, ScramjetClient } from "../client"; +// we don't want to end up overriding a property on window that's derived from a prototype until we've proxied the prototype +export const order = 3; + export default function (client: ScramjetClient, self: typeof window) { // an automated approach to cleaning the documentProxy from dom functions // it will trigger an illegal invocation if you pass the proxy to c++ code, we gotta hotswap it out with the real one