Skip to content

Commit

Permalink
feat: WebRTC-Direct support for Node.js
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain committed Jun 7, 2024
1 parent 90d10b5 commit eb61d74
Show file tree
Hide file tree
Showing 18 changed files with 698 additions and 94 deletions.
17 changes: 15 additions & 2 deletions packages/integration-tests/test/interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { mplex } from '@libp2p/mplex'
import { peerIdFromKeys } from '@libp2p/peer-id'
import { tcp } from '@libp2p/tcp'
import { tls } from '@libp2p/tls'
import { webRTCDirect } from '@libp2p/webrtc'
import { multiaddr } from '@multiformats/multiaddr'
import { execa } from 'execa'
import { path as p2pd } from 'go-libp2p'
Expand Down Expand Up @@ -45,6 +46,12 @@ async function createGoPeer (options: SpawnOptions): Promise<Daemon> {

if (options.noListen === true) {
opts.push('-noListenAddrs')

if (options.transport === 'webrtc-direct') {
// dialing webrtc-direct is broken in go-libp2p at the moment
// https://github.com/libp2p/go-libp2p/issues/2827
throw new UnsupportedError()
}
} else {
if (options.transport == null || options.transport === 'tcp') {
opts.push('-hostAddrs=/ip4/127.0.0.1/tcp/0')
Expand Down Expand Up @@ -132,7 +139,11 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
addresses: {
listen: []
},
transports: [tcp(), circuitRelayTransport()],
transports: [
tcp(),
circuitRelayTransport(),
webRTCDirect()
],
streamMuxers: [],
connectionEncryption: [noise()],
connectionManager: {
Expand All @@ -143,12 +154,14 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
if (options.noListen !== true) {
if (options.transport == null || options.transport === 'tcp') {
opts.addresses?.listen?.push('/ip4/127.0.0.1/tcp/0')
} else if (options.transport === 'webrtc-direct') {
opts.addresses?.listen?.push('/ip4/127.0.0.1/udp/0/webrtc-direct')
} else {
throw new UnsupportedError()
}
}

if (options.transport === 'webtransport' || options.transport === 'webrtc-direct') {
if (options.transport === 'webtransport') {
throw new UnsupportedError()
}

Expand Down
7 changes: 6 additions & 1 deletion packages/transport-webrtc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"doc-check": "aegir doc-check"
},
"dependencies": {
"@chainsafe/is-ip": "^2.0.2",
"@chainsafe/libp2p-noise": "^15.0.0",
"@libp2p/interface": "^1.4.0",
"@libp2p/interface-internal": "^1.2.2",
Expand All @@ -58,6 +59,7 @@
"@multiformats/mafmt": "^12.1.6",
"@multiformats/multiaddr": "^12.2.3",
"@multiformats/multiaddr-matcher": "^1.2.1",
"@peculiar/x509": "^1.11.0",
"detect-browser": "^5.3.0",
"it-length-prefixed": "^9.0.4",
"it-protobuf-stream": "^1.1.3",
Expand All @@ -72,6 +74,7 @@
"protons-runtime": "^5.4.0",
"race-signal": "^1.0.2",
"react-native-webrtc": "^118.0.7",
"stun": "^2.1.0",
"uint8arraylist": "^2.4.8",
"uint8arrays": "^5.1.0"
},
Expand All @@ -98,7 +101,9 @@
"sinon-ts": "^2.0.0"
},
"browser": {
"./dist/src/webrtc/index.js": "./dist/src/webrtc/index.browser.js"
"./dist/src/webrtc/index.js": "./dist/src/webrtc/index.browser.js",
"./dist/src/private-to-public/listener.js": "./dist/src/private-to-public/listener.browser.js",
"./dist/src/private-to-public/utils/get-dialer-rtcpeerconnection.js": "./dist/src/private-to-public/utils/get-dialer-rtcpeerconnection.browser.js"
},
"react-native": {
"./dist/src/webrtc/index.js": "./dist/src/webrtc/index.react-native.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TypedEventEmitter } from '@libp2p/interface'
import { unimplemented } from '../error.js'
import type { PeerId, ListenerEvents, Listener } from '@libp2p/interface'
import type { TransportManager } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'

export interface WebRTCDirectListenerComponents {
peerId: PeerId
transportManager: TransportManager
}

export interface WebRTCDirectListenerInit {
shutdownController: AbortController
}

export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> implements Listener {
async listen (): Promise<void> {
throw unimplemented('WebRTCTransport.createListener')
}

getAddrs (): Multiaddr[] {
return []
}

async close (): Promise<void> {

}
}
Loading

0 comments on commit eb61d74

Please sign in to comment.