UNPKG

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