@libp2p/webrtc 3.2.1 → 3.2.2-62a56b54
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 +13 -13
- package/dist/src/index.d.ts +29 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/maconn.d.ts.map +1 -1
- package/dist/src/maconn.js +5 -2
- package/dist/src/maconn.js.map +1 -1
- package/dist/src/muxer.d.ts +10 -13
- package/dist/src/muxer.d.ts.map +1 -1
- package/dist/src/muxer.js +44 -29
- package/dist/src/muxer.js.map +1 -1
- package/dist/src/pb/message.d.ts +2 -1
- package/dist/src/pb/message.d.ts.map +1 -1
- package/dist/src/pb/message.js +2 -0
- package/dist/src/pb/message.js.map +1 -1
- package/dist/src/private-to-private/initiate-connection.d.ts +25 -0
- package/dist/src/private-to-private/initiate-connection.d.ts.map +1 -0
- package/dist/src/private-to-private/initiate-connection.js +145 -0
- package/dist/src/private-to-private/initiate-connection.js.map +1 -0
- package/dist/src/private-to-private/listener.d.ts +6 -2
- package/dist/src/private-to-private/listener.d.ts.map +1 -1
- package/dist/src/private-to-private/listener.js +6 -3
- package/dist/src/private-to-private/listener.js.map +1 -1
- package/dist/src/private-to-private/signaling-stream-handler.d.ts +10 -0
- package/dist/src/private-to-private/signaling-stream-handler.d.ts.map +1 -0
- package/dist/src/private-to-private/signaling-stream-handler.js +97 -0
- package/dist/src/private-to-private/signaling-stream-handler.js.map +1 -0
- package/dist/src/private-to-private/transport.d.ts +12 -2
- package/dist/src/private-to-private/transport.d.ts.map +1 -1
- package/dist/src/private-to-private/transport.js +67 -56
- package/dist/src/private-to-private/transport.js.map +1 -1
- package/dist/src/private-to-private/util.d.ts +6 -5
- package/dist/src/private-to-private/util.d.ts.map +1 -1
- package/dist/src/private-to-private/util.js +72 -21
- package/dist/src/private-to-private/util.js.map +1 -1
- package/dist/src/private-to-public/transport.d.ts +2 -2
- package/dist/src/private-to-public/transport.d.ts.map +1 -1
- package/dist/src/private-to-public/transport.js +2 -2
- package/dist/src/private-to-public/transport.js.map +1 -1
- package/dist/src/stream.d.ts +39 -19
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +135 -39
- package/dist/src/stream.js.map +1 -1
- package/dist/src/util.d.ts +6 -0
- package/dist/src/util.d.ts.map +1 -1
- package/dist/src/util.js +46 -0
- package/dist/src/util.js.map +1 -1
- package/package.json +17 -11
- package/src/index.ts +34 -0
- package/src/maconn.ts +7 -2
- package/src/muxer.ts +58 -44
- package/src/pb/message.proto +6 -1
- package/src/pb/message.ts +4 -2
- package/src/private-to-private/initiate-connection.ts +191 -0
- package/src/private-to-private/listener.ts +12 -4
- package/src/private-to-private/signaling-stream-handler.ts +129 -0
- package/src/private-to-private/transport.ts +87 -59
- package/src/private-to-private/util.ts +89 -24
- package/src/private-to-public/transport.ts +4 -4
- package/src/stream.ts +163 -61
- package/src/util.ts +60 -0
- package/dist/src/private-to-private/handler.d.ts +0 -26
- package/dist/src/private-to-private/handler.d.ts.map +0 -1
- package/dist/src/private-to-private/handler.js +0 -137
- package/dist/src/private-to-private/handler.js.map +0 -1
- package/dist/typedoc-urls.json +0 -6
- package/src/private-to-private/handler.ts +0 -177
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
3
|
-
import { abortableDuplex } from 'abortable-iterator'
|
|
4
|
-
import { pbStream } from 'it-protobuf-stream'
|
|
5
|
-
import pDefer, { type DeferredPromise } from 'p-defer'
|
|
6
|
-
import { DataChannelMuxerFactory } from '../muxer.js'
|
|
7
|
-
import { RTCPeerConnection, RTCSessionDescription } from '../webrtc/index.js'
|
|
8
|
-
import { Message } from './pb/message.js'
|
|
9
|
-
import { readCandidatesUntilConnected, resolveOnConnected } from './util.js'
|
|
10
|
-
import type { DataChannelOpts } from '../stream.js'
|
|
11
|
-
import type { Stream } from '@libp2p/interface/connection'
|
|
12
|
-
import type { StreamMuxerFactory } from '@libp2p/interface/stream-muxer'
|
|
13
|
-
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
|
14
|
-
|
|
15
|
-
const DEFAULT_TIMEOUT = 30 * 1000
|
|
16
|
-
|
|
17
|
-
const log = logger('libp2p:webrtc:peer')
|
|
18
|
-
|
|
19
|
-
export type IncomingStreamOpts = { rtcConfiguration?: RTCConfiguration, dataChannelOptions?: Partial<DataChannelOpts> } & IncomingStreamData
|
|
20
|
-
|
|
21
|
-
export async function handleIncomingStream ({ rtcConfiguration, dataChannelOptions, stream: rawStream }: IncomingStreamOpts): Promise<{ pc: RTCPeerConnection, muxerFactory: StreamMuxerFactory, remoteAddress: string }> {
|
|
22
|
-
const signal = AbortSignal.timeout(DEFAULT_TIMEOUT)
|
|
23
|
-
const stream = pbStream(abortableDuplex(rawStream, signal)).pb(Message)
|
|
24
|
-
const pc = new RTCPeerConnection(rtcConfiguration)
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
const muxerFactory = new DataChannelMuxerFactory({ peerConnection: pc, dataChannelOptions })
|
|
28
|
-
const connectedPromise: DeferredPromise<void> = pDefer()
|
|
29
|
-
const answerSentPromise: DeferredPromise<void> = pDefer()
|
|
30
|
-
|
|
31
|
-
signal.onabort = () => {
|
|
32
|
-
connectedPromise.reject(new CodeError('Timed out while trying to connect', 'ERR_TIMEOUT'))
|
|
33
|
-
}
|
|
34
|
-
// candidate callbacks
|
|
35
|
-
pc.onicecandidate = ({ candidate }) => {
|
|
36
|
-
answerSentPromise.promise.then(
|
|
37
|
-
async () => {
|
|
38
|
-
await stream.write({
|
|
39
|
-
type: Message.Type.ICE_CANDIDATE,
|
|
40
|
-
data: (candidate != null) ? JSON.stringify(candidate.toJSON()) : ''
|
|
41
|
-
})
|
|
42
|
-
},
|
|
43
|
-
(err) => {
|
|
44
|
-
log.error('cannot set candidate since sending answer failed', err)
|
|
45
|
-
connectedPromise.reject(err)
|
|
46
|
-
}
|
|
47
|
-
)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
resolveOnConnected(pc, connectedPromise)
|
|
51
|
-
|
|
52
|
-
// read an SDP offer
|
|
53
|
-
const pbOffer = await stream.read()
|
|
54
|
-
if (pbOffer.type !== Message.Type.SDP_OFFER) {
|
|
55
|
-
throw new Error(`expected message type SDP_OFFER, received: ${pbOffer.type ?? 'undefined'} `)
|
|
56
|
-
}
|
|
57
|
-
const offer = new RTCSessionDescription({
|
|
58
|
-
type: 'offer',
|
|
59
|
-
sdp: pbOffer.data
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
await pc.setRemoteDescription(offer).catch(err => {
|
|
63
|
-
log.error('could not execute setRemoteDescription', err)
|
|
64
|
-
throw new Error('Failed to set remoteDescription')
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
// create and write an SDP answer
|
|
68
|
-
const answer = await pc.createAnswer().catch(err => {
|
|
69
|
-
log.error('could not execute createAnswer', err)
|
|
70
|
-
answerSentPromise.reject(err)
|
|
71
|
-
throw new Error('Failed to create answer')
|
|
72
|
-
})
|
|
73
|
-
// write the answer to the remote
|
|
74
|
-
await stream.write({ type: Message.Type.SDP_ANSWER, data: answer.sdp })
|
|
75
|
-
|
|
76
|
-
await pc.setLocalDescription(answer).catch(err => {
|
|
77
|
-
log.error('could not execute setLocalDescription', err)
|
|
78
|
-
answerSentPromise.reject(err)
|
|
79
|
-
throw new Error('Failed to set localDescription')
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
answerSentPromise.resolve()
|
|
83
|
-
|
|
84
|
-
// wait until candidates are connected
|
|
85
|
-
await readCandidatesUntilConnected(connectedPromise, pc, stream)
|
|
86
|
-
|
|
87
|
-
const remoteAddress = parseRemoteAddress(pc.currentRemoteDescription?.sdp ?? '')
|
|
88
|
-
|
|
89
|
-
return { pc, muxerFactory, remoteAddress }
|
|
90
|
-
} catch (err) {
|
|
91
|
-
pc.close()
|
|
92
|
-
throw err
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export interface ConnectOptions {
|
|
97
|
-
stream: Stream
|
|
98
|
-
signal: AbortSignal
|
|
99
|
-
rtcConfiguration?: RTCConfiguration
|
|
100
|
-
dataChannelOptions?: Partial<DataChannelOpts>
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export async function initiateConnection ({ rtcConfiguration, dataChannelOptions, signal, stream: rawStream }: ConnectOptions): Promise<{ pc: RTCPeerConnection, muxerFactory: StreamMuxerFactory, remoteAddress: string }> {
|
|
104
|
-
const stream = pbStream(abortableDuplex(rawStream, signal)).pb(Message)
|
|
105
|
-
// setup peer connection
|
|
106
|
-
const pc = new RTCPeerConnection(rtcConfiguration)
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
const muxerFactory = new DataChannelMuxerFactory({ peerConnection: pc, dataChannelOptions })
|
|
110
|
-
|
|
111
|
-
const connectedPromise: DeferredPromise<void> = pDefer()
|
|
112
|
-
resolveOnConnected(pc, connectedPromise)
|
|
113
|
-
|
|
114
|
-
// reject the connectedPromise if the signal aborts
|
|
115
|
-
signal.onabort = connectedPromise.reject
|
|
116
|
-
// we create the channel so that the peerconnection has a component for which
|
|
117
|
-
// to collect candidates. The label is not relevant to connection initiation
|
|
118
|
-
// but can be useful for debugging
|
|
119
|
-
const channel = pc.createDataChannel('init')
|
|
120
|
-
// setup callback to write ICE candidates to the remote
|
|
121
|
-
// peer
|
|
122
|
-
pc.onicecandidate = ({ candidate }) => {
|
|
123
|
-
void stream.write({
|
|
124
|
-
type: Message.Type.ICE_CANDIDATE,
|
|
125
|
-
data: (candidate != null) ? JSON.stringify(candidate.toJSON()) : ''
|
|
126
|
-
})
|
|
127
|
-
.catch(err => {
|
|
128
|
-
log.error('error sending ICE candidate', err)
|
|
129
|
-
})
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// create an offer
|
|
133
|
-
const offerSdp = await pc.createOffer()
|
|
134
|
-
// write the offer to the stream
|
|
135
|
-
await stream.write({ type: Message.Type.SDP_OFFER, data: offerSdp.sdp })
|
|
136
|
-
// set offer as local description
|
|
137
|
-
await pc.setLocalDescription(offerSdp).catch(err => {
|
|
138
|
-
log.error('could not execute setLocalDescription', err)
|
|
139
|
-
throw new Error('Failed to set localDescription')
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
// read answer
|
|
143
|
-
const answerMessage = await stream.read()
|
|
144
|
-
if (answerMessage.type !== Message.Type.SDP_ANSWER) {
|
|
145
|
-
throw new Error('remote should send an SDP answer')
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const answerSdp = new RTCSessionDescription({ type: 'answer', sdp: answerMessage.data })
|
|
149
|
-
await pc.setRemoteDescription(answerSdp).catch(err => {
|
|
150
|
-
log.error('could not execute setRemoteDescription', err)
|
|
151
|
-
throw new Error('Failed to set remoteDescription')
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
await readCandidatesUntilConnected(connectedPromise, pc, stream)
|
|
155
|
-
channel.close()
|
|
156
|
-
|
|
157
|
-
const remoteAddress = parseRemoteAddress(pc.currentRemoteDescription?.sdp ?? '')
|
|
158
|
-
|
|
159
|
-
return { pc, muxerFactory, remoteAddress }
|
|
160
|
-
} catch (err) {
|
|
161
|
-
pc.close()
|
|
162
|
-
throw err
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function parseRemoteAddress (sdp: string): string {
|
|
167
|
-
// 'a=candidate:1746876089 1 udp 2113937151 0614fbad-b...ocal 54882 typ host generation 0 network-cost 999'
|
|
168
|
-
const candidateLine = sdp.split('\r\n').filter(line => line.startsWith('a=candidate')).pop()
|
|
169
|
-
const candidateParts = candidateLine?.split(' ')
|
|
170
|
-
|
|
171
|
-
if (candidateLine == null || candidateParts == null || candidateParts.length < 5) {
|
|
172
|
-
log('could not parse remote address from', candidateLine)
|
|
173
|
-
return '/webrtc'
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return `/dnsaddr/${candidateParts[4]}/${candidateParts[2].toLowerCase()}/${candidateParts[5]}/webrtc`
|
|
177
|
-
}
|