/// import PeerInfo from './peer-info'; import { EventEmitter } from 'events'; import { Socket } from 'net'; interface SwarmOptions { /** * Optionally overwrite the default set of bootstrap servers * Each string has to be a valid IPv4 address followed by a port like so: * * `127.0.0.1:5001` */ bootstrap?: Array; /** * Set to false if this is a long running instance on a server * When running in ephemeral mode you don't join the DHT but just * query it instead. If unset, or set to a non-boolean (default undefined) * then the node will start in short-lived (ephemeral) mode and switch * to long-lived (non-ephemeral) mode after a certain period of uptime */ ephemeral?: boolean; /** total amount of peers that this peer will connect to */ maxPeers?: number; /** * Set et to a number to restrict the amount of server socket * based peer connections, unrestricted by default. * setting to 0 is the same as Infinity, to disallowe server * connections set to -1 */ maxServerSockets?: number; /** * set to a number to restrict the amount of client sockets * based peer connections, unrestricted by default. */ maxClientSockets?: number; /** apply a filter before connecting to the peer */ validatePeer?: (peer: any) => boolean; /** Set to false to hide the local Address on the network */ announceLocalAddress?: boolean; /** configure peer management behaviour */ queue?: { /** * an array of backoff times, in millieconds * every time a failing peer connection is retried * it will wait for specified milliseconds based on the * retry count, until it reaches the end of the requeue * array at which time the peer is considered unresponsive * and retry attempts cease */ requeue?: Array; /** * configure when to forget certain peer characteristics * and treat them as fresh peer connections again */ forget?: { /** * how long to wait before forgetting that a peer * has become unresponsive */ unresponsive?: number; /** * how long to wait before fogetting that a peer has been banned */ banned?: number; }; /** attempt to reuse existing connections between peers across multiple topics */ multiplex?: boolean; }; } interface TopicStatus { announce?: boolean; lookup?: boolean; } declare interface Hyperswarm { on(event: 'connection', listener: (socket: Socket, peerInfo: PeerInfo) => void): this; on(event: 'disconnection', listener: (socket: Socket, peerInfo: PeerInfo) => void): this; on(event: 'error', listener: (err: Error) => void): this; on(event: 'listening', listener: () => void): this; on(event: 'close', listener: () => void): this; on(event: 'updated', listener: (topic: Buffer) => void): this; on(event: 'peer-rejected', listener: (peer: any) => void): this; on(event: 'peer', listener: (peer: any) => void): this; on(event: 'join', listener: (topic: Buffer, status: TopicStatus) => void): this; on(event: 'leave', listener: (topic: Buffer) => void): this; } /** * This class is the Hyperswarm Class * */ declare class Hyperswarm extends EventEmitter { /** Is true if the Hyperswarm Instance has been destroyed */ destroyed: boolean; prioritisedInflight: number; /** The number of active client sockets */ clientSockets: number; /** The number of active server sockets */ serverSockets: number; /** The number of peers we'Re connected with */ peers: number; /** The maximum number of peers we can have */ maxPeers: number; /** The maximum number of serverSockets we can have */ maxServerSockets: number; /** The maximum number of clientSockets we can have */ maxClientSockets: number; /** If we're open to ceonnection */ open: boolean; /** A Set that contains all the open Sockets we have right now. Can be interated over to send a message to each peer */ connections: Set; /** The function that gets used to validate new peers before we connect to them*/ validatePeer: SwarmOptions['validatePeer']; /** @internal */ network: any; /** @internal */ private kStatus; /** @internal */ private kFlush; /** @internal */ private kQueue; /** @internal */ constructor(options?: SwarmOptions); /** * stable */ address(): { host: string; port: number; }; /** * stable */ status(key: Buffer): TopicStatus | null; /** * stable */ remoteAddress(): { host: string; port: number; } | null; /** * stable */ holepunchable(): boolean; /** * stable */ listen(port?: number): Promise; /** * stable */ join(key: Buffer, opts?: TopicStatus): Promise<{ topic: Buffer; lookup: boolean; announce: boolean; }>; /** * @todo: Promisify */ leave(topic: Buffer, onleave?: () => void): void; /** * @todo: Promisify */ flush(cb: (err?: Error) => void): void; private kLeave; /** * stable */ connect(peer: any): Promise<{ socket: Socket; isTCP: boolean; }>; /** * stable */ connectivity(): Promise<{ err?: Error; bound?: boolean; bootstrapped?: boolean; holepunched?: boolean; }>; /** * stable */ destroy(): Promise; /** * stable */ private kDrain; /** * stable */ private kIncrPeerCount; /** * stable */ private kDecrPeerCount; } export default Hyperswarm;