diff --git a/packages/interface-compliance-tests/src/mocks/connection-manager.ts b/packages/interface-compliance-tests/src/mocks/connection-manager.ts index 4f5cecbac0..81904d407f 100644 --- a/packages/interface-compliance-tests/src/mocks/connection-manager.ts +++ b/packages/interface-compliance-tests/src/mocks/connection-manager.ts @@ -6,7 +6,7 @@ import { isMultiaddr, type Multiaddr } from '@multiformats/multiaddr' import { connectionPair } from './connection.js' import type { Libp2pEvents, PendingDial } from '@libp2p/interface' import type { Connection } from '@libp2p/interface/connection' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PubSub } from '@libp2p/interface/pubsub' import type { Startable } from '@libp2p/interface/startable' import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' @@ -16,7 +16,7 @@ export interface MockNetworkComponents { peerId: PeerId registrar: Registrar connectionManager: ConnectionManager - events: EventEmitter + events: TypedEventTarget pubsub?: PubSub } @@ -51,7 +51,7 @@ export const mockNetwork = new MockNetwork() export interface MockConnectionManagerComponents { peerId: PeerId registrar: Registrar - events: EventEmitter + events: TypedEventTarget } class MockConnectionManager implements ConnectionManager, Startable { diff --git a/packages/interface-compliance-tests/src/mocks/peer-discovery.ts b/packages/interface-compliance-tests/src/mocks/peer-discovery.ts index eab17bea6d..69bd2afc29 100644 --- a/packages/interface-compliance-tests/src/mocks/peer-discovery.ts +++ b/packages/interface-compliance-tests/src/mocks/peer-discovery.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { peerDiscovery } from '@libp2p/interface/peer-discovery' import * as PeerIdFactory from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' @@ -12,7 +12,7 @@ interface MockDiscoveryInit { /** * Emits 'peer' events on discovery. */ -export class MockDiscovery extends EventEmitter implements PeerDiscovery { +export class MockDiscovery extends TypedEventEmitter implements PeerDiscovery { public readonly options: MockDiscoveryInit private _isRunning: boolean private _timer: any diff --git a/packages/interface-compliance-tests/src/mocks/upgrader.ts b/packages/interface-compliance-tests/src/mocks/upgrader.ts index b4ef88002d..4f69268071 100644 --- a/packages/interface-compliance-tests/src/mocks/upgrader.ts +++ b/packages/interface-compliance-tests/src/mocks/upgrader.ts @@ -1,18 +1,18 @@ import { mockConnection } from './connection.js' import type { Libp2pEvents } from '@libp2p/interface' import type { Connection, MultiaddrConnection } from '@libp2p/interface/connection' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { Upgrader, UpgraderOptions } from '@libp2p/interface/transport' import type { Registrar } from '@libp2p/interface-internal/registrar' export interface MockUpgraderInit { registrar?: Registrar - events?: EventEmitter + events?: TypedEventTarget } class MockUpgrader implements Upgrader { private readonly registrar?: Registrar - private readonly events?: EventEmitter + private readonly events?: TypedEventTarget constructor (init: MockUpgraderInit) { this.registrar = init.registrar diff --git a/packages/interface-compliance-tests/src/pubsub/utils.ts b/packages/interface-compliance-tests/src/pubsub/utils.ts index 79bf894479..fdd7c2552c 100644 --- a/packages/interface-compliance-tests/src/pubsub/utils.ts +++ b/packages/interface-compliance-tests/src/pubsub/utils.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { pEvent } from 'p-event' import pWaitFor from 'p-wait-for' @@ -19,7 +19,7 @@ export async function createComponents (): Promise { const components: any = { peerId: await createEd25519PeerId(), registrar: mockRegistrar(), - events: new EventEmitter() + events: new TypedEventEmitter() } components.connectionManager = mockConnectionManager(components) diff --git a/packages/interface-compliance-tests/src/transport/dial-test.ts b/packages/interface-compliance-tests/src/transport/dial-test.ts index 2139a755d0..98035d9aa5 100644 --- a/packages/interface-compliance-tests/src/transport/dial-test.ts +++ b/packages/interface-compliance-tests/src/transport/dial-test.ts @@ -1,5 +1,5 @@ import { AbortError } from '@libp2p/interface/errors' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { expect } from 'aegir/chai' import all from 'it-all' import drain from 'it-drain' @@ -27,7 +27,7 @@ export default (common: TestSetup): void => { registrar = mockRegistrar() upgrader = mockUpgrader({ registrar, - events: new EventEmitter() + events: new TypedEventEmitter() }); ({ addrs, transport, connector } = await common.setup()) diff --git a/packages/interface-compliance-tests/src/transport/listen-test.ts b/packages/interface-compliance-tests/src/transport/listen-test.ts index d518511dc7..d9f3a17c22 100644 --- a/packages/interface-compliance-tests/src/transport/listen-test.ts +++ b/packages/interface-compliance-tests/src/transport/listen-test.ts @@ -1,5 +1,5 @@ /* eslint max-nested-callbacks: ["error", 8] */ -import { CustomEvent, EventEmitter } from '@libp2p/interface/events' +import { CustomEvent, TypedEventEmitter } from '@libp2p/interface/events' import { expect } from 'aegir/chai' import drain from 'it-drain' import { pipe } from 'it-pipe' @@ -27,7 +27,7 @@ export default (common: TestSetup): void => { registrar = mockRegistrar() upgrader = mockUpgrader({ registrar, - events: new EventEmitter() + events: new TypedEventEmitter() }); ({ transport, addrs } = await common.setup()) diff --git a/packages/interface/src/events.ts b/packages/interface/src/events.ts index 4fff7e8f91..2f4e863b0e 100644 --- a/packages/interface/src/events.ts +++ b/packages/interface/src/events.ts @@ -15,7 +15,23 @@ interface Listener { * https://github.com/microsoft/TypeScript/issues/299 * etc */ -export class EventEmitter> extends EventTarget { +export interface TypedEventTarget > extends EventTarget { + addEventListener(type: K, listener: EventHandler | null, options?: boolean | AddEventListenerOptions): void + + listenerCount (type: string): number + + removeEventListener(type: K, listener?: EventHandler | null, options?: boolean | EventListenerOptions): void + + removeEventListener (type: string, listener?: EventHandler, options?: boolean | EventListenerOptions): void + + safeDispatchEvent(type: keyof EventMap, detail: CustomEventInit): boolean +} + +/** + * An implementation of a typed event target + * etc + */ +export class TypedEventEmitter> extends EventTarget implements TypedEventTarget { #listeners = new Map() listenerCount (type: string): number { @@ -98,3 +114,6 @@ class CustomEventPolyfill extends Event { } export const CustomEvent = globalThis.CustomEvent ?? CustomEventPolyfill + +// TODO: remove this in v1 +export { TypedEventEmitter as EventEmitter } diff --git a/packages/interface/src/index.ts b/packages/interface/src/index.ts index 0dd02078f8..d50e335d69 100644 --- a/packages/interface/src/index.ts +++ b/packages/interface/src/index.ts @@ -16,7 +16,7 @@ import type { Connection, NewStreamOptions, Stream } from './connection/index.js' import type { ContentRouting } from './content-routing/index.js' -import type { EventEmitter } from './events.js' +import type { TypedEventTarget } from './events.js' import type { KeyChain } from './keychain/index.js' import type { Metrics } from './metrics/index.js' import type { PeerId } from './peer-id/index.js' @@ -303,7 +303,7 @@ export interface PendingDial { /** * Libp2p nodes implement this interface. */ -export interface Libp2p extends Startable, EventEmitter> { +export interface Libp2p extends Startable, TypedEventTarget> { /** * The PeerId is a unique identifier for a node on the network. * diff --git a/packages/interface/src/peer-discovery/index.ts b/packages/interface/src/peer-discovery/index.ts index 4b8aa9c258..55b1227eb1 100644 --- a/packages/interface/src/peer-discovery/index.ts +++ b/packages/interface/src/peer-discovery/index.ts @@ -1,4 +1,4 @@ -import type { EventEmitter } from '../events.js' +import type { TypedEventTarget } from '../events.js' import type { PeerInfo } from '../peer-info/index.js' /** @@ -26,4 +26,4 @@ export interface PeerDiscoveryEvents { 'peer': CustomEvent } -export interface PeerDiscovery extends EventEmitter {} +export interface PeerDiscovery extends TypedEventTarget {} diff --git a/packages/interface/src/pubsub/index.ts b/packages/interface/src/pubsub/index.ts index 6fd94382a1..1ce9464b6b 100644 --- a/packages/interface/src/pubsub/index.ts +++ b/packages/interface/src/pubsub/index.ts @@ -1,5 +1,5 @@ import type { Stream } from '../connection/index.js' -import type { EventEmitter } from '../events.js' +import type { TypedEventTarget } from '../events.js' import type { PeerId } from '../peer-id/index.js' import type { Pushable } from 'it-pushable' import type { Uint8ArrayList } from 'uint8arraylist' @@ -65,7 +65,7 @@ export interface PubSubRPC { messages: PubSubRPCMessage[] } -export interface PeerStreams extends EventEmitter { +export interface PeerStreams extends TypedEventTarget { id: PeerId protocol: string outboundStream?: Pushable @@ -152,7 +152,7 @@ export interface TopicValidatorFn { (peer: PeerId, message: Message): TopicValidatorResult | Promise } -export interface PubSub = PubSubEvents> extends EventEmitter { +export interface PubSub = PubSubEvents> extends TypedEventTarget { /** * The global signature policy controls whether or not we sill send and receive * signed or unsigned messages. diff --git a/packages/interface/src/transport/index.ts b/packages/interface/src/transport/index.ts index 5acbfcc50e..22ab1a803b 100644 --- a/packages/interface/src/transport/index.ts +++ b/packages/interface/src/transport/index.ts @@ -1,5 +1,5 @@ import type { Connection, MultiaddrConnection } from '../connection/index.js' -import type { EventEmitter } from '../events.js' +import type { TypedEventTarget } from '../events.js' import type { AbortOptions } from '../index.js' import type { StreamMuxerFactory } from '../stream-muxer/index.js' import type { Multiaddr } from '@multiformats/multiaddr' @@ -11,7 +11,7 @@ export interface ListenerEvents { 'close': CustomEvent } -export interface Listener extends EventEmitter { +export interface Listener extends TypedEventTarget { /** * Start a listener */ diff --git a/packages/kad-dht/src/dual-kad-dht.ts b/packages/kad-dht/src/dual-kad-dht.ts index 6d704725fb..ef06f739ea 100644 --- a/packages/kad-dht/src/dual-kad-dht.ts +++ b/packages/kad-dht/src/dual-kad-dht.ts @@ -1,6 +1,6 @@ import { type ContentRouting, contentRouting } from '@libp2p/interface/content-routing' import { CodeError } from '@libp2p/interface/errors' -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { type PeerDiscovery, peerDiscovery, type PeerDiscoveryEvents } from '@libp2p/interface/peer-discovery' import { type PeerRouting, peerRouting } from '@libp2p/interface/peer-routing' import { logger } from '@libp2p/logger' @@ -121,7 +121,7 @@ function multiaddrIsPublic (multiaddr: Multiaddr): boolean { * A DHT implementation modelled after Kademlia with S/Kademlia modifications. * Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht. */ -export class DefaultDualKadDHT extends EventEmitter implements DualKadDHT, PeerDiscovery { +export class DefaultDualKadDHT extends TypedEventEmitter implements DualKadDHT, PeerDiscovery { public readonly wan: DefaultKadDHT public readonly lan: DefaultKadDHT public readonly components: KadDHTComponents diff --git a/packages/kad-dht/src/index.ts b/packages/kad-dht/src/index.ts index edb1e4f679..679a7ba62f 100644 --- a/packages/kad-dht/src/index.ts +++ b/packages/kad-dht/src/index.ts @@ -1,7 +1,7 @@ import { DefaultDualKadDHT } from './dual-kad-dht.js' import type { ProvidersInit } from './providers.js' import type { Libp2pEvents, AbortOptions } from '@libp2p/interface' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { Metrics } from '@libp2p/interface/metrics' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerInfo } from '@libp2p/interface/peer-info' @@ -314,7 +314,7 @@ export interface KadDHTComponents { metrics?: Metrics connectionManager: ConnectionManager datastore: Datastore - events: EventEmitter + events: TypedEventTarget } export function kadDHT (init?: KadDHTInit): (components: KadDHTComponents) => DualKadDHT { diff --git a/packages/kad-dht/src/kad-dht.ts b/packages/kad-dht/src/kad-dht.ts index f9f260452c..fb5be07aa9 100644 --- a/packages/kad-dht/src/kad-dht.ts +++ b/packages/kad-dht/src/kad-dht.ts @@ -1,4 +1,4 @@ -import { CustomEvent, EventEmitter } from '@libp2p/interface/events' +import { CustomEvent, TypedEventEmitter } from '@libp2p/interface/events' import { type Logger, logger } from '@libp2p/logger' import pDefer from 'p-defer' import { PROTOCOL_DHT, PROTOCOL_PREFIX, LAN_PREFIX } from './constants.js' @@ -39,7 +39,7 @@ export interface SingleKadDHTInit extends KadDHTInit { * A DHT implementation modelled after Kademlia with S/Kademlia modifications. * Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht. */ -export class DefaultKadDHT extends EventEmitter implements KadDHT { +export class DefaultKadDHT extends TypedEventEmitter implements KadDHT { public protocol: string public routingTable: RoutingTable public providers: Providers diff --git a/packages/kad-dht/src/network.ts b/packages/kad-dht/src/network.ts index b10ad6e592..c968b10f9b 100644 --- a/packages/kad-dht/src/network.ts +++ b/packages/kad-dht/src/network.ts @@ -1,5 +1,5 @@ import { CodeError } from '@libp2p/interface/errors' -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { abortableDuplex } from 'abortable-iterator' import drain from 'it-drain' @@ -35,7 +35,7 @@ interface NetworkEvents { /** * Handle network operations for the dht */ -export class Network extends EventEmitter implements Startable { +export class Network extends TypedEventEmitter implements Startable { private readonly log: Logger private readonly protocol: string private running: boolean diff --git a/packages/kad-dht/src/query/manager.ts b/packages/kad-dht/src/query/manager.ts index df830faf2d..675c9a9f81 100644 --- a/packages/kad-dht/src/query/manager.ts +++ b/packages/kad-dht/src/query/manager.ts @@ -1,6 +1,6 @@ import { setMaxListeners } from 'events' import { AbortError } from '@libp2p/interface/errors' -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { PeerSet } from '@libp2p/peer-collections' import { anySignal } from 'any-signal' @@ -143,7 +143,7 @@ export class QueryManager implements Startable { // query a subset of peers up to `kBucketSize / 2` in length const startTime = Date.now() - const cleanUp = new EventEmitter() + const cleanUp = new TypedEventEmitter() try { if (options.isSelfQuery !== true && this.initialQuerySelfHasRun != null) { diff --git a/packages/kad-dht/src/query/query-path.ts b/packages/kad-dht/src/query/query-path.ts index 991f1abd42..382585ae26 100644 --- a/packages/kad-dht/src/query/query-path.ts +++ b/packages/kad-dht/src/query/query-path.ts @@ -9,7 +9,7 @@ import { queryErrorEvent } from './events.js' import type { CleanUpEvents } from './manager.js' import type { QueryEvent, QueryOptions } from '../index.js' import type { QueryFunc } from '../query/types.js' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { Logger } from '@libp2p/logger' import type { PeerSet } from '@libp2p/peer-collections' @@ -60,7 +60,7 @@ export interface QueryPathOptions extends QueryOptions { /** * will emit a 'cleanup' event if the caller exits the for..await of early */ - cleanUp: EventEmitter + cleanUp: TypedEventTarget /** * A timeout for queryFunc in ms @@ -185,7 +185,7 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator, log: Logger): AsyncGenerator { +async function * toGenerator (queue: Queue, signal: AbortSignal, cleanUp: TypedEventTarget, log: Logger): AsyncGenerator { let deferred = defer() let running = true const results: QueryEvent[] = [] diff --git a/packages/kad-dht/src/routing-table/index.ts b/packages/kad-dht/src/routing-table/index.ts index b0bfbb90ff..ee7b24bcf8 100644 --- a/packages/kad-dht/src/routing-table/index.ts +++ b/packages/kad-dht/src/routing-table/index.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { PeerSet } from '@libp2p/peer-collections' import Queue from 'p-queue' @@ -43,7 +43,7 @@ export interface RoutingTableEvents { * A wrapper around `k-bucket`, to provide easy store and * retrieval for peers. */ -export class RoutingTable extends EventEmitter implements Startable { +export class RoutingTable extends TypedEventEmitter implements Startable { public kBucketSize: number public kb?: KBucket public pingQueue: Queue diff --git a/packages/kad-dht/src/routing-table/k-bucket.ts b/packages/kad-dht/src/routing-table/k-bucket.ts index b8e510d0a2..57585926e9 100644 --- a/packages/kad-dht/src/routing-table/k-bucket.ts +++ b/packages/kad-dht/src/routing-table/k-bucket.ts @@ -27,7 +27,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' function arrayEquals (array1: Uint8Array, array2: Uint8Array): boolean { @@ -122,10 +122,8 @@ export interface Bucket { /** * Implementation of a Kademlia DHT k-bucket used for storing * contact (peer node) information. - * - * @extends EventEmitter */ -export class KBucket extends EventEmitter { +export class KBucket extends TypedEventEmitter { public localNodeId: Uint8Array public root: Bucket private readonly numberOfNodesPerKBucket: number diff --git a/packages/kad-dht/src/topology-listener.ts b/packages/kad-dht/src/topology-listener.ts index 462b633a3c..8af9a210a4 100644 --- a/packages/kad-dht/src/topology-listener.ts +++ b/packages/kad-dht/src/topology-listener.ts @@ -1,4 +1,4 @@ -import { CustomEvent, EventEmitter } from '@libp2p/interface/events' +import { CustomEvent, TypedEventEmitter } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import type { KadDHTComponents } from '.' import type { PeerId } from '@libp2p/interface/peer-id' @@ -17,7 +17,7 @@ export interface TopologyListenerEvents { /** * Receives notifications of new peers joining the network that support the DHT protocol */ -export class TopologyListener extends EventEmitter implements Startable { +export class TopologyListener extends TypedEventEmitter implements Startable { private readonly log: Logger private readonly components: KadDHTComponents private readonly protocol: string diff --git a/packages/kad-dht/test/routing-table.spec.ts b/packages/kad-dht/test/routing-table.spec.ts index 669115c349..35381a044f 100644 --- a/packages/kad-dht/test/routing-table.spec.ts +++ b/packages/kad-dht/test/routing-table.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { mockConnectionManager } from '@libp2p/interface-compliance-tests/mocks' import { PeerSet } from '@libp2p/peer-collections' import { peerIdFromString } from '@libp2p/peer-id' @@ -33,7 +33,7 @@ describe('Routing Table', () => { beforeEach(async function () { this.timeout(20 * 1000) - const events = new EventEmitter() + const events = new TypedEventEmitter() components = { peerId: await createPeerId(), diff --git a/packages/kad-dht/test/rpc/handlers/get-providers.spec.ts b/packages/kad-dht/test/rpc/handlers/get-providers.spec.ts index 20cab3c7e7..18a3706f64 100644 --- a/packages/kad-dht/test/rpc/handlers/get-providers.spec.ts +++ b/packages/kad-dht/test/rpc/handlers/get-providers.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { PersistentPeerStore } from '@libp2p/peer-store' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -43,7 +43,7 @@ describe('rpc - handlers - GetProviders', () => { peerStore = new PersistentPeerStore({ peerId, datastore: new MemoryDatastore(), - events: new EventEmitter() + events: new TypedEventEmitter() }) const components: GetProvidersHandlerComponents = { diff --git a/packages/kad-dht/test/rpc/handlers/get-value.spec.ts b/packages/kad-dht/test/rpc/handlers/get-value.spec.ts index 32cf1c9bba..ad79dda7c6 100644 --- a/packages/kad-dht/test/rpc/handlers/get-value.spec.ts +++ b/packages/kad-dht/test/rpc/handlers/get-value.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { PersistentPeerStore } from '@libp2p/peer-store' import { expect } from 'aegir/chai' import { MemoryDatastore } from 'datastore-core' @@ -40,7 +40,7 @@ describe('rpc - handlers - GetValue', () => { peerStore = new PersistentPeerStore({ peerId, datastore, - events: new EventEmitter() + events: new TypedEventEmitter() }) const components: GetValueHandlerComponents = { diff --git a/packages/kad-dht/test/rpc/index.node.ts b/packages/kad-dht/test/rpc/index.node.ts index da58989b23..25ced21d11 100644 --- a/packages/kad-dht/test/rpc/index.node.ts +++ b/packages/kad-dht/test/rpc/index.node.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start } from '@libp2p/interface/startable' import { mockStream } from '@libp2p/interface-compliance-tests/mocks' import { PersistentPeerStore } from '@libp2p/peer-store' @@ -51,7 +51,7 @@ describe('rpc', () => { } components.peerStore = new PersistentPeerStore({ ...components, - events: new EventEmitter() + events: new TypedEventEmitter() }) await start(...Object.values(components)) diff --git a/packages/kad-dht/test/utils/test-dht.ts b/packages/kad-dht/test/utils/test-dht.ts index 04be4041be..bfddebdaad 100644 --- a/packages/kad-dht/test/utils/test-dht.ts +++ b/packages/kad-dht/test/utils/test-dht.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockRegistrar, mockConnectionManager, mockNetwork } from '@libp2p/interface-compliance-tests/mocks' import { logger } from '@libp2p/logger' @@ -29,7 +29,7 @@ export class TestDHT { } async spawn (options: Partial = {}, autoStart = true): Promise { - const events = new EventEmitter() + const events = new TypedEventEmitter() const components: KadDHTComponents = { peerId: await createPeerId(), datastore: new MemoryDatastore(), diff --git a/packages/libp2p/src/address-manager/index.ts b/packages/libp2p/src/address-manager/index.ts index 44a8504f51..f6bafad41f 100644 --- a/packages/libp2p/src/address-manager/index.ts +++ b/packages/libp2p/src/address-manager/index.ts @@ -3,7 +3,7 @@ import { peerIdFromString } from '@libp2p/peer-id' import { multiaddr } from '@multiformats/multiaddr' import { debounce } from './utils.js' import type { Libp2pEvents } from '@libp2p/interface' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerStore } from '@libp2p/interface/peer-store' import type { TransportManager } from '@libp2p/interface-internal/transport-manager' @@ -38,7 +38,7 @@ export interface DefaultAddressManagerComponents { peerId: PeerId transportManager: TransportManager peerStore: PeerStore - events: EventEmitter + events: TypedEventTarget } /** diff --git a/packages/libp2p/src/circuit-relay/index.ts b/packages/libp2p/src/circuit-relay/index.ts index 91ab9007e4..5e8757fb10 100644 --- a/packages/libp2p/src/circuit-relay/index.ts +++ b/packages/libp2p/src/circuit-relay/index.ts @@ -35,7 +35,7 @@ */ import type { Limit } from './pb/index.js' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventEmitter } from '@libp2p/interface/events' import type { PeerMap } from '@libp2p/peer-collections' import type { Multiaddr } from '@multiformats/multiaddr' @@ -51,7 +51,7 @@ export interface CircuitRelayServiceEvents { 'relay:advert:error': CustomEvent } -export interface CircuitRelayService extends EventEmitter { +export interface CircuitRelayService extends TypedEventEmitter { reservations: PeerMap } diff --git a/packages/libp2p/src/circuit-relay/server/advert-service.ts b/packages/libp2p/src/circuit-relay/server/advert-service.ts index 5d2c61990f..4e84f41c2c 100644 --- a/packages/libp2p/src/circuit-relay/server/advert-service.ts +++ b/packages/libp2p/src/circuit-relay/server/advert-service.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import pRetry from 'p-retry' import { codes } from '../../errors.js' @@ -31,7 +31,7 @@ export interface AdvertServiceEvents { 'advert:error': CustomEvent } -export class AdvertService extends EventEmitter implements Startable { +export class AdvertService extends TypedEventEmitter implements Startable { private readonly contentRouting: ContentRouting private timeout?: any private started: boolean diff --git a/packages/libp2p/src/circuit-relay/server/index.ts b/packages/libp2p/src/circuit-relay/server/index.ts index aed3d5be8b..8fce918570 100644 --- a/packages/libp2p/src/circuit-relay/server/index.ts +++ b/packages/libp2p/src/circuit-relay/server/index.ts @@ -1,5 +1,5 @@ import { setMaxListeners } from 'events' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { peerIdFromBytes } from '@libp2p/peer-id' import { RecordEnvelope } from '@libp2p/peer-record' @@ -99,7 +99,7 @@ const defaults = { maxOutboundStopStreams: MAX_CONNECTIONS } -class CircuitRelayServer extends EventEmitter implements Startable, CircuitRelayService { +class CircuitRelayServer extends TypedEventEmitter implements Startable, CircuitRelayService { private readonly registrar: Registrar private readonly peerStore: PeerStore private readonly addressManager: AddressManager diff --git a/packages/libp2p/src/circuit-relay/transport/discovery.ts b/packages/libp2p/src/circuit-relay/transport/discovery.ts index 58ddc80cd9..77a4af1208 100644 --- a/packages/libp2p/src/circuit-relay/transport/discovery.ts +++ b/packages/libp2p/src/circuit-relay/transport/discovery.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { RELAY_RENDEZVOUS_NS, @@ -32,7 +32,7 @@ export interface RelayDiscoveryComponents { * ReservationManager automatically makes a circuit v2 reservation on any connected * peers that support the circuit v2 HOP protocol. */ -export class RelayDiscovery extends EventEmitter implements Startable { +export class RelayDiscovery extends TypedEventEmitter implements Startable { private readonly peerId: PeerId private readonly peerStore: PeerStore private readonly contentRouting: ContentRouting diff --git a/packages/libp2p/src/circuit-relay/transport/index.ts b/packages/libp2p/src/circuit-relay/transport/index.ts index 483f9a0366..022b44c57e 100644 --- a/packages/libp2p/src/circuit-relay/transport/index.ts +++ b/packages/libp2p/src/circuit-relay/transport/index.ts @@ -17,7 +17,7 @@ import type { Libp2pEvents, AbortOptions } from '@libp2p/interface' import type { Connection, Stream } from '@libp2p/interface/connection' import type { ConnectionGater } from '@libp2p/interface/connection-gater' import type { ContentRouting } from '@libp2p/interface/content-routing' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerStore } from '@libp2p/interface/peer-store' import type { AddressManager } from '@libp2p/interface-internal/address-manager' @@ -50,7 +50,7 @@ export interface CircuitRelayTransportComponents extends RelayDiscoveryComponent addressManager: AddressManager contentRouting: ContentRouting connectionGater: ConnectionGater - events: EventEmitter + events: TypedEventTarget } interface ConnectOptions { diff --git a/packages/libp2p/src/circuit-relay/transport/listener.ts b/packages/libp2p/src/circuit-relay/transport/listener.ts index bb67007db9..29b6c6e608 100644 --- a/packages/libp2p/src/circuit-relay/transport/listener.ts +++ b/packages/libp2p/src/circuit-relay/transport/listener.ts @@ -1,5 +1,5 @@ import { CodeError } from '@libp2p/interface/errors' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { PeerMap } from '@libp2p/peer-collections' import { peerIdFromString } from '@libp2p/peer-id' @@ -18,7 +18,7 @@ export interface CircuitRelayTransportListenerComponents { relayStore: ReservationStore } -class CircuitRelayTransportListener extends EventEmitter implements Listener { +class CircuitRelayTransportListener extends TypedEventEmitter implements Listener { private readonly connectionManager: ConnectionManager private readonly relayStore: ReservationStore private readonly listeningAddrs: PeerMap diff --git a/packages/libp2p/src/circuit-relay/transport/reservation-store.ts b/packages/libp2p/src/circuit-relay/transport/reservation-store.ts index 2edcd4f998..9300357240 100644 --- a/packages/libp2p/src/circuit-relay/transport/reservation-store.ts +++ b/packages/libp2p/src/circuit-relay/transport/reservation-store.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { PeerMap } from '@libp2p/peer-collections' import { multiaddr } from '@multiformats/multiaddr' @@ -32,7 +32,7 @@ export interface RelayStoreComponents { connectionManager: ConnectionManager transportManager: TransportManager peerStore: PeerStore - events: EventEmitter + events: TypedEventTarget } export interface RelayStoreInit { @@ -76,12 +76,12 @@ export interface ReservationStoreEvents { 'relay:removed': CustomEvent } -export class ReservationStore extends EventEmitter implements Startable { +export class ReservationStore extends TypedEventEmitter implements Startable { private readonly peerId: PeerId private readonly connectionManager: ConnectionManager private readonly transportManager: TransportManager private readonly peerStore: PeerStore - private readonly events: EventEmitter + private readonly events: TypedEventTarget private readonly reserveQueue: PeerJobQueue private readonly reservations: PeerMap private readonly maxDiscoveredRelays: number diff --git a/packages/libp2p/src/components.ts b/packages/libp2p/src/components.ts index f11fb2cfb6..2d65d956dc 100644 --- a/packages/libp2p/src/components.ts +++ b/packages/libp2p/src/components.ts @@ -4,7 +4,7 @@ import type { Libp2pEvents } from '@libp2p/interface' import type { ConnectionProtector } from '@libp2p/interface/connection' import type { ConnectionGater } from '@libp2p/interface/connection-gater' import type { ContentRouting } from '@libp2p/interface/content-routing' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { Metrics } from '@libp2p/interface/metrics' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerRouting } from '@libp2p/interface/peer-routing' @@ -18,7 +18,7 @@ import type { Datastore } from 'interface-datastore' export interface Components extends Record, Startable { peerId: PeerId - events: EventEmitter + events: TypedEventTarget addressManager: AddressManager peerStore: PeerStore upgrader: Upgrader @@ -35,7 +35,7 @@ export interface Components extends Record, Startable { export interface ComponentsInit { peerId?: PeerId - events?: EventEmitter + events?: TypedEventTarget addressManager?: AddressManager peerStore?: PeerStore upgrader?: Upgrader diff --git a/packages/libp2p/src/connection-manager/auto-dial.ts b/packages/libp2p/src/connection-manager/auto-dial.ts index 6d27e6f42b..a110adfd46 100644 --- a/packages/libp2p/src/connection-manager/auto-dial.ts +++ b/packages/libp2p/src/connection-manager/auto-dial.ts @@ -4,7 +4,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { PeerJobQueue } from '../utils/peer-job-queue.js' import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PEER_RETRY_THRESHOLD, AUTO_DIAL_PRIORITY, LAST_DIAL_FAILURE_KEY, MIN_CONNECTIONS } from './constants.js' import type { Libp2pEvents } from '@libp2p/interface' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerStore } from '@libp2p/interface/peer-store' import type { Startable } from '@libp2p/interface/startable' import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' @@ -24,7 +24,7 @@ interface AutoDialInit { interface AutoDialComponents { connectionManager: ConnectionManager peerStore: PeerStore - events: EventEmitter + events: TypedEventTarget } const defaultOptions = { diff --git a/packages/libp2p/src/connection-manager/connection-pruner.ts b/packages/libp2p/src/connection-manager/connection-pruner.ts index 04fb85d7fb..adbed0a56c 100644 --- a/packages/libp2p/src/connection-manager/connection-pruner.ts +++ b/packages/libp2p/src/connection-manager/connection-pruner.ts @@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger' import { PeerMap } from '@libp2p/peer-collections' import { MAX_CONNECTIONS } from './constants.js' import type { Libp2pEvents } from '@libp2p/interface' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerStore } from '@libp2p/interface/peer-store' import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' import type { Multiaddr } from '@multiformats/multiaddr' @@ -17,7 +17,7 @@ interface ConnectionPrunerInit { interface ConnectionPrunerComponents { connectionManager: ConnectionManager peerStore: PeerStore - events: EventEmitter + events: TypedEventTarget } const defaultOptions = { @@ -33,7 +33,7 @@ export class ConnectionPruner { private readonly connectionManager: ConnectionManager private readonly peerStore: PeerStore private readonly allow: Multiaddr[] - private readonly events: EventEmitter + private readonly events: TypedEventTarget constructor (components: ConnectionPrunerComponents, init: ConnectionPrunerInit = {}) { this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections diff --git a/packages/libp2p/src/connection-manager/index.ts b/packages/libp2p/src/connection-manager/index.ts index d94612fd9f..c13e49a53a 100644 --- a/packages/libp2p/src/connection-manager/index.ts +++ b/packages/libp2p/src/connection-manager/index.ts @@ -15,7 +15,7 @@ import { DialQueue } from './dial-queue.js' import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions } from '@libp2p/interface' import type { Connection, MultiaddrConnection } from '@libp2p/interface/connection' import type { ConnectionGater } from '@libp2p/interface/connection-gater' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { Metrics } from '@libp2p/interface/metrics' import type { PeerId } from '@libp2p/interface/peer-id' import type { Peer, PeerStore } from '@libp2p/interface/peer-store' @@ -165,7 +165,7 @@ export interface DefaultConnectionManagerComponents { peerStore: PeerStore transportManager: TransportManager connectionGater: ConnectionGater - events: EventEmitter + events: TypedEventTarget } /** @@ -187,7 +187,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable { private readonly peerStore: PeerStore private readonly metrics?: Metrics - private readonly events: EventEmitter + private readonly events: TypedEventTarget constructor (components: DefaultConnectionManagerComponents, init: ConnectionManagerInit = {}) { this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections diff --git a/packages/libp2p/src/identify/identify.ts b/packages/libp2p/src/identify/identify.ts index caa5248b45..940533ef50 100644 --- a/packages/libp2p/src/identify/identify.ts +++ b/packages/libp2p/src/identify/identify.ts @@ -21,7 +21,7 @@ import { Identify } from './pb/message.js' import type { IdentifyService, IdentifyServiceComponents, IdentifyServiceInit } from './index.js' import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions } from '@libp2p/interface' import type { Connection, Stream } from '@libp2p/interface/connection' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { Peer, PeerStore } from '@libp2p/interface/peer-store' import type { Startable } from '@libp2p/interface/startable' @@ -70,7 +70,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService { private readonly maxPushOutgoingStreams: number private readonly maxIdentifyMessageSize: number private readonly maxObservedAddresses: number - private readonly events: EventEmitter + private readonly events: TypedEventTarget private readonly runOnTransientConnection: boolean constructor (components: IdentifyServiceComponents, init: IdentifyServiceInit) { diff --git a/packages/libp2p/src/identify/index.ts b/packages/libp2p/src/identify/index.ts index c74238e832..61dc2e3523 100644 --- a/packages/libp2p/src/identify/index.ts +++ b/packages/libp2p/src/identify/index.ts @@ -5,7 +5,7 @@ import { import { DefaultIdentifyService } from './identify.js' import { Identify } from './pb/message.js' import type { AbortOptions, IdentifyResult, Libp2pEvents } from '@libp2p/interface' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerStore } from '@libp2p/interface/peer-store' import type { Connection } from '@libp2p/interface/src/connection/index.js' @@ -58,7 +58,7 @@ export interface IdentifyServiceComponents { connectionManager: ConnectionManager registrar: Registrar addressManager: AddressManager - events: EventEmitter + events: TypedEventTarget } /** diff --git a/packages/libp2p/src/libp2p.ts b/packages/libp2p/src/libp2p.ts index 29c80f0cc7..cdc0c5caa8 100644 --- a/packages/libp2p/src/libp2p.ts +++ b/packages/libp2p/src/libp2p.ts @@ -2,7 +2,7 @@ import { setMaxListeners } from 'events' import { unmarshalPublicKey } from '@libp2p/crypto/keys' import { type ContentRouting, contentRouting } from '@libp2p/interface/content-routing' import { CodeError } from '@libp2p/interface/errors' -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { peerDiscovery } from '@libp2p/interface/peer-discovery' import { type PeerRouting, peerRouting } from '@libp2p/interface/peer-routing' import { DefaultKeyChain } from '@libp2p/keychain' @@ -42,7 +42,7 @@ import type { Datastore } from 'interface-datastore' const log = logger('libp2p') -export class Libp2pNode> extends EventEmitter implements Libp2p { +export class Libp2pNode> extends TypedEventEmitter implements Libp2p { public peerId: PeerId public peerStore: PeerStore public contentRouting: ContentRouting @@ -59,7 +59,7 @@ export class Libp2pNode> extends // event bus - components can listen to this emitter to be notified of system events // and also cause them to be emitted - const events = new EventEmitter() + const events = new TypedEventEmitter() const originalDispatch = events.dispatchEvent.bind(events) events.dispatchEvent = (evt: any) => { const internalResult = originalDispatch(evt) diff --git a/packages/libp2p/src/registrar.ts b/packages/libp2p/src/registrar.ts index c679888f4f..268a35c364 100644 --- a/packages/libp2p/src/registrar.ts +++ b/packages/libp2p/src/registrar.ts @@ -3,7 +3,7 @@ import { logger } from '@libp2p/logger' import merge from 'merge-options' import { codes } from './errors.js' import type { Libp2pEvents, PeerUpdate } from '@libp2p/interface' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerStore } from '@libp2p/interface/peer-store' import type { Topology } from '@libp2p/interface/topology' @@ -19,7 +19,7 @@ export interface RegistrarComponents { peerId: PeerId connectionManager: ConnectionManager peerStore: PeerStore - events: EventEmitter + events: TypedEventTarget } /** diff --git a/packages/libp2p/src/transport-manager.ts b/packages/libp2p/src/transport-manager.ts index 38f5359616..f18ecdcecc 100644 --- a/packages/libp2p/src/transport-manager.ts +++ b/packages/libp2p/src/transport-manager.ts @@ -5,7 +5,7 @@ import { logger } from '@libp2p/logger' import { codes } from './errors.js' import type { Libp2pEvents, AbortOptions } from '@libp2p/interface' import type { Connection } from '@libp2p/interface/connection' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { Metrics } from '@libp2p/interface/metrics' import type { Startable } from '@libp2p/interface/startable' import type { Listener, Transport, Upgrader } from '@libp2p/interface/transport' @@ -23,7 +23,7 @@ export interface DefaultTransportManagerComponents { metrics?: Metrics addressManager: AddressManager upgrader: Upgrader - events: EventEmitter + events: TypedEventTarget } export class DefaultTransportManager implements TransportManager, Startable { diff --git a/packages/libp2p/src/upgrader.ts b/packages/libp2p/src/upgrader.ts index 18934e3ad0..ad27ba02db 100644 --- a/packages/libp2p/src/upgrader.ts +++ b/packages/libp2p/src/upgrader.ts @@ -11,7 +11,7 @@ import type { Libp2pEvents, AbortOptions } from '@libp2p/interface' import type { MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions } from '@libp2p/interface/connection' import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface/connection-encrypter' import type { ConnectionGater } from '@libp2p/interface/connection-gater' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { Metrics } from '@libp2p/interface/metrics' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerStore } from '@libp2p/interface/peer-store' @@ -104,7 +104,7 @@ export interface DefaultUpgraderComponents { connectionProtector?: ConnectionProtector registrar: Registrar peerStore: PeerStore - events: EventEmitter + events: TypedEventTarget } type EncryptedConn = Duplex, Source, Promise> @@ -116,7 +116,7 @@ export class DefaultUpgrader implements Upgrader { private readonly connectionEncryption: Map private readonly muxers: Map private readonly inboundUpgradeTimeout: number - private readonly events: EventEmitter + private readonly events: TypedEventTarget constructor (components: DefaultUpgraderComponents, init: UpgraderInit) { this.components = components diff --git a/packages/libp2p/test/addresses/address-manager.spec.ts b/packages/libp2p/test/addresses/address-manager.spec.ts index 7f67fa9b07..5bee38bb70 100644 --- a/packages/libp2p/test/addresses/address-manager.spec.ts +++ b/packages/libp2p/test/addresses/address-manager.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -18,7 +18,7 @@ const announceAddreses = ['/dns4/peer.io'] describe('Address Manager', () => { let peerId: PeerId let peerStore: StubbedInstance - let events: EventEmitter + let events: TypedEventTarget beforeEach(async () => { peerId = await createEd25519PeerId() @@ -26,7 +26,7 @@ describe('Address Manager', () => { // @ts-expect-error incorrect return type patch: Promise.resolve({}) }) - events = new EventEmitter() + events = new TypedEventEmitter() }) it('should not need any addresses', () => { diff --git a/packages/libp2p/test/circuit-relay/hop.spec.ts b/packages/libp2p/test/circuit-relay/hop.spec.ts index 7c3405197c..b1beb7abb9 100644 --- a/packages/libp2p/test/circuit-relay/hop.spec.ts +++ b/packages/libp2p/test/circuit-relay/hop.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ['error', 5] */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { isStartable } from '@libp2p/interface/startable' import { mockRegistrar, mockUpgrader, mockNetwork, mockConnectionManager, mockConnectionGater } from '@libp2p/interface-compliance-tests/mocks' import { PeerMap } from '@libp2p/peer-collections' @@ -38,7 +38,7 @@ interface Node { connectionManager: ConnectionManager circuitRelayTransport: Transport connectionGater: ConnectionGater - events: EventEmitter + events: TypedEventTarget } let peerIndex = 0 @@ -66,7 +66,7 @@ describe('circuit-relay hop protocol', function () { ]) const peerStore = stubInterface() - const events = new EventEmitter() + const events = new TypedEventEmitter() events.addEventListener('connection:open', (evt) => { const conn = evt.detail connections.set(conn.remotePeer, conn) diff --git a/packages/libp2p/test/circuit-relay/stop.spec.ts b/packages/libp2p/test/circuit-relay/stop.spec.ts index 872f01d104..f17642b951 100644 --- a/packages/libp2p/test/circuit-relay/stop.spec.ts +++ b/packages/libp2p/test/circuit-relay/stop.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { isStartable } from '@libp2p/interface/startable' import { mockStream } from '@libp2p/interface-compliance-tests/mocks' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -43,7 +43,7 @@ describe('circuit-relay stop protocol', function () { transportManager: stubInterface(), upgrader: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() } transport = circuitRelayTransport({ diff --git a/packages/libp2p/test/connection-manager/auto-dial.spec.ts b/packages/libp2p/test/connection-manager/auto-dial.spec.ts index 3c5968bdf1..13634b49e1 100644 --- a/packages/libp2p/test/connection-manager/auto-dial.spec.ts +++ b/packages/libp2p/test/connection-manager/auto-dial.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { PeerMap } from '@libp2p/peer-collections' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { PersistentPeerStore } from '@libp2p/peer-store' @@ -23,13 +23,13 @@ import type { ConnectionManager } from '@libp2p/interface-internal/connection-ma describe('auto-dial', () => { let autoDialler: AutoDial - let events: EventEmitter + let events: TypedEventTarget let peerStore: PeerStore let peerId: PeerId beforeEach(async () => { peerId = await createEd25519PeerId() - events = new EventEmitter() + events = new TypedEventEmitter() peerStore = new PersistentPeerStore({ datastore: new MemoryDatastore(), events, diff --git a/packages/libp2p/test/connection-manager/direct.node.ts b/packages/libp2p/test/connection-manager/direct.node.ts index 6da375ad6b..ae5ff3aed3 100644 --- a/packages/libp2p/test/connection-manager/direct.node.ts +++ b/packages/libp2p/test/connection-manager/direct.node.ts @@ -6,7 +6,7 @@ import path from 'node:path' import { yamux } from '@chainsafe/libp2p-yamux' import { type Connection, type ConnectionProtector, isConnection } from '@libp2p/interface/connection' import { AbortError } from '@libp2p/interface/errors' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockConnection, mockConnectionGater, mockDuplex, mockMultiaddrConnection, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' import { mplex } from '@libp2p/mplex' @@ -58,7 +58,7 @@ describe('dialing (direct, TCP)', () => { createEd25519PeerId() ]) - const remoteEvents = new EventEmitter() + const remoteEvents = new TypedEventEmitter() remoteComponents = defaultComponents({ peerId: remotePeerId, events: remoteEvents, @@ -78,7 +78,7 @@ describe('dialing (direct, TCP)', () => { remoteTM = remoteComponents.transportManager = new DefaultTransportManager(remoteComponents) remoteTM.add(tcp()()) - const localEvents = new EventEmitter() + const localEvents = new TypedEventEmitter() localComponents = defaultComponents({ peerId: localPeerId, events: localEvents, diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index 780c45f605..305ae4e524 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -2,7 +2,7 @@ import { yamux } from '@chainsafe/libp2p-yamux' import { AbortError } from '@libp2p/interface/errors' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockConnectionGater, mockDuplex, mockMultiaddrConnection, mockUpgrader, mockConnection } from '@libp2p/interface-compliance-tests/mocks' import { mplex } from '@libp2p/mplex' import { peerIdFromString } from '@libp2p/peer-id' @@ -45,7 +45,7 @@ describe('dialing (direct, WebSockets)', () => { let connectionManager: DefaultConnectionManager beforeEach(async () => { - const localEvents = new EventEmitter() + const localEvents = new TypedEventEmitter() localComponents = defaultComponents({ peerId: await createEd25519PeerId(), datastore: new MemoryDatastore(), diff --git a/packages/libp2p/test/connection-manager/index.node.ts b/packages/libp2p/test/connection-manager/index.node.ts index add7158a47..cac8b990d3 100644 --- a/packages/libp2p/test/connection-manager/index.node.ts +++ b/packages/libp2p/test/connection-manager/index.node.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start } from '@libp2p/interface/startable' import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks' import { expect } from 'aegir/chai' @@ -53,7 +53,7 @@ describe('Connection Manager', () => { peerStore, transportManager: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() }) const connectionManager = new DefaultConnectionManager(components, { maxConnections: 1000, @@ -91,7 +91,7 @@ describe('Connection Manager', () => { peerStore, transportManager: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() }) const connectionManager = new DefaultConnectionManager(components, { maxConnections: 1000, diff --git a/packages/libp2p/test/connection-manager/index.spec.ts b/packages/libp2p/test/connection-manager/index.spec.ts index 699339da03..ee31843332 100644 --- a/packages/libp2p/test/connection-manager/index.spec.ts +++ b/packages/libp2p/test/connection-manager/index.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { KEEP_ALIVE } from '@libp2p/interface/peer-store/tags' import { mockConnection, mockDuplex, mockMultiaddrConnection, mockMetrics } from '@libp2p/interface-compliance-tests/mocks' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -365,7 +365,7 @@ describe('Connection Manager', () => { peerStore: stubInterface(), transportManager: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() }, { ...defaultOptions, deny: [ @@ -393,7 +393,7 @@ describe('Connection Manager', () => { peerStore: stubInterface(), transportManager: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() }, { ...defaultOptions, maxConnections: 1 @@ -425,7 +425,7 @@ describe('Connection Manager', () => { peerStore: stubInterface(), transportManager: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() }, { ...defaultOptions, inboundConnectionThreshold: 1 @@ -461,7 +461,7 @@ describe('Connection Manager', () => { peerStore: stubInterface(), transportManager: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() }, { ...defaultOptions, maxConnections: 1, @@ -497,7 +497,7 @@ describe('Connection Manager', () => { peerStore: stubInterface(), transportManager: stubInterface(), connectionGater: stubInterface(), - events: new EventEmitter() + events: new TypedEventEmitter() }, { ...defaultOptions, maxIncomingPendingConnections: 1 diff --git a/packages/libp2p/test/fetch/index.spec.ts b/packages/libp2p/test/fetch/index.spec.ts index 9ef87c88e5..7b90a0f7db 100644 --- a/packages/libp2p/test/fetch/index.spec.ts +++ b/packages/libp2p/test/fetch/index.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -27,7 +27,7 @@ const defaultInit: FetchServiceInit = { async function createComponents (index: number): Promise { const peerId = await createEd25519PeerId() - const events = new EventEmitter() + const events = new TypedEventEmitter() const components = defaultComponents({ peerId, registrar: mockRegistrar(), diff --git a/packages/libp2p/test/identify/index.spec.ts b/packages/libp2p/test/identify/index.spec.ts index ed7d54ff09..63134a7d0c 100644 --- a/packages/libp2p/test/identify/index.spec.ts +++ b/packages/libp2p/test/identify/index.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockConnectionGater, mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -51,7 +51,7 @@ const protocols = [MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_PUSH] async function createComponents (index: number): Promise { const peerId = await createEd25519PeerId() - const events = new EventEmitter() + const events = new TypedEventEmitter() const components = defaultComponents({ peerId, datastore: new MemoryDatastore(), diff --git a/packages/libp2p/test/identify/push.spec.ts b/packages/libp2p/test/identify/push.spec.ts index 2916a8e1c4..a73bdc3655 100644 --- a/packages/libp2p/test/identify/push.spec.ts +++ b/packages/libp2p/test/identify/push.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockConnectionGater, mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -42,7 +42,7 @@ const protocols = [MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_PUSH] async function createComponents (index: number): Promise { const peerId = await createEd25519PeerId() - const events = new EventEmitter() + const events = new TypedEventEmitter() const components = defaultComponents({ peerId, datastore: new MemoryDatastore(), diff --git a/packages/libp2p/test/peer-discovery/index.node.ts b/packages/libp2p/test/peer-discovery/index.node.ts index 7adfb0c867..d3d6bd0939 100644 --- a/packages/libp2p/test/peer-discovery/index.node.ts +++ b/packages/libp2p/test/peer-discovery/index.node.ts @@ -2,7 +2,7 @@ import { bootstrap } from '@libp2p/bootstrap' import { randomBytes } from '@libp2p/crypto' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { peerDiscovery } from '@libp2p/interface/peer-discovery' import { kadDHT } from '@libp2p/kad-dht' import { mdns } from '@libp2p/mdns' @@ -22,7 +22,7 @@ import type { KadDHT } from '@libp2p/kad-dht' const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0') -class TestPeerDiscovery extends EventEmitter implements PeerDiscovery { +class TestPeerDiscovery extends TypedEventEmitter implements PeerDiscovery { get [peerDiscovery] (): PeerDiscovery { return this } diff --git a/packages/libp2p/test/ping/index.spec.ts b/packages/libp2p/test/ping/index.spec.ts index c37d5d0ebe..9b6aef1385 100644 --- a/packages/libp2p/test/ping/index.spec.ts +++ b/packages/libp2p/test/ping/index.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -28,7 +28,7 @@ const defaultInit: PingServiceInit = { async function createComponents (index: number): Promise { const peerId = await createEd25519PeerId() - const events = new EventEmitter() + const events = new TypedEventEmitter() const components = defaultComponents({ peerId, registrar: mockRegistrar(), diff --git a/packages/libp2p/test/registrar/registrar.spec.ts b/packages/libp2p/test/registrar/registrar.spec.ts index 0e5b585acf..138b45b30e 100644 --- a/packages/libp2p/test/registrar/registrar.spec.ts +++ b/packages/libp2p/test/registrar/registrar.spec.ts @@ -2,7 +2,7 @@ import { yamux } from '@chainsafe/libp2p-yamux' import { CodeError } from '@libp2p/interface/errors' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { mockDuplex, mockMultiaddrConnection, mockUpgrader, mockConnection } from '@libp2p/interface-compliance-tests/mocks' import { mplex } from '@libp2p/mplex' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -43,7 +43,7 @@ describe('registrar', () => { describe('errors', () => { beforeEach(() => { - const events = new EventEmitter() + const events = new TypedEventEmitter() components = defaultComponents({ peerId, events, @@ -81,13 +81,13 @@ describe('registrar', () => { let peerId: PeerId let connectionManager: StubbedInstance let peerStore: StubbedInstance - let events: EventEmitter + let events: TypedEventTarget beforeEach(async () => { peerId = await createEd25519PeerId() connectionManager = stubInterface() peerStore = stubInterface() - events = new EventEmitter() + events = new TypedEventEmitter() registrar = new DefaultRegistrar({ peerId, diff --git a/packages/libp2p/test/transports/transport-manager.node.ts b/packages/libp2p/test/transports/transport-manager.node.ts index 7404e88bbe..290414e618 100644 --- a/packages/libp2p/test/transports/transport-manager.node.ts +++ b/packages/libp2p/test/transports/transport-manager.node.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { FaultTolerance } from '@libp2p/interface/transport' import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' @@ -33,7 +33,7 @@ describe('Transport Manager (TCP)', () => { }) beforeEach(async () => { - const events = new EventEmitter() + const events = new TypedEventEmitter() components = defaultComponents({ peerId: localPeer, events, diff --git a/packages/libp2p/test/transports/transport-manager.spec.ts b/packages/libp2p/test/transports/transport-manager.spec.ts index 89024dd8bc..c71a124a92 100644 --- a/packages/libp2p/test/transports/transport-manager.spec.ts +++ b/packages/libp2p/test/transports/transport-manager.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { FaultTolerance } from '@libp2p/interface/transport' import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' @@ -26,7 +26,7 @@ describe('Transport Manager (WebSockets)', () => { let components: Components beforeEach(async () => { - const events = new EventEmitter() + const events = new TypedEventEmitter() components = { peerId: await createEd25519PeerId(), events, diff --git a/packages/libp2p/test/upgrading/upgrader.spec.ts b/packages/libp2p/test/upgrading/upgrader.spec.ts index f1343f128e..6f83c08ecd 100644 --- a/packages/libp2p/test/upgrading/upgrader.spec.ts +++ b/packages/libp2p/test/upgrading/upgrader.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ import { yamux } from '@chainsafe/libp2p-yamux' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockConnectionGater, mockConnectionManager, mockMultiaddrConnPair, mockRegistrar, mockStream, mockMuxer } from '@libp2p/interface-compliance-tests/mocks' import { mplex } from '@libp2p/mplex' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -76,7 +76,7 @@ describe('Upgrader', () => { registrar: mockRegistrar(), datastore: new MemoryDatastore(), connectionProtector: localConnectionProtector, - events: new EventEmitter() + events: new TypedEventEmitter() }) localComponents.peerStore = new PersistentPeerStore(localComponents) localComponents.connectionManager = mockConnectionManager(localComponents) @@ -103,7 +103,7 @@ describe('Upgrader', () => { registrar: mockRegistrar(), datastore: new MemoryDatastore(), connectionProtector: remoteConnectionProtector, - events: new EventEmitter() + events: new TypedEventEmitter() }) remoteComponents.peerStore = new PersistentPeerStore(remoteComponents) remoteComponents.connectionManager = mockConnectionManager(remoteComponents) diff --git a/packages/libp2p/test/upnp-nat/upnp-nat.node.ts b/packages/libp2p/test/upnp-nat/upnp-nat.node.ts index 42d6cc6407..8e39909147 100644 --- a/packages/libp2p/test/upnp-nat/upnp-nat.node.ts +++ b/packages/libp2p/test/upnp-nat/upnp-nat.node.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { FaultTolerance } from '@libp2p/interface/transport' import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' @@ -31,7 +31,7 @@ describe('UPnP NAT (TCP)', () => { let client: StubbedInstance async function createNatManager (addrs = DEFAULT_ADDRESSES, natManagerOptions = {}): Promise<{ natManager: any, components: Components }> { - const events = new EventEmitter() + const events = new TypedEventEmitter() const components: any = defaultComponents({ peerId: await createEd25519PeerId(), upgrader: mockUpgrader({ events }), diff --git a/packages/peer-discovery-bootstrap/src/index.ts b/packages/peer-discovery-bootstrap/src/index.ts index 1debb10fc6..7400d494f4 100644 --- a/packages/peer-discovery-bootstrap/src/index.ts +++ b/packages/peer-discovery-bootstrap/src/index.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { peerDiscovery } from '@libp2p/interface/peer-discovery' import { logger } from '@libp2p/logger' import { peerIdFromString } from '@libp2p/peer-id' @@ -50,7 +50,7 @@ export interface BootstrapComponents { /** * Emits 'peer' events on a regular interval for each peer in the provided list. */ -class Bootstrap extends EventEmitter implements PeerDiscovery, Startable { +class Bootstrap extends TypedEventEmitter implements PeerDiscovery, Startable { static tag = 'bootstrap' private timer?: ReturnType diff --git a/packages/peer-discovery-mdns/src/index.ts b/packages/peer-discovery-mdns/src/index.ts index 7424131c06..98d568640e 100644 --- a/packages/peer-discovery-mdns/src/index.ts +++ b/packages/peer-discovery-mdns/src/index.ts @@ -1,4 +1,4 @@ -import { CustomEvent, EventEmitter } from '@libp2p/interface/events' +import { CustomEvent, TypedEventEmitter } from '@libp2p/interface/events' import { peerDiscovery } from '@libp2p/interface/peer-discovery' import { logger } from '@libp2p/logger' import multicastDNS from 'multicast-dns' @@ -24,7 +24,7 @@ export interface MulticastDNSComponents { addressManager: AddressManager } -class MulticastDNS extends EventEmitter implements PeerDiscovery, Startable { +class MulticastDNS extends TypedEventEmitter implements PeerDiscovery, Startable { public mdns?: multicastDNS.MulticastDNS private readonly broadcast: boolean diff --git a/packages/peer-store/src/index.ts b/packages/peer-store/src/index.ts index d130a8a7c3..ec9b4632bf 100644 --- a/packages/peer-store/src/index.ts +++ b/packages/peer-store/src/index.ts @@ -3,7 +3,7 @@ import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record' import all from 'it-all' import { PersistentStore, type PeerUpdate } from './store.js' import type { Libp2pEvents } from '@libp2p/interface' -import type { EventEmitter } from '@libp2p/interface/events' +import type { TypedEventTarget } from '@libp2p/interface/events' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerStore, Peer, PeerData, PeerQuery } from '@libp2p/interface/peer-store' import type { Multiaddr } from '@multiformats/multiaddr' @@ -14,7 +14,7 @@ const log = logger('libp2p:peer-store') export interface PersistentPeerStoreComponents { peerId: PeerId datastore: Datastore - events: EventEmitter + events: TypedEventTarget } /** @@ -33,7 +33,7 @@ export interface PersistentPeerStoreInit { */ export class PersistentPeerStore implements PeerStore { private readonly store: PersistentStore - private readonly events: EventEmitter + private readonly events: TypedEventTarget private readonly peerId: PeerId constructor (components: PersistentPeerStoreComponents, init: PersistentPeerStoreInit = {}) { diff --git a/packages/peer-store/test/index.spec.ts b/packages/peer-store/test/index.spec.ts index 868d811c72..e5fd106beb 100644 --- a/packages/peer-store/test/index.spec.ts +++ b/packages/peer-store/test/index.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record' import { multiaddr } from '@multiformats/multiaddr' @@ -18,12 +18,12 @@ describe('PersistentPeerStore', () => { let peerId: PeerId let otherPeerId: PeerId let peerStore: PersistentPeerStore - let events: EventEmitter + let events: TypedEventTarget beforeEach(async () => { peerId = await createEd25519PeerId() otherPeerId = await createEd25519PeerId() - events = new EventEmitter() + events = new TypedEventEmitter() peerStore = new PersistentPeerStore({ peerId, events, datastore: new MemoryDatastore() }) }) diff --git a/packages/peer-store/test/merge.spec.ts b/packages/peer-store/test/merge.spec.ts index 304512781f..eefeec19c7 100644 --- a/packages/peer-store/test/merge.spec.ts +++ b/packages/peer-store/test/merge.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -20,12 +20,12 @@ describe('merge', () => { let peerId: PeerId let otherPeerId: PeerId let peerStore: PersistentPeerStore - let events: EventEmitter + let events: TypedEventTarget beforeEach(async () => { peerId = await createEd25519PeerId() otherPeerId = await createEd25519PeerId() - events = new EventEmitter() + events = new TypedEventEmitter() peerStore = new PersistentPeerStore({ peerId, events, datastore: new MemoryDatastore() }) }) diff --git a/packages/peer-store/test/patch.spec.ts b/packages/peer-store/test/patch.spec.ts index a30e22e872..929425dcb9 100644 --- a/packages/peer-store/test/patch.spec.ts +++ b/packages/peer-store/test/patch.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -20,12 +20,12 @@ describe('patch', () => { let peerId: PeerId let otherPeerId: PeerId let peerStore: PersistentPeerStore - let events: EventEmitter + let events: TypedEventTarget beforeEach(async () => { peerId = await createEd25519PeerId() otherPeerId = await createEd25519PeerId() - events = new EventEmitter() + events = new TypedEventEmitter() peerStore = new PersistentPeerStore({ peerId, events, datastore: new MemoryDatastore() }) }) diff --git a/packages/peer-store/test/save.spec.ts b/packages/peer-store/test/save.spec.ts index 43552cb883..23a7e29905 100644 --- a/packages/peer-store/test/save.spec.ts +++ b/packages/peer-store/test/save.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events' import { createEd25519PeerId, createRSAPeerId, createSecp256k1PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -23,12 +23,12 @@ describe('save', () => { let peerId: PeerId let otherPeerId: PeerId let peerStore: PersistentPeerStore - let events: EventEmitter + let events: TypedEventTarget beforeEach(async () => { peerId = await createEd25519PeerId() otherPeerId = await createEd25519PeerId() - events = new EventEmitter() + events = new TypedEventEmitter() peerStore = new PersistentPeerStore({ peerId, events, datastore: new MemoryDatastore() }) }) diff --git a/packages/protocol-perf/test/index.spec.ts b/packages/protocol-perf/test/index.spec.ts index 83fd6c310f..fc8299ed10 100644 --- a/packages/protocol-perf/test/index.spec.ts +++ b/packages/protocol-perf/test/index.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { connectionPair, mockRegistrar, type MockNetworkComponents, mockConnectionManager } from '@libp2p/interface-compliance-tests/mocks' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -11,7 +11,7 @@ export async function createComponents (): Promise { const components: any = { peerId: await createEd25519PeerId(), registrar: mockRegistrar(), - events: new EventEmitter() + events: new TypedEventEmitter() } components.connectionManager = mockConnectionManager(components) diff --git a/packages/pubsub/src/index.ts b/packages/pubsub/src/index.ts index b8760e3a4f..2de4fa3fce 100644 --- a/packages/pubsub/src/index.ts +++ b/packages/pubsub/src/index.ts @@ -1,5 +1,5 @@ import { CodeError } from '@libp2p/interface/errors' -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { type PubSub, type Message, type StrictNoSign, type StrictSign, type PubSubInit, type PubSubEvents, type PeerStreams, type PubSubRPCMessage, type PubSubRPC, type PubSubRPCSubscription, type SubscriptionChangeData, type PublishResult, type TopicValidatorFn, TopicValidatorResult } from '@libp2p/interface/pubsub' import { logger } from '@libp2p/logger' import { PeerMap, PeerSet } from '@libp2p/peer-collections' @@ -28,7 +28,7 @@ export interface PubSubComponents { * PubSubBaseProtocol handles the peers and connections logic for pubsub routers * and specifies the API that pubsub routers should have. */ -export abstract class PubSubBaseProtocol = PubSubEvents> extends EventEmitter implements PubSub { +export abstract class PubSubBaseProtocol = PubSubEvents> extends TypedEventEmitter implements PubSub { public started: boolean /** * Map of topics to which peers are subscribed to diff --git a/packages/pubsub/src/peer-streams.ts b/packages/pubsub/src/peer-streams.ts index c4a13387a5..8e1646907a 100644 --- a/packages/pubsub/src/peer-streams.ts +++ b/packages/pubsub/src/peer-streams.ts @@ -1,4 +1,4 @@ -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { abortableSource } from 'abortable-iterator' import * as lp from 'it-length-prefixed' @@ -20,7 +20,7 @@ export interface PeerStreamsInit { /** * Thin wrapper around a peer's inbound / outbound pubsub streams */ -export class PeerStreams extends EventEmitter { +export class PeerStreams extends TypedEventEmitter { public readonly id: PeerId public readonly protocol: string /** diff --git a/packages/transport-tcp/src/listener.ts b/packages/transport-tcp/src/listener.ts index 95792ed137..ee2d1a20f9 100644 --- a/packages/transport-tcp/src/listener.ts +++ b/packages/transport-tcp/src/listener.ts @@ -1,6 +1,6 @@ import net from 'net' import { CodeError } from '@libp2p/interface/errors' -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { CODE_P2P } from './constants.js' import { toMultiaddrConnection } from './socket-to-conn.js' @@ -71,7 +71,7 @@ type Status = { code: TCPListenerStatusCode.INACTIVE } | { netConfig: NetConfig } -export class TCPListener extends EventEmitter implements Listener { +export class TCPListener extends TypedEventEmitter implements Listener { private readonly server: net.Server /** Keep track of open connections to destroy in case of timeout */ private readonly connections = new Set() diff --git a/packages/transport-tcp/test/connection-limits.spec.ts b/packages/transport-tcp/test/connection-limits.spec.ts index 30aea96ac7..67a47c23e7 100644 --- a/packages/transport-tcp/test/connection-limits.spec.ts +++ b/packages/transport-tcp/test/connection-limits.spec.ts @@ -1,6 +1,6 @@ import net from 'node:net' import { promisify } from 'util' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -88,7 +88,7 @@ describe('closeAbove/listenBelow', () => { const trasnport = tcp({ closeServerOnMaxConnections: { listenBelow, closeAbove } })() const upgrader = mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) const listener = trasnport.createListener({ upgrader }) as TCPListener // eslint-disable-next-line @typescript-eslint/promise-function-async @@ -115,7 +115,7 @@ describe('closeAbove/listenBelow', () => { const trasnport = tcp({ closeServerOnMaxConnections: { listenBelow, closeAbove } })() const upgrader = mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) const listener = trasnport.createListener({ upgrader }) as TCPListener // eslint-disable-next-line @typescript-eslint/promise-function-async @@ -150,7 +150,7 @@ describe('closeAbove/listenBelow', () => { const trasnport = tcp({ closeServerOnMaxConnections: { listenBelow, closeAbove } })() const upgrader = mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) const listener = trasnport.createListener({ upgrader }) as TCPListener // eslint-disable-next-line @typescript-eslint/promise-function-async @@ -181,7 +181,7 @@ describe('closeAbove/listenBelow', () => { const trasnport = tcp({ closeServerOnMaxConnections: { listenBelow, closeAbove } })() const upgrader = mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) const listener = trasnport.createListener({ upgrader }) as TCPListener // eslint-disable-next-line @typescript-eslint/promise-function-async diff --git a/packages/transport-tcp/test/connection.spec.ts b/packages/transport-tcp/test/connection.spec.ts index 0308d61708..a8cb893edb 100644 --- a/packages/transport-tcp/test/connection.spec.ts +++ b/packages/transport-tcp/test/connection.spec.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -13,7 +13,7 @@ describe('valid localAddr and remoteAddr', () => { beforeEach(() => { transport = tcp()() upgrader = mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) }) diff --git a/packages/transport-tcp/test/listen-dial.spec.ts b/packages/transport-tcp/test/listen-dial.spec.ts index a2988bce77..4c7f608350 100644 --- a/packages/transport-tcp/test/listen-dial.spec.ts +++ b/packages/transport-tcp/test/listen-dial.spec.ts @@ -1,6 +1,6 @@ import os from 'os' import path from 'path' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockRegistrar, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -22,7 +22,7 @@ describe('listen', () => { beforeEach(() => { transport = tcp()() upgrader = mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) }) @@ -175,7 +175,7 @@ describe('dial', () => { }) upgrader = mockUpgrader({ registrar, - events: new EventEmitter() + events: new TypedEventEmitter() }) transport = tcp()() diff --git a/packages/transport-tcp/test/max-connections.spec.ts b/packages/transport-tcp/test/max-connections.spec.ts index 13d18d0c14..495a23e991 100644 --- a/packages/transport-tcp/test/max-connections.spec.ts +++ b/packages/transport-tcp/test/max-connections.spec.ts @@ -1,6 +1,6 @@ import net from 'node:net' import { promisify } from 'node:util' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -22,7 +22,7 @@ describe('maxConnections', () => { const transport = tcp({ maxConnections })() const upgrader = mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) const listener = transport.createListener({ upgrader }) // eslint-disable-next-line @typescript-eslint/promise-function-async diff --git a/packages/transport-webrtc/src/private-to-private/listener.ts b/packages/transport-webrtc/src/private-to-private/listener.ts index 53a3d299c6..6a73e9e14a 100644 --- a/packages/transport-webrtc/src/private-to-private/listener.ts +++ b/packages/transport-webrtc/src/private-to-private/listener.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { Circuit } from '@multiformats/mafmt' import type { PeerId } from '@libp2p/interface/peer-id' import type { ListenerEvents, Listener } from '@libp2p/interface/transport' @@ -14,7 +14,7 @@ export interface WebRTCPeerListenerInit { shutdownController: AbortController } -export class WebRTCPeerListener extends EventEmitter implements Listener { +export class WebRTCPeerListener extends TypedEventEmitter implements Listener { private readonly peerId: PeerId private readonly transportManager: TransportManager private readonly shutdownController: AbortController diff --git a/packages/transport-websockets/.aegir.js b/packages/transport-websockets/.aegir.js index 1a81ef61be..d7e24c7d10 100644 --- a/packages/transport-websockets/.aegir.js +++ b/packages/transport-websockets/.aegir.js @@ -6,7 +6,7 @@ export default { async before () { const { multiaddr } = await import('@multiformats/multiaddr') const { mockRegistrar, mockUpgrader } = await import('@libp2p/interface-compliance-tests/mocks') - const { EventEmitter } = await import('@libp2p/interface/events') + const { TypedEventEmitter } = await import('@libp2p/interface/events') const { webSockets } = await import('./dist/src/index.js') const protocol = '/echo/1.0.0' @@ -19,7 +19,7 @@ export default { }) const upgrader = mockUpgrader({ registrar, - events: new EventEmitter() + events: new TypedEventEmitter() }) const ws = webSockets()() diff --git a/packages/transport-websockets/src/listener.ts b/packages/transport-websockets/src/listener.ts index 9adb5957ae..6f307c0ecb 100644 --- a/packages/transport-websockets/src/listener.ts +++ b/packages/transport-websockets/src/listener.ts @@ -1,5 +1,5 @@ import os from 'os' -import { EventEmitter, CustomEvent } from '@libp2p/interface/events' +import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr' import { multiaddr, protocols } from '@multiformats/multiaddr' @@ -14,7 +14,7 @@ import type { WebSocketServer } from 'it-ws/server' const log = logger('libp2p:websockets:listener') -class WebSocketListener extends EventEmitter implements Listener { +class WebSocketListener extends TypedEventEmitter implements Listener { private readonly connections: Set private listeningMultiaddr?: Multiaddr private readonly server: WebSocketServer diff --git a/packages/transport-websockets/test/browser.ts b/packages/transport-websockets/test/browser.ts index 616f09e37e..88e311c67c 100644 --- a/packages/transport-websockets/test/browser.ts +++ b/packages/transport-websockets/test/browser.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -23,7 +23,7 @@ describe('libp2p-websockets', () => { ws = webSockets()() conn = await ws.dial(ma, { upgrader: mockUpgrader({ - events: new EventEmitter() + events: new TypedEventEmitter() }) }) }) diff --git a/packages/transport-websockets/test/node.ts b/packages/transport-websockets/test/node.ts index 32aa1b02ae..8c296900ce 100644 --- a/packages/transport-websockets/test/node.ts +++ b/packages/transport-websockets/test/node.ts @@ -4,7 +4,7 @@ import fs from 'fs' import http from 'http' import https from 'https' -import { EventEmitter } from '@libp2p/interface/events' +import { TypedEventEmitter } from '@libp2p/interface/events' import { mockRegistrar, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -41,7 +41,7 @@ void registrar.handle(protocol, (evt) => { }) const upgrader = mockUpgrader({ registrar, - events: new EventEmitter() + events: new TypedEventEmitter() }) describe('instantiate the transport', () => {