From 7956dd640388fce62c83453d56e1a20aec2212b2 Mon Sep 17 00:00:00 2001 From: Jonas Gloning <34194370+jonasgloning@users.noreply.github.com> Date: Sun, 3 Dec 2023 11:36:47 +0100 Subject: [PATCH] fix: support Blobs nested in objects Closes #1163 --- e2e/datachannel/blobs.js | 3 +++ .../BufferedConnection/BinaryPack.ts | 22 +++++++++++-------- package-lock.json | 8 +++---- package.json | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/e2e/datachannel/blobs.js b/e2e/datachannel/blobs.js index 3ab9cd99d..b8887024d 100644 --- a/e2e/datachannel/blobs.js +++ b/e2e/datachannel/blobs.js @@ -2,6 +2,7 @@ import { commit_data } from "../data.js"; import { expect } from "https://esm.sh/v126/chai@4.3.7/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs"; const Encoder = new TextEncoder(); +const Decoder = new TextDecoder(); /** @param {unknown[]} received */ export const check = (received) => { @@ -10,6 +11,8 @@ export const check = (received) => { (blob) => Encoder.encode(JSON.stringify(blob)).buffer, ); expect(received).to.deep.equal(commits_as_arraybuffer); + const parsed_received = received.map((r) => JSON.parse(Decoder.decode(r))); + expect(parsed_received).to.deep.equal(commit_data); }; /** * @param {import("../peerjs").DataConnection} dataConnection diff --git a/lib/dataconnection/BufferedConnection/BinaryPack.ts b/lib/dataconnection/BufferedConnection/BinaryPack.ts index a0585affb..856b86580 100644 --- a/lib/dataconnection/BufferedConnection/BinaryPack.ts +++ b/lib/dataconnection/BufferedConnection/BinaryPack.ts @@ -75,16 +75,11 @@ export class BinaryPack extends BufferedConnection { } } - protected override _send( - data: Packable, - chunked: boolean, - ): void | Promise { - if (data instanceof Blob) { - return data.arrayBuffer().then((buffer) => { - this._send(buffer, chunked); - }); - } + protected override _send(data: Packable, chunked: boolean) { const blob = pack(data); + if (blob instanceof Promise) { + return this._send_blob(blob); + } if (!chunked && blob.byteLength > this.chunker.chunkedMTU) { this._sendChunks(blob); @@ -93,6 +88,15 @@ export class BinaryPack extends BufferedConnection { this._bufferedSend(blob); } + private async _send_blob(blobPromise: Promise) { + const blob = await blobPromise; + if (blob.byteLength > this.chunker.chunkedMTU) { + this._sendChunks(blob); + return; + } + + this._bufferedSend(blob); + } private _sendChunks(blob: ArrayBuffer) { const blobs = this.chunker.chunk(blob); diff --git a/package-lock.json b/package-lock.json index 729d58faa..2178e51e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@msgpack/msgpack": "^2.8.0", "cbor-x": "^1.5.3", "eventemitter3": "^4.0.7", - "peerjs-js-binarypack": "^2.0.0", + "peerjs-js-binarypack": "^2.1.0", "webrtc-adapter": "^8.0.0" }, "devDependencies": { @@ -15512,9 +15512,9 @@ } }, "node_modules/peerjs-js-binarypack": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/peerjs-js-binarypack/-/peerjs-js-binarypack-2.0.0.tgz", - "integrity": "sha512-wu+L0Qeg4IH2DXm3B6xKP5ODeCIovwEEO/Fu3MVqApPQeVLzSdZpFzQzPobh+sdhUWMQGEO7YxHeiwpPngLjqQ==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/peerjs-js-binarypack/-/peerjs-js-binarypack-2.1.0.tgz", + "integrity": "sha512-YIwCC+pTzp3Bi8jPI9UFKO0t0SLo6xALnHkiNt/iUFmUUZG0fEEmEyFKvjsDKweiFitzHRyhuh6NvyJZ4nNxMg==", "engines": { "node": ">= 14.0.0" }, diff --git a/package.json b/package.json index 12be8dee5..b3cb20599 100644 --- a/package.json +++ b/package.json @@ -218,7 +218,7 @@ "@msgpack/msgpack": "^2.8.0", "cbor-x": "^1.5.3", "eventemitter3": "^4.0.7", - "peerjs-js-binarypack": "^2.0.0", + "peerjs-js-binarypack": "^2.1.0", "webrtc-adapter": "^8.0.0" }, "alias": {