Skip to content

Commit 5ccf596

Browse files
committed
Expose websocket server to plugins
Same reasoning used when exposing Express.
1 parent d3c3405 commit 5ccf596

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/node/plugin.ts

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 6 additions & 1 deletion
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
@@ -101,6 +102,11 @@ export interface WebsocketRouter {
101102
*/
102103
export function WsRouter(): WebsocketRouter
103104

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

0 commit comments

Comments
 (0)