Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
fix!: remove @libp2p/components (#477)
Browse files Browse the repository at this point in the history
`@libp2p/components` is a choke-point for our dependency graph as it depends on every interface, meaning when one interface revs a major `@libp2p/components` major has to change too which means every module depending on it also needs a major.

Switch instead to constructor injection of simple objects that let modules declare their dependencies on interfaces directly instead of indirectly via `@libp2p/components`

Refs libp2p/js-libp2p-components#6

BREAKING CHANGE: modules no longer implement `Initializable` instead switching to constructor injection
  • Loading branch information
achingbrain authored Oct 12, 2022
1 parent 69032c8 commit efbcd99
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 44 deletions.
12 changes: 5 additions & 7 deletions packages/webrtc-star-transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/components": "^3.0.0",
"@libp2p/interface-connection": "^3.0.1",
"@libp2p/interface-peer-discovery": "^1.0.0",
"@libp2p/interface-peer-id": "^1.0.2",
Expand All @@ -155,15 +154,15 @@
"delay": "^5.0.0",
"err-code": "^3.0.1",
"iso-random-stream": "^2.0.2",
"multiformats": "^9.6.3",
"multiformats": "^10.0.0",
"p-defer": "^4.0.0",
"socket.io-client": "^4.1.2",
"uint8arrays": "^3.0.0"
"uint8arrays": "^4.0.2"
},
"devDependencies": {
"@libp2p/interface-mocks": "^6.0.0",
"@libp2p/interface-peer-discovery-compliance-tests": "^1.0.1",
"@libp2p/interface-transport-compliance-tests": "^2.0.5",
"@libp2p/interface-mocks": "^7.0.1",
"@libp2p/interface-peer-discovery-compliance-tests": "^2.0.0",
"@libp2p/interface-transport-compliance-tests": "^3.0.0",
"@libp2p/peer-id-factory": "^1.0.9",
"@libp2p/webrtc-star-signalling-server": "^2.0.0",
"@mapbox/node-pre-gyp": "^1.0.5",
Expand All @@ -176,7 +175,6 @@
"p-event": "^5.0.1",
"p-wait-for": "^5.0.0",
"sinon": "^14.0.0",
"uint8arrays": "^3.0.0",
"uint8arraylist": "^2.3.2",
"util": "^0.12.4",
"wrtc": "^0.4.6"
Expand Down
28 changes: 17 additions & 11 deletions packages/webrtc-star-transport/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import type { Connection, MultiaddrConnection } from '@libp2p/interface-connecti
import type { Transport, Listener, DialOptions, CreateListenerOptions } from '@libp2p/interface-transport'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
import type { WebRTCStarSocket, HandshakeSignal } from '@libp2p/webrtc-star-protocol'
import { Components, Initializable } from '@libp2p/components'
import { symbol as peerDiscoverySymbol } from '@libp2p/interface-peer-discovery'
import type { PeerId } from '@libp2p/interface-peer-id'

const webrtcSupport = 'RTCPeerConnection' in globalThis
const log = logger('libp2p:webrtc-star')
Expand Down Expand Up @@ -90,20 +90,26 @@ export interface SignalServer extends EventEmitter<SignalServerServerEvents> {
close: () => Promise<void>
}

export interface WebRTCStarComponents {
peerId: PeerId
}

/**
* @class WebRTCStar
*/
export class WebRTCStar implements Transport, Initializable {
export class WebRTCStar implements Transport {
public wrtc?: WRTC
public discovery: PeerDiscovery & Startable
public sigServers: Map<string, SignalServer>
private components: Components = new Components()
private readonly components: WebRTCStarComponents

constructor (options?: WebRTCStarInit) {
if (options?.wrtc != null) {
this.wrtc = options.wrtc
constructor (components: WebRTCStarComponents, init?: WebRTCStarInit) {
if (init?.wrtc != null) {
this.wrtc = init.wrtc
}

this.components = components

// Keep Signalling references
this.sigServers = new Map()

Expand All @@ -120,10 +126,6 @@ export class WebRTCStar implements Transport, Initializable {
return '@libp2p/webrtc-star'
}

init (components: Components) {
this.components = components
}

async dial (ma: Multiaddr, options: WebRTCStarDialOptions) {
const rawConn = await this._connect(ma, options)
const maConn = toMultiaddrConnection(rawConn, { remoteAddr: ma, signal: options.signal })
Expand Down Expand Up @@ -248,7 +250,7 @@ export class WebRTCStar implements Transport, Initializable {
options.channelOptions.wrtc = this.wrtc
}

return createListener(options.upgrader, options.handler ?? noop, this.components.getPeerId(), this, options)
return createListener(options.upgrader, options.handler ?? noop, this.components.peerId, this, options)
}

/**
Expand Down Expand Up @@ -288,3 +290,7 @@ export class WebRTCStar implements Transport, Initializable {
}))
}
}

export function webRTCStar (init: WebRTCStarInit = {}): (components: WebRTCStarComponents) => WebRTCStar {
return (components: WebRTCStarComponents) => new WebRTCStar(components, init)
}
6 changes: 2 additions & 4 deletions packages/webrtc-star-transport/test/browser.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/* eslint-env mocha */

import { WebRTCStar } from '../src/index.js'
import { webRTCStar } from '../src/index.js'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import dialTests from './transport/dial.js'
import listenTests from './transport/listen.js'
import discoveryTests from './transport/discovery.js'
import filterTests from './transport/filter.js'
import { Components } from '@libp2p/components'
import { mockRegistrar, mockUpgrader } from '@libp2p/interface-mocks'

describe('browser RTC', () => {
const create = async () => {
const peerId = await createEd25519PeerId()
const ws = new WebRTCStar()
ws.init(new Components({ peerId }))
const ws = webRTCStar()({ peerId })

const registrar = mockRegistrar()
const upgrader = mockUpgrader({ registrar })
Expand Down
9 changes: 3 additions & 6 deletions packages/webrtc-star-transport/test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import sinon from 'sinon'
import { multiaddr } from '@multiformats/multiaddr'
import testsTransport from '@libp2p/interface-transport-compliance-tests'
import testsDiscovery from '@libp2p/interface-peer-discovery-compliance-tests'
import { WebRTCStar } from '../src/index.js'
import { webRTCStar } from '../src/index.js'
import pWaitFor from 'p-wait-for'
import { peerIdFromString } from '@libp2p/peer-id'
import { Components } from '@libp2p/components'

describe('interface-transport compliance', function () {
testsTransport({
async setup () {
const peerId = peerIdFromString('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a')
const ws = new WebRTCStar({ wrtc })
ws.init(new Components({ peerId }))
const ws = webRTCStar({ wrtc })({ peerId })

const base = (id: string) => {
return `/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/p2p/${id}`
Expand Down Expand Up @@ -49,8 +47,7 @@ describe('interface-discovery compliance', () => {
testsDiscovery({
async setup () {
const peerId = peerIdFromString('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2d')
const ws = new WebRTCStar({ wrtc })
ws.init(new Components({ peerId }))
const ws = webRTCStar({ wrtc })({ peerId })
const maStr = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2d'

const discovery = ws.discovery
Expand Down
13 changes: 5 additions & 8 deletions packages/webrtc-star-transport/test/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import wrtc from 'wrtc'
// @ts-expect-error no types
import electronWebRTC from 'electron-webrtc'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { WebRTCStar } from '../src/index.js'
import { webRTCStar } from '../src/index.js'
import dialTests from './transport/dial.js'
import listenTests from './transport/listen.js'
import discoveryTests from './transport/discovery.js'
import filterTests from './transport/filter.js'
import multipleSignalServersTests from './transport/multiple-signal-servers.js'
import trackTests from './transport/track.js'
import reconnectTests from './transport/reconnect.node.js'
import { Components } from '@libp2p/components'
import type { PeerTransport } from './index.js'
import { mockRegistrar, mockUpgrader } from '@libp2p/interface-mocks'

Expand All @@ -24,10 +23,9 @@ process.on('beforeExit', (code) => process.exit(code))
describe('transport: with wrtc', () => {
const create = async (): Promise<PeerTransport> => {
const peerId = await createEd25519PeerId()
const ws = new WebRTCStar({
const ws = webRTCStar({
wrtc
})
ws.init(new Components({ peerId }))
})({ peerId })

const registrar = mockRegistrar()
const upgrader = mockUpgrader({ registrar })
Expand All @@ -53,10 +51,9 @@ describe('transport: with wrtc', () => {
describe.skip('transport: with electron-webrtc', () => {
const create = async () => {
const peerId = await createEd25519PeerId()
const ws = new WebRTCStar({
const ws = webRTCStar({
wrtc: electronWebRTC()
})
ws.init(new Components({ peerId }))
})({ peerId })

const registrar = mockRegistrar()
const upgrader = mockUpgrader({ registrar })
Expand Down
12 changes: 4 additions & 8 deletions packages/webrtc-star-transport/test/transport/instance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/* eslint-env mocha */

import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
import { WebRTCStar } from '../../src/index.js'
import { webRTCStar } from '../../src/index.js'

describe('instantiate the transport', () => {
it('create', () => {
const wstar = new WebRTCStar()
it('create', async () => {
const wstar = webRTCStar()({ peerId: await createEd25519PeerId() })
expect(wstar).to.exist()
})

it('create without new', () => {
// @ts-expect-error WebRTCStar is a class and needs new
expect(() => WebRTCStar()).to.throw()
})
})

0 comments on commit efbcd99

Please sign in to comment.