Skip to content

Commit

Permalink
fix!: remove @libp2p/components (#13)
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 48ca9b6 commit 3fafe00
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ $ npm i @chainsafe/libp2p-yamux
## Usage

```js
import { YamuxMuxer } from '@chainsafe/libp2p-yamux'
import { Components } from '@libp2p/interfaces/components'
import { yamux } from '@chainsafe/libp2p-yamux'
import { pipe } from 'it-pipe'
import { duplexPair } from 'it-pair/duplex'
import all from 'it-all'

// Connect two yamux muxers to demo basic stream multiplexing functionality

const clientMuxer = new YamuxMuxer(new Components(), {
const clientMuxer = yamux({
client: true,
onIncomingStream: stream => {
// echo data on incoming streams
Expand All @@ -41,9 +40,9 @@ const clientMuxer = new YamuxMuxer(new Components(), {
onStreamEnd: stream => {
// do nothing
}
})
})()

const serverMuxer = new YamuxMuxer(new Components(), {
const serverMuxer = yamux({
client: false,
onIncomingStream: stream => {
// echo data on incoming streams
Expand All @@ -52,7 +51,7 @@ const serverMuxer = new YamuxMuxer(new Components(), {
onStreamEnd: stream => {
// do nothing
}
})
})()

// `p` is our "connections", what we use to connect the two sides
// In a real application, a connection is usually to a remote computer
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/components": "^3.0.0",
"@libp2p/interface-connection": "^3.0.1",
"@libp2p/interface-metrics": "^3.0.0",
"@libp2p/interface-stream-muxer": "^3.0.0",
"@libp2p/logger": "^2.0.1",
"@libp2p/tracked-map": "^2.0.2",
Expand All @@ -181,9 +181,9 @@
"iso-random-stream": "^2.0.2",
"it-pipe": "^2.0.4",
"it-pushable": "^3.1.0",
"multiformats": "^9.7.1",
"multiformats": "^10.0.0",
"uint8arraylist": "^2.3.2",
"uint8arrays": "^3.1.0"
"uint8arrays": "^4.0.2"
},
"devDependencies": {
"@dapplion/benchmark": "^0.2.2",
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
import { Yamux } from './muxer.js'
import type { YamuxMuxerInit, YamuxComponents } from './muxer.js'
export { GoAwayCode } from './frame.js'
export * from './muxer.js'

export function yamux (init: YamuxMuxerInit = {}): (components?: YamuxComponents) => StreamMuxerFactory {
return (components: YamuxComponents = {}) => new Yamux(components, init)
}
22 changes: 11 additions & 11 deletions src/muxer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Components } from '@libp2p/components'
import type { Initializable } from '@libp2p/components'
import type { Stream } from '@libp2p/interface-connection'
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
import { abortableSource } from 'abortable-iterator'
Expand All @@ -17,23 +15,25 @@ import { Config, defaultConfig, verifyConfig } from './config.js'
import { Decoder } from './decode.js'
import type { Logger } from '@libp2p/logger'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { Metrics } from '@libp2p/interface-metrics'

const YAMUX_PROTOCOL_ID = '/yamux/1.0.0'

export interface YamuxMuxerInit extends StreamMuxerInit, Partial<Config> {
}

export class Yamux implements StreamMuxerFactory, Initializable {
export interface YamuxComponents {
metrics?: Metrics
}

export class Yamux implements StreamMuxerFactory {
protocol = YAMUX_PROTOCOL_ID
private components = new Components()
private readonly components: YamuxComponents
private readonly _init: YamuxMuxerInit

constructor (init: YamuxMuxerInit = {}) {
this._init = init
}

init (components: Components): void {
constructor (components: YamuxComponents, init: YamuxMuxerInit = {}) {
this.components = components
this._init = init
}

createStreamMuxer (init?: YamuxMuxerInit): YamuxMuxer {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class YamuxMuxer implements StreamMuxer {
private readonly onIncomingStream?: (stream: Stream) => void
private readonly onStreamEnd?: (stream: Stream) => void

constructor (components: Components, init: YamuxMuxerInit) {
constructor (components: YamuxComponents, init: YamuxMuxerInit) {
this._init = init
this.client = init.direction === 'outbound'
this.config = { ...defaultConfig, ...init }
Expand All @@ -94,7 +94,7 @@ export class YamuxMuxer implements StreamMuxer {
this.onIncomingStream = init.onIncomingStream
this.onStreamEnd = init.onStreamEnd

this._streams = trackedMap({ metrics: components.getMetrics(), component: 'yamux', metric: 'streams' })
this._streams = trackedMap({ metrics: components.metrics, component: 'yamux', metric: 'streams' })

this.source = pushable({
onEnd: (err?: Error): void => {
Expand Down
2 changes: 1 addition & 1 deletion test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TestYamux } from './util.js'
describe('compliance', () => {
tests({
async setup () {
return new TestYamux()
return new TestYamux({})
},
async teardown () {}
})
Expand Down
3 changes: 1 addition & 2 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Components } from '@libp2p/components'
import { logger } from '@libp2p/logger'
import type { Transform } from 'it-stream-types'
import { duplexPair } from 'it-pair/duplex'
Expand Down Expand Up @@ -34,7 +33,7 @@ export class TestYamux extends Yamux {
}

export function testYamuxMuxer (name: string, client: boolean, conf: YamuxMuxerInit = {}) {
return new YamuxMuxer(new Components(), {
return new YamuxMuxer({}, {
...testConf,
...conf,
direction: client ? 'outbound' : 'inbound',
Expand Down

0 comments on commit 3fafe00

Please sign in to comment.