Skip to content

Commit

Permalink
feat: stabilise Deno.upgradeWebSocket (denoland#12024)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Sep 13, 2021
1 parent 3ef23e2 commit a95ca9d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
46 changes: 46 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
47 changes: 0 additions & 47 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
Permissions: __bootstrap.permissions.Permissions,
PermissionStatus: __bootstrap.permissions.PermissionStatus,
serveHttp: __bootstrap.http.serveHttp,
upgradeWebSocket: __bootstrap.http.upgradeWebSocket,
};

__bootstrap.denoNsUnstable = {
Expand All @@ -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,
Expand Down

0 comments on commit a95ca9d

Please sign in to comment.