Skip to content

Commit

Permalink
Merge branch 'binarypack2' into rc
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgloning committed Feb 25, 2023
2 parents 2241eab + 18d491a commit 18c0f1b
Show file tree
Hide file tree
Showing 10 changed files with 9,658 additions and 21,657 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github: peers
open_collective: peer
4 changes: 2 additions & 2 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v2
- uses: actions/cache@v2
uses: actions/checkout@v3
- uses: actions/cache@v3
name: Configure npm caching
with:
path: ~/.npm
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: "lts/*"
- name: Install dependencies
run: npm ci
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v4
uses: crazy-max/ghaction-import-gpg@v5
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.GPG_PASSPHRASE }}
Expand Down
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
## [1.4.6-rc.2](https://github.com/peers/peerjs/compare/v1.4.6-rc.1...v1.4.6-rc.2) (2022-06-08)
## [1.4.7](https://github.com/peers/peerjs/compare/v1.4.6...v1.4.7) (2022-08-09)


### Bug Fixes

* **typings:** `MediaConnection.answer()` doesn’t need a `stream` anymore, thanks [@matallui](https://github.com/matallui)! ([666dcd9](https://github.com/peers/peerjs/commit/666dcd9770fe080e00898b9138664e8996bf5162))
* **typings:** much stronger event typings for `DataConnection`,`MediaConnection` ([0c96603](https://github.com/peers/peerjs/commit/0c96603a3f97f28eabe24906e692c31ef0ebca13))
* **browser-bundle:** Leaked private functions in the global scope ([857d425](https://github.com/peers/peerjs/commit/857d42524a929388b352a2330f18fdfc15df6c22)), closes [#989](https://github.com/peers/peerjs/issues/989)

## [1.4.6-rc.1](https://github.com/peers/peerjs/compare/v1.4.5...v1.4.6-rc.1) (2022-05-25)
## [1.4.6](https://github.com/peers/peerjs/compare/v1.4.5...v1.4.6) (2022-05-25)


### Bug Fixes
Expand Down
224 changes: 112 additions & 112 deletions README.md

Large diffs are not rendered by default.

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;
}
Loading

0 comments on commit 18c0f1b

Please sign in to comment.