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

fix: update to latest interfaces #164

Merged
merged 1 commit into from
Jan 30, 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
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ updates:
directory: "/"
schedule:
interval: daily
time: "11:00"
time: "10:00"
open-pull-requests-limit: 10
4 changes: 3 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
Apache-2.0: https://www.apache.org/licenses/license-2.0
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
123 changes: 77 additions & 46 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,59 @@
"name": "@libp2p/tcp",
"version": "1.0.2",
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
"main": "./dist/src/index.js",
"type": "module",
"exports": {
".": {
"import": "./dist/src/index.js"
}
},
"scripts": {
"lint": "aegir lint",
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
"build": "tsc",
"pretest": "npm run build",
"test": "aegir test -f ./dist/test/**/*.js",
"test:node": "npm run test -- -t node --cov",
"test:electron-main": "npm run test -- -t electron-main",
"release": "semantic-release"
},
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p-tcp#readme",
"repository": {
"type": "git",
"url": "https://github.com/libp2p/js-libp2p-tcp.git"
"url": "git+https://github.com/libp2p/js-libp2p-tcp.git"
},
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-tcp/issues"
},
"keywords": [
"IPFS",
"TCP",
"libp2p",
"network",
"p2p",
"peer",
"peer-to-peer",
"IPFS",
"TCP"
"peer-to-peer"
],
"license": "(Apache-2.0 OR MIT)",
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-tcp/issues"
},
"homepage": "https://github.com/libp2p/js-libp2p-tcp",
"engines": {
"node": ">=14.0.0"
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"types": "dist/src/index.d.ts",
"devDependencies": {
"@libp2p/interface-compliance-tests": "^1.0.1",
"@libp2p/interfaces": "^1.0.0",
"@types/debug": "^4.1.5",
"@types/mocha": "^9.0.0",
"aegir": "^36.1.3",
"it-pipe": "^1.1.0",
"sinon": "^12.0.0",
"streaming-iterables": "^6.0.0"
"main": "src/index.js",
"type": "module",
"types": "types/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"types/*",
"types/src/*"
],
"types/*": [
"types/*",
"types/src/*"
]
}
},
"dependencies": {
"@libp2p/utils": "^1.0.1",
"@multiformats/mafmt": "^11.0.0",
"@multiformats/multiaddr": "^10.1.1",
"abortable-iterator": "^3.0.1",
"debug": "^4.3.1",
"err-code": "^3.0.1",
"stream-to-it": "^0.2.2"
"files": [
"*",
"!**/*.tsbuildinfo",
"!**/browser-test",
"!**/node-test"
],
"exports": {
".": {
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"release": {
"branches": [
Expand Down Expand Up @@ -136,9 +132,44 @@
}
],
"@semantic-release/changelog",
"@semantic-release/npm",
[
"@semantic-release/npm",
{
"pkgRoot": "dist"
}
],
"@semantic-release/github",
"@semantic-release/git"
]
},
"scripts": {
"lint": "aegir lint",
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
"build": "tsc",
"pretest": "npm run build",
"test": "aegir test -f ./dist/test/**/*.js",
"test:node": "npm run test -- -t node --cov",
"test:electron-main": "npm run test -- -t electron-main",
"release": "semantic-release"
},
"dependencies": {
"@libp2p/utils": "^1.0.1",
"@multiformats/mafmt": "^11.0.0",
"@multiformats/multiaddr": "^10.1.1",
"abortable-iterator": "^4.0.2",
"debug": "^4.3.1",
"err-code": "^3.0.1",
"stream-to-it": "^0.2.2"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^1.0.1",
"@libp2p/interfaces": "^1.0.0",
"@types/debug": "^4.1.5",
"@types/mocha": "^9.0.0",
"aegir": "^36.1.3",
"it-pipe": "^2.0.3",
"sinon": "^13.0.0",
"streaming-iterables": "^6.0.0",
"uint8arrays": "^3.0.0"
}
}
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import net from 'net'
import * as mafmt from '@multiformats/mafmt'
import errCode from 'err-code'
import debug from 'debug'
import { toConnection } from './socket-to-conn.js'
import { toMultiaddrConnection } from './socket-to-conn.js'
import { createListener } from './listener.js'
import { multiaddrToNetConfig } from './utils.js'
import { AbortError } from 'abortable-iterator'
import { CODE_CIRCUIT, CODE_P2P } from './constants.js'
import type { Transport, Upgrader } from '@libp2p/interfaces/transport'
import type { Connection } from '@libp2p/interfaces/connection'
import type { Transport, Upgrader, ListenerOptions } from '@libp2p/interfaces/transport'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Socket } from 'net'

Expand All @@ -30,7 +29,7 @@ interface DialOptions {
signal?: AbortSignal
}

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

constructor (options: TCPOptions) {
Expand All @@ -51,7 +50,7 @@ export class TCP implements Transport<DialOptions, {}> {
log('socket error', err)
})

const maConn = toConnection(socket, { remoteAddr: ma, signal: options.signal })
const maConn = toMultiaddrConnection(socket, { remoteAddr: ma, signal: options.signal })
log('new outbound connection %s', maConn.remoteAddr)
const conn = await this._upgrader.upgradeOutbound(maConn)
log('outbound connection %s upgraded', maConn.remoteAddr)
Expand Down Expand Up @@ -126,8 +125,8 @@ export class TCP implements Transport<DialOptions, {}> {
* anytime a new incoming Connection has been successfully upgraded via
* `upgrader.upgradeInbound`.
*/
createListener (options: {}, handler?: (connection: Connection) => void) {
return createListener({ handler: handler, upgrader: this._upgrader })
createListener (options: ListenerOptions = {}) {
return createListener({ upgrader: this._upgrader, ...options })
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/listener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import net from 'net'
import { EventEmitter } from 'events'
import debug from 'debug'
import { toConnection } from './socket-to-conn.js'
import { toMultiaddrConnection } from './socket-to-conn.js'
import { CODE_P2P } from './constants.js'
import {
getMultiaddrs,
Expand Down Expand Up @@ -55,7 +55,7 @@ export function createListener (context: Context) {

let maConn: MultiaddrConnection
try {
maConn = toConnection(socket, { listeningAddr })
maConn = toMultiaddrConnection(socket, { listeningAddr })
} catch (err) {
log.error('inbound connection failed', err)
return
Expand All @@ -66,8 +66,7 @@ export function createListener (context: Context) {
upgrader.upgradeInbound(maConn)
.then((conn) => {
log('inbound connection %s upgraded', maConn.remoteAddr)

trackConn(server, maConn)
trackConn(server, maConn, socket)

if (handler != null) {
handler(conn)
Expand Down Expand Up @@ -160,13 +159,12 @@ export function createListener (context: Context) {
return listener
}

function trackConn (server: ServerWithMultiaddrConnections, maConn: MultiaddrConnection) {
function trackConn (server: ServerWithMultiaddrConnections, maConn: MultiaddrConnection, socket: net.Socket) {
server.__connections.push(maConn)

const untrackConn = () => {
server.__connections = server.__connections.filter(c => c !== maConn)
}

// @ts-expect-error
maConn.conn.once('close', untrackConn)
socket.once('close', untrackConn)
}
12 changes: 4 additions & 8 deletions src/socket-to-conn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import abortable from 'abortable-iterator'
import { abortableSource } from 'abortable-iterator'
import debug from 'debug'
// @ts-expect-error no types
import toIterable from 'stream-to-it'
Expand All @@ -21,7 +21,7 @@ interface ToConnectionOptions {
* Convert a socket into a MultiaddrConnection
* https://github.com/libp2p/interface-transport#multiaddrconnection
*/
export const toConnection = (socket: Socket, options?: ToConnectionOptions) => {
export const toMultiaddrConnection = (socket: Socket, options?: ToConnectionOptions) => {
options = options ?? {}

// Check if we are connected on a unix path
Expand All @@ -38,7 +38,7 @@ export const toConnection = (socket: Socket, options?: ToConnectionOptions) => {
const maConn: MultiaddrConnection = {
async sink (source) {
if ((options?.signal) != null) {
source = abortable(source, options.signal)
source = abortableSource(source, options.signal)
}

try {
Expand All @@ -61,11 +61,7 @@ export const toConnection = (socket: Socket, options?: ToConnectionOptions) => {
},

// Missing Type for "abortable"
source: (options.signal != null) ? abortable(source, options.signal) : source,

conn: socket,

localAddr: options.localAddr ?? toMultiaddr(socket.localAddress ?? '', socket.localPort ?? ''),
source: (options.signal != null) ? abortableSource(source, options.signal) : source,

// If the remote address was passed, use it - it may have the peer ID encapsulated
remoteAddr: options.remoteAddr ?? toMultiaddr(socket.remoteAddress ?? '', socket.remotePort ?? ''),
Expand Down
18 changes: 8 additions & 10 deletions test/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('valid localAddr and remoteAddr', () => {
const handler = (conn: Connection) => handled(conn)

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

// Listen on the multi-address
await listener.listen(ma)
Expand All @@ -30,19 +32,13 @@ describe('valid localAddr and remoteAddr', () => {
expect(localAddrs.length).to.equal(1)

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

// Wait for the incoming dial to be handled
const listenerConn = await handlerPromise
await handlerPromise

// Close the listener
await listener.close()

expect(dialerConn.localAddr.toString())
.to.equal(listenerConn.remoteAddr.toString())

expect(dialerConn.remoteAddr.toString())
.to.equal(listenerConn.localAddr.toString())
})

it('should handle multiple simultaneous closes', async () => {
Expand All @@ -53,7 +49,9 @@ describe('valid localAddr and remoteAddr', () => {
const handler = (conn: Connection) => handled(conn)

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

// Listen on the multi-address
await listener.listen(ma)
Expand Down
Loading