@libp2p/webrtc 6.0.2-334f91bdc → 6.0.2-39e2e541a

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.
Files changed (55) hide show
  1. package/dist/index.min.js +6 -6
  2. package/dist/index.min.js.map +3 -3
  3. package/dist/src/muxer.d.ts +0 -8
  4. package/dist/src/muxer.d.ts.map +1 -1
  5. package/dist/src/muxer.js +15 -38
  6. package/dist/src/muxer.js.map +1 -1
  7. package/dist/src/private-to-private/initiate-connection.d.ts +2 -1
  8. package/dist/src/private-to-private/initiate-connection.d.ts.map +1 -1
  9. package/dist/src/private-to-private/initiate-connection.js +4 -5
  10. package/dist/src/private-to-private/initiate-connection.js.map +1 -1
  11. package/dist/src/private-to-private/signaling-stream-handler.js +4 -4
  12. package/dist/src/private-to-private/signaling-stream-handler.js.map +1 -1
  13. package/dist/src/private-to-private/transport.d.ts.map +1 -1
  14. package/dist/src/private-to-private/transport.js +3 -5
  15. package/dist/src/private-to-private/transport.js.map +1 -1
  16. package/dist/src/private-to-public/listener.d.ts +0 -1
  17. package/dist/src/private-to-public/listener.d.ts.map +1 -1
  18. package/dist/src/private-to-public/listener.js +4 -9
  19. package/dist/src/private-to-public/listener.js.map +1 -1
  20. package/dist/src/private-to-public/transport.d.ts.map +1 -1
  21. package/dist/src/private-to-public/transport.js +3 -5
  22. package/dist/src/private-to-public/transport.js.map +1 -1
  23. package/dist/src/private-to-public/utils/connect.d.ts +4 -4
  24. package/dist/src/private-to-public/utils/connect.d.ts.map +1 -1
  25. package/dist/src/private-to-public/utils/connect.js +9 -7
  26. package/dist/src/private-to-public/utils/connect.js.map +1 -1
  27. package/dist/src/private-to-public/utils/get-rtcpeerconnection.browser.d.ts +1 -6
  28. package/dist/src/private-to-public/utils/get-rtcpeerconnection.browser.d.ts.map +1 -1
  29. package/dist/src/private-to-public/utils/get-rtcpeerconnection.browser.js +3 -15
  30. package/dist/src/private-to-public/utils/get-rtcpeerconnection.browser.js.map +1 -1
  31. package/dist/src/private-to-public/utils/get-rtcpeerconnection.d.ts +2 -17
  32. package/dist/src/private-to-public/utils/get-rtcpeerconnection.d.ts.map +1 -1
  33. package/dist/src/private-to-public/utils/get-rtcpeerconnection.js +7 -18
  34. package/dist/src/private-to-public/utils/get-rtcpeerconnection.js.map +1 -1
  35. package/dist/src/rtcpeerconnection-to-conn.d.ts +1 -0
  36. package/dist/src/rtcpeerconnection-to-conn.d.ts.map +1 -1
  37. package/dist/src/rtcpeerconnection-to-conn.js.map +1 -1
  38. package/dist/src/stream.d.ts.map +1 -1
  39. package/dist/src/stream.js +17 -25
  40. package/dist/src/stream.js.map +1 -1
  41. package/dist/src/util.js +1 -1
  42. package/dist/src/util.js.map +1 -1
  43. package/package.json +8 -8
  44. package/src/muxer.ts +15 -50
  45. package/src/private-to-private/initiate-connection.ts +5 -6
  46. package/src/private-to-private/signaling-stream-handler.ts +4 -4
  47. package/src/private-to-private/transport.ts +4 -6
  48. package/src/private-to-public/listener.ts +4 -10
  49. package/src/private-to-public/transport.ts +3 -8
  50. package/src/private-to-public/utils/connect.ts +14 -11
  51. package/src/private-to-public/utils/get-rtcpeerconnection.browser.ts +3 -20
  52. package/src/private-to-public/utils/get-rtcpeerconnection.ts +8 -31
  53. package/src/rtcpeerconnection-to-conn.ts +1 -0
  54. package/src/stream.ts +18 -26
  55. package/src/util.ts +1 -1
@@ -69,13 +69,13 @@ export async function handleIncomingStream (stream: Stream, connection: Connecti
69
69
  })
70
70
 
71
71
  await peerConnection.setRemoteDescription(offer).catch(err => {
72
- log.error('could not execute setRemoteDescription - %e', err)
72
+ log.error('could not execute setRemoteDescription', err)
73
73
  throw new SDPHandshakeFailedError('Failed to set remoteDescription')
74
74
  })
75
75
 
76
76
  // create and write an SDP answer
77
77
  const answer = await peerConnection.createAnswer().catch(err => {
78
- log.error('could not execute createAnswer - %e', err)
78
+ log.error('could not execute createAnswer', err)
79
79
  throw new SDPHandshakeFailedError('Failed to create answer')
80
80
  })
81
81
 
@@ -87,7 +87,7 @@ export async function handleIncomingStream (stream: Stream, connection: Connecti
87
87
  })
88
88
 
89
89
  await peerConnection.setLocalDescription(answer).catch(err => {
90
- log.error('could not execute setLocalDescription - %e', err)
90
+ log.error('could not execute setLocalDescription', err)
91
91
  throw new SDPHandshakeFailedError('Failed to set localDescription')
92
92
  })
93
93
 
@@ -101,7 +101,7 @@ export async function handleIncomingStream (stream: Stream, connection: Connecti
101
101
  })
102
102
  } catch (err: any) {
103
103
  if (peerConnection.connectionState !== 'connected') {
104
- log.error('error while handling signaling stream from peer %a - %e', connection.remoteAddr, err)
104
+ log.error('error while handling signaling stream from peer %a', connection.remoteAddr, err)
105
105
 
106
106
  peerConnection.close()
107
107
  throw err
@@ -114,7 +114,7 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
114
114
 
115
115
  this._onProtocol(stream, connection, signal)
116
116
  .catch(err => {
117
- this.log.error('failed to handle incoming connect from %p - %e', connection.remotePeer, err)
117
+ this.log.error('failed to handle incoming connect from %p', connection.remotePeer, err)
118
118
  })
119
119
  .finally(() => {
120
120
  signal.clear()
@@ -229,7 +229,6 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
229
229
  })
230
230
 
231
231
  const webRTCConn = toMultiaddrConnection({
232
- // @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
233
232
  peerConnection,
234
233
  remoteAddr: remoteAddress,
235
234
  metrics: this.metrics?.listenerEvents,
@@ -246,10 +245,9 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
246
245
  })
247
246
 
248
247
  // close the connection on shut down
249
- // @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
250
248
  this._closeOnShutdown(peerConnection, webRTCConn)
251
249
  } catch (err: any) {
252
- this.log.error('incoming signaling error - %e', err)
250
+ this.log.error('incoming signaling error', err)
253
251
 
254
252
  peerConnection.close()
255
253
  stream.abort(err)
@@ -257,12 +255,12 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
257
255
  }
258
256
  }
259
257
 
260
- private _closeOnShutdown (pc: globalThis.RTCPeerConnection, webRTCConn: MultiaddrConnection): void {
258
+ private _closeOnShutdown (pc: RTCPeerConnection, webRTCConn: MultiaddrConnection): void {
261
259
  // close the connection on shut down
262
260
  const shutDownListener = (): void => {
263
261
  webRTCConn.close()
264
262
  .catch(err => {
265
- this.log.error('could not close WebRTCMultiaddrConnection - %e', err)
263
+ this.log.error('could not close WebRTCMultiaddrConnection', err)
266
264
  })
267
265
  }
268
266
 
@@ -27,7 +27,6 @@ export interface WebRTCDirectListenerComponents {
27
27
  keychain?: Keychain
28
28
  datastore: Datastore
29
29
  metrics?: Metrics
30
- events?: CounterGroup
31
30
  }
32
31
 
33
32
  export interface WebRTCDirectListenerInit {
@@ -160,7 +159,7 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
160
159
 
161
160
  this.incomingConnection(ufrag, remoteHost, remotePort, signal)
162
161
  .catch(err => {
163
- this.log.error('error processing incoming STUN request - %e', err)
162
+ this.log.error('error processing incoming STUN request', err)
164
163
  })
165
164
  .finally(() => {
166
165
  signal.clear()
@@ -185,13 +184,7 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
185
184
  signal.throwIfAborted()
186
185
 
187
186
  // https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md#browser-to-public-server
188
- const results = await createDialerRTCPeerConnection('server', ufrag, {
189
- rtcConfiguration: this.init.rtcConfiguration,
190
- certificate: this.certificate,
191
- events: this.metrics?.listenerEvents,
192
- dataChannel: this.init.dataChannel
193
- })
194
- peerConnection = results.peerConnection
187
+ peerConnection = await createDialerRTCPeerConnection('server', ufrag, this.init.rtcConfiguration, this.certificate)
195
188
 
196
189
  this.connections.set(key, peerConnection)
197
190
 
@@ -208,10 +201,11 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
208
201
  })
209
202
 
210
203
  try {
211
- await connect(peerConnection, results.muxerFactory, ufrag, {
204
+ await connect(peerConnection, ufrag, {
212
205
  role: 'server',
213
206
  log: this.log,
214
207
  logger: this.components.logger,
208
+ metrics: this.components.metrics,
215
209
  events: this.metrics?.listenerEvents,
216
210
  signal,
217
211
  remoteAddr: multiaddr(`/ip${isIPv4(remoteHost) ? 4 : 6}/${remoteHost}/udp/${remotePort}/webrtc-direct`),
@@ -163,19 +163,14 @@ export class WebRTCDirectTransport implements Transport, Startable {
163
163
  const ufrag = genUfrag()
164
164
 
165
165
  // https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md#browser-to-public-server
166
- const {
167
- peerConnection,
168
- muxerFactory
169
- } = await createDialerRTCPeerConnection('client', ufrag, {
170
- rtcConfiguration: typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration ?? {},
171
- dataChannel: this.init.dataChannel
172
- })
166
+ const peerConnection = await createDialerRTCPeerConnection('client', ufrag, typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration ?? {})
173
167
 
174
168
  try {
175
- return await connect(peerConnection, muxerFactory, ufrag, {
169
+ return await connect(peerConnection, ufrag, {
176
170
  role: 'client',
177
171
  log: this.log,
178
172
  logger: this.components.logger,
173
+ metrics: this.components.metrics,
179
174
  events: this.metrics?.dialerEvents,
180
175
  signal: options.signal,
181
176
  remoteAddr: ma,
@@ -9,12 +9,13 @@ import { generateNoisePrologue } from './generate-noise-prologue.js'
9
9
  import * as sdp from './sdp.js'
10
10
  import type { DirectRTCPeerConnection } from './get-rtcpeerconnection.js'
11
11
  import type { DataChannelOptions } from '../../index.js'
12
- import type { ComponentLogger, Connection, CounterGroup, Logger, PeerId, PrivateKey, Upgrader } from '@libp2p/interface'
12
+ import type { ComponentLogger, Connection, CounterGroup, Logger, Metrics, PeerId, PrivateKey, Upgrader } from '@libp2p/interface'
13
13
  import type { Multiaddr } from '@multiformats/multiaddr'
14
14
 
15
15
  export interface ConnectOptions {
16
16
  log: Logger
17
17
  logger: ComponentLogger
18
+ metrics?: Metrics
18
19
  events?: CounterGroup
19
20
  remoteAddr: Multiaddr
20
21
  role: 'client' | 'server'
@@ -36,13 +37,9 @@ export interface ServerOptions extends ConnectOptions {
36
37
 
37
38
  const CONNECTION_STATE_CHANGE_EVENT = isFirefox ? 'iceconnectionstatechange' : 'connectionstatechange'
38
39
 
39
- function isServer (options: ClientOptions | ServerOptions, peerConnection: any): peerConnection is DirectRTCPeerConnection {
40
- return options.role === 'server'
41
- }
42
-
43
- export async function connect (peerConnection: RTCPeerConnection, muxerFactory: DataChannelMuxerFactory, ufrag: string, options: ClientOptions): Promise<Connection>
44
- export async function connect (peerConnection: DirectRTCPeerConnection, muxerFactory: DataChannelMuxerFactory, ufrag: string, options: ServerOptions): Promise<void>
45
- export async function connect (peerConnection: RTCPeerConnection | DirectRTCPeerConnection, muxerFactory: DataChannelMuxerFactory, ufrag: string, options: ClientOptions | ServerOptions): Promise<any> {
40
+ export async function connect (peerConnection: DirectRTCPeerConnection, ufrag: string, options: ClientOptions): Promise<Connection>
41
+ export async function connect (peerConnection: DirectRTCPeerConnection, ufrag: string, options: ServerOptions): Promise<void>
42
+ export async function connect (peerConnection: DirectRTCPeerConnection, ufrag: string, options: ConnectOptions): Promise<any> {
46
43
  // create data channel for running the noise handshake. Once the data
47
44
  // channel is opened, the listener will initiate the noise handshake. This
48
45
  // is used to confirm the identity of the peer.
@@ -91,7 +88,7 @@ export async function connect (peerConnection: RTCPeerConnection | DirectRTCPeer
91
88
 
92
89
  options.log.trace('%s handshake channel opened', options.role)
93
90
 
94
- if (isServer(options, peerConnection)) {
91
+ if (options.role === 'server') {
95
92
  // now that the connection has been opened, add the remote's certhash to
96
93
  // it's multiaddr so we can complete the noise handshake
97
94
  const remoteFingerprint = peerConnection.remoteFingerprint()?.value ?? ''
@@ -127,7 +124,6 @@ export async function connect (peerConnection: RTCPeerConnection | DirectRTCPeer
127
124
  // Creating the connection before completion of the noise
128
125
  // handshake ensures that the stream opening callback is set up
129
126
  const maConn = toMultiaddrConnection({
130
- // @ts-expect-error types are broken
131
127
  peerConnection,
132
128
  remoteAddr: options.remoteAddr,
133
129
  metrics: options.events,
@@ -141,7 +137,7 @@ export async function connect (peerConnection: RTCPeerConnection | DirectRTCPeer
141
137
  case 'disconnected':
142
138
  case 'closed':
143
139
  maConn.close().catch((err) => {
144
- options.log.error('error closing connection - %e', err)
140
+ options.log.error('error closing connection', err)
145
141
  maConn.abort(err)
146
142
  })
147
143
  break
@@ -153,6 +149,13 @@ export async function connect (peerConnection: RTCPeerConnection | DirectRTCPeer
153
149
  // Track opened peer connection
154
150
  options.events?.increment({ peer_connection: true })
155
151
 
152
+ const muxerFactory = new DataChannelMuxerFactory({
153
+ // @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
154
+ peerConnection,
155
+ metrics: options.events,
156
+ dataChannelOptions: options.dataChannel
157
+ })
158
+
156
159
  if (options.role === 'client') {
157
160
  // For outbound connections, the remote is expected to start the noise
158
161
  // handshake. Therefore, we need to secure an inbound noise connection
@@ -1,10 +1,4 @@
1
- import { DataChannelMuxerFactory } from '../../muxer.ts'
2
- import type { CreateDialerRTCPeerConnectionOptions } from './get-rtcpeerconnection.ts'
3
-
4
- export async function createDialerRTCPeerConnection (role: 'client' | 'server', ufrag: string, options: CreateDialerRTCPeerConnectionOptions = {}): Promise<{ peerConnection: RTCPeerConnection, muxerFactory: DataChannelMuxerFactory }> {
5
- // @ts-expect-error options type is wrong
6
- let certificate: RTCCertificate = options.certificate
7
-
1
+ export async function createDialerRTCPeerConnection (role: 'client' | 'server', ufrag: string, rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>), certificate?: RTCCertificate): Promise<RTCPeerConnection> {
8
2
  if (certificate == null) {
9
3
  // ECDSA is preferred over RSA here. From our testing we find that P-256 elliptic
10
4
  // curve is supported by Pion, webrtc-rs, as well as Chromium (P-228 and P-384
@@ -19,21 +13,10 @@ export async function createDialerRTCPeerConnection (role: 'client' | 'server',
19
13
  })
20
14
  }
21
15
 
22
- const rtcConfig = typeof options.rtcConfiguration === 'function' ? await options.rtcConfiguration() : options.rtcConfiguration
16
+ const rtcConfig = typeof rtcConfiguration === 'function' ? await rtcConfiguration() : rtcConfiguration
23
17
 
24
- const peerConnection = new RTCPeerConnection({
18
+ return new RTCPeerConnection({
25
19
  ...(rtcConfig ?? {}),
26
20
  certificates: [certificate]
27
21
  })
28
-
29
- const muxerFactory = new DataChannelMuxerFactory({
30
- peerConnection,
31
- metrics: options.events,
32
- dataChannelOptions: options.dataChannel
33
- })
34
-
35
- return {
36
- peerConnection,
37
- muxerFactory
38
- }
39
22
  }
@@ -2,10 +2,8 @@ import { Crypto } from '@peculiar/webcrypto'
2
2
  import { PeerConnection } from 'node-datachannel'
3
3
  import { RTCPeerConnection } from 'node-datachannel/polyfill'
4
4
  import { DEFAULT_ICE_SERVERS, MAX_MESSAGE_SIZE } from '../../constants.js'
5
- import { DataChannelMuxerFactory } from '../../muxer.ts'
6
5
  import { generateTransportCertificate } from './generate-certificates.js'
7
- import type { DataChannelOptions, TransportCertificate } from '../../index.js'
8
- import type { CounterGroup } from '@libp2p/interface'
6
+ import type { TransportCertificate } from '../../index.js'
9
7
  import type { CertificateFingerprint } from 'node-datachannel'
10
8
 
11
9
  const crypto = new Crypto()
@@ -87,17 +85,8 @@ function mapIceServers (iceServers?: RTCIceServer[]): string[] {
87
85
  .flat() ?? []
88
86
  }
89
87
 
90
- export interface CreateDialerRTCPeerConnectionOptions {
91
- rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
92
- certificate?: TransportCertificate
93
- events?: CounterGroup
94
- dataChannel?: DataChannelOptions
95
- }
96
-
97
- export async function createDialerRTCPeerConnection (role: 'client', ufrag: string, options?: CreateDialerRTCPeerConnectionOptions): Promise<{ peerConnection: globalThis.RTCPeerConnection, muxerFactory: DataChannelMuxerFactory }>
98
- export async function createDialerRTCPeerConnection (role: 'server', ufrag: string, options?: CreateDialerRTCPeerConnectionOptions): Promise<{ peerConnection: DirectRTCPeerConnection, muxerFactory: DataChannelMuxerFactory }>
99
- export async function createDialerRTCPeerConnection (role: 'client' | 'server', ufrag: string, options: CreateDialerRTCPeerConnectionOptions = {}): Promise<{ peerConnection: globalThis.RTCPeerConnection | DirectRTCPeerConnection, muxerFactory: DataChannelMuxerFactory }> {
100
- if (options.certificate == null) {
88
+ export async function createDialerRTCPeerConnection (role: 'client' | 'server', ufrag: string, rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>), certificate?: TransportCertificate): Promise<DirectRTCPeerConnection> {
89
+ if (certificate == null) {
101
90
  // ECDSA is preferred over RSA here. From our testing we find that P-256
102
91
  // elliptic curve is supported by Pion, webrtc-rs, as well as Chromium
103
92
  // (P-228 and P-384 was not supported in Chromium). We use the same hash
@@ -107,36 +96,24 @@ export async function createDialerRTCPeerConnection (role: 'client' | 'server',
107
96
  namedCurve: 'P-256'
108
97
  }, true, ['sign', 'verify'])
109
98
 
110
- options.certificate = await generateTransportCertificate(keyPair, {
99
+ certificate = await generateTransportCertificate(keyPair, {
111
100
  days: 365
112
101
  })
113
102
  }
114
103
 
115
- const rtcConfig = typeof options.rtcConfiguration === 'function' ? await options.rtcConfiguration() : options.rtcConfiguration
104
+ const rtcConfig = typeof rtcConfiguration === 'function' ? await rtcConfiguration() : rtcConfiguration
116
105
 
117
- const peerConnection = new DirectRTCPeerConnection({
106
+ return new DirectRTCPeerConnection({
118
107
  ...rtcConfig,
119
108
  ufrag,
120
109
  peerConnection: new PeerConnection(`${role}-${Date.now()}`, {
121
110
  disableFingerprintVerification: true,
122
111
  disableAutoNegotiation: true,
123
- certificatePemFile: options.certificate.pem,
124
- keyPemFile: options.certificate.privateKey,
112
+ certificatePemFile: certificate.pem,
113
+ keyPemFile: certificate.privateKey,
125
114
  enableIceUdpMux: role === 'server',
126
115
  maxMessageSize: MAX_MESSAGE_SIZE,
127
116
  iceServers: mapIceServers(rtcConfig?.iceServers ?? DEFAULT_ICE_SERVERS.map(urls => ({ urls })))
128
117
  })
129
118
  })
130
-
131
- const muxerFactory = new DataChannelMuxerFactory({
132
- // @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
133
- peerConnection,
134
- metrics: options.events,
135
- dataChannelOptions: options.dataChannel
136
- })
137
-
138
- return {
139
- peerConnection,
140
- muxerFactory
141
- }
142
119
  }
@@ -1,4 +1,5 @@
1
1
  import { AbstractMultiaddrConnection } from '@libp2p/utils'
2
+ import type { RTCPeerConnection } from './webrtc/index.js'
2
3
  import type { AbortOptions, MultiaddrConnection } from '@libp2p/interface'
3
4
  import type { AbstractMultiaddrConnectionInit, SendResult } from '@libp2p/utils'
4
5
  import type { Uint8ArrayList } from 'uint8arraylist'
package/src/stream.ts CHANGED
@@ -88,6 +88,23 @@ export class WebRTCStream extends AbstractStream {
88
88
  }
89
89
  }
90
90
 
91
+ if (this.channel.readyState !== 'open') {
92
+ this.log('channel ready state is "%s" and not "open", waiting for "open" event before sending data', this.channel.readyState)
93
+ pEvent(this.channel, 'open', {
94
+ rejectionEvents: [
95
+ 'close',
96
+ 'error'
97
+ ]
98
+ })
99
+ .then(() => {
100
+ this.log('channel ready state is now "%s", dispatching drain', this.channel.readyState)
101
+ this.safeDispatchEvent('drain')
102
+ })
103
+ .catch(err => {
104
+ this.abort(err.error ?? err)
105
+ })
106
+ }
107
+
91
108
  // pipe framed protobuf messages through a length prefixed decoder, and
92
109
  // surface data from the `Message.message` field through a source.
93
110
  Promise.resolve().then(async () => {
@@ -96,7 +113,7 @@ export class WebRTCStream extends AbstractStream {
96
113
  }
97
114
  })
98
115
  .catch(err => {
99
- this.log.error('error processing incoming data channel messages - %e', err)
116
+ this.log.error('error processing incoming data channel messages', err)
100
117
  })
101
118
 
102
119
  // close when both writable ends are closed or an error occurs
@@ -107,26 +124,6 @@ export class WebRTCStream extends AbstractStream {
107
124
  }
108
125
  }
109
126
  this.addEventListener('close', cleanUpDatachannelOnClose)
110
-
111
- // chrome can receive message events before the open even is fired - calling
112
- // code needs to attach message event listeners before these events occur
113
- // but we need to wait before sending any data so this has to be done async
114
- if (this.channel.readyState !== 'open') {
115
- this.log('channel ready state is "%s" and not "open", waiting for "open" event before sending data', this.channel.readyState)
116
- pEvent(this.channel, 'open', {
117
- rejectionEvents: [
118
- 'close',
119
- 'error'
120
- ]
121
- })
122
- .then(() => {
123
- this.log('channel ready state is now "%s", dispatching drain', this.channel.readyState)
124
- this.safeDispatchEvent('drain')
125
- })
126
- .catch(err => {
127
- this.abort(err.error ?? err)
128
- })
129
- }
130
127
  }
131
128
 
132
129
  sendNewStream (): void {
@@ -207,13 +204,8 @@ export class WebRTCStream extends AbstractStream {
207
204
  options?.signal?.throwIfAborted()
208
205
  this.receivedFinAck = Promise.withResolvers<void>()
209
206
 
210
- // wait for either:
211
- // 1. the FIN_ACK to be received
212
- // 2. the datachannel to close
213
- // 3. timeout
214
207
  await Promise.any([
215
208
  raceSignal(this.receivedFinAck.promise, options?.signal),
216
- pEvent(this.channel, 'close'),
217
209
  new Promise<void>(resolve => {
218
210
  AbortSignal.timeout(this.finAckTimeout)
219
211
  .addEventListener('abort', () => {
package/src/util.ts CHANGED
@@ -77,7 +77,7 @@ export function drainAndClose (channel: RTCDataChannel, direction: string, drain
77
77
  }
78
78
  })
79
79
  .catch(err => {
80
- options.log.error('error closing outbound stream - %e', err)
80
+ options.log.error('error closing outbound stream', err)
81
81
  })
82
82
  }
83
83