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

fix: update interfaces #172

Merged
merged 1 commit into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@
"release": "semantic-release"
},
"dependencies": {
"@libp2p/logger": "^1.0.2",
"@libp2p/utils": "^1.0.6",
"@multiformats/mafmt": "^11.0.0",
"@multiformats/multiaddr": "^10.1.1",
"@libp2p/logger": "^1.1.2",
"@libp2p/utils": "^1.0.9",
"@multiformats/mafmt": "^11.0.2",
"@multiformats/multiaddr": "^10.1.5",
"abortable-iterator": "^4.0.2",
"err-code": "^3.0.1",
"stream-to-it": "^0.2.2"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^1.1.2",
"@libp2p/interfaces": "^1.3.2",
"@libp2p/interface-compliance-tests": "^1.1.17",
"@libp2p/interfaces": "^1.3.14",
"aegir": "^36.1.3",
"it-all": "^1.0.6",
"it-pipe": "^2.0.3",
"sinon": "^13.0.0",
"sinon": "^13.0.1",
"uint8arrays": "^3.0.0"
}
}
47 changes: 14 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,25 @@ import { logger } from '@libp2p/logger'
import { toMultiaddrConnection } from './socket-to-conn.js'
import { createListener } from './listener.js'
import { multiaddrToNetConfig } from './utils.js'
import { AbortError } from 'abortable-iterator'
import { AbortError } from '@libp2p/interfaces/errors'
import { CODE_CIRCUIT, CODE_P2P } from './constants.js'
import type { Transport, Upgrader, ListenerOptions } from '@libp2p/interfaces/transport'
import { CreateListenerOptions, DialOptions, symbol, Transport } from '@libp2p/interfaces/transport'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Socket } from 'net'
import type { AbortOptions } from '@libp2p/interfaces'

const log = logger('libp2p:tcp')

/**
* @typedef {import('multiaddr').Multiaddr} Multiaddr
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('libp2p-interfaces/src/transport/types').Upgrader} Upgrader
* @typedef {import('libp2p-interfaces/src/transport/types').Listener} Listener
* @typedef {import('net').Socket} Socket
*/

interface TCPOptions {
upgrader: Upgrader
}

interface DialOptions {
signal?: AbortSignal
}

export class TCP implements Transport<DialOptions, ListenerOptions> {
private readonly _upgrader: Upgrader

constructor (options: TCPOptions) {
const { upgrader } = options

if (upgrader == null) {
throw new Error('An upgrader must be provided. See https://github.com/libp2p/interface-transport#upgrader.')
}
export class TCP implements Transport {
get [symbol] (): true {
return true
}

this._upgrader = upgrader
get [Symbol.toStringTag] () {
return this.constructor.name
}

async dial (ma: Multiaddr, options: DialOptions = {}) {
async dial (ma: Multiaddr, options: DialOptions) {
const socket = await this._connect(ma, options)

// Avoid uncaught errors caused by unstable connections
Expand All @@ -52,12 +33,12 @@ export class TCP implements Transport<DialOptions, ListenerOptions> {

const maConn = toMultiaddrConnection(socket, { remoteAddr: ma, signal: options.signal })
log('new outbound connection %s', maConn.remoteAddr)
const conn = await this._upgrader.upgradeOutbound(maConn)
const conn = await options.upgrader.upgradeOutbound(maConn)
log('outbound connection %s upgraded', maConn.remoteAddr)
return conn
}

async _connect (ma: Multiaddr, options: DialOptions = {}) {
async _connect (ma: Multiaddr, options: AbortOptions = {}) {
if (options.signal?.aborted === true) {
throw new AbortError()
}
Expand Down Expand Up @@ -125,8 +106,8 @@ export class TCP implements Transport<DialOptions, ListenerOptions> {
* anytime a new incoming Connection has been successfully upgraded via
* `upgrader.upgradeInbound`.
*/
createListener (options: ListenerOptions = {}) {
return createListener({ upgrader: this._upgrader, ...options })
createListener (options: CreateListenerOptions) {
return createListener(options)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function createListener (context: Context) {
handler(conn)
}

listener.dispatchEvent(new CustomEvent('connection', { detail: conn }))
listener.dispatchEvent(new CustomEvent<Connection>('connection', { detail: conn }))
})
.catch(async err => {
log.error('inbound connection failed', err)
Expand Down Expand Up @@ -98,7 +98,7 @@ export function createListener (context: Context) {
const address = server.address()

if (address == null) {
throw new Error('Listener is not ready yet')
return []
}

if (typeof address === 'string') {
Expand Down Expand Up @@ -151,7 +151,7 @@ export function createListener (context: Context) {

server
.on('listening', () => listener.dispatchEvent(new CustomEvent('listening')))
.on('error', err => listener.dispatchEvent(new CustomEvent('error', { detail: err })))
.on('error', err => listener.dispatchEvent(new CustomEvent<Error>('error', { detail: err })))
.on('close', () => listener.dispatchEvent(new CustomEvent('close')))

return listener
Expand Down
10 changes: 2 additions & 8 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ import { TCP } from '../src/index.js'

describe('interface-transport compliance', () => {
tests({
async setup (args) {
if (args == null) {
throw new Error('No args')
}

const { upgrader } = args

const tcp = new TCP({ upgrader })
async setup () {
const tcp = new TCP()
const addrs = [
new Multiaddr('/ip4/127.0.0.1/tcp/9091'),
new Multiaddr('/ip4/127.0.0.1/tcp/9092'),
Expand Down
19 changes: 14 additions & 5 deletions test/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { TCP } from '../src/index.js'
import { Multiaddr } from '@multiformats/multiaddr'
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
import type { Connection } from '@libp2p/interfaces/connection'
import type { Upgrader } from '@libp2p/interfaces/transport'

describe('valid localAddr and remoteAddr', () => {
let tcp: TCP
let upgrader: Upgrader

beforeEach(() => {
tcp = new TCP({ upgrader: mockUpgrader() })
tcp = new TCP()
upgrader = mockUpgrader()
})

const ma = new Multiaddr('/ip4/127.0.0.1/tcp/0')
Expand All @@ -22,7 +25,8 @@ describe('valid localAddr and remoteAddr', () => {

// Create a listener with the handler
const listener = tcp.createListener({
handler
handler,
upgrader
})

// Listen on the multi-address
Expand All @@ -32,7 +36,9 @@ describe('valid localAddr and remoteAddr', () => {
expect(localAddrs.length).to.equal(1)

// Dial to that address
await tcp.dial(localAddrs[0])
await tcp.dial(localAddrs[0], {
upgrader
})

// Wait for the incoming dial to be handled
await handlerPromise
Expand All @@ -50,7 +56,8 @@ describe('valid localAddr and remoteAddr', () => {

// Create a listener with the handler
const listener = tcp.createListener({
handler
handler,
upgrader
})

// Listen on the multi-address
Expand All @@ -60,7 +67,9 @@ describe('valid localAddr and remoteAddr', () => {
expect(localAddrs.length).to.equal(1)

// Dial to that address
const dialerConn = await tcp.dial(localAddrs[0])
const dialerConn = await tcp.dial(localAddrs[0], {
upgrader
})

// Wait for the incoming dial to be handled
await handlerPromise
Expand Down
3 changes: 1 addition & 2 deletions test/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'aegir/utils/chai.js'
import { TCP } from '../src/index.js'
import { Multiaddr } from '@multiformats/multiaddr'
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'

describe('filter addrs', () => {
const base = '/ip4/127.0.0.1'
Expand All @@ -10,7 +9,7 @@ describe('filter addrs', () => {
let tcp: TCP

before(() => {
tcp = new TCP({ upgrader: mockUpgrader() })
tcp = new TCP()
})

it('filter valid addrs for this transport', () => {
Expand Down
Loading