From a95ca9dc70515240b1be7a12dbf686b1ebfb3490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 13 Sep 2021 14:06:12 +0200 Subject: [PATCH] feat: stabilise Deno.upgradeWebSocket (#12024) --- cli/dts/lib.deno.ns.d.ts | 46 +++++++++++++++++++++++++++++++++ cli/dts/lib.deno.unstable.d.ts | 47 ---------------------------------- runtime/js/90_deno_ns.js | 2 +- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index b8126f4cf7..5b6849bfd8 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -2454,4 +2454,50 @@ declare namespace Deno { * ``` */ export function serveHttp(conn: Conn): HttpConn; + + export interface WebSocketUpgrade { + response: Response; + socket: WebSocket; + } + + export interface UpgradeWebSocketOptions { + protocol?: string; + } + + /** + * Used to upgrade an incoming HTTP request to a WebSocket. + * + * Given a request, returns a pair of WebSocket and Response. The original + * request must be responded to with the returned response for the websocket + * upgrade to be successful. + * + * ```ts + * const conn = await Deno.connect({ port: 80, hostname: "127.0.0.1" }); + * const httpConn = Deno.serveHttp(conn); + * const e = await httpConn.nextRequest(); + * if (e) { + * const { socket, response } = Deno.upgradeWebSocket(e.request); + * socket.onopen = () => { + * socket.send("Hello World!"); + * }; + * socket.onmessage = (e) => { + * console.log(e.data); + * socket.close(); + * }; + * socket.onclose = () => console.log("WebSocket has been closed."); + * socket.onerror = (e) => console.error("WebSocket error:", e); + * e.respondWith(response); + * } + * ``` + * + * If the request body is disturbed (read from) before the upgrade is + * completed, upgrading fails. + * + * This operation does not yet consume the request or open the websocket. This + * only happens once the returned response has been passed to `respondWith`. + */ + export function upgradeWebSocket( + request: Request, + options?: UpgradeWebSocketOptions, + ): WebSocketUpgrade; } diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index 82ab9f5a19..5854a51812 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -1003,53 +1003,6 @@ declare namespace Deno { }; } - export interface WebSocketUpgrade { - response: Response; - socket: WebSocket; - } - - export interface UpgradeWebSocketOptions { - protocol?: string; - } - - /** **UNSTABLE**: new API, yet to be vetted. - * - * Used to upgrade an incoming HTTP request to a WebSocket. - * - * Given a request, returns a pair of WebSocket and Response. The original - * request must be responded to with the returned response for the websocket - * upgrade to be successful. - * - * ```ts - * const conn = await Deno.connect({ port: 80, hostname: "127.0.0.1" }); - * const httpConn = Deno.serveHttp(conn); - * const e = await httpConn.nextRequest(); - * if (e) { - * const { socket, response } = Deno.upgradeWebSocket(e.request); - * socket.onopen = () => { - * socket.send("Hello World!"); - * }; - * socket.onmessage = (e) => { - * console.log(e.data); - * socket.close(); - * }; - * socket.onclose = () => console.log("WebSocket has been closed."); - * socket.onerror = (e) => console.error("WebSocket error:", e); - * e.respondWith(response); - * } - * ``` - * - * If the request body is disturbed (read from) before the upgrade is - * completed, upgrading fails. - * - * This operation does not yet consume the request or open the websocket. This - * only happens once the returned response has been passed to `respondWith`. - */ - export function upgradeWebSocket( - request: Request, - options?: UpgradeWebSocketOptions, - ): WebSocketUpgrade; - /** The type of the resource record. * Only the listed types are supported currently. */ export type RecordType = diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index 8f1fe89f57..177a0dea08 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -102,6 +102,7 @@ Permissions: __bootstrap.permissions.Permissions, PermissionStatus: __bootstrap.permissions.PermissionStatus, serveHttp: __bootstrap.http.serveHttp, + upgradeWebSocket: __bootstrap.http.upgradeWebSocket, }; __bootstrap.denoNsUnstable = { @@ -126,7 +127,6 @@ listenDatagram: __bootstrap.netUnstable.listenDatagram, startTls: __bootstrap.tls.startTls, umask: __bootstrap.fs.umask, - upgradeWebSocket: __bootstrap.http.upgradeWebSocket, futime: __bootstrap.fs.futime, futimeSync: __bootstrap.fs.futimeSync, utime: __bootstrap.fs.utime,