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 (#190)
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 3e19583 commit 388b30d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 64 deletions.
4 changes: 2 additions & 2 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { pipe }from 'it-pipe'
export default {
test: {
async before () {
const { WebSockets } = await import('./dist/src/index.js')
const { webSockets } = await import('./dist/src/index.js')

const protocol = '/echo/1.0.0'
const registrar = mockRegistrar()
Expand All @@ -20,7 +20,7 @@ export default {
registrar
})

const ws = new WebSockets()
const ws = webSockets()()
const ma = multiaddr('/ip4/127.0.0.1/tcp/9095/ws')
const listener = ws.createListener({
upgrader
Expand Down
61 changes: 29 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# @libp2p/websockets <!-- omit in toc -->

[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![IRC](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-websockets.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-websockets)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-websockets/actions/workflows/js-test-and-release.yml)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-websockets/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-websockets/actions/workflows/js-test-and-release.yml)

> JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport spec
## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Description](#description)
- [Usage](#usage)
- [Constructor properties](#constructor-properties)
- [Libp2p Usage Example](#libp2p-usage-example)
Expand All @@ -30,10 +28,6 @@ $ npm i @libp2p/websockets
[![](https://github.com/raw/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
[![](https://github.com/raw/libp2p/interface-connection/master/img/badge.png)](https://github.com/libp2p/interface-connection)

## Description

`libp2p-websockets` is the WebSockets implementation compatible with libp2p.

## Usage

```sh
Expand All @@ -43,14 +37,17 @@ $ npm i @libp2p/websockets
### Constructor properties

```js
import { WebSockets } from '@libp2p/websockets'

const properties = {
upgrader,
filter
}

const ws = new WebSockets(properties)
import { createLibp2pNode } from 'libp2p'
import { webSockets } from '@libp2p/webrtc-direct'

const node = await createLibp2p({
transports: [
webSockets()
]
//... other config
})
await node.start()
await node.dial('/ip4/127.0.0.1/tcp/9090/ws')
```

| Name | Type | Description | Default |
Expand All @@ -72,26 +69,26 @@ The available filters are:
## Libp2p Usage Example

```js
import Libp2p from 'libp2p'
import { Websockets } from '@libp2p/websockets'
import filters from 'libp2p-websockets/filters'
import { MPLEX } from 'libp2p-mplex'
import { NOISE } from 'libp2p-noise'
import { createLibp2pNode } from 'libp2p'
import { websockets } from '@libp2p/websockets'
import filters from '@libp2p/websockets/filters'
import { mplex } from '@libp2p/mplex'
import { noise } from '@libp2p/noise'

const transportKey = Websockets.prototype[Symbol.toStringTag]
const node = await Libp2p.create({
modules: {
transport: [Websockets],
streamMuxer: [MPLEX],
connEncryption: [NOISE]
},
config: {
transport: {
[transportKey]: { // Transport properties -- Libp2p upgrader is automatically added
filter: filters.dnsWsOrWss
}
}
}
transport: [
websockets({
// connect to all sockets, even insecure ones
filters: filters.all
})
],
streamMuxers: [
mplex()
],
connectionEncryption: [
noise()
]
})
```

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@
"wherearewe": "^2.0.1"
},
"devDependencies": {
"@libp2p/interface-mocks": "^6.0.1",
"@libp2p/interface-transport-compliance-tests": "^2.0.6",
"@libp2p/interface-mocks": "^7.0.1",
"@libp2p/interface-transport-compliance-tests": "^3.0.0",
"@types/ws": "^8.2.2",
"aegir": "^37.5.3",
"is-loopback-addr": "^2.0.1",
Expand All @@ -187,7 +187,7 @@
"it-take": "^1.0.2",
"p-wait-for": "^5.0.0",
"uint8arraylist": "^2.3.2",
"uint8arrays": "^3.0.0"
"uint8arrays": "^4.0.2"
},
"browser": {
"./dist/src/listener.js": "./dist/src/listener.browser.js"
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface WebSocketsInit extends AbortOptions, WebSocketOptions {
server?: Server
}

export class WebSockets implements Transport {
class WebSockets implements Transport {
private readonly init?: WebSocketsInit

constructor (init?: WebSocketsInit) {
Expand Down Expand Up @@ -139,3 +139,9 @@ export class WebSockets implements Transport {
return filters.all(multiaddrs)
}
}

export function webSockets (init: WebSocketsInit = {}): (components?: any) => Transport {
return () => {
return new WebSockets(init)
}
}
9 changes: 5 additions & 4 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import { multiaddr } from '@multiformats/multiaddr'
import { pipe } from 'it-pipe'
import all from 'it-all'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { WebSockets } from '../src/index.js'
import { webSockets } from '../src/index.js'
import { mockUpgrader } from '@libp2p/interface-mocks'
import { isBrowser, isWebWorker } from 'wherearewe'
import type { Connection } from '@libp2p/interface-connection'
import type { Transport } from '@libp2p/interface-transport'

const protocol = '/echo/1.0.0'

describe('libp2p-websockets', () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9095/ws')
let ws: WebSockets
let ws: Transport
let conn: Connection

beforeEach(async () => {
ws = new WebSockets()
ws = webSockets()()
conn = await ws.dial(ma, { upgrader: mockUpgrader() })
})

Expand Down Expand Up @@ -86,6 +87,6 @@ describe('libp2p-websockets', () => {
})

it('.createServer throws in browser', () => {
expect(new WebSockets().createListener).to.throw()
expect(webSockets()().createListener).to.throw()
})
})
4 changes: 2 additions & 2 deletions test/compliance.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import tests from '@libp2p/interface-transport-compliance-tests'
import { multiaddr } from '@multiformats/multiaddr'
import http from 'http'
import { WebSockets } from '../src/index.js'
import { webSockets } from '../src/index.js'
import * as filters from '../src/filters.js'
import type { WebSocketListenerInit } from '../src/listener.js'

describe('interface-transport compliance', () => {
tests({
async setup () {
const ws = new WebSockets({ filter: filters.all })
const ws = webSockets({ filter: filters.all })()
const addrs = [
multiaddr('/ip4/127.0.0.1/tcp/9091/ws'),
multiaddr('/ip4/127.0.0.1/tcp/9092/ws'),
Expand Down
40 changes: 20 additions & 20 deletions test/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { mockRegistrar, mockUpgrader } from '@libp2p/interface-mocks'
import defer from 'p-defer'
import waitFor from 'p-wait-for'
import { WebSockets } from '../src/index.js'
import { webSockets } from '../src/index.js'
import * as filters from '../src/filters.js'
import drain from 'it-drain'
import type { Listener } from '@libp2p/interface-transport'
import type { Listener, Transport } from '@libp2p/interface-transport'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { Source } from 'it-stream-types'
import './compliance.node.js'
Expand All @@ -43,7 +43,7 @@ const upgrader = mockUpgrader({

describe('instantiate the transport', () => {
it('create', () => {
const ws = new WebSockets()
const ws = webSockets()()
expect(ws).to.exist()
})
})
Expand All @@ -52,7 +52,7 @@ describe('listen', () => {
it('should close connections when stopping the listener', async () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/47382/ws')

const ws = new WebSockets()
const ws = webSockets()()
const listener = ws.createListener({
handler: (conn) => {
void conn.newStream([protocol]).then(async (stream) => {
Expand All @@ -75,12 +75,12 @@ describe('listen', () => {
})

describe('ip4', () => {
let ws: WebSockets
let ws: Transport
const ma = multiaddr('/ip4/127.0.0.1/tcp/47382/ws')
let listener: Listener

beforeEach(() => {
ws = new WebSockets()
ws = webSockets()()
})

afterEach(async () => {
Expand Down Expand Up @@ -184,11 +184,11 @@ describe('listen', () => {
})

describe('ip6', () => {
let ws: WebSockets
let ws: Transport
const ma = multiaddr('/ip6/::1/tcp/9091/ws')

beforeEach(() => {
ws = new WebSockets()
ws = webSockets()()
})

it('listen, check for promise', async () => {
Expand Down Expand Up @@ -229,12 +229,12 @@ describe('listen', () => {

describe('dial', () => {
describe('ip4', () => {
let ws: WebSockets
let ws: Transport
let listener: Listener
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')

beforeEach(async () => {
ws = new WebSockets()
ws = webSockets()()
listener = ws.createListener({ upgrader })
return await listener.listen(ma)
})
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('dial', () => {

it('should resolve port 0', async () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/0/ws')
const ws = new WebSockets()
const ws = webSockets()()

// Create a Promise that resolves when a connection is handled
const deferred = defer()
Expand All @@ -295,12 +295,12 @@ describe('dial', () => {
})

describe('ip4 no loopback', () => {
let ws: WebSockets
let ws: Transport
let listener: Listener
const ma = multiaddr('/ip4/0.0.0.0/tcp/0/ws')

beforeEach(async () => {
ws = new WebSockets()
ws = webSockets()()
listener = ws.createListener({
handler: (conn) => {
void conn.newStream([protocol]).then(async (stream) => {
Expand Down Expand Up @@ -336,7 +336,7 @@ describe('dial', () => {
})

describe('ip4 with wss', () => {
let ws: WebSockets
let ws: Transport
let listener: Listener
const ma = multiaddr('/ip4/127.0.0.1/tcp/37284/wss')
let server: https.Server
Expand All @@ -346,7 +346,7 @@ describe('dial', () => {
cert: fs.readFileSync('./test/fixtures/certificate.pem'),
key: fs.readFileSync('./test/fixtures/key.pem')
})
ws = new WebSockets({ websocket: { rejectUnauthorized: false }, server })
ws = webSockets({ websocket: { rejectUnauthorized: false }, server })()
listener = ws.createListener({
handler: (conn) => {
void conn.newStream([protocol]).then(async (stream) => {
Expand Down Expand Up @@ -383,12 +383,12 @@ describe('dial', () => {
})

describe('ip6', () => {
let ws: WebSockets
let ws: Transport
let listener: Listener
const ma = multiaddr('/ip6/::1/tcp/9091/ws')

beforeEach(async () => {
ws = new WebSockets()
ws = webSockets()()
listener = ws.createListener({
handler: (conn) => {
void conn.newStream([protocol]).then(async (stream) => {
Expand Down Expand Up @@ -426,11 +426,11 @@ describe('dial', () => {
})

describe('filter addrs', () => {
let ws: WebSockets
let ws: Transport

describe('default filter addrs with only dns', () => {
before(() => {
ws = new WebSockets()
ws = webSockets()()
})

it('should filter out invalid WS addresses', function () {
Expand Down Expand Up @@ -498,7 +498,7 @@ describe('filter addrs', () => {

describe('custom filter addrs', () => {
before(() => {
ws = new WebSockets({ filter: filters.all })
ws = webSockets()({ filter: filters.all })
})

it('should fail invalid WS addresses', function () {
Expand Down

0 comments on commit 388b30d

Please sign in to comment.