Skip to content

Commit eb61d74

Browse files
committed
feat: WebRTC-Direct support for Node.js
Supports listening and dialing WebRTC Direct multiaddrs in Node.js. Depends on: - [ ] libp2p/go-libp2p#2827 - [ ] paullouisageneau/libdatachannel#1201 - [ ] paullouisageneau/libdatachannel#1204 - [ ] murat-dogan/node-datachannel#257 - [ ] murat-dogan/node-datachannel#256 Closes: - #2581
1 parent 90d10b5 commit eb61d74

18 files changed

+698
-94
lines changed

packages/integration-tests/test/interop.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { mplex } from '@libp2p/mplex'
1515
import { peerIdFromKeys } from '@libp2p/peer-id'
1616
import { tcp } from '@libp2p/tcp'
1717
import { tls } from '@libp2p/tls'
18+
import { webRTCDirect } from '@libp2p/webrtc'
1819
import { multiaddr } from '@multiformats/multiaddr'
1920
import { execa } from 'execa'
2021
import { path as p2pd } from 'go-libp2p'
@@ -45,6 +46,12 @@ async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
4546

4647
if (options.noListen === true) {
4748
opts.push('-noListenAddrs')
49+
50+
if (options.transport === 'webrtc-direct') {
51+
// dialing webrtc-direct is broken in go-libp2p at the moment
52+
// https://github.com/libp2p/go-libp2p/issues/2827
53+
throw new UnsupportedError()
54+
}
4855
} else {
4956
if (options.transport == null || options.transport === 'tcp') {
5057
opts.push('-hostAddrs=/ip4/127.0.0.1/tcp/0')
@@ -132,7 +139,11 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
132139
addresses: {
133140
listen: []
134141
},
135-
transports: [tcp(), circuitRelayTransport()],
142+
transports: [
143+
tcp(),
144+
circuitRelayTransport(),
145+
webRTCDirect()
146+
],
136147
streamMuxers: [],
137148
connectionEncryption: [noise()],
138149
connectionManager: {
@@ -143,12 +154,14 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
143154
if (options.noListen !== true) {
144155
if (options.transport == null || options.transport === 'tcp') {
145156
opts.addresses?.listen?.push('/ip4/127.0.0.1/tcp/0')
157+
} else if (options.transport === 'webrtc-direct') {
158+
opts.addresses?.listen?.push('/ip4/127.0.0.1/udp/0/webrtc-direct')
146159
} else {
147160
throw new UnsupportedError()
148161
}
149162
}
150163

151-
if (options.transport === 'webtransport' || options.transport === 'webrtc-direct') {
164+
if (options.transport === 'webtransport') {
152165
throw new UnsupportedError()
153166
}
154167

packages/transport-webrtc/package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"doc-check": "aegir doc-check"
5151
},
5252
"dependencies": {
53+
"@chainsafe/is-ip": "^2.0.2",
5354
"@chainsafe/libp2p-noise": "^15.0.0",
5455
"@libp2p/interface": "^1.4.0",
5556
"@libp2p/interface-internal": "^1.2.2",
@@ -58,6 +59,7 @@
5859
"@multiformats/mafmt": "^12.1.6",
5960
"@multiformats/multiaddr": "^12.2.3",
6061
"@multiformats/multiaddr-matcher": "^1.2.1",
62+
"@peculiar/x509": "^1.11.0",
6163
"detect-browser": "^5.3.0",
6264
"it-length-prefixed": "^9.0.4",
6365
"it-protobuf-stream": "^1.1.3",
@@ -72,6 +74,7 @@
7274
"protons-runtime": "^5.4.0",
7375
"race-signal": "^1.0.2",
7476
"react-native-webrtc": "^118.0.7",
77+
"stun": "^2.1.0",
7578
"uint8arraylist": "^2.4.8",
7679
"uint8arrays": "^5.1.0"
7780
},
@@ -98,7 +101,9 @@
98101
"sinon-ts": "^2.0.0"
99102
},
100103
"browser": {
101-
"./dist/src/webrtc/index.js": "./dist/src/webrtc/index.browser.js"
104+
"./dist/src/webrtc/index.js": "./dist/src/webrtc/index.browser.js",
105+
"./dist/src/private-to-public/listener.js": "./dist/src/private-to-public/listener.browser.js",
106+
"./dist/src/private-to-public/utils/get-dialer-rtcpeerconnection.js": "./dist/src/private-to-public/utils/get-dialer-rtcpeerconnection.browser.js"
102107
},
103108
"react-native": {
104109
"./dist/src/webrtc/index.js": "./dist/src/webrtc/index.react-native.js"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { TypedEventEmitter } from '@libp2p/interface'
2+
import { unimplemented } from '../error.js'
3+
import type { PeerId, ListenerEvents, Listener } from '@libp2p/interface'
4+
import type { TransportManager } from '@libp2p/interface-internal'
5+
import type { Multiaddr } from '@multiformats/multiaddr'
6+
7+
export interface WebRTCDirectListenerComponents {
8+
peerId: PeerId
9+
transportManager: TransportManager
10+
}
11+
12+
export interface WebRTCDirectListenerInit {
13+
shutdownController: AbortController
14+
}
15+
16+
export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> implements Listener {
17+
async listen (): Promise<void> {
18+
throw unimplemented('WebRTCTransport.createListener')
19+
}
20+
21+
getAddrs (): Multiaddr[] {
22+
return []
23+
}
24+
25+
async close (): Promise<void> {
26+
27+
}
28+
}

0 commit comments

Comments
 (0)