1
0

add websocket support

This commit is contained in:
Connor McKelvey
2022-02-18 20:33:01 -07:00
parent 2437f82e94
commit ebabaf5362
3 changed files with 19 additions and 7 deletions

View File

@@ -138,11 +138,12 @@ Key | Type | Description
### `res`-Object ### `res`-Object
Key | Type | Description Key | Type | Description
--------- | ------------------- | ----------- ----------- | ------------------- | -----------
`body` | `object` / `string` | Either set an `object` (will be converted to JSON) or a string `body` | `object` / `string` | Either set an `object` (will be converted to JSON) or a string
`headers` | `object` | Object you can set response headers in `headers` | `object` | Object you can set response headers in
`status` | `integer` | Return status code (default: `204`) `status` | `integer` | Return status code (default: `204`)
`webSocket` | `WebSocket` | Upgraded websocket connection
## Setup ## Setup

4
index.d.ts vendored
View File

@@ -302,6 +302,10 @@ type RouterResponse = {
body: { body: {
[key: string]: string [key: string]: string
} | string } | string
/**
* Upgraded websocket connection
*/
webSocket?: WebSocket
} }
/** /**
* Next Function * Next Function

View File

@@ -363,10 +363,17 @@ class Router {
if (res.raw) { if (res.raw) {
return res.raw return res.raw
} }
return new Response(res.body, {
const resInit = {
status: res.status || (res.body ? 200 : 204), status: res.status || (res.body ? 200 : 204),
headers: res.headers headers: res.headers
}) }
if (res.webSocket) {
resInit.webSocket = res.webSocket
}
return new Response(res.body, resInit)
} catch(err) { } catch(err) {
console.error(err) console.error(err)
return new Response(this.debugMode ? err.stack : '', { status: 500 }) return new Response(this.debugMode ? err.stack : '', { status: 500 })