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

Commit fc53a59

Browse files
authored
fix!: remove @libp2p/components (#194)
`@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 5cf8ccc commit fc53a59

11 files changed

+39
-78
lines changed

.aegir.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ process.on('beforeExit', (code) => process.exit(code))
99
const ECHO_PROTOCOL = '/echo/1.0.0'
1010

1111
async function before () {
12-
const { WebRTCDirect } = await import('./dist/src/index.js')
12+
const { webRTCDirect } = await import('./dist/src/index.js')
1313
const { pipe } = await import('it-pipe')
1414
const { multiaddr } = await import('@multiformats/multiaddr')
1515
const { mockUpgrader, mockRegistrar } = await import('@libp2p/interface-mocks')
@@ -28,9 +28,9 @@ async function before () {
2828
registrar
2929
})
3030

31-
const wd = new WebRTCDirect({
31+
const wd = webRTCDirect({
3232
wrtc
33-
})
33+
})()
3434

3535
const listeners = await Promise.all(
3636
[REMOTE_MULTIADDR_IP4, REMOTE_MULTIADDR_IP6].map(async ma => {

README.md

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,55 +33,19 @@ $ npm i @libp2p/webrtc-direct
3333
## Usage
3434

3535
```js
36-
import { WebRTCDirect } from '@libp2p/webrtc-direct'
37-
import { multiaddr } from '@multiformats/multiaddr'
38-
import { pipe } from 'it-pipe'
39-
import all from 'it-all'
40-
41-
const ECHO_PROTOCOL = '/echo/1.0.0'
42-
const addr = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
43-
const webRTCDirect = new WebRTCDirect()
44-
45-
const listener = webRTCDirect.createListener({
46-
handler: (connection) => {
47-
console.log('new connection opened')
48-
49-
connection.newStream([ECHO_PROTOCOL])
50-
.then(({ stream }) => {
51-
void pipe(stream, stream)
52-
})
53-
},
54-
upgrader
36+
import { createLibp2pNode } from 'libp2p'
37+
import { webRTCDirect } from '@libp2p/webrtc-direct'
38+
39+
const node = await createLibp2p({
40+
transports: [
41+
webRTCDirect()
42+
]
43+
//... other config
5544
})
56-
57-
await listener.listen(addr)
58-
console.log('listening')
59-
60-
const connection = await webRTCDirect.dial(addr, {
61-
upgrader
62-
})
63-
const { stream } = await connection.newStream([ECHO_PROTOCOL])
64-
const values = await pipe(
65-
[uint8arrayFromString('hello')],
66-
stream,
67-
(source) => all(source)
68-
)
69-
console.log(`Value: ${uint8ArrayToString(values[0])}`)
70-
71-
// Close connection after reading
72-
await listener.close()
45+
await node.start()
46+
await node.dial('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
7347
```
7448

75-
Outputs:
76-
77-
```sh
78-
listening
79-
new connection opened
80-
Value: hello
81-
```
82-
83-
Note that it may take some time for the connection to be established.
84-
8549
## API
8650

8751
### Transport

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@
148148
"@multiformats/multiaddr": "^11.0.0",
149149
"abortable-iterator": "^4.0.2",
150150
"err-code": "^3.0.0",
151-
"multiformats": "^9.4.5",
151+
"multiformats": "^10.0.0",
152152
"native-fetch": "^4.0.2",
153153
"p-event": "^5.0.1",
154-
"uint8arrays": "^3.0.0",
154+
"uint8arrays": "^4.0.2",
155155
"undici": "^5.2.0",
156156
"wherearewe": "^2.0.1"
157157
},
158158
"devDependencies": {
159-
"@libp2p/interface-mocks": "^6.0.1",
160-
"@libp2p/interface-transport-compliance-tests": "^2.0.6",
159+
"@libp2p/interface-mocks": "^7.0.1",
160+
"@libp2p/interface-transport-compliance-tests": "^3.0.0",
161161
"@mapbox/node-pre-gyp": "^1.0.8",
162162
"aegir": "^37.5.3",
163163
"it-all": "^1.0.6",

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface WebRTCDirectInit {
2121
recieverOptions?: WebRTCReceiverInit
2222
}
2323

24-
export class WebRTCDirect implements Transport {
24+
class WebRTCDirect implements Transport {
2525
private readonly initiatorOptions?: WebRTCInitiatorInit
2626
private readonly recieverOptions?: WebRTCReceiverInit
2727
public wrtc?: WRTC
@@ -198,3 +198,7 @@ export class WebRTCDirect implements Transport {
198198
})
199199
}
200200
}
201+
202+
export function webRTCDirect (init: WebRTCDirectInit = {}): () => Transport {
203+
return () => new WebRTCDirect(init)
204+
}

test/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import listenTests from './listen.js'
33
import dialTests from './dial.js'
4-
import { WebRTCDirect } from '../src/index.js'
4+
import { webRTCDirect } from '../src/index.js'
55

66
describe('browser RTC', () => {
77
const create = async () => {
8-
const ws = new WebRTCDirect()
8+
const ws = webRTCDirect()()
99

1010
return ws
1111
}

test/compliance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-env mocha */
22

3+
import type { Transport } from '@libp2p/interface-transport'
34
import tests from '@libp2p/interface-transport-compliance-tests'
45
import { multiaddr } from '@multiformats/multiaddr'
5-
import type { WebRTCDirect } from '../src/index.js'
66

7-
export default (create: () => Promise<WebRTCDirect>) => {
7+
export default (create: () => Promise<Transport>) => {
88
describe('interface-transport compliance', function () {
99
this.timeout(20 * 1000)
1010

test/dial.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { pipe } from 'it-pipe'
66
import all from 'it-all'
77
import { fromString } from 'uint8arrays/from-string'
88
import { mockRegistrar, mockUpgrader } from '@libp2p/interface-mocks'
9-
import type { WebRTCDirect } from '../src/index.js'
10-
import type { Upgrader } from '@libp2p/interface-transport'
9+
import type { Transport, Upgrader } from '@libp2p/interface-transport'
1110
import type { Uint8ArrayList } from 'uint8arraylist'
1211
import type { Source } from 'it-stream-types'
1312

@@ -22,7 +21,7 @@ async function * toBytes (source: Source<Uint8ArrayList>) {
2221
}
2322
}
2423

25-
export default (create: () => Promise<WebRTCDirect>) => {
24+
export default (create: () => Promise<Transport>) => {
2625
describe('dial', function () {
2726
this.timeout(20 * 1000)
2827

test/filter.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import { expect } from 'aegir/chai'
44
import { multiaddr } from '@multiformats/multiaddr'
5-
import { WebRTCDirect } from '../src/index.js'
5+
import { webRTCDirect } from '../src/index.js'
66

77
describe('filter', () => {
88
it('filters non valid webrtc-direct multiaddrs', () => {
9-
const wd = new WebRTCDirect()
9+
const wd = webRTCDirect()()
1010
const maArr = [
1111
multiaddr('/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct'),
1212
multiaddr('/ip4/127.0.0.1/tcp/9090/ws'),
@@ -21,7 +21,7 @@ describe('filter', () => {
2121
})
2222

2323
it('filter a single addr for this transport', () => {
24-
const wd = new WebRTCDirect()
24+
const wd = webRTCDirect()()
2525
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
2626

2727
const filtered = wd.filter([ma])

test/instance.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
/* eslint-env mocha */
22

33
import { expect } from 'aegir/chai'
4-
import { WebRTCDirect } from '../src/index.js'
4+
import { webRTCDirect } from '../src/index.js'
55

66
describe('instances', () => {
77
it('create', (done) => {
8-
const wdirect = new WebRTCDirect()
8+
const wdirect = webRTCDirect()()
99
expect(wdirect).to.exist()
1010
done()
1111
})
12-
13-
it('create without new throws', (done) => {
14-
// @ts-expect-error need new keyword
15-
expect(() => WebRTCDirect()).to.throw()
16-
done()
17-
})
1812
})

test/listen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import { expect } from 'aegir/chai'
44
import { multiaddr } from '@multiformats/multiaddr'
55
import { mockRegistrar, mockUpgrader } from '@libp2p/interface-mocks'
66
import { isBrowser } from 'wherearewe'
7-
import type { WebRTCDirect } from '../src/index.js'
87
import { pipe } from 'it-pipe'
98
import { pEvent } from 'p-event'
9+
import type { Transport } from '@libp2p/interface-transport'
1010

1111
const ECHO_PROTOCOL = '/echo/1.0.0'
1212

13-
export default (create: () => Promise<WebRTCDirect>) => {
13+
export default (create: () => Promise<Transport>) => {
1414
describe('listen', function () {
1515
this.timeout(20 * 1000)
1616

1717
if (isBrowser) {
1818
return
1919
}
2020

21-
let wd: WebRTCDirect
21+
let wd: Transport
2222

2323
const ma = multiaddr('/ip4/127.0.0.1/tcp/20123/http/p2p-webrtc-direct')
2424

test/node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import complianceTests from './compliance.js'
44
import dialTests from './dial.js'
55
// @ts-expect-error no types
66
import wrtc from 'wrtc'
7-
import { WebRTCDirect } from '../src/index.js'
7+
import { webRTCDirect } from '../src/index.js'
88

99
// TODO: Temporary fix per wrtc issue
1010
// https://github.com/node-webrtc/node-webrtc/issues/636#issuecomment-774171409
1111
process.on('beforeExit', (code) => process.exit(code))
1212

1313
describe('transport: with wrtc', () => {
1414
const create = async () => {
15-
const ws = new WebRTCDirect({
15+
const ws = webRTCDirect({
1616
wrtc
17-
})
17+
})()
1818

1919
return ws
2020
}

0 commit comments

Comments
 (0)