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

Commit

Permalink
fix!: remove @libp2p/components (#223)
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 4a424c0 commit 9c9497f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 26 deletions.
1 change: 0 additions & 1 deletion benchmark/send-and-receive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-console */
'use strict'

/*
$ node benchmark/send-and-receive.js
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/components": "^3.0.0",
"@libp2p/interface-connection": "^3.0.1",
"@libp2p/interface-stream-muxer": "^3.0.0",
"@libp2p/logger": "^2.0.0",
Expand All @@ -159,11 +158,11 @@
"it-stream-types": "^1.0.4",
"rate-limiter-flexible": "^2.3.9",
"uint8arraylist": "^2.1.1",
"uint8arrays": "^3.0.0",
"uint8arrays": "^4.0.2",
"varint": "^6.0.0"
},
"devDependencies": {
"@libp2p/interface-stream-muxer-compliance-tests": "^5.0.0",
"@libp2p/interface-stream-muxer-compliance-tests": "^6.0.0",
"@types/varint": "^6.0.0",
"aegir": "^37.2.0",
"cborg": "^1.8.1",
Expand Down
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Components, Initializable } from '@libp2p/components'
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
import { MplexStreamMuxer } from './mplex.js'

Expand Down Expand Up @@ -39,23 +38,22 @@ export interface MplexInit {
disconnectThreshold?: number
}

export class Mplex implements StreamMuxerFactory, Initializable {
class Mplex implements StreamMuxerFactory {
public protocol = '/mplex/6.7.0'
private readonly _init: MplexInit
private components: Components = new Components()

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

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

createStreamMuxer (init: StreamMuxerInit = {}): StreamMuxer {
return new MplexStreamMuxer(this.components, {
return new MplexStreamMuxer({
...init,
...this._init
})
}
}

export function mplex (init: MplexInit = {}): () => StreamMuxerFactory {
return () => new Mplex(init)
}
5 changes: 1 addition & 4 deletions src/mplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { toString as uint8ArrayToString } from 'uint8arrays'
import { logger } from '@libp2p/logger'
import errCode from 'err-code'
import { RateLimiterMemory } from 'rate-limiter-flexible'
import type { Components } from '@libp2p/components'
import type { Sink } from 'it-stream-types'
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
import type { Stream } from '@libp2p/interface-connection'
Expand Down Expand Up @@ -62,7 +61,7 @@ export class MplexStreamMuxer implements StreamMuxer {
private readonly closeController: AbortController
private readonly rateLimiter: RateLimiterMemory

constructor (components: Components, init?: MplexStreamMuxerInit) {
constructor (init?: MplexStreamMuxerInit) {
init = init ?? {}

this._streamId = 0
Expand Down Expand Up @@ -101,8 +100,6 @@ export class MplexStreamMuxer implements StreamMuxer {
})
}

init (components: Components) {}

/**
* Returns a Map of streams and their ids
*/
Expand Down
6 changes: 3 additions & 3 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-env mocha */

import tests from '@libp2p/interface-stream-muxer-compliance-tests'
import { Mplex } from '../src/index.js'
import { mplex } from '../src/index.js'

describe('compliance', () => {
tests({
async setup () {
return new Mplex({
return mplex({
maxInboundStreams: Infinity,
disconnectThreshold: Infinity
})
})()
},
async teardown () {}
})
Expand Down
14 changes: 7 additions & 7 deletions test/mplex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint max-nested-callbacks: ["error", 5] */

import { expect } from 'aegir/chai'
import { Mplex } from '../src/index.js'
import { mplex } from '../src/index.js'
import { CloseInitiatorMessage, Message, MessageInitiatorMessage, MessageTypes, NewStreamMessage } from '../src/message-types.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
Expand All @@ -18,9 +18,9 @@ import { Uint8ArrayList } from 'uint8arraylist'
describe('mplex', () => {
it('should restrict number of initiator streams per connection', async () => {
const maxOutboundStreams = 10
const factory = new Mplex({
const factory = mplex({
maxOutboundStreams
})
})()
const muxer = factory.createStreamMuxer()

// max out the streams for this connection
Expand All @@ -34,10 +34,10 @@ describe('mplex', () => {

it('should restrict number of recipient streams per connection', async () => {
const maxInboundStreams = 10
const factory = new Mplex({
const factory = mplex({
maxInboundStreams,
disconnectThreshold: Infinity
})
})()
const muxer = factory.createStreamMuxer()
const stream = pushable()

Expand Down Expand Up @@ -122,9 +122,9 @@ describe('mplex', () => {
})()

// create the muxer
const factory = new Mplex({
const factory = mplex({
maxStreamBufferSize
})
})()
const muxer = factory.createStreamMuxer({
onIncomingStream () {
// do nothing with the stream so the buffer fills up
Expand Down

0 comments on commit 9c9497f

Please sign in to comment.