chore: make ws self test work under bun #2268

Merged
Sainan merged 1 commits from bun-ws into main 2025-06-23 21:52:02 -07:00
Showing only changes of commit bac7104db6 - Show all commits

View File

@ -10,7 +10,7 @@ import { Account } from "../models/loginModel";
import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "./loginService";
import { IDatabaseAccountJson } from "../types/loginTypes";
import { HydratedDocument } from "mongoose";
import { Agent, WebSocket } from "undici";
import { Agent, WebSocket as UnidiciWebSocket } from "undici";
let httpServer: http.Server | undefined;
let httpsServer: https.Server | undefined;
@ -46,13 +46,9 @@ export const startWebServer = (): void => {
"Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
);
// https://github.com/oven-sh/bun/issues/20547
if (!process.versions.bun) {
void runWsSelfTest("wss", httpsPort).then(ok => {
if (!ok) {
logger.warn(
`WSS self-test failed. The server may not actually be reachable at port ${httpsPort}.`
);
logger.warn(`WSS self-test failed. The server may not actually be reachable at port ${httpsPort}.`);
if (process.platform == "win32") {
logger.warn(
`You can check who actually has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${httpsPort}).OwningProcess`
@ -60,21 +56,35 @@ export const startWebServer = (): void => {
}
}
});
}
});
});
};
const runWsSelfTest = (protocol: "ws" | "wss", port: number): Promise<boolean> => {
return new Promise(resolve => {
const agent = new Agent({ connect: { rejectUnauthorized: false } });
const client = new WebSocket(`${protocol}://localhost:${port}/custom/selftest`, { dispatcher: agent });
// https://github.com/oven-sh/bun/issues/20547
if (process.versions.bun) {
const client = new WebSocket(`${protocol}://localhost:${port}/custom/selftest`, {
tls: { rejectUnauthorized: false }
} as unknown as string);
client.onmessage = (e): void => {
resolve(e.data == "SpaceNinjaServer");
};
client.onerror = client.onclose = (): void => {
resolve(false);
};
} else {
const agent = new Agent({ connect: { rejectUnauthorized: false } });
const client = new UnidiciWebSocket(`${protocol}://localhost:${port}/custom/selftest`, {
dispatcher: agent
});
client.onmessage = (e): void => {
resolve(e.data == "SpaceNinjaServer");
};
client.onerror = client.onclose = (): void => {
resolve(false);
};
}
});
};