UNPKG

7.27 kBTypeScriptView Raw
1declare module 'node:dgram' {
2 export * from 'dgram';
3}
4
5declare module 'dgram' {
6 import { AddressInfo } from 'node:net';
7 import * as dns from 'node:dns';
8 import EventEmitter = require('node:events');
9
10 interface RemoteInfo {
11 address: string;
12 family: 'IPv4' | 'IPv6';
13 port: number;
14 size: number;
15 }
16
17 interface BindOptions {
18 port?: number;
19 address?: string;
20 exclusive?: boolean;
21 fd?: number;
22 }
23
24 type SocketType = "udp4" | "udp6";
25
26 interface SocketOptions {
27 type: SocketType;
28 reuseAddr?: boolean;
29 /**
30 * @default false
31 */
32 ipv6Only?: boolean;
33 recvBufferSize?: number;
34 sendBufferSize?: number;
35 lookup?: (hostname: string, options: dns.LookupOneOptions, callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void) => void;
36 }
37
38 function createSocket(type: SocketType, callback?: (msg: Buffer, rinfo: RemoteInfo) => void): Socket;
39 function createSocket(options: SocketOptions, callback?: (msg: Buffer, rinfo: RemoteInfo) => void): Socket;
40
41 class Socket extends EventEmitter {
42 addMembership(multicastAddress: string, multicastInterface?: string): void;
43 address(): AddressInfo;
44 bind(port?: number, address?: string, callback?: () => void): void;
45 bind(port?: number, callback?: () => void): void;
46 bind(callback?: () => void): void;
47 bind(options: BindOptions, callback?: () => void): void;
48 close(callback?: () => void): void;
49 connect(port: number, address?: string, callback?: () => void): void;
50 connect(port: number, callback: () => void): void;
51 disconnect(): void;
52 dropMembership(multicastAddress: string, multicastInterface?: string): void;
53 getRecvBufferSize(): number;
54 getSendBufferSize(): number;
55 ref(): this;
56 remoteAddress(): AddressInfo;
57 send(msg: string | Uint8Array | ReadonlyArray<any>, port?: number, address?: string, callback?: (error: Error | null, bytes: number) => void): void;
58 send(msg: string | Uint8Array | ReadonlyArray<any>, port?: number, callback?: (error: Error | null, bytes: number) => void): void;
59 send(msg: string | Uint8Array | ReadonlyArray<any>, callback?: (error: Error | null, bytes: number) => void): void;
60 send(msg: string | Uint8Array, offset: number, length: number, port?: number, address?: string, callback?: (error: Error | null, bytes: number) => void): void;
61 send(msg: string | Uint8Array, offset: number, length: number, port?: number, callback?: (error: Error | null, bytes: number) => void): void;
62 send(msg: string | Uint8Array, offset: number, length: number, callback?: (error: Error | null, bytes: number) => void): void;
63 setBroadcast(flag: boolean): void;
64 setMulticastInterface(multicastInterface: string): void;
65 setMulticastLoopback(flag: boolean): void;
66 setMulticastTTL(ttl: number): void;
67 setRecvBufferSize(size: number): void;
68 setSendBufferSize(size: number): void;
69 setTTL(ttl: number): void;
70 unref(): this;
71 /**
72 * Tells the kernel to join a source-specific multicast channel at the given
73 * `sourceAddress` and `groupAddress`, using the `multicastInterface` with the
74 * `IP_ADD_SOURCE_MEMBERSHIP` socket option.
75 * If the `multicastInterface` argument
76 * is not specified, the operating system will choose one interface and will add
77 * membership to it.
78 * To add membership to every available interface, call
79 * `socket.addSourceSpecificMembership()` multiple times, once per interface.
80 */
81 addSourceSpecificMembership(sourceAddress: string, groupAddress: string, multicastInterface?: string): void;
82
83 /**
84 * Instructs the kernel to leave a source-specific multicast channel at the given
85 * `sourceAddress` and `groupAddress` using the `IP_DROP_SOURCE_MEMBERSHIP`
86 * socket option. This method is automatically called by the kernel when the
87 * socket is closed or the process terminates, so most apps will never have
88 * reason to call this.
89 *
90 * If `multicastInterface` is not specified, the operating system will attempt to
91 * drop membership on all valid interfaces.
92 */
93 dropSourceSpecificMembership(sourceAddress: string, groupAddress: string, multicastInterface?: string): void;
94
95 /**
96 * events.EventEmitter
97 * 1. close
98 * 2. connect
99 * 3. error
100 * 4. listening
101 * 5. message
102 */
103 addListener(event: string, listener: (...args: any[]) => void): this;
104 addListener(event: "close", listener: () => void): this;
105 addListener(event: "connect", listener: () => void): this;
106 addListener(event: "error", listener: (err: Error) => void): this;
107 addListener(event: "listening", listener: () => void): this;
108 addListener(event: "message", listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
109
110 emit(event: string | symbol, ...args: any[]): boolean;
111 emit(event: "close"): boolean;
112 emit(event: "connect"): boolean;
113 emit(event: "error", err: Error): boolean;
114 emit(event: "listening"): boolean;
115 emit(event: "message", msg: Buffer, rinfo: RemoteInfo): boolean;
116
117 on(event: string, listener: (...args: any[]) => void): this;
118 on(event: "close", listener: () => void): this;
119 on(event: "connect", listener: () => void): this;
120 on(event: "error", listener: (err: Error) => void): this;
121 on(event: "listening", listener: () => void): this;
122 on(event: "message", listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
123
124 once(event: string, listener: (...args: any[]) => void): this;
125 once(event: "close", listener: () => void): this;
126 once(event: "connect", listener: () => void): this;
127 once(event: "error", listener: (err: Error) => void): this;
128 once(event: "listening", listener: () => void): this;
129 once(event: "message", listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
130
131 prependListener(event: string, listener: (...args: any[]) => void): this;
132 prependListener(event: "close", listener: () => void): this;
133 prependListener(event: "connect", listener: () => void): this;
134 prependListener(event: "error", listener: (err: Error) => void): this;
135 prependListener(event: "listening", listener: () => void): this;
136 prependListener(event: "message", listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
137
138 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
139 prependOnceListener(event: "close", listener: () => void): this;
140 prependOnceListener(event: "connect", listener: () => void): this;
141 prependOnceListener(event: "error", listener: (err: Error) => void): this;
142 prependOnceListener(event: "listening", listener: () => void): this;
143 prependOnceListener(event: "message", listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
144 }
145}