Skip to content

Commit b0ce60b

Browse files
saschanazsandersn
andcommitted
Add HTML Web sockets types (#803)
* Support new constructor() syntax * Add HTML Web sockets types Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent 3271ea0 commit b0ce60b

6 files changed

+154
-4
lines changed

baselines/dom.generated.d.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,11 +3672,18 @@ declare var ClipboardEvent: {
36723672

36733673
/** A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute. */
36743674
interface CloseEvent extends Event {
3675+
/**
3676+
* Returns the WebSocket connection close code provided by the server.
3677+
*/
36753678
readonly code: number;
3679+
/**
3680+
* Returns the WebSocket connection close reason provided by the server.
3681+
*/
36763682
readonly reason: string;
3683+
/**
3684+
* Returns true if the connection closed cleanly; false otherwise.
3685+
*/
36773686
readonly wasClean: boolean;
3678-
/** @deprecated */
3679-
initCloseEvent(typeArg: string, canBubbleArg: boolean, cancelableArg: boolean, wasCleanArg: boolean, codeArg: number, reasonArg: string): void;
36803687
}
36813688

36823689
declare var CloseEvent: {
@@ -18381,17 +18388,45 @@ interface WebSocketEventMap {
1838118388

1838218389
/** Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. */
1838318390
interface WebSocket extends EventTarget {
18391+
/**
18392+
* Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:
18393+
*
18394+
* Can be set, to change how binary data is returned. The default is "blob".
18395+
*/
1838418396
binaryType: BinaryType;
18397+
/**
18398+
* Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.
18399+
*
18400+
* If the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.)
18401+
*/
1838518402
readonly bufferedAmount: number;
18403+
/**
18404+
* Returns the extensions selected by the server, if any.
18405+
*/
1838618406
readonly extensions: string;
1838718407
onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
1838818408
onerror: ((this: WebSocket, ev: Event) => any) | null;
1838918409
onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
1839018410
onopen: ((this: WebSocket, ev: Event) => any) | null;
18411+
/**
18412+
* Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation.
18413+
*/
1839118414
readonly protocol: string;
18415+
/**
18416+
* Returns the state of the WebSocket object's connection. It can have the values described below.
18417+
*/
1839218418
readonly readyState: number;
18419+
/**
18420+
* Returns the URL that was used to establish the WebSocket connection.
18421+
*/
1839318422
readonly url: string;
18423+
/**
18424+
* Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.
18425+
*/
1839418426
close(code?: number, reason?: string): void;
18427+
/**
18428+
* Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView.
18429+
*/
1839518430
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
1839618431
readonly CLOSED: number;
1839718432
readonly CLOSING: number;

baselines/webworker.generated.d.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,18 @@ declare var Clients: {
896896

897897
/** A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute. */
898898
interface CloseEvent extends Event {
899+
/**
900+
* Returns the WebSocket connection close code provided by the server.
901+
*/
899902
readonly code: number;
903+
/**
904+
* Returns the WebSocket connection close reason provided by the server.
905+
*/
900906
readonly reason: string;
907+
/**
908+
* Returns true if the connection closed cleanly; false otherwise.
909+
*/
901910
readonly wasClean: boolean;
902-
/** @deprecated */
903-
initCloseEvent(typeArg: string, canBubbleArg: boolean, cancelableArg: boolean, wasCleanArg: boolean, codeArg: number, reasonArg: string): void;
904911
}
905912

906913
declare var CloseEvent: {
@@ -5276,17 +5283,45 @@ interface WebSocketEventMap {
52765283

52775284
/** Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. */
52785285
interface WebSocket extends EventTarget {
5286+
/**
5287+
* Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:
5288+
*
5289+
* Can be set, to change how binary data is returned. The default is "blob".
5290+
*/
52795291
binaryType: BinaryType;
5292+
/**
5293+
* Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.
5294+
*
5295+
* If the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.)
5296+
*/
52805297
readonly bufferedAmount: number;
5298+
/**
5299+
* Returns the extensions selected by the server, if any.
5300+
*/
52815301
readonly extensions: string;
52825302
onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
52835303
onerror: ((this: WebSocket, ev: Event) => any) | null;
52845304
onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
52855305
onopen: ((this: WebSocket, ev: Event) => any) | null;
5306+
/**
5307+
* Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation.
5308+
*/
52865309
readonly protocol: string;
5310+
/**
5311+
* Returns the state of the WebSocket object's connection. It can have the values described below.
5312+
*/
52875313
readonly readyState: number;
5314+
/**
5315+
* Returns the URL that was used to establish the WebSocket connection.
5316+
*/
52885317
readonly url: string;
5318+
/**
5319+
* Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.
5320+
*/
52895321
close(code?: number, reason?: string): void;
5322+
/**
5323+
* Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView.
5324+
*/
52905325
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
52915326
readonly CLOSED: number;
52925327
readonly CLOSING: number;

inputfiles/addedTypes.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,20 @@
22032203
}
22042204
]
22052205
}
2206+
},
2207+
"WebSocket": {
2208+
"events": {
2209+
"event": [
2210+
{
2211+
"name": "close",
2212+
"type": "CloseEvent"
2213+
},
2214+
{
2215+
"name": "error",
2216+
"type": "Event"
2217+
}
2218+
]
2219+
}
22062220
}
22072221
}
22082222
},
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"websocket": "Creates a new WebSocket object, immediately establishing the associated WebSocket connection.\n\nurl is a string giving the URL over which the connection is established. Only \"ws\" or \"wss\" schemes are allowed; others will cause a \"SyntaxError\" DOMException. URLs with fragments will also cause such an exception.\n\nprotocols is either a string or an array of strings. If it is a string, it is equivalent to an array consisting of just that string; if it is omitted, it is equivalent to the empty array. Each string in the array is a subprotocol name. The connection will only be established if the server reports that it has selected one of these subprotocols. The subprotocol names have to match the requirements for elements that comprise the value of Sec-WebSocket-Protocol fields as defined by The WebSocket protocol. [WSP]",
3+
"websocket-send": "Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView.",
4+
"websocket-close": "Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.",
5+
"websocket-url": "Returns the URL that was used to establish the WebSocket connection.",
6+
"websocket-readystate": "Returns the state of the WebSocket object's connection. It can have the values described below.",
7+
"websocket-bufferedamount": "Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.\n\nIf the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.)",
8+
"websocket-extensions": "Returns the extensions selected by the server, if any.",
9+
"websocket-protocol": "Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation.",
10+
"websocket-binarytype": "Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:\n\nCan be set, to change how binary data is returned. The default is \"blob\".",
11+
"closeevent-wasclean": "Returns true if the connection closed cleanly; false otherwise.",
12+
"closeevent-code": "Returns the WebSocket connection close code provided by the server.",
13+
"closeevent-reason": "Returns the WebSocket connection close reason provided by the server.",
14+
"binarytype-blob": "Binary data is returned in Blob form.",
15+
"binarytype-arraybuffer": "Binary data is returned in ArrayBuffer form."
16+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
enum BinaryType { "blob", "arraybuffer" };
2+
[Exposed=(Window,Worker)]
3+
interface WebSocket : EventTarget {
4+
constructor(USVString url, optional (DOMString or sequence<DOMString>) protocols = []);
5+
6+
readonly attribute USVString url;
7+
8+
// ready state
9+
const unsigned short CONNECTING = 0;
10+
const unsigned short OPEN = 1;
11+
const unsigned short CLOSING = 2;
12+
const unsigned short CLOSED = 3;
13+
readonly attribute unsigned short readyState;
14+
readonly attribute unsigned long long bufferedAmount;
15+
16+
// networking
17+
attribute EventHandler onopen;
18+
attribute EventHandler onerror;
19+
attribute EventHandler onclose;
20+
readonly attribute DOMString extensions;
21+
readonly attribute DOMString protocol;
22+
void close(optional [Clamp] unsigned short code, optional USVString reason);
23+
24+
// messaging
25+
attribute EventHandler onmessage;
26+
attribute BinaryType binaryType;
27+
void send(USVString data);
28+
void send(Blob data);
29+
void send(ArrayBuffer data);
30+
void send(ArrayBufferView data);
31+
};
32+
33+
[Exposed=(Window,Worker)]
34+
interface CloseEvent : Event {
35+
constructor(DOMString type, optional CloseEventInit eventInitDict = {});
36+
37+
readonly attribute boolean wasClean;
38+
readonly attribute unsigned short code;
39+
readonly attribute USVString reason;
40+
};
41+
42+
dictionary CloseEventInit : EventInit {
43+
boolean wasClean = false;
44+
unsigned short code = 0;
45+
USVString reason = "";
46+
};

inputfiles/idlSources.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@
322322
"url": "https://html.spec.whatwg.org/multipage/webappapis.html",
323323
"title": "HTML - Web application APIs"
324324
},
325+
{
326+
"url": "https://html.spec.whatwg.org/multipage/web-sockets.html",
327+
"title": "HTML - Web sockets"
328+
},
325329
{
326330
"url": "https://html.spec.whatwg.org/multipage/webstorage.html",
327331
"title": "HTML - Web storage"

0 commit comments

Comments
 (0)