mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
feat: stabilise Deno.upgradeWebSocket (#12024)
This commit is contained in:
parent
3ef23e25c6
commit
a95ca9dc70
3 changed files with 47 additions and 48 deletions
46
cli/dts/lib.deno.ns.d.ts
vendored
46
cli/dts/lib.deno.ns.d.ts
vendored
|
@ -2454,4 +2454,50 @@ declare namespace Deno {
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function serveHttp(conn: Conn): HttpConn;
|
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
cli/dts/lib.deno.unstable.d.ts
vendored
47
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -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.
|
/** The type of the resource record.
|
||||||
* Only the listed types are supported currently. */
|
* Only the listed types are supported currently. */
|
||||||
export type RecordType =
|
export type RecordType =
|
||||||
|
|
|
@ -102,6 +102,7 @@
|
||||||
Permissions: __bootstrap.permissions.Permissions,
|
Permissions: __bootstrap.permissions.Permissions,
|
||||||
PermissionStatus: __bootstrap.permissions.PermissionStatus,
|
PermissionStatus: __bootstrap.permissions.PermissionStatus,
|
||||||
serveHttp: __bootstrap.http.serveHttp,
|
serveHttp: __bootstrap.http.serveHttp,
|
||||||
|
upgradeWebSocket: __bootstrap.http.upgradeWebSocket,
|
||||||
};
|
};
|
||||||
|
|
||||||
__bootstrap.denoNsUnstable = {
|
__bootstrap.denoNsUnstable = {
|
||||||
|
@ -126,7 +127,6 @@
|
||||||
listenDatagram: __bootstrap.netUnstable.listenDatagram,
|
listenDatagram: __bootstrap.netUnstable.listenDatagram,
|
||||||
startTls: __bootstrap.tls.startTls,
|
startTls: __bootstrap.tls.startTls,
|
||||||
umask: __bootstrap.fs.umask,
|
umask: __bootstrap.fs.umask,
|
||||||
upgradeWebSocket: __bootstrap.http.upgradeWebSocket,
|
|
||||||
futime: __bootstrap.fs.futime,
|
futime: __bootstrap.fs.futime,
|
||||||
futimeSync: __bootstrap.fs.futimeSync,
|
futimeSync: __bootstrap.fs.futimeSync,
|
||||||
utime: __bootstrap.fs.utime,
|
utime: __bootstrap.fs.utime,
|
||||||
|
|
Loading…
Reference in a new issue