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

Commit

Permalink
fix!: remove @libp2p/components (#219)
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 adf01ac commit be2dbc3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 57 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ $ npm i @libp2p/tcp
## Usage

```js
import { TCP } from '@libp2p/tcp'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import {pipe} from 'it-pipe'
import { pipe } from 'it-pipe'
import all from 'it-all'

// A simple upgrader that just returns the MultiaddrConnection
Expand All @@ -47,9 +47,9 @@ const upgrader = {
upgradeOutbound: async maConn => maConn
}

const tcp = new TCP()
const transport = tcp()()

const listener = tcp.createListener({
const listener = transport.createListener({
upgrader,
handler: (socket) => {
console.log('new connection opened')
Expand All @@ -64,7 +64,7 @@ const addr = multiaddr('/ip4/127.0.0.1/tcp/9090')
await listener.listen(addr)
console.log('listening')

const socket = await tcp.dial(addr, { upgrader })
const socket = await transport.dial(addr, { upgrader })
const values = await pipe(
socket,
all
Expand All @@ -89,7 +89,7 @@ Value: hello World!

[![](https://github.com/raw/libp2p/js-libp2p-interfaces/master/packages/libp2p-interfaces/src/transport/img/badge.png)](https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-interfaces/src/transport)

`libp2p-tcp` accepts TCP addresses as both IPFS and non IPFS encapsulated addresses, i.e:
`@libp2p/tcp` accepts TCP addresses as both IPFS and non IPFS encapsulated addresses, i.e:

`/ip4/127.0.0.1/tcp/4001`
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@
"stream-to-it": "^0.2.2"
},
"devDependencies": {
"@libp2p/interface-mocks": "^6.0.0",
"@libp2p/interface-transport-compliance-tests": "^2.0.6",
"@libp2p/interface-mocks": "^7.0.1",
"@libp2p/interface-transport-compliance-tests": "^3.0.0",
"aegir": "^37.5.3",
"it-all": "^1.0.6",
"it-pipe": "^2.0.3",
"p-defer": "^4.0.0",
"sinon": "^14.0.0",
"uint8arrays": "^3.0.0"
"uint8arrays": "^4.0.2"
}
}
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface TCPCreateListenerOptions extends CreateListenerOptions, TCPSock

}

export class TCP implements Transport {
class TCP implements Transport {
private readonly opts: TCPOptions

constructor (options: TCPOptions = {}) {
Expand Down Expand Up @@ -189,3 +189,9 @@ export class TCP implements Transport {
})
}
}

export function tcp (init: TCPOptions = {}): (components?: any) => Transport {
return () => {
return new TCP(init)
}
}
6 changes: 3 additions & 3 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import sinon from 'sinon'
import tests from '@libp2p/interface-transport-compliance-tests'
import { multiaddr } from '@multiformats/multiaddr'
import net from 'net'
import { TCP } from '../src/index.js'
import { tcp } from '../src/index.js'

describe('interface-transport compliance', () => {
tests({
async setup () {
const tcp = new TCP()
const transport = tcp()()
const addrs = [
multiaddr('/ip4/127.0.0.1/tcp/9091'),
multiaddr('/ip4/127.0.0.1/tcp/9092'),
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('interface-transport compliance', () => {
}
}

return { transport: tcp, addrs, connector }
return { transport, addrs, connector }
},
async teardown () {}
})
Expand Down
16 changes: 8 additions & 8 deletions test/connection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { expect } from 'aegir/chai'
import { TCP } from '../src/index.js'
import { tcp } from '../src/index.js'
import { multiaddr } from '@multiformats/multiaddr'
import { mockUpgrader } from '@libp2p/interface-mocks'
import type { Connection } from '@libp2p/interface-connection'
import type { Upgrader } from '@libp2p/interface-transport'
import type { Transport, Upgrader } from '@libp2p/interface-transport'

describe('valid localAddr and remoteAddr', () => {
let tcp: TCP
let transport: Transport
let upgrader: Upgrader

beforeEach(() => {
tcp = new TCP()
transport = tcp()()
upgrader = mockUpgrader()
})

Expand All @@ -24,7 +24,7 @@ describe('valid localAddr and remoteAddr', () => {
const handler = (conn: Connection) => handled(conn)

// Create a listener with the handler
const listener = tcp.createListener({
const listener = transport.createListener({
handler,
upgrader
})
Expand All @@ -36,7 +36,7 @@ describe('valid localAddr and remoteAddr', () => {
expect(localAddrs.length).to.equal(1)

// Dial to that address
await tcp.dial(localAddrs[0], {
await transport.dial(localAddrs[0], {
upgrader
})

Expand All @@ -55,7 +55,7 @@ describe('valid localAddr and remoteAddr', () => {
const handler = (conn: Connection) => handled(conn)

// Create a listener with the handler
const listener = tcp.createListener({
const listener = transport.createListener({
handler,
upgrader
})
Expand All @@ -67,7 +67,7 @@ describe('valid localAddr and remoteAddr', () => {
expect(localAddrs.length).to.equal(1)

// Dial to that address
const dialerConn = await tcp.dial(localAddrs[0], {
const dialerConn = await transport.dial(localAddrs[0], {
upgrader
})

Expand Down
11 changes: 6 additions & 5 deletions test/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { expect } from 'aegir/chai'
import { TCP } from '../src/index.js'
import { tcp } from '../src/index.js'
import { multiaddr } from '@multiformats/multiaddr'
import type { Transport } from '@libp2p/interface-transport'

describe('filter addrs', () => {
const base = '/ip4/127.0.0.1'
const ipfs = '/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw'

let tcp: TCP
let transport: Transport

before(() => {
tcp = new TCP()
transport = tcp()()
})

it('filter valid addrs for this transport', () => {
Expand All @@ -22,7 +23,7 @@ describe('filter addrs', () => {
const ma7 = multiaddr('/dns4/libp2p.io/tcp/9090')
const ma8 = multiaddr('/dnsaddr/libp2p.io/tcp/9090')

const valid = tcp.filter([ma1, ma2, ma3, ma4, ma5, ma6, ma7, ma8])
const valid = transport.filter([ma1, ma2, ma3, ma4, ma5, ma6, ma7, ma8])
expect(valid.length).to.equal(4)
expect(valid[0]).to.deep.equal(ma1)
expect(valid[1]).to.deep.equal(ma4)
Expand All @@ -31,7 +32,7 @@ describe('filter addrs', () => {
it('filter a single addr for this transport', () => {
const ma1 = multiaddr(base + '/tcp/9090')

const valid = tcp.filter([ma1])
const valid = transport.filter([ma1])
expect(valid.length).to.equal(1)
expect(valid[0]).to.eql(ma1)
})
Expand Down
Loading

0 comments on commit be2dbc3

Please sign in to comment.