Skip to content

Commit

Permalink
fix(datachannel): sending order is now preserved correctly
Browse files Browse the repository at this point in the history
Closes #746
  • Loading branch information
jonasgloning committed Feb 25, 2023
1 parent 3371155 commit 18d491a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
29 changes: 14 additions & 15 deletions lib/dataconnection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { util } from "./util";
import { concatArrayBuffers, util } from "./util";
import logger from "./logger";
import { Negotiator } from "./negotiator";
import { ConnectionType, SerializationType, ServerMessageType } from "./enums";
Expand Down Expand Up @@ -45,7 +45,7 @@ export class DataConnection
private _buffering = false;
private _chunkedData: {
[id: number]: {
data: Blob[];
data: Uint8Array[];
count: number;
total: number;
};
Expand Down Expand Up @@ -168,7 +168,7 @@ export class DataConnection
__peerData: number;
n: number;
total: number;
data: Blob;
data: ArrayBuffer;
}): void {
const id = data.__peerData;
const chunkInfo = this._chunkedData[id] || {
Expand All @@ -177,7 +177,7 @@ export class DataConnection
total: data.total,
};

chunkInfo.data[data.n] = data.data;
chunkInfo.data[data.n] = new Uint8Array(data.data);
chunkInfo.count++;
this._chunkedData[id] = chunkInfo;

Expand All @@ -186,8 +186,8 @@ export class DataConnection
delete this._chunkedData[id];

// We've received all the chunks--time to construct the complete data.
const data = new Blob(chunkInfo.data);
this._handleDataMessage({ data });
const data = concatArrayBuffers(chunkInfo.data);
this.emit("data", util.unpack(data));
}
}

Expand Down Expand Up @@ -246,6 +246,11 @@ export class DataConnection
return;
}

if (data instanceof Blob) {
data.arrayBuffer().then((ab) => this.send(ab));
return;
}

if (this.serialization === SerializationType.JSON) {
this._bufferedSend(this.stringify(data));
} else if (
Expand All @@ -254,18 +259,12 @@ export class DataConnection
) {
const blob = util.pack(data);

if (!chunked && blob.size > util.chunkedMTU) {
if (!chunked && blob.byteLength > util.chunkedMTU) {
this._sendChunks(blob);
return;
}

if (!util.supports.binaryBlob) {
// We only do this if we really need to (e.g. blobs are not supported),
// because this conversion is costly.
this._encodingQueue.enque(blob);
} else {
this._bufferedSend(blob);
}
this._bufferedSend(blob);
} else {
this._bufferedSend(data);
}
Expand Down Expand Up @@ -327,7 +326,7 @@ export class DataConnection
}
}

private _sendChunks(blob: Blob): void {
private _sendChunks(blob: ArrayBuffer): void {
const blobs = util.chunk(blob);
logger.log(`DC#${this.connectionId} Try to send ${blobs.length} chunks...`);

Expand Down
23 changes: 17 additions & 6 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Types aren’t accurate
//@ts-ignore
import BinaryPack from "peerjs-js-binarypack";
import * as BinaryPack from "peerjs-js-binarypack";
import { Supports } from "./supports";

export interface UtilSupportsObj {
Expand Down Expand Up @@ -105,10 +103,10 @@ class Util {
private _dataCount: number = 1;

chunk(
blob: Blob,
): { __peerData: number; n: number; total: number; data: Blob }[] {
blob: ArrayBuffer,
): { __peerData: number; n: number; total: number; data: ArrayBuffer }[] {
const chunks = [];
const size = blob.size;
const size = blob.byteLength;
const total = Math.ceil(size / util.chunkedMTU);

let index = 0;
Expand Down Expand Up @@ -172,3 +170,16 @@ class Util {
}
}
export const util = new Util();
export function concatArrayBuffers(bufs: Uint8Array[]) {
let size = 0;
for (const buf of bufs) {
size += buf.byteLength;
}
const result = new Uint8Array(size);
let offset = 0;
for (const buf of bufs) {
result.set(buf, offset);
offset += buf.byteLength;
}
return result;
}
21 changes: 14 additions & 7 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 @@ -184,7 +184,7 @@
"dependencies": {
"@swc/helpers": "^0.4.0",
"eventemitter3": "^4.0.7",
"peerjs-js-binarypack": "1.0.1",
"peerjs-js-binarypack": "^2.0.0-rc.1",
"webrtc-adapter": "^8.0.0"
}
}

0 comments on commit 18d491a

Please sign in to comment.