Repository navigation
Expand file tree
/
Copy pathindex.d.ts
More file actions
285 lines (251 loc) · 10.3 KB
/
Copy pathindex.d.ts
File metadata and controls
285 lines (251 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
export declare const ImplType: {
readonly NSD: 'NSD'
readonly DNSSD: 'DNSSD'
}
/** Android only: which discovery implementation to use */
export type ImplType = (typeof ImplType)[keyof typeof ImplType]
export interface Service {
/** Human-readable service name */
name: string
/** Full service name, e.g. `XeroxPrinter.local._http._tcp.` */
fullName: string
/** Hostname, e.g. `XeroxPrinter.local.` */
host: string
port: number
/** IPv4 and IPv6 addresses, IPv4 first */
addresses: string[]
ipv4: string[]
ipv6: string[]
/** TXT record attributes */
txt: Record<string, string>
}
type TxtValue = string | number | boolean
/** TXT records as an object, or as [key, value] pairs to control the order */
export type TxtRecord = Record<string, TxtValue> | Array<[string, TxtValue]>
export interface ScanOptions {
/** Service type without underscore, e.g. `'http'`. Defaults to `'http'` */
type?: string
/** `'tcp'` or `'udp'`. Defaults to `'tcp'` */
protocol?: string
/** Defaults to `'local.'` */
domain?: string
/** Android only, defaults to `NSD` */
implType?: ImplType
/**
* Seconds to try resolving each service before retrying once, then emitting an `error` with code `'TIMEOUT'`.
* Defaults to `5`. Android `NSD` before Android 14 can't retry a resolve, the error comes after twice this time
*/
resolveTimeout?: number
/** Only find services registered with this subtype, e.g. `'printer'` */
subtype?: string
/**
* Network interface to scan on, e.g. `'en0'` on iOS or `'wlan0'` on Android. All of them by default.
* Android `NSD` needs Android 13 or later for it
*/
networkInterface?: string
}
export interface PublishOptions {
/** Service type without underscore, e.g. `'http'` */
type: string
/** `'tcp'` or `'udp'` */
protocol: string
/** Defaults to `'local.'` */
domain?: string
/** Service name, should be unique on the network */
name: string
port: number
txt?: TxtRecord
/** Android only, defaults to `NSD` */
implType?: ImplType
/** Subtypes to register the service with, e.g. `['printer']` */
subtypes?: string[]
/** Network interface to publish on, all of them by default. Android `NSD` needs Android 13 or later for it */
networkInterface?: string
}
export interface UpdateOptions {
/** The new TXT record, replaces the current one */
txt?: TxtRecord
/** Android only, defaults to the implementation the service was published with */
implType?: ImplType
}
export interface ResolveOptions {
/** Name of the service */
name: string
/** Service type without underscore, e.g. `'http'`. Defaults to `'http'` */
type?: string
/** Defaults to `'tcp'` */
protocol?: string
/** Defaults to `'local.'` */
domain?: string
/** Android only, defaults to `NSD` */
implType?: ImplType
/** Seconds before rejecting with code `'TIMEOUT'`, defaults to `5` */
timeout?: number
/** Network interface to resolve on, all of them by default */
networkInterface?: string
}
/**
* Errors emitted by the `error` event
*
* - `DNSSD`: DNSServiceErrorType, on iOS and from Android's embedded mDNSResponder,
* e.g. `-65555` type missing from NSBonjourServices, `-65570` Local Network access denied
* - `NsdManager`: Android NsdManager failure code
* - `Windows`: Win32 / DNS_STATUS code from the Windows DNS-SD functions
* - `RNZeroconf`: the library's own errors, e.g. `'EXCEPTION'`, `'TIMEOUT'`, `'UNKNOWN_INTERFACE'`
*/
export interface ZeroconfError extends Error {
code: number | string
domain: 'DNSSD' | 'NsdManager' | 'RNZeroconf' | 'Windows'
/** The service the error is about, when there is one */
serviceName?: string
}
export interface LocalNetworkAccessOptions {
/** Service type without underscore, must be listed in `NSBonjourServices`. Defaults to the first one listed */
type?: string
/** Defaults to `'tcp'` */
protocol?: string
/** iOS: seconds to wait for an answer, defaults to `5` */
timeout?: number
/** Android: request `ACCESS_LOCAL_NETWORK` when it is missing, defaults to `true` */
request?: boolean
}
/** Published services don't carry the address family fields */
export type PublishedService = Omit<Service, 'ipv4' | 'ipv6'>
export interface ZeroconfEvents {
start: () => void
stop: () => void
/** Service found, before resolution */
found: (name: string) => void
/** Service fully resolved with network info */
resolved: (service: Service) => void
/** Service removed from the network */
remove: (name: string) => void
/** Services or service types list changed */
update: () => void
/** Service type found by `scanServiceTypes` */
typeFound: (serviceType: ServiceType) => void
/** Service type no longer advertised, during `scanServiceTypes` */
typeRemove: (serviceType: ServiceType) => void
error: (error: ZeroconfError) => void
published: (service: PublishedService) => void
unpublished: (service: PublishedService) => void
}
/** A service type advertised on the network, e.g. `{ type: 'http', protocol: 'tcp' }` for `_http._tcp` */
export interface ServiceType {
type: string
protocol: 'tcp' | 'udp'
}
export interface ServiceTypesScanOptions {
/** Defaults to `local.` */
domain?: string
/** Android only, defaults to `ImplType.NSD`. With `NSD`, services other apps publish on the same phone are left out (this app's own are included), `DNSSD` includes them */
implType?: ImplType
/** Network interface to list the types on, all of them by default */
networkInterface?: string
}
/** Extends the `events` package EventEmitter */
export default class Zeroconf {
constructor()
on<E extends keyof ZeroconfEvents>(event: E, listener: ZeroconfEvents[E]): this
once<E extends keyof ZeroconfEvents>(event: E, listener: ZeroconfEvents[E]): this
addListener<E extends keyof ZeroconfEvents>(event: E, listener: ZeroconfEvents[E]): this
removeListener<E extends keyof ZeroconfEvents>(event: E, listener: ZeroconfEvents[E]): this
off<E extends keyof ZeroconfEvents>(event: E, listener: ZeroconfEvents[E]): this
removeAllListeners(event?: keyof ZeroconfEvents): this
/** Adds a listener and returns a function that removes it */
subscribe<E extends keyof ZeroconfEvents>(event: E, listener: ZeroconfEvents[E]): () => void
listenerCount(event: keyof ZeroconfEvents): number
/** Add the native event listeners (called automatically in the constructor) */
addDeviceListeners(): void
/** Remove the native event listeners, call it when you are done with the instance */
removeDeviceListeners(): void
/** All the services found so far, keyed by name */
getServices(): Record<string, Service>
/** Service types found by `scanServiceTypes` */
getServiceTypes(): ServiceType[]
/**
* Scan for the service types advertised on the network, emits `typeFound` and `typeRemove`.
* On iOS it requires the `com.apple.developer.networking.multicast` entitlement.
*/
scanServiceTypes(options?: ServiceTypesScanOptions): void
/** Scan for services, defaults to `_http._tcp.` on the `local.` domain */
scan(options?: ScanOptions): void
/** @deprecated Use `scan({ type, protocol, domain, implType })` */
scan(type?: string, protocol?: string, domain?: string, implType?: ImplType): void
/**
* Checks the Local Network permission. iOS can show the permission prompt when it hasn't been answered yet.
* Android 17 (API 37): apps targeting API 37 need `ACCESS_LOCAL_NETWORK`, requested when missing unless
* `request` is `false`. Resolves `'granted'` when there is nothing to grant.
*/
checkLocalNetworkAccess(
options?: LocalNetworkAccessOptions,
): Promise<'granted' | 'denied' | 'unknown'>
/**
* Stop the current scan
* @param implType Android only, defaults to the implementation used by the last scan
*/
stop(implType?: ImplType): void
/**
* Publish a service. Resolves with the published service once it is advertised, its name can differ
* from the requested one when that name is already taken. Rejects with a `ZeroconfError`.
*/
publishService(options: PublishOptions): Promise<PublishedService>
/** @deprecated Use `publishService({ type, protocol, domain, name, port, txt, implType })` */
publishService(
type: string,
protocol: string,
domain: string | undefined,
name: string,
port: number,
txt?: TxtRecord,
implType?: ImplType,
): Promise<PublishedService>
/**
* Unpublish a service. Resolves once it is no longer advertised, rejects with a `ZeroconfError`
* (e.g. code `'NOT_PUBLISHED'`).
* @param implType Android only, defaults to the implementation the service was published with
*/
unpublishService(name: string, implType?: ImplType): Promise<PublishedService | null>
/**
* Replace the TXT record of a published service, resolves with the updated service.
* Android `NSD` has no update: the service is published again under the same name.
*/
updateService(name: string, options: UpdateOptions): Promise<PublishedService>
/** Resolve one service by name without scanning. Rejects with code `'TIMEOUT'` when it doesn't answer in time */
resolveService(options: ResolveOptions): Promise<Service>
}
export interface UseZeroconfOptions extends ScanOptions {
/** Scan only while true, defaults to `true` */
enabled?: boolean
}
export interface UseZeroconfResult {
/** Resolved services, updated as they are found, resolved and removed */
services: Service[]
isScanning: boolean
/** Last error, reset when a new scan starts */
error: ZeroconfError | null
stop(): void
/** Clears the services and scans again */
restart(): void
}
/**
* Scans while mounted and returns the resolved services. Scans again when the options change,
* stops and cleans up on unmount. Each hook runs its own scan, several can run at once.
*/
export function useZeroconf(options?: UseZeroconfOptions): UseZeroconfResult
export interface UseServiceTypesOptions extends ServiceTypesScanOptions {
/** Scan only while true, defaults to `true` */
enabled?: boolean
}
export interface UseServiceTypesResult {
/** Service types advertised on the network, updated as they appear and disappear */
serviceTypes: ServiceType[]
isScanning: boolean
/** Last error, reset when a new scan starts */
error: ZeroconfError | null
stop(): void
/** Clears the service types and scans again */
restart(): void
}
/** Lists the service types advertised on the network while mounted */
export function useServiceTypes(options?: UseServiceTypesOptions): UseServiceTypesResult