From 0e1410bb95ad293df314a9a5fbe9d149f623f652 Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Mon, 14 Aug 2023 22:00:38 -0700 Subject: [PATCH] WINDOW.ARRAYBUFFER IM GOING CRAZY --- client/package.json | 3 ++- client/src/AdriftClient.ts | 9 +++++---- client/src/Connection.ts | 6 +++--- client/src/DevWsTransport.ts | 2 +- pnpm-lock.yaml | 8 ++++++++ 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/client/package.json b/client/package.json index b3fc8de..e1b8dac 100644 --- a/client/package.json +++ b/client/package.json @@ -13,7 +13,8 @@ "@types/uuid": "^9.0.2", "bare-client-custom": "file:../bare-client-custom", "firebase": "^10.1.0", - "protocol": "workspace:*" + "protocol": "workspace:*", + "web-streams-polyfill": "^3.2.1" }, "devDependencies": { "@types/node": "^20.4.10" diff --git a/client/src/AdriftClient.ts b/client/src/AdriftClient.ts index 832c132..1ede195 100644 --- a/client/src/AdriftClient.ts +++ b/client/src/AdriftClient.ts @@ -26,14 +26,14 @@ function createBodyStream( body = new TextEncoder().encode(body); } - if (ArrayBuffer.isView(body)) { + if (window.ArrayBuffer.isView(body)) { body = body.buffer.slice( body.byteOffset, body.byteOffset + body.byteLength ); } - if (body instanceof ArrayBuffer) { + if (body instanceof window.ArrayBuffer) { if (body.byteLength == 0) { return null; } @@ -68,7 +68,7 @@ function createBodyStream( if (chunk instanceof Blob) { chunk = await chunk.arrayBuffer(); } - if (ArrayBuffer.isView(chunk)) { + if (window.ArrayBuffer.isView(chunk)) { chunk = chunk.buffer.slice( chunk.byteOffset, chunk.byteOffset + chunk.byteLength @@ -76,7 +76,7 @@ function createBodyStream( } // if none of those worked, give up. - if (!(chunk instanceof ArrayBuffer)) { + if (!(chunk instanceof window.ArrayBuffer)) { console.error({ chunk }); throw new Error("Invalid type read from body stream: " + chunk); } @@ -187,6 +187,7 @@ export class AdriftBareClient extends Client { ws.dispatchEvent(new CloseEvent("close", { code, reason, wasClean })); }, (data) => { + console.log({ data, binaryType: ws.binaryType }); ws.dispatchEvent( new MessageEvent("message", { data, diff --git a/client/src/Connection.ts b/client/src/Connection.ts index 3a1a6bf..350c922 100644 --- a/client/src/Connection.ts +++ b/client/src/Connection.ts @@ -147,7 +147,7 @@ export class Connection { type: C2SRequestType, data?: ArrayBuffer | Blob ): Promise { - let header = new ArrayBuffer(2 + 1); + let header = new window.ArrayBuffer(2 + 1); let view = new DataView(header); let cursor = 0; @@ -245,11 +245,11 @@ export class Connection { ).catch(cleanup); return; } - if (data instanceof ArrayBuffer) { + if (data instanceof window.ArrayBuffer) { this.send(seq, C2SRequestTypes.WSSendBinary, data).catch(cleanup); return; } - if (ArrayBuffer.isView(data)) { + if (window.ArrayBuffer.isView(data)) { this.send( seq, C2SRequestTypes.WSSendBinary, diff --git a/client/src/DevWsTransport.ts b/client/src/DevWsTransport.ts index 11e1f15..458683c 100644 --- a/client/src/DevWsTransport.ts +++ b/client/src/DevWsTransport.ts @@ -14,7 +14,7 @@ export class DevWsTransport extends Transport { } onmessage(msg: MessageEvent) { - if (msg.data instanceof ArrayBuffer) { + if (msg.data instanceof window.ArrayBuffer) { this.ondata(msg.data); return; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 912de03..8e8b66b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -174,6 +174,9 @@ importers: protocol: specifier: workspace:* version: link:../protocol + web-streams-polyfill: + specifier: ^3.2.1 + version: 3.2.1 devDependencies: '@types/node': specifier: ^20.4.10 @@ -10998,6 +11001,11 @@ packages: defaults: 1.0.4 dev: false + /web-streams-polyfill@3.2.1: + resolution: {integrity: sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==} + engines: {node: '>= 8'} + dev: false + /webidl-conversions@3.0.1: resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} dev: false