mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
fix websockets
This commit is contained in:
parent
83e6374131
commit
ae771f444d
2 changed files with 47 additions and 4 deletions
|
@ -9,6 +9,7 @@ import {
|
||||||
rewriteJs,
|
rewriteJs,
|
||||||
rewriteSrcset,
|
rewriteSrcset,
|
||||||
} from "../../shared";
|
} from "../../shared";
|
||||||
|
import type { URLMeta } from "../../shared/rewriters/url";
|
||||||
|
|
||||||
export default function (client: ScramjetClient, self: typeof window) {
|
export default function (client: ScramjetClient, self: typeof window) {
|
||||||
const attrObject = {
|
const attrObject = {
|
||||||
|
@ -65,7 +66,16 @@ export default function (client: ScramjetClient, self: typeof window) {
|
||||||
) {
|
) {
|
||||||
value = encodeUrl(value, client.meta);
|
value = encodeUrl(value, client.meta);
|
||||||
} else if (attr === "srcdoc") {
|
} else if (attr === "srcdoc") {
|
||||||
value = rewriteHtml(value, client.cookieStore, undefined, true);
|
value = rewriteHtml(
|
||||||
|
value,
|
||||||
|
client.cookieStore,
|
||||||
|
{
|
||||||
|
// srcdoc preserves parent origin i think
|
||||||
|
base: new URL(client.url.origin),
|
||||||
|
origin: new URL(client.url.origin),
|
||||||
|
} as URLMeta,
|
||||||
|
true
|
||||||
|
);
|
||||||
} else if (["srcset", "imagesrcset"].includes(attr)) {
|
} else if (["srcset", "imagesrcset"].includes(attr)) {
|
||||||
value = rewriteSrcset(value, client.meta);
|
value = rewriteSrcset(value, client.meta);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
new Proxy(ev, {
|
new Proxy(ev, {
|
||||||
get(target, prop) {
|
get(target, prop) {
|
||||||
if (prop === "isTrusted") return true;
|
if (prop === "isTrusted") return true;
|
||||||
|
|
||||||
return Reflect.get(target, prop);
|
return Reflect.get(target, prop);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -53,13 +54,34 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
listeners: {},
|
listeners: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
barews.addEventListener("open", (ev) => {
|
function fakeEventSend(fakeev: Event) {
|
||||||
const fakeev = new Event("open");
|
state["on" + fakeev.type]?.(trustEvent(fakeev));
|
||||||
state.onopen?.(trustEvent(fakeev));
|
|
||||||
fakeWebSocket.dispatchEvent(fakeev);
|
fakeWebSocket.dispatchEvent(fakeev);
|
||||||
|
}
|
||||||
|
|
||||||
|
barews.addEventListener("open", (ev) => {
|
||||||
|
fakeEventSend(new Event("open"));
|
||||||
|
});
|
||||||
|
barews.addEventListener("close", (ev) => {
|
||||||
|
fakeEventSend(new CloseEvent("close", ev));
|
||||||
|
});
|
||||||
|
barews.addEventListener("message", (ev) => {
|
||||||
|
const fakeev = new MessageEvent("message", {
|
||||||
|
data: ev.data,
|
||||||
|
origin: ev.origin,
|
||||||
|
lastEventId: ev.lastEventId,
|
||||||
|
source: ev.source,
|
||||||
|
ports: ev.ports,
|
||||||
|
});
|
||||||
|
|
||||||
|
fakeEventSend(fakeev);
|
||||||
|
});
|
||||||
|
barews.addEventListener("error", (ev) => {
|
||||||
|
fakeEventSend(new Event("error"));
|
||||||
});
|
});
|
||||||
|
|
||||||
socketmap.set(fakeWebSocket, state);
|
socketmap.set(fakeWebSocket, state);
|
||||||
|
ctx.return(fakeWebSocket);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -116,6 +138,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.binaryType", {
|
client.Trap("WebSocket.prototype.binaryType", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.binaryType;
|
return ws.binaryType;
|
||||||
},
|
},
|
||||||
set(ctx, v: string) {
|
set(ctx, v: string) {
|
||||||
|
@ -133,6 +156,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.extensions", {
|
client.Trap("WebSocket.prototype.extensions", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.extensions;
|
return ws.extensions;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -140,6 +164,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.onclose", {
|
client.Trap("WebSocket.prototype.onclose", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.onclose;
|
return ws.onclose;
|
||||||
},
|
},
|
||||||
set(ctx, v: (ev: CloseEvent) => any) {
|
set(ctx, v: (ev: CloseEvent) => any) {
|
||||||
|
@ -151,6 +176,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.onerror", {
|
client.Trap("WebSocket.prototype.onerror", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.onerror;
|
return ws.onerror;
|
||||||
},
|
},
|
||||||
set(ctx, v: (ev: Event) => any) {
|
set(ctx, v: (ev: Event) => any) {
|
||||||
|
@ -162,6 +188,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.onmessage", {
|
client.Trap("WebSocket.prototype.onmessage", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.onmessage;
|
return ws.onmessage;
|
||||||
},
|
},
|
||||||
set(ctx, v: (ev: MessageEvent) => any) {
|
set(ctx, v: (ev: MessageEvent) => any) {
|
||||||
|
@ -173,6 +200,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.onopen", {
|
client.Trap("WebSocket.prototype.onopen", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.onopen;
|
return ws.onopen;
|
||||||
},
|
},
|
||||||
set(ctx, v: (ev: Event) => any) {
|
set(ctx, v: (ev: Event) => any) {
|
||||||
|
@ -184,6 +212,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.url", {
|
client.Trap("WebSocket.prototype.url", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.url;
|
return ws.url;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -191,6 +220,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.protocol", {
|
client.Trap("WebSocket.prototype.protocol", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.protocol;
|
return ws.protocol;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -198,6 +228,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Trap("WebSocket.prototype.readyState", {
|
client.Trap("WebSocket.prototype.readyState", {
|
||||||
get(ctx) {
|
get(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
|
||||||
return ws.barews.readyState;
|
return ws.barews.readyState;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -213,6 +244,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Proxy("WebSocket.prototype.close", {
|
client.Proxy("WebSocket.prototype.close", {
|
||||||
apply(ctx) {
|
apply(ctx) {
|
||||||
const ws = socketmap.get(ctx.this);
|
const ws = socketmap.get(ctx.this);
|
||||||
|
if (ctx.args[0] === undefined) ctx.args[0] = 1000;
|
||||||
|
if (ctx.args[1] === undefined) ctx.args[1] = "";
|
||||||
ctx.return(ws.barews.close(ctx.args[0], ctx.args[1]));
|
ctx.return(ws.barews.close(ctx.args[0], ctx.args[1]));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue