|
| 1 | +/// <reference types="node" /> |
| 2 | +export default class UdpSocket extends EventEmitter { |
| 3 | + /** |
| 4 | + * @param {{ type: string; reusePort?: boolean; debug?: boolean; }} options |
| 5 | + * @param {((...args: any[]) => void) | undefined} [onmessage] |
| 6 | + */ |
| 7 | + constructor(options: { |
| 8 | + type: string; |
| 9 | + reusePort?: boolean; |
| 10 | + debug?: boolean; |
| 11 | + }, onmessage?: ((...args: any[]) => void) | undefined); |
| 12 | + type: string; |
| 13 | + reusePort: boolean | undefined; |
| 14 | + debugEnabled: boolean | undefined; |
| 15 | + /** @private */ |
| 16 | + private _destroyed; |
| 17 | + /** @private */ |
| 18 | + private _id; |
| 19 | + /** @private */ |
| 20 | + private _state; |
| 21 | + /** @private */ |
| 22 | + private _address; |
| 23 | + /** @private */ |
| 24 | + private _port; |
| 25 | + /** @private */ |
| 26 | + private _subscription; |
| 27 | + /** |
| 28 | + * @private |
| 29 | + */ |
| 30 | + private _debug; |
| 31 | + /** |
| 32 | + * For UDP sockets, causes the `UdpSocket` to listen for datagram messages on a named `port` |
| 33 | + * and optional `address`. If `port` is not specified or is `0`, the operating system will |
| 34 | + * attempt to bind to a random port. If `address` is not specified, the operating system |
| 35 | + * will attempt to listen on all addresses. Once binding is complete, a `'listening'` event |
| 36 | + * is emitted and the optional `callback` function is called. |
| 37 | + * |
| 38 | + * Specifying both a `'listening'` event listener and passing a callback to the `socket.bind()` |
| 39 | + * method is not harmful but not very useful. |
| 40 | + * |
| 41 | + * If binding fails, an `'error'` event is generated. In rare case (e.g. attempting to bind |
| 42 | + * with a closed socket), an `Error` may be thrown. |
| 43 | + * |
| 44 | + * @param {any[]} args |
| 45 | + */ |
| 46 | + bind(...args: any[]): void; |
| 47 | + /** |
| 48 | + * Close the underlying socket and stop listening for data on it. If a callback is provided, |
| 49 | + * it is added as a listener for the `'close'` event. |
| 50 | + * |
| 51 | + * @param {(...args: any[]) => void} callback |
| 52 | + */ |
| 53 | + close(callback?: (...args: any[]) => void): NodeJS.Immediate | undefined; |
| 54 | + /** |
| 55 | + * NOT IMPLEMENTED |
| 56 | + * |
| 57 | + * @deprecated |
| 58 | + * @param {number} port |
| 59 | + * @param {string} address |
| 60 | + * @param {(...args: any[]) => void} callback |
| 61 | + */ |
| 62 | + connect(port: number, address: string, callback: (...args: any[]) => void): void; |
| 63 | + /** |
| 64 | + * NOT IMPLEMENTED |
| 65 | + * |
| 66 | + * @deprecated |
| 67 | + */ |
| 68 | + disconnect(): void; |
| 69 | + /** |
| 70 | + * @private |
| 71 | + * @param {{ data: string; address: string; port: number; }} info |
| 72 | + */ |
| 73 | + private _onReceive; |
| 74 | + /** |
| 75 | + * Broadcasts a datagram on the socket. For connectionless sockets, the |
| 76 | + * destination `port` and `address` must be specified. Connected sockets, |
| 77 | + * on the other hand, will use their associated remote endpoint, |
| 78 | + * so the `port` and `address` arguments must not be set. |
| 79 | + * |
| 80 | + * The `msg` argument contains the message to be sent. Depending on its type, |
| 81 | + * different behavior can apply. If `msg` is a Buffer, any `TypedArray` or a `DataView`, |
| 82 | + * the `offset` and `length` specify the offset within the `Buffer` where the message |
| 83 | + * begins and the number of bytes in the message, respectively. If `msg` is a |
| 84 | + * `string`, then it is automatically converted to a `Buffer` with `'utf8'` encoding. |
| 85 | + * With messages that contain multi-byte characters, `offset` and `length` will be |
| 86 | + * calculated with respect to byte length and not the character position. |
| 87 | + * If `msg` is an array, `offset` and `length` must not be specified. |
| 88 | + * |
| 89 | + * The `address` argument is a string. If the value of `address` is a host name, |
| 90 | + * DNS will be used to resolve the address of the host. If `address` is not provided |
| 91 | + * or otherwise falsy, `'127.0.0.1'` (for `udp4` sockets) or `'::1'` |
| 92 | + * (for `udp6` sockets) will be used by default. |
| 93 | + * |
| 94 | + * If the socket has not been previously bound with a call to `bind`, it's |
| 95 | + * assigned a random port number and bound to the "all interfaces" address |
| 96 | + * (`'0.0.0.0'` for `udp4` sockets, `'::0'` for `udp6` sockets). |
| 97 | + * |
| 98 | + * An optional `callback` function may be specified to as a way of |
| 99 | + * reporting DNS errors or for determining when it is safe to reuse the |
| 100 | + * `buf` object. |
| 101 | + * |
| 102 | + * The only way to know for sure that the datagram has been sent is by |
| 103 | + * using a `callback`. If an error occurs and a `callback` is given, |
| 104 | + * the `error` will be passed as the first argument to the `callback`. |
| 105 | + * If a `callback` is not given, the error is emitted as an `'error'` |
| 106 | + * event on the `socket` object. |
| 107 | + * |
| 108 | + * Offset and length are optional but both _must_ be set if either are used. |
| 109 | + * They are supported only when the first argument is a `Buffer`, |
| 110 | + * a `TypedArray`, or a `DataView`. |
| 111 | + * |
| 112 | + * This method throws `ERR_SOCKET_BAD_PORT` if called on an unbound socket. |
| 113 | + * |
| 114 | + * @param {string | Buffer | Uint8Array | Array<any>} msg Message to be sent. |
| 115 | + * @param {number} [offset] Offset in the buffer where the message starts. |
| 116 | + * @param {number} [length] Number of bytes in the message. |
| 117 | + * @param {number} [port] Destination port. |
| 118 | + * @param {string} [address] Destination host name or IP address. |
| 119 | + * @param {(error?: Error) => void} [callback] Called when the message has been sent. |
| 120 | + */ |
| 121 | + send(msg: string | Buffer | Uint8Array | Array<any>, offset?: number | undefined, length?: number | undefined, port?: number | undefined, address?: string | undefined, callback?: ((error?: Error | undefined) => void) | undefined): void; |
| 122 | + /** |
| 123 | + * @private |
| 124 | + * @param {string | Buffer | Uint8Array | Array<any>} msg |
| 125 | + * @param {BufferEncoding} [encoding] |
| 126 | + */ |
| 127 | + private _generateSendBuffer; |
| 128 | + /** |
| 129 | + * Returns an object containing the address information for a socket. |
| 130 | + * For UDP sockets, this object will contain `address`, `family` and `port` properties. |
| 131 | + */ |
| 132 | + address(): { |
| 133 | + address: string; |
| 134 | + port: number; |
| 135 | + family: string; |
| 136 | + }; |
| 137 | + /** |
| 138 | + * Sets or clears the `SO_BROADCAST` socket option. When set to `true`, |
| 139 | + * UDP packets may be sent to a local interface's broadcast address. |
| 140 | + * |
| 141 | + * This method throws `EBADF` if called on an unbound socket. |
| 142 | + * |
| 143 | + * @param {boolean} flag |
| 144 | + */ |
| 145 | + setBroadcast(flag: boolean): void; |
| 146 | + /** |
| 147 | + * NOT IMPLEMENTED |
| 148 | + * |
| 149 | + * @deprecated |
| 150 | + * @param {string} multicastInterface |
| 151 | + */ |
| 152 | + setMulticastInterface(multicastInterface: string): void; |
| 153 | + /** |
| 154 | + * NOT IMPLEMENTED |
| 155 | + * |
| 156 | + * @deprecated |
| 157 | + * @param {boolean} flag |
| 158 | + */ |
| 159 | + setMulticastLoopback(flag: boolean): void; |
| 160 | + /** |
| 161 | + * NOT IMPLEMENTED |
| 162 | + * |
| 163 | + * @deprecated |
| 164 | + * @param {number} ttl |
| 165 | + */ |
| 166 | + setMulticastTTL(ttl: number): void; |
| 167 | + /** |
| 168 | + * NOT IMPLEMENTED |
| 169 | + * |
| 170 | + * @deprecated |
| 171 | + * @param {number} size |
| 172 | + */ |
| 173 | + setRecvBufferSize(size: number): void; |
| 174 | + /** |
| 175 | + * NOT IMPLEMENTED |
| 176 | + * |
| 177 | + * @deprecated |
| 178 | + * @param {number} size |
| 179 | + */ |
| 180 | + setSendBufferSize(size: number): void; |
| 181 | + /** |
| 182 | + * NOT IMPLEMENTED |
| 183 | + * |
| 184 | + * @deprecated |
| 185 | + * @param {number} ttl |
| 186 | + */ |
| 187 | + setTTL(ttl: number): void; |
| 188 | + /** |
| 189 | + * NOT IMPLEMENTED |
| 190 | + * |
| 191 | + * @deprecated |
| 192 | + */ |
| 193 | + unref(): void; |
| 194 | + /** |
| 195 | + * Tells the kernel to join a multicast group at the given `multicastAddress` and |
| 196 | + * `multicastInterface` using the `IP_ADD_MEMBERSHIP` socket option. |
| 197 | + * |
| 198 | + * If the `multicastInterface` argument is not specified, the operating system will |
| 199 | + * choose one interface and will add membership to it. To add membership to every |
| 200 | + * available interface, call `addMembership` multiple times, once per interface. |
| 201 | + * |
| 202 | + * @param {string} multicastAddress |
| 203 | + * @param {string} [multicastInterface] |
| 204 | + */ |
| 205 | + addMembership(multicastAddress: string, multicastInterface?: string | undefined): void; |
| 206 | + /** |
| 207 | + * NOT IMPLEMENTED |
| 208 | + * |
| 209 | + * @deprecated |
| 210 | + * @param {string} sourceAddress |
| 211 | + * @param {string} groupAddress |
| 212 | + * @param {string} [multicastInterface] |
| 213 | + */ |
| 214 | + addSourceSpecificMembership(sourceAddress: string, groupAddress: string, multicastInterface?: string | undefined): void; |
| 215 | + /** |
| 216 | + * Instructs the kernel to leave a multicast group at `multicastAddress` using the |
| 217 | + * `IP_DROP_MEMBERSHIP` socket option. This method is automatically called by the |
| 218 | + * kernel when the socket is closed or the process terminates, so most apps will |
| 219 | + * never have reason to call this. |
| 220 | + * |
| 221 | + * If `multicastInterface` is not specified, the operating system will attempt to |
| 222 | + * drop membership on all valid interfaces. |
| 223 | + * |
| 224 | + * @param {string} multicastAddress |
| 225 | + * @param {string} [multicastInterface] |
| 226 | + */ |
| 227 | + dropMembership(multicastAddress: string, multicastInterface?: string | undefined): void; |
| 228 | + /** |
| 229 | + * NOT IMPLEMENTED |
| 230 | + * |
| 231 | + * @deprecated |
| 232 | + * @param {string} sourceAddress |
| 233 | + * @param {string} groupAddress |
| 234 | + * @param {string} [multicastInterface] |
| 235 | + */ |
| 236 | + dropSourceSpecificMembership(sourceAddress: string, groupAddress: string, multicastInterface?: string | undefined): void; |
| 237 | + /** |
| 238 | + * NOT IMPLEMENTED |
| 239 | + * |
| 240 | + * @deprecated |
| 241 | + */ |
| 242 | + getRecvBufferSize(): void; |
| 243 | + /** |
| 244 | + * NOT IMPLEMENTED |
| 245 | + * |
| 246 | + * @deprecated |
| 247 | + */ |
| 248 | + getSendBufferSize(): void; |
| 249 | + /** |
| 250 | + * NOT IMPLEMENTED |
| 251 | + * |
| 252 | + * @deprecated |
| 253 | + */ |
| 254 | + ref(): void; |
| 255 | + /** |
| 256 | + * NOT IMPLEMENTED |
| 257 | + * |
| 258 | + * @deprecated |
| 259 | + */ |
| 260 | + remoteAddress(): void; |
| 261 | +} |
| 262 | +import { EventEmitter } from "node/events"; |
| 263 | +import { Buffer } from "buffer"; |
0 commit comments