1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

feat: stabilise Deno.upgradeWebSocket (#12024)

This commit is contained in:
Bartek Iwańczuk 2021-09-13 14:06:12 +02:00 committed by GitHub
parent 3ef23e25c6
commit a95ca9dc70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 48 deletions

View file

@ -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;
}

View file

@ -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 =

View file

@ -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,