UNPKG

@collight/dns-sd

Version:

A DNS Service Discovery implementation in modern TypeScript

72 lines (71 loc) 2.45 kB
import { EventEmitter } from 'events'; import { SrvAnswer, StringAnswer, TxtAnswer } from 'dns-packet'; import { default as mDNS, MulticastDNS } from 'multicast-dns'; /** * Represents a DNS Resource Record relevant for mDNS responses. */ export type MDNSRecord = StringAnswer | TxtAnswer | SrvAnswer; type EventMap = { responded: [packet: mDNS.ResponseOutgoingPacket, error: Error | null, bytes?: number]; }; /** * An mDNS server that responds to DNS queries on the local network. * * Maintains a registry of published records and automatically responds * to queries using the multicast-dns protocol. * * Emits: * - `responded` when a response is sent to a query * * @example * ```ts * const server = new MDNSServer({}, console.error) * server.register([{ name: '_http._tcp.local', type: 'PTR', data: 'MyService._http._tcp.local' }]) * ``` * * @see https://datatracker.ietf.org/doc/html/rfc6762 */ export declare class MDNSServer extends EventEmitter<EventMap> { mdns: MulticastDNS; private typeToRecords; /** * @param options - Configuration passed to the underlying `multicast-dns` instance. */ constructor(options: mDNS.Options); /** * Registers a list of DNS records to be used in future responses. * * Duplicate records (based on type, name, and deep equality of data) are ignored. * * @param records - An array of mDNS records to register. */ register(records: MDNSRecord[]): void; /** * Unregisters a set of previously registered DNS records. * * Matching is done by `record.name` only (not deeply). * * @param records - Records to remove from the server registry. */ unregister(records: MDNSRecord[]): void; /** * Returns all records of a given type matching a name. * * If the name is not fully qualified (no dot), only the first segment of the record name is compared. * * @param type - DNS record type. * @param name - Name to match against. * @returns Matching records. */ private getRecordsOf; /** * Responds to incoming mDNS queries with matching registered records. * Handles `ANY` queries and populates additional records for `PTR → SRV → A/AAAA + TXT` dependencies, * * @param query - The incoming mDNS query packet. * * @see {@link https://www.rfc-editor.org/rfc/rfc6763#section-12} */ private respond; } export {};