@libp2p/webrtc 1.1.9 → 1.1.11

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 (59) hide show
  1. package/dist/index.min.js +15 -15
  2. package/dist/src/index.d.ts +2 -2
  3. package/dist/src/index.d.ts.map +1 -1
  4. package/dist/src/index.js.map +1 -1
  5. package/dist/src/maconn.d.ts +2 -2
  6. package/dist/src/maconn.d.ts.map +1 -1
  7. package/dist/src/maconn.js +20 -8
  8. package/dist/src/maconn.js.map +1 -1
  9. package/dist/src/muxer.d.ts +3 -2
  10. package/dist/src/muxer.d.ts.map +1 -1
  11. package/dist/src/muxer.js +31 -17
  12. package/dist/src/muxer.js.map +1 -1
  13. package/dist/src/peer_transport/handler.d.ts +1 -1
  14. package/dist/src/peer_transport/handler.d.ts.map +1 -1
  15. package/dist/src/peer_transport/handler.js +6 -8
  16. package/dist/src/peer_transport/handler.js.map +1 -1
  17. package/dist/src/peer_transport/listener.d.ts +2 -2
  18. package/dist/src/peer_transport/listener.d.ts.map +1 -1
  19. package/dist/src/peer_transport/listener.js +2 -1
  20. package/dist/src/peer_transport/listener.js.map +1 -1
  21. package/dist/src/peer_transport/pb/index.d.ts +1 -1
  22. package/dist/src/peer_transport/pb/index.d.ts.map +1 -1
  23. package/dist/src/peer_transport/transport.d.ts +6 -7
  24. package/dist/src/peer_transport/transport.d.ts.map +1 -1
  25. package/dist/src/peer_transport/transport.js +10 -12
  26. package/dist/src/peer_transport/transport.js.map +1 -1
  27. package/dist/src/peer_transport/util.d.ts +1 -1
  28. package/dist/src/peer_transport/util.d.ts.map +1 -1
  29. package/dist/src/peer_transport/util.js +1 -1
  30. package/dist/src/peer_transport/util.js.map +1 -1
  31. package/dist/src/sdp.d.ts +3 -1
  32. package/dist/src/sdp.d.ts.map +1 -1
  33. package/dist/src/sdp.js +26 -0
  34. package/dist/src/sdp.js.map +1 -1
  35. package/dist/src/stream.d.ts +5 -5
  36. package/dist/src/stream.d.ts.map +1 -1
  37. package/dist/src/stream.js +49 -25
  38. package/dist/src/stream.js.map +1 -1
  39. package/dist/src/transport.d.ts +4 -4
  40. package/dist/src/transport.d.ts.map +1 -1
  41. package/dist/src/transport.js +26 -28
  42. package/dist/src/transport.js.map +1 -1
  43. package/dist/src/util.d.ts +1 -3
  44. package/dist/src/util.d.ts.map +1 -1
  45. package/dist/src/util.js +1 -3
  46. package/dist/src/util.js.map +1 -1
  47. package/package.json +27 -29
  48. package/src/index.ts +3 -3
  49. package/src/maconn.ts +4 -5
  50. package/src/muxer.ts +5 -5
  51. package/src/peer_transport/handler.ts +7 -9
  52. package/src/peer_transport/listener.ts +3 -3
  53. package/src/peer_transport/pb/index.ts +1 -1
  54. package/src/peer_transport/transport.ts +13 -18
  55. package/src/peer_transport/util.ts +2 -2
  56. package/src/sdp.ts +33 -3
  57. package/src/stream.ts +7 -7
  58. package/src/transport.ts +29 -37
  59. package/src/util.ts +1 -3
package/src/sdp.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import type { Multiaddr } from '@multiformats/multiaddr'
3
2
  import { bases } from 'multiformats/basics'
4
3
  import * as multihashes from 'multihashes'
5
- import type { HashCode, HashName } from 'multihashes'
6
-
7
4
  import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithm } from './error.js'
8
5
  import { CERTHASH_CODE } from './transport.js'
6
+ import type { Multiaddr } from '@multiformats/multiaddr'
7
+ import type { HashCode, HashName } from 'multihashes'
9
8
 
10
9
  const log = logger('libp2p:webrtc:sdp')
11
10
 
@@ -15,6 +14,37 @@ const log = logger('libp2p:webrtc:sdp')
15
14
  // @ts-expect-error - Not easy to combine these types.
16
15
  export const mbdecoder: any = Object.values(bases).map(b => b.decoder).reduce((d, b) => d.or(b))
17
16
 
17
+ export function getLocalFingerprint (pc: RTCPeerConnection): string | undefined {
18
+ // try to fetch fingerprint from local certificate
19
+ const localCert = pc.getConfiguration().certificates?.at(0)
20
+ if (localCert == null || localCert.getFingerprints == null) {
21
+ log.trace('fetching fingerprint from local SDP')
22
+ const localDescription = pc.localDescription
23
+ if (localDescription == null) {
24
+ return undefined
25
+ }
26
+ return getFingerprintFromSdp(localDescription.sdp)
27
+ }
28
+
29
+ log.trace('fetching fingerprint from local certificate')
30
+
31
+ if (localCert.getFingerprints().length === 0) {
32
+ return undefined
33
+ }
34
+
35
+ const fingerprint = localCert.getFingerprints()[0].value
36
+ if (fingerprint == null) {
37
+ throw invalidFingerprint('', 'no fingerprint on local certificate')
38
+ }
39
+
40
+ return fingerprint
41
+ }
42
+
43
+ const fingerprintRegex = /^a=fingerprint:(?:\w+-[0-9]+)\s(?<fingerprint>(:?[0-9a-fA-F]{2})+)$/m
44
+ export function getFingerprintFromSdp (sdp: string): string | undefined {
45
+ const searchResult = sdp.match(fingerprintRegex)
46
+ return searchResult?.groups?.fingerprint
47
+ }
18
48
  /**
19
49
  * Get base2 | identity decoders
20
50
  */
package/src/stream.ts CHANGED
@@ -1,14 +1,13 @@
1
- import type { Stream, StreamStat, Direction } from '@libp2p/interface-connection'
2
1
  import { logger } from '@libp2p/logger'
3
2
  import * as lengthPrefixed from 'it-length-prefixed'
4
3
  import merge from 'it-merge'
5
4
  import { pipe } from 'it-pipe'
6
5
  import { pushable } from 'it-pushable'
7
- import defer, { DeferredPromise } from 'p-defer'
8
- import type { Source } from 'it-stream-types'
6
+ import defer, { type DeferredPromise } from 'p-defer'
9
7
  import { Uint8ArrayList } from 'uint8arraylist'
10
-
11
8
  import * as pb from '../proto_ts/message.js'
9
+ import type { Stream, StreamStat, Direction } from '@libp2p/interface-connection'
10
+ import type { Source } from 'it-stream-types'
12
11
 
13
12
  const log = logger('libp2p:webrtc:stream')
14
13
 
@@ -182,7 +181,7 @@ export class WebRTCStream implements Stream {
182
181
  * Read unwrapped protobuf data from the underlying datachannel.
183
182
  * _src is exposed to the user via the `source` getter to .
184
183
  */
185
- private readonly _src: Source<Uint8ArrayList>
184
+ private readonly _src: AsyncGenerator<Uint8ArrayList, any, unknown>
186
185
 
187
186
  /**
188
187
  * push data from the underlying datachannel to the length prefix decoder
@@ -213,6 +212,7 @@ export class WebRTCStream implements Stream {
213
212
 
214
213
  constructor (opts: StreamInitOpts) {
215
214
  this.channel = opts.channel
215
+ this.channel.binaryType = 'arraybuffer'
216
216
  this.id = this.channel.label
217
217
 
218
218
  this.stat = opts.stat
@@ -281,9 +281,9 @@ export class WebRTCStream implements Stream {
281
281
  }
282
282
 
283
283
  // If user attempts to set a new source this should be a noop
284
- set source (_src: Source<Uint8ArrayList>) { }
284
+ set source (_src: AsyncGenerator<Uint8ArrayList, any, unknown>) { }
285
285
 
286
- get source (): Source<Uint8ArrayList> {
286
+ get source (): AsyncGenerator<Uint8ArrayList, any, unknown> {
287
287
  return this._src
288
288
  }
289
289
 
package/src/transport.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import { noise as Noise } from '@chainsafe/libp2p-noise'
2
- import type { Connection } from '@libp2p/interface-connection'
3
- import type { PeerId } from '@libp2p/interface-peer-id'
4
- import { CreateListenerOptions, Listener, symbol, Transport } from '@libp2p/interface-transport'
2
+ import { type CreateListenerOptions, type Listener, symbol, type Transport } from '@libp2p/interface-transport'
5
3
  import { logger } from '@libp2p/logger'
6
4
  import * as p from '@libp2p/peer-id'
7
- import type { Multiaddr } from '@multiformats/multiaddr'
5
+ import { protocols } from '@multiformats/multiaddr'
8
6
  import * as multihashes from 'multihashes'
9
- import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
10
7
  import { concat } from 'uint8arrays/concat'
8
+ import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
11
9
  import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from './error.js'
12
10
  import { WebRTCMultiaddrConnection } from './maconn.js'
13
11
  import { DataChannelMuxerFactory } from './muxer.js'
14
- import type { WebRTCDialOptions } from './options.js'
12
+ import { isFirefox } from './peer_transport/util.js'
15
13
  import * as sdp from './sdp.js'
16
14
  import { WebRTCStream } from './stream.js'
17
15
  import { genUfrag } from './util.js'
18
- import { protocols } from '@multiformats/multiaddr'
19
- import { isFirefox } from './peer_transport/util.js'
16
+ import type { WebRTCDialOptions } from './options.js'
17
+ import type { Connection } from '@libp2p/interface-connection'
18
+ import type { PeerId } from '@libp2p/interface-peer-id'
19
+ import type { Multiaddr } from '@multiformats/multiaddr'
20
20
 
21
21
  const log = logger('libp2p:webrtc:transport')
22
22
 
@@ -83,16 +83,12 @@ export class WebRTCDirectTransport implements Transport {
83
83
  /**
84
84
  * Implement toString() for WebRTCTransport
85
85
  */
86
- get [Symbol.toStringTag] (): string {
87
- return '@libp2p/webrtc-direct'
88
- }
86
+ readonly [Symbol.toStringTag] = '@libp2p/webrtc-direct'
89
87
 
90
88
  /**
91
89
  * Symbol.for('@libp2p/transport')
92
90
  */
93
- get [symbol] (): true {
94
- return true
95
- }
91
+ readonly [symbol] = true
96
92
 
97
93
  /**
98
94
  * Connect to a peer using a multiaddr
@@ -118,6 +114,7 @@ export class WebRTCDirectTransport implements Transport {
118
114
  namedCurve: 'P-256',
119
115
  hash: sdp.toSupportedHashFunction(remoteCerthash.name)
120
116
  } as any)
117
+
121
118
  const peerConnection = new RTCPeerConnection({ certificates: [certificate] })
122
119
 
123
120
  // create data channel for running the noise handshake. Once the data channel is opened,
@@ -181,12 +178,24 @@ export class WebRTCDirectTransport implements Transport {
181
178
  source: {
182
179
  [Symbol.asyncIterator]: async function * () {
183
180
  for await (const list of wrappedChannel.source) {
184
- yield list.subarray()
181
+ for (const buf of list) {
182
+ yield buf
183
+ }
185
184
  }
186
185
  }
187
186
  }
188
187
  }
189
188
 
189
+ // Creating the connection before completion of the noise
190
+ // handshake ensures that the stream opening callback is set up
191
+ const maConn = new WebRTCMultiaddrConnection({
192
+ peerConnection,
193
+ remoteAddr: ma,
194
+ timeline: {
195
+ open: Date.now()
196
+ }
197
+ })
198
+
190
199
  const eventListeningName = isFirefox ? 'iceconnectionstatechange' : 'connectionstatechange'
191
200
 
192
201
  peerConnection.addEventListener(eventListeningName, () => {
@@ -206,23 +215,13 @@ export class WebRTCDirectTransport implements Transport {
206
215
  }
207
216
  }, { signal })
208
217
 
209
- // Creating the connection before completion of the noise
210
- // handshake ensures that the stream opening callback is set up
211
- const maConn = new WebRTCMultiaddrConnection({
212
- peerConnection,
213
- remoteAddr: ma,
214
- timeline: {
215
- open: Date.now()
216
- }
217
- })
218
-
219
218
  const muxerFactory = new DataChannelMuxerFactory(peerConnection)
220
219
 
221
220
  // For outbound connections, the remote is expected to start the noise handshake.
222
221
  // Therefore, we need to secure an inbound noise connection from the remote.
223
222
  await noise.secureInbound(myPeerId, wrappedDuplex, theirPeerId)
224
223
 
225
- return await options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory })
224
+ return options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory })
226
225
  }
227
226
 
228
227
  /**
@@ -234,19 +233,12 @@ export class WebRTCDirectTransport implements Transport {
234
233
  throw invalidArgument('no local certificate')
235
234
  }
236
235
 
237
- const localCert = pc.getConfiguration().certificates?.at(0)
238
-
239
- if (localCert === undefined || localCert.getFingerprints().length === 0) {
240
- throw invalidArgument('no fingerprint on local certificate')
241
- }
242
-
243
- const localFingerprint = localCert.getFingerprints()[0]
244
-
245
- if (localFingerprint.value === undefined) {
246
- throw invalidArgument('no fingerprint on local certificate')
236
+ const localFingerprint = sdp.getLocalFingerprint(pc)
237
+ if (localFingerprint == null) {
238
+ throw invalidArgument('no local fingerprint found')
247
239
  }
248
240
 
249
- const localFpString = localFingerprint.value.replace(/:/g, '')
241
+ const localFpString = localFingerprint.trim().toLowerCase().replaceAll(':', '')
250
242
  const localFpArray = uint8arrayFromString(localFpString, 'hex')
251
243
  const local = multihashes.encode(localFpArray, hashCode)
252
244
  const remote: Uint8Array = sdp.mbdecoder.decode(sdp.certhash(ma))
package/src/util.ts CHANGED
@@ -1,6 +1,4 @@
1
- export const nopSource = {
2
- async * [Symbol.asyncIterator] () {}
3
- }
1
+ export const nopSource = async function * nop (): AsyncGenerator<Uint8Array, any, unknown> {}
4
2
 
5
3
  export const nopSink = async (_: any): Promise<void> => {}
6
4