Skip to content

Commit 51a48ba

Browse files
committed
Standardize disposals
1 parent 0cfd245 commit 51a48ba

File tree

8 files changed

+18
-38
lines changed

8 files changed

+18
-38
lines changed

src/node/main.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ export const runCodeServer = async (
157157
}
158158

159159
let linkAgent: undefined | ChildProcessWithoutNullStreams
160-
161160
try {
162161
linkAgent = startLink(serverAddress)
163162
linkAgent.on("error", (error) => {
@@ -194,13 +193,11 @@ export const runCodeServer = async (
194193
}
195194
}
196195

197-
const dispose = () => {
198-
disposeApp()
199-
linkAgent?.kill()
200-
}
201-
202196
return {
203197
server,
204-
dispose,
198+
dispose: () => {
199+
disposeApp()
200+
linkAgent?.kill()
201+
},
205202
}
206203
}

src/node/routes/index.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { Heart } from "../heart"
1515
import { ensureAuthenticated, redirect } from "../http"
1616
import { PluginAPI } from "../plugin"
1717
import { getMediaMime, paths } from "../util"
18-
import { wrapper } from "../wrapper"
1918
import * as apps from "./apps"
2019
import * as domainProxy from "./domainProxy"
2120
import { errorHandler, wsErrorHandler } from "./errors"
@@ -46,9 +45,6 @@ export const register = async (
4645
})
4746
})
4847
})
49-
server.on("close", () => {
50-
heart.dispose()
51-
})
5248

5349
app.disable("x-powered-by")
5450
wsApp.disable("x-powered-by")
@@ -114,13 +110,13 @@ export const register = async (
114110
})
115111
})
116112

113+
let pluginApi: PluginAPI
117114
if (!process.env.CS_DISABLE_PLUGINS) {
118115
const workingDir = args._ && args._.length > 0 ? path.resolve(args._[args._.length - 1]) : undefined
119-
const pluginApi = new PluginAPI(logger, process.env.CS_PLUGIN, process.env.CS_PLUGIN_PATH, workingDir)
116+
pluginApi = new PluginAPI(logger, process.env.CS_PLUGIN, process.env.CS_PLUGIN_PATH, workingDir)
120117
await pluginApi.loadPlugins()
121118
pluginApi.mount(app, wsApp)
122119
app.use("/api/applications", ensureAuthenticated, apps.router(pluginApi))
123-
wrapper.onDispose(() => pluginApi.dispose())
124120
}
125121

126122
app.use(express.json())
@@ -170,6 +166,8 @@ export const register = async (
170166
wsApp.use(wsErrorHandler)
171167

172168
return () => {
169+
heart.dispose()
170+
pluginApi?.dispose()
173171
vscode?.codeServerMain.dispose()
174172
}
175173
}

test/unit/node/plugin.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe("plugin", () => {
5858
})
5959

6060
afterAll(async () => {
61-
await s.close()
61+
await s.dispose()
6262
})
6363

6464
it("/api/applications", async () => {

test/unit/node/proxy.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("proxy", () => {
2424
})
2525

2626
afterAll(async () => {
27-
await nhooyrDevServer.close()
27+
await nhooyrDevServer.dispose()
2828
})
2929

3030
beforeEach(() => {
@@ -33,7 +33,7 @@ describe("proxy", () => {
3333

3434
afterEach(async () => {
3535
if (codeServer) {
36-
await codeServer.close()
36+
await codeServer.dispose()
3737
codeServer = undefined
3838
}
3939
})

test/unit/node/routes/health.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe("health", () => {
66

77
afterEach(async () => {
88
if (codeServer) {
9-
await codeServer.close()
9+
await codeServer.dispose()
1010
codeServer = undefined
1111
}
1212
})

test/unit/node/routes/login.test.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,11 @@ describe("login", () => {
5858
afterEach(async () => {
5959
process.env.PASSWORD = previousEnvPassword
6060
if (_codeServer) {
61-
await _codeServer.close()
61+
await _codeServer.dispose()
6262
_codeServer = undefined
6363
}
6464
})
6565

66-
afterAll(async () => {
67-
if (_codeServer) {
68-
_codeServer.dispose()
69-
}
70-
})
71-
7266
it("should return HTML with 'Missing password' message", async () => {
7367
const resp = await codeServer().fetch("/login", { method: "POST" })
7468

test/unit/node/routes/static.test.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,11 @@ describe("/_static", () => {
3333

3434
afterEach(async () => {
3535
if (_codeServer) {
36-
await _codeServer.close()
36+
await _codeServer.dispose()
3737
_codeServer = undefined
3838
}
3939
})
4040

41-
afterAll(async () => {
42-
if (_codeServer) {
43-
_codeServer.dispose()
44-
}
45-
})
46-
4741
function commonTests() {
4842
it("should return a 404 when a file is not provided", async () => {
4943
const resp = await codeServer().fetch(`/_static/`)

test/utils/httpserver.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export class HttpServer {
6060
}
6161

6262
/**
63-
* close cleans up the server.
63+
* Clean up the server.
6464
*/
65-
public close(): Promise<void> {
65+
public dispose(): Promise<void> {
6666
return new Promise((resolve, reject) => {
6767
// Close will not actually close anything; it just waits until everything
6868
// is closed.
@@ -74,6 +74,8 @@ export class HttpServer {
7474
resolve()
7575
})
7676

77+
this._dispose?.()
78+
7779
// If there are sockets remaining we might need to force close them or
7880
// this promise might never resolve.
7981
if (this.sockets.size > 0) {
@@ -90,11 +92,6 @@ export class HttpServer {
9092
})
9193
}
9294

93-
public dispose() {
94-
console.log("Disposing HTTP Server")
95-
this._dispose?.()
96-
}
97-
9895
/**
9996
* fetch fetches the request path.
10097
* The request path must be rooted!

0 commit comments

Comments
 (0)