@libp2p/webrtc 5.2.4 → 5.2.5-0555339ba
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 +7 -7
- package/dist/src/constants.d.ts +58 -0
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +77 -0
- package/dist/src/constants.js.map +1 -1
- package/dist/src/index.d.ts +8 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/muxer.d.ts.map +1 -1
- package/dist/src/muxer.js +3 -3
- package/dist/src/muxer.js.map +1 -1
- package/dist/src/private-to-private/initiate-connection.d.ts.map +1 -1
- package/dist/src/private-to-private/initiate-connection.js +3 -2
- package/dist/src/private-to-private/initiate-connection.js.map +1 -1
- package/dist/src/private-to-private/listener.d.ts +4 -1
- package/dist/src/private-to-private/listener.d.ts.map +1 -1
- package/dist/src/private-to-private/listener.js +14 -2
- package/dist/src/private-to-private/listener.js.map +1 -1
- package/dist/src/private-to-private/transport.d.ts +12 -3
- package/dist/src/private-to-private/transport.d.ts.map +1 -1
- package/dist/src/private-to-private/transport.js +5 -7
- package/dist/src/private-to-private/transport.js.map +1 -1
- package/dist/src/private-to-public/utils/get-rtcpeerconnection.d.ts.map +1 -1
- package/dist/src/private-to-public/utils/get-rtcpeerconnection.js +1 -2
- package/dist/src/private-to-public/utils/get-rtcpeerconnection.js.map +1 -1
- package/dist/src/private-to-public/utils/sdp.d.ts.map +1 -1
- package/dist/src/private-to-public/utils/sdp.js +1 -2
- package/dist/src/private-to-public/utils/sdp.js.map +1 -1
- package/dist/src/stream.d.ts +0 -33
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +1 -51
- package/dist/src/stream.js.map +1 -1
- package/dist/src/util.d.ts +0 -1
- package/dist/src/util.d.ts.map +1 -1
- package/dist/src/util.js +1 -2
- package/dist/src/util.js.map +1 -1
- package/package.json +8 -8
- package/src/constants.ts +91 -0
- package/src/index.ts +9 -6
- package/src/muxer.ts +3 -4
- package/src/private-to-private/initiate-connection.ts +3 -2
- package/src/private-to-private/listener.ts +20 -3
- package/src/private-to-private/transport.ts +18 -10
- package/src/private-to-public/utils/get-rtcpeerconnection.ts +1 -2
- package/src/private-to-public/utils/sdp.ts +1 -2
- package/src/stream.ts +1 -60
- package/src/util.ts +1 -3
- package/dist/typedoc-urls.json +0 -14
@@ -2,6 +2,7 @@ import { InvalidParametersError, serviceCapabilities, serviceDependencies, setMa
|
|
2
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
3
3
|
import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
4
4
|
import { WebRTC } from '@multiformats/multiaddr-matcher'
|
5
|
+
import { SIGNALING_PROTOCOL } from '../constants.js'
|
5
6
|
import { WebRTCMultiaddrConnection } from '../maconn.js'
|
6
7
|
import { DataChannelMuxerFactory } from '../muxer.js'
|
7
8
|
import { getRtcConfiguration } from '../util.js'
|
@@ -10,22 +11,28 @@ import { initiateConnection } from './initiate-connection.js'
|
|
10
11
|
import { WebRTCPeerListener } from './listener.js'
|
11
12
|
import { handleIncomingStream } from './signaling-stream-handler.js'
|
12
13
|
import type { DataChannelOptions } from '../index.js'
|
13
|
-
import type { OutboundConnectionUpgradeEvents, CreateListenerOptions, DialTransportOptions, Transport, Listener, Upgrader, ComponentLogger, Logger, Connection, PeerId, CounterGroup, Metrics, Startable, OpenConnectionProgressEvents, IncomingStreamData } from '@libp2p/interface'
|
14
|
+
import type { OutboundConnectionUpgradeEvents, CreateListenerOptions, DialTransportOptions, Transport, Listener, Upgrader, ComponentLogger, Logger, Connection, PeerId, CounterGroup, Metrics, Startable, OpenConnectionProgressEvents, IncomingStreamData, Libp2pEvents, TypedEventTarget } from '@libp2p/interface'
|
14
15
|
import type { Registrar, ConnectionManager, TransportManager } from '@libp2p/interface-internal'
|
15
16
|
import type { ProgressEvent } from 'progress-events'
|
16
17
|
|
17
|
-
const WEBRTC_TRANSPORT = '/webrtc'
|
18
|
-
const CIRCUIT_RELAY_TRANSPORT = '/p2p-circuit'
|
19
|
-
export const SIGNALING_PROTO_ID = '/webrtc-signaling/0.0.1'
|
20
|
-
|
21
18
|
export interface WebRTCTransportInit {
|
19
|
+
/**
|
20
|
+
* Add additional configuration to any RTCPeerConnections that are created.
|
21
|
+
*
|
22
|
+
* This could be extra STUN/TURN servers, certificate, etc.
|
23
|
+
*/
|
22
24
|
rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Any options here will be applied to any RTCDataChannels that are opened.
|
28
|
+
*/
|
23
29
|
dataChannel?: DataChannelOptions
|
24
30
|
|
25
31
|
/**
|
26
32
|
* Inbound connections must complete the upgrade within this many ms
|
27
33
|
*
|
28
|
-
* @default
|
34
|
+
* @default 30_000
|
35
|
+
* @deprecated configure `connectionManager.inboundUpgradeTimeout` instead
|
29
36
|
*/
|
30
37
|
inboundConnectionTimeout?: number
|
31
38
|
}
|
@@ -38,6 +45,7 @@ export interface WebRTCTransportComponents {
|
|
38
45
|
connectionManager: ConnectionManager
|
39
46
|
metrics?: Metrics
|
40
47
|
logger: ComponentLogger
|
48
|
+
events: TypedEventTarget<Libp2pEvents>
|
41
49
|
}
|
42
50
|
|
43
51
|
export interface WebRTCTransportMetrics {
|
@@ -104,7 +112,7 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
|
|
104
112
|
}
|
105
113
|
|
106
114
|
async start (): Promise<void> {
|
107
|
-
await this.components.registrar.handle(
|
115
|
+
await this.components.registrar.handle(SIGNALING_PROTOCOL, (data: IncomingStreamData) => {
|
108
116
|
// ensure we don't try to upgrade forever
|
109
117
|
const signal = this.components.upgrader.createInboundAbortSignal(this.shutdownController.signal)
|
110
118
|
|
@@ -122,7 +130,7 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
|
|
122
130
|
}
|
123
131
|
|
124
132
|
async stop (): Promise<void> {
|
125
|
-
await this.components.registrar.unhandle(
|
133
|
+
await this.components.registrar.unhandle(SIGNALING_PROTOCOL)
|
126
134
|
this._started = false
|
127
135
|
}
|
128
136
|
|
@@ -254,12 +262,12 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
|
|
254
262
|
}
|
255
263
|
|
256
264
|
export function splitAddr (ma: Multiaddr): { baseAddr: Multiaddr, peerId: PeerId } {
|
257
|
-
const addrs = ma.toString().split(
|
265
|
+
const addrs = ma.toString().split('/webrtc/')
|
258
266
|
if (addrs.length !== 2) {
|
259
267
|
throw new InvalidParametersError('webrtc protocol was not present in multiaddr')
|
260
268
|
}
|
261
269
|
|
262
|
-
if (!addrs[0].includes(
|
270
|
+
if (!addrs[0].includes('/p2p-circuit')) {
|
263
271
|
throw new InvalidParametersError('p2p-circuit protocol was not present in multiaddr')
|
264
272
|
}
|
265
273
|
|
@@ -1,8 +1,7 @@
|
|
1
1
|
import { PeerConnection } from '@ipshipyard/node-datachannel'
|
2
2
|
import { RTCPeerConnection } from '@ipshipyard/node-datachannel/polyfill'
|
3
3
|
import { Crypto } from '@peculiar/webcrypto'
|
4
|
-
import { DEFAULT_ICE_SERVERS } from '../../constants.js'
|
5
|
-
import { MAX_MESSAGE_SIZE } from '../../stream.js'
|
4
|
+
import { DEFAULT_ICE_SERVERS, MAX_MESSAGE_SIZE } from '../../constants.js'
|
6
5
|
import { generateTransportCertificate } from './generate-certificates.js'
|
7
6
|
import type { TransportCertificate } from '../../index.js'
|
8
7
|
import type { CertificateFingerprint } from '@ipshipyard/node-datachannel'
|
@@ -4,9 +4,8 @@ import { base64url } from 'multiformats/bases/base64'
|
|
4
4
|
import { bases, digest } from 'multiformats/basics'
|
5
5
|
import * as Digest from 'multiformats/hashes/digest'
|
6
6
|
import { sha256 } from 'multiformats/hashes/sha2'
|
7
|
-
import { CODEC_CERTHASH } from '../../constants.js'
|
7
|
+
import { CODEC_CERTHASH, MAX_MESSAGE_SIZE } from '../../constants.js'
|
8
8
|
import { InvalidFingerprintError, UnsupportedHashAlgorithmError } from '../../error.js'
|
9
|
-
import { MAX_MESSAGE_SIZE } from '../../stream.js'
|
10
9
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
11
10
|
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
12
11
|
|
package/src/stream.ts
CHANGED
@@ -7,8 +7,8 @@ import pDefer from 'p-defer'
|
|
7
7
|
import pTimeout from 'p-timeout'
|
8
8
|
import { raceEvent } from 'race-event'
|
9
9
|
import { raceSignal } from 'race-signal'
|
10
|
-
import { encodingLength } from 'uint8-varint'
|
11
10
|
import { Uint8ArrayList } from 'uint8arraylist'
|
11
|
+
import { BUFFERED_AMOUNT_LOW_TIMEOUT, FIN_ACK_TIMEOUT, MAX_BUFFERED_AMOUNT, MAX_MESSAGE_SIZE, OPEN_TIMEOUT, PROTOBUF_OVERHEAD } from './constants.js'
|
12
12
|
import { Message } from './private-to-public/pb/message.js'
|
13
13
|
import type { DataChannelOptions } from './index.js'
|
14
14
|
import type { RTCDataChannel } from './webrtc/index.js'
|
@@ -27,65 +27,6 @@ export interface WebRTCStreamInit extends AbstractStreamInit, DataChannelOptions
|
|
27
27
|
logger: ComponentLogger
|
28
28
|
}
|
29
29
|
|
30
|
-
/**
|
31
|
-
* How much can be buffered to the DataChannel at once
|
32
|
-
*/
|
33
|
-
export const MAX_BUFFERED_AMOUNT = 2 * 1024 * 1024
|
34
|
-
|
35
|
-
/**
|
36
|
-
* How long time we wait for the 'bufferedamountlow' event to be emitted
|
37
|
-
*/
|
38
|
-
export const BUFFERED_AMOUNT_LOW_TIMEOUT = 30 * 1000
|
39
|
-
|
40
|
-
/**
|
41
|
-
* Max message size that can be sent to the DataChannel. In browsers this is
|
42
|
-
* 256KiB but go-libp2p and rust-libp2p only support 16KiB at the time of
|
43
|
-
* writing.
|
44
|
-
*
|
45
|
-
* @see https://blog.mozilla.org/webrtc/large-data-channel-messages/
|
46
|
-
* @see https://issues.webrtc.org/issues/40644524
|
47
|
-
*/
|
48
|
-
export const MAX_MESSAGE_SIZE = 16 * 1024
|
49
|
-
|
50
|
-
/**
|
51
|
-
* max protobuf overhead:
|
52
|
-
*
|
53
|
-
* ```
|
54
|
-
* [message-length][flag-field-id+type][flag-field-length][flag-field][message-field-id+type][message-field-length][message-field]
|
55
|
-
* ```
|
56
|
-
*/
|
57
|
-
function calculateProtobufOverhead (maxMessageSize = MAX_MESSAGE_SIZE): number {
|
58
|
-
// these have a fixed size
|
59
|
-
const messageLength = encodingLength(maxMessageSize - encodingLength(maxMessageSize))
|
60
|
-
const flagField = 1 + encodingLength(Object.keys(Message.Flag).length - 1) // id+type/value
|
61
|
-
const messageFieldIdType = 1 // id+type
|
62
|
-
const available = maxMessageSize - messageLength - flagField - messageFieldIdType
|
63
|
-
|
64
|
-
// let message-length/message-data fill the rest of the message
|
65
|
-
const messageFieldLengthLength = encodingLength(available)
|
66
|
-
|
67
|
-
return messageLength + flagField + messageFieldIdType + messageFieldLengthLength
|
68
|
-
}
|
69
|
-
|
70
|
-
/**
|
71
|
-
* The protobuf message overhead includes the maximum amount of all bytes in the
|
72
|
-
* protobuf that aren't message field bytes
|
73
|
-
*/
|
74
|
-
export const PROTOBUF_OVERHEAD = calculateProtobufOverhead()
|
75
|
-
|
76
|
-
/**
|
77
|
-
* When closing streams we send a FIN then wait for the remote to
|
78
|
-
* reply with a FIN_ACK. If that does not happen within this timeout
|
79
|
-
* we close the stream anyway.
|
80
|
-
*/
|
81
|
-
export const FIN_ACK_TIMEOUT = 5000
|
82
|
-
|
83
|
-
/**
|
84
|
-
* When sending data messages, if the channel is not in the "open" state, wait
|
85
|
-
* this long for the "open" event to fire.
|
86
|
-
*/
|
87
|
-
export const OPEN_TIMEOUT = 5000
|
88
|
-
|
89
30
|
export class WebRTCStream extends AbstractStream {
|
90
31
|
/**
|
91
32
|
* The data channel used to send and receive data
|
package/src/util.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { detect } from 'detect-browser'
|
2
2
|
import pDefer from 'p-defer'
|
3
3
|
import pTimeout from 'p-timeout'
|
4
|
-
import { DEFAULT_ICE_SERVERS, UFRAG_ALPHABET, UFRAG_PREFIX } from './constants.js'
|
4
|
+
import { DATA_CHANNEL_DRAIN_TIMEOUT, DEFAULT_ICE_SERVERS, UFRAG_ALPHABET, UFRAG_PREFIX } from './constants.js'
|
5
5
|
import type { RTCDataChannel } from './webrtc/index.js'
|
6
6
|
import type { PeerConnection } from '@ipshipyard/node-datachannel'
|
7
7
|
import type { LoggerOptions } from '@libp2p/interface'
|
@@ -13,8 +13,6 @@ export const nopSource = async function * nop (): AsyncGenerator<Uint8Array, any
|
|
13
13
|
|
14
14
|
export const nopSink = async (_: any): Promise<void> => {}
|
15
15
|
|
16
|
-
export const DATA_CHANNEL_DRAIN_TIMEOUT = 30 * 1000
|
17
|
-
|
18
16
|
export function drainAndClose (channel: RTCDataChannel, direction: string, drainTimeout: number = DATA_CHANNEL_DRAIN_TIMEOUT, options: LoggerOptions): void {
|
19
17
|
if (channel.readyState !== 'open') {
|
20
18
|
return
|
package/dist/typedoc-urls.json
DELETED
@@ -1,14 +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
|
-
"TransportCertificate": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.TransportCertificate.html",
|
5
|
-
".:TransportCertificate": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.TransportCertificate.html",
|
6
|
-
"WebRTCDirectTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCDirectTransportComponents.html",
|
7
|
-
"WebRTCTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCTransportComponents.html",
|
8
|
-
"WebRTCTransportDirectInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCTransportDirectInit.html",
|
9
|
-
"WebRTCTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCTransportInit.html",
|
10
|
-
"webRTC": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTC.html",
|
11
|
-
".:webRTC": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTC.html",
|
12
|
-
"webRTCDirect": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTCDirect.html",
|
13
|
-
".:webRTCDirect": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTCDirect.html"
|
14
|
-
}
|