forked from thoov/mock-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
198 lines (169 loc) · 5.78 KB
/
index.d.ts
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
// Type definitions for Mock Socket 8.X+
// Project: Mock Socket
// Definitions by: Travis Hoover <https://github.com/thoov/mock-socket>
declare module 'mock-socket' {
type Room = string;
class EventTarget {
listeners: any;
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
dispatchEvent(evt: Event): boolean;
removeEventListener(type: string, listener?: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
}
interface WebSocketCallbackMap {
close: () => void;
error: (err: Error) => void;
message: (message: string | Blob | ArrayBuffer | ArrayBufferView) => void;
open: () => void;
}
interface OpenEvent {
type: string;
target: WebSocket;
}
interface ErrorEvent {
error: any;
message: string;
type: string;
target: WebSocket;
}
interface CloseEvent {
wasClean: boolean;
code: number;
reason: string;
type: string;
target: WebSocket;
}
type Data = string | Buffer | ArrayBuffer | Buffer[];
interface MessageEvent {
data: Data;
type: string;
target: WebSocket;
}
//
// https://html.spec.whatwg.org/multipage/web-sockets.html#websocket
//
class WebSocket extends EventTarget {
constructor(url?: string, protocols?: string|string[]);
static readonly CONNECTING: 0;
static readonly OPEN: 1;
static readonly CLOSING: 2;
static readonly CLOSED: 3;
readonly url: string;
readonly CONNECTING: 0;
readonly OPEN: 1;
readonly CLOSING: 2;
readonly CLOSED: 3;
readonly readyState: number;
readonly bufferedAmount: number;
onopen: (event: OpenEvent) => void;
onerror: (event: ErrorEvent) => void;
onclose: (event: CloseEvent) => void;
onmessage: (event: MessageEvent) => void;
readonly extensions: string;
readonly protocol: string;
close(code?: number, reason?: string): void;
binaryType: BinaryType;
send(data: string | Blob | ArrayBuffer | ArrayBufferView): void;
on<K extends keyof WebSocketCallbackMap>(type: K, callback: WebSocketCallbackMap[K]): void;
}
class Server extends EventTarget {
constructor(url: string, options?: ServerOptions);
readonly options?: ServerOptions;
start(): void;
stop(callback?: () => void): void;
on(type: string, callback: (socket: WebSocket) => void): void;
close(options?: CloseOptions): void;
emit(event: string, data: any, options?: EmitOptions): void;
clients(): WebSocket[];
to(room: any, broadcaster: any, broadcastList?: object): ToReturnObject;
in(any: any): ToReturnObject;
simulate(event: string): void;
public of(url: string): Server;
}
interface CloseOptions {
code: number;
reason: string;
wasClean: boolean;
}
interface EmitOptions {
websockets: WebSocket[];
}
interface ToReturnObject {
to: (chainedRoom: any, chainedBroadcaster: any) => ToReturnObject;
emit(event: Event, data: any): void;
}
export interface ServerOptions {
verifyClient?: () => boolean;
selectProtocol?: (protocols: string[]) => string | null;
normalizeSendData?: boolean
}
export interface Handshake {
/**
* The headers sent as part of the handshake
*/
headers: object;
/**
* The date of creation (as string)
*/
time: string;
/**
* The ip of the client
*/
address: string;
/**
* Whether the connection is cross-domain
*/
xdomain: boolean;
/**
* Whether the connection is secure
*/
secure: boolean;
/**
* The date of creation (as unix timestamp)
*/
issued: number;
/**
* The request URL string
*/
url: string;
/**
* The query object
*/
query: { [key:string]: string };
/**
* The auth object
*/
auth: object;
}
export interface EventsMap {
[event: string]: any;
}
export interface DefaultEventsMap {
[event: string]: (...args: any[]) => void;
}
type FallbackToUntypedListener<T> = [T] extends [never] ? (...args: any[]) => void : T;
export type EventNames<Map extends EventsMap> = keyof Map & (string | symbol);
export type EventParams<Map extends EventsMap, Ev extends EventNames<Map>> = Parameters<Map[Ev]>;
export type ReservedOrUserEventNames<ReservedEventsMap extends EventsMap, UserEvents extends EventsMap> = EventNames<ReservedEventsMap> | EventNames<UserEvents>;
export type ReservedOrUserListener<ReservedEvents extends EventsMap, UserEvents extends EventsMap, Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>> = FallbackToUntypedListener<Ev extends EventNames<ReservedEvents> ? ReservedEvents[Ev] : Ev extends EventNames<UserEvents> ? UserEvents[Ev] : never>;
interface SocketReservedEvents {
connect: () => void;
connect_error: (err: Error) => void;
disconnect: () => void;
}
class SocketIO<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> extends EventTarget {
id: string
handshake: Handshake;
constructor(url?: string, protocol?: string)
close(): this
disconnect(): this
emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): this;
send(...args: any[]): this;
on<Ev extends ReservedOrUserEventNames<SocketReservedEvents, ListenEvents>>(ev: Ev, listener: ReservedOrUserListener<SocketReservedEvents, ListenEvents, Ev>): this;
off<Ev extends ReservedOrUserEventNames<SocketReservedEvents, ListenEvents>>(ev: Ev, listener: ReservedOrUserListener<SocketReservedEvents, ListenEvents, Ev>): this;
join(rooms: Room | Array<Room>): Promise<void> | void;
leave(room: string): Promise<void> | void;
to(name: Room): this;
in(name: Room): this;
get broadcast(): this;
}
}