返回提交历史
Modified
src/services/webService.ts
+31
-21
XFEstudio/SpaceNinjaServer
chore: make ws self test work under bun (#2268)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2268 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
f242d9f8
代码差异
1 个文件
+31
-21
@@ -10,7 +10,7 @@ import { Account } from "../models/loginModel";
10
10
import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "./loginService";
11
11
import { IDatabaseAccountJson } from "../types/loginTypes";
12
12
import { HydratedDocument } from "mongoose";
13
import { Agent, WebSocket } from "undici";
13
import { Agent, WebSocket as UnidiciWebSocket } from "undici";
14
14
15
15
let httpServer: http.Server | undefined;
16
16
let httpsServer: https.Server | undefined;
@@ -46,35 +46,45 @@ export const startWebServer = (): void => {
46
46
"Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
47
47
);
48
48
49
// https://github.com/oven-sh/bun/issues/20547
50
if (!process.versions.bun) {
51
void runWsSelfTest("wss", httpsPort).then(ok => {
52
if (!ok) {
49
void runWsSelfTest("wss", httpsPort).then(ok => {
50
if (!ok) {
51
logger.warn(`WSS self-test failed. The server may not actually be reachable at port ${httpsPort}.`);
52
if (process.platform == "win32") {
53
53
logger.warn(
54
`WSS self-test failed. The server may not actually be reachable at port ${httpsPort}.`
54
`You can check who actually has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${httpsPort}).OwningProcess`
55
55
);
56
if (process.platform == "win32") {
57
logger.warn(
58
`You can check who actually has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${httpsPort}).OwningProcess`
59
);
60
}
61
56
}
62
});
63
}
57
}
58
});
64
59
});
65
60
});
66
61
};
67
62
68
63
const runWsSelfTest = (protocol: "ws" | "wss", port: number): Promise<boolean> => {
69
64
return new Promise(resolve => {
70
const agent = new Agent({ connect: { rejectUnauthorized: false } });
71
const client = new WebSocket(`${protocol}://localhost:${port}/custom/selftest`, { dispatcher: agent });
72
client.onmessage = (e): void => {
73
resolve(e.data == "SpaceNinjaServer");
74
};
75
client.onerror = client.onclose = (): void => {
76
resolve(false);
77
};
65
// https://github.com/oven-sh/bun/issues/20547
66
if (process.versions.bun) {
67
const client = new WebSocket(`${protocol}://localhost:${port}/custom/selftest`, {
68
tls: { rejectUnauthorized: false }
69
} as unknown as string);
70
client.onmessage = (e): void => {
71
resolve(e.data == "SpaceNinjaServer");
72
};
73
client.onerror = client.onclose = (): void => {
74
resolve(false);
75
};
76
} else {
77
const agent = new Agent({ connect: { rejectUnauthorized: false } });
78
const client = new UnidiciWebSocket(`${protocol}://localhost:${port}/custom/selftest`, {
79
dispatcher: agent
80
});
81
client.onmessage = (e): void => {
82
resolve(e.data == "SpaceNinjaServer");
83
};
84
client.onerror = client.onclose = (): void => {
85
resolve(false);
86
};
87
}
78
88
});
79
89
};
80
90