From d127794f4e9b24a6f8cf13ba5a62368af59f9084 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sat, 29 Jan 2022 16:24:42 +0000 Subject: [PATCH] fix: update to latest interfaces Updates to changes from https://github.com/libp2p/js-libp2p-interfaces/pull/153 --- .github/dependabot.yml | 2 +- LICENSE | 4 +- LICENSE-MIT | 2 +- package.json | 123 ++++++++++++++++++++++++--------------- src/index.ts | 13 ++--- src/listener.ts | 12 ++-- src/socket-to-conn.ts | 12 ++-- test/connection.spec.ts | 18 +++--- test/listen-dial.spec.ts | 29 +++++---- 9 files changed, 122 insertions(+), 93 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index de46e32..290ad02 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -4,5 +4,5 @@ updates: directory: "/" schedule: interval: daily - time: "11:00" + time: "10:00" open-pull-requests-limit: 10 diff --git a/LICENSE b/LICENSE index b0b237f..20ce483 100644 --- a/LICENSE +++ b/LICENSE @@ -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 \ No newline at end of file +Apache-2.0: https://www.apache.org/licenses/license-2.0 diff --git a/LICENSE-MIT b/LICENSE-MIT index 749aa1e..72dc60d 100644 --- a/LICENSE-MIT +++ b/LICENSE-MIT @@ -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. \ No newline at end of file +THE SOFTWARE. diff --git a/package.json b/package.json index 61dbaf3..8ceb431 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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" } } diff --git a/src/index.ts b/src/index.ts index 7472d76..e4f562d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -30,7 +29,7 @@ interface DialOptions { signal?: AbortSignal } -export class TCP implements Transport { +export class TCP implements Transport { private readonly _upgrader: Upgrader constructor (options: TCPOptions) { @@ -51,7 +50,7 @@ export class TCP implements Transport { 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) @@ -126,8 +125,8 @@ export class TCP implements Transport { * 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 }) } /** diff --git a/src/listener.ts b/src/listener.ts index 70df84c..6741672 100644 --- a/src/listener.ts +++ b/src/listener.ts @@ -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, @@ -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 @@ -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) @@ -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) } diff --git a/src/socket-to-conn.ts b/src/socket-to-conn.ts index 629ded7..192a7d4 100644 --- a/src/socket-to-conn.ts +++ b/src/socket-to-conn.ts @@ -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' @@ -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 @@ -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 { @@ -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 ?? ''), diff --git a/test/connection.spec.ts b/test/connection.spec.ts index 0d55583..4144ac7 100644 --- a/test/connection.spec.ts +++ b/test/connection.spec.ts @@ -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) @@ -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 () => { @@ -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) diff --git a/test/listen-dial.spec.ts b/test/listen-dial.spec.ts index 78ec219..3bc7aa5 100644 --- a/test/listen-dial.spec.ts +++ b/test/listen-dial.spec.ts @@ -3,9 +3,10 @@ import { TCP } from '../src/index.js' import os from 'os' import path from 'path' import { Multiaddr } from '@multiformats/multiaddr' -import pipe from 'it-pipe' +import { pipe } from 'it-pipe' import { collect } from 'streaming-iterables' import { mockUpgrader } from '@libp2p/interface-compliance-tests/transport/utils' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' const isCI = process.env.CI @@ -132,12 +133,12 @@ describe('dial', () => { const { stream } = await conn.newStream(['/test/stream']) const values = await pipe( - ['hey'], + [uint8ArrayFromString('hey')], stream, collect ) - expect(values).to.deep.equal(['hey']) + expect(values[0]).to.equalBytes(uint8ArrayFromString('hey')) await conn.close() await listener.close() }) @@ -154,11 +155,11 @@ describe('dial', () => { const { stream } = await conn.newStream(['/test/stream']) const values = await pipe( - ['hey'], + [uint8ArrayFromString('hey')], stream, collect ) - expect(values).to.deep.equal(['hey']) + expect(values[0]).to.equalBytes(uint8ArrayFromString('hey')) await conn.close() await listener.close() }) @@ -173,7 +174,7 @@ describe('dial', () => { const { stream } = await conn.newStream(['/test/stream']) const values = await pipe( - ['hey'], + [uint8ArrayFromString('hey')], stream, collect ) @@ -189,8 +190,10 @@ describe('dial', () => { const ma = new Multiaddr('/ip6/::/tcp/9090') - const listener = tcp.createListener({}, (conn) => { - conn.close().then(() => handled()).catch(() => {}) + const listener = tcp.createListener({ + handler: (conn) => { + conn.close().then(() => handled()).catch(() => {}) + } }) await listener.listen(ma) @@ -215,8 +218,10 @@ describe('dial', () => { const ma = new Multiaddr('/ip6/::/tcp/9090') - const listener = tcp.createListener({}, () => { - handled() + const listener = tcp.createListener({ + handler: () => { + handled() + } }) await listener.listen(ma) @@ -237,11 +242,11 @@ describe('dial', () => { const { stream } = await conn.newStream(['/test/stream']) const values = await pipe( - ['hey'], + [uint8ArrayFromString('hey')], stream, collect ) - expect(values).to.deep.equal(['hey']) + expect(values[0]).to.equalBytes(uint8ArrayFromString('hey')) await conn.close() await listener.close()