Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: emit error on connections when receiving "EXPIRE" #1125

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions e2e/peer/peer-unavailable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!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>PEER-UNAVAILABLE</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 connectionErrors = document.getElementById("messages");
const peerErrors = document.getElementById("error-message");

const not_existing_peer = crypto
.getRandomValues(new Uint8Array(16))
.join("");

const peer = new Peer();
peer
.once(
"error",
(error) => void (peerErrors.textContent += JSON.stringify(error)),
)
.once("open", (id) => {
const connection = peer.connect(not_existing_peer);
connection.once(
"error",
(error) =>
void (connectionErrors.textContent += JSON.stringify(error)),
);
});
</script>
</body>
</html>
5 changes: 5 additions & 0 deletions e2e/peer/peer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ describe("Peer", () => {
await P.waitForMessage('{"type":"disconnected"}');
expect(await P.errorMessage.getText()).toBe("");
});
it("should emit an error, when the remote peer is unavailable", async () => {
await P.open("peer-unavailable");
await P.waitForMessage('{"type":"peer-unavailable"}');
expect(await P.errorMessage.getText()).toBe('{"type":"peer-unavailable"}');
});
});
1 change: 1 addition & 0 deletions lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export enum PeerErrorType {
}

export enum BaseConnectionErrorType {
PeerUnavailable = "peer-unavailable",
NegotiationFailed = "negotiation-failed",
ConnectionClosed = "connection-closed",
}
Expand Down
17 changes: 16 additions & 1 deletion lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Socket } from "./socket";
import { MediaConnection } from "./mediaconnection";
import type { DataConnection } from "./dataconnection/DataConnection";
import {
BaseConnectionErrorType,
ConnectionType,
PeerErrorType,
ServerMessageType,
Expand Down Expand Up @@ -379,6 +380,16 @@ export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
PeerErrorType.PeerUnavailable,
`Could not connect to peer ${peerId}`,
);
// Emit an error on all connections with this peer.
const connections = (this._connections.get(peerId) ?? []).filter(
(c) => c.peer === peerId,
);
for (const conn of connections) {
conn.emitError(
BaseConnectionErrorType.PeerUnavailable,
`${peerId} is unavailable`,
);
}
break;
case ServerMessageType.Offer: {
// we should consider switching this to CALL/CONNECT, but this is the least breaking option.
Expand Down Expand Up @@ -484,7 +495,11 @@ export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {

/**
* Connects to the remote peer specified by id and returns a data connection.
* @param peer The brokering ID of the remote peer (their {@apilink Peer.id}).
*
* Make sure to listen to the `error` event of the resulting {@link DataConnection}
* in case the connection fails.
*
* @param peer The brokering ID of the remote peer (their {@link Peer.id}).
* @param options for specifying details about Peer Connection
*/
connect(peer: string, options: PeerConnectOption = {}): DataConnection {
Expand Down