Skip to content

Commit

Permalink
fix: support Blobs nested in objects
Browse files Browse the repository at this point in the history
Closes #1163
  • Loading branch information
jonasgloning committed Dec 3, 2023
1 parent ee531bd commit 7956dd6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
3 changes: 3 additions & 0 deletions e2e/datachannel/blobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand Down
22 changes: 13 additions & 9 deletions lib/dataconnection/BufferedConnection/BinaryPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,11 @@ export class BinaryPack extends BufferedConnection {
}
}

protected override _send(
data: Packable,
chunked: boolean,
): void | Promise<void> {
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);
Expand All @@ -93,6 +88,15 @@ export class BinaryPack extends BufferedConnection {

this._bufferedSend(blob);
}
private async _send_blob(blobPromise: Promise<ArrayBufferLike>) {
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);
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 7956dd6

Please sign in to comment.