@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.
- package/dist/index.min.js +12 -12
- package/dist/src/private-to-private/initiate-connection.d.ts.map +1 -1
- package/dist/src/private-to-private/initiate-connection.js +8 -15
- package/dist/src/private-to-private/initiate-connection.js.map +1 -1
- package/dist/src/private-to-private/signaling-stream-handler.d.ts.map +1 -1
- package/dist/src/private-to-private/signaling-stream-handler.js +16 -32
- package/dist/src/private-to-private/signaling-stream-handler.js.map +1 -1
- package/dist/src/private-to-private/transport.d.ts.map +1 -1
- package/dist/src/private-to-private/transport.js +2 -1
- package/dist/src/private-to-private/transport.js.map +1 -1
- package/dist/src/private-to-private/util.d.ts +1 -3
- package/dist/src/private-to-private/util.d.ts.map +1 -1
- package/dist/src/private-to-private/util.js +16 -25
- package/dist/src/private-to-private/util.js.map +1 -1
- package/package.json +6 -7
- package/src/private-to-private/initiate-connection.ts +8 -17
- package/src/private-to-private/signaling-stream-handler.ts +19 -43
- package/src/private-to-private/transport.ts +2 -1
- package/src/private-to-private/util.ts +19 -28
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface'
|
|
2
|
-
import
|
|
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 (
|
|
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
|
-
|
|
76
|
-
signal
|
|
77
|
-
|
|
61
|
+
|
|
62
|
+
if (options.signal?.aborted === true) {
|
|
63
|
+
throw err
|
|
64
|
+
}
|
|
78
65
|
}
|
|
79
66
|
}
|
|
80
67
|
|
|
81
|
-
|
|
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 (
|
|
74
|
+
switch (getConnectionState(pc)) {
|
|
84
75
|
case 'connected':
|
|
85
76
|
promise.resolve()
|
|
86
77
|
break
|