From 986e0ca8bf4f556259c0c3b4800b54751d2a4d96 Mon Sep 17 00:00:00 2001 From: WofWca Date: Sat, 26 Aug 2023 20:46:55 +0400 Subject: [PATCH] feat: emit error on connections when receiving "EXPIRE" Closes #924 --- lib/peer.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/peer.ts b/lib/peer.ts index b782f8f08..2a21700a6 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -392,6 +392,22 @@ export class Peer extends EventEmitter { PeerErrorType.PeerUnavailable, `Could not connect to peer ${peerId}`, ); + + // This is so that an error is emited on a connection that has just been created, + // but could not be established because the server responded with + // `ServerMessageType.Expire` (but yes, we emit the error on all connections). + // See https://github.com/peers/peerjs/issues/924 + // + // TODO how about `this._cleanupPeer(peerId);` instead? + // Or how about make the server send back `connectionId`, and only emit the error + // on that connection? + const connections = this._connections.get(peerId); + if (connections) { + for (const c of connections) { + c.emit("error", new Error(`Server says ${peerId} is unavailable`)); + } + } + break; case ServerMessageType.Offer: { // we should consider switching this to CALL/CONNECT, but this is the least breaking option.