diff --git a/package.json b/package.json index a8971d7caf..da5f65e4d0 100644 --- a/package.json +++ b/package.json @@ -162,6 +162,7 @@ "@libp2p/record": "^3.0.0", "@libp2p/topology": "^4.0.0", "@multiformats/multiaddr": "^12.0.0", + "@types/sinon": "^10.0.14", "abortable-iterator": "^5.0.1", "any-signal": "^4.1.1", "datastore-core": "^9.0.1", @@ -197,7 +198,7 @@ "@types/lodash.range": "^3.2.6", "@types/varint": "^6.0.0", "@types/which": "^3.0.0", - "aegir": "^38.1.2", + "aegir": "^39.0.5", "datastore-level": "^10.0.0", "delay": "^5.0.0", "execa": "^7.0.0", diff --git a/src/content-fetching/index.ts b/src/content-fetching/index.ts index 20ca863344..83fd17de48 100644 --- a/src/content-fetching/index.ts +++ b/src/content-fetching/index.ts @@ -1,30 +1,30 @@ import { CodeError } from '@libp2p/interfaces/errors' -import { equals as uint8ArrayEquals } from 'uint8arrays/equals' +import { logger } from '@libp2p/logger' import { Libp2pRecord } from '@libp2p/record' -import { verifyRecord } from '@libp2p/record/validators' import { bestRecord } from '@libp2p/record/selectors' -import parallel from 'it-parallel' +import { verifyRecord } from '@libp2p/record/validators' import map from 'it-map' -import { - valueEvent, - queryErrorEvent -} from '../query/events.js' -import { Message, MESSAGE_TYPE } from '../message/index.js' +import parallel from 'it-parallel' import { pipe } from 'it-pipe' +import { equals as uint8ArrayEquals } from 'uint8arrays/equals' import { ALPHA } from '../constants.js' +import { Message, MESSAGE_TYPE } from '../message/index.js' +import { + valueEvent, + queryErrorEvent +} from '../query/events.js' import { createPutRecord, convertBuffer, bufferToRecordKey } from '../utils.js' -import { logger } from '@libp2p/logger' -import type { Validators, Selectors, ValueEvent, QueryOptions, QueryEvent } from '@libp2p/interface-dht' +import type { KadDHTComponents } from '../index.js' +import type { Network } from '../network.js' import type { PeerRouting } from '../peer-routing/index.js' import type { QueryManager } from '../query/manager.js' +import type { QueryFunc } from '../query/types.js' import type { RoutingTable } from '../routing-table/index.js' -import type { Network } from '../network.js' -import type { Logger } from '@libp2p/logger' +import type { Validators, Selectors, ValueEvent, QueryOptions, QueryEvent } from '@libp2p/interface-dht' import type { AbortOptions } from '@libp2p/interfaces' -import type { QueryFunc } from '../query/types.js' -import type { KadDHTComponents } from '../index.js' +import type { Logger } from '@libp2p/logger' export interface ContentFetchingInit { validators: Validators diff --git a/src/content-routing/index.ts b/src/content-routing/index.ts index b60292ed38..0159431aa2 100644 --- a/src/content-routing/index.ts +++ b/src/content-routing/index.ts @@ -1,28 +1,28 @@ -import { Message, MESSAGE_TYPE } from '../message/index.js' -import parallel from 'it-parallel' +import { logger } from '@libp2p/logger' import map from 'it-map' -import { convertBuffer } from '../utils.js' -import { ALPHA } from '../constants.js' +import parallel from 'it-parallel' import { pipe } from 'it-pipe' +import { ALPHA } from '../constants.js' +import { Message, MESSAGE_TYPE } from '../message/index.js' import { queryErrorEvent, peerResponseEvent, providerEvent } from '../query/events.js' -import { logger } from '@libp2p/logger' -import type { PeerResponseEvent, ProviderEvent, QueryEvent, QueryOptions } from '@libp2p/interface-dht' -import type { PeerRouting } from '../peer-routing/index.js' -import type { QueryManager } from '../query/manager.js' -import type { RoutingTable } from '../routing-table/index.js' +import { convertBuffer } from '../utils.js' +import type { KadDHTComponents } from '../index.js' import type { Network } from '../network.js' -import type { Logger } from '@libp2p/logger' +import type { PeerRouting } from '../peer-routing/index.js' import type { Providers } from '../providers.js' +import type { QueryManager } from '../query/manager.js' import type { QueryFunc } from '../query/types.js' -import type { CID } from 'multiformats/cid' +import type { RoutingTable } from '../routing-table/index.js' +import type { PeerResponseEvent, ProviderEvent, QueryEvent, QueryOptions } from '@libp2p/interface-dht' +import type { PeerInfo } from '@libp2p/interface-peer-info' import type { AbortOptions } from '@libp2p/interfaces' +import type { Logger } from '@libp2p/logger' import type { Multiaddr } from '@multiformats/multiaddr' -import type { PeerInfo } from '@libp2p/interface-peer-info' -import type { KadDHTComponents } from '../index.js' +import type { CID } from 'multiformats/cid' export interface ContentRoutingInit { network: Network diff --git a/src/dual-kad-dht.ts b/src/dual-kad-dht.ts index f4fce412c2..e3ab5dfcb3 100644 --- a/src/dual-kad-dht.ts +++ b/src/dual-kad-dht.ts @@ -1,16 +1,16 @@ -import { logger } from '@libp2p/logger' +import { symbol } from '@libp2p/interface-peer-discovery' import { CodeError } from '@libp2p/interfaces/errors' +import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' +import { logger } from '@libp2p/logger' import merge from 'it-merge' import { queryErrorEvent } from './query/events.js' +import type { KadDHTComponents } from './index.js' import type { KadDHT } from './kad-dht.js' import type { DualDHT, QueryEvent, QueryOptions } from '@libp2p/interface-dht' +import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery' +import type { PeerId } from '@libp2p/interface-peer-id' import type { AbortOptions } from '@libp2p/interfaces' -import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' import type { CID } from 'multiformats' -import type { PeerId } from '@libp2p/interface-peer-id' -import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery' -import { symbol } from '@libp2p/interface-peer-discovery' -import type { KadDHTComponents } from './index.js' const log = logger('libp2p:kad-dht') @@ -43,13 +43,9 @@ export class DualKadDHT extends EventEmitter implements Dua }) } - get [symbol] (): true { - return true - } + readonly [symbol] = true - get [Symbol.toStringTag] (): '@libp2p/dual-kad-dht' { - return '@libp2p/dual-kad-dht' - } + readonly [Symbol.toStringTag] = '@libp2p/dual-kad-dht' /** * Is this DHT running. @@ -62,7 +58,7 @@ export class DualKadDHT extends EventEmitter implements Dua * If 'server' this node will respond to DHT queries, if 'client' this node will not */ async getMode (): Promise<'client' | 'server'> { - return await this.wan.getMode() + return this.wan.getMode() } /** diff --git a/src/index.ts b/src/index.ts index 4ced200b73..50171cf28d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,22 +1,22 @@ -import { KadDHT as SingleKadDHT } from './kad-dht.js' -import { DualKadDHT } from './dual-kad-dht.js' -import drain from 'it-drain' +import { type ContentRouting, contentRouting } from '@libp2p/interface-content-routing' +import { type PeerDiscovery, peerDiscovery } from '@libp2p/interface-peer-discovery' +import { type PeerRouting, peerRouting } from '@libp2p/interface-peer-routing' import { CodeError } from '@libp2p/interfaces/errors' -import type { DHT, DualDHT, Selectors, Validators } from '@libp2p/interface-dht' -import { ContentRouting, contentRouting } from '@libp2p/interface-content-routing' -import type { CID } from 'multiformats/cid' -import type { AbortOptions } from '@libp2p/interfaces' -import type { PeerInfo } from '@libp2p/interface-peer-info' +import drain from 'it-drain' +import { DualKadDHT } from './dual-kad-dht.js' +import { KadDHT as SingleKadDHT } from './kad-dht.js' import type { ProvidersInit } from './providers.js' -import type { Registrar } from '@libp2p/interface-registrar' import type { AddressManager } from '@libp2p/interface-address-manager' -import type { PeerStore } from '@libp2p/interface-peer-store' import type { ConnectionManager } from '@libp2p/interface-connection-manager' +import type { DHT, DualDHT, Selectors, Validators } from '@libp2p/interface-dht' import type { Metrics } from '@libp2p/interface-metrics' -import type { Datastore } from 'interface-datastore' -import { PeerRouting, peerRouting } from '@libp2p/interface-peer-routing' -import { PeerDiscovery, peerDiscovery } from '@libp2p/interface-peer-discovery' import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerInfo } from '@libp2p/interface-peer-info' +import type { PeerStore } from '@libp2p/interface-peer-store' +import type { Registrar } from '@libp2p/interface-registrar' +import type { AbortOptions } from '@libp2p/interfaces' +import type { Datastore } from 'interface-datastore' +import type { CID } from 'multiformats/cid' export interface KadDHTInit { /** diff --git a/src/kad-dht.ts b/src/kad-dht.ts index 6f1410b4fc..134858d0c2 100644 --- a/src/kad-dht.ts +++ b/src/kad-dht.ts @@ -1,31 +1,31 @@ -import { RoutingTable } from './routing-table/index.js' -import { RoutingTableRefresh } from './routing-table/refresh.js' -import { Network } from './network.js' +import { symbol } from '@libp2p/interface-peer-discovery' +import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events' +import { type Logger, logger } from '@libp2p/logger' +import { selectors as recordSelectors } from '@libp2p/record/selectors' +import { validators as recordValidators } from '@libp2p/record/validators' +import pDefer from 'p-defer' +import { PROTOCOL_DHT, PROTOCOL_PREFIX, LAN_PREFIX } from './constants.js' import { ContentFetching } from './content-fetching/index.js' import { ContentRouting } from './content-routing/index.js' +import { Network } from './network.js' import { PeerRouting } from './peer-routing/index.js' import { Providers } from './providers.js' import { QueryManager } from './query/manager.js' +import { QuerySelf } from './query-self.js' +import { RoutingTable } from './routing-table/index.js' +import { RoutingTableRefresh } from './routing-table/refresh.js' import { RPC } from './rpc/index.js' import { TopologyListener } from './topology-listener.js' -import { QuerySelf } from './query-self.js' import { removePrivateAddresses, removePublicAddresses } from './utils.js' -import { Logger, logger } from '@libp2p/logger' +import type { KadDHTComponents, KadDHTInit } from './index.js' import type { QueryOptions, Validators, Selectors, DHT, QueryEvent } from '@libp2p/interface-dht' -import type { PeerInfo } from '@libp2p/interface-peer-info' -import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events' +import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery' import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerInfo } from '@libp2p/interface-peer-info' import type { CID } from 'multiformats/cid' -import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery' -import type { KadDHTComponents, KadDHTInit } from './index.js' -import { validators as recordValidators } from '@libp2p/record/validators' -import { selectors as recordSelectors } from '@libp2p/record/selectors' -import { symbol } from '@libp2p/interface-peer-discovery' -import { PROTOCOL_DHT, PROTOCOL_PREFIX, LAN_PREFIX } from './constants.js' -import pDefer from 'p-defer' export const DEFAULT_MAX_INBOUND_STREAMS = 32 export const DEFAULT_MAX_OUTBOUND_STREAMS = 64 @@ -219,13 +219,9 @@ export class KadDHT extends EventEmitter implements DHT { }) } - get [symbol] (): true { - return true - } + readonly [symbol] = true - get [Symbol.toStringTag] (): '@libp2p/kad-dht' { - return '@libp2p/kad-dht' - } + readonly [Symbol.toStringTag] = '@libp2p/kad-dht' async onPeerConnect (peerData: PeerInfo): Promise { this.log('peer %p connected with protocols', peerData.id, peerData.protocols) diff --git a/src/message/index.ts b/src/message/index.ts index 203f0d5113..ab946ab90c 100644 --- a/src/message/index.ts +++ b/src/message/index.ts @@ -1,6 +1,6 @@ import { peerIdFromBytes } from '@libp2p/peer-id' -import { multiaddr } from '@multiformats/multiaddr' import { Libp2pRecord } from '@libp2p/record' +import { multiaddr } from '@multiformats/multiaddr' import { Message as PBMessage } from './dht.js' import type { PeerInfo } from '@libp2p/interface-peer-info' import type { Uint8ArrayList } from 'uint8arraylist' diff --git a/src/network.ts b/src/network.ts index c1db024211..630bbf3986 100644 --- a/src/network.ts +++ b/src/network.ts @@ -1,28 +1,28 @@ import { CodeError } from '@libp2p/interfaces/errors' -import { pipe } from 'it-pipe' -import * as lp from 'it-length-prefixed' +import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' +import { logger } from '@libp2p/logger' +import { abortableDuplex } from 'abortable-iterator' import drain from 'it-drain' import first from 'it-first' +import * as lp from 'it-length-prefixed' +import { pipe } from 'it-pipe' import { Message } from './message/index.js' -import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' import { dialingPeerEvent, sendingQueryEvent, peerResponseEvent, queryErrorEvent } from './query/events.js' -import { logger } from '@libp2p/logger' +import type { KadDHTComponents } from './index.js' +import type { Stream } from '@libp2p/interface-connection' +import type { QueryEvent } from '@libp2p/interface-dht' import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerInfo } from '@libp2p/interface-peer-info' import type { AbortOptions } from '@libp2p/interfaces' import type { Startable } from '@libp2p/interfaces/startable' import type { Logger } from '@libp2p/logger' import type { Duplex, Source } from 'it-stream-types' -import type { PeerInfo } from '@libp2p/interface-peer-info' -import type { Stream } from '@libp2p/interface-connection' -import { abortableDuplex } from 'abortable-iterator' import type { Uint8ArrayList } from 'uint8arraylist' -import type { KadDHTComponents } from './index.js' -import type { QueryEvent } from '@libp2p/interface-dht' export interface NetworkInit { protocol: string diff --git a/src/peer-list/peer-distance-list.ts b/src/peer-list/peer-distance-list.ts index 072e655df1..5cccd9a51d 100644 --- a/src/peer-list/peer-distance-list.ts +++ b/src/peer-list/peer-distance-list.ts @@ -1,6 +1,6 @@ -import * as utils from '../utils.js' import { compare as uint8ArrayCompare } from 'uint8arrays/compare' import { xor as uint8ArrayXor } from 'uint8arrays/xor' +import * as utils from '../utils.js' import type { PeerId } from '@libp2p/interface-peer-id' interface PeerDistance { diff --git a/src/peer-routing/index.ts b/src/peer-routing/index.ts index 328a89562a..a0819f642f 100644 --- a/src/peer-routing/index.ts +++ b/src/peer-routing/index.ts @@ -1,28 +1,28 @@ +import { keys } from '@libp2p/crypto' import { CodeError } from '@libp2p/interfaces/errors' +import { logger } from '@libp2p/logger' +import { peerIdFromKeys } from '@libp2p/peer-id' +import { Libp2pRecord } from '@libp2p/record' import { verifyRecord } from '@libp2p/record/validators' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { Message, MESSAGE_TYPE } from '../message/index.js' -import * as utils from '../utils.js' +import { PeerDistanceList } from '../peer-list/peer-distance-list.js' import { queryErrorEvent, finalPeerEvent, valueEvent } from '../query/events.js' -import { PeerDistanceList } from '../peer-list/peer-distance-list.js' -import { Libp2pRecord } from '@libp2p/record' -import { logger } from '@libp2p/logger' -import { keys } from '@libp2p/crypto' -import { peerIdFromKeys } from '@libp2p/peer-id' -import type { DHTRecord, DialingPeerEvent, FinalPeerEvent, QueryEvent, Validators } from '@libp2p/interface-dht' -import type { RoutingTable } from '../routing-table/index.js' -import type { QueryManager, QueryOptions } from '../query/manager.js' +import * as utils from '../utils.js' +import type { KadDHTComponents } from '../index.js' import type { Network } from '../network.js' -import type { Logger } from '@libp2p/logger' -import type { AbortOptions } from '@libp2p/interfaces' +import type { QueryManager, QueryOptions } from '../query/manager.js' import type { QueryFunc } from '../query/types.js' -import type { PeerInfo } from '@libp2p/interface-peer-info' +import type { RoutingTable } from '../routing-table/index.js' +import type { DHTRecord, DialingPeerEvent, FinalPeerEvent, QueryEvent, Validators } from '@libp2p/interface-dht' import type { PeerId } from '@libp2p/interface-peer-id' -import type { KadDHTComponents } from '../index.js' +import type { PeerInfo } from '@libp2p/interface-peer-info' +import type { AbortOptions } from '@libp2p/interfaces' +import type { Logger } from '@libp2p/logger' export interface PeerRoutingInit { routingTable: RoutingTable diff --git a/src/providers.ts b/src/providers.ts index f5121f9d48..46e2703792 100644 --- a/src/providers.ts +++ b/src/providers.ts @@ -1,20 +1,20 @@ +import { logger } from '@libp2p/logger' +import { peerIdFromString } from '@libp2p/peer-id' import cache from 'hashlru' -import varint from 'varint' import { Key } from 'interface-datastore/key' import Queue from 'p-queue' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import varint from 'varint' import { PROVIDERS_CLEANUP_INTERVAL, PROVIDERS_VALIDITY, PROVIDERS_LRU_CACHE_SIZE, PROVIDER_KEY_PREFIX } from './constants.js' -import { logger } from '@libp2p/logger' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import { peerIdFromString } from '@libp2p/peer-id' -import type { Datastore } from 'interface-datastore' +import type { PeerId } from '@libp2p/interface-peer-id' import type { Startable } from '@libp2p/interfaces/startable' +import type { Datastore } from 'interface-datastore' import type { CID } from 'multiformats' -import type { PeerId } from '@libp2p/interface-peer-id' const log = logger('libp2p:kad-dht:providers') @@ -209,7 +209,7 @@ export class Providers implements Startable { * Get a list of providers for the given CID */ async getProviders (cid: CID): Promise { - return await this.syncQueue.add(async () => { + return this.syncQueue.add(async () => { log('get providers for %s', cid) const provs = await this._getProvidersMap(cid) diff --git a/src/query-self.ts b/src/query-self.ts index 6b2fae6ab6..5aed773fe8 100644 --- a/src/query-self.ts +++ b/src/query-self.ts @@ -1,15 +1,15 @@ import { setMaxListeners } from 'events' -import take from 'it-take' +import { logger, type Logger } from '@libp2p/logger' +import { anySignal } from 'any-signal' import length from 'it-length' +import { pipe } from 'it-pipe' +import take from 'it-take' import { QUERY_SELF_INTERVAL, QUERY_SELF_TIMEOUT, K, QUERY_SELF_INITIAL_INTERVAL } from './constants.js' -import { anySignal } from 'any-signal' -import { logger, Logger } from '@libp2p/logger' +import type { KadDHTComponents } from './index.js' import type { PeerRouting } from './peer-routing/index.js' +import type { RoutingTable } from './routing-table/index.js' import type { Startable } from '@libp2p/interfaces/startable' -import { pipe } from 'it-pipe' -import type { KadDHTComponents } from './index.js' import type { DeferredPromise } from 'p-defer' -import type { RoutingTable } from './routing-table/index.js' export interface QuerySelfInit { lan: boolean @@ -146,7 +146,7 @@ export class QuerySelf implements Startable { isSelfQuery: true }), (source) => take(source, this.count), - async (source) => await length(source) + async (source) => length(source) ) this.log('self-query ran successfully - found %d peers', found) diff --git a/src/query/events.ts b/src/query/events.ts index a52b3c1e56..3f4a7e5790 100644 --- a/src/query/events.ts +++ b/src/query/events.ts @@ -1,8 +1,8 @@ import { MESSAGE_TYPE_LOOKUP } from '../message/index.js' import type { Message } from '../message/dht.js' import type { SendingQueryEvent, PeerResponseEvent, DialingPeerEvent, AddingPeerEvent, ValueEvent, ProviderEvent, QueryErrorEvent, FinalPeerEvent } from '@libp2p/interface-dht' -import type { PeerInfo } from '@libp2p/interface-peer-info' import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerInfo } from '@libp2p/interface-peer-info' import type { Libp2pRecord } from '@libp2p/record' export interface QueryEventFields { diff --git a/src/query/manager.ts b/src/query/manager.ts index 799bcbd3e5..81d8dc47d1 100644 --- a/src/query/manager.ts +++ b/src/query/manager.ts @@ -1,22 +1,22 @@ +import { setMaxListeners } from 'events' +import { AbortError } from '@libp2p/interfaces/errors' +import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' +import { logger } from '@libp2p/logger' +import { PeerSet } from '@libp2p/peer-collections' import { anySignal } from 'any-signal' +import merge from 'it-merge' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { ALPHA, K, DEFAULT_QUERY_TIMEOUT } from '../constants.js' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { queryPath } from './query-path.js' -import merge from 'it-merge' -import { setMaxListeners } from 'events' -import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events' -import { logger } from '@libp2p/logger' -import type { PeerId } from '@libp2p/interface-peer-id' -import type { Startable } from '@libp2p/interfaces/startable' import type { QueryFunc } from './types.js' import type { QueryEvent } from '@libp2p/interface-dht' -import { PeerSet } from '@libp2p/peer-collections' import type { Metric, Metrics } from '@libp2p/interface-metrics' -import type { DeferredPromise } from 'p-defer' +import type { PeerId } from '@libp2p/interface-peer-id' import type { AbortOptions } from '@libp2p/interfaces' -import { AbortError } from '@libp2p/interfaces/errors' +import type { Startable } from '@libp2p/interfaces/startable' +import type { DeferredPromise } from 'p-defer' export interface CleanUpEvents { 'cleanup': CustomEvent diff --git a/src/query/query-path.ts b/src/query/query-path.ts index ca7236d36d..88b887f272 100644 --- a/src/query/query-path.ts +++ b/src/query/query-path.ts @@ -1,18 +1,18 @@ +import { CodeError } from '@libp2p/interfaces/errors' +import { anySignal } from 'any-signal' +import defer from 'p-defer' import Queue from 'p-queue' -import { xor } from 'uint8arrays/xor' import { toString } from 'uint8arrays/to-string' -import defer from 'p-defer' -import { CodeError } from '@libp2p/interfaces/errors' +import { xor } from 'uint8arrays/xor' import { convertPeerId, convertBuffer } from '../utils.js' -import { anySignal } from 'any-signal' -import type { PeerId } from '@libp2p/interface-peer-id' -import type { EventEmitter } from '@libp2p/interfaces/events' +import { queryErrorEvent } from './events.js' import type { CleanUpEvents } from './manager.js' -import type { Logger } from '@libp2p/logger' import type { QueryFunc } from '../query/types.js' import type { QueryEvent } from '@libp2p/interface-dht' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { EventEmitter } from '@libp2p/interfaces/events' +import type { Logger } from '@libp2p/logger' import type { PeerSet } from '@libp2p/peer-collections' -import { queryErrorEvent } from './events.js' const MAX_XOR = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF') diff --git a/src/query/types.ts b/src/query/types.ts index b7a4ee9df3..0ebedefe21 100644 --- a/src/query/types.ts +++ b/src/query/types.ts @@ -1,5 +1,5 @@ -import type { PeerId } from '@libp2p/interface-peer-id' import type { QueryEvent } from '@libp2p/interface-dht' +import type { PeerId } from '@libp2p/interface-peer-id' export interface QueryContext { // the key we are looking up diff --git a/src/routing-table/index.ts b/src/routing-table/index.ts index be19928ce5..e9b6290ba3 100644 --- a/src/routing-table/index.ts +++ b/src/routing-table/index.ts @@ -1,15 +1,15 @@ +import { logger } from '@libp2p/logger' +import { PeerSet } from '@libp2p/peer-collections' // @ts-expect-error no types import KBuck from 'k-bucket' -import * as utils from '../utils.js' import Queue from 'p-queue' -import { logger } from '@libp2p/logger' +import * as utils from '../utils.js' +import type { ConnectionManager } from '@libp2p/interface-connection-manager' +import type { Metric, Metrics } from '@libp2p/interface-metrics' import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerStore } from '@libp2p/interface-peer-store' import type { Startable } from '@libp2p/interfaces/startable' import type { Logger } from '@libp2p/logger' -import { PeerSet } from '@libp2p/peer-collections' -import type { Metric, Metrics } from '@libp2p/interface-metrics' -import type { PeerStore } from '@libp2p/interface-peer-store' -import type { ConnectionManager } from '@libp2p/interface-connection-manager' export const KAD_CLOSE_TAG_NAME = 'kad-close' export const KAD_CLOSE_TAG_VALUE = 50 diff --git a/src/routing-table/refresh.ts b/src/routing-table/refresh.ts index e7f9c7f14d..5c3d198c1c 100644 --- a/src/routing-table/refresh.ts +++ b/src/routing-table/refresh.ts @@ -1,15 +1,15 @@ -import { xor as uint8ArrayXor } from 'uint8arrays/xor' -import GENERATED_PREFIXES from './generated-prefix-list.js' -import { sha256 } from 'multiformats/hashes/sha2' import { randomBytes } from '@libp2p/crypto' -import { peerIdFromBytes } from '@libp2p/peer-id' import { logger } from '@libp2p/logger' +import { peerIdFromBytes } from '@libp2p/peer-id' import length from 'it-length' +import { sha256 } from 'multiformats/hashes/sha2' +import { xor as uint8ArrayXor } from 'uint8arrays/xor' import { TABLE_REFRESH_INTERVAL, TABLE_REFRESH_QUERY_TIMEOUT } from '../constants.js' +import GENERATED_PREFIXES from './generated-prefix-list.js' import type { RoutingTable } from './index.js' -import type { Logger } from '@libp2p/logger' import type { PeerRouting } from '../peer-routing/index.js' import type { PeerId } from '@libp2p/interface-peer-id' +import type { Logger } from '@libp2p/logger' /** * Cannot generate random KadIds longer than this + 1 diff --git a/src/rpc/handlers/add-provider.ts b/src/rpc/handlers/add-provider.ts index 347276f41c..f01c5f21da 100644 --- a/src/rpc/handlers/add-provider.ts +++ b/src/rpc/handlers/add-provider.ts @@ -1,10 +1,10 @@ -import { CID } from 'multiformats/cid' import { CodeError } from '@libp2p/interfaces/errors' import { logger } from '@libp2p/logger' +import { CID } from 'multiformats/cid' +import type { Message } from '../../message/index.js' import type { Providers } from '../../providers' -import type { PeerId } from '@libp2p/interface-peer-id' import type { DHTMessageHandler } from '../index.js' -import type { Message } from '../../message/index.js' +import type { PeerId } from '@libp2p/interface-peer-id' const log = logger('libp2p:kad-dht:rpc:handlers:add-provider') diff --git a/src/rpc/handlers/find-node.ts b/src/rpc/handlers/find-node.ts index f15d6c1d1a..1bf7b6ce47 100644 --- a/src/rpc/handlers/find-node.ts +++ b/src/rpc/handlers/find-node.ts @@ -1,16 +1,16 @@ -import { Message } from '../../message/index.js' import { logger } from '@libp2p/logger' +import { protocols } from '@multiformats/multiaddr' +import { equals as uint8ArrayEquals } from 'uint8arrays' +import { Message } from '../../message/index.js' import { removePrivateAddresses, removePublicAddresses } from '../../utils.js' -import { equals as uint8ArrayEquals } from 'uint8arrays' -import { protocols } from '@multiformats/multiaddr' -import type { PeerInfo } from '@libp2p/interface-peer-info' -import type { DHTMessageHandler } from '../index.js' import type { PeerRouting } from '../../peer-routing/index.js' -import type { PeerId } from '@libp2p/interface-peer-id' +import type { DHTMessageHandler } from '../index.js' import type { AddressManager } from '@libp2p/interface-address-manager' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerInfo } from '@libp2p/interface-peer-info' const log = logger('libp2p:kad-dht:rpc:handlers:find-node') diff --git a/src/rpc/handlers/get-providers.ts b/src/rpc/handlers/get-providers.ts index b79bde5c0e..c213591b61 100644 --- a/src/rpc/handlers/get-providers.ts +++ b/src/rpc/handlers/get-providers.ts @@ -1,14 +1,14 @@ -import { CID } from 'multiformats/cid' import { CodeError } from '@libp2p/interfaces/errors' +import { logger } from '@libp2p/logger' +import { CID } from 'multiformats/cid' import { Message } from '../../message/index.js' import { removePrivateAddresses, removePublicAddresses } from '../../utils.js' -import { logger } from '@libp2p/logger' -import type { DHTMessageHandler } from '../index.js' -import type { Providers } from '../../providers.js' import type { PeerRouting } from '../../peer-routing/index.js' +import type { Providers } from '../../providers.js' +import type { DHTMessageHandler } from '../index.js' import type { PeerId } from '@libp2p/interface-peer-id' import type { PeerInfo } from '@libp2p/interface-peer-info' import type { PeerStore } from '@libp2p/interface-peer-store' diff --git a/src/rpc/handlers/get-value.ts b/src/rpc/handlers/get-value.ts index 8b746710bf..d5a7c0a311 100644 --- a/src/rpc/handlers/get-value.ts +++ b/src/rpc/handlers/get-value.ts @@ -1,14 +1,14 @@ -import { Libp2pRecord } from '@libp2p/record' import { CodeError } from '@libp2p/interfaces/errors' -import { Message, MESSAGE_TYPE } from '../../message/index.js' +import { logger } from '@libp2p/logger' +import { Libp2pRecord } from '@libp2p/record' import { MAX_RECORD_AGE } from '../../constants.js' +import { Message, MESSAGE_TYPE } from '../../message/index.js' import { bufferToRecordKey, isPublicKeyKey, fromPublicKeyKey } from '../../utils.js' -import { logger } from '@libp2p/logger' +import type { PeerRouting } from '../../peer-routing/index.js' import type { DHTMessageHandler } from '../index.js' import type { PeerId } from '@libp2p/interface-peer-id' -import type { PeerRouting } from '../../peer-routing/index.js' import type { PeerStore } from '@libp2p/interface-peer-store' import type { Datastore } from 'interface-datastore' diff --git a/src/rpc/handlers/put-value.ts b/src/rpc/handlers/put-value.ts index 0dcb960700..04aad2b52e 100644 --- a/src/rpc/handlers/put-value.ts +++ b/src/rpc/handlers/put-value.ts @@ -1,11 +1,11 @@ -import { bufferToRecordKey } from '../../utils.js' import { CodeError } from '@libp2p/interfaces/errors' +import { type Logger, logger } from '@libp2p/logger' import { verifyRecord } from '@libp2p/record/validators' -import { Logger, logger } from '@libp2p/logger' +import { bufferToRecordKey } from '../../utils.js' +import type { Message } from '../../message/index.js' import type { DHTMessageHandler } from '../index.js' import type { Validators } from '@libp2p/interface-dht' import type { PeerId } from '@libp2p/interface-peer-id' -import type { Message } from '../../message/index.js' import type { Datastore } from 'interface-datastore' export interface PutValueHandlerInit { diff --git a/src/rpc/index.ts b/src/rpc/index.ts index f79fd2ee43..83507ea79a 100644 --- a/src/rpc/index.ts +++ b/src/rpc/index.ts @@ -1,19 +1,19 @@ -import { pipe } from 'it-pipe' +import { type Logger, logger } from '@libp2p/logger' import * as lp from 'it-length-prefixed' -import { Logger, logger } from '@libp2p/logger' -import type { RoutingTable } from '../routing-table' -import type { PeerId } from '@libp2p/interface-peer-id' +import { pipe } from 'it-pipe' import { Message, MESSAGE_TYPE } from '../message/index.js' import { AddProviderHandler } from './handlers/add-provider.js' -import { FindNodeHandler, FindNodeHandlerComponents } from './handlers/find-node.js' -import { GetProvidersHandler, GetProvidersHandlerComponents } from './handlers/get-providers.js' -import { GetValueHandler, GetValueHandlerComponents } from './handlers/get-value.js' +import { FindNodeHandler, type FindNodeHandlerComponents } from './handlers/find-node.js' +import { GetProvidersHandler, type GetProvidersHandlerComponents } from './handlers/get-providers.js' +import { GetValueHandler, type GetValueHandlerComponents } from './handlers/get-value.js' import { PingHandler } from './handlers/ping.js' -import { PutValueHandler, PutValueHandlerComponents } from './handlers/put-value.js' -import type { IncomingStreamData } from '@libp2p/interface-registrar' -import type { Providers } from '../providers' +import { PutValueHandler, type PutValueHandlerComponents } from './handlers/put-value.js' import type { PeerRouting } from '../peer-routing' +import type { Providers } from '../providers' +import type { RoutingTable } from '../routing-table' import type { Validators } from '@libp2p/interface-dht' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { IncomingStreamData } from '@libp2p/interface-registrar' export interface DHTMessageHandler { handle: (peerId: PeerId, msg: Message) => Promise @@ -69,7 +69,7 @@ export class RPC { return } - return await handler.handle(peerId, msg) + return handler.handle(peerId, msg) } /** diff --git a/src/topology-listener.ts b/src/topology-listener.ts index 0758409f8c..162e2c782c 100644 --- a/src/topology-listener.ts +++ b/src/topology-listener.ts @@ -1,10 +1,10 @@ -import { createTopology } from '@libp2p/topology' import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events' import { logger } from '@libp2p/logger' -import type { Logger } from '@libp2p/logger' -import type { Startable } from '@libp2p/interfaces/startable' -import type { PeerId } from '@libp2p/interface-peer-id' +import { createTopology } from '@libp2p/topology' import type { KadDHTComponents } from '.' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { Startable } from '@libp2p/interfaces/startable' +import type { Logger } from '@libp2p/logger' export interface TopologyListenerInit { protocol: string diff --git a/src/utils.ts b/src/utils.ts index 1bfeaf182d..dccf4c5b08 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,14 +1,14 @@ -import { sha256 } from 'multiformats/hashes/sha2' -import { Key } from 'interface-datastore/key' +import { peerIdFromBytes } from '@libp2p/peer-id' import { Libp2pRecord } from '@libp2p/record' +import { Key } from 'interface-datastore/key' +import { sha256 } from 'multiformats/hashes/sha2' +import isPrivateIp from 'private-ip' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import { concat as uint8ArrayConcat } from 'uint8arrays/concat' -import isPrivateIp from 'private-ip' -import type { PeerInfo } from '@libp2p/interface-peer-info' -import { peerIdFromBytes } from '@libp2p/peer-id' -import type { PeerId } from '@libp2p/interface-peer-id' import { RECORD_KEY_PREFIX } from './constants.js' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerInfo } from '@libp2p/interface-peer-info' // const IPNS_PREFIX = uint8ArrayFromString('/ipns/') const PK_PREFIX = uint8ArrayFromString('/pk/') @@ -92,7 +92,7 @@ export async function convertBuffer (buf: Uint8Array): Promise { * Creates a DHT ID by hashing a Peer ID */ export async function convertPeerId (peerId: PeerId): Promise { - return await convertBuffer(peerId.toBytes()) + return convertBuffer(peerId.toBytes()) } /** diff --git a/test/generate-peers/generate-peers.node.ts b/test/generate-peers/generate-peers.node.ts index a7a1d388c0..f429d6534c 100644 --- a/test/generate-peers/generate-peers.node.ts +++ b/test/generate-peers/generate-peers.node.ts @@ -1,17 +1,17 @@ /* eslint-env mocha */ +import path from 'path' +import { fileURLToPath } from 'url' +import { createRSAPeerId } from '@libp2p/peer-id-factory' import { expect } from 'aegir/chai' -import which from 'which' import { execa } from 'execa' +import { stubInterface } from 'ts-sinon' import { toString as uintArrayToString } from 'uint8arrays/to-string' +import which from 'which' import { RoutingTable } from '../../src/routing-table/index.js' import { RoutingTableRefresh } from '../../src/routing-table/refresh.js' -import { createRSAPeerId } from '@libp2p/peer-id-factory' import { convertPeerId } from '../../src/utils.js' -import { stubInterface } from 'ts-sinon' -import path from 'path' -import { fileURLToPath } from 'url' import type { ConnectionManager } from '@libp2p/interface-connection-manager' import type { PeerStore } from '@libp2p/interface-peer-store' diff --git a/test/kad-dht.spec.ts b/test/kad-dht.spec.ts index f9a49c44fd..82391edd79 100644 --- a/test/kad-dht.spec.ts +++ b/test/kad-dht.spec.ts @@ -1,32 +1,32 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -import { expect } from 'aegir/chai' -import sinon from 'sinon' -import { Libp2pRecord } from '@libp2p/record' +import { EventTypes, type FinalPeerEvent, MessageType, type QueryEvent, type ValueEvent } from '@libp2p/interface-dht' import { CodeError } from '@libp2p/interfaces/errors' -import { equals as uint8ArrayEquals } from 'uint8arrays/equals' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import drain from 'it-drain' -import { EventTypes, FinalPeerEvent, MessageType, QueryEvent, ValueEvent } from '@libp2p/interface-dht' -import all from 'it-all' +import { Libp2pRecord } from '@libp2p/record' +import { expect } from 'aegir/chai' import delay from 'delay' +import all from 'it-all' +import drain from 'it-drain' import filter from 'it-filter' import last from 'it-last' -import * as kadUtils from '../src/utils.js' +import map from 'it-map' +import { pipe } from 'it-pipe' +import sinon from 'sinon' +import { equals as uint8ArrayEquals } from 'uint8arrays/equals' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import * as c from '../src/constants.js' import { MESSAGE_TYPE } from '../src/message/index.js' import { peerResponseEvent } from '../src/query/events.js' +import * as kadUtils from '../src/utils.js' import { createPeerIds } from './utils/create-peer-id.js' import { createValues } from './utils/create-values.js' -import { TestDHT } from './utils/test-dht.js' import { countDiffPeers } from './utils/index.js' import { sortClosestPeers } from './utils/sort-closest-peers.js' +import { TestDHT } from './utils/test-dht.js' +import type { DualKadDHT } from '../src/dual-kad-dht.js' import type { PeerId } from '@libp2p/interface-peer-id' import type { CID } from 'multiformats/cid' -import type { DualKadDHT } from '../src/dual-kad-dht.js' -import { pipe } from 'it-pipe' -import map from 'it-map' async function findEvent (events: AsyncIterable, name: 'FINAL_PEER'): Promise async function findEvent (events: AsyncIterable, name: 'VALUE'): Promise @@ -689,10 +689,10 @@ describe('KadDHT', () => { const nDHTs = 101 const dhts = await Promise.all( - new Array(nDHTs).fill(0).map(async () => await tdht.spawn()) + new Array(nDHTs).fill(0).map(async () => tdht.spawn()) ) - const dhtsById: Map = new Map(dhts.map((d) => [d.components.peerId, d])) + const dhtsById = new Map(dhts.map((d) => [d.components.peerId, d])) const ids = [...dhtsById.keys()] // The origin node for the FIND_PEER query @@ -755,7 +755,7 @@ describe('KadDHT', () => { source => filter(source, (event) => event.type === EventTypes.FINAL_PEER), // @ts-expect-error tsc has problems with filtering source => map(source, (event) => event.peer.id), - async source => await all(source) + async source => all(source) ) const actualClosest = await sortClosestPeers(otherIds, rtval) @@ -779,7 +779,7 @@ describe('KadDHT', () => { const nDHTs = 30 const dhts = await Promise.all( - new Array(nDHTs).fill(0).map(async () => await tdht.spawn()) + new Array(nDHTs).fill(0).map(async () => tdht.spawn()) ) const connected: Array> = [] diff --git a/test/kad-utils.spec.ts b/test/kad-utils.spec.ts index 9a5f91cc7d..2d99cc4ebf 100644 --- a/test/kad-utils.spec.ts +++ b/test/kad-utils.spec.ts @@ -1,10 +1,10 @@ /* eslint-env mocha */ +import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import { multiaddr } from '@multiformats/multiaddr' import * as utils from '../src/utils.js' import { createPeerId, createPeerIds } from './utils/create-peer-id.js' diff --git a/test/message.node.ts b/test/message.node.ts index 24ee27d0e0..7154664498 100644 --- a/test/message.node.ts +++ b/test/message.node.ts @@ -1,11 +1,11 @@ /* eslint-env mocha */ -import { expect } from 'aegir/chai' -import range from 'lodash.range' import fs from 'fs' import path from 'path' -import { Message } from '../src/message/index.js' import { isPeerId } from '@libp2p/interface-peer-id' +import { expect } from 'aegir/chai' +import range from 'lodash.range' +import { Message } from '../src/message/index.js' describe('Message', () => { it('go-interop', () => { diff --git a/test/message.spec.ts b/test/message.spec.ts index e360b013ad..64cd22bc34 100644 --- a/test/message.spec.ts +++ b/test/message.spec.ts @@ -1,12 +1,12 @@ /* eslint-env mocha */ -import { expect } from 'aegir/chai' +import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import { Libp2pRecord } from '@libp2p/record' import { multiaddr } from '@multiformats/multiaddr' +import { expect } from 'aegir/chai' import random from 'lodash.random' -import { Libp2pRecord } from '@libp2p/record' -import { Message, MESSAGE_TYPE } from '../src/message/index.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import { Message, MESSAGE_TYPE } from '../src/message/index.js' describe('Message', () => { it('create', () => { @@ -23,7 +23,7 @@ describe('Message', () => { this.timeout(10 * 1000) const peers = await Promise.all( - Array.from({ length: 5 }).map(async () => await createEd25519PeerId())) + Array.from({ length: 5 }).map(async () => createEd25519PeerId())) const closer = peers.slice(0, 5).map((p) => ({ id: p, diff --git a/test/multiple-nodes.spec.ts b/test/multiple-nodes.spec.ts index bc73477483..aa12d18e5e 100644 --- a/test/multiple-nodes.spec.ts +++ b/test/multiple-nodes.spec.ts @@ -1,10 +1,10 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' -import { TestDHT } from './utils/test-dht.js' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import drain from 'it-drain' import last from 'it-last' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { TestDHT } from './utils/test-dht.js' import type { DualKadDHT } from '../src/dual-kad-dht.js' describe('multiple nodes', function () { @@ -17,7 +17,7 @@ describe('multiple nodes', function () { beforeEach(async function () { tdht = new TestDHT() dhts = await Promise.all( - new Array(n).fill(0).map(async () => await tdht.spawn({ + new Array(n).fill(0).map(async () => tdht.spawn({ clientMode: false })) ) @@ -26,7 +26,7 @@ describe('multiple nodes', function () { const range = Array.from(Array(n - 1).keys()) // connect the last one with the others one by one - return await Promise.all(range.map(async (i) => { await tdht.connect(dhts[n - 1], dhts[i]) })) + return Promise.all(range.map(async (i) => { await tdht.connect(dhts[n - 1], dhts[i]) })) }) afterEach(async function () { diff --git a/test/network.spec.ts b/test/network.spec.ts index 4056898268..cf25d38c41 100644 --- a/test/network.spec.ts +++ b/test/network.spec.ts @@ -1,21 +1,21 @@ /* eslint-env mocha */ +import { mockStream } from '@libp2p/interface-mocks' import { expect } from 'aegir/chai' -import { pipe } from 'it-pipe' +import all from 'it-all' import * as lp from 'it-length-prefixed' +import map from 'it-map' +import { pipe } from 'it-pipe' import pDefer from 'p-defer' +import { Uint8ArrayList } from 'uint8arraylist' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import all from 'it-all' import { Message, MESSAGE_TYPE } from '../src/message/index.js' import { TestDHT } from './utils/test-dht.js' -import { mockStream } from '@libp2p/interface-mocks' import type { DualKadDHT } from '../src/dual-kad-dht.js' import type { Connection } from '@libp2p/interface-connection' import type { PeerId } from '@libp2p/interface-peer-id' -import type { Sink, Source } from 'it-stream-types' -import { Uint8ArrayList } from 'uint8arraylist' -import map from 'it-map' import type { Multiaddr } from '@multiformats/multiaddr' +import type { Sink, Source } from 'it-stream-types' describe('Network', () => { let dht: DualKadDHT @@ -78,7 +78,7 @@ describe('Network', () => { const res = await pipe( source, (source) => lp.decode(source), - async (source) => await all(source) + async (source) => all(source) ) expect(Message.deserialize(res[0]).type).to.eql(MESSAGE_TYPE.PING) finish() @@ -107,7 +107,7 @@ describe('Network', () => { expect(response).to.have.property('messageType', MESSAGE_TYPE.FIND_NODE) finish() - return await defer.promise + return defer.promise }) }) }) diff --git a/test/peer-distance-list.spec.ts b/test/peer-distance-list.spec.ts index 25f357aa8e..06bc6ece8f 100644 --- a/test/peer-distance-list.spec.ts +++ b/test/peer-distance-list.spec.ts @@ -1,9 +1,9 @@ /* eslint-env mocha */ -import { expect } from 'aegir/chai' import { peerIdFromString } from '@libp2p/peer-id' -import * as kadUtils from '../src/utils.js' +import { expect } from 'aegir/chai' import { PeerDistanceList } from '../src/peer-list/peer-distance-list.js' +import * as kadUtils from '../src/utils.js' describe('PeerDistanceList', () => { const p1 = peerIdFromString('12D3KooWSExt8hTzoaHEhn435BTK6BPNSY1LpTc1j2o9Gw53tXE1') diff --git a/test/peer-list.spec.ts b/test/peer-list.spec.ts index 5e33412697..f2ebd2fecf 100644 --- a/test/peer-list.spec.ts +++ b/test/peer-list.spec.ts @@ -1,9 +1,9 @@ /* eslint-env mocha */ -import type { PeerId } from '@libp2p/interface-peer-id' import { expect } from 'aegir/chai' import { PeerList } from '../src/peer-list/index.js' import { createPeerIds } from './utils/create-peer-id.js' +import type { PeerId } from '@libp2p/interface-peer-id' describe('PeerList', () => { let peers: PeerId[] diff --git a/test/providers.node.ts b/test/providers.node.ts index 6cbcf86e4a..934142165f 100644 --- a/test/providers.node.ts +++ b/test/providers.node.ts @@ -1,12 +1,12 @@ /* eslint-env mocha */ -import { LevelDatastore } from 'datastore-level' -import path from 'path' import os from 'os' +import path from 'path' +import { MemoryDatastore } from 'datastore-core/memory' +import { LevelDatastore } from 'datastore-level' import { Providers } from '../src/providers.js' -import { createValues } from './utils/create-values.js' import { createPeerIds } from './utils/create-peer-id.js' -import { MemoryDatastore } from 'datastore-core/memory' +import { createValues } from './utils/create-values.js' describe('Providers', () => { let providers: Providers diff --git a/test/providers.spec.ts b/test/providers.spec.ts index 7a429d7275..84051bf8e0 100644 --- a/test/providers.spec.ts +++ b/test/providers.spec.ts @@ -2,13 +2,13 @@ import { expect } from 'aegir/chai' import { MemoryDatastore } from 'datastore-core/memory' +import delay from 'delay' import { CID } from 'multiformats/cid' import { sha256 } from 'multiformats/hashes/sha2' -import { Providers } from '../src/providers.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { Providers } from '../src/providers.js' import { createPeerIds } from './utils/create-peer-id.js' import type { PeerId } from '@libp2p/interface-peer-id' -import delay from 'delay' describe('Providers', () => { let peers: PeerId[] @@ -69,13 +69,13 @@ describe('Providers', () => { }) const hashes = await Promise.all([...new Array(100)].map(async (i: number) => { - return await sha256.digest(uint8ArrayFromString(`hello ${i}`)) + return sha256.digest(uint8ArrayFromString(`hello ${i}`)) })) const cids = hashes.map((h) => CID.createV0(h)) await Promise.all(cids.map(async cid => { await providers.addProvider(cid, peers[0]) })) - const provs = await Promise.all(cids.map(async cid => await providers.getProviders(cid))) + const provs = await Promise.all(cids.map(async cid => providers.getProviders(cid))) expect(provs).to.have.length(100) for (const p of provs) { diff --git a/test/query.spec.ts b/test/query.spec.ts index 55468f5e4a..81b329b26c 100644 --- a/test/query.spec.ts +++ b/test/query.spec.ts @@ -1,24 +1,24 @@ /* eslint-env mocha */ +import { EventTypes, type QueryEvent } from '@libp2p/interface-dht' import { expect } from 'aegir/chai' import delay from 'delay' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { QueryManager, QueryManagerInit } from '../src/query/manager.js' -import { createPeerId, createPeerIds } from './utils/create-peer-id.js' import all from 'it-all' import drain from 'it-drain' -import { sortClosestPeers } from './utils/sort-closest-peers.js' +import pDefer from 'p-defer' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { MESSAGE_TYPE } from '../src/message/index.js' import { peerResponseEvent, valueEvent, queryErrorEvent } from '../src/query/events.js' -import type { PeerId } from '@libp2p/interface-peer-id' -import { EventTypes, QueryEvent } from '@libp2p/interface-dht' -import { MESSAGE_TYPE } from '../src/message/index.js' -import type { QueryFunc } from '../src/query/types.js' +import { QueryManager, type QueryManagerInit } from '../src/query/manager.js' import { convertBuffer } from '../src/utils.js' -import pDefer from 'p-defer' +import { createPeerId, createPeerIds } from './utils/create-peer-id.js' +import { sortClosestPeers } from './utils/sort-closest-peers.js' +import type { QueryFunc } from '../src/query/types.js' +import type { PeerId } from '@libp2p/interface-peer-id' interface TopologyEntry { delay?: number diff --git a/test/routing-table.spec.ts b/test/routing-table.spec.ts index 191261a9d5..e7de073797 100644 --- a/test/routing-table.spec.ts +++ b/test/routing-table.spec.ts @@ -1,28 +1,28 @@ /* eslint-env mocha */ +import { mockConnectionManager } from '@libp2p/interface-mocks' +import { EventEmitter } from '@libp2p/interfaces/events' +import { PeerSet } from '@libp2p/peer-collections' +import { peerIdFromString } from '@libp2p/peer-id' +import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import { PersistentPeerStore } from '@libp2p/peer-store' import { expect } from 'aegir/chai' +import { MemoryDatastore } from 'datastore-core' +import all from 'it-all' +import { pipe } from 'it-pipe' import random from 'lodash.random' +import pWaitFor from 'p-wait-for' import sinon from 'sinon' -import { KAD_CLOSE_TAG_NAME, KAD_CLOSE_TAG_VALUE, KBUCKET_SIZE, RoutingTable, RoutingTableComponents } from '../src/routing-table/index.js' +import { stubInterface } from 'ts-sinon' +import { PROTOCOL_DHT } from '../src/constants.js' +import { KAD_CLOSE_TAG_NAME, KAD_CLOSE_TAG_VALUE, KBUCKET_SIZE, RoutingTable, type RoutingTableComponents } from '../src/routing-table/index.js' import * as kadUtils from '../src/utils.js' import { createPeerId, createPeerIds } from './utils/create-peer-id.js' -import { PROTOCOL_DHT } from '../src/constants.js' -import { peerIdFromString } from '@libp2p/peer-id' -import { mockConnectionManager } from '@libp2p/interface-mocks' -import { PersistentPeerStore } from '@libp2p/peer-store' -import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { sortClosestPeers } from './utils/sort-closest-peers.js' -import pWaitFor from 'p-wait-for' -import { pipe } from 'it-pipe' -import all from 'it-all' -import { PeerSet } from '@libp2p/peer-collections' -import { stubInterface } from 'ts-sinon' -import type { Registrar } from '@libp2p/interface-registrar' import type { ConnectionManager } from '@libp2p/interface-connection-manager' -import type { PeerStore } from '@libp2p/interface-peer-store' -import { MemoryDatastore } from 'datastore-core' -import { EventEmitter } from '@libp2p/interfaces/events' import type { Libp2pEvents } from '@libp2p/interface-libp2p' +import type { PeerStore } from '@libp2p/interface-peer-store' +import type { Registrar } from '@libp2p/interface-registrar' describe('Routing Table', () => { let table: RoutingTable @@ -267,7 +267,7 @@ describe('Routing Table', () => { } } }, - async (source) => await all(source) + async (source) => all(source) )) } @@ -275,7 +275,7 @@ describe('Routing Table', () => { const localNodeId = await kadUtils.convertPeerId(components.peerId) const sortedPeerList = await sortClosestPeers( await Promise.all( - new Array(KBUCKET_SIZE + 1).fill(0).map(async () => await createEd25519PeerId()) + new Array(KBUCKET_SIZE + 1).fill(0).map(async () => createEd25519PeerId()) ), localNodeId ) diff --git a/test/rpc/handlers/add-provider.spec.ts b/test/rpc/handlers/add-provider.spec.ts index 66b0b1fedd..3455ce5e70 100644 --- a/test/rpc/handlers/add-provider.spec.ts +++ b/test/rpc/handlers/add-provider.spec.ts @@ -1,17 +1,17 @@ /* eslint-env mocha */ -import { expect } from 'aegir/chai' import { multiaddr } from '@multiformats/multiaddr' +import { expect } from 'aegir/chai' +import { MemoryDatastore } from 'datastore-core' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { Message, MESSAGE_TYPE } from '../../../src/message/index.js' +import { Providers } from '../../../src/providers.js' import { AddProviderHandler } from '../../../src/rpc/handlers/add-provider.js' import { createPeerIds } from '../../utils/create-peer-id.js' -import type { PeerId } from '@libp2p/interface-peer-id' import { createValues } from '../../utils/create-values.js' -import type { CID } from 'multiformats' import type { DHTMessageHandler } from '../../../src/rpc/index.js' -import { Providers } from '../../../src/providers.js' -import { MemoryDatastore } from 'datastore-core' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { CID } from 'multiformats' describe('rpc - handlers - AddProvider', () => { let peerIds: PeerId[] diff --git a/test/rpc/handlers/find-node.spec.ts b/test/rpc/handlers/find-node.spec.ts index d2a52b140c..98ec8e84b0 100644 --- a/test/rpc/handlers/find-node.spec.ts +++ b/test/rpc/handlers/find-node.spec.ts @@ -1,16 +1,16 @@ /* eslint-env mocha */ +import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' +import Sinon, { type SinonStubbedInstance } from 'sinon' +import { stubInterface } from 'ts-sinon' import { Message, MESSAGE_TYPE } from '../../../src/message/index.js' +import { PeerRouting } from '../../../src/peer-routing/index.js' import { FindNodeHandler } from '../../../src/rpc/handlers/find-node.js' -import { multiaddr } from '@multiformats/multiaddr' import { createPeerId } from '../../utils/create-peer-id.js' -import { PeerRouting } from '../../../src/peer-routing/index.js' -import Sinon, { SinonStubbedInstance } from 'sinon' -import type { PeerId } from '@libp2p/interface-peer-id' import type { DHTMessageHandler } from '../../../src/rpc/index.js' import type { AddressManager } from '@libp2p/interface-address-manager' -import { stubInterface } from 'ts-sinon' +import type { PeerId } from '@libp2p/interface-peer-id' import type { StubbedInstance } from 'ts-sinon' const T = MESSAGE_TYPE.FIND_NODE diff --git a/test/rpc/handlers/get-providers.spec.ts b/test/rpc/handlers/get-providers.spec.ts index 46364ed844..49feba7fcc 100644 --- a/test/rpc/handlers/get-providers.spec.ts +++ b/test/rpc/handlers/get-providers.spec.ts @@ -1,21 +1,21 @@ /* eslint-env mocha */ +import { EventEmitter } from '@libp2p/interfaces/events' +import { PersistentPeerStore } from '@libp2p/peer-store' +import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' -import { Message, MESSAGE_TYPE } from '../../../src/message/index.js' -import { GetProvidersHandler, GetProvidersHandlerComponents } from '../../../src/rpc/handlers/get-providers.js' +import { MemoryDatastore } from 'datastore-core' +import Sinon, { type SinonStubbedInstance } from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { multiaddr } from '@multiformats/multiaddr' -import { createPeerId } from '../../utils/create-peer-id.js' -import { createValues, Value } from '../../utils/create-values.js' -import Sinon, { SinonStubbedInstance } from 'sinon' -import { Providers } from '../../../src/providers.js' +import { Message, MESSAGE_TYPE } from '../../../src/message/index.js' import { PeerRouting } from '../../../src/peer-routing/index.js' +import { Providers } from '../../../src/providers.js' +import { GetProvidersHandler, type GetProvidersHandlerComponents } from '../../../src/rpc/handlers/get-providers.js' +import { createPeerId } from '../../utils/create-peer-id.js' +import { createValues, type Value } from '../../utils/create-values.js' +import type { Libp2pEvents } from '@libp2p/interface-libp2p' import type { PeerId } from '@libp2p/interface-peer-id' -import { PersistentPeerStore } from '@libp2p/peer-store' -import { MemoryDatastore } from 'datastore-core' import type { PeerInfo } from '@libp2p/interface-peer-info' -import { EventEmitter } from '@libp2p/interfaces/events' -import type { Libp2pEvents } from '@libp2p/interface-libp2p' import type { PeerStore } from '@libp2p/interface-peer-store' const T = MESSAGE_TYPE.GET_PROVIDERS diff --git a/test/rpc/handlers/get-value.spec.ts b/test/rpc/handlers/get-value.spec.ts index 2c71870784..b8cb3b7b6b 100644 --- a/test/rpc/handlers/get-value.spec.ts +++ b/test/rpc/handlers/get-value.spec.ts @@ -1,22 +1,22 @@ /* eslint-env mocha */ +import { EventEmitter } from '@libp2p/interfaces/events' +import { PersistentPeerStore } from '@libp2p/peer-store' +import { Libp2pRecord } from '@libp2p/record' import { expect } from 'aegir/chai' +import { MemoryDatastore } from 'datastore-core' +import Sinon from 'sinon' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { Message, MESSAGE_TYPE } from '../../../src/message/index.js' -import { GetValueHandler, GetValueHandlerComponents } from '../../../src/rpc/handlers/get-value.js' +import { PeerRouting } from '../../../src/peer-routing/index.js' +import { GetValueHandler, type GetValueHandlerComponents } from '../../../src/rpc/handlers/get-value.js' import * as utils from '../../../src/utils.js' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { createPeerId } from '../../utils/create-peer-id.js' +import type { Libp2pEvents } from '@libp2p/interface-libp2p' import type { PeerId } from '@libp2p/interface-peer-id' -import type { SinonStubbedInstance } from 'sinon' -import { PeerRouting } from '../../../src/peer-routing/index.js' -import Sinon from 'sinon' -import { PersistentPeerStore } from '@libp2p/peer-store' -import { MemoryDatastore } from 'datastore-core' -import type { Datastore } from 'interface-datastore' -import { Libp2pRecord } from '@libp2p/record' import type { PeerStore } from '@libp2p/interface-peer-store' -import { EventEmitter } from '@libp2p/interfaces/events' -import type { Libp2pEvents } from '@libp2p/interface-libp2p' +import type { Datastore } from 'interface-datastore' +import type { SinonStubbedInstance } from 'sinon' const T = MESSAGE_TYPE.GET_VALUE diff --git a/test/rpc/handlers/ping.spec.ts b/test/rpc/handlers/ping.spec.ts index d3c49203c3..0c696c5441 100644 --- a/test/rpc/handlers/ping.spec.ts +++ b/test/rpc/handlers/ping.spec.ts @@ -1,9 +1,9 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { Message, MESSAGE_TYPE } from '../../../src/message/index.js' import { PingHandler } from '../../../src/rpc/handlers/ping.js' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { createPeerId } from '../../utils/create-peer-id.js' import type { DHTMessageHandler } from '../../../src/rpc/index.js' import type { PeerId } from '@libp2p/interface-peer-id' diff --git a/test/rpc/handlers/put-value.spec.ts b/test/rpc/handlers/put-value.spec.ts index 6b1e1fdf24..fe957e8aba 100644 --- a/test/rpc/handlers/put-value.spec.ts +++ b/test/rpc/handlers/put-value.spec.ts @@ -1,18 +1,18 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -import { expect } from 'aegir/chai' import { Libp2pRecord } from '@libp2p/record' +import { expect } from 'aegir/chai' +import { MemoryDatastore } from 'datastore-core' import delay from 'delay' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { Message, MESSAGE_TYPE } from '../../../src/message/index.js' import { PutValueHandler } from '../../../src/rpc/handlers/put-value.js' import * as utils from '../../../src/utils.js' import { createPeerId } from '../../utils/create-peer-id.js' +import type { Validators } from '@libp2p/interface-dht' import type { PeerId } from '@libp2p/interface-peer-id' import type { Datastore } from 'interface-datastore' -import { MemoryDatastore } from 'datastore-core' -import type { Validators } from '@libp2p/interface-dht' const T = MESSAGE_TYPE.PUT_VALUE diff --git a/test/rpc/index.node.ts b/test/rpc/index.node.ts index b21bdcbfee..556246d13f 100644 --- a/test/rpc/index.node.ts +++ b/test/rpc/index.node.ts @@ -1,34 +1,34 @@ /* eslint-env mocha */ +import { mockStream } from '@libp2p/interface-mocks' +import { EventEmitter } from '@libp2p/interfaces/events' +import { start } from '@libp2p/interfaces/startable' +import { PersistentPeerStore } from '@libp2p/peer-store' import { expect } from 'aegir/chai' -import pDefer from 'p-defer' -import { pipe } from 'it-pipe' -import * as lp from 'it-length-prefixed' +import { MemoryDatastore } from 'datastore-core' import all from 'it-all' -import { Message, MESSAGE_TYPE } from '../../src/message/index.js' -import { RPC, RPCComponents } from '../../src/rpc/index.js' +import * as lp from 'it-length-prefixed' +import map from 'it-map' +import { pipe } from 'it-pipe' +import pDefer from 'p-defer' +import Sinon, { type SinonStubbedInstance } from 'sinon' +import { stubInterface } from 'ts-sinon' +import { Uint8ArrayList } from 'uint8arraylist' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { createPeerId } from '../utils/create-peer-id.js' -import type { PeerId } from '@libp2p/interface-peer-id' -import { PersistentPeerStore } from '@libp2p/peer-store' -import { MemoryDatastore } from 'datastore-core' -import Sinon, { SinonStubbedInstance } from 'sinon' -import { Providers } from '../../src/providers.js' +import { Message, MESSAGE_TYPE } from '../../src/message/index.js' import { PeerRouting } from '../../src/peer-routing/index.js' -import type { Validators } from '@libp2p/interface-dht' -import type { Datastore } from 'interface-datastore' +import { Providers } from '../../src/providers.js' import { RoutingTable } from '../../src/routing-table/index.js' -import type { Duplex, Source } from 'it-stream-types' -import { mockStream } from '@libp2p/interface-mocks' -import { start } from '@libp2p/interfaces/startable' -import { Uint8ArrayList } from 'uint8arraylist' -import map from 'it-map' -import { stubInterface } from 'ts-sinon' -import type { Connection } from '@libp2p/interface-connection' +import { RPC, type RPCComponents } from '../../src/rpc/index.js' +import { createPeerId } from '../utils/create-peer-id.js' import type { AddressManager } from '@libp2p/interface-address-manager' -import type { PeerStore } from '@libp2p/interface-peer-store' -import { EventEmitter } from '@libp2p/interfaces/events' +import type { Connection } from '@libp2p/interface-connection' +import type { Validators } from '@libp2p/interface-dht' import type { Libp2pEvents } from '@libp2p/interface-libp2p' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerStore } from '@libp2p/interface-peer-store' +import type { Datastore } from 'interface-datastore' +import type { Duplex, Source } from 'it-stream-types' describe('rpc', () => { let peerId: PeerId @@ -98,7 +98,7 @@ describe('rpc', () => { const res = await pipe( source, (source) => lp.decode(source), - async (source) => await all(source) + async (source) => all(source) ) validateMessage(res) } diff --git a/test/utils/create-peer-id.ts b/test/utils/create-peer-id.ts index c4c3e69b9b..5295535fb2 100644 --- a/test/utils/create-peer-id.ts +++ b/test/utils/create-peer-id.ts @@ -1,12 +1,12 @@ -import type { Ed25519PeerId } from '@libp2p/interface-peer-id' import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import type { Ed25519PeerId } from '@libp2p/interface-peer-id' /** * Creates multiple PeerIds */ export async function createPeerIds (length: number): Promise { - return await Promise.all( - new Array(length).fill(0).map(async () => await createEd25519PeerId()) + return Promise.all( + new Array(length).fill(0).map(async () => createEd25519PeerId()) ) } diff --git a/test/utils/create-values.ts b/test/utils/create-values.ts index 8b2ac24565..b4e2e75611 100644 --- a/test/utils/create-values.ts +++ b/test/utils/create-values.ts @@ -1,8 +1,7 @@ +import { randomBytes } from '@libp2p/crypto' import { CID } from 'multiformats/cid' import * as raw from 'multiformats/codecs/raw' - import { sha256 } from 'multiformats/hashes/sha2' -import { randomBytes } from '@libp2p/crypto' export interface Value { cid: CID @@ -10,7 +9,7 @@ export interface Value { } export async function createValues (length: number): Promise { - return await Promise.all( + return Promise.all( Array.from({ length }).map(async () => { const bytes = randomBytes(32) const h = await sha256.digest(bytes) diff --git a/test/utils/sort-closest-peers.ts b/test/utils/sort-closest-peers.ts index ee0ad2a6b7..1fb81e5322 100644 --- a/test/utils/sort-closest-peers.ts +++ b/test/utils/sort-closest-peers.ts @@ -1,8 +1,8 @@ -import { xor as uint8ArrayXor } from 'uint8arrays/xor' -import { compare as uint8ArrayCompare } from 'uint8arrays/compare' -import { convertPeerId } from '../../src/utils.js' import all from 'it-all' import map from 'it-map' +import { compare as uint8ArrayCompare } from 'uint8arrays/compare' +import { xor as uint8ArrayXor } from 'uint8arrays/xor' +import { convertPeerId } from '../../src/utils.js' import type { PeerId } from '@libp2p/interface-peer-id' /** diff --git a/test/utils/test-dht.ts b/test/utils/test-dht.ts index c022b65972..1dcf9615fe 100644 --- a/test/utils/test-dht.ts +++ b/test/utils/test-dht.ts @@ -1,23 +1,23 @@ +import { mockRegistrar, mockConnectionManager, mockNetwork } from '@libp2p/interface-mocks' +import { EventEmitter } from '@libp2p/interfaces/events' +import { start } from '@libp2p/interfaces/startable' +import { logger } from '@libp2p/logger' import { PersistentPeerStore } from '@libp2p/peer-store' -import pRetry from 'p-retry' import { multiaddr } from '@multiformats/multiaddr' -import { createPeerId } from './create-peer-id.js' import { MemoryDatastore } from 'datastore-core/memory' -import { mockRegistrar, mockConnectionManager, mockNetwork } from '@libp2p/interface-mocks' -import type { Registrar } from '@libp2p/interface-registrar' -import { KadDHT } from '../../src/kad-dht.js' +import delay from 'delay' +import pRetry from 'p-retry' +import { stubInterface } from 'ts-sinon' import { DualKadDHT } from '../../src/dual-kad-dht.js' -import { logger } from '@libp2p/logger' +import { KadDHT } from '../../src/kad-dht.js' +import { createPeerId } from './create-peer-id.js' import type { KadDHTComponents, KadDHTInit } from '../../src/index.js' import type { AddressManager } from '@libp2p/interface-address-manager' -import { stubInterface } from 'ts-sinon' -import { start } from '@libp2p/interfaces/startable' -import delay from 'delay' import type { ConnectionManager } from '@libp2p/interface-connection-manager' -import type { PeerStore } from '@libp2p/interface-peer-store' -import type { PeerId } from '@libp2p/interface-peer-id' -import { EventEmitter } from '@libp2p/interfaces/events' import type { Libp2pEvents } from '@libp2p/interface-libp2p' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerStore } from '@libp2p/interface-peer-store' +import type { Registrar } from '@libp2p/interface-registrar' const log = logger('libp2p:kad-dht:test-dht') @@ -170,10 +170,10 @@ export class TestDHT { }) // Check routing tables - return await Promise.all( + return Promise.all( routingTableChecks .map( - async check => await pRetry(check, { retries: 50 }) + async check => pRetry(check, { retries: 50 }) ) ) }