From 4ee0c742332f1a2caa2c3747cd30b1a988a6ca4a Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 24 Nov 2019 12:28:27 +0900 Subject: [PATCH 1/2] Support new constructor() syntax --- src/widlprocess.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/widlprocess.ts b/src/widlprocess.ts index 7372dcf9f..004bdeffa 100644 --- a/src/widlprocess.ts +++ b/src/widlprocess.ts @@ -114,7 +114,7 @@ function convertInterfaceCommon(i: webidl2.InterfaceType | webidl2.InterfaceMixi methods: { method: {} }, "anonymous-methods": { method: [] }, properties: { property: {}, namesakes: {} }, - constructor: getConstructor(i.extAttrs, i.name), + constructor: getConstructor(i.members, i.name) || getOldStyleConstructor(i.extAttrs, i.name), "named-constructor": getNamedConstructor(i.extAttrs, i.name), exposed: getExtAttrConcatenated(i.extAttrs, "Exposed"), global: getExtAttrConcatenated(i.extAttrs, "Global"), @@ -173,7 +173,24 @@ function convertInterfaceCommon(i: webidl2.InterfaceType | webidl2.InterfaceMixi return result; } -function getConstructor(extAttrs: webidl2.ExtendedAttribute[], parent: string) { +function getConstructor(members: webidl2.IDLInterfaceMemberType[], parent: string) { + const constructor: Browser.Constructor = { + signature: [] + }; + for (const member of members) { + if (member.type === "constructor") { + constructor.signature.push({ + type: parent, + param: member.arguments.map(convertArgument) + }); + } + } + if (constructor.signature.length) { + return constructor; + } +} + +function getOldStyleConstructor(extAttrs: webidl2.ExtendedAttribute[], parent: string) { const constructor: Browser.Constructor = { signature: [] }; @@ -181,7 +198,7 @@ function getConstructor(extAttrs: webidl2.ExtendedAttribute[], parent: string) { if (extAttr.name === "Constructor") { constructor.signature.push({ type: parent, - param: extAttr.arguments ? extAttr.arguments.map(convertArgument) : [] + param: extAttr.arguments.map(convertArgument) }); } } From 4e2f8ee10255ba5cbd1e889ac9e7f36b3b078fd8 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 24 Nov 2019 13:06:27 +0900 Subject: [PATCH 2/2] Add HTML Web sockets types --- baselines/dom.generated.d.ts | 39 +++++++++++++++- baselines/webworker.generated.d.ts | 39 +++++++++++++++- inputfiles/addedTypes.json | 14 ++++++ .../idl/HTML - Web sockets.commentmap.json | 16 +++++++ inputfiles/idl/HTML - Web sockets.widl | 46 +++++++++++++++++++ inputfiles/idlSources.json | 4 ++ 6 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 inputfiles/idl/HTML - Web sockets.commentmap.json create mode 100644 inputfiles/idl/HTML - Web sockets.widl diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index cc5537b24..3b4f60c53 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -3639,11 +3639,18 @@ declare var ClipboardEvent: { /** 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. */ interface CloseEvent extends Event { + /** + * Returns the WebSocket connection close code provided by the server. + */ readonly code: number; + /** + * Returns the WebSocket connection close reason provided by the server. + */ readonly reason: string; + /** + * Returns true if the connection closed cleanly; false otherwise. + */ readonly wasClean: boolean; - /** @deprecated */ - initCloseEvent(typeArg: string, canBubbleArg: boolean, cancelableArg: boolean, wasCleanArg: boolean, codeArg: number, reasonArg: string): void; } declare var CloseEvent: { @@ -18385,17 +18392,45 @@ interface WebSocketEventMap { /** Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. */ interface WebSocket extends EventTarget { + /** + * Returns a string that indicates how binary data from the WebSocket object is exposed to scripts: + * + * Can be set, to change how binary data is returned. The default is "blob". + */ binaryType: BinaryType; + /** + * 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. + * + * 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.) + */ readonly bufferedAmount: number; + /** + * Returns the extensions selected by the server, if any. + */ readonly extensions: string; onclose: ((this: WebSocket, ev: CloseEvent) => any) | null; onerror: ((this: WebSocket, ev: Event) => any) | null; onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null; onopen: ((this: WebSocket, ev: Event) => any) | null; + /** + * 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. + */ readonly protocol: string; + /** + * Returns the state of the WebSocket object's connection. It can have the values described below. + */ readonly readyState: number; + /** + * Returns the URL that was used to establish the WebSocket connection. + */ readonly url: string; + /** + * Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason. + */ close(code?: number, reason?: string): void; + /** + * Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. + */ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void; readonly CLOSED: number; readonly CLOSING: number; diff --git a/baselines/webworker.generated.d.ts b/baselines/webworker.generated.d.ts index 111b94ad0..0f1f3a56a 100644 --- a/baselines/webworker.generated.d.ts +++ b/baselines/webworker.generated.d.ts @@ -893,11 +893,18 @@ declare var Clients: { /** 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. */ interface CloseEvent extends Event { + /** + * Returns the WebSocket connection close code provided by the server. + */ readonly code: number; + /** + * Returns the WebSocket connection close reason provided by the server. + */ readonly reason: string; + /** + * Returns true if the connection closed cleanly; false otherwise. + */ readonly wasClean: boolean; - /** @deprecated */ - initCloseEvent(typeArg: string, canBubbleArg: boolean, cancelableArg: boolean, wasCleanArg: boolean, codeArg: number, reasonArg: string): void; } declare var CloseEvent: { @@ -5269,17 +5276,45 @@ interface WebSocketEventMap { /** Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. */ interface WebSocket extends EventTarget { + /** + * Returns a string that indicates how binary data from the WebSocket object is exposed to scripts: + * + * Can be set, to change how binary data is returned. The default is "blob". + */ binaryType: BinaryType; + /** + * 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. + * + * 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.) + */ readonly bufferedAmount: number; + /** + * Returns the extensions selected by the server, if any. + */ readonly extensions: string; onclose: ((this: WebSocket, ev: CloseEvent) => any) | null; onerror: ((this: WebSocket, ev: Event) => any) | null; onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null; onopen: ((this: WebSocket, ev: Event) => any) | null; + /** + * 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. + */ readonly protocol: string; + /** + * Returns the state of the WebSocket object's connection. It can have the values described below. + */ readonly readyState: number; + /** + * Returns the URL that was used to establish the WebSocket connection. + */ readonly url: string; + /** + * Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason. + */ close(code?: number, reason?: string): void; + /** + * Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. + */ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void; readonly CLOSED: number; readonly CLOSING: number; diff --git a/inputfiles/addedTypes.json b/inputfiles/addedTypes.json index a6ce1c001..78f087408 100644 --- a/inputfiles/addedTypes.json +++ b/inputfiles/addedTypes.json @@ -2282,6 +2282,20 @@ } } } + }, + "WebSocket": { + "events": { + "event": [ + { + "name": "close", + "type": "CloseEvent" + }, + { + "name": "error", + "type": "Event" + } + ] + } } } }, diff --git a/inputfiles/idl/HTML - Web sockets.commentmap.json b/inputfiles/idl/HTML - Web sockets.commentmap.json new file mode 100644 index 000000000..64db3afc4 --- /dev/null +++ b/inputfiles/idl/HTML - Web sockets.commentmap.json @@ -0,0 +1,16 @@ +{ + "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]", + "websocket-send": "Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView.", + "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.", + "websocket-url": "Returns the URL that was used to establish the WebSocket connection.", + "websocket-readystate": "Returns the state of the WebSocket object's connection. It can have the values described below.", + "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.)", + "websocket-extensions": "Returns the extensions selected by the server, if any.", + "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.", + "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\".", + "closeevent-wasclean": "Returns true if the connection closed cleanly; false otherwise.", + "closeevent-code": "Returns the WebSocket connection close code provided by the server.", + "closeevent-reason": "Returns the WebSocket connection close reason provided by the server.", + "binarytype-blob": "Binary data is returned in Blob form.", + "binarytype-arraybuffer": "Binary data is returned in ArrayBuffer form." +} diff --git a/inputfiles/idl/HTML - Web sockets.widl b/inputfiles/idl/HTML - Web sockets.widl new file mode 100644 index 000000000..c629ce1f1 --- /dev/null +++ b/inputfiles/idl/HTML - Web sockets.widl @@ -0,0 +1,46 @@ +enum BinaryType { "blob", "arraybuffer" }; +[Exposed=(Window,Worker)] +interface WebSocket : EventTarget { + constructor(USVString url, optional (DOMString or sequence) protocols = []); + + readonly attribute USVString url; + + // ready state + const unsigned short CONNECTING = 0; + const unsigned short OPEN = 1; + const unsigned short CLOSING = 2; + const unsigned short CLOSED = 3; + readonly attribute unsigned short readyState; + readonly attribute unsigned long long bufferedAmount; + + // networking + attribute EventHandler onopen; + attribute EventHandler onerror; + attribute EventHandler onclose; + readonly attribute DOMString extensions; + readonly attribute DOMString protocol; + void close(optional [Clamp] unsigned short code, optional USVString reason); + + // messaging + attribute EventHandler onmessage; + attribute BinaryType binaryType; + void send(USVString data); + void send(Blob data); + void send(ArrayBuffer data); + void send(ArrayBufferView data); +}; + +[Exposed=(Window,Worker)] +interface CloseEvent : Event { + constructor(DOMString type, optional CloseEventInit eventInitDict = {}); + + readonly attribute boolean wasClean; + readonly attribute unsigned short code; + readonly attribute USVString reason; +}; + +dictionary CloseEventInit : EventInit { + boolean wasClean = false; + unsigned short code = 0; + USVString reason = ""; +}; diff --git a/inputfiles/idlSources.json b/inputfiles/idlSources.json index 3eee3c5fe..84eed7017 100644 --- a/inputfiles/idlSources.json +++ b/inputfiles/idlSources.json @@ -326,6 +326,10 @@ "url": "https://html.spec.whatwg.org/multipage/webappapis.html", "title": "HTML - Web application APIs" }, + { + "url": "https://html.spec.whatwg.org/multipage/web-sockets.html", + "title": "HTML - Web sockets" + }, { "url": "https://html.spec.whatwg.org/multipage/webstorage.html", "title": "HTML - Web storage"