From 87aed42d50f888300383d5d4949733dd5215a678 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Fri, 10 Feb 2023 11:14:52 +0530 Subject: [PATCH] Refactor protocol stream creation --- packages/peer/src/index.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/peer/src/index.ts b/packages/peer/src/index.ts index a5929c80..6fd9cc66 100644 --- a/packages/peer/src/index.ts +++ b/packages/peer/src/index.ts @@ -258,8 +258,7 @@ export class Peer { // Open stream if connection exists and it doesn't already have a stream with chat protocol if (connection && !connection.streams.some(stream => stream.stat.protocol === CHAT_PROTOCOL)) { - const stream = await connection.newStream([CHAT_PROTOCOL]); - this._handleStream(peerId, stream); + await this._createProtocolStream(connection, CHAT_PROTOCOL); } } @@ -329,18 +328,12 @@ export class Peer { console.log('Closed'); } - try { - // Open stream in new connection for chat protocol - const protocols = await this._node.peerStore.protoBook.get(remotePeerId); + // Open stream in new connection for chat protocol (if handled by remote peer) + const protocols = await this._node.peerStore.protoBook.get(remotePeerId); - // Dial if protocol is handled by remote peer - // The chat protocol may not be updated in the list and will be handled later on change:protocols event - if (protocols.includes(CHAT_PROTOCOL)) { - const stream = await connection.newStream([CHAT_PROTOCOL]); - this._handleStream(remotePeerId, stream); - } - } catch (err: any) { - console.log(`Could not create a new protocol stream with ${remotePeerId.toString()}`, err); + // The chat protocol may not be updated in the list and will be handled later on change:protocols event + if (protocols.includes(CHAT_PROTOCOL)) { + await this._createProtocolStream(connection, CHAT_PROTOCOL); } } @@ -353,6 +346,18 @@ export class Peer { ); } + async _createProtocolStream (connection: Connection, protocol: string) { + assert(this._node); + const remotePeerId = connection.remotePeer; + + try { + const stream = await connection.newStream([protocol]); + this._handleStream(remotePeerId, stream); + } catch (err: any) { + console.log(`Could not create a new ${protocol} stream with ${remotePeerId.toString()}`, err); + } + } + async _handleDeadConnections (remotePeerId: PeerId) { // Close existing connections of remote peer console.log(`Closing connections for ${remotePeerId}`);