diff --git a/package.json b/package.json index 21b27e2..cd0b683 100644 --- a/package.json +++ b/package.json @@ -154,7 +154,7 @@ "devDependencies": { "@libp2p/interface-mocks": "^8.0.1", "@libp2p/interface-transport-compliance-tests": "^3.0.0", - "aegir": "^37.7.3", + "aegir": "^38.1.0", "it-all": "^2.0.0", "it-pipe": "^2.0.3", "p-defer": "^4.0.0", diff --git a/src/index.ts b/src/index.ts index 50aed20..243452a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,7 +92,7 @@ class TCP implements Transport { return true } - get [Symbol.toStringTag] () { + get [Symbol.toStringTag] (): string { return '@libp2p/tcp' } @@ -114,7 +114,7 @@ class TCP implements Transport { metrics: this.metrics?.dialerEvents }) - const onAbort = () => { + const onAbort = (): void => { maConn.close().catch(err => { log.error('Error closing maConn after abort', err) }) @@ -138,7 +138,7 @@ class TCP implements Transport { return conn } - async _connect (ma: Multiaddr, options: TCPDialOptions) { + async _connect (ma: Multiaddr, options: TCPDialOptions): Promise { if (options.signal?.aborted === true) { throw new AbortError() } @@ -151,14 +151,14 @@ class TCP implements Transport { log('dialing %j', cOpts) const rawSocket = net.connect(cOpts) - const onError = (err: Error) => { + const onError = (err: Error): void => { err.message = `connection error ${cOptsStr}: ${err.message}` this.metrics?.dialerEvents.increment({ error: true }) done(err) } - const onTimeout = () => { + const onTimeout = (): void => { log('connection timeout %s', cOptsStr) this.metrics?.dialerEvents.increment({ timeout: true }) @@ -167,20 +167,20 @@ class TCP implements Transport { rawSocket.emit('error', err) } - const onConnect = () => { + const onConnect = (): void => { log('connection opened %j', cOpts) this.metrics?.dialerEvents.increment({ connect: true }) done() } - const onAbort = () => { + const onAbort = (): void => { log('connection aborted %j', cOpts) this.metrics?.dialerEvents.increment({ abort: true }) rawSocket.destroy() done(new AbortError()) } - const done = (err?: any) => { + const done = (err?: any): void => { rawSocket.removeListener('error', onError) rawSocket.removeListener('timeout', onTimeout) rawSocket.removeListener('connect', onConnect) @@ -190,7 +190,7 @@ class TCP implements Transport { } if (err != null) { - return reject(err) + reject(err); return } resolve(rawSocket) diff --git a/src/listener.ts b/src/listener.ts index 7ca3ff3..617f08a 100644 --- a/src/listener.ts +++ b/src/listener.ts @@ -19,7 +19,7 @@ const log = logger('libp2p:tcp:listener') /** * Attempts to close the given maConn. If a failure occurs, it will be logged */ -async function attemptClose (maConn: MultiaddrConnection) { +async function attemptClose (maConn: MultiaddrConnection): Promise { try { await maConn.close() } catch (err) { @@ -54,7 +54,7 @@ export interface TCPListenerMetrics { events: CounterGroup } -type Status = {started: false} | { +type Status = { started: false } | { started: true listeningAddr: Multiaddr peerId: string | null @@ -150,7 +150,7 @@ export class TCPListener extends EventEmitter implements Listene }) } - private onSocket (socket: net.Socket) { + private onSocket (socket: net.Socket): void { // Avoid uncaught errors caused by unstable connections socket.on('error', err => { log('socket error', err) @@ -230,7 +230,7 @@ export class TCPListener extends EventEmitter implements Listene } } - getAddrs () { + getAddrs (): Multiaddr[] { if (!this.status.started) { return [] } @@ -262,7 +262,7 @@ export class TCPListener extends EventEmitter implements Listene return addrs.map(ma => peerId != null ? ma.encapsulate(`/p2p/${peerId}`) : ma) } - async listen (ma: Multiaddr) { + async listen (ma: Multiaddr): Promise { if (this.status.started) { throw Error('server is already listening') } @@ -280,9 +280,9 @@ export class TCPListener extends EventEmitter implements Listene await this.netListen() } - async close () { + async close (): Promise { await Promise.all( - Array.from(this.connections.values()).map(async maConn => await attemptClose(maConn)) + Array.from(this.connections.values()).map(async maConn => { await attemptClose(maConn) }) ) // netClose already checks if server.listening diff --git a/src/socket-to-conn.ts b/src/socket-to-conn.ts index 6369f90..611c456 100644 --- a/src/socket-to-conn.ts +++ b/src/socket-to-conn.ts @@ -26,7 +26,7 @@ interface ToConnectionOptions { * Convert a socket into a MultiaddrConnection * https://github.com/libp2p/interface-transport#multiaddrconnection */ -export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptions) => { +export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptions): MultiaddrConnection => { const metrics = options.metrics const metricPrefix = options.metricPrefix ?? '' const inactivityTimeout = options.socketInactivityTimeout ?? SOCKET_TIMEOUT diff --git a/src/utils.ts b/src/utils.ts index 502c9b1..e5931c6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,19 +25,19 @@ export function multiaddrToNetConfig (addr: Multiaddr): NetConfig { return addr.toOptions() } -export function getMultiaddrs (proto: 'ip4' | 'ip6', ip: string, port: number) { - const toMa = (ip: string) => multiaddr(`/${proto}/${ip}/tcp/${port}`) +export function getMultiaddrs (proto: 'ip4' | 'ip6', ip: string, port: number): Multiaddr[] { + const toMa = (ip: string): Multiaddr => multiaddr(`/${proto}/${ip}/tcp/${port}`) return (isAnyAddr(ip) ? getNetworkAddrs(ProtoFamily[proto]) : [ip]).map(toMa) } -export function isAnyAddr (ip: string) { +export function isAnyAddr (ip: string): boolean { return ['0.0.0.0', '::'].includes(ip) } const networks = os.networkInterfaces() -function getNetworkAddrs (family: string) { - const addresses = [] +function getNetworkAddrs (family: string): string[] { + const addresses: string[] = [] for (const [, netAddrs] of Object.entries(networks)) { if (netAddrs != null) { diff --git a/test/connection.spec.ts b/test/connection.spec.ts index 254826a..8712885 100644 --- a/test/connection.spec.ts +++ b/test/connection.spec.ts @@ -21,7 +21,7 @@ describe('valid localAddr and remoteAddr', () => { let handled: (conn: Connection) => void const handlerPromise = new Promise(resolve => { handled = resolve }) - const handler = (conn: Connection) => handled(conn) + const handler = (conn: Connection): void => { handled(conn) } // Create a listener with the handler const listener = transport.createListener({ @@ -52,7 +52,7 @@ describe('valid localAddr and remoteAddr', () => { let handled: (conn: Connection) => void const handlerPromise = new Promise(resolve => { handled = resolve }) - const handler = (conn: Connection) => handled(conn) + const handler = (conn: Connection): void => { handled(conn) } // Create a listener with the handler const listener = transport.createListener({ @@ -76,7 +76,7 @@ describe('valid localAddr and remoteAddr', () => { // Close the dialer with two simultaneous calls to `close` await Promise.race([ - new Promise((resolve, reject) => setTimeout(() => reject(new Error('Timed out waiting for connection close')), 500)), + new Promise((resolve, reject) => setTimeout(() => { reject(new Error('Timed out waiting for connection close')) }, 500)), await Promise.all([ dialerConn.close(), dialerConn.close() diff --git a/test/listen-dial.spec.ts b/test/listen-dial.spec.ts index ded447d..cd884de 100644 --- a/test/listen-dial.spec.ts +++ b/test/listen-dial.spec.ts @@ -259,7 +259,7 @@ describe('dial', () => { // let multistream select finish before closing setTimeout(() => { void conn.close() - .then(() => handled()) + .then(() => { handled() }) }, 100) }, upgrader @@ -272,7 +272,7 @@ describe('dial', () => { upgrader }) const stream = await conn.newStream([protocol]) - await pipe(stream) + pipe(stream) await handledPromise await conn.close() @@ -340,7 +340,7 @@ describe('dial', () => { // take a long time to give us time to abort the dial await new Promise((resolve) => { - setTimeout(() => resolve(), 100) + setTimeout(() => { resolve() }, 100) }) } @@ -352,7 +352,7 @@ describe('dial', () => { const abortController = new AbortController() // abort once the upgrade process has started - void maConnPromise.promise.then(() => abortController.abort()) + void maConnPromise.promise.then(() => { abortController.abort() }) await expect(transport.dial(ma, { upgrader, diff --git a/test/max-connections-close.spec.ts b/test/max-connections-close.spec.ts index c9cb926..1cb51c4 100644 --- a/test/max-connections-close.spec.ts +++ b/test/max-connections-close.spec.ts @@ -45,7 +45,7 @@ describe('close server on maxConnections', () => { return socket } - async function assertConnectedSocket (i: number) { + async function assertConnectedSocket (i: number): Promise { const socket = createSocket() await new Promise((resolve, reject) => { @@ -61,7 +61,7 @@ describe('close server on maxConnections', () => { return socket } - async function assertRefusedSocket (i: number) { + async function assertRefusedSocket (i: number): Promise { const socket = createSocket() await new Promise((resolve, reject) => { @@ -79,7 +79,7 @@ describe('close server on maxConnections', () => { }) } - async function assertServerConnections (connections: number) { + async function assertServerConnections (connections: number): Promise { // Expect server connections but allow time for sockets to connect or disconnect for (let i = 0; i < 100; i++) { // eslint-disable-next-line @typescript-eslint/dot-notation diff --git a/test/socket-to-conn.spec.ts b/test/socket-to-conn.spec.ts index 49c339f..6b63501 100644 --- a/test/socket-to-conn.spec.ts +++ b/test/socket-to-conn.spec.ts @@ -5,7 +5,7 @@ import { toMultiaddrConnection } from '../src/socket-to-conn.js' import os from 'os' import type { ServerOpts, SocketConstructorOpts } from 'net' -async function setup (opts?: { server?: ServerOpts, client?: SocketConstructorOpts }) { +async function setup (opts?: { server?: ServerOpts, client?: SocketConstructorOpts }): Promise<{ server: Server, serverSocket: Socket, clientSocket: Socket }> { const serverListening = defer() const server = createServer(opts?.server)