@libp2p/webrtc 5.2.24-f5932c294 → 5.2.24-fb19b055d

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 (90) hide show
  1. package/README.md +10 -20
  2. package/dist/index.min.js +17 -17
  3. package/dist/index.min.js.map +4 -4
  4. package/dist/src/constants.d.ts +4 -23
  5. package/dist/src/constants.d.ts.map +1 -1
  6. package/dist/src/constants.js +4 -23
  7. package/dist/src/constants.js.map +1 -1
  8. package/dist/src/index.d.ts +20 -22
  9. package/dist/src/index.d.ts.map +1 -1
  10. package/dist/src/index.js +12 -22
  11. package/dist/src/index.js.map +1 -1
  12. package/dist/src/muxer.d.ts +14 -46
  13. package/dist/src/muxer.d.ts.map +1 -1
  14. package/dist/src/muxer.js +30 -138
  15. package/dist/src/muxer.js.map +1 -1
  16. package/dist/src/private-to-private/initiate-connection.d.ts +2 -3
  17. package/dist/src/private-to-private/initiate-connection.d.ts.map +1 -1
  18. package/dist/src/private-to-private/initiate-connection.js +37 -5
  19. package/dist/src/private-to-private/initiate-connection.js.map +1 -1
  20. package/dist/src/private-to-private/signaling-stream-handler.d.ts +4 -4
  21. package/dist/src/private-to-private/signaling-stream-handler.d.ts.map +1 -1
  22. package/dist/src/private-to-private/signaling-stream-handler.js +19 -7
  23. package/dist/src/private-to-private/signaling-stream-handler.js.map +1 -1
  24. package/dist/src/private-to-private/transport.d.ts +2 -9
  25. package/dist/src/private-to-private/transport.d.ts.map +1 -1
  26. package/dist/src/private-to-private/transport.js +30 -15
  27. package/dist/src/private-to-private/transport.js.map +1 -1
  28. package/dist/src/private-to-private/util.d.ts +3 -2
  29. package/dist/src/private-to-private/util.d.ts.map +1 -1
  30. package/dist/src/private-to-private/util.js +26 -14
  31. package/dist/src/private-to-private/util.js.map +1 -1
  32. package/dist/src/private-to-public/listener.d.ts.map +1 -1
  33. package/dist/src/private-to-public/listener.js +21 -15
  34. package/dist/src/private-to-public/listener.js.map +1 -1
  35. package/dist/src/private-to-public/transport.d.ts +0 -8
  36. package/dist/src/private-to-public/transport.d.ts.map +1 -1
  37. package/dist/src/private-to-public/transport.js +3 -2
  38. package/dist/src/private-to-public/transport.js.map +1 -1
  39. package/dist/src/private-to-public/utils/connect.d.ts +1 -1
  40. package/dist/src/private-to-public/utils/connect.d.ts.map +1 -1
  41. package/dist/src/private-to-public/utils/connect.js +17 -14
  42. package/dist/src/private-to-public/utils/connect.js.map +1 -1
  43. package/dist/src/private-to-public/utils/get-rtcpeerconnection.d.ts +4 -4
  44. package/dist/src/private-to-public/utils/get-rtcpeerconnection.d.ts.map +1 -1
  45. package/dist/src/private-to-public/utils/get-rtcpeerconnection.js +13 -2
  46. package/dist/src/private-to-public/utils/get-rtcpeerconnection.js.map +1 -1
  47. package/dist/src/private-to-public/utils/sdp.d.ts.map +1 -1
  48. package/dist/src/private-to-public/utils/sdp.js +25 -13
  49. package/dist/src/private-to-public/utils/sdp.js.map +1 -1
  50. package/dist/src/private-to-public/utils/stun-listener.js +1 -1
  51. package/dist/src/private-to-public/utils/stun-listener.js.map +1 -1
  52. package/dist/src/rtcpeerconnection-to-conn.d.ts +12 -0
  53. package/dist/src/rtcpeerconnection-to-conn.d.ts.map +1 -0
  54. package/dist/src/rtcpeerconnection-to-conn.js +46 -0
  55. package/dist/src/rtcpeerconnection-to-conn.js.map +1 -0
  56. package/dist/src/stream.d.ts +14 -26
  57. package/dist/src/stream.d.ts.map +1 -1
  58. package/dist/src/stream.js +134 -204
  59. package/dist/src/stream.js.map +1 -1
  60. package/dist/src/util.d.ts +3 -1
  61. package/dist/src/util.d.ts.map +1 -1
  62. package/dist/src/util.js +19 -0
  63. package/dist/src/util.js.map +1 -1
  64. package/dist/src/webrtc/index.d.ts +1 -1
  65. package/dist/src/webrtc/index.d.ts.map +1 -1
  66. package/dist/src/webrtc/index.js +1 -1
  67. package/dist/src/webrtc/index.js.map +1 -1
  68. package/package.json +26 -29
  69. package/src/constants.ts +5 -28
  70. package/src/index.ts +21 -22
  71. package/src/muxer.ts +39 -169
  72. package/src/private-to-private/initiate-connection.ts +46 -8
  73. package/src/private-to-private/signaling-stream-handler.ts +23 -10
  74. package/src/private-to-private/transport.ts +33 -25
  75. package/src/private-to-private/util.ts +33 -16
  76. package/src/private-to-public/listener.ts +22 -15
  77. package/src/private-to-public/transport.ts +3 -12
  78. package/src/private-to-public/utils/connect.ts +18 -15
  79. package/src/private-to-public/utils/get-rtcpeerconnection.ts +16 -4
  80. package/src/private-to-public/utils/sdp.ts +29 -13
  81. package/src/private-to-public/utils/stun-listener.ts +1 -1
  82. package/src/rtcpeerconnection-to-conn.ts +66 -0
  83. package/src/stream.ts +153 -237
  84. package/src/util.ts +22 -1
  85. package/src/webrtc/index.ts +1 -1
  86. package/dist/src/maconn.d.ts +0 -58
  87. package/dist/src/maconn.d.ts.map +0 -1
  88. package/dist/src/maconn.js +0 -56
  89. package/dist/src/maconn.js.map +0 -1
  90. package/src/maconn.ts +0 -101
@@ -1,10 +1,10 @@
1
- import { PeerConnection } from '@ipshipyard/node-datachannel'
2
- import { RTCPeerConnection } from '@ipshipyard/node-datachannel/polyfill'
3
1
  import { Crypto } from '@peculiar/webcrypto'
2
+ import { PeerConnection } from 'node-datachannel'
3
+ import { RTCPeerConnection } from 'node-datachannel/polyfill'
4
4
  import { DEFAULT_ICE_SERVERS, MAX_MESSAGE_SIZE } from '../../constants.js'
5
5
  import { generateTransportCertificate } from './generate-certificates.js'
6
6
  import type { TransportCertificate } from '../../index.js'
7
- import type { CertificateFingerprint } from '@ipshipyard/node-datachannel'
7
+ import type { CertificateFingerprint } from 'node-datachannel'
8
8
 
9
9
  const crypto = new Crypto()
10
10
 
@@ -14,7 +14,7 @@ interface DirectRTCPeerConnectionInit extends RTCConfiguration {
14
14
  }
15
15
 
16
16
  export class DirectRTCPeerConnection extends RTCPeerConnection {
17
- private readonly peerConnection: PeerConnection
17
+ private peerConnection: PeerConnection
18
18
  private readonly ufrag: string
19
19
 
20
20
  constructor (init: DirectRTCPeerConnectionInit) {
@@ -22,6 +22,18 @@ export class DirectRTCPeerConnection extends RTCPeerConnection {
22
22
 
23
23
  this.peerConnection = init.peerConnection
24
24
  this.ufrag = init.ufrag
25
+
26
+ // make sure C++ peer connection is garbage collected
27
+ // https://github.com/murat-dogan/node-datachannel/issues/366#issuecomment-3228453155
28
+ this.addEventListener('connectionstatechange', () => {
29
+ switch (this.connectionState) {
30
+ case 'closed':
31
+ this.peerConnection.close()
32
+ break
33
+ default:
34
+ break
35
+ }
36
+ })
25
37
  }
26
38
 
27
39
  async createOffer (): Promise<globalThis.RTCSessionDescriptionInit | any> {
@@ -1,10 +1,11 @@
1
1
  import { InvalidParametersError } from '@libp2p/interface'
2
- import { multiaddr } from '@multiformats/multiaddr'
2
+ import { getNetConfig } from '@libp2p/utils'
3
+ import { CODE_CERTHASH, multiaddr } from '@multiformats/multiaddr'
3
4
  import { base64url } from 'multiformats/bases/base64'
4
5
  import { bases, digest } from 'multiformats/basics'
5
6
  import * as Digest from 'multiformats/hashes/digest'
6
7
  import { sha256 } from 'multiformats/hashes/sha2'
7
- import { CODEC_CERTHASH, MAX_MESSAGE_SIZE } from '../../constants.js'
8
+ import { MAX_MESSAGE_SIZE } from '../../constants.js'
8
9
  import { InvalidFingerprintError, UnsupportedHashAlgorithmError } from '../../error.js'
9
10
  import type { Multiaddr } from '@multiformats/multiaddr'
10
11
  import type { MultihashDigest } from 'multiformats/hashes/interface'
@@ -27,8 +28,8 @@ export function getFingerprintFromSdp (sdp: string | undefined): string | undefi
27
28
 
28
29
  // Extract the certhash from a multiaddr
29
30
  export function certhash (ma: Multiaddr): string {
30
- const tups = ma.stringTuples()
31
- const certhash = tups.filter((tup) => tup[0] === CODEC_CERTHASH).map((tup) => tup[1])[0]
31
+ const components = ma.getComponents()
32
+ const certhash = components.find(c => c.code === CODE_CERTHASH)?.value
32
33
 
33
34
  if (certhash === undefined || certhash === '') {
34
35
  throw new InvalidParametersError(`Couldn't find a certhash component of multiaddr: ${ma.toString()}`)
@@ -100,15 +101,20 @@ export function toSupportedHashFunction (code: number): 'sha-1' | 'sha-256' | 's
100
101
  * ice-lite mode and DTLS active mode.
101
102
  */
102
103
  export function serverAnswerFromMultiaddr (ma: Multiaddr, ufrag: string): RTCSessionDescriptionInit {
103
- const { host, port, family } = ma.toOptions()
104
+ const { host, port, type } = getNetConfig(ma)
105
+
106
+ if (type !== 'ip4' && type !== 'ip6') {
107
+ throw new InvalidParametersError(`Multiaddr ${ma} was not an IPv4 or IPv6 address`)
108
+ }
109
+
104
110
  const fingerprint = ma2Fingerprint(ma)
105
111
  const sdp = `v=0
106
- o=- 0 0 IN IP${family} ${host}
112
+ o=- 0 0 IN IP${type === 'ip4' ? 4 : 6} ${host}
107
113
  s=-
108
114
  t=0 0
109
115
  a=ice-lite
110
116
  m=application ${port} UDP/DTLS/SCTP webrtc-datachannel
111
- c=IN IP${family} ${host}
117
+ c=IN IP${type === 'ip4' ? 4 : 6} ${host}
112
118
  a=mid:0
113
119
  a=ice-options:ice2
114
120
  a=ice-ufrag:${ufrag}
@@ -131,11 +137,16 @@ a=end-of-candidates
131
137
  * Create an offer SDP message from a multiaddr
132
138
  */
133
139
  export function clientOfferFromMultiAddr (ma: Multiaddr, ufrag: string): RTCSessionDescriptionInit {
134
- const { host, port, family } = ma.toOptions()
140
+ const { host, port, type } = getNetConfig(ma)
141
+
142
+ if (type !== 'ip4' && type !== 'ip6') {
143
+ throw new InvalidParametersError(`Multiaddr ${ma} was not an IPv4 or IPv6 address`)
144
+ }
145
+
135
146
  const sdp = `v=0
136
- o=- 0 0 IN IP${family} ${host}
147
+ o=- 0 0 IN IP${type === 'ip4' ? 4 : 6} ${host}
137
148
  s=-
138
- c=IN IP${family} ${host}
149
+ c=IN IP${type === 'ip4' ? 4 : 6} ${host}
139
150
  t=0 0
140
151
  a=ice-options:ice2,trickle
141
152
  m=application ${port} UDP/DTLS/SCTP webrtc-datachannel
@@ -166,8 +177,13 @@ export function munge (desc: RTCSessionDescriptionInit, ufrag: string): RTCSessi
166
177
 
167
178
  const lineBreak = desc.sdp.includes('\r\n') ? '\r\n' : '\n'
168
179
 
169
- desc.sdp = desc.sdp
170
- .replace(/\na=ice-ufrag:[^\n]*\n/, '\na=ice-ufrag:' + ufrag + lineBreak)
171
- .replace(/\na=ice-pwd:[^\n]*\n/, '\na=ice-pwd:' + ufrag + lineBreak)
180
+ try {
181
+ desc.sdp = desc.sdp
182
+ .replace(/\na=ice-ufrag:[^\n]*\n/, '\na=ice-ufrag:' + ufrag + lineBreak)
183
+ .replace(/\na=ice-pwd:[^\n]*\n/, '\na=ice-pwd:' + ufrag + lineBreak)
184
+ } catch {
185
+ // fails under Node.js
186
+ }
187
+
172
188
  return desc
173
189
  }
@@ -1,5 +1,5 @@
1
1
  import { isIPv4 } from '@chainsafe/is-ip'
2
- import { IceUdpMuxListener } from '@ipshipyard/node-datachannel'
2
+ import { IceUdpMuxListener } from 'node-datachannel'
3
3
  import type { Logger } from '@libp2p/interface'
4
4
  import type { AddressInfo } from 'node:net'
5
5
 
@@ -0,0 +1,66 @@
1
+ import { AbstractMultiaddrConnection } from '@libp2p/utils'
2
+ import type { RTCPeerConnection } from './webrtc/index.js'
3
+ import type { AbortOptions, MultiaddrConnection } from '@libp2p/interface'
4
+ import type { AbstractMultiaddrConnectionInit, SendResult } from '@libp2p/utils'
5
+ import type { Uint8ArrayList } from 'uint8arraylist'
6
+
7
+ export interface RTCPeerConnectionMultiaddrConnectionInit extends Omit<AbstractMultiaddrConnectionInit, 'stream'> {
8
+ peerConnection: RTCPeerConnection
9
+ }
10
+
11
+ class RTCPeerConnectionMultiaddrConnection extends AbstractMultiaddrConnection {
12
+ private peerConnection: RTCPeerConnection
13
+
14
+ constructor (init: RTCPeerConnectionMultiaddrConnectionInit) {
15
+ super(init)
16
+
17
+ this.peerConnection = init.peerConnection
18
+
19
+ const initialState = init.peerConnection.connectionState
20
+
21
+ this.peerConnection.onconnectionstatechange = () => {
22
+ this.log.trace('peer connection state change %s initial state %s', this.peerConnection.connectionState, initialState)
23
+
24
+ if (this.peerConnection.connectionState === 'disconnected' || this.peerConnection.connectionState === 'failed' || this.peerConnection.connectionState === 'closed') {
25
+ // nothing else to do but close the connection
26
+ this.onTransportClosed()
27
+
28
+ // only necessary with node-datachannel
29
+ // https://github.com/murat-dogan/node-datachannel/issues/366#issuecomment-3228453155
30
+ this.peerConnection.close()
31
+ }
32
+ }
33
+ }
34
+
35
+ sendData (data: Uint8ArrayList): SendResult {
36
+ return {
37
+ sentBytes: data.byteLength,
38
+ canSendMore: true
39
+ }
40
+ }
41
+
42
+ async sendClose (options?: AbortOptions): Promise<void> {
43
+ this.peerConnection.close()
44
+ options?.signal?.throwIfAborted()
45
+ }
46
+
47
+ sendReset (): void {
48
+ this.peerConnection.close()
49
+ }
50
+
51
+ sendPause (): void {
52
+ // TODO: readable backpressure?
53
+ }
54
+
55
+ sendResume (): void {
56
+ // TODO: readable backpressure?
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Convert a RTCPeerConnection into a MultiaddrConnection
62
+ * https://github.com/libp2p/interface-transport#multiaddrconnection
63
+ */
64
+ export const toMultiaddrConnection = (init: RTCPeerConnectionMultiaddrConnectionInit): MultiaddrConnection => {
65
+ return new RTCPeerConnectionMultiaddrConnection(init)
66
+ }