Skip to content

Commit

Permalink
fix: add binarytype support to websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Oct 21, 2024
1 parent a105aa9 commit 15c75b4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/client/shared/requests/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,25 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
barews.addEventListener("close", (ev) => {
fakeEventSend(new CloseEvent("close", ev));
});
barews.addEventListener("message", (ev) => {
barews.addEventListener("message", async (ev) => {
let payload = ev.data;
if (typeof payload === "string") {
// DO NOTHING
} else if ("byteLength" in payload) {
if (state.binaryType === "blob") {
payload = new Blob([payload]);
} else {
Object.setPrototypeOf(payload, ArrayBuffer.prototype);
}
} else if ("arrayBuffer" in payload) {
if (state.binaryType === "arraybuffer") {
payload = await payload.arrayBuffer();
Object.setPrototypeOf(payload, ArrayBuffer.prototype);
}
}

const fakeev = new MessageEvent("message", {
data: ev.data,
data: payload,
origin: ev.origin,
lastEventId: ev.lastEventId,
source: ev.source,
Expand Down

0 comments on commit 15c75b4

Please sign in to comment.