microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
db80cd4e10b83524bc77c15018effab72cbabeb8

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/typings/websocket/websocket.d.ts

658lines · modecode

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