Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
fix: update project, remove @libp2p/components and unused deps (#20)
Browse files Browse the repository at this point in the history
Removes a lot of dependencies that are not used.

Updates exported function for use with `libp2p@0.40.x`
  • Loading branch information
achingbrain authored Oct 17, 2022
1 parent dfe49de commit 568638e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 72 deletions.
1 change: 0 additions & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default {
});
})))


return {
server,
env: {
Expand Down
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# @libp2p/webtransport <!-- 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-webtransport.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-webtransport)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-webtransport/test%20&%20maybe%20release/main?style=flat-square)](https://github.com/libp2p/js-libp2p-webtransport/actions/workflows/js-test-and-release.yml)

> JavaScript implementation of the WebTransport module that libp2p uses and that implements the interface-transport spec
Expand Down Expand Up @@ -40,16 +41,17 @@ $ npm i @libp2p/webtransport
## Libp2p Usage Example

```js
import Libp2p from 'libp2p'
import { WebTransport } from '@libp2p/webtransport'
import { NOISE } from 'libp2p-noise'

const node = await Libp2p.create({
modules: {
connEncryption: [NOISE]
transports: [new WebTransport()],
connectionEncryption: [new Noise()]
},
import { createLibp2pNode } from 'libp2p'
import { webTransport } from '@libp2p/webtransport'
import { noise } from 'libp2p-noise'

const node = await createLibp2pNode({
transports: [
webTransport()
],
connectionEncryption: [
noise()
]
})
```

Expand Down
51 changes: 14 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@libp2p/webtransport",
"version": "1.0.1",
"description": "JavaScript implementation of the webtransport module that libp2p uses and that implements the interface-transport spec",
"description": "JavaScript implementation of the WebTransport module that libp2p uses and that implements the interface-transport spec",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p-webtransport#readme",
"repository": {
Expand Down Expand Up @@ -146,51 +146,28 @@
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check -i libp2p",
"dep-check": "aegir dep-check",
"build": "aegir build",
"test": "aegir test",
"test:chrome": "aegir test -t browser -f ./dist/test/browser.js --cov",
"release": "aegir release",
"proto:gen": "protons ./src/proto/webtransport.proto"
"test": "aegir test -t browser",
"test:chrome": "aegir test -t browser --cov",
"release": "aegir release"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^9.0.0",
"@libp2p/interface-connection": "^3.0.1",
"@libp2p/interface-connection": "^3.0.2",
"@libp2p/interface-peer-id": "^1.0.5",
"@libp2p/interface-stream-muxer": "^3.0.0",
"@libp2p/interface-transport": "^2.0.0",
"@libp2p/interfaces": "^3.0.1",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^1.1.15",
"@libp2p/utils": "^3.0.0",
"@multiformats/mafmt": "^11.0.3",
"@multiformats/multiaddr": "^11.0.3",
"@multiformats/multiaddr-to-uri": "^9.0.0",
"abortable-iterator": "^4.0.2",
"err-code": "^3.0.1",
"it-ws": "^5.0.0",
"@libp2p/logger": "^2.0.2",
"@libp2p/peer-id": "^1.1.16",
"@multiformats/multiaddr": "^11.0.5",
"it-stream-types": "^1.0.4",
"multiformats": "^10.0.0",
"p-defer": "^4.0.0",
"p-timeout": "^6.0.0",
"protons-runtime": "^4.0.1",
"uint8arraylist": "^2.3.2",
"wherearewe": "^2.0.1"
"uint8arraylist": "^2.3.3"
},
"devDependencies": {
"@libp2p/interface-mocks": "^7.0.1",
"@libp2p/interface-transport-compliance-tests": "^3.0.0",
"@types/ws": "^8.2.2",
"aegir": "^37.3.0",
"is-loopback-addr": "^2.0.1",
"it-all": "^1.0.6",
"it-drain": "^1.0.5",
"it-goodbye": "^4.0.1",
"it-pipe": "^2.0.3",
"it-stream-types": "^1.0.4",
"it-take": "^1.0.2",
"libp2p": "next",
"p-wait-for": "^5.0.0",
"protons": "^6.0.0",
"uint8arrays": "^4.0.2"
"aegir": "^37.5.6",
"libp2p": "^0.40.0"
},
"browser": {
"./dist/src/listener.js": "./dist/src/listener.browser.js"
Expand Down
39 changes: 21 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { logger } from '@libp2p/logger'
import { Noise } from '@chainsafe/libp2p-noise'
import type { Components, Initializable } from '@libp2p/components'
import { Transport, symbol, CreateListenerOptions, DialOptions } from '@libp2p/interface-transport'
import { Transport, symbol, CreateListenerOptions, DialOptions, Listener } from '@libp2p/interface-transport'
import type { Connection, Direction, MultiaddrConnection, Stream } from '@libp2p/interface-connection'
import { Multiaddr, protocols } from '@multiformats/multiaddr'
import { peerIdFromString } from '@libp2p/peer-id'
Expand Down Expand Up @@ -240,16 +239,23 @@ export function isSubset (set: Uint8Array[], maybeSubset: Uint8Array[]): boolean
return (intersection.length === maybeSubset.length)
}

export interface WebTransportConfig {
maxInboundStreams: 1000
export interface WebTransportInit {
maxInboundStreams?: number
}

export class WebTransport implements Transport, Initializable {
private components?: Components
private readonly config: WebTransportConfig
export interface WebTransportComponents {
peerId: PeerId
}

class WebTransport implements Transport {
private readonly components: WebTransportComponents
private readonly config: Required<WebTransportInit>

init (components: Components) {
constructor (components: WebTransportComponents, init: WebTransportInit = {}) {
this.components = components
this.config = {
maxInboundStreams: init.maxInboundStreams ?? 1000
}
}

get [Symbol.toStringTag] () {
Expand All @@ -260,13 +266,9 @@ export class WebTransport implements Transport, Initializable {
return true
}

constructor (config?: WebTransportConfig) {
this.config = config ?? { maxInboundStreams: 1000 }
}

async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
log('dialing %s', ma)
const localPeer = this.components?.getPeerId()
const localPeer = this.components.peerId
if (localPeer === undefined) {
throw new Error('Need a local peerid')
}
Expand Down Expand Up @@ -325,7 +327,7 @@ export class WebTransport implements Transport, Initializable {
return await options.upgrader.upgradeOutbound(maConn, { skipEncryption: true, muxerFactory: this.webtransportMuxer(wt), skipProtection: true })
}

async authenticateWebTransport (wt: typeof window.WebTransport, localPeer: PeerId, remotePeer: PeerId, certhashes: Array<MultihashDigest<number>>) {
async authenticateWebTransport (wt: typeof window.WebTransport, localPeer: PeerId, remotePeer: PeerId, certhashes: Array<MultihashDigest<number>>): Promise<boolean> {
const stream = await wt.createBidirectionalStream()
const writer = stream.writable.getWriter()
const reader = stream.readable.getReader()
Expand Down Expand Up @@ -445,11 +447,8 @@ export class WebTransport implements Transport, Initializable {
}
}

createListener (options: CreateListenerOptions) {
createListener (options: CreateListenerOptions): Listener {
throw new Error('Webtransport servers are not supported in Node or the browser')
// Unreachable, here to make TS happy
// eslint-disable-next-line no-unreachable
return null as any
}

/**
Expand All @@ -459,3 +458,7 @@ export class WebTransport implements Transport, Initializable {
return multiaddrs.filter(ma => ma.protoNames().includes('webtransport'))
}
}

export function webTransport (init: WebTransportInit = {}): (components: WebTransportComponents) => Transport {
return (components: WebTransportComponents) => new WebTransport(components, init)
}
10 changes: 5 additions & 5 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { expect } from 'aegir/chai'
import { multiaddr } from '@multiformats/multiaddr'
import { Noise } from '@chainsafe/libp2p-noise'
import { WebTransport as WebTransportLibp2p, isSubset } from '../src/index'
import { webTransport, isSubset } from '../src/index'
import { createLibp2p } from 'libp2p'

declare global {
Expand All @@ -19,8 +19,8 @@ describe('libp2p-webtransport', () => {
const maStr: string = process.env.serverAddr!
const ma = multiaddr(maStr)
const node = await createLibp2p({
transports: [new WebTransportLibp2p()],
connectionEncryption: [new Noise()]
transports: [webTransport()],
connectionEncryption: [() => new Noise()]
})

await node.start()
Expand All @@ -41,8 +41,8 @@ describe('libp2p-webtransport', () => {
const ma = multiaddr(maStrNoCerthash + '/p2p/' + maStrP2p)

const node = await createLibp2p({
transports: [new WebTransportLibp2p()],
connectionEncryption: [new Noise()]
transports: [webTransport()],
connectionEncryption: [() => new Noise()]
})
await node.start()

Expand Down

0 comments on commit 568638e

Please sign in to comment.