@collight/dns-sd
Version:
A DNS Service Discovery implementation in modern TypeScript
83 lines (82 loc) • 2.82 kB
TypeScript
import { EventEmitter } from 'events';
import { MulticastDNS } from 'multicast-dns';
import { DiscoveredService } from './DiscoveredService';
export type BrowserFilter = {
/**
* Network protocol used by the service, either TCP or UDP.
* @default 'tcp'
*/
protocol: 'tcp' | 'udp';
/**
* The service type to browse for. This corresponds to the DNS-SD service type without the leading underscore.
* @example 'http', 'ipp'
*/
type: string;
/**
* Optional service subtypes to filter for. When provided, the browser targets this instance specifically.
* @example ['foo', 'bar']
*/
subtypes?: string[];
/**
* Optional specific instance name to filter for. When provided, the browser targets this instance specifically.
* @example 'MyPrinter'
*/
name?: string | RegExp;
/**
* Optional TXT record key-value pairs to filter discovered services.
* Only services whose TXT records match all keys and values will be emitted.
*/
txt?: Record<string, string | RegExp>;
};
export interface BrowserOptions {
filter?: BrowserFilter;
}
interface BrowserEventMap {
up: [service: DiscoveredService];
down: [service: DiscoveredService];
update: [curr: DiscoveredService, prev: DiscoveredService];
}
/**
* Browses the local network for mDNS/DNS-SD service instances.
*
* Emits:
* - `up`: when a new service appears.
* - `down`: when a previously seen service sends a "goodbye" (TTL = 0).
* - `update`: when an existing service has received new records
*/
export declare class Browser extends EventEmitter<BrowserEventMap> {
static TLD: string;
static WILDCARD: string;
services: DiscoveredService[];
private mdns;
private filter?;
private onresponse?;
constructor(mdns: MulticastDNS, options?: BrowserOptions);
private get queryNames();
/**
* Starts the browser and begins listening for mDNS service announcements.
* - Has no effect if the browser is already started.
*/
start(): void;
/**
* Stops the browser from listening to mDNS service announcements.
* - Has no effect if the browser is already stopped
*/
stop(): void;
/**
* Sends an active query to refresh the list of discovered services.
* - Triggers a new PTR record query for the configured service name
* - Causes mDNS responders to reply with their current service information
* - Useful for manual refresh when you suspect stale service data
*/
update(): void;
private match;
/**
* TODO: {@link https://datatracker.ietf.org/doc/html/rfc6762#section-5.2 | Continuous Multicast DNS Querying}
*/
private setupTTLTimer;
private addService;
private updateService;
private removeService;
}
export {};