Move websocket routes into a separate app

This is mostly so we don't have to do any wacky patching but it also
makes it so we don't have to keep checking if the request is a web
socket request every time we add middleware.
This commit is contained in:
Asher
2020-11-05 12:58:37 -06:00
parent 9e09c1f92b
commit 7b2752a62c
8 changed files with 134 additions and 145 deletions

View File

@ -1,8 +1,6 @@
import { field, logger } from "@coder/logger"
import * as express from "express"
import * as expressCore from "express-serve-static-core"
import * as http from "http"
import * as net from "net"
import qs from "qs"
import safeCompare from "safe-compare"
import { HttpCode, HttpError } from "../common/http"
@ -135,111 +133,3 @@ export const getCookieDomain = (host: string, proxyDomains: string[]): string |
logger.debug("got cookie doman", field("host", host))
return host || undefined
}
declare module "express" {
function Router(options?: express.RouterOptions): express.Router & WithWebsocketMethod
type WebSocketRequestHandler = (
req: express.Request & WithWebSocket,
res: express.Response,
next: express.NextFunction,
) => void | Promise<void>
type WebSocketMethod<T> = (route: expressCore.PathParams, ...handlers: WebSocketRequestHandler[]) => T
interface WithWebSocket {
ws: net.Socket
head: Buffer
}
interface WithWebsocketMethod {
ws: WebSocketMethod<this>
}
}
interface WebsocketRequest extends express.Request, express.WithWebSocket {
_ws_handled: boolean
}
function isWebSocketRequest(req: express.Request): req is WebsocketRequest {
return !!(req as WebsocketRequest).ws
}
export const handleUpgrade = (app: express.Express, server: http.Server): void => {
server.on("upgrade", (req, socket, head) => {
socket.on("error", () => socket.destroy())
req.ws = socket
req.head = head
req._ws_handled = false
const res = new http.ServerResponse(req)
res.writeHead = function writeHead(statusCode: number) {
if (statusCode > 200) {
socket.destroy(new Error(`${statusCode}`))
}
return res
}
// Send the request off to be handled by Express.
;(app as any).handle(req, res, () => {
if (!req._ws_handled) {
socket.destroy(new Error("Not found"))
}
})
})
}
/**
* Patch Express routers to handle web sockets.
*
* Not using express-ws since the ws-wrapped sockets don't work with the proxy.
*/
function patchRouter(): void {
// This works because Router is also the prototype assigned to the routers it
// returns.
// Store this since the original method will be overridden.
const originalGet = (express.Router as any).prototype.get
// Inject the `ws` method.
;(express.Router as any).prototype.ws = function ws(
route: expressCore.PathParams,
...handlers: express.WebSocketRequestHandler[]
) {
originalGet.apply(this, [
route,
...handlers.map((handler) => {
const wrapped: express.Handler = (req, res, next) => {
if (isWebSocketRequest(req)) {
req._ws_handled = true
return handler(req, res, next)
}
next()
}
return wrapped
}),
])
return this
}
// Overwrite `get` so we can distinguish between websocket and non-websocket
// routes.
;(express.Router as any).prototype.get = function get(route: expressCore.PathParams, ...handlers: express.Handler[]) {
originalGet.apply(this, [
route,
...handlers.map((handler) => {
const wrapped: express.Handler = (req, res, next) => {
if (!isWebSocketRequest(req)) {
return handler(req, res, next)
}
next()
}
return wrapped
}),
])
return this
}
}
// This needs to happen before anything creates a router.
patchRouter()