reticulum-js
    Preparing search index...

    Class AutoInterface

    AutoInterface — zero-config IPv6-multicast local-network peering.

    Phases 1–3 of work doc #12: discovery + peer tracking (1), the data path (2), and the lifecycle jobs — peer expiry, reverse-peering send, link-local rebind, multicast-echo watchdog (3). Phase 4 (register in the interface registry; schema already present) remains. (AutoInterfacePeer) and registering it with a transport arrives in Phase 2; the lifecycle jobs (expiry, reverse-peering send, rebind, watchdog) arrive in Phase 3.

    The parent interface dispatches:

    • "connected" once discovery sockets are up,
    • "peer" with { address, ifname } when a new peer is authenticated (Phase 2 will additionally spawn a peer interface and dispatch "connection"),
    • "closed" on disconnect(),
    • "error" on socket errors.

    Hierarchy

    • Interface
      • AutoInterface
    Index
    _announceTimers: Record<string, Timeout>
    _closed: boolean = false

    Whether a terminal closed event has already been dispatched for the current connection episode (dedupe guard).

    _packetWriter: WritableStreamDefaultWriter<any> | null = null
    _peerJobsTimer: Timeout | null
    _reconnectAbort: AbortController | null = null

    AbortController for the current reconnect wait, so disconnect() can cancel an in-flight backoff immediately.

    _reconnectAttempts: number = 0

    Reconnect attempt counter for the current drop episode. Reset to 0 at the start of each Interface._runReconnectLoop run.

    _reconnecting: boolean = false

    Single-flight guard: only one reconnect loop runs at a time.

    adoptedInterfaces: Record<string, string>
    allowedInterfaces: string[]
    announceInterval: number
    autoReconnect: boolean = RECONNECT_DEFAULTS.autoReconnect

    Whether automatic reconnection is enabled for this initiator interface.

    bitrate: number
    carrierChanged: boolean

    Set whenever peer/link state changes a way that should reset announce-rate control.

    connectTimeout: number = RECONNECT_DEFAULTS.connectTimeout

    Per-dial connect timeout in seconds.

    dataPort: number
    dataSockets: Record<string, Socket>
    detached: boolean = false

    Permanent stop signal read by the reconnect loop. Set by disconnect().

    discoveryPort: number
    discoveryScope: string
    groupId: Uint8Array<ArrayBuffer>
    ifacSize: number
    ignoredInterfaces: string[]
    initialEchoes: Record<string, number>
    initiator: boolean = false

    Whether this interface is the initiator (the outbound dialer). Only initiators reconnect; adopted/server-spawned sockets never do (matching the Python reference initiator flag).

    linkLocalAddresses: string[]
    maxReconnectTries: number = RECONNECT_DEFAULTS.maxReconnectTries

    Maximum reconnection attempts per drop. Infinity retries forever.

    mcastDiscoveryAddress: string | null
    mifDeque: { expiresAt: number; hash: Uint8Array }[]

    Multi-interface dedup deque: recent data-packet hashes with their expiry. Capped at MULTI_IF_DEQUE_LEN entries; a hit within MULTI_IF_DEQUE_TTL drops a duplicate seen on another interface.

    multicastAddressType: string
    multicastEchoes: Record<string, number>
    multicastEchoTimeout: number
    multicastSockets: Record<string, Socket>
    name: string
    online: boolean
    peeringTimeout: number
    peerJobInterval: number
    peers: Record<
        string,
        { ifname: string; lastHeard: number; lastOutbound: number },
    >
    reconnectWait: number = RECONNECT_DEFAULTS.reconnectWait

    Seconds to wait between reconnection attempts.

    reversePeeringInterval: number
    socket: Socket | null = null

    The underlying socket, when this interface is backed by a Node.js stream.

    spawnedInterfaces: Record<string, AutoInterfacePeer>
    timedOutInterfaces: Record<string, boolean>
    transport: TransportCore | null

    Transport that owns this interface, set via attachTransport. When present, spawned peers are auto-registered with it; otherwise the caller registers them via the "connection" event.

    unicastDiscoveryPort: number
    unicastSockets: Record<string, Socket>
    • get peerCount(): number

      Number of spawned peer data interfaces. Mirrors Python's len(self.spawned_interfaces) (the count used for peer_count).

      Returns number

    • get readable(): any

      The parent AutoInterface has no RNS byte stream of its own — it only spawns per-peer interfaces that do. Returns null (rather than throwing) so TransportCore.addInterface can attach the parent to receive the transport ref via attachTransport without trying to grab a writer.

      Returns any

    • Protected

      Signals the reconnect loop to stop and cancels any in-flight backoff. Client subclasses call this at the top of their disconnect().

      Returns void

    • Protected

      Dispatches a terminal closed event exactly once per connection episode.

      Returns void

    • Protected

      Dials the peer and sets up the RNS streams, resolving once connected and dispatching connected. Implemented by reconnect-capable client subclasses; used both for the initial connection and each reconnect attempt by the shared Interface._runReconnectLoop.

      Returns Promise<void>

    • Protected

      Called when the underlying connection drops (the inbound stream ends or errors). For an initiator with auto-reconnect enabled and not deliberately detached, dispatches disconnected and kicks off the reconnect loop; otherwise dispatches a terminal closed event.

      Matches the Python reference read_loop, which reconnects the initiator on any termination and tears down (non-reconnecting) everyone else.

      Returns void

    • Protected

      Initializes shared reconnect state from constructor options. Called by client interface subclasses (TCP, WebSocket) that support reconnection.

      Subclasses must also set Interface.initiator: true for an outbound dialer, false for an adopted/server-spawned socket.

      Parameters

      • options: ReconnectOptions

      Returns void

    • Decides whether an interface should be adopted, mirroring the Python reference's per-interface skip chain.

      When an allow-list (AutoInterface.allowedInterfaces, devices) is set, only those names are adopted, and they bypass the ignore lists (including loopback) — matching Python's if len(allowed) > 0 and not ifname in allowed: skip, with the same relaxation that lets the single-host loopback smoketest pin lo0 explicitly. (Python unconditionally skips lo0 on Darwin even when allowed; we relax only that, so production parity holds: loopback is never adopted without devices.)

      Parameters

      • ifname: string

      Returns boolean

    • Multi-interface dedup check. Returns true (and remembers nothing) if the packet hash was seen within MULTI_IF_DEQUE_TTL; otherwise remembers it and returns false. Mirrors Python's mif_deque/mif_deque_times logic.

      Parameters

      • hash: Uint8Array<ArrayBufferLike>

        SHA-256 of the raw datagram bytes.

      Returns boolean

      true if the packet is a recent duplicate.

    • Protected

      Authenticates an inbound discovery datagram and, on success, records the peer. Verifies that the first HASHLENGTH_BYTES bytes equal SHA-256(group_id || src_addr), exactly as Python's discovery_handler does. Self-multicast-echoes (src is one of our own link-local addresses) feed the carrier watchdog state instead of adding a peer.

      Parameters

      • data: Uint8Array<ArrayBufferLike>
      • rinfo: { address: string; port: number; scopeId?: number }
      • ifname: string

      Returns Promise<void>

    • Protected

      One peer-jobs tick. Expires silent peers (and tears down their spawned interfaces), sends reverse-peering packets to peers due for one, rebinds the per-interface data socket when its link-local address changes, and runs the multicast-echo carrier watchdog. Mirrors Python's peer_jobs.

      Returns Promise<void>

    • Protected

      Runs the single-flight reconnect loop. Repeatedly waits reconnectWait seconds then attempts to re-establish the connection via the subclass _establishConnection() hook, until it succeeds, the interface is detached, or maxReconnectTries is exceeded (terminal closed).

      Each attempt fires a reconnecting event with the upcoming attempt number, the wait, and the cap, for observability. A successful reconnect fires connected (via _establishConnection).

      Returns Promise<void>

    • Sends a raw RNS packet to a peer on the given interface, from that interface's data socket. The destination is re-scoped with %ifname so the OS routes it out the correct link-local. Mirrors Python's AutoInterfacePeer.process_outgoing send path (Python uses a shared unbound outbound socket + addr%ifindex; we reuse the per-interface bound data socket, which already has the correct source address).

      Parameters

      • addr: string

        Descope'd peer link-local address.

      • ifname: string
      • data: Uint8Array<ArrayBufferLike>

      Returns void

    • Protected

      Resolves after ms, or immediately if signal aborts. Used so disconnect() can cancel an in-flight reconnect backoff at once.

      Parameters

      • ms: number
      • signal: AbortSignal

      Returns Promise<void>

    • The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

      MDN Reference

      Parameters

      • type: string
      • callback: EventListenerOrEventListenerObject | null
      • Optionaloptions: boolean | AddEventListenerOptions

      Returns void

    • Records a discovered peer, or refreshes a known one, mirroring Python's add_peer. For a new peer this spawns an AutoInterfacePeer data interface, registers it with the attached transport (if any), and dispatches a "connection" event with the peer for parity with the other server interfaces.

      If addr is one of our own link-local addresses, this is our own multicast echo: it feeds the carrier watchdog (multicastEchoes/initialEchoes) rather than adding a peer.

      Parameters

      • addr: string

        Descope'd source link-local address.

      • ifname: string

      Returns void

    • Receives the owning transport from TransportCore.addInterface so spawned peers can be auto-registered (Open Q1 of work doc #12). Also registers any peers spawned before the transport was attached, so the addInterface/connect call order doesn't matter.

      Parameters

      • transport: TransportCore

      Returns void

    • Enumerates, adopts suitable interfaces, opens the per-interface multicast + unicast discovery sockets, and starts the announce loop and receive handlers. Resolves once all adopted interfaces are listening and dispatches "connected".

      The discovery address and per-interface tokens are derived lazily here (rather than in the constructor) because Identity.fullHash is async.

      Returns Promise<void>

    • Closes all discovery and data sockets, stops the announce loops, disconnects every spawned peer (which dispatches "closed" so an attached transport removes it), drops all tracking state, and dispatches "closed".

      Returns Promise<void>

    • The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

      MDN Reference

      Parameters

      • event: Event

      Returns boolean

    • The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

      MDN Reference

      Parameters

      • type: string
      • callback: EventListenerOrEventListenerObject | null
      • Optionaloptions: boolean | EventListenerOptions

      Returns void

    • Returns the JSON Schema describing the options accepted by the AutoInterface constructor, for dynamically-generated setup UIs.

      Returns Record<string, any>

      A JSON Schema object.