@libp2p/webrtc 4.1.1 → 4.1.2-34cf1f7cd

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.
@@ -11,8 +11,8 @@ import { initiateConnection } from './initiate-connection.js'
11
11
  import { WebRTCPeerListener } from './listener.js'
12
12
  import { handleIncomingStream } from './signaling-stream-handler.js'
13
13
  import type { DataChannelOptions } from '../index.js'
14
- import type { OutboundConnectionUpgradeEvents, CreateListenerOptions, DialOptions, Transport, Listener, Upgrader, ComponentLogger, Logger, Connection, PeerId, CounterGroup, Metrics, Startable } from '@libp2p/interface'
15
- import type { IncomingStreamData, Registrar, ConnectionManager, TransportManager, OpenConnectionProgressEvents } from '@libp2p/interface-internal'
14
+ import type { OutboundConnectionUpgradeEvents, CreateListenerOptions, DialTransportOptions, Transport, Listener, Upgrader, ComponentLogger, Logger, Connection, PeerId, CounterGroup, Metrics, Startable, OpenConnectionProgressEvents } from '@libp2p/interface'
15
+ import type { IncomingStreamData, Registrar, ConnectionManager, TransportManager } from '@libp2p/interface-internal'
16
16
  import type { ProgressEvent } from 'progress-events'
17
17
 
18
18
  const WEBRTC_TRANSPORT = '/webrtc'
@@ -145,7 +145,7 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
145
145
  * For a circuit relay, this will be of the form
146
146
  * <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
147
147
  */
148
- async dial (ma: Multiaddr, options: DialOptions<WebRTCDialEvents>): Promise<Connection> {
148
+ async dial (ma: Multiaddr, options: DialTransportOptions<WebRTCDialEvents>): Promise<Connection> {
149
149
  this.log.trace('dialing address: %a', ma)
150
150
 
151
151
  const { remoteAddress, peerConnection, muxerFactory } = await initiateConnection({
@@ -1,4 +1,4 @@
1
- import type { CreateListenerOptions, DialOptions } from '@libp2p/interface'
1
+ import type { CreateListenerOptions, DialTransportOptions } from '@libp2p/interface'
2
2
 
3
3
  export interface WebRTCListenerOptions extends CreateListenerOptions {}
4
- export interface WebRTCDialOptions extends DialOptions {}
4
+ export interface WebRTCDialOptions extends DialTransportOptions {}
@@ -1,10 +1,9 @@
1
- import { bases } from 'multiformats/basics'
2
- import * as multihashes from 'multihashes'
3
- import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithm } from '../error.js'
1
+ import { type Multiaddr } from '@multiformats/multiaddr'
2
+ import { bases, digest } from 'multiformats/basics'
3
+ import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithmCode } from '../error.js'
4
4
  import { CERTHASH_CODE } from './transport.js'
5
5
  import type { LoggerOptions } from '@libp2p/interface'
6
- import type { Multiaddr } from '@multiformats/multiaddr'
7
- import type { HashCode, HashName } from 'multihashes'
6
+ import type { MultihashDigest } from 'multiformats/hashes/interface'
8
7
 
9
8
  /**
10
9
  * Get base2 | identity decoders
@@ -71,9 +70,8 @@ export function certhash (ma: Multiaddr): string {
71
70
  /**
72
71
  * Convert a certhash into a multihash
73
72
  */
74
- export function decodeCerthash (certhash: string): { code: HashCode, name: HashName, length: number, digest: Uint8Array } {
75
- const mbdecoded = mbdecoder.decode(certhash)
76
- return multihashes.decode(mbdecoded)
73
+ export function decodeCerthash (certhash: string): MultihashDigest {
74
+ return digest.decode(mbdecoder.decode(certhash))
77
75
  }
78
76
 
79
77
  /**
@@ -81,7 +79,7 @@ export function decodeCerthash (certhash: string): { code: HashCode, name: HashN
81
79
  */
82
80
  export function ma2Fingerprint (ma: Multiaddr): string[] {
83
81
  const mhdecoded = decodeCerthash(certhash(ma))
84
- const prefix = toSupportedHashFunction(mhdecoded.name)
82
+ const prefix = toSupportedHashFunction(mhdecoded.code)
85
83
  const fingerprint = mhdecoded.digest.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '')
86
84
  const sdp = fingerprint.match(/.{1,2}/g)
87
85
 
@@ -89,22 +87,22 @@ export function ma2Fingerprint (ma: Multiaddr): string[] {
89
87
  throw invalidFingerprint(fingerprint, ma.toString())
90
88
  }
91
89
 
92
- return [`${prefix.toUpperCase()} ${sdp.join(':').toUpperCase()}`, fingerprint]
90
+ return [`${prefix} ${sdp.join(':').toUpperCase()}`, fingerprint]
93
91
  }
94
92
 
95
93
  /**
96
94
  * Normalize the hash name from a given multihash has name
97
95
  */
98
- export function toSupportedHashFunction (name: multihashes.HashName): string {
99
- switch (name) {
100
- case 'sha1':
101
- return 'sha-1'
102
- case 'sha2-256':
103
- return 'sha-256'
104
- case 'sha2-512':
105
- return 'sha-512'
96
+ export function toSupportedHashFunction (code: number): 'SHA-1' | 'SHA-256' | 'SHA-512' {
97
+ switch (code) {
98
+ case 0x11:
99
+ return 'SHA-1'
100
+ case 0x12:
101
+ return 'SHA-256'
102
+ case 0x13:
103
+ return 'SHA-512'
106
104
  default:
107
- throw unsupportedHashAlgorithm(name)
105
+ throw unsupportedHashAlgorithmCode(code)
108
106
  }
109
107
  }
110
108
 
@@ -3,7 +3,7 @@ import { transportSymbol, serviceCapabilities } from '@libp2p/interface'
3
3
  import * as p from '@libp2p/peer-id'
4
4
  import { protocols } from '@multiformats/multiaddr'
5
5
  import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
6
- import * as multihashes from 'multihashes'
6
+ import * as Digest from 'multiformats/hashes/digest'
7
7
  import { concat } from 'uint8arrays/concat'
8
8
  import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
9
9
  import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from '../error.js'
@@ -135,7 +135,7 @@ export class WebRTCDirectTransport implements Transport {
135
135
  const certificate = await RTCPeerConnection.generateCertificate({
136
136
  name: 'ECDSA',
137
137
  namedCurve: 'P-256',
138
- hash: sdp.toSupportedHashFunction(remoteCerthash.name)
138
+ hash: sdp.toSupportedHashFunction(remoteCerthash.code)
139
139
  } as any)
140
140
 
141
141
  const peerConnection = new RTCPeerConnection({
@@ -273,7 +273,7 @@ export class WebRTCDirectTransport implements Transport {
273
273
  * Generate a noise prologue from the peer connection's certificate.
274
274
  * noise prologue = bytes('libp2p-webrtc-noise:') + noise-responder fingerprint + noise-initiator fingerprint
275
275
  */
276
- private generateNoisePrologue (pc: RTCPeerConnection, hashCode: multihashes.HashCode, ma: Multiaddr): Uint8Array {
276
+ private generateNoisePrologue (pc: RTCPeerConnection, hashCode: number, ma: Multiaddr): Uint8Array {
277
277
  if (pc.getConfiguration().certificates?.length === 0) {
278
278
  throw invalidArgument('no local certificate')
279
279
  }
@@ -287,10 +287,10 @@ export class WebRTCDirectTransport implements Transport {
287
287
 
288
288
  const localFpString = localFingerprint.trim().toLowerCase().replaceAll(':', '')
289
289
  const localFpArray = uint8arrayFromString(localFpString, 'hex')
290
- const local = multihashes.encode(localFpArray, hashCode)
290
+ const local = Digest.create(hashCode, localFpArray)
291
291
  const remote: Uint8Array = sdp.mbdecoder.decode(sdp.certhash(ma))
292
292
  const prefix = uint8arrayFromString('libp2p-webrtc-noise:')
293
293
 
294
- return concat([prefix, local, remote])
294
+ return concat([prefix, local.bytes, remote])
295
295
  }
296
296
  }
package/src/stream.ts CHANGED
@@ -27,7 +27,7 @@ export interface WebRTCStreamInit extends AbstractStreamInit, DataChannelOptions
27
27
  /**
28
28
  * How much can be buffered to the DataChannel at once
29
29
  */
30
- export const MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
30
+ export const MAX_BUFFERED_AMOUNT = 2 * 1024 * 1024
31
31
 
32
32
  /**
33
33
  * How long time we wait for the 'bufferedamountlow' event to be emitted
@@ -35,9 +35,9 @@ export const MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
35
35
  export const BUFFERED_AMOUNT_LOW_TIMEOUT = 30 * 1000
36
36
 
37
37
  /**
38
- * protobuf field definition overhead
38
+ * protobuf field definition overhead + length encoding prefix length
39
39
  */
40
- export const PROTOBUF_OVERHEAD = 5
40
+ export const PROTOBUF_OVERHEAD = 7
41
41
 
42
42
  /**
43
43
  * Length of varint, in bytes
@@ -46,8 +46,11 @@ export const VARINT_LENGTH = 2
46
46
 
47
47
  /**
48
48
  * Max message size that can be sent to the DataChannel
49
+ *
50
+ * @see https://blog.mozilla.org/webrtc/large-data-channel-messages/
51
+ * @see https://issues.webrtc.org/issues/40644524
49
52
  */
50
- export const MAX_MESSAGE_SIZE = 16 * 1024
53
+ export const MAX_MESSAGE_SIZE = 256 * 1024
51
54
 
52
55
  /**
53
56
  * When closing streams we send a FIN then wait for the remote to
@@ -1,8 +0,0 @@
1
- {
2
- "DataChannelOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.DataChannelOptions.html",
3
- ".:DataChannelOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.DataChannelOptions.html",
4
- "webRTC": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTC.html",
5
- ".:webRTC": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTC.html",
6
- "webRTCDirect": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTCDirect.html",
7
- ".:webRTCDirect": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTCDirect.html"
8
- }