1
1
import { CodeError } from '@libp2p/interface'
2
2
import { multiaddr , type Multiaddr } from '@multiformats/multiaddr'
3
3
import { pbStream } from 'it-protobuf-stream'
4
- import pDefer , { type DeferredPromise } from 'p-defer'
5
4
import { type RTCPeerConnection , RTCSessionDescription } from '../webrtc/index.js'
6
5
import { Message } from './pb/message.js'
7
6
import { readCandidatesUntilConnected } from './util.js'
@@ -20,8 +19,6 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
20
19
const messageStream = pbStream ( stream ) . pb ( Message )
21
20
22
21
try {
23
- const answerSentPromise : DeferredPromise < void > = pDefer ( )
24
-
25
22
// candidate callbacks
26
23
peerConnection . onicecandidate = ( { candidate } ) => {
27
24
// a null candidate means end-of-candidates, an empty string candidate
@@ -67,7 +64,6 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
67
64
// create and write an SDP answer
68
65
const answer = await peerConnection . createAnswer ( ) . catch ( err => {
69
66
log . error ( 'could not execute createAnswer' , err )
70
- answerSentPromise . reject ( err )
71
67
throw new CodeError ( 'Failed to create answer' , 'ERR_SDP_HANDSHAKE_FAILED' )
72
68
} )
73
69
@@ -78,16 +74,11 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
78
74
signal
79
75
} )
80
76
81
- peerConnection . setLocalDescription ( answer ) . then ( ( ) => {
82
- answerSentPromise . resolve ( )
83
- } , err => {
77
+ await peerConnection . setLocalDescription ( answer ) . catch ( err => {
84
78
log . error ( 'could not execute setLocalDescription' , err )
85
- answerSentPromise . reject ( err )
86
79
throw new CodeError ( 'Failed to set localDescription' , 'ERR_SDP_HANDSHAKE_FAILED' )
87
80
} )
88
81
89
- await answerSentPromise . promise
90
-
91
82
log . trace ( 'recipient read candidates until connected' )
92
83
93
84
// wait until candidates are connected
@@ -96,11 +87,6 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
96
87
signal,
97
88
log
98
89
} )
99
-
100
- log . trace ( 'recipient connected, closing signaling stream' )
101
- await messageStream . unwrap ( ) . unwrap ( ) . close ( {
102
- signal
103
- } )
104
90
} catch ( err : any ) {
105
91
if ( peerConnection . connectionState !== 'connected' ) {
106
92
log . error ( 'error while handling signaling stream from peer %a' , connection . remoteAddr , err )
0 commit comments