Skip to content

Commit 5505959

Browse files
committed
Expose websocket server to plugins
Same reasoning used when exposing Express.
1 parent b13db31 commit 5505959

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/node/plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as pluginapi from "../../typings/pluginapi"
77
import { version } from "./constants"
88
import { proxy } from "./proxy"
99
import * as util from "./util"
10-
import { Router as WsRouter, WebsocketRouter } from "./wsRouter"
10+
import { Router as WsRouter, WebsocketRouter, wss } from "./wsRouter"
1111
const fsp = fs.promises
1212

1313
/**
@@ -24,6 +24,7 @@ require("module")._load = function (request: string, parent: object, isMain: boo
2424
Level,
2525
proxy,
2626
WsRouter,
27+
wss,
2728
}
2829
}
2930
return originalLoad.apply(this, [request, parent, isMain])

test/test-plugin/src/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as cs from "code-server"
22
import * as fspath from "path"
3-
import Websocket from "ws"
4-
5-
const wss = new Websocket.Server({ noServer: true })
63

74
export const plugin: cs.Plugin = {
85
displayName: "Test Plugin",
@@ -28,7 +25,7 @@ export const plugin: cs.Plugin = {
2825
wsRouter() {
2926
const wr = cs.WsRouter()
3027
wr.ws("/test-app", (req) => {
31-
wss.handleUpgrade(req, req.socket, req.head, (ws) => {
28+
cs.wss.handleUpgrade(req, req.socket, req.head, (ws) => {
3229
ws.send("hello")
3330
})
3431
})

typings/pluginapi.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as express from "express"
66
import * as expressCore from "express-serve-static-core"
77
import ProxyServer from "http-proxy"
88
import * as net from "net"
9+
import Websocket from "ws"
910

1011
/**
1112
* Overlay
@@ -102,6 +103,11 @@ export interface WebsocketRouter {
102103
*/
103104
export function WsRouter(): WebsocketRouter
104105

106+
/**
107+
* The websocket server used by code-server.
108+
*/
109+
export const wss: Websocket.Server
110+
105111
/**
106112
* The Express import used by code-server.
107113
*
@@ -110,7 +116,6 @@ export function WsRouter(): WebsocketRouter
110116
* instances.
111117
*/
112118
export { express }
113-
114119
/**
115120
* Use to add a field to a log.
116121
*

0 commit comments

Comments
 (0)