mirror of
https://github.com/spacebarchat/server.git
synced 2026-03-30 20:25:40 +00:00
gateway: util function to do gateway offload stuff
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user