forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.d.ts
658 lines (559 loc) · 25.6 KB
/
websocket.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// Type definitions for websocket
// Project: https://github.com/Worlize/WebSocket-Node
// Definitions by: Paul Loyd <https://github.com/loyd>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
declare module "websocket" {
import events = require('events');
import http = require('http');
import net = require('net');
import url = require('url');
export interface IStringified {
toString: (...args: any[]) => string;
}
export interface IConfig {
/**
* The maximum allowed received frame size in bytes.
* Single frame messages will also be limited to this maximum.
*/
maxReceivedFrameSize?: number;
/** The maximum allowed aggregate message size (for fragmented messages) in bytes */
maxReceivedMessageSize?: number;
/**
* Whether or not to fragment outgoing messages. If true, messages will be
* automatically fragmented into chunks of up to `fragmentationThreshold` bytes.
* @default true
*/
fragmentOutgoingMessages?: boolean;
/**
* The maximum size of a frame in bytes before it is automatically fragmented.
* @default 16KiB
*/
fragmentationThreshold?: number;
/**
* If true, fragmented messages will be automatically assembled and the full
* message will be emitted via a `message` event. If false, each frame will be
* emitted on the `connection` object via a `frame` event and the application
* will be responsible for aggregating multiple fragmented frames. Single-frame
* messages will emit a `message` event in addition to the `frame` event.
* @default true
*/
assembleFragments?: boolean;
/**
* The number of milliseconds to wait after sending a close frame for an
* `acknowledgement` to come back before giving up and just closing the socket.
* @default 5000
*/
closeTimeout?: number;
}
export interface IServerConfig extends IConfig {
/** The http server instance to attach to */
httpServer: http.Server;
/**
* The maximum allowed received frame size in bytes.
* Single frame messages will also be limited to this maximum.
* @default 64KiB
*/
maxReceivedFrameSize?: number;
/**
* The maximum allowed aggregate message size (for fragmented messages) in bytes.
* @default 1MiB
*/
maxReceivedMessageSize?: number;
/**
* If true, the server will automatically send a ping to all clients every
* `keepaliveInterval` milliseconds. Each client has an independent `keepalive`
* timer, which is reset when any data is received from that client.
* @default true
*/
keepalive?: boolean;
/**
* The interval in milliseconds to send `keepalive` pings to connected clients.
* @default 20000
*/
keepaliveInterval?: number;
/**
* If true, the server will consider any connection that has not received any
* data within the amount of time specified by `keepaliveGracePeriod` after a
* `keepalive` ping has been sent. Ignored if `keepalive` is false.
* @default true
*/
dropConnectionOnKeepaliveTimeout?: boolean;
/**
* The amount of time to wait after sending a `keepalive` ping before closing
* the connection if the connected peer does not respond. Ignored if `keepalive`
* or `dropConnectionOnKeepaliveTimeout` are false. The grace period timer is
* reset when any data is received from the client.
* @default 10000
*/
keepaliveGracePeriod?: number;
/**
* If this is true, websocket connections will be accepted regardless of the path
* and protocol specified by the client. The protocol accepted will be the first
* that was requested by the client.
* @default false
*/
autoAcceptConnections?: boolean;
/**
* The Nagle Algorithm makes more efficient use of network resources by introducing a
* small delay before sending small packets so that multiple messages can be batched
* together before going onto the wire. This however comes at the cost of latency.
* @default true
*/
disableNagleAlgorithm?: boolean;
}
export class server extends events.EventEmitter {
config: IServerConfig;
connections: connection[];
constructor(serverConfig?: IServerConfig);
/** Send binary message for each connection */
broadcast(data: Buffer): void;
/** Send UTF-8 message for each connection */
broadcast(data: IStringified): void;
/** Send binary message for each connection */
broadcastBytes(data: Buffer): void;
/** Send UTF-8 message for each connection */
broadcastUTF(data: IStringified): void;
/** Attach the `server` instance to a Node http.Server instance */
mount(serverConfig: IServerConfig): void;
/**
* Detach the `server` instance from the Node http.Server instance.
* All existing connections are left alone and will not be affected,
* but no new WebSocket connections will be accepted.
*/
unmount(): void;
/** Close all open WebSocket connections */
closeAllConnections(): void;
/** Close all open WebSocket connections and unmount the server */
shutDown(): void;
// Events
on(event: string, listener: () => void): server;
on(event: 'request', cb: (request: request) => void): server;
on(event: 'connect', cb: (connection: connection) => void): server;
on(event: 'close', cb: (connection: connection, reason: number, desc: string) => void): server;
addListener(event: string, listener: () => void): server;
addListener(event: 'request', cb: (request: request) => void): server;
addListener(event: 'connect', cb: (connection: connection) => void): server;
addListener(event: 'close', cb: (connection: connection, reason: number, desc: string) => void): server;
}
export interface ICookie {
name: string;
value: string;
path?: string;
domain?: string;
expires?: Date;
maxage?: number;
secure?: boolean;
httponly?: boolean;
}
export interface IExtension {
name: string;
value: string;
}
export class request extends events.EventEmitter {
/** A reference to the original Node HTTP request object */
httpRequest: http.ClientRequest;
/** This will include the port number if a non-standard port is used */
host: string;
/** A string containing the path that was requested by the client */
resource: string;
/** `Sec-WebSocket-Key` */
key: string;
/** Parsed resource, including the query string parameters */
resourceURL: url.Url;
/**
* Client's IP. If an `X-Forwarded-For` header is present, the value will be taken
* from that header to facilitate WebSocket servers that live behind a reverse-proxy
*/
remoteAddress: string;
/**
* If the client is a web browser, origin will be a string containing the URL
* of the page containing the script that opened the connection.
* If the client is not a web browser, origin may be `null` or "*".
*/
origin: string;
/** The version of the WebSocket protocol requested by the client */
webSocketVersion: number;
/** An array containing a list of extensions requested by the client */
requestedExtensions: any[];
cookies: ICookie[];
socket: net.Socket;
/**
* List of strings that indicate the subprotocols the client would like to speak.
* The server should select the best one that it can support from the list and
* pass it to the `accept` function when accepting the connection.
* Note that all the strings in the `requestedProtocols` array will have been
* converted to lower case.
*/
requestedProtocols: string[];
protocolFullCaseMap: {[key: string]: string};
constructor(socket: net.Socket, httpRequest: http.ClientRequest, config: IServerConfig);
/**
* After inspecting the `request` properties, call this function on the
* request object to accept the connection. If you don't have a particular subprotocol
* you wish to speak, you may pass `null` for the `acceptedProtocol` parameter.
*
* @param [acceptedProtocol] case-insensitive value that was requested by the client
*/
accept(acceptedProtocol?: string, allowedOrigin?: string, cookies?: ICookie[]): connection;
/**
* Reject connection.
* You may optionally pass in an HTTP Status code (such as 404) and a textual
* description that will be sent to the client in the form of an
* `X-WebSocket-Reject-Reason` header.
*/
reject(httpStatus?: number, reason?: string): void;
// Events
on(event: string, listener: () => void): request;
on(event: 'requestAccepted', cb: (connection: connection) => void): request;
on(event: 'requestRejected', cb: () => void): request;
addListener(event: string, listener: () => void): request;
addListener(event: 'requestAccepted', cb: (connection: connection) => void): request;
addListener(event: 'requestRejected', cb: () => void): request;
}
export interface IMessage {
type: string;
utf8Data?: string;
binaryData?: Buffer;
}
export interface IBufferList extends events.EventEmitter {
encoding: string;
length: number;
write(buf: Buffer): boolean;
end(buf: Buffer): void;
/**
* For each buffer, perform some action.
* If fn's result is a true value, cut out early.
*/
forEach(fn: (buf: Buffer) => boolean): void;
/** Create a single buffer out of all the chunks */
join(start: number, end: number): Buffer;
/** Join all the chunks to existing buffer */
joinInto(buf: Buffer, offset: number, start: number, end: number): Buffer;
/**
* Advance the buffer stream by `n` bytes.
* If `n` the aggregate advance offset passes the end of the buffer list,
* operations such as `take` will return empty strings until enough data is pushed.
*/
advance(n: number): IBufferList;
/**
* Take `n` bytes from the start of the buffers.
* If there are less than `n` bytes in all the buffers or `n` is undefined,
* returns the entire concatenated buffer string.
*/
take(n: number, encoding?: string): any;
take(encoding?: string): any;
// Events
on(event: string, listener: () => void): IBufferList;
on(event: 'advance', cb: (n: number) => void): IBufferList;
on(event: 'write', cb: (buf: Buffer) => void): IBufferList;
addListener(event: string, listener: () => void): IBufferList;
addListener(event: 'advance', cb: (n: number) => void): IBufferList;
addListener(event: 'write', cb: (buf: Buffer) => void): IBufferList;
}
class connection extends events.EventEmitter {
static CLOSE_REASON_NORMAL: number;
static CLOSE_REASON_GOING_AWAY: number;
static CLOSE_REASON_PROTOCOL_ERROR: number;
static CLOSE_REASON_UNPROCESSABLE_INPUT: number;
static CLOSE_REASON_RESERVED: number;
static CLOSE_REASON_NOT_PROVIDED: number;
static CLOSE_REASON_ABNORMAL: number;
static CLOSE_REASON_INVALID_DATA: number;
static CLOSE_REASON_POLICY_VIOLATION: number;
static CLOSE_REASON_MESSAGE_TOO_BIG: number;
static CLOSE_REASON_EXTENSION_REQUIRED: number;
/**
* After the connection is closed, contains a textual description of the reason for
* the connection closure, or `null` if the connection is still open.
*/
closeDescription: string;
/**
* After the connection is closed, contains the numeric close reason status code,
* or `-1` if the connection is still open.
*/
closeReasonCode: number;
/**
* The subprotocol that was chosen to be spoken on this connection. This field
* will have been converted to lower case.
*/
protocol: string;
config: IConfig;
socket: net.Socket;
maskOutgoingPackets: boolean;
maskBytes: Buffer;
frameHeader: Buffer;
bufferList: IBufferList;
currentFrame: frame;
fragmentationSize: number;
frameQueue: frame[];
state: string;
waitingForCloseResponse: boolean;
closeTimeout: number;
assembleFragments: number;
maxReceivedMessageSize: number;
outputPaused: boolean;
bytesWaitingToFlush: number;
socketHadError: boolean;
/** An array of extensions that were negotiated for this connection */
extensions: IExtension[];
/**
* The IP address of the remote peer as a string. In the case of a server,
* the `X-Forwarded-For` header will be respected and preferred for the purposes
* of populating this field. If you need to get to the actual remote IP address,
* `socket.remoteAddress` will provide it.
*/
remoteAddress: string;
/** The version of the WebSocket protocol requested by the client */
webSocketVersion: number;
/** Whether or not the connection is still connected. Read-only */
connected: boolean;
constructor(socket: net.Socket, extensions: IExtension[], protocol: string,
maskOutgoingPackets: boolean, config: IConfig);
/**
* Close the connection. A close frame will be sent to the remote peer indicating
* that we wish to close the connection, and we will then wait for up to
* `config.closeTimeout` milliseconds for an acknowledgment from the remote peer
* before terminating the underlying socket connection.
*/
close(): void;
/**
* Send a close frame to the remote peer and immediately close the socket without
* waiting for a response. This should generally be used only in error conditions.
*/
drop(reasonCode?: number, description?: string): void;
/**
* Immediately sends the specified string as a UTF-8 WebSocket message to the remote
* peer. If `config.fragmentOutgoingMessages` is true the message may be sent as
* multiple fragments if it exceeds `config.fragmentationThreshold` bytes.
*/
sendUTF(data: IStringified): void;
/**
* Immediately sends the specified Node Buffer object as a Binary WebSocket message
* to the remote peer. If config.fragmentOutgoingMessages is true the message may be
* sent as multiple fragments if it exceeds config.fragmentationThreshold bytes.
*/
sendBytes(buffer: Buffer): void;
/** Auto-detect the data type and send UTF-8 or Binary message */
send(data: Buffer): void;
send(data: IStringified): void;
/** Sends a ping frame. Ping frames must not exceed 125 bytes in length. */
ping(data: Buffer): void;
ping(data: IStringified): void;
/**
* Sends a pong frame. Pong frames may be sent unsolicited and such pong frames will
* trigger no action on the receiving peer. Pong frames sent in response to a ping
* frame must mirror the payload data of the ping frame exactly.
* The `connection` object handles this internally for you, so there should
* be no need to use this method to respond to pings.
* Pong frames must not exceed 125 bytes in length.
*/
pong(buffer: Buffer): void;
/**
* Serializes a `frame` object into binary data and immediately sends it to
* the remote peer. This is an advanced function, requiring you to manually compose
* your own `frame`. You should probably use sendUTF or sendBytes instead.
*/
sendFrame(frame: frame): void;
/** Set or reset the `keepalive` timer when data is received */
setKeepaliveTimer(): void;
setGracePeriodTimer(): void;
setCloseTimer(): void;
clearCloseTimer(): void;
processFrame(frame: frame): void;
fragmentAndSend(frame: frame, cb?: (err: Error) => void): void;
sendCloseFrame(reasonCode: number, reasonText: string, force: boolean): void;
sendCloseFrame(): void;
sendFrame(frame: frame, force: boolean, cb?: (msg: string) => void): void;
sendFrame(frame: frame, cb?: (msg: string) => void): void;
// Events
on(event: string, listener: () => void): connection;
on(event: 'message', cb: (data: IMessage) => void): connection;
on(event: 'frame', cb: (frame: frame) => void): connection;
on(event: 'close', cb: (code: number, desc: string) => void): connection;
on(event: 'error', cb: (err: Error) => void): connection;
addListener(event: string, listener: () => void): connection;
addListener(event: 'message', cb: (data: IMessage) => void): connection;
addListener(event: 'frame', cb: (frame: frame) => void): connection;
addListener(event: 'close', cb: (code: number, desc: string) => void): connection;
addListener(event: 'error', cb: (err: Error) => void): connection;
}
class frame {
/** Whether or not this is last frame in a fragmentation sequence */
fin: boolean;
/**
* Represents the RSV1 field in the framing. Setting this to true will result in
* a Protocol Error on the receiving peer.
*/
rsv1: boolean;
/**
* Represents the RSV1 field in the framing. Setting this to true will result in
* a Protocol Error on the receiving peer.
*/
rsv2: boolean;
/**
* Represents the RSV1 field in the framing. Setting this to true will result in
* a Protocol Error on the receiving peer.
*/
rsv3: boolean;
/**
* Whether or not this frame is (or should be) masked. For outgoing frames, when
* connected as a client, this flag is automatically forced to true by `connection`.
* Outgoing frames sent from the server-side of a connection are not masked.
*/
mask: number;
/**
* Identifies which kind of frame this is.
*
* Hex - Dec - Description
* 0x00 - 0 - Continuation
* 0x01 - 1 - Text Frame
* 0x02 - 2 - Binary Frame
* 0x08 - 8 - Close Frame
* 0x09 - 9 - Ping Frame
* 0x0A - 10 - Pong Frame
*/
opcode: number;
/**
* Identifies the length of the payload data on a received frame.
* When sending a frame, will be automatically calculated from `binaryPayload` object.
*/
length: number;
/**
* The binary payload data.
* Even text frames are sent with a Buffer providing the binary payload data.
*/
binaryPayload: Buffer;
maskBytes: Buffer;
frameHeader: Buffer;
config: IConfig;
maxReceivedFrameSize: number;
protocolError: boolean;
frameTooLarge: boolean;
invalidCloseFrameLength: boolean;
closeStatus: number;
addData(bufferList: IBufferList): boolean;
throwAwayPayload(bufferList: IBufferList): boolean;
toBuffer(nullMask: boolean): Buffer;
}
export interface IClientConfig extends IConfig {
/**
* Which version of the WebSocket protocol to use when making the connection.
* Currently supported values are 8 and 13. This option will be removed once the
* protocol is finalized by the IETF It is only available to ease the transition
* through the intermediate draft protocol versions. The only thing this affects
* the name of the Origin header.
* @default 13
*/
webSocketVersion?: number;
/**
* The maximum allowed received frame size in bytes.
* Single frame messages will also be limited to this maximum.
* @default 1MiB
*/
maxReceivedFrameSize?: number;
/**
* The maximum allowed aggregate message size (for fragmented messages) in bytes.
* @default 8MiB
*/
maxReceivedMessageSize?: number;
}
class client extends events.EventEmitter {
protocols: string[];
origin: string;
url: url.Url;
secure: boolean;
socket: net.Socket;
response: http.ClientResponse;
constructor(clientConfig?: IClientConfig);
/**
* Establish a connection. The remote server will select the best subprotocol that
* it supports and send that back when establishing the connection.
*
* @param [origin] can be used in user-agent scenarios to identify the page containing
* any scripting content that caused the connection to be requested.
* @param requestUrl should be a standard websocket url
*/
connect(requestUrl: url.Url, protocols?: string[], origin?: string, headers?: any[]): void;
connect(requestUrl: string, protocols?: string[], origin?: string, headers?: any[]): void;
connect(requestUrl: url.Url, protocols?: string, origin?: string, headers?: any[]): void;
connect(requestUrl: string, protocols?: string, origin?: string, headers?: any[]): void;
// Events
on(event: string, listener: () => void): client;
on(event: 'connect', cb: (connection: connection) => void): client;
on(event: 'connectFailed', cb: (err: Error) => void): client;
addListener(event: string, listener: () => void): client;
addListener(event: 'connect', cb: (connection: connection) => void): client;
addListener(event: 'connectFailed', cb: (err: Error) => void): client;
}
class routerRequest extends events.EventEmitter {
/** A reference to the original Node HTTP request object */
httpRequest: http.ClientRequest;
/** A string containing the path that was requested by the client */
resource: string;
/** Parsed resource, including the query string parameters */
resourceURL: url.Url;
/**
* Client's IP. If an `X-Forwarded-For` header is present, the value will be taken
* from that header to facilitate WebSocket servers that live behind a reverse-proxy
*/
remoteAddress: string;
/**
* If the client is a web browser, origin will be a string containing the URL
* of the page containing the script that opened the connection.
* If the client is not a web browser, origin may be `null` or "*".
*/
origin: string;
/** The version of the WebSocket protocol requested by the client */
webSocketVersion: number;
/** An array containing a list of extensions requested by the client */
requestedExtensions: any[];
cookies: ICookie[];
constructor(webSocketRequest: request, resolvedProtocol: string);
/**
* After inspecting the `request` properties, call this function on the
* request object to accept the connection. If you don't have a particular subprotocol
* you wish to speak, you may pass `null` for the `acceptedProtocol` parameter.
*
* @param [acceptedProtocol] case-insensitive value that was requested by the client
*/
accept(acceptedProtocol?: string, allowedOrigin?: string, cookies?: ICookie[]): connection;
/**
* Reject connection.
* You may optionally pass in an HTTP Status code (such as 404) and a textual
* description that will be sent to the client in the form of an
* `X-WebSocket-Reject-Reason` header.
*/
reject(httpStatus?: number, reason?: string): void;
// Events
on(event: string, listener: () => void): request;
on(event: 'requestAccepted', cb: (connection: connection) => void): request;
on(event: 'requestRejected', cb: () => void): request;
addListener(event: string, listener: () => void): request;
addListener(event: 'requestAccepted', cb: (connection: connection) => void): request;
addListener(event: 'requestRejected', cb: () => void): request;
}
interface IRouterConfig {
/*
* The WebSocketServer instance to attach to.
*/
server: server
}
class router extends events.EventEmitter {
constructor(config?: IRouterConfig);
/** Attach to WebSocket server */
attachServer(server: server): void;
/** Detach from WebSocket server */
detachServer(): void;
mount(path: string, cb: (request: routerRequest) => void): void;
mount(path: string, protocol: string, cb: (request: routerRequest) => void): void;
mount(path: RegExp, cb: (request: routerRequest) => void): void;
mount(path: RegExp, protocol: string, cb: (request: routerRequest) => void): void;
unmount(path: string, protocol?: string): void;
unmount(path: RegExp, protocol?: string): void;
}
export var version: string;
export var constants: {
DEBUG: boolean;
};
}