Skip to content

Commit 9883a87

Browse files
committed
Fix websocket router types
It seems req was `any` before so now we have to handle the types. Also it seems the socket is of type `stream.Duplex`, not `net.Socket`. The ws types had to be updated to support the new type. Unfortunately Code still uses the old type so cast for now. In the web socket router just use a cast for the extra properties we add. We could add the types to the Express namespace but I am not sure we really want these commonly accessible so keep with the casts for now. Likely we should use Express's `locals` or something instead.
1 parent e05404a commit 9883a87

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@types/semver": "^7.1.0",
5050
"@types/split2": "^3.2.0",
5151
"@types/trusted-types": "^2.0.2",
52-
"@types/ws": "^8.0.0",
52+
"@types/ws": "^8.5.3",
5353
"@typescript-eslint/eslint-plugin": "^5.23.0",
5454
"@typescript-eslint/parser": "^5.23.0",
5555
"audit-ci": "^6.0.0",

src/node/routes/vscode.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export class CodeServerRouteWrapper {
127127

128128
private $proxyWebsocket = async (req: WebsocketRequest) => {
129129
const wrappedSocket = await this._socketProxyProvider.createProxy(req.ws)
130-
this._codeServerMain.handleUpgrade(req, wrappedSocket)
130+
// This should actually accept a duplex stream but it seems Code has not
131+
// been updated to match the Node 16 types so cast for now. There does not
132+
// appear to be any code specific to sockets so this should be fine.
133+
this._codeServerMain.handleUpgrade(req, wrappedSocket as net.Socket)
131134

132135
req.ws.resume()
133136
}

src/node/socket.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { promises as fs } from "fs"
22
import * as net from "net"
33
import * as path from "path"
4+
import * as stream from "stream"
45
import * as tls from "tls"
56
import { Emitter } from "../common/emitter"
67
import { generateUuid } from "../common/util"
@@ -27,10 +28,13 @@ export class SocketProxyProvider {
2728
}
2829

2930
/**
30-
* Create a socket proxy for TLS sockets. If it's not a TLS socket the
31-
* original socket is returned. This will spawn a proxy server on demand.
31+
* Create a socket proxy for TLS sockets. If it is not a TLS socket the
32+
* original socket or stream is returned. This will spawn a proxy server on
33+
* demand.
3234
*/
33-
public async createProxy(socket: net.Socket): Promise<net.Socket> {
35+
public async createProxy(socket: tls.TLSSocket | net.Socket): Promise<net.Socket>
36+
public async createProxy(socket: stream.Duplex): Promise<stream.Duplex>
37+
public async createProxy(socket: tls.TLSSocket | net.Socket | stream.Duplex): Promise<net.Socket | stream.Duplex> {
3438
if (!(socket instanceof tls.TLSSocket)) {
3539
return socket
3640
}

src/node/wsRouter.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ export const handleUpgrade = (app: express.Express, server: http.Server): void =
88
server.on("upgrade", (req, socket, head) => {
99
socket.pause()
1010

11-
req.ws = socket
12-
req.head = head
13-
req._ws_handled = false
11+
const wreq = req as InternalWebsocketRequest
12+
wreq.ws = socket
13+
wreq.head = head
14+
wreq._ws_handled = false
1415

1516
// Send the request off to be handled by Express.
16-
;(app as any).handle(req, new http.ServerResponse(req), () => {
17-
if (!req._ws_handled) {
17+
;(app as any).handle(wreq, new http.ServerResponse(wreq), () => {
18+
if (!wreq._ws_handled) {
1819
socket.end("HTTP/1.1 404 Not Found\r\n\r\n")
1920
}
2021
})

typings/pluginapi.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { field, Level, Logger } from "@coder/logger"
55
import * as express from "express"
66
import * as expressCore from "express-serve-static-core"
77
import ProxyServer from "http-proxy"
8-
import * as net from "net"
8+
import * as stream from "stream"
99
import Websocket from "ws"
1010

1111
/**
@@ -97,7 +97,7 @@ export declare class HttpError extends Error {
9797
}
9898

9999
export interface WebsocketRequest extends express.Request {
100-
ws: net.Socket
100+
ws: stream.Duplex
101101
head: Buffer
102102
}
103103

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,10 @@
500500
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e"
501501
integrity sha512-FvUupuM3rlRsRtCN+fDudtmytGO6iHJuuRKS1Ss0pG5z8oX0diNEw94UEL7hgDbpN94rgaK5R7sWm6RrSkZuAQ==
502502

503-
"@types/ws@^8.0.0":
504-
version "8.2.0"
505-
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.2.0.tgz#75faefbe2328f3b833cb8dc640658328990d04f3"
506-
integrity sha512-cyeefcUCgJlEk+hk2h3N+MqKKsPViQgF5boi9TTHSK+PoR9KWBb/C5ccPcDyAqgsbAYHTwulch725DV84+pSpg==
503+
"@types/ws@^8.5.3":
504+
version "8.5.3"
505+
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d"
506+
integrity sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==
507507
dependencies:
508508
"@types/node" "*"
509509

0 commit comments

Comments
 (0)