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

Commit 57ae109

Browse files
committed
fix!: remove @libp2p/components
`@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
1 parent 4a424c0 commit 57ae109

File tree

6 files changed

+27
-28
lines changed

6 files changed

+27
-28
lines changed

benchmark/send-and-receive.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable no-console */
2-
'use strict'
32

43
/*
54
$ node benchmark/send-and-receive.js

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@
146146
"release": "aegir release"
147147
},
148148
"dependencies": {
149-
"@libp2p/components": "^3.0.0",
150149
"@libp2p/interface-connection": "^3.0.1",
151150
"@libp2p/interface-stream-muxer": "^3.0.0",
152151
"@libp2p/logger": "^2.0.0",
@@ -159,11 +158,11 @@
159158
"it-stream-types": "^1.0.4",
160159
"rate-limiter-flexible": "^2.3.9",
161160
"uint8arraylist": "^2.1.1",
162-
"uint8arrays": "^3.0.0",
161+
"uint8arrays": "^4.0.2",
163162
"varint": "^6.0.0"
164163
},
165164
"devDependencies": {
166-
"@libp2p/interface-stream-muxer-compliance-tests": "^5.0.0",
165+
"@libp2p/interface-stream-muxer-compliance-tests": "^6.0.0",
167166
"@types/varint": "^6.0.0",
168167
"aegir": "^37.2.0",
169168
"cborg": "^1.8.1",

src/index.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Components, Initializable } from '@libp2p/components'
21
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
32
import { MplexStreamMuxer } from './mplex.js'
43

@@ -39,23 +38,28 @@ export interface MplexInit {
3938
disconnectThreshold?: number
4039
}
4140

42-
export class Mplex implements StreamMuxerFactory, Initializable {
41+
export interface MplexComponents {
42+
43+
}
44+
45+
class Mplex implements StreamMuxerFactory {
4346
public protocol = '/mplex/6.7.0'
4447
private readonly _init: MplexInit
45-
private components: Components = new Components()
48+
private readonly _components: MplexComponents
4649

47-
constructor (init: MplexInit = {}) {
50+
constructor (components: MplexComponents, init: MplexInit = {}) {
51+
this._components = components
4852
this._init = init
4953
}
5054

51-
init (components: Components): void {
52-
this.components = components
53-
}
54-
5555
createStreamMuxer (init: StreamMuxerInit = {}): StreamMuxer {
56-
return new MplexStreamMuxer(this.components, {
56+
return new MplexStreamMuxer(this._components, {
5757
...init,
5858
...this._init
5959
})
6060
}
6161
}
62+
63+
export function mplex (init: MplexInit = {}): (components?: MplexComponents) => StreamMuxerFactory {
64+
return (components: MplexComponents = {}) => new Mplex(components, init)
65+
}

src/mplex.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import { toString as uint8ArrayToString } from 'uint8arrays'
1010
import { logger } from '@libp2p/logger'
1111
import errCode from 'err-code'
1212
import { RateLimiterMemory } from 'rate-limiter-flexible'
13-
import type { Components } from '@libp2p/components'
1413
import type { Sink } from 'it-stream-types'
1514
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
1615
import type { Stream } from '@libp2p/interface-connection'
17-
import type { MplexInit } from './index.js'
16+
import type { MplexComponents, MplexInit } from './index.js'
1817
import anySignal from 'any-signal'
1918
import type { Uint8ArrayList } from 'uint8arraylist'
2019

@@ -62,7 +61,7 @@ export class MplexStreamMuxer implements StreamMuxer {
6261
private readonly closeController: AbortController
6362
private readonly rateLimiter: RateLimiterMemory
6463

65-
constructor (components: Components, init?: MplexStreamMuxerInit) {
64+
constructor (components: MplexComponents, init?: MplexStreamMuxerInit) {
6665
init = init ?? {}
6766

6867
this._streamId = 0
@@ -101,8 +100,6 @@ export class MplexStreamMuxer implements StreamMuxer {
101100
})
102101
}
103102

104-
init (components: Components) {}
105-
106103
/**
107104
* Returns a Map of streams and their ids
108105
*/

test/compliance.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-env mocha */
22

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

66
describe('compliance', () => {
77
tests({
88
async setup () {
9-
return new Mplex({
9+
return mplex({
1010
maxInboundStreams: Infinity,
1111
disconnectThreshold: Infinity
12-
})
12+
})()
1313
},
1414
async teardown () {}
1515
})

test/mplex.spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint max-nested-callbacks: ["error", 5] */
33

44
import { expect } from 'aegir/chai'
5-
import { Mplex } from '../src/index.js'
5+
import { mplex } from '../src/index.js'
66
import { CloseInitiatorMessage, Message, MessageInitiatorMessage, MessageTypes, NewStreamMessage } from '../src/message-types.js'
77
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
88
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
@@ -18,9 +18,9 @@ import { Uint8ArrayList } from 'uint8arraylist'
1818
describe('mplex', () => {
1919
it('should restrict number of initiator streams per connection', async () => {
2020
const maxOutboundStreams = 10
21-
const factory = new Mplex({
21+
const factory = mplex({
2222
maxOutboundStreams
23-
})
23+
})()
2424
const muxer = factory.createStreamMuxer()
2525

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

3535
it('should restrict number of recipient streams per connection', async () => {
3636
const maxInboundStreams = 10
37-
const factory = new Mplex({
37+
const factory = mplex({
3838
maxInboundStreams,
3939
disconnectThreshold: Infinity
40-
})
40+
})()
4141
const muxer = factory.createStreamMuxer()
4242
const stream = pushable()
4343

@@ -122,9 +122,9 @@ describe('mplex', () => {
122122
})()
123123

124124
// create the muxer
125-
const factory = new Mplex({
125+
const factory = mplex({
126126
maxStreamBufferSize
127-
})
127+
})()
128128
const muxer = factory.createStreamMuxer({
129129
onIncomingStream () {
130130
// do nothing with the stream so the buffer fills up

0 commit comments

Comments
 (0)