Skip to content

Commit

Permalink
Merge branch 'binarypack2.1' into rc
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgloning committed Dec 3, 2023
2 parents d2b845b + 7956dd6 commit 9136cf2
Show file tree
Hide file tree
Showing 28 changed files with 4,959 additions and 4,390 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ npm-debug.log
.DS_STORE

test/output
browserstack.err
.tscache
test/public
.vscode/
30 changes: 17 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
# [1.5.0-rc.1](https://github.com/peers/peerjs/compare/v1.4.8-rc.1...v1.5.0-rc.1) (2023-08-14)
## [1.5.1](https://github.com/peers/peerjs/compare/v1.5.0...v1.5.1) (2023-09-23)


### Bug Fixes

* convert `Blob`s to `ArrayBuffer`s during `.send()` ([95bb0f7](https://github.com/peers/peerjs/commit/95bb0f7fa9aa0d119613727c32857e5af33e14a1)), closes [#1137](https://github.com/peers/peerjs/issues/1137)
* convert `Blob`s to `ArrayBuffer`s during `.send()` ([#1142](https://github.com/peers/peerjs/issues/1142)) ([094f849](https://github.com/peers/peerjs/commit/094f849816d327bf74a447fbf7d58195c1a4fc66))

# [1.5.0](https://github.com/peers/peerjs/compare/v1.4.7...v1.5.0) (2023-09-03)


### Bug Fixes

* **datachannel:** sending order is now preserved correctly ([#1038](https://github.com/peers/peerjs/issues/1038)) ([0fb6179](https://github.com/peers/peerjs/commit/0fb61792ed3afe91123550a159c8633ed0976f89)), closes [#746](https://github.com/peers/peerjs/issues/746)
* **deps:** update dependency @swc/helpers to ^0.4.0 ([a7de8b7](https://github.com/peers/peerjs/commit/a7de8b78f57a5cf9708fa54e9f82f4ab43c0bca2))
* **deps:** update dependency cbor-x to v1.5.4 ([c1f04ec](https://github.com/peers/peerjs/commit/c1f04ecf686e64266fb54b3e4992c73c1522ae79))
* **deps:** update dependency eventemitter3 to v5 ([caf01c6](https://github.com/peers/peerjs/commit/caf01c6440534cbe190facd84cecf9ca62e4a5ce))
* **deps:** update dependency peerjs-js-binarypack to v1.0.2 ([7452e75](https://github.com/peers/peerjs/commit/7452e7591d4982d9472c524d6ad30e66c2a2b44f))
* **deps:** update dependency webrtc-adapter to v8 ([431f00b](https://github.com/peers/peerjs/commit/431f00bd89809867a19c98224509982b82769558))
* **deps:** update dependency webrtc-adapter to v8.2.2 ([62402fc](https://github.com/peers/peerjs/commit/62402fcae03c78382d7fa80c11f459aca8d21620))
* **deps:** update dependency webrtc-adapter to v8.2.3 ([963455e](https://github.com/peers/peerjs/commit/963455ee383a069e53bd93b1128d82615a698245))
* **MediaConnection:** `close` event is fired on remote Peer ([0836356](https://github.com/peers/peerjs/commit/0836356d18c91449f4cbb23e4d4660a4351d7f56)), closes [#636](https://github.com/peers/peerjs/issues/636) [#1089](https://github.com/peers/peerjs/issues/1089) [#1032](https://github.com/peers/peerjs/issues/1032) [#832](https://github.com/peers/peerjs/issues/832) [#780](https://github.com/peers/peerjs/issues/780) [#653](https://github.com/peers/peerjs/issues/653)
* **npm audit:** Updates all dependencies that cause npm audit to issue a warning ([6ef5707](https://github.com/peers/peerjs/commit/6ef5707dc85d8b921d8dfea74890b110ddf5cd4f))


### Features

* `.type` property on `Error`s emitted from connections ([#1126](https://github.com/peers/peerjs/issues/1126)) ([debe7a6](https://github.com/peers/peerjs/commit/debe7a63474b9cdb705676d4c7892b0cd294402a))
* `PeerError` from connections ([ad3a0cb](https://github.com/peers/peerjs/commit/ad3a0cbe8c5346509099116441e6c3ff0b6ca6c4))
* **DataConnection:** handle close messages and flush option ([6ca38d3](https://github.com/peers/peerjs/commit/6ca38d32b0929745b92a55c8f6aada1ee0895ce7)), closes [#982](https://github.com/peers/peerjs/issues/982)
* **MediaChannel:** Add experimental `willCloseOnRemote` event to MediaConnection. ([ed84829](https://github.com/peers/peerjs/commit/ed84829a1092422f3d7f92f467bcf5b8ada82891))
* MsgPack/Cbor serialization ([fcffbf2](https://github.com/peers/peerjs/commit/fcffbf243cb7d6dabfc773211c155c0ae1e00baf))

## [1.4.8-rc.1](https://github.com/peers/peerjs/compare/v1.4.7...v1.4.8-rc.1) (2023-02-25)


### Bug Fixes

* `close` event was not triggered reliably ([#860](https://github.com/peers/peerjs/issues/860)) ([ec1d5ae](https://github.com/peers/peerjs/commit/ec1d5ae8436b5265a59ef277e3fa05608f19f1ae)), closes [#636](https://github.com/peers/peerjs/issues/636)
* **datachannel:** sending order is now preserved correctly ([18d491a](https://github.com/peers/peerjs/commit/18d491a83df759f26c08d1bb10d6011891eb2efc)), closes [#746](https://github.com/peers/peerjs/issues/746)
* **deps:** update dependency @swc/helpers to ^0.4.0 ([a7de8b7](https://github.com/peers/peerjs/commit/a7de8b78f57a5cf9708fa54e9f82f4ab43c0bca2))
* **deps:** update dependency eventemitter3 to v5 ([caf01c6](https://github.com/peers/peerjs/commit/caf01c6440534cbe190facd84cecf9ca62e4a5ce))
* **deps:** update dependency webrtc-adapter to v8 ([431f00b](https://github.com/peers/peerjs/commit/431f00bd89809867a19c98224509982b82769558))
* **npm audit:** Updates all dependencies that cause npm audit to issue a warning ([6ef5707](https://github.com/peers/peerjs/commit/6ef5707dc85d8b921d8dfea74890b110ddf5cd4f))
* MsgPack/Cbor serialization ([#1120](https://github.com/peers/peerjs/issues/1120)) ([4367256](https://github.com/peers/peerjs/commit/43672564ee9edcb15e736b0333c6ad8aeae20c59)), closes [#611](https://github.com/peers/peerjs/issues/611)

## [1.4.7](https://github.com/peers/peerjs/compare/v1.4.6...v1.4.7) (2022-08-09)

Expand Down
1 change: 0 additions & 1 deletion browserstack.err

This file was deleted.

24 changes: 24 additions & 0 deletions e2e/datachannel/blobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { commit_data } from "../data.js";
import { expect } from "https://esm.sh/v126/[email protected]/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs";

const Encoder = new TextEncoder();
const Decoder = new TextDecoder();

/** @param {unknown[]} received */
export const check = (received) => {
expect(received).to.be.an("array").with.lengthOf(commit_data.length);
const commits_as_arraybuffer = commit_data.map(
(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
*/
export const send = async (dataConnection) => {
for (const commit of commit_data) {
await dataConnection.send(new Blob([JSON.stringify(commit)]));
}
};
25 changes: 25 additions & 0 deletions e2e/datachannel/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { commit_data } from "../data.js";
import { expect } from "https://esm.sh/v126/[email protected]/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs";

const Encoder = new TextEncoder();

/** @param {unknown[]} received */
export const check = (received) => {
expect(received).to.be.an("array").with.lengthOf(commit_data.length);
const commits_as_arraybuffer = commit_data.map(
(blob) => Encoder.encode(JSON.stringify(blob)).buffer,
);
expect(received).to.deep.equal(commits_as_arraybuffer);
};
/**
* @param {import("../peerjs").DataConnection} dataConnection
*/
export const send = async (dataConnection) => {
for (const commit of commit_data) {
await dataConnection.send(
new File([JSON.stringify(commit)], "commit.txt", {
type: "dummy/data",
}),
);
}
};
4 changes: 2 additions & 2 deletions e2e/datachannel/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ const serialization = params.get("serialization");
}
});

sendBtn.addEventListener("click", () => {
sendBtn.addEventListener("click", async () => {
dataConnection.once("error", (err) => {
errorMessage.innerText = err.toString();
});
send(dataConnection);
await send(dataConnection);
dataConnection.close({ flush: true });
messages.textContent = "Sent!";
});
Expand Down
12 changes: 12 additions & 0 deletions e2e/datachannel/serialization_binary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ describe("DataChannel:Binary", () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL,
2,
);
it(
"should transfer Blobs",
serializationTest("./blobs", "binary"),
jasmine.DEFAULT_TIMEOUT_INTERVAL,
2,
);
it(
"should transfer Files",
serializationTest("./files", "binary"),
jasmine.DEFAULT_TIMEOUT_INTERVAL,
2,
);
it(
"should transfer arrays",
serializationTest("./arrays", "binary"),
Expand Down
34 changes: 34 additions & 0 deletions e2e/peer/disconnected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<h1>ID-TAKEN</h1>
<div id="messages"></div>
<div id="error-message"></div>
<script src="/dist/peerjs.js"></script>
<script type="application/javascript">
/**
* @type {typeof import("../..").Peer}
*/
const Peer = window.peerjs.Peer;

const messages = document.getElementById("messages");

const peer = new Peer();
peer
.once(
"error",
(error) => void (messages.textContent = JSON.stringify(error)),
)
.once("open", (id) => {
peer.disconnect();
peer.connect("otherid");
});
</script>
</body>
</html>
56 changes: 56 additions & 0 deletions e2e/peer/id-taken.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<h1>ID-TAKEN</h1>
<div id="messages"></div>
<div id="error-message"></div>
<script src="/dist/peerjs.js"></script>
<script type="application/javascript">
/**
* @type {typeof import("../..").Peer}
*/
const Peer = window.peerjs.Peer;

const messages = document.getElementById("messages");
const errorMessage = document.getElementById("error-message");

// Peer A should be created without an error
const peerA = new Peer()
.once(
"error",
(err) => (errorMessage.textContent += JSON.stringify(err)),
)
.once("open", (id) => {
// Create 10 new `Peer`s that will try to steel A's id
let peers_try_to_take = Array.from(
{ length: 10 },
(_, i) =>
new Promise((resolve, reject) =>
new Peer(id)
.once("open", () =>
reject(`Peer ${i} failed! Connection got established.`),
)
.once("error", (error) => {
if (error.type === "unavailable-id") {
resolve(`ID already taken. (${i})`);
} else {
reject(error);
}
}),
),
);
Promise.all(peers_try_to_take)
.then(() => (messages.textContent = "No ID takeover"))
.catch(
(error) => (errorMessage.textContent += JSON.stringify(error)),
);
});
</script>
</body>
</html>
26 changes: 26 additions & 0 deletions e2e/peer/peer.page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { browser, $ } from "@wdio/globals";
class PeerPage {
get messages() {
return $("div[id='messages']");
}

get errorMessage() {
return $("div[id='error-message']");
}

waitForMessage(right: string) {
return browser.waitUntil(
async () => {
const messages = await this.messages.getText();
return messages.startsWith(right);
},
{ timeoutMsg: `Expected message to start with ${right}`, timeout: 10000 },
);
}

async open(test: string) {
await browser.url(`/e2e/peer/${test}.html`);
}
}

export default new PeerPage();
20 changes: 20 additions & 0 deletions e2e/peer/peer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import P from "./peer.page.js";
import { browser, expect } from "@wdio/globals";

describe("Peer", () => {
it("should emit an error, when the ID is already taken", async () => {
await P.open("id-taken");
await P.waitForMessage("No ID takeover");
expect(await P.errorMessage.getText()).toBe("");
});
it("should emit an error, when the server is unavailable", async () => {
await P.open("server-unavailable");
await P.waitForMessage('{"type":"server-error"}');
expect(await P.errorMessage.getText()).toBe("");
});
it("should emit an error, when it got disconnected from the server", async () => {
await P.open("disconnected");
await P.waitForMessage('{"type":"disconnected"}');
expect(await P.errorMessage.getText()).toBe("");
});
});
31 changes: 31 additions & 0 deletions e2e/peer/server-unavailable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<h1>ID-TAKEN</h1>
<div id="messages"></div>
<div id="error-message"></div>
<script src="/dist/peerjs.js"></script>
<script type="application/javascript">
/**
* @type {typeof import("../..").Peer}
*/
const Peer = window.peerjs.Peer;

const messages = document.getElementById("messages");
const errorMessage = document.getElementById("error-message");

new Peer({
host: "thisserverwillneverexist.example.com",
}).once(
"error",
(error) => void (messages.textContent = JSON.stringify(error)),
);
</script>
</body>
</html>
24 changes: 18 additions & 6 deletions lib/baseconnection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { EventEmitter, ValidEventTypes } from "eventemitter3";
import type { Peer } from "./peer";
import type { ServerMessage } from "./servermessage";
import type { ConnectionType } from "./enums";
import { BaseConnectionErrorType } from "./enums";
import {
EventEmitterWithError,
type EventsWithError,
PeerError,
} from "./peerError";
import type { ValidEventTypes } from "eventemitter3";

export type BaseConnectionEvents = {
export interface BaseConnectionEvents<
ErrorType extends string = BaseConnectionErrorType,
> extends EventsWithError<ErrorType> {
/**
* Emitted when either you or the remote peer closes the connection.
*
Expand All @@ -17,13 +25,17 @@ export type BaseConnectionEvents = {
* connection.on('error', (error) => { ... });
* ```
*/
error: (error: Error) => void;
error: (error: PeerError<`${ErrorType}`>) => void;
iceStateChanged: (state: RTCIceConnectionState) => void;
};
}

export abstract class BaseConnection<
T extends ValidEventTypes,
> extends EventEmitter<T & BaseConnectionEvents> {
SubClassEvents extends ValidEventTypes,
ErrorType extends string = never,
> extends EventEmitterWithError<
ErrorType | BaseConnectionErrorType,
SubClassEvents & BaseConnectionEvents<BaseConnectionErrorType | ErrorType>
> {
protected _open = false;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/cborPeer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Peer, SerializerMapping } from "./peer";
import { Peer, type SerializerMapping } from "./peer";
import { Cbor } from "./dataconnection/StreamConnection/Cbor";

export class CborPeer extends Peer {
Expand Down
Loading

0 comments on commit 9136cf2

Please sign in to comment.