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 | declare module 'dgram' {
|
28 | import { AddressInfo } from 'node:net';
|
29 | import * as dns from 'node:dns';
|
30 | import { EventEmitter, Abortable } from 'node:events';
|
31 | interface RemoteInfo {
|
32 | address: string;
|
33 | family: 'IPv4' | 'IPv6';
|
34 | port: number;
|
35 | size: number;
|
36 | }
|
37 | interface BindOptions {
|
38 | port?: number | undefined;
|
39 | address?: string | undefined;
|
40 | exclusive?: boolean | undefined;
|
41 | fd?: number | undefined;
|
42 | }
|
43 | type SocketType = 'udp4' | 'udp6';
|
44 | interface SocketOptions extends Abortable {
|
45 | type: SocketType;
|
46 | reuseAddr?: boolean | undefined;
|
47 | |
48 |
|
49 |
|
50 | ipv6Only?: boolean | undefined;
|
51 | recvBufferSize?: number | undefined;
|
52 | sendBufferSize?: number | undefined;
|
53 | lookup?: ((hostname: string, options: dns.LookupOneOptions, callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void) => void) | undefined;
|
54 | }
|
55 | /**
|
56 | * Creates a `dgram.Socket` object. Once the socket is created, calling `socket.bind()` will instruct the socket to begin listening for datagram
|
57 | * messages. When `address` and `port` are not passed to `socket.bind()` the
|
58 | * method will bind the socket to the "all interfaces" address on a random port
|
59 | * (it does the right thing for both `udp4` and `udp6` sockets). The bound address
|
60 | * and port can be retrieved using `socket.address().address` and `socket.address().port`.
|
61 | *
|
62 | * If the `signal` option is enabled, calling `.abort()` on the corresponding`AbortController` is similar to calling `.close()` on the socket:
|
63 | *
|
64 | * ```js
|
65 | * const controller = new AbortController();
|
66 | * const { signal } = controller;
|
67 | * const server = dgram.createSocket({ type: 'udp4', signal });
|
68 | * server.on('message', (msg, rinfo) => {
|
69 | * console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
70 | * });
|
71 | * // Later, when you want to close the server.
|
72 | * controller.abort();
|
73 | * ```
|
74 | * @since v0.11.13
|
75 | * @param options Available options are:
|
76 | * @param callback Attached as a listener for `'message'` events. Optional.
|
77 | */
|
78 | function createSocket(type: SocketType, callback?: (msg: Buffer, rinfo: RemoteInfo) => void): Socket;
|
79 | function createSocket(options: SocketOptions, callback?: (msg: Buffer, rinfo: RemoteInfo) => void): Socket;
|
80 | /**
|
81 | * Encapsulates the datagram functionality.
|
82 | *
|
83 | * New instances of `dgram.Socket` are created using {@link createSocket}.
|
84 | * The `new` keyword is not to be used to create `dgram.Socket` instances.
|
85 | * @since v0.1.99
|
86 | */
|
87 | class Socket extends EventEmitter {
|
88 | /**
|
89 | * Tells the kernel to join a multicast group at the given `multicastAddress` and`multicastInterface` using the `IP_ADD_MEMBERSHIP` socket option. If the`multicastInterface` argument is not
|
90 | * specified, the operating system will choose
|
91 | * one interface and will add membership to it. To add membership to every
|
92 | * available interface, call `addMembership` multiple times, once per interface.
|
93 | *
|
94 | * When called on an unbound socket, this method will implicitly bind to a random
|
95 | * port, listening on all interfaces.
|
96 | *
|
97 | * When sharing a UDP socket across multiple `cluster` workers, the`socket.addMembership()` function must be called only once or an`EADDRINUSE` error will occur:
|
98 | *
|
99 | * ```js
|
100 | * const cluster = require('cluster');
|
101 | * const dgram = require('dgram');
|
102 | * if (cluster.isPrimary) {
|
103 | * cluster.fork(); // Works ok.
|
104 | * cluster.fork(); // Fails with EADDRINUSE.
|
105 | * } else {
|
106 | * const s = dgram.createSocket('udp4');
|
107 | * s.bind(1234, () => {
|
108 | * s.addMembership('224.0.0.114');
|
109 | * });
|
110 | * }
|
111 | * ```
|
112 | * @since v0.6.9
|
113 | */
|
114 | addMembership(multicastAddress: string, multicastInterface?: string): void;
|
115 | /**
|
116 | * Returns an object containing the address information for a socket.
|
117 | * For UDP sockets, this object will contain `address`, `family` and `port`properties.
|
118 | *
|
119 | * This method throws `EBADF` if called on an unbound socket.
|
120 | * @since v0.1.99
|
121 | */
|
122 | address(): AddressInfo;
|
123 | /**
|
124 | * For UDP sockets, causes the `dgram.Socket` to listen for datagram
|
125 | * messages on a named `port` and optional `address`. If `port` is not
|
126 | * specified or is `0`, the operating system will attempt to bind to a
|
127 | * random port. If `address` is not specified, the operating system will
|
128 | * attempt to listen on all addresses. Once binding is complete, a`'listening'` event is emitted and the optional `callback` function is
|
129 | * called.
|
130 | *
|
131 | * Specifying both a `'listening'` event listener and passing a`callback` to the `socket.bind()` method is not harmful but not very
|
132 | * useful.
|
133 | *
|
134 | * A bound datagram socket keeps the Node.js process running to receive
|
135 | * datagram messages.
|
136 | *
|
137 | * If binding fails, an `'error'` event is generated. In rare case (e.g.
|
138 | * attempting to bind with a closed socket), an `Error` may be thrown.
|
139 | *
|
140 | * Example of a UDP server listening on port 41234:
|
141 | *
|
142 | * ```js
|
143 | * const dgram = require('dgram');
|
144 | * const server = dgram.createSocket('udp4');
|
145 | *
|
146 | * server.on('error', (err) => {
|
147 | * console.log(`server error:\n${err.stack}`);
|
148 | * server.close();
|
149 | * });
|
150 | *
|
151 | * server.on('message', (msg, rinfo) => {
|
152 | * console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
153 | * });
|
154 | *
|
155 | * server.on('listening', () => {
|
156 | * const address = server.address();
|
157 | * console.log(`server listening ${address.address}:${address.port}`);
|
158 | * });
|
159 | *
|
160 | * server.bind(41234);
|
161 | * // Prints: server listening 0.0.0.0:41234
|
162 | * ```
|
163 | * @since v0.1.99
|
164 | * @param callback with no parameters. Called when binding is complete.
|
165 | */
|
166 | bind(port?: number, address?: string, callback?: () => void): void;
|
167 | bind(port?: number, callback?: () => void): void;
|
168 | bind(callback?: () => void): void;
|
169 | bind(options: BindOptions, callback?: () => void): void;
|
170 | /**
|
171 | * Close the underlying socket and stop listening for data on it. If a callback is
|
172 | * provided, it is added as a listener for the `'close'` event.
|
173 | * @since v0.1.99
|
174 | * @param callback Called when the socket has been closed.
|
175 | */
|
176 | close(callback?: () => void): void;
|
177 | /**
|
178 | * Associates the `dgram.Socket` to a remote address and port. Every
|
179 | * message sent by this handle is automatically sent to that destination. Also,
|
180 | * the socket will only receive messages from that remote peer.
|
181 | * Trying to call `connect()` on an already connected socket will result
|
182 | * in an `ERR_SOCKET_DGRAM_IS_CONNECTED` exception. If `address` is not
|
183 | * provided, `'127.0.0.1'` (for `udp4` sockets) or `'::1'` (for `udp6` sockets)
|
184 | * will be used by default. Once the connection is complete, a `'connect'` event
|
185 | * is emitted and the optional `callback` function is called. In case of failure,
|
186 | * the `callback` is called or, failing this, an `'error'` event is emitted.
|
187 | * @since v12.0.0
|
188 | * @param callback Called when the connection is completed or on error.
|
189 | */
|
190 | connect(port: number, address?: string, callback?: () => void): void;
|
191 | connect(port: number, callback: () => void): void;
|
192 | /**
|
193 | * A synchronous function that disassociates a connected `dgram.Socket` from
|
194 | * its remote address. Trying to call `disconnect()` on an unbound or already
|
195 | * disconnected socket will result in an `ERR_SOCKET_DGRAM_NOT_CONNECTED` exception.
|
196 | * @since v12.0.0
|
197 | */
|
198 | disconnect(): void;
|
199 | /**
|
200 | * Instructs the kernel to leave a multicast group at `multicastAddress` using the`IP_DROP_MEMBERSHIP` socket option. This method is automatically called by the
|
201 | * kernel when the socket is closed or the process terminates, so most apps will
|
202 | * never have reason to call this.
|
203 | *
|
204 | * If `multicastInterface` is not specified, the operating system will attempt to
|
205 | * drop membership on all valid interfaces.
|
206 | * @since v0.6.9
|
207 | */
|
208 | dropMembership(multicastAddress: string, multicastInterface?: string): void;
|
209 | /**
|
210 | * This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound socket.
|
211 | * @since v8.7.0
|
212 | * @return the `SO_RCVBUF` socket receive buffer size in bytes.
|
213 | */
|
214 | getRecvBufferSize(): number;
|
215 | /**
|
216 | * This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound socket.
|
217 | * @since v8.7.0
|
218 | * @return the `SO_SNDBUF` socket send buffer size in bytes.
|
219 | */
|
220 | getSendBufferSize(): number;
|
221 | /**
|
222 | * By default, binding a socket will cause it to block the Node.js process from
|
223 | * exiting as long as the socket is open. The `socket.unref()` method can be used
|
224 | * to exclude the socket from the reference counting that keeps the Node.js
|
225 | * process active. The `socket.ref()` method adds the socket back to the reference
|
226 | * counting and restores the default behavior.
|
227 | *
|
228 | * Calling `socket.ref()` multiples times will have no additional effect.
|
229 | *
|
230 | * The `socket.ref()` method returns a reference to the socket so calls can be
|
231 | * chained.
|
232 | * @since v0.9.1
|
233 | */
|
234 | ref(): this;
|
235 | /**
|
236 | * Returns an object containing the `address`, `family`, and `port` of the remote
|
237 | * endpoint. This method throws an `ERR_SOCKET_DGRAM_NOT_CONNECTED` exception
|
238 | * if the socket is not connected.
|
239 | * @since v12.0.0
|
240 | */
|
241 | remoteAddress(): AddressInfo;
|
242 | /**
|
243 | * Broadcasts a datagram on the socket.
|
244 | * For connectionless sockets, the destination `port` and `address` must be
|
245 | * specified. Connected sockets, on the other hand, will use their associated
|
246 | * remote endpoint, so the `port` and `address` arguments must not be set.
|
247 | *
|
248 | * The `msg` argument contains the message to be sent.
|
249 | * Depending on its type, different behavior can apply. If `msg` is a `Buffer`,
|
250 | * any `TypedArray` or a `DataView`,
|
251 | * the `offset` and `length` specify the offset within the `Buffer` where the
|
252 | * message begins and the number of bytes in the message, respectively.
|
253 | * If `msg` is a `String`, then it is automatically converted to a `Buffer`with `'utf8'` encoding. With messages that
|
254 | * contain multi-byte characters, `offset` and `length` will be calculated with
|
255 | * respect to `byte length` and not the character position.
|
256 | * If `msg` is an array, `offset` and `length` must not be specified.
|
257 | *
|
258 | * The `address` argument is a string. If the value of `address` is a host name,
|
259 | * DNS will be used to resolve the address of the host. If `address` is not
|
260 | * provided or otherwise falsy, `'127.0.0.1'` (for `udp4` sockets) or `'::1'`(for `udp6` sockets) will be used by default.
|
261 | *
|
262 | * If the socket has not been previously bound with a call to `bind`, the socket
|
263 | * is assigned a random port number and is bound to the "all interfaces" address
|
264 | * (`'0.0.0.0'` for `udp4` sockets, `'::0'` for `udp6` sockets.)
|
265 | *
|
266 | * An optional `callback` function may be specified to as a way of reporting
|
267 | * DNS errors or for determining when it is safe to reuse the `buf` object.
|
268 | * DNS lookups delay the time to send for at least one tick of the
|
269 | * Node.js event loop.
|
270 | *
|
271 | * The only way to know for sure that the datagram has been sent is by using a`callback`. If an error occurs and a `callback` is given, the error will be
|
272 | * passed as the first argument to the `callback`. If a `callback` is not given,
|
273 | * the error is emitted as an `'error'` event on the `socket` object.
|
274 | *
|
275 | * Offset and length are optional but both _must_ be set if either are used.
|
276 | * They are supported only when the first argument is a `Buffer`, a `TypedArray`,
|
277 | * or a `DataView`.
|
278 | *
|
279 | * This method throws `ERR_SOCKET_BAD_PORT` if called on an unbound socket.
|
280 | *
|
281 | * Example of sending a UDP packet to a port on `localhost`;
|
282 | *
|
283 | * ```js
|
284 | * const dgram = require('dgram');
|
285 | * const message = Buffer.from('Some bytes');
|
286 | * const client = dgram.createSocket('udp4');
|
287 | * client.send(message, 41234, 'localhost', (err) => {
|
288 | * client.close();
|
289 | * });
|
290 | * ```
|
291 | *
|
292 | * Example of sending a UDP packet composed of multiple buffers to a port on`127.0.0.1`;
|
293 | *
|
294 | * ```js
|
295 | * const dgram = require('dgram');
|
296 | * const buf1 = Buffer.from('Some ');
|
297 | * const buf2 = Buffer.from('bytes');
|
298 | * const client = dgram.createSocket('udp4');
|
299 | * client.send([buf1, buf2], 41234, (err) => {
|
300 | * client.close();
|
301 | * });
|
302 | * ```
|
303 | *
|
304 | * Sending multiple buffers might be faster or slower depending on the
|
305 | * application and operating system. Run benchmarks to
|
306 | * determine the optimal strategy on a case-by-case basis. Generally speaking,
|
307 | * however, sending multiple buffers is faster.
|
308 | *
|
309 | * Example of sending a UDP packet using a socket connected to a port on`localhost`:
|
310 | *
|
311 | * ```js
|
312 | * const dgram = require('dgram');
|
313 | * const message = Buffer.from('Some bytes');
|
314 | * const client = dgram.createSocket('udp4');
|
315 | * client.connect(41234, 'localhost', (err) => {
|
316 | * client.send(message, (err) => {
|
317 | * client.close();
|
318 | * });
|
319 | * });
|
320 | * ```
|
321 | * @since v0.1.99
|
322 | * @param msg Message to be sent.
|
323 | * @param offset Offset in the buffer where the message starts.
|
324 | * @param length Number of bytes in the message.
|
325 | * @param port Destination port.
|
326 | * @param address Destination host name or IP address.
|
327 | * @param callback Called when the message has been sent.
|
328 | */
|
329 | send(msg: string | Uint8Array | ReadonlyArray<any>, port?: number, address?: string, callback?: (error: Error | null, bytes: number) => void): void;
|
330 | send(msg: string | Uint8Array | ReadonlyArray<any>, port?: number, callback?: (error: Error | null, bytes: number) => void): void;
|
331 | send(msg: string | Uint8Array | ReadonlyArray<any>, callback?: (error: Error | null, bytes: number) => void): void;
|
332 | send(msg: string | Uint8Array, offset: number, length: number, port?: number, address?: string, callback?: (error: Error | null, bytes: number) => void): void;
|
333 | send(msg: string | Uint8Array, offset: number, length: number, port?: number, callback?: (error: Error | null, bytes: number) => void): void;
|
334 | send(msg: string | Uint8Array, offset: number, length: number, callback?: (error: Error | null, bytes: number) => void): void;
|
335 | /**
|
336 | * Sets or clears the `SO_BROADCAST` socket option. When set to `true`, UDP
|
337 | * packets may be sent to a local interface's broadcast address.
|
338 | *
|
339 | * This method throws `EBADF` if called on an unbound socket.
|
340 | * @since v0.6.9
|
341 | */
|
342 | setBroadcast(flag: boolean): void;
|
343 | /**
|
344 | * _All references to scope in this section are referring to[IPv6 Zone Indices](https:
|
345 | * 4007](https:
|
346 | * _with a scope index is written as `'IP%scope'` where scope is an interface name_
|
347 | * _or interface number._
|
348 | *
|
349 | * Sets the default outgoing multicast interface of the socket to a chosen
|
350 | * interface or back to system interface selection. The `multicastInterface` must
|
351 | * be a valid string representation of an IP from the socket's family.
|
352 | *
|
353 | * For IPv4 sockets, this should be the IP configured for the desired physical
|
354 | * interface. All packets sent to multicast on the socket will be sent on the
|
355 | * interface determined by the most recent successful use of this call.
|
356 | *
|
357 | * For IPv6 sockets, `multicastInterface` should include a scope to indicate the
|
358 | * interface as in the examples that follow. In IPv6, individual `send` calls can
|
359 | * also use explicit scope in addresses, so only packets sent to a multicast
|
360 | * address without specifying an explicit scope are affected by the most recent
|
361 | * successful use of this call.
|
362 | *
|
363 | * This method throws `EBADF` if called on an unbound socket.
|
364 | *
|
365 | * #### Example: IPv6 outgoing multicast interface
|
366 | *
|
367 | * On most systems, where scope format uses the interface name:
|
368 | *
|
369 | * ```js
|
370 | * const socket = dgram.createSocket('udp6');
|
371 | *
|
372 | * socket.bind(1234, () => {
|
373 | * socket.setMulticastInterface('::%eth1');
|
374 | * });
|
375 | * ```
|
376 | *
|
377 | * On Windows, where scope format uses an interface number:
|
378 | *
|
379 | * ```js
|
380 | * const socket = dgram.createSocket('udp6');
|
381 | *
|
382 | * socket.bind(1234, () => {
|
383 | * socket.setMulticastInterface('::%2');
|
384 | * });
|
385 | * ```
|
386 | *
|
387 | * #### Example: IPv4 outgoing multicast interface
|
388 | *
|
389 | * All systems use an IP of the host on the desired physical interface:
|
390 | *
|
391 | * ```js
|
392 | * const socket = dgram.createSocket('udp4');
|
393 | *
|
394 | * socket.bind(1234, () => {
|
395 | * socket.setMulticastInterface('10.0.0.2');
|
396 | * });
|
397 | * ```
|
398 | * @since v8.6.0
|
399 | */
|
400 | setMulticastInterface(multicastInterface: string): void;
|
401 | |
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 | setMulticastLoopback(flag: boolean): void;
|
409 | |
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | setMulticastTTL(ttl: number): void;
|
422 | |
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 | setRecvBufferSize(size: number): void;
|
430 | |
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | setSendBufferSize(size: number): void;
|
438 | |
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 | setTTL(ttl: number): void;
|
452 | |
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | unref(): this;
|
466 | |
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | addSourceSpecificMembership(sourceAddress: string, groupAddress: string, multicastInterface?: string): void;
|
477 | |
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 | dropSourceSpecificMembership(sourceAddress: string, groupAddress: string, multicastInterface?: string): void;
|
488 | |
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | addListener(event: string, listener: (...args: any[]) => void): this;
|
497 | addListener(event: 'close', listener: () => void): this;
|
498 | addListener(event: 'connect', listener: () => void): this;
|
499 | addListener(event: 'error', listener: (err: Error) => void): this;
|
500 | addListener(event: 'listening', listener: () => void): this;
|
501 | addListener(event: 'message', listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
|
502 | emit(event: string | symbol, ...args: any[]): boolean;
|
503 | emit(event: 'close'): boolean;
|
504 | emit(event: 'connect'): boolean;
|
505 | emit(event: 'error', err: Error): boolean;
|
506 | emit(event: 'listening'): boolean;
|
507 | emit(event: 'message', msg: Buffer, rinfo: RemoteInfo): boolean;
|
508 | on(event: string, listener: (...args: any[]) => void): this;
|
509 | on(event: 'close', listener: () => void): this;
|
510 | on(event: 'connect', listener: () => void): this;
|
511 | on(event: 'error', listener: (err: Error) => void): this;
|
512 | on(event: 'listening', listener: () => void): this;
|
513 | on(event: 'message', listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
|
514 | once(event: string, listener: (...args: any[]) => void): this;
|
515 | once(event: 'close', listener: () => void): this;
|
516 | once(event: 'connect', listener: () => void): this;
|
517 | once(event: 'error', listener: (err: Error) => void): this;
|
518 | once(event: 'listening', listener: () => void): this;
|
519 | once(event: 'message', listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
|
520 | prependListener(event: string, listener: (...args: any[]) => void): this;
|
521 | prependListener(event: 'close', listener: () => void): this;
|
522 | prependListener(event: 'connect', listener: () => void): this;
|
523 | prependListener(event: 'error', listener: (err: Error) => void): this;
|
524 | prependListener(event: 'listening', listener: () => void): this;
|
525 | prependListener(event: 'message', listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
|
526 | prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
527 | prependOnceListener(event: 'close', listener: () => void): this;
|
528 | prependOnceListener(event: 'connect', listener: () => void): this;
|
529 | prependOnceListener(event: 'error', listener: (err: Error) => void): this;
|
530 | prependOnceListener(event: 'listening', listener: () => void): this;
|
531 | prependOnceListener(event: 'message', listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
|
532 | }
|
533 | }
|
534 | declare module 'node:dgram' {
|
535 | export * from 'dgram';
|
536 | }
|
537 |
|
\ | No newline at end of file |