summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-03-13 06:09:27 +0100
committerRory& <root@rory.gay>2026-03-13 06:09:27 +0100
commit1e82f2f1f89bc099ea2d57796bc0d8bfcb954bde (patch)
tree55d0466ebeac5bfab2dadf8e199fd22ffdce80b9
parentadmin api: send SB_SESSION_REMOVE when deleting user (diff)
downloadserver-ts-1e82f2f1f89bc099ea2d57796bc0d8bfcb954bde.tar.xz
gateway: util function to do gateway offload stuff
-rw-r--r--src/gateway/util/Utils.ts36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/gateway/util/Utils.ts b/src/gateway/util/Utils.ts

index a629751b..74ec33cf 100644 --- a/src/gateway/util/Utils.ts +++ b/src/gateway/util/Utils.ts
@@ -1,4 +1,7 @@ -import { VoiceState } from "@spacebar/util"; +import { Event, VoiceState } from "@spacebar/util"; +import { WebSocket } from "./WebSocket"; +import { OPCODES } from "./Constants"; +import { Send } from "./Send"; export function parseStreamKey(streamKey: string): { type: "guild" | "call"; @@ -58,3 +61,34 @@ export async function cleanupOnStartup(): Promise<void> { .then((e) => console.log("[Gateway] Successfully cleaned voice states")) .catch((e) => console.error("[Gateway] Error cleaning voice states on startup:", e)); } + +export async function handleOffloadedGatewayRequest(socket: WebSocket, url: string, body: unknown) { + // TODO: async json object streaming + const resp = await fetch(url, { + body: JSON.stringify(body), + method: "POST", + headers: { + Authorization: `Bearer ${socket.accessToken}`, + // because the session may not have an id in the token! + "X-Session-Id": socket.session_id, + }, + }); + + if (!resp.ok) { + const text = await resp.text(); + console.error(`[Gateway] Offloaded request to ${url} failed with status ${resp.status}: ${text}`); + throw new Error(`Offloaded request failed with status ${resp.status}: ${text}`); + } + + const data = ((await resp.json()) as Event[]).toReversed(); + while (data.length > 0) { + const event = data.pop()!; + if (process.env.WS_VERBOSE) console.log(`[Gateway] Received offloaded event: ${JSON.stringify(event)}`); + await Send(socket, { + op: OPCODES.Dispatch, + s: socket.sequence++, + t: event.event, + d: event.data, + }); + } +}