Enforce backend WS timeout + subprotocol logic + formatting

This commit is contained in:
PoleTransformer 2025-12-31 09:51:49 -08:00
parent 307c208518
commit 8594bc81b3
3 changed files with 7 additions and 6 deletions

View File

@ -36,7 +36,7 @@ class WebSocketMonitorType extends MonitorType {
if (checkStatusCode(code, JSON.parse(monitor.accepted_statuscodes_json))) {
heartbeat.status = UP;
heartbeat.msg = message;
return; //success at this point
return; // success at this point
}
// Throw an error using friendly name if defined, fallback to generic msg
@ -57,9 +57,10 @@ class WebSocketMonitorType extends MonitorType {
*/
async attemptUpgrade(monitor) {
return new Promise((resolve) => {
let ws;
const timeoutMs = (monitor.timeout ?? 20) * 1000;
// If user inputs subprotocol(s), convert to array, set Sec-WebSocket-Protocol header, timeout in ms. Subprotocol Identifier column: https://www.iana.org/assignments/websocket/websocket.xml#subprotocol-name
ws = !monitor.wsSubprotocol ? new WebSocket(monitor.url, { handshakeTimeout: (monitor?.timeout ?? 0) * 1000 }) : new WebSocket(monitor.url, monitor.wsSubprotocol.replace(/\s/g, "").split(","), { handshakeTimeout: (monitor?.timeout ?? 0) * 1000 });
const subprotocol = monitor.wsSubprotocol ? monitor.wsSubprotocol.replace(/\s/g, "").split(",") : undefined;
const ws = new WebSocket(monitor.url, subprotocol, { handshakeTimeout: timeoutMs });
ws.addEventListener("open", (event) => {
// Immediately close the connection

View File

@ -848,7 +848,7 @@
</div>
<i18n-t tag="div" class="form-text" keypath="wsCodeDescription">
<template #documentation>
<a href="https://datatracker.ietf.org/doc/html/rfc6455#section-7.4" target="_blank" rel="noopener noreferrer">{{ 'RFC6455' }}</a>
<a href="https://datatracker.ietf.org/doc/html/rfc6455#section-7.4" target="_blank" rel="noopener noreferrer">RFC 6455</a>
</template>
</i18n-t>
</div>

View File

@ -230,7 +230,7 @@ describe("Websocket Test", {
);
});
test("Secure WS no support one subprotocol", async () => {
test("Secure WS no subprotocol support", async () => {
const websocketMonitor = new WebSocketMonitorType();
const monitor = {
@ -305,7 +305,7 @@ describe("Websocket Test", {
assert.deepStrictEqual(heartbeat, expected);
});
test("Insecure WS support one subprotocol", async (t) => {
test("Insecure WS supports one subprotocol", async (t) => {
t.after(() => wss.close());
const websocketMonitor = new WebSocketMonitorType();
const wss = new WebSocketServer({ port: 8080,