import { EventEmitter } from "eventemitter3"; import type { RelayInfoDocument, SystemInterface } from "."; import { Connection, type RelaySettings } from "./connection"; import type { NostrEvent, OkResponse, ReqCommand, TaggedNostrEvent } from "./nostr"; /** * Events which the ConnectionType must emit */ export interface ConnectionTypeEvents { change: () => void; connected: (wasReconnect: boolean) => void; error: () => void; unverifiedEvent: (sub: string, e: TaggedNostrEvent) => void; eose: (sub: string) => void; closed: (sub: string, reason: string) => void; disconnect: (code: number) => void; auth: (challenge: string, relay: string, cb: (ev: NostrEvent) => void) => void; notice: (msg: string) => void; unknownMessage: (obj: Array) => void; } export type ConnectionSubscription = Record; /** * Basic relay connection */ export type ConnectionType = { readonly id: string; readonly address: string; readonly info: RelayInfoDocument | undefined; readonly isDown: boolean; readonly isOpen: boolean; readonly activeSubscriptions: number; readonly maxSubscriptions: number; settings: RelaySettings; ephemeral: boolean; /** * Connect to relay */ connect: () => Promise; /** * Disconnect relay */ close: () => void; /** * Publish an event to this relay */ publish: (ev: NostrEvent, timeout?: number) => Promise; /** * Send request to relay */ request: (req: ReqCommand, cbSent?: () => void) => void; /** * Send raw json object on wire (used by negentropy sync) */ sendRaw: (obj: object) => void; /** * Close a request */ closeRequest: (id: string) => void; } & EventEmitter; /** * Events which are emitted by the connection pool */ export interface ConnectionPoolEvents { connected: (address: string, wasReconnect: boolean) => void; connectFailed: (address: string) => void; event: (address: string, sub: string, e: TaggedNostrEvent) => void; eose: (address: string, sub: string) => void; disconnect: (address: string, code: number) => void; auth: (address: string, challenge: string, relay: string, cb: (ev: NostrEvent) => void) => void; notice: (address: string, msg: string) => void; } /** * Base connection pool */ export type ConnectionPool = { getConnection(id: string): ConnectionType | undefined; connect(address: string, options: RelaySettings, ephemeral: boolean): Promise; disconnect(address: string): void; broadcast(ev: NostrEvent, cb?: (rsp: OkResponse) => void): Promise; broadcastTo(address: string, ev: NostrEvent): Promise; } & EventEmitter & Iterable<[string, ConnectionType]>; /** * Function for building new connections */ export type ConnectionBuilder = (address: string, options: RelaySettings, ephemeral: boolean) => Promise | T; /** * Simple connection pool containing connections to multiple nostr relays */ export declare class DefaultConnectionPool extends EventEmitter implements ConnectionPool { #private; constructor(system: SystemInterface, builder?: ConnectionBuilder); /** * Get a connection object from the pool */ getConnection(id: string): T | undefined; /** * Add a new relay to the pool */ connect(address: string, options: RelaySettings, ephemeral: boolean): Promise; /** * Remove relay from pool */ disconnect(address: string): void; /** * Broadcast event to all write relays. * @remarks Also write event to read relays of those who are `p` tagged in the event (Inbox model) */ broadcast(ev: NostrEvent, cb?: (rsp: OkResponse) => void): Promise; /** * Send event to specific relay */ broadcastTo(address: string, ev: NostrEvent): Promise; [Symbol.iterator](): Generator<[string, T], void, unknown>; } //# sourceMappingURL=connection-pool.d.ts.map