@libp2p/webrtc 3.2.10 → 3.2.11-05b52d69c
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/maconn.d.ts +6 -1
- package/dist/src/maconn.d.ts.map +1 -1
- package/dist/src/maconn.js +6 -6
- package/dist/src/maconn.js.map +1 -1
- package/dist/src/muxer.d.ts +12 -2
- package/dist/src/muxer.d.ts.map +1 -1
- package/dist/src/muxer.js +16 -7
- package/dist/src/muxer.js.map +1 -1
- package/dist/src/private-to-private/initiate-connection.d.ts +3 -2
- package/dist/src/private-to-private/initiate-connection.d.ts.map +1 -1
- package/dist/src/private-to-private/initiate-connection.js +3 -4
- package/dist/src/private-to-private/initiate-connection.js.map +1 -1
- package/dist/src/private-to-private/signaling-stream-handler.d.ts +3 -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 +3 -4
- package/dist/src/private-to-private/signaling-stream-handler.js.map +1 -1
- package/dist/src/private-to-private/transport.d.ts +3 -0
- package/dist/src/private-to-private/transport.d.ts.map +1 -1
- package/dist/src/private-to-private/transport.js +13 -11
- package/dist/src/private-to-private/transport.js.map +1 -1
- package/dist/src/private-to-private/util.d.ts +2 -1
- package/dist/src/private-to-private/util.d.ts.map +1 -1
- package/dist/src/private-to-private/util.js +5 -8
- package/dist/src/private-to-private/util.js.map +1 -1
- package/dist/src/private-to-public/sdp.d.ts +2 -1
- package/dist/src/private-to-public/sdp.d.ts.map +1 -1
- package/dist/src/private-to-public/sdp.js +3 -6
- package/dist/src/private-to-public/sdp.js.map +1 -1
- package/dist/src/private-to-public/transport.d.ts +3 -0
- package/dist/src/private-to-public/transport.d.ts.map +1 -1
- package/dist/src/private-to-public/transport.js +21 -10
- package/dist/src/private-to-public/transport.js.map +1 -1
- package/dist/src/stream.d.ts +3 -1
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +1 -2
- package/dist/src/stream.js.map +1 -1
- package/dist/src/util.d.ts +2 -1
- package/dist/src/util.d.ts.map +1 -1
- package/dist/src/util.js +4 -6
- package/dist/src/util.js.map +1 -1
- package/package.json +18 -17
- package/src/maconn.ts +11 -7
- package/src/muxer.ts +29 -8
- package/src/private-to-private/initiate-connection.ts +5 -6
- package/src/private-to-private/signaling-stream-handler.ts +5 -5
- package/src/private-to-private/transport.ts +15 -12
- package/src/private-to-private/util.ts +7 -10
- package/src/private-to-public/sdp.ts +4 -8
- package/src/private-to-public/transport.ts +23 -11
- package/src/stream.ts +6 -3
- package/src/util.ts +5 -7
- package/dist/typedoc-urls.json +0 -8
package/src/maconn.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
1
|
import { nopSink, nopSource } from './util.js'
|
|
2
|
+
import type { ComponentLogger, Logger } from '@libp2p/interface'
|
|
3
3
|
import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface/connection'
|
|
4
4
|
import type { CounterGroup } from '@libp2p/interface/metrics'
|
|
5
5
|
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
|
|
6
6
|
import type { Source, Sink } from 'it-stream-types'
|
|
7
7
|
|
|
8
|
-
const log = logger('libp2p:webrtc:maconn')
|
|
9
|
-
|
|
10
8
|
interface WebRTCMultiaddrConnectionInit {
|
|
11
9
|
/**
|
|
12
10
|
* WebRTC Peer Connection
|
|
@@ -29,7 +27,12 @@ interface WebRTCMultiaddrConnectionInit {
|
|
|
29
27
|
metrics?: CounterGroup
|
|
30
28
|
}
|
|
31
29
|
|
|
30
|
+
export interface WebRTCMultiaddrConnectionComponents {
|
|
31
|
+
logger: ComponentLogger
|
|
32
|
+
}
|
|
33
|
+
|
|
32
34
|
export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
35
|
+
private readonly log: Logger
|
|
33
36
|
/**
|
|
34
37
|
* WebRTC Peer Connection
|
|
35
38
|
*/
|
|
@@ -60,7 +63,8 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
60
63
|
*/
|
|
61
64
|
sink: Sink<Source<Uint8Array>, Promise<void>> = nopSink
|
|
62
65
|
|
|
63
|
-
constructor (init: WebRTCMultiaddrConnectionInit) {
|
|
66
|
+
constructor (components: WebRTCMultiaddrConnectionComponents, init: WebRTCMultiaddrConnectionInit) {
|
|
67
|
+
this.log = components.logger.forComponent('libp2p:webrtc:maconn')
|
|
64
68
|
this.remoteAddr = init.remoteAddr
|
|
65
69
|
this.timeline = init.timeline
|
|
66
70
|
this.peerConnection = init.peerConnection
|
|
@@ -68,7 +72,7 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
68
72
|
const initialState = this.peerConnection.connectionState
|
|
69
73
|
|
|
70
74
|
this.peerConnection.onconnectionstatechange = () => {
|
|
71
|
-
log.trace('peer connection state change', this.peerConnection.connectionState, 'initial state', initialState)
|
|
75
|
+
this.log.trace('peer connection state change', this.peerConnection.connectionState, 'initial state', initialState)
|
|
72
76
|
|
|
73
77
|
if (this.peerConnection.connectionState === 'disconnected' || this.peerConnection.connectionState === 'failed' || this.peerConnection.connectionState === 'closed') {
|
|
74
78
|
// nothing else to do but close the connection
|
|
@@ -78,7 +82,7 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
async close (options?: AbortOptions): Promise<void> {
|
|
81
|
-
log.trace('closing connection')
|
|
85
|
+
this.log.trace('closing connection')
|
|
82
86
|
|
|
83
87
|
this.peerConnection.close()
|
|
84
88
|
this.timeline.close = Date.now()
|
|
@@ -86,7 +90,7 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
86
90
|
}
|
|
87
91
|
|
|
88
92
|
abort (err: Error): void {
|
|
89
|
-
log.error('closing connection due to error', err)
|
|
93
|
+
this.log.error('closing connection due to error', err)
|
|
90
94
|
|
|
91
95
|
this.peerConnection.close()
|
|
92
96
|
this.timeline.close = Date.now()
|
package/src/muxer.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
1
|
import { createStream } from './stream.js'
|
|
3
2
|
import { drainAndClose, nopSink, nopSource } from './util.js'
|
|
4
3
|
import type { DataChannelOptions } from './index.js'
|
|
4
|
+
import type { ComponentLogger, Logger } from '@libp2p/interface'
|
|
5
5
|
import type { Stream } from '@libp2p/interface/connection'
|
|
6
6
|
import type { CounterGroup } from '@libp2p/interface/metrics'
|
|
7
7
|
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface/stream-muxer'
|
|
@@ -9,8 +9,6 @@ import type { AbortOptions } from '@multiformats/multiaddr'
|
|
|
9
9
|
import type { Source, Sink } from 'it-stream-types'
|
|
10
10
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
11
11
|
|
|
12
|
-
const log = logger('libp2p:webrtc:muxer')
|
|
13
|
-
|
|
14
12
|
const PROTOCOL = '/webrtc'
|
|
15
13
|
|
|
16
14
|
export interface DataChannelMuxerFactoryInit {
|
|
@@ -32,6 +30,10 @@ export interface DataChannelMuxerFactoryInit {
|
|
|
32
30
|
dataChannelOptions?: DataChannelOptions
|
|
33
31
|
}
|
|
34
32
|
|
|
33
|
+
export interface DataChannelMuxerFactoryComponents {
|
|
34
|
+
logger: ComponentLogger
|
|
35
|
+
}
|
|
36
|
+
|
|
35
37
|
interface BufferedStream {
|
|
36
38
|
stream: Stream
|
|
37
39
|
channel: RTCDataChannel
|
|
@@ -48,8 +50,10 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
|
|
|
48
50
|
private bufferedStreams: BufferedStream[] = []
|
|
49
51
|
private readonly metrics?: CounterGroup
|
|
50
52
|
private readonly dataChannelOptions?: DataChannelOptions
|
|
53
|
+
private readonly components: DataChannelMuxerFactoryComponents
|
|
51
54
|
|
|
52
|
-
constructor (init: DataChannelMuxerFactoryInit) {
|
|
55
|
+
constructor (components: DataChannelMuxerFactoryComponents, init: DataChannelMuxerFactoryInit) {
|
|
56
|
+
this.components = components
|
|
53
57
|
this.peerConnection = init.peerConnection
|
|
54
58
|
this.metrics = init.metrics
|
|
55
59
|
this.protocol = init.protocol ?? PROTOCOL
|
|
@@ -66,6 +70,7 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
|
|
|
66
70
|
onEnd: (err) => {
|
|
67
71
|
bufferedStream.onEnd(err)
|
|
68
72
|
},
|
|
73
|
+
logger: components.logger,
|
|
69
74
|
...this.dataChannelOptions
|
|
70
75
|
})
|
|
71
76
|
|
|
@@ -80,7 +85,7 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
|
|
|
80
85
|
}
|
|
81
86
|
|
|
82
87
|
createStreamMuxer (init?: StreamMuxerInit): StreamMuxer {
|
|
83
|
-
return new DataChannelMuxer({
|
|
88
|
+
return new DataChannelMuxer(this.components, {
|
|
84
89
|
...init,
|
|
85
90
|
peerConnection: this.peerConnection,
|
|
86
91
|
dataChannelOptions: this.dataChannelOptions,
|
|
@@ -95,6 +100,10 @@ export interface DataChannelMuxerInit extends DataChannelMuxerFactoryInit, Strea
|
|
|
95
100
|
streams: BufferedStream[]
|
|
96
101
|
}
|
|
97
102
|
|
|
103
|
+
export interface DataChannelMuxerComponents {
|
|
104
|
+
logger: ComponentLogger
|
|
105
|
+
}
|
|
106
|
+
|
|
98
107
|
/**
|
|
99
108
|
* A libp2p data channel stream muxer
|
|
100
109
|
*/
|
|
@@ -105,11 +114,15 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
105
114
|
public streams: Stream[]
|
|
106
115
|
public protocol: string
|
|
107
116
|
|
|
117
|
+
private readonly log: Logger
|
|
108
118
|
private readonly peerConnection: RTCPeerConnection
|
|
109
119
|
private readonly dataChannelOptions: DataChannelOptions
|
|
110
120
|
private readonly metrics?: CounterGroup
|
|
121
|
+
private readonly logger: ComponentLogger
|
|
111
122
|
|
|
112
|
-
constructor (readonly init: DataChannelMuxerInit) {
|
|
123
|
+
constructor (components: DataChannelMuxerComponents, readonly init: DataChannelMuxerInit) {
|
|
124
|
+
this.log = components.logger.forComponent('libp2p:webrtc:muxer')
|
|
125
|
+
this.logger = components.logger
|
|
113
126
|
this.streams = init.streams.map(s => s.stream)
|
|
114
127
|
this.peerConnection = init.peerConnection
|
|
115
128
|
this.protocol = init.protocol ?? PROTOCOL
|
|
@@ -129,6 +142,7 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
129
142
|
onEnd: () => {
|
|
130
143
|
this.#onStreamEnd(stream, channel)
|
|
131
144
|
},
|
|
145
|
+
logger: this.logger,
|
|
132
146
|
...this.dataChannelOptions
|
|
133
147
|
})
|
|
134
148
|
|
|
@@ -158,8 +172,14 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
158
172
|
}
|
|
159
173
|
|
|
160
174
|
#onStreamEnd (stream: Stream, channel: RTCDataChannel): void {
|
|
161
|
-
log.trace('stream %s %s %s onEnd', stream.direction, stream.id, stream.protocol)
|
|
162
|
-
drainAndClose(
|
|
175
|
+
this.log.trace('stream %s %s %s onEnd', stream.direction, stream.id, stream.protocol)
|
|
176
|
+
drainAndClose(
|
|
177
|
+
channel,
|
|
178
|
+
`${stream.direction} ${stream.id} ${stream.protocol}`,
|
|
179
|
+
this.dataChannelOptions.drainTimeout, {
|
|
180
|
+
log: this.log
|
|
181
|
+
}
|
|
182
|
+
)
|
|
163
183
|
this.streams = this.streams.filter(s => s.id !== stream.id)
|
|
164
184
|
this.metrics?.increment({ stream_end: true })
|
|
165
185
|
this.init?.onStreamEnd?.(stream)
|
|
@@ -206,6 +226,7 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
206
226
|
onEnd: () => {
|
|
207
227
|
this.#onStreamEnd(stream, channel)
|
|
208
228
|
},
|
|
229
|
+
logger: this.logger,
|
|
209
230
|
...this.dataChannelOptions
|
|
210
231
|
})
|
|
211
232
|
this.streams.push(stream)
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
3
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
4
3
|
import { pbStream } from 'it-protobuf-stream'
|
|
5
4
|
import pDefer, { type DeferredPromise } from 'p-defer'
|
|
@@ -8,21 +7,20 @@ import { Message } from './pb/message.js'
|
|
|
8
7
|
import { SIGNALING_PROTO_ID, splitAddr, type WebRTCTransportMetrics } from './transport.js'
|
|
9
8
|
import { readCandidatesUntilConnected, resolveOnConnected } from './util.js'
|
|
10
9
|
import type { DataChannelOptions } from '../index.js'
|
|
10
|
+
import type { LoggerOptions } from '@libp2p/interface'
|
|
11
11
|
import type { Connection } from '@libp2p/interface/connection'
|
|
12
12
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
13
13
|
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
|
14
14
|
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
15
15
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
16
16
|
|
|
17
|
-
const log = logger('libp2p:webrtc:initiate-connection')
|
|
18
|
-
|
|
19
17
|
export interface IncomingStreamOpts extends IncomingStreamData {
|
|
20
18
|
rtcConfiguration?: RTCConfiguration
|
|
21
19
|
dataChannelOptions?: Partial<DataChannelOptions>
|
|
22
20
|
signal: AbortSignal
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
export interface ConnectOptions {
|
|
23
|
+
export interface ConnectOptions extends LoggerOptions {
|
|
26
24
|
peerConnection: RTCPeerConnection
|
|
27
25
|
multiaddr: Multiaddr
|
|
28
26
|
connectionManager: ConnectionManager
|
|
@@ -32,7 +30,7 @@ export interface ConnectOptions {
|
|
|
32
30
|
metrics?: WebRTCTransportMetrics
|
|
33
31
|
}
|
|
34
32
|
|
|
35
|
-
export async function initiateConnection ({ peerConnection, signal, metrics, multiaddr: ma, connectionManager, transportManager }: ConnectOptions): Promise<{ remoteAddress: Multiaddr }> {
|
|
33
|
+
export async function initiateConnection ({ peerConnection, signal, metrics, multiaddr: ma, connectionManager, transportManager, log }: ConnectOptions): Promise<{ remoteAddress: Multiaddr }> {
|
|
36
34
|
const { baseAddr } = splitAddr(ma)
|
|
37
35
|
|
|
38
36
|
metrics?.dialerEvents.increment({ open: true })
|
|
@@ -147,7 +145,8 @@ export async function initiateConnection ({ peerConnection, signal, metrics, mul
|
|
|
147
145
|
|
|
148
146
|
await readCandidatesUntilConnected(connectedPromise, peerConnection, messageStream, {
|
|
149
147
|
direction: 'initiator',
|
|
150
|
-
signal
|
|
148
|
+
signal,
|
|
149
|
+
log
|
|
151
150
|
})
|
|
152
151
|
|
|
153
152
|
log.trace('initiator connected, closing init channel')
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
3
2
|
import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
4
3
|
import { pbStream } from 'it-protobuf-stream'
|
|
5
4
|
import pDefer, { type DeferredPromise } from 'p-defer'
|
|
6
5
|
import { type RTCPeerConnection, RTCSessionDescription } from '../webrtc/index.js'
|
|
7
6
|
import { Message } from './pb/message.js'
|
|
8
7
|
import { readCandidatesUntilConnected, resolveOnConnected } from './util.js'
|
|
8
|
+
import type { Logger } from '@libp2p/interface'
|
|
9
9
|
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
|
10
10
|
|
|
11
|
-
const log = logger('libp2p:webrtc:signaling-stream-handler')
|
|
12
|
-
|
|
13
11
|
export interface IncomingStreamOpts extends IncomingStreamData {
|
|
14
12
|
peerConnection: RTCPeerConnection
|
|
15
13
|
signal: AbortSignal
|
|
14
|
+
log: Logger
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
export async function handleIncomingStream ({ peerConnection, stream, signal, connection }: IncomingStreamOpts): Promise<{ remoteAddress: Multiaddr }> {
|
|
17
|
+
export async function handleIncomingStream ({ peerConnection, stream, signal, connection, log }: IncomingStreamOpts): Promise<{ remoteAddress: Multiaddr }> {
|
|
19
18
|
log.trace('new inbound signaling stream')
|
|
20
19
|
|
|
21
20
|
const messageStream = pbStream(stream).pb(Message)
|
|
@@ -104,7 +103,8 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
|
|
|
104
103
|
// wait until candidates are connected
|
|
105
104
|
await readCandidatesUntilConnected(connectedPromise, peerConnection, messageStream, {
|
|
106
105
|
direction: 'recipient',
|
|
107
|
-
signal
|
|
106
|
+
signal,
|
|
107
|
+
log
|
|
108
108
|
})
|
|
109
109
|
|
|
110
110
|
log.trace('recipient connected, closing signaling stream')
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { type CreateListenerOptions, type DialOptions, symbol, type Transport, type Listener, type Upgrader } from '@libp2p/interface/transport'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
4
3
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
5
4
|
import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
6
5
|
import { WebRTC } from '@multiformats/multiaddr-matcher'
|
|
@@ -12,6 +11,7 @@ import { initiateConnection } from './initiate-connection.js'
|
|
|
12
11
|
import { WebRTCPeerListener } from './listener.js'
|
|
13
12
|
import { handleIncomingStream } from './signaling-stream-handler.js'
|
|
14
13
|
import type { DataChannelOptions } from '../index.js'
|
|
14
|
+
import type { ComponentLogger, Logger } from '@libp2p/interface'
|
|
15
15
|
import type { Connection } from '@libp2p/interface/connection'
|
|
16
16
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
17
17
|
import type { CounterGroup, Metrics } from '@libp2p/interface/src/metrics/index.js'
|
|
@@ -20,8 +20,6 @@ import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/r
|
|
|
20
20
|
import type { ConnectionManager } from '@libp2p/interface-internal/src/connection-manager/index.js'
|
|
21
21
|
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
22
22
|
|
|
23
|
-
const log = logger('libp2p:webrtc:peer')
|
|
24
|
-
|
|
25
23
|
const WEBRTC_TRANSPORT = '/webrtc'
|
|
26
24
|
const CIRCUIT_RELAY_TRANSPORT = '/p2p-circuit'
|
|
27
25
|
export const SIGNALING_PROTO_ID = '/webrtc-signaling/0.0.1'
|
|
@@ -45,6 +43,7 @@ export interface WebRTCTransportComponents {
|
|
|
45
43
|
transportManager: TransportManager
|
|
46
44
|
connectionManager: ConnectionManager
|
|
47
45
|
metrics?: Metrics
|
|
46
|
+
logger: ComponentLogger
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
export interface WebRTCTransportMetrics {
|
|
@@ -53,6 +52,7 @@ export interface WebRTCTransportMetrics {
|
|
|
53
52
|
}
|
|
54
53
|
|
|
55
54
|
export class WebRTCTransport implements Transport, Startable {
|
|
55
|
+
private readonly log: Logger
|
|
56
56
|
private _started = false
|
|
57
57
|
private readonly metrics?: WebRTCTransportMetrics
|
|
58
58
|
private readonly shutdownController: AbortController
|
|
@@ -61,6 +61,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
61
61
|
private readonly components: WebRTCTransportComponents,
|
|
62
62
|
private readonly init: WebRTCTransportInit = {}
|
|
63
63
|
) {
|
|
64
|
+
this.log = components.logger.forComponent('libp2p:webrtc')
|
|
64
65
|
this.shutdownController = new AbortController()
|
|
65
66
|
|
|
66
67
|
if (components.metrics != null) {
|
|
@@ -83,7 +84,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
83
84
|
|
|
84
85
|
async start (): Promise<void> {
|
|
85
86
|
await this.components.registrar.handle(SIGNALING_PROTO_ID, (data: IncomingStreamData) => {
|
|
86
|
-
this._onProtocol(data).catch(err => { log.error('failed to handle incoming connect from %p', data.connection.remotePeer, err) })
|
|
87
|
+
this._onProtocol(data).catch(err => { this.log.error('failed to handle incoming connect from %p', data.connection.remotePeer, err) })
|
|
87
88
|
}, {
|
|
88
89
|
runOnTransientConnection: true
|
|
89
90
|
})
|
|
@@ -118,10 +119,10 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
118
119
|
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
|
|
119
120
|
*/
|
|
120
121
|
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
|
|
121
|
-
log.trace('dialing address: %a', ma)
|
|
122
|
+
this.log.trace('dialing address: %a', ma)
|
|
122
123
|
|
|
123
124
|
const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
|
|
124
|
-
const muxerFactory = new DataChannelMuxerFactory({
|
|
125
|
+
const muxerFactory = new DataChannelMuxerFactory(this.components, {
|
|
125
126
|
peerConnection,
|
|
126
127
|
dataChannelOptions: this.init.dataChannel
|
|
127
128
|
})
|
|
@@ -132,10 +133,11 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
132
133
|
dataChannelOptions: this.init.dataChannel,
|
|
133
134
|
signal: options.signal,
|
|
134
135
|
connectionManager: this.components.connectionManager,
|
|
135
|
-
transportManager: this.components.transportManager
|
|
136
|
+
transportManager: this.components.transportManager,
|
|
137
|
+
log: this.log
|
|
136
138
|
})
|
|
137
139
|
|
|
138
|
-
const webRTCConn = new WebRTCMultiaddrConnection({
|
|
140
|
+
const webRTCConn = new WebRTCMultiaddrConnection(this.components, {
|
|
139
141
|
peerConnection,
|
|
140
142
|
timeline: { open: Date.now() },
|
|
141
143
|
remoteAddr: remoteAddress,
|
|
@@ -157,7 +159,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
157
159
|
async _onProtocol ({ connection, stream }: IncomingStreamData): Promise<void> {
|
|
158
160
|
const signal = AbortSignal.timeout(this.init.inboundConnectionTimeout ?? INBOUND_CONNECTION_TIMEOUT)
|
|
159
161
|
const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
|
|
160
|
-
const muxerFactory = new DataChannelMuxerFactory({
|
|
162
|
+
const muxerFactory = new DataChannelMuxerFactory(this.components, {
|
|
161
163
|
peerConnection,
|
|
162
164
|
dataChannelOptions: this.init.dataChannel
|
|
163
165
|
})
|
|
@@ -167,10 +169,11 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
167
169
|
peerConnection,
|
|
168
170
|
connection,
|
|
169
171
|
stream,
|
|
170
|
-
signal
|
|
172
|
+
signal,
|
|
173
|
+
log: this.log
|
|
171
174
|
})
|
|
172
175
|
|
|
173
|
-
const webRTCConn = new WebRTCMultiaddrConnection({
|
|
176
|
+
const webRTCConn = new WebRTCMultiaddrConnection(this.components, {
|
|
174
177
|
peerConnection,
|
|
175
178
|
timeline: { open: (new Date()).getTime() },
|
|
176
179
|
remoteAddr: remoteAddress,
|
|
@@ -201,7 +204,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
201
204
|
const shutDownListener = (): void => {
|
|
202
205
|
webRTCConn.close()
|
|
203
206
|
.catch(err => {
|
|
204
|
-
log.error('could not close WebRTCMultiaddrConnection', err)
|
|
207
|
+
this.log.error('could not close WebRTCMultiaddrConnection', err)
|
|
205
208
|
})
|
|
206
209
|
}
|
|
207
210
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
3
2
|
import { abortableSource } from 'abortable-iterator'
|
|
4
3
|
import { anySignal } from 'any-signal'
|
|
5
4
|
import * as lp from 'it-length-prefixed'
|
|
@@ -7,13 +6,12 @@ import { AbortError, raceSignal } from 'race-signal'
|
|
|
7
6
|
import { isFirefox } from '../util.js'
|
|
8
7
|
import { RTCIceCandidate } from '../webrtc/index.js'
|
|
9
8
|
import { Message } from './pb/message.js'
|
|
9
|
+
import type { LoggerOptions } from '@libp2p/interface'
|
|
10
10
|
import type { Stream } from '@libp2p/interface/connection'
|
|
11
11
|
import type { AbortOptions, MessageStream } from 'it-protobuf-stream'
|
|
12
12
|
import type { DeferredPromise } from 'p-defer'
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export interface ReadCandidatesOptions extends AbortOptions {
|
|
14
|
+
export interface ReadCandidatesOptions extends AbortOptions, LoggerOptions {
|
|
17
15
|
direction: string
|
|
18
16
|
}
|
|
19
17
|
|
|
@@ -47,7 +45,7 @@ export const readCandidatesUntilConnected = async (connectedPromise: DeferredPro
|
|
|
47
45
|
let candidateInit = JSON.parse(message.data ?? 'null')
|
|
48
46
|
|
|
49
47
|
if (candidateInit === '') {
|
|
50
|
-
log.trace('end-of-candidates for this generation received')
|
|
48
|
+
options.log.trace('end-of-candidates for this generation received')
|
|
51
49
|
candidateInit = {
|
|
52
50
|
candidate: '',
|
|
53
51
|
sdpMid: '0',
|
|
@@ -56,7 +54,7 @@ export const readCandidatesUntilConnected = async (connectedPromise: DeferredPro
|
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
if (candidateInit === null) {
|
|
59
|
-
log.trace('end-of-candidates received')
|
|
57
|
+
options.log.trace('end-of-candidates received')
|
|
60
58
|
candidateInit = {
|
|
61
59
|
candidate: null,
|
|
62
60
|
sdpMid: '0',
|
|
@@ -68,16 +66,16 @@ export const readCandidatesUntilConnected = async (connectedPromise: DeferredPro
|
|
|
68
66
|
// see - https://www.w3.org/TR/webrtc/#rtcpeerconnectioniceevent
|
|
69
67
|
const candidate = new RTCIceCandidate(candidateInit)
|
|
70
68
|
|
|
71
|
-
log.trace('%s received new ICE candidate', options.direction, candidate)
|
|
69
|
+
options.log.trace('%s received new ICE candidate', options.direction, candidate)
|
|
72
70
|
|
|
73
71
|
try {
|
|
74
72
|
await pc.addIceCandidate(candidate)
|
|
75
73
|
} catch (err) {
|
|
76
|
-
log.error('%s bad candidate received', options.direction, err)
|
|
74
|
+
options.log.error('%s bad candidate received', options.direction, err)
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
77
|
} catch (err) {
|
|
80
|
-
log.error('%s error parsing ICE candidate', options.direction, err)
|
|
78
|
+
options.log.error('%s error parsing ICE candidate', options.direction, err)
|
|
81
79
|
} finally {
|
|
82
80
|
signal.clear()
|
|
83
81
|
}
|
|
@@ -95,7 +93,6 @@ export const readCandidatesUntilConnected = async (connectedPromise: DeferredPro
|
|
|
95
93
|
|
|
96
94
|
export function resolveOnConnected (pc: RTCPeerConnection, promise: DeferredPromise<void>): void {
|
|
97
95
|
pc[isFirefox ? 'oniceconnectionstatechange' : 'onconnectionstatechange'] = (_) => {
|
|
98
|
-
log.trace('receiver peerConnectionState state change: %s', pc.connectionState)
|
|
99
96
|
switch (isFirefox ? pc.iceConnectionState : pc.connectionState) {
|
|
100
97
|
case 'connected':
|
|
101
98
|
promise.resolve()
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
1
|
import { bases } from 'multiformats/basics'
|
|
3
2
|
import * as multihashes from 'multihashes'
|
|
4
3
|
import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithm } from '../error.js'
|
|
5
4
|
import { CERTHASH_CODE } from './transport.js'
|
|
5
|
+
import type { LoggerOptions } from '@libp2p/interface'
|
|
6
6
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
7
|
import type { HashCode, HashName } from 'multihashes'
|
|
8
8
|
|
|
9
|
-
const log = logger('libp2p:webrtc:sdp')
|
|
10
|
-
|
|
11
9
|
/**
|
|
12
10
|
* Get base2 | identity decoders
|
|
13
11
|
*/
|
|
14
12
|
// @ts-expect-error - Not easy to combine these types.
|
|
15
13
|
export const mbdecoder: any = Object.values(bases).map(b => b.decoder).reduce((d, b) => d.or(b))
|
|
16
14
|
|
|
17
|
-
export function getLocalFingerprint (pc: RTCPeerConnection): string | undefined {
|
|
15
|
+
export function getLocalFingerprint (pc: RTCPeerConnection, options: LoggerOptions): string | undefined {
|
|
18
16
|
// try to fetch fingerprint from local certificate
|
|
19
17
|
const localCert = pc.getConfiguration().certificates?.at(0)
|
|
20
18
|
if (localCert == null || localCert.getFingerprints == null) {
|
|
21
|
-
log.trace('fetching fingerprint from local SDP')
|
|
19
|
+
options.log.trace('fetching fingerprint from local SDP')
|
|
22
20
|
const localDescription = pc.localDescription
|
|
23
21
|
if (localDescription == null) {
|
|
24
22
|
return undefined
|
|
@@ -26,7 +24,7 @@ export function getLocalFingerprint (pc: RTCPeerConnection): string | undefined
|
|
|
26
24
|
return getFingerprintFromSdp(localDescription.sdp)
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
log.trace('fetching fingerprint from local certificate')
|
|
27
|
+
options.log.trace('fetching fingerprint from local certificate')
|
|
30
28
|
|
|
31
29
|
if (localCert.getFingerprints().length === 0) {
|
|
32
30
|
return undefined
|
|
@@ -55,8 +53,6 @@ function ipv (ma: Multiaddr): string {
|
|
|
55
53
|
}
|
|
56
54
|
}
|
|
57
55
|
|
|
58
|
-
log('Warning: multiaddr does not appear to contain IP4 or IP6, defaulting to IP6', ma)
|
|
59
|
-
|
|
60
56
|
return 'IP6'
|
|
61
57
|
}
|
|
62
58
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { noise as Noise } from '@chainsafe/libp2p-noise'
|
|
2
2
|
import { type CreateListenerOptions, symbol, type Transport, type Listener } from '@libp2p/interface/transport'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
4
3
|
import * as p from '@libp2p/peer-id'
|
|
5
4
|
import { protocols } from '@multiformats/multiaddr'
|
|
6
5
|
import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
|
|
@@ -17,13 +16,12 @@ import * as sdp from './sdp.js'
|
|
|
17
16
|
import { genUfrag } from './util.js'
|
|
18
17
|
import type { WebRTCDialOptions } from './options.js'
|
|
19
18
|
import type { DataChannelOptions } from '../index.js'
|
|
19
|
+
import type { ComponentLogger, Logger } from '@libp2p/interface'
|
|
20
20
|
import type { Connection } from '@libp2p/interface/connection'
|
|
21
21
|
import type { CounterGroup, Metrics } from '@libp2p/interface/metrics'
|
|
22
22
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
23
23
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
24
24
|
|
|
25
|
-
const log = logger('libp2p:webrtc:transport')
|
|
26
|
-
|
|
27
25
|
/**
|
|
28
26
|
* The time to wait, in milliseconds, for the data channel handshake to complete
|
|
29
27
|
*/
|
|
@@ -49,6 +47,7 @@ export const CERTHASH_CODE: number = protocols('certhash').code
|
|
|
49
47
|
export interface WebRTCDirectTransportComponents {
|
|
50
48
|
peerId: PeerId
|
|
51
49
|
metrics?: Metrics
|
|
50
|
+
logger: ComponentLogger
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
export interface WebRTCMetrics {
|
|
@@ -60,10 +59,12 @@ export interface WebRTCTransportDirectInit {
|
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
export class WebRTCDirectTransport implements Transport {
|
|
62
|
+
private readonly log: Logger
|
|
63
63
|
private readonly metrics?: WebRTCMetrics
|
|
64
64
|
private readonly components: WebRTCDirectTransportComponents
|
|
65
65
|
private readonly init: WebRTCTransportDirectInit
|
|
66
66
|
constructor (components: WebRTCDirectTransportComponents, init: WebRTCTransportDirectInit = {}) {
|
|
67
|
+
this.log = components.logger.forComponent('libp2p:webrtc-direct')
|
|
67
68
|
this.components = components
|
|
68
69
|
this.init = init
|
|
69
70
|
if (components.metrics != null) {
|
|
@@ -81,7 +82,7 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
81
82
|
*/
|
|
82
83
|
async dial (ma: Multiaddr, options: WebRTCDialOptions): Promise<Connection> {
|
|
83
84
|
const rawConn = await this._connect(ma, options)
|
|
84
|
-
log('dialing address: %a', ma)
|
|
85
|
+
this.log('dialing address: %a', ma)
|
|
85
86
|
return rawConn
|
|
86
87
|
}
|
|
87
88
|
|
|
@@ -144,7 +145,7 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
144
145
|
const handshakeDataChannel = peerConnection.createDataChannel('', { negotiated: true, id: 0 })
|
|
145
146
|
const handshakeTimeout = setTimeout(() => {
|
|
146
147
|
const error = `Data channel was never opened: state: ${handshakeDataChannel.readyState}`
|
|
147
|
-
log.error(error)
|
|
148
|
+
this.log.error(error)
|
|
148
149
|
this.metrics?.dialerEvents.increment({ open_error: true })
|
|
149
150
|
reject(dataChannelError('data', error))
|
|
150
151
|
}, HANDSHAKE_TIMEOUT_MS)
|
|
@@ -159,7 +160,7 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
159
160
|
clearTimeout(handshakeTimeout)
|
|
160
161
|
const errorTarget = event.target?.toString() ?? 'not specified'
|
|
161
162
|
const error = `Error opening a data channel for handshaking: ${errorTarget}`
|
|
162
|
-
log.error(error)
|
|
163
|
+
this.log.error(error)
|
|
163
164
|
// NOTE: We use unknown error here but this could potentially be considered a reset by some standards.
|
|
164
165
|
this.metrics?.dialerEvents.increment({ unknown_error: true })
|
|
165
166
|
reject(dataChannelError('data', error))
|
|
@@ -194,7 +195,12 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
194
195
|
// we pass in undefined for these parameters.
|
|
195
196
|
const noise = Noise({ prologueBytes: fingerprintsPrologue })()
|
|
196
197
|
|
|
197
|
-
const wrappedChannel = createStream({
|
|
198
|
+
const wrappedChannel = createStream({
|
|
199
|
+
channel: handshakeDataChannel,
|
|
200
|
+
direction: 'inbound',
|
|
201
|
+
logger: this.components.logger,
|
|
202
|
+
...(this.init.dataChannel ?? {})
|
|
203
|
+
})
|
|
198
204
|
const wrappedDuplex = {
|
|
199
205
|
...wrappedChannel,
|
|
200
206
|
sink: wrappedChannel.sink.bind(wrappedChannel),
|
|
@@ -209,7 +215,7 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
209
215
|
|
|
210
216
|
// Creating the connection before completion of the noise
|
|
211
217
|
// handshake ensures that the stream opening callback is set up
|
|
212
|
-
const maConn = new WebRTCMultiaddrConnection({
|
|
218
|
+
const maConn = new WebRTCMultiaddrConnection(this.components, {
|
|
213
219
|
peerConnection,
|
|
214
220
|
remoteAddr: ma,
|
|
215
221
|
timeline: {
|
|
@@ -226,7 +232,7 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
226
232
|
case 'disconnected':
|
|
227
233
|
case 'closed':
|
|
228
234
|
maConn.close().catch((err) => {
|
|
229
|
-
log.error('error closing connection', err)
|
|
235
|
+
this.log.error('error closing connection', err)
|
|
230
236
|
}).finally(() => {
|
|
231
237
|
// Remove the event listener once the connection is closed
|
|
232
238
|
controller.abort()
|
|
@@ -240,7 +246,11 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
240
246
|
// Track opened peer connection
|
|
241
247
|
this.metrics?.dialerEvents.increment({ peer_connection: true })
|
|
242
248
|
|
|
243
|
-
const muxerFactory = new DataChannelMuxerFactory(
|
|
249
|
+
const muxerFactory = new DataChannelMuxerFactory(this.components, {
|
|
250
|
+
peerConnection,
|
|
251
|
+
metrics: this.metrics?.dialerEvents,
|
|
252
|
+
dataChannelOptions: this.init.dataChannel
|
|
253
|
+
})
|
|
244
254
|
|
|
245
255
|
// For outbound connections, the remote is expected to start the noise handshake.
|
|
246
256
|
// Therefore, we need to secure an inbound noise connection from the remote.
|
|
@@ -262,7 +272,9 @@ export class WebRTCDirectTransport implements Transport {
|
|
|
262
272
|
throw invalidArgument('no local certificate')
|
|
263
273
|
}
|
|
264
274
|
|
|
265
|
-
const localFingerprint = sdp.getLocalFingerprint(pc
|
|
275
|
+
const localFingerprint = sdp.getLocalFingerprint(pc, {
|
|
276
|
+
log: this.log
|
|
277
|
+
})
|
|
266
278
|
if (localFingerprint == null) {
|
|
267
279
|
throw invalidArgument('no local fingerprint found')
|
|
268
280
|
}
|
package/src/stream.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { AbstractStream, type AbstractStreamInit } from '@libp2p/interface/stream-muxer/stream'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
4
3
|
import * as lengthPrefixed from 'it-length-prefixed'
|
|
5
4
|
import { type Pushable, pushable } from 'it-pushable'
|
|
6
5
|
import pDefer from 'p-defer'
|
|
@@ -10,7 +9,7 @@ import { raceSignal } from 'race-signal'
|
|
|
10
9
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
11
10
|
import { Message } from './pb/message.js'
|
|
12
11
|
import type { DataChannelOptions } from './index.js'
|
|
13
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
12
|
+
import type { AbortOptions, ComponentLogger } from '@libp2p/interface'
|
|
14
13
|
import type { Direction } from '@libp2p/interface/connection'
|
|
15
14
|
import type { DeferredPromise } from 'p-defer'
|
|
16
15
|
|
|
@@ -22,6 +21,8 @@ export interface WebRTCStreamInit extends AbstractStreamInit, DataChannelOptions
|
|
|
22
21
|
* {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel}
|
|
23
22
|
*/
|
|
24
23
|
channel: RTCDataChannel
|
|
24
|
+
|
|
25
|
+
logger: ComponentLogger
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -379,6 +380,8 @@ export interface WebRTCStreamOptions extends DataChannelOptions {
|
|
|
379
380
|
* A callback invoked when the channel ends
|
|
380
381
|
*/
|
|
381
382
|
onEnd?(err?: Error | undefined): void
|
|
383
|
+
|
|
384
|
+
logger: ComponentLogger
|
|
382
385
|
}
|
|
383
386
|
|
|
384
387
|
export function createStream (options: WebRTCStreamOptions): WebRTCStream {
|
|
@@ -386,7 +389,7 @@ export function createStream (options: WebRTCStreamOptions): WebRTCStream {
|
|
|
386
389
|
|
|
387
390
|
return new WebRTCStream({
|
|
388
391
|
id: direction === 'inbound' ? (`i${channel.id}`) : `r${channel.id}`,
|
|
389
|
-
log: logger(`libp2p:webrtc:stream:${direction}:${channel.id}`),
|
|
392
|
+
log: options.logger.forComponent(`libp2p:webrtc:stream:${direction}:${channel.id}`),
|
|
390
393
|
...options
|
|
391
394
|
})
|
|
392
395
|
}
|