UNPKG

3.09 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import { RemoteInfo } from "dgram";
4
5declare function bonjour(opts?: bonjour.BonjourOptions): bonjour.Bonjour;
6export = bonjour;
7declare namespace bonjour {
8 /**
9 * Start a browser
10 *
11 * The browser listens for services by querying for PTR records of a given
12 * type, protocol and domain, e.g. _http._tcp.local.
13 *
14 * If no type is given, a wild card search is performed.
15 *
16 * An internal list of online services is kept which starts out empty. When
17 * ever a new service is discovered, it's added to the list and an "up" event
18 * is emitted with that service. When it's discovered that the service is no
19 * longer available, it is removed from the list and a "down" event is emitted
20 * with that service.
21 */
22 interface Browser extends NodeJS.EventEmitter {
23 services: RemoteService[];
24 start(): void;
25 update(): void;
26 stop(): void;
27 on(event: "up" | "down", listener: (service: RemoteService) => void): this;
28 once(event: "up" | "down", listener: (service: RemoteService) => void): this;
29 removeListener(event: "up" | "down", listener: (service: RemoteService) => void): this;
30 removeAllListeners(event?: "up" | "down"): this;
31 }
32 interface BrowserOptions {
33 type?: string | undefined;
34 subtypes?: string[] | undefined;
35 protocol?: string | undefined;
36 txt?: { [key: string]: string } | undefined;
37 }
38
39 interface ServiceOptions {
40 name: string;
41 host?: string | undefined;
42 port: number;
43 type: string;
44 subtypes?: string[] | undefined;
45 protocol?: "udp" | "tcp" | undefined;
46 txt?: { [key: string]: string } | undefined;
47 probe?: boolean | undefined;
48 }
49
50 interface BaseService {
51 name: string;
52 fqdn: string;
53 host: string;
54 port: number;
55 type: string;
56 protocol: string;
57 subtypes: string[];
58 txt: { [key: string]: string };
59 }
60 interface RemoteService extends BaseService {
61 referer: RemoteInfo;
62 rawTxt: Buffer;
63 addresses: string[];
64 }
65 interface Service extends BaseService, NodeJS.EventEmitter {
66 published: boolean;
67 addresses: string[];
68
69 stop(cb?: () => void): void;
70 start(): void;
71 }
72 interface BonjourOptions {
73 type?: "udp4" | "udp6" | undefined;
74 multicast?: boolean | undefined;
75 interface?: string | undefined;
76 port?: number | undefined;
77 ip?: string | undefined;
78 ttl?: number | undefined;
79 loopback?: boolean | undefined;
80 reuseAddr?: boolean | undefined;
81 }
82 interface Bonjour {
83 (opts?: BonjourOptions): Bonjour;
84 publish(options: ServiceOptions): Service;
85 unpublishAll(cb?: () => void): void;
86 find(options: BrowserOptions, onUp?: (service: RemoteService) => void): Browser;
87 findOne(options: BrowserOptions, cb?: (service: RemoteService) => void): Browser;
88 destroy(): void;
89 }
90}