@libp2p/webrtc 4.0.8 → 4.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  import { CodeError } from '@libp2p/interface'
2
- import { closeSource } from '@libp2p/utils/close-source'
3
- import { anySignal } from 'any-signal'
2
+ import pDefer from 'p-defer'
4
3
  import { isFirefox } from '../util.js'
5
4
  import { RTCIceCandidate } from '../webrtc/index.js'
6
5
  import { Message } from './pb/message.js'
@@ -12,32 +11,19 @@ export interface ReadCandidatesOptions extends AbortOptions, LoggerOptions {
12
11
  direction: string
13
12
  }
14
13
 
15
- export const readCandidatesUntilConnected = async (connectedPromise: DeferredPromise<void>, pc: RTCPeerConnection, stream: MessageStream<Message, Stream>, options: ReadCandidatesOptions): Promise<void> => {
16
- // if we connect, stop trying to read from the stream
17
- const controller = new AbortController()
18
- connectedPromise.promise.then(() => {
19
- controller.abort()
20
- }, () => {
21
- controller.abort()
22
- })
23
-
24
- const signal = anySignal([
25
- controller.signal,
26
- options.signal
27
- ])
28
-
29
- const abortListener = (): void => {
30
- closeSource(stream.unwrap().unwrap().source, options.log)
31
- }
32
-
33
- signal.addEventListener('abort', abortListener)
34
-
14
+ export const readCandidatesUntilConnected = async (pc: RTCPeerConnection, stream: MessageStream<Message, Stream>, options: ReadCandidatesOptions): Promise<void> => {
35
15
  try {
16
+ const connectedPromise: DeferredPromise<void> = pDefer()
17
+ resolveOnConnected(pc, connectedPromise)
18
+
36
19
  // read candidates until we are connected or we reach the end of the stream
37
20
  while (true) {
21
+ // if we connect, stop trying to read from the stream
38
22
  const message = await Promise.race([
39
23
  connectedPromise.promise,
40
- stream.read()
24
+ stream.read({
25
+ signal: options.signal
26
+ })
41
27
  ])
42
28
 
43
29
  // stream ended or we became connected
@@ -72,15 +58,20 @@ export const readCandidatesUntilConnected = async (connectedPromise: DeferredPro
72
58
  }
73
59
  } catch (err) {
74
60
  options.log.error('%s error parsing ICE candidate', options.direction, err)
75
- } finally {
76
- signal.removeEventListener('abort', abortListener)
77
- signal.clear()
61
+
62
+ if (options.signal?.aborted === true) {
63
+ throw err
64
+ }
78
65
  }
79
66
  }
80
67
 
81
- export function resolveOnConnected (pc: RTCPeerConnection, promise: DeferredPromise<void>): void {
68
+ function getConnectionState (pc: RTCPeerConnection): string {
69
+ return isFirefox ? pc.iceConnectionState : pc.connectionState
70
+ }
71
+
72
+ function resolveOnConnected (pc: RTCPeerConnection, promise: DeferredPromise<void>): void {
82
73
  pc[isFirefox ? 'oniceconnectionstatechange' : 'onconnectionstatechange'] = (_) => {
83
- switch (isFirefox ? pc.iceConnectionState : pc.connectionState) {
74
+ switch (getConnectionState(pc)) {
84
75
  case 'connected':
85
76
  promise.resolve()
86
77
  break